vanspod 0.4.15 → 0.5.1

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.
package/scripts/tray.ps1 CHANGED
@@ -128,6 +128,29 @@ if (-not $onIcon) {
128
128
  $onIcon = [System.Drawing.Icon]::FromHandle($marked.GetHicon())
129
129
  } catch { $onIcon = [System.Drawing.SystemIcons]::Shield }
130
130
  }
131
+ # The third look, for Auto Approval (ADR 0014): the "on" icon with a purple badge and a white bolt.
132
+ $autoIcon = $null
133
+ try {
134
+ $badged = New-Object System.Drawing.Bitmap($size.Width, $size.Height)
135
+ $graphics = [System.Drawing.Graphics]::FromImage($badged)
136
+ $graphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias
137
+ $graphics.DrawIcon($onIcon, (New-Object System.Drawing.Rectangle(0, 0, $size.Width, $size.Height)))
138
+ $badge = [Math]::Max(8, [int]($size.Width * 0.6))
139
+ $bx = $size.Width - $badge
140
+ $by = $size.Height - $badge
141
+ $purple = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::FromArgb(124, 58, 237))
142
+ $graphics.FillEllipse([System.Drawing.Brushes]::White, $bx, $by, $badge, $badge)
143
+ $graphics.FillEllipse($purple, $bx + 1, $by + 1, $badge - 2, $badge - 2)
144
+ # The bolt, on a 24 x 24 grid: M13 2 4 14h7l-1 8 9-12h-7z
145
+ $unit = ($badge - 2) / 24.0
146
+ $bolt = @(@(13, 2), @(4, 14), @(11, 14), @(10, 22), @(19, 10), @(12, 10)) | ForEach-Object {
147
+ New-Object System.Drawing.PointF(($bx + 1 + $_[0] * $unit), ($by + 1 + $_[1] * $unit))
148
+ }
149
+ $graphics.FillPolygon([System.Drawing.Brushes]::White, [System.Drawing.PointF[]]$bolt)
150
+ $purple.Dispose()
151
+ $graphics.Dispose()
152
+ $autoIcon = [System.Drawing.Icon]::FromHandle($badged.GetHicon())
153
+ } catch { $autoIcon = [System.Drawing.SystemIcons]::Warning }
131
154
 
132
155
  $notify = New-Object System.Windows.Forms.NotifyIcon
133
156
  $notify.Icon = $offIcon
@@ -138,9 +161,14 @@ $notify.add_MouseClick({ if ($_.Button -eq [System.Windows.Forms.MouseButtons]::
138
161
  $menu = New-Object System.Windows.Forms.ContextMenuStrip
139
162
  $open = $menu.Items.Add("開啟 VansPod")
140
163
  $open.add_Click($openPage)
141
- $script:accessItem = New-Object System.Windows.Forms.ToolStripMenuItem("讓 Agent Builder 使用這台電腦")
164
+ $script:accessItem = New-Object System.Windows.Forms.ToolStripMenuItem("允許 Agent 使用這台電腦")
142
165
  $script:accessItem.add_Click({ Send-ToVansPod @{ type = 'access'; on = (-not $script:access) } })
143
166
  [void]$menu.Items.Add($script:accessItem)
167
+ # 只在 Auto Approval 期間出現。
168
+ $script:stopAutoItem = New-Object System.Windows.Forms.ToolStripMenuItem("停止自動允許")
169
+ $script:stopAutoItem.Visible = $false
170
+ $script:stopAutoItem.add_Click({ Send-ToVansPod @{ type = 'auto_approval_stop' } })
171
+ [void]$menu.Items.Add($script:stopAutoItem)
144
172
  [void]$menu.Items.Add((New-Object System.Windows.Forms.ToolStripSeparator))
145
173
  $stop = $menu.Items.Add("結束 VansPod")
146
174
  $stop.add_Click({
@@ -182,11 +210,13 @@ if ($script:mascot) {
182
210
  $popup.Controls.Add($picture)
183
211
  $textLeft = 64
184
212
  }
185
- $heading = New-Object System.Windows.Forms.Label
186
- $heading.Text = "Agent Builder 想要使用這台電腦"
187
- $heading.Font = $bold
188
- $heading.SetBounds($textLeft, 10, (388 - $textLeft), 22)
189
- $popup.Controls.Add($heading)
213
+ $autoColor = [System.Drawing.Color]::FromArgb(124, 58, 237)
214
+ $script:heading = New-Object System.Windows.Forms.Label
215
+ $script:heading.Text = "Agent 想要使用這台電腦"
216
+ $script:heading.Font = $bold
217
+ $script:heading.AutoEllipsis = $true
218
+ $script:heading.SetBounds($textLeft, 10, (388 - $textLeft), 22)
219
+ $popup.Controls.Add($script:heading)
190
220
  $script:titleLabel = New-Object System.Windows.Forms.Label
191
221
  $script:titleLabel.AutoEllipsis = $true
192
222
  $script:titleLabel.SetBounds($textLeft, 34, (388 - $textLeft), 22)
@@ -212,10 +242,66 @@ $script:denyButton.Text = "拒絕"
212
242
  $script:denyButton.SetBounds(176, 266, 100, 36)
213
243
  $popup.Controls.Add($script:denyButton)
214
244
  $script:approveButton = New-Object VansPodTray.MouseOnlyButton
215
- $script:approveButton.Text = "允許"
245
+ $script:approveButton.Text = "允許一次"
216
246
  $script:approveButton.Font = $bold
217
247
  $script:approveButton.SetBounds(288, 266, 100, 36)
218
248
  $popup.Controls.Add($script:approveButton)
249
+ # 自動允許 ▾:選一個時間,先看提醒,按「確定打開」才打開 Auto Approval(ADR 0014)。
250
+ # Auto Approval 期間沒有這個按鈕。
251
+ $script:autoButton = New-Object VansPodTray.MouseOnlyButton
252
+ $script:autoButton.Text = "自動允許 ▾"
253
+ $script:autoButton.SetBounds(12, 266, 110, 36)
254
+ $popup.Controls.Add($script:autoButton)
255
+ $script:autoMenu = New-Object System.Windows.Forms.ContextMenuStrip
256
+ foreach ($choice in @(@('10', '10 分鐘'), @('30', '30 分鐘'), @('until_off', '直到關閉開關'))) {
257
+ $item = New-Object System.Windows.Forms.ToolStripMenuItem($choice[1])
258
+ $item.Tag = $choice[0]
259
+ $item.add_Click({ param($sender) Show-Reminder ([string]$sender.Tag) ([string]$sender.Text) })
260
+ [void]$script:autoMenu.Items.Add($item)
261
+ }
262
+ $script:autoButton.add_Click({ $script:autoMenu.Show($script:autoButton, (New-Object System.Drawing.Point(0, $script:autoButton.Height))) })
263
+
264
+ # 提醒:蓋在 Approval Popup 上面,同樣不搶鍵盤、沒有預設按鈕。
265
+ $script:reminder = New-Object System.Windows.Forms.Panel
266
+ $script:reminder.SetBounds(0, 0, 400, 318)
267
+ $script:reminder.BackColor = [System.Drawing.Color]::White
268
+ $script:reminder.Visible = $false
269
+ $popup.Controls.Add($script:reminder)
270
+ $script:reminderTitle = New-Object System.Windows.Forms.Label
271
+ $script:reminderTitle.Font = $bold
272
+ $script:reminderTitle.ForeColor = $autoColor
273
+ $script:reminderTitle.SetBounds(12, 10, 376, 22)
274
+ $script:reminder.Controls.Add($script:reminderTitle)
275
+ $reminderText = New-Object System.Windows.Forms.Label
276
+ $reminderText.Text = "自動允許期間,Agent 可以不經過你,直接執行指令、寫入檔案和儲存圖片。"
277
+ $reminderText.SetBounds(12, 38, 376, 36)
278
+ $script:reminder.Controls.Add($reminderText)
279
+ $reminderWarn = New-Object System.Windows.Forms.Label
280
+ $reminderWarn.Text = "如果你的 Agent 會讀網頁或電子郵件,那些內容可能藏著給 Agent 的指令。"
281
+ $reminderWarn.Font = New-Object System.Drawing.Font("Microsoft JhengHei UI", 9, [System.Drawing.FontStyle]::Bold)
282
+ $reminderWarn.ForeColor = [System.Drawing.Color]::FromArgb(192, 54, 44)
283
+ $reminderWarn.SetBounds(12, 80, 376, 36)
284
+ $script:reminder.Controls.Add($reminderWarn)
285
+ $reminderExceptions = New-Object System.Windows.Forms.Label
286
+ $reminderExceptions.Text = "下面這兩種情況仍然會問你:`r`n・碰到機密檔案,例如 .env、私密金鑰`r`n・刪除專案資料夾以外的檔案`r`n`r`n排隊中的確認也會一起允許。你隨時可以按「停止自動允許」。"
287
+ $reminderExceptions.ForeColor = [System.Drawing.Color]::FromArgb(90, 90, 90)
288
+ $reminderExceptions.SetBounds(12, 122, 376, 132)
289
+ $script:reminder.Controls.Add($reminderExceptions)
290
+ $reminderCancel = New-Object VansPodTray.MouseOnlyButton
291
+ $reminderCancel.Text = "取消"
292
+ $reminderCancel.SetBounds(176, 266, 100, 36)
293
+ $reminderCancel.add_Click({ Hide-Reminder })
294
+ $script:reminder.Controls.Add($reminderCancel)
295
+ $reminderOk = New-Object VansPodTray.MouseOnlyButton
296
+ $reminderOk.Text = "確定打開"
297
+ $reminderOk.Font = $bold
298
+ $reminderOk.SetBounds(288, 266, 100, 36)
299
+ $reminderOk.add_Click({
300
+ if ($script:reminderDuration) { Send-ToVansPod @{ type = 'auto_approval_start'; duration = $script:reminderDuration } }
301
+ Hide-Reminder
302
+ })
303
+ $script:reminder.Controls.Add($reminderOk)
304
+ $script:reminderDuration = $null
219
305
  # 沒有預設按鈕:AcceptButton 與 CancelButton 都不設定。
220
306
 
221
307
  $script:access = $false
@@ -242,17 +328,58 @@ function Send-Decision([string]$decision) {
242
328
  $script:approveButton.add_Click({ Send-Decision 'approve' })
243
329
  $script:denyButton.add_Click({ Send-Decision 'deny' })
244
330
 
331
+ function Show-Reminder([string]$duration, [string]$label) {
332
+ $script:reminderDuration = $duration
333
+ $script:reminderTitle.Text = "打開自動允許($label)"
334
+ $script:reminder.Visible = $true
335
+ $script:reminder.BringToFront()
336
+ }
337
+ function Hide-Reminder {
338
+ $script:reminderDuration = $null
339
+ $script:reminder.Visible = $false
340
+ }
341
+
342
+ # Auto Approval:$script:autoOn、結束時間($null 代表直到關閉開關)。
343
+ $script:autoOn = $false
344
+ $script:autoDeadline = $null
345
+ function Update-TrayText {
346
+ if (-not $script:access) { $notify.Text = "VansPod"; return }
347
+ if (-not $script:autoOn) { $notify.Text = "VansPod:Agent 可以使用這台電腦"; return }
348
+ if ($null -eq $script:autoDeadline) { $notify.Text = "VansPod:自動允許中,直到關閉開關"; return }
349
+ $left = [Math]::Max(0, [int][Math]::Ceiling(($script:autoDeadline - [DateTime]::UtcNow).TotalMinutes))
350
+ $notify.Text = "VansPod:自動允許中,剩 $left 分鐘"
351
+ }
352
+
245
353
  function Show-State($state) {
246
354
  $script:access = [bool]$state.access
247
355
  $script:accessItem.Checked = $script:access
248
- if ($script:access) { $notify.Icon = $onIcon; $notify.Text = "VansPod:Agent Builder 可以使用這台電腦" }
249
- else { $notify.Icon = $offIcon; $notify.Text = "VansPod" }
356
+ $auto = $state.autoApproval
357
+ $script:autoOn = [bool]$auto
358
+ if ($auto -and $null -ne $auto.secondsLeft) { $script:autoDeadline = [DateTime]::UtcNow.AddSeconds([int]$auto.secondsLeft) }
359
+ else { $script:autoDeadline = $null }
360
+ $script:stopAutoItem.Visible = $script:autoOn
361
+ if (-not $script:access) { $notify.Icon = $offIcon }
362
+ elseif ($script:autoOn) { $notify.Icon = $autoIcon }
363
+ else { $notify.Icon = $onIcon }
364
+ Update-TrayText
365
+ # Auto Approval 期間沒有「自動允許 ▾」,提醒也收起來。
366
+ $script:autoButton.Visible = -not $script:autoOn
367
+ if ($script:autoOn) { Hide-Reminder }
250
368
  $approval = $state.approval
251
369
  if (-not $approval) {
252
370
  $script:shownId = $null
371
+ Hide-Reminder
253
372
  $popup.Hide()
254
373
  return
255
374
  }
375
+ # Auto Approval 期間只有例外會顯示:標題寫出為什麼仍要確認。
376
+ if ($script:autoOn -and $approval.exception) {
377
+ $script:heading.Text = "自動允許期間仍要確認:$($approval.exception)"
378
+ $script:heading.ForeColor = $autoColor
379
+ } else {
380
+ $script:heading.Text = "Agent 想要使用這台電腦"
381
+ $script:heading.ForeColor = [System.Drawing.SystemColors]::ControlText
382
+ }
256
383
  $script:waiting = [int]$approval.waiting
257
384
  if ($approval.id -ne $script:shownId) {
258
385
  $script:shownId = [string]$approval.id
@@ -291,14 +418,17 @@ $pipeTimer.add_Tick({
291
418
  $pipeTimer.Start()
292
419
  $clock = New-Object System.Windows.Forms.Timer
293
420
  $clock.Interval = 1000
294
- $clock.add_Tick({ if ($script:shownId) { Update-Time } })
421
+ $clock.add_Tick({
422
+ if ($script:shownId) { Update-Time }
423
+ if ($script:autoOn) { Update-TrayText }
424
+ })
295
425
  $clock.Start()
296
426
 
297
427
  try { [System.Windows.Forms.Application]::Run() }
298
428
  finally {
299
429
  $pipeTimer.Stop(); $clock.Stop()
300
430
  $popup.Dispose()
301
- $notify.Visible = $false; $notify.Dispose(); $menu.Dispose()
431
+ $notify.Visible = $false; $notify.Dispose(); $menu.Dispose(); $script:autoMenu.Dispose()
302
432
  if ($offIcon) { $offIcon.Dispose() }
303
433
  if ($script:mascot) { $script:mascot.Dispose() }
304
434
  }