aiden-runtime 4.1.2 → 4.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -19,24 +19,84 @@
19
19
  */
20
20
  Object.defineProperty(exports, "__esModule", { value: true });
21
21
  exports.appLaunchTool = void 0;
22
+ exports.processNameFromApp = processNameFromApp;
22
23
  const _psHelpers_1 = require("./_psHelpers");
24
+ /**
25
+ * Derive the bare process-name we expect `Get-Process` to find after
26
+ * launch. Strips path components, lowercases, drops the `.exe` extension.
27
+ * Used by the v4.1.3-essentials launch-verification poll.
28
+ *
29
+ * "C:\\Program Files\\Spotify\\Spotify.exe" → "spotify"
30
+ * "Spotify.exe" → "spotify"
31
+ * "spotify" → "spotify"
32
+ * "notepad++.exe" → "notepad++"
33
+ *
34
+ * Pure helper, exported for unit testing.
35
+ */
36
+ function processNameFromApp(app) {
37
+ // Strip path components (Windows uses \; tolerate / too).
38
+ let bare = app.replace(/\\/g, '/').split('/').pop() ?? app;
39
+ // Drop a single trailing .exe (case-insensitive).
40
+ bare = bare.replace(/\.exe$/i, '');
41
+ return bare.toLowerCase();
42
+ }
23
43
  function buildPs(appName, args) {
24
44
  // Single-quote escape the app name for PowerShell.
25
45
  const safeApp = appName.replace(/'/g, "''");
46
+ // The Get-Process verification probe uses the bare process name
47
+ // (no path, no .exe). Compute it once on the TS side so the PS
48
+ // script doesn't have to do string surgery.
49
+ const procName = processNameFromApp(appName).replace(/'/g, "''");
26
50
  const argString = args && args.length > 0
27
51
  ? `-ArgumentList @(${args.map((a) => `'${a.replace(/'/g, "''")}'`).join(',')})`
28
52
  : '';
53
+ // v4.1.3-essentials launch reliability fix:
54
+ //
55
+ // Primary path: `Start-Process -PassThru` — captures PID for any
56
+ // traditional Win32 exe. Fails for UWP / Microsoft Store apps
57
+ // (Spotify is UWP on most systems) because UWP launches route
58
+ // through ShellExecute which doesn't yield a child-process handle
59
+ // for `-PassThru`.
60
+ //
61
+ // Fallback path: `[System.Diagnostics.Process]::Start($app)` — the
62
+ // direct .NET ShellExecute call. Same App Paths / shell-association
63
+ // resolution as cmd's `start` builtin, but with proper error
64
+ // propagation (Windows popup → .NET exception → PS throw → tool
65
+ // returns success:false) and no quoting hell.
66
+ //
67
+ // Verification: after either path lands "PID=unknown", sleep 300ms
68
+ // and probe `Get-Process` for the bare process name. If the process
69
+ // exists, capture its PID — the launch verifiably succeeded. If not,
70
+ // signal "launched but no matching process appeared" so the tool can
71
+ // surface `success:false` honestly instead of pretending it worked.
29
72
  return [
73
+ `$ErrorActionPreference = 'Stop';`,
74
+ `$pid_out = $null;`,
30
75
  `try {`,
31
- ` $p = Start-Process '${safeApp}' ${argString} -PassThru -ErrorAction Stop;`,
32
- ` Write-Output ('PID=' + $p.Id);`,
76
+ ` $p = Start-Process '${safeApp}' ${argString} -PassThru;`,
77
+ ` if ($p -and $p.Id) { $pid_out = $p.Id }`,
33
78
  `} catch {`,
34
- // App Paths registry resolution fallback: Start-Process sometimes
35
- // fails on bare names that Windows would otherwise resolve via
36
- // App Paths (Spotify, Chrome). Fall back to `start <name>` which
37
- // honours App Paths.
38
- ` cmd /c "start '' '${safeApp}'";`,
39
- ` Write-Output 'PID=unknown (launched via cmd start fallback)';`,
79
+ ` try {`,
80
+ ` $p = [System.Diagnostics.Process]::Start('${safeApp}');`,
81
+ ` if ($p -and $p.Id) { $pid_out = $p.Id }`,
82
+ ` } catch {`,
83
+ ` Write-Output ('LAUNCH_FAILED=' + $_.Exception.Message);`,
84
+ ` return;`,
85
+ ` }`,
86
+ `}`,
87
+ // If we got a PID from either Start-Process or .NET Process.Start,
88
+ // we're done — emit it and exit.
89
+ `if ($pid_out) { Write-Output ('PID=' + $pid_out); return };`,
90
+ // Otherwise (UWP path, both layers returned null) verify via
91
+ // Get-Process. 300ms grace; enough for Windows shell to either
92
+ // launch the app or surface the "cannot find" popup.
93
+ `Start-Sleep -Milliseconds 300;`,
94
+ `$found = Get-Process -Name '${procName}' -ErrorAction SilentlyContinue ` +
95
+ `| Select-Object -First 1;`,
96
+ `if ($found) {`,
97
+ ` Write-Output ('PID=' + $found.Id + ' (verified via Get-Process)');`,
98
+ `} else {`,
99
+ ` Write-Output ('LAUNCH_UNVERIFIED=' + '${procName}');`,
40
100
  `}`,
41
101
  ].join(' ');
42
102
  }
@@ -77,10 +137,76 @@ exports.appLaunchTool = {
77
137
  timeoutMs: 20000,
78
138
  });
79
139
  const out = stdout.trim();
80
- // Extract PID when Start-Process succeeded; null for cmd-fallback path.
140
+ // v4.1.3-essentials: the PS script emits exactly ONE of three
141
+ // outcomes. Parse in order of confidence:
142
+ // 1. `LAUNCH_FAILED=<message>` → .NET Process.Start threw;
143
+ // the popup-error class is here.
144
+ // 2. `LAUNCH_UNVERIFIED=<name>` → ShellExecute returned but
145
+ // no matching process appeared
146
+ // within 300ms — silently broken.
147
+ // 3. `PID=<n>` (optional `(verified via Get-Process)` suffix) →
148
+ // verified launch with PID.
149
+ //
150
+ // Outcomes 1 and 2 return `success:false` so the model + user see
151
+ // the honest failure instead of a "launched" lie. Outcome 3 still
152
+ // sets `degraded:true` for the case where Start-Process succeeded
153
+ // but the app might still crash post-init (Spotify "boots" for 21s
154
+ // before stable state) — caller verifies via `os_process_list`.
155
+ const launchFailedMatch = out.match(/LAUNCH_FAILED=(.+)$/m);
156
+ if (launchFailedMatch) {
157
+ return {
158
+ success: false,
159
+ app,
160
+ raw: out,
161
+ error: `Could not launch '${app}': ${launchFailedMatch[1].trim()}. ` +
162
+ `Verify the app is installed and resolvable via App Paths or PATH.`,
163
+ };
164
+ }
165
+ const launchUnverifiedMatch = out.match(/LAUNCH_UNVERIFIED=(.+)$/m);
166
+ if (launchUnverifiedMatch) {
167
+ return {
168
+ success: false,
169
+ app,
170
+ raw: out,
171
+ error: `Launch attempted but no process named '${launchUnverifiedMatch[1].trim()}' ` +
172
+ `appeared within 300ms. Windows may have shown an error dialog, ` +
173
+ `or the app failed to start. Try \`os_process_list\` with a ` +
174
+ `name filter to confirm, or pass an absolute path.`,
175
+ };
176
+ }
177
+ // Extract PID — both bare `PID=12345` and the verified
178
+ // `PID=12345 (verified via Get-Process)` shapes parse the same.
81
179
  const pidMatch = out.match(/PID=(\d+)/);
82
180
  const pid = pidMatch ? Number(pidMatch[1]) : null;
83
- return { success: true, app, pid, raw: out };
181
+ const verified = /verified via Get-Process/.test(out);
182
+ if (pid === null) {
183
+ // Shouldn't happen — the PS script always emits one of the
184
+ // three outcome lines. Surface honestly so the model sees the
185
+ // unexpected stdout instead of pretending success.
186
+ return {
187
+ success: false,
188
+ app,
189
+ raw: out,
190
+ error: `Launch returned unexpected stdout (no PID / failure sentinel). ` +
191
+ `Output: ${out.slice(0, 200)}`,
192
+ };
193
+ }
194
+ // Verified launch — still degraded because the app may crash
195
+ // post-init or split into a different process tree (Chrome's
196
+ // multi-process model, Spotify's spawn-and-detach). The honest
197
+ // signal is "we have a PID we can hand off; verify via
198
+ // os_process_list before relying on it".
199
+ return {
200
+ success: true,
201
+ app,
202
+ pid,
203
+ verified,
204
+ raw: out,
205
+ degraded: true,
206
+ degradedReason: verified
207
+ ? `launched (PID ${pid}, verified via Get-Process); call os_process_list to confirm it's still alive`
208
+ : `launched (PID ${pid}); call os_process_list to confirm it's still alive`,
209
+ };
84
210
  }
85
211
  catch (e) {
86
212
  return {
@@ -30,7 +30,13 @@ const ACTION_KEYS = {
30
30
  exports.mediaKeyTool = {
31
31
  schema: {
32
32
  name: 'media_key',
33
- description: 'Send a media-control key to the active media session (Spotify, YouTube, etc.). Pair with `now_playing` to inspect current state. Windows-only in v4.1.2.',
33
+ description: 'FALLBACK ONLY prefer `media_transport(action, target)` for verified ' +
34
+ 'control of named apps (Spotify, YouTube, etc.). Use `media_key` only ' +
35
+ 'when (1) the target app is unknown / not registered with the OS media ' +
36
+ 'bus, or (2) `media_transport` returned `NoSession`. Blind global ' +
37
+ 'keystroke (VK_MEDIA_PLAY_PAUSE and friends) — Windows doesn\'t surface ' +
38
+ 'routing outcome, so this tool always reports `degraded:true`. Pair ' +
39
+ 'with `now_playing` to inspect state first. Windows-only.',
34
40
  inputSchema: {
35
41
  type: 'object',
36
42
  properties: {
@@ -48,8 +54,20 @@ exports.mediaKeyTool = {
48
54
  mutates: true,
49
55
  toolset: 'system',
50
56
  async execute(args, _ctx) {
51
- if (!(0, _psHelpers_1.isWindows)())
52
- return (0, _psHelpers_1.windowsOnlyError)('media_key');
57
+ if (!(0, _psHelpers_1.isWindows)()) {
58
+ return (0, _psHelpers_1.windowsOnlyError)('media_key', {
59
+ canStill: [
60
+ '`shell_exec` with `xdotool key XF86AudioPlay` on Linux X11',
61
+ '`shell_exec` with `osascript -e \'tell application "Spotify" to playpause\'` on macOS',
62
+ 'Use `media_transport` if a layer-1 skill (Spotify Web API) is installed',
63
+ ],
64
+ cannotReliably: [
65
+ 'Blind global VK_MEDIA_PLAY_PAUSE keystroke via SendKeys',
66
+ ],
67
+ fix: 'Run Aiden on Windows for direct media-key emission, or use the ' +
68
+ 'platform-native helpers above via `shell_exec`.',
69
+ });
70
+ }
53
71
  const action = args.action;
54
72
  if (!ACTION_KEYS[action]) {
55
73
  return {
@@ -66,7 +84,20 @@ exports.mediaKeyTool = {
66
84
  ].join(' ');
67
85
  try {
68
86
  await (0, _psHelpers_1.runPowerShell)(script, { timeoutMs: 5000 });
69
- return { success: true, action };
87
+ // v4.1.3-repl-polish: SendKeys returns 0 whether or not any
88
+ // media-aware app received the keystroke — Windows doesn't
89
+ // surface the SMTC routing outcome to user-mode. We could
90
+ // scan `osProcessListImpl` for known media apps, but that's
91
+ // a cross-tool dep that distorts mediaKey's surface area. The
92
+ // honest answer is "we don't know if it landed"; the trail
93
+ // row renders yellow to signal that to the user without
94
+ // affecting the model's read of the result.
95
+ return {
96
+ success: true,
97
+ action,
98
+ degraded: true,
99
+ degradedReason: 'media key sent; cannot verify any app received it',
100
+ };
70
101
  }
71
102
  catch (e) {
72
103
  return {
@@ -0,0 +1,163 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) 2026 Shiva Deore (Taracod).
4
+ * Licensed under AGPL-3.0. See LICENSE for details.
5
+ *
6
+ * Aiden — local-first agent.
7
+ */
8
+ /**
9
+ * tools/v4/system/mediaSessions.ts — `media_sessions` tool. v4.1.4-media.
10
+ *
11
+ * Enumerate every Windows GSMTC (GlobalSystemMediaTransportControls) media
12
+ * session — one entry per app that has registered with the OS media bus
13
+ * (Spotify, YouTube in browser, Windows Media Player, Apple Music for
14
+ * Windows, VLC with the SMTC plugin, etc.).
15
+ *
16
+ * Layer 2 of the three-layer media-control hierarchy v4.1.4 establishes:
17
+ * 1. Semantic API (Spotify Web API when authed) — out of this slice
18
+ * 2. OS media-session API (GSMTC) ← this tool reads, mediaTransport writes
19
+ * 3. Global media keys (mediaKey tool) — blind fallback
20
+ *
21
+ * Pairs with `media_transport` (write tool) — the model calls
22
+ * `media_sessions` to see what's available, then `media_transport`
23
+ * with a target string ("spotify", "chrome", etc.) to act. Distinct
24
+ * from `now_playing` which only returns the SINGLE active session.
25
+ *
26
+ * Read-only. Windows-only in v4.1.4 (consistent with the rest of the
27
+ * computer-control family).
28
+ */
29
+ Object.defineProperty(exports, "__esModule", { value: true });
30
+ exports.__friendlyAppName = exports.mediaSessionsTool = void 0;
31
+ const _psHelpers_1 = require("./_psHelpers");
32
+ /** Map a Windows AppUserModelId to a friendly display name. Mirror of
33
+ * the normalization in core/tools/nowPlaying.ts; kept in sync so the
34
+ * two tools talk about the same app the same way. */
35
+ function friendlyAppName(aumid) {
36
+ if (!aumid)
37
+ return 'unknown';
38
+ const id = aumid.toLowerCase();
39
+ if (id.includes('spotify'))
40
+ return 'Spotify';
41
+ if (id.includes('msedge'))
42
+ return 'Microsoft Edge';
43
+ if (id.includes('chrome'))
44
+ return 'Google Chrome';
45
+ if (id.includes('firefox'))
46
+ return 'Firefox';
47
+ if (id.includes('vlc'))
48
+ return 'VLC';
49
+ if (id.includes('groove'))
50
+ return 'Groove Music';
51
+ if (id.includes('mediaplay'))
52
+ return 'Windows Media Player';
53
+ if (id.includes('apple'))
54
+ return 'Apple Music';
55
+ return aumid;
56
+ }
57
+ /**
58
+ * Build the PowerShell snippet. Enumerates every session via
59
+ * `GetSessions()`, marks the current one (the OS-routed-keypress
60
+ * target), and returns a JSON array. Each session's media properties
61
+ * are awaited individually — TryGetMediaPropertiesAsync can return
62
+ * null on transient state (track-skip mid-call) which we surface as
63
+ * empty fields rather than failing the whole enumeration.
64
+ */
65
+ function buildPs() {
66
+ return `
67
+ ${(0, _psHelpers_1.winRtAwaitPreamble)()}
68
+ $mgType = [Windows.Media.Control.GlobalSystemMediaTransportControlsSessionManager,Windows.Media.Control,ContentType=WindowsRuntime]
69
+ $pType = [Windows.Media.Control.GlobalSystemMediaTransportControlsSessionMediaProperties,Windows.Media.Control,ContentType=WindowsRuntime]
70
+ $mgr = Await ($mgType::RequestAsync()) $mgType
71
+ $current = $mgr.GetCurrentSession()
72
+ $currentId = if ($current) { $current.SourceAppUserModelId } else { '' }
73
+ $sessions = $mgr.GetSessions()
74
+ $out = @()
75
+ foreach ($s in $sessions) {
76
+ $p = $null
77
+ try { $p = Await ($s.TryGetMediaPropertiesAsync()) $pType } catch { $p = $null }
78
+ $pb = $s.GetPlaybackInfo()
79
+ $row = @{
80
+ appUserModelId = $s.SourceAppUserModelId
81
+ isCurrent = ($s.SourceAppUserModelId -eq $currentId)
82
+ playbackStatus = $pb.PlaybackStatus.ToString()
83
+ title = if ($p) { $p.Title } else { $null }
84
+ artist = if ($p) { $p.Artist } else { $null }
85
+ album = if ($p) { $p.AlbumTitle } else { $null }
86
+ }
87
+ $out += $row
88
+ }
89
+ if ($out.Count -eq 0) {
90
+ '[]'
91
+ } else {
92
+ $out | ConvertTo-Json -Compress -Depth 3
93
+ }
94
+ `.trim();
95
+ }
96
+ exports.mediaSessionsTool = {
97
+ schema: {
98
+ name: 'media_sessions',
99
+ description: 'List active Windows MEDIA PLAYBACK sessions (audio/video apps — ' +
100
+ 'Spotify, YouTube in browser, VLC, etc.). NOT for past conversation ' +
101
+ 'history — call `session_search` for chat-message search or ' +
102
+ '`recall_session` for past-session topic recall. One entry per app, ' +
103
+ 'including which one is the OS-routed target for global media keys. ' +
104
+ 'Use this BEFORE `media_transport` when you need to pick a specific ' +
105
+ 'app rather than blindly toggling the current session. Distinct from ' +
106
+ '`now_playing` which returns only the single current session. ' +
107
+ 'Windows-only in v4.1.4.',
108
+ inputSchema: {
109
+ type: 'object',
110
+ properties: {},
111
+ },
112
+ },
113
+ category: 'read',
114
+ mutates: false,
115
+ toolset: 'system',
116
+ async execute(_args, _ctx) {
117
+ if (!(0, _psHelpers_1.isWindows)()) {
118
+ return (0, _psHelpers_1.windowsOnlyError)('media_sessions', {
119
+ canStill: [
120
+ 'Call `now_playing` if a Spotify Web API skill exposes that surface',
121
+ 'Use `os_process_list` with a media-app filter (spotify, vlc, chrome) for coarse presence detection',
122
+ '`shell_exec` with `playerctl --list-all` on Linux to enumerate MPRIS clients',
123
+ ],
124
+ cannotReliably: [
125
+ 'OS-level enumeration of every media-bus-registered app',
126
+ 'Distinguishing the OS-routed "current" session from inactive ones',
127
+ ],
128
+ fix: 'Run Aiden on Windows for GSMTC enumeration, or wrap your platform\'s ' +
129
+ 'native media-control bus (MPRIS / NowPlaying) in a skill.',
130
+ });
131
+ }
132
+ try {
133
+ const { stdout } = await (0, _psHelpers_1.runPowerShell)(buildPs(), { timeoutMs: 8000 });
134
+ const trimmed = stdout.trim();
135
+ if (trimmed.length === 0 || trimmed === '[]') {
136
+ return { success: true, sessions: [], count: 0 };
137
+ }
138
+ const parsed = JSON.parse(trimmed);
139
+ // ConvertTo-Json emits an object (single result) or array (multiple).
140
+ // Normalise to array, then attach friendlyApp.
141
+ const rows = Array.isArray(parsed) ? parsed : [parsed];
142
+ const sessions = rows.map((row) => ({
143
+ appUserModelId: String(row.appUserModelId ?? ''),
144
+ friendlyApp: friendlyAppName(row.appUserModelId),
145
+ isCurrent: row.isCurrent === true,
146
+ playbackStatus: String(row.playbackStatus ?? 'Unknown'),
147
+ title: typeof row.title === 'string' ? row.title : undefined,
148
+ artist: typeof row.artist === 'string' ? row.artist : undefined,
149
+ album: typeof row.album === 'string' ? row.album : undefined,
150
+ }));
151
+ return { success: true, sessions, count: sessions.length };
152
+ }
153
+ catch (e) {
154
+ return {
155
+ success: false,
156
+ error: e instanceof Error ? e.message : String(e),
157
+ };
158
+ }
159
+ },
160
+ };
161
+ // Re-export the friendly-app mapper so mediaTransport can use the same
162
+ // normalization for target-string matching.
163
+ exports.__friendlyAppName = friendlyAppName;
@@ -0,0 +1,211 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) 2026 Shiva Deore (Taracod).
4
+ * Licensed under AGPL-3.0. See LICENSE for details.
5
+ *
6
+ * Aiden — local-first agent.
7
+ */
8
+ /**
9
+ * tools/v4/system/mediaTransport.ts — `media_transport` tool. v4.1.4-media.
10
+ *
11
+ * Verified play/pause/skip against a specific GSMTC session. Replaces
12
+ * the blind-keystroke `media_key` behavior for the common case where
13
+ * the user names an app ("pause Spotify", "resume YouTube"): instead
14
+ * of blasting VK_MEDIA_PLAY_PAUSE at whichever app the OS most
15
+ * recently routed to, we enumerate sessions, match the target by
16
+ * AppUserModelId substring (or fall back to title contains), and call
17
+ * `TryPlayAsync()` / `TryPauseAsync()` / etc. directly on that session.
18
+ *
19
+ * Layer 2 of the three-layer media-control hierarchy v4.1.4 establishes:
20
+ * 1. Semantic API (Spotify Web API when authed) — out of this slice
21
+ * 2. OS media-session API (GSMTC) ← this tool writes
22
+ * 3. Global media keys (mediaKey tool) — blind fallback
23
+ *
24
+ * Honesty story: unlike `media_key`'s blind keystroke + degraded flag,
25
+ * `media_transport` reports `success: true` ONLY when GSMTC returns
26
+ * its `Success` result. Failures (session disappeared mid-call, app
27
+ * doesn't support that action, no matching target) surface as
28
+ * `success: false` with the specific reason. No degraded flag — we
29
+ * either have OS-confirmed action or we have an honest failure.
30
+ */
31
+ Object.defineProperty(exports, "__esModule", { value: true });
32
+ exports.mediaTransportTool = void 0;
33
+ const _psHelpers_1 = require("./_psHelpers");
34
+ /** GSMTC API call per action. Keys match the schema enum verbatim. */
35
+ const ACTION_METHOD = {
36
+ play: 'TryPlayAsync',
37
+ pause: 'TryPauseAsync',
38
+ toggle: 'TryTogglePlayPauseAsync',
39
+ next: 'TrySkipNextAsync',
40
+ previous: 'TrySkipPreviousAsync',
41
+ stop: 'TryStopAsync',
42
+ };
43
+ /**
44
+ * Build the PowerShell snippet. `target` is a case-insensitive substring
45
+ * matched against each session's AppUserModelId first, then the track
46
+ * title as a softer fallback. Empty/omitted target selects the current
47
+ * session (matches the legacy `media_key` semantics, no surprise).
48
+ *
49
+ * Output: a single JSON line with `matched` (boolean — did we find a
50
+ * session) and `result` (the GSMTC enum value as a string —
51
+ * `Success` / `Failed` / `UnknownError` etc.).
52
+ */
53
+ function buildPs(action, target) {
54
+ const method = ACTION_METHOD[action];
55
+ // Single-quote-escape target for PS string literal. Lowercase compare
56
+ // happens inside the script so the model can pass "Spotify" or "spotify".
57
+ const safeTarget = target.replace(/'/g, "''");
58
+ return `
59
+ ${(0, _psHelpers_1.winRtAwaitPreamble)()}
60
+ $mgType = [Windows.Media.Control.GlobalSystemMediaTransportControlsSessionManager,Windows.Media.Control,ContentType=WindowsRuntime]
61
+ $pType = [Windows.Media.Control.GlobalSystemMediaTransportControlsSessionMediaProperties,Windows.Media.Control,ContentType=WindowsRuntime]
62
+ $mgr = Await ($mgType::RequestAsync()) $mgType
63
+ $target = '${safeTarget}'
64
+ $picked = $null
65
+ if ($target.Length -gt 0) {
66
+ $lt = $target.ToLower()
67
+ foreach ($s in $mgr.GetSessions()) {
68
+ if ($s.SourceAppUserModelId -and $s.SourceAppUserModelId.ToLower().Contains($lt)) {
69
+ $picked = $s
70
+ break
71
+ }
72
+ }
73
+ if (-not $picked) {
74
+ # Soft fallback: title contains.
75
+ foreach ($s in $mgr.GetSessions()) {
76
+ $p = $null
77
+ try { $p = Await ($s.TryGetMediaPropertiesAsync()) $pType } catch { $p = $null }
78
+ if ($p -and $p.Title -and $p.Title.ToLower().Contains($lt)) {
79
+ $picked = $s
80
+ break
81
+ }
82
+ }
83
+ }
84
+ } else {
85
+ $picked = $mgr.GetCurrentSession()
86
+ }
87
+ if (-not $picked) {
88
+ @{ matched=$false; result='NoSession'; appUserModelId=$null } | ConvertTo-Json -Compress
89
+ exit 0
90
+ }
91
+ $res = Await ($picked.${method}()) ([bool])
92
+ # v4.1.3-essentials bugfix: PowerShell 5.1 does NOT accept a bare
93
+ # parenthesized \`if\` expression inside a hashtable literal — it
94
+ # parses \`(if ...)\` as a command invocation and fails with
95
+ # "The term 'if' is not recognized as the name of a cmdlet..." (no
96
+ # ternary operator until PS 7+). The \`$(...)\` subexpression
97
+ # operator forces statement-context evaluation in PS 5.1, which is
98
+ # what we need here.
99
+ $status = if ($res) { 'Success' } else { 'Failed' }
100
+ @{ matched=$true; result=$status; appUserModelId=$picked.SourceAppUserModelId } | ConvertTo-Json -Compress
101
+ `.trim();
102
+ }
103
+ exports.mediaTransportTool = {
104
+ schema: {
105
+ name: 'media_transport',
106
+ description: 'PREFERRED for named-app media control. Verified play/pause/skip ' +
107
+ 'against a specific Windows GSMTC media session — returns OS-confirmed ' +
108
+ 'success/failure, NOT a blind keystroke like `media_key`. Use this ' +
109
+ 'whenever the user names an app ("pause Spotify", "resume YouTube"). ' +
110
+ 'Target matches by AppUserModelId substring ("spotify" → Spotify.exe), ' +
111
+ 'then track title as soft fallback. Omit `target` to act on the ' +
112
+ 'current session. Pair with `media_sessions` (read) to enumerate ' +
113
+ 'available apps. Windows-only in v4.1.4.',
114
+ inputSchema: {
115
+ type: 'object',
116
+ properties: {
117
+ action: {
118
+ type: 'string',
119
+ enum: ['play', 'pause', 'toggle', 'next', 'previous', 'stop'],
120
+ description: "Action to invoke on the matched session. 'toggle' flips " +
121
+ "play/pause. 'play' / 'pause' are explicit. 'next' / 'previous' " +
122
+ "skip tracks. 'stop' halts playback.",
123
+ },
124
+ target: {
125
+ type: 'string',
126
+ description: 'Optional app/track identifier. Case-insensitive substring ' +
127
+ 'match against AppUserModelId first ("spotify" matches ' +
128
+ 'Spotify.exe), then track title. Omit to act on the OS-routed ' +
129
+ 'current session.',
130
+ },
131
+ },
132
+ required: ['action'],
133
+ },
134
+ },
135
+ category: 'execute',
136
+ mutates: true,
137
+ toolset: 'system',
138
+ async execute(args, _ctx) {
139
+ if (!(0, _psHelpers_1.isWindows)()) {
140
+ // v4.1.3-essentials: tailored capability card for non-Windows.
141
+ // Layer-1 (web API) and layer-3b (CDP) alternatives exist on
142
+ // every platform; only layer-2 (GSMTC verified transport) is
143
+ // Windows-bound.
144
+ return (0, _psHelpers_1.windowsOnlyError)('media_transport', {
145
+ canStill: [
146
+ 'Use Spotify Web API via a skill that wraps OAuth + /me/player',
147
+ 'Use Chrome DevTools Protocol (`browser_*` tools) to drive a YouTube tab',
148
+ 'Use `shell_exec` with `playerctl` (Linux) or `osascript` (macOS) for system-wide control',
149
+ ],
150
+ cannotReliably: [
151
+ 'GSMTC-verified play/pause/skip with OS-level success confirmation',
152
+ 'Target a specific app by AppUserModelId without OS media-session APIs',
153
+ ],
154
+ fix: 'Run Aiden on Windows for GSMTC, OR install a Spotify-OAuth skill ' +
155
+ 'for layer-1 control, OR use `shell_exec` with the platform\'s media-key utility.',
156
+ });
157
+ }
158
+ const action = args.action;
159
+ if (!ACTION_METHOD[action]) {
160
+ return {
161
+ success: false,
162
+ error: `Unknown action: ${String(args.action)}. ` +
163
+ `Valid: ${Object.keys(ACTION_METHOD).join(', ')}`,
164
+ };
165
+ }
166
+ const target = typeof args.target === 'string' ? args.target.trim() : '';
167
+ try {
168
+ const { stdout } = await (0, _psHelpers_1.runPowerShell)(buildPs(action, target), {
169
+ timeoutMs: 8000,
170
+ });
171
+ const trimmed = stdout.trim();
172
+ if (trimmed.length === 0) {
173
+ return {
174
+ success: false,
175
+ error: 'media_transport returned empty output from PowerShell',
176
+ };
177
+ }
178
+ const parsed = JSON.parse(trimmed);
179
+ if (!parsed.matched) {
180
+ return {
181
+ success: false,
182
+ error: target
183
+ ? `No media session matched target "${target}". Call media_sessions to see what's available.`
184
+ : 'No active media session. Open a media app first (Spotify, YouTube, etc.).',
185
+ };
186
+ }
187
+ if (parsed.result !== 'Success') {
188
+ return {
189
+ success: false,
190
+ error: `GSMTC ${action} returned ${parsed.result} for ${parsed.appUserModelId}. ` +
191
+ `The app may not support that action in its current state.`,
192
+ appUserModelId: parsed.appUserModelId,
193
+ };
194
+ }
195
+ // OS-confirmed success. No degraded flag — unlike media_key we
196
+ // KNOW the action landed on a specific session and the OS
197
+ // accepted it.
198
+ return {
199
+ success: true,
200
+ action,
201
+ appUserModelId: parsed.appUserModelId,
202
+ };
203
+ }
204
+ catch (e) {
205
+ return {
206
+ success: false,
207
+ error: e instanceof Error ? e.message : String(e),
208
+ };
209
+ }
210
+ },
211
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aiden-runtime",
3
- "version": "4.1.2",
3
+ "version": "4.1.3",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },