@xiaolikt/ai-vault 0.12.3 → 0.12.4
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/Cargo.lock +1 -1
- package/crates/ai-vault/Cargo.toml +1 -1
- package/crates/ai-vault/build.rs +82 -0
- package/crates/ai-vault/src/cli.rs +46 -5
- package/crates/ai-vault/src/dialog.rs +211 -390
- package/crates/ai-vault/windows/dialog.cs +579 -0
- package/package.json +6 -6
- package/packages/pi-ai-vault/package.json +2 -2
- package/packages/pi-ai-vault/src/bitwarden.ts +40 -15
- package/packages/pi-ai-vault/src/cli.ts +26 -3
- package/tests/windows.test.mjs +133 -79
|
@@ -0,0 +1,579 @@
|
|
|
1
|
+
// ai-vault — the Windows dialogs: one password, one line of text, or a yes/no.
|
|
2
|
+
//
|
|
3
|
+
// A small WinForms program, built by csc during `cargo build` and shipped
|
|
4
|
+
// beside ai-vault.exe. It started life as PowerShell scripts, which cost about
|
|
5
|
+
// two seconds a window: loading the assemblies and compiling the DPI helper.
|
|
6
|
+
// Here the DPI helper is native, the palette is read from the settings Windows
|
|
7
|
+
// already keeps, and a window opens in about 150 ms.
|
|
8
|
+
//
|
|
9
|
+
// Protocol, on the stdin/stdout the host gave us (never a console of its own):
|
|
10
|
+
//
|
|
11
|
+
// request one JSON object, UTF-8: {"kind":"password"|"text"|"confirm",
|
|
12
|
+
// "title":…, "label":…, "mask":bool, "ok":…, "cancel":…,
|
|
13
|
+
// "show":…, "allow":…, "deny":…}
|
|
14
|
+
// response one JSON object: {"ok":bool,"value":"…","cancelled":bool,
|
|
15
|
+
// "on":"a screen"|""}
|
|
16
|
+
//
|
|
17
|
+
// Exit codes are the CLI's contract, not this program's business: it answers in
|
|
18
|
+
// JSON and lets ai-vault decide.
|
|
19
|
+
|
|
20
|
+
using System;
|
|
21
|
+
using System.Collections.Generic;
|
|
22
|
+
using System.Drawing;
|
|
23
|
+
using System.Drawing.Drawing2D;
|
|
24
|
+
using System.IO;
|
|
25
|
+
using System.Runtime.InteropServices;
|
|
26
|
+
using System.Text;
|
|
27
|
+
using System.Windows.Forms;
|
|
28
|
+
using Microsoft.Win32;
|
|
29
|
+
|
|
30
|
+
namespace AiVault
|
|
31
|
+
{
|
|
32
|
+
internal static class Native
|
|
33
|
+
{
|
|
34
|
+
[DllImport("user32.dll")] internal static extern bool SetProcessDPIAware();
|
|
35
|
+
[DllImport("dwmapi.dll")] internal static extern int DwmSetWindowAttribute(IntPtr hwnd, int attribute, ref int value, int size);
|
|
36
|
+
[DllImport("user32.dll", CharSet = CharSet.Unicode)] internal static extern IntPtr OpenInputDesktop(int flags, bool inherit, uint access);
|
|
37
|
+
[DllImport("user32.dll")] internal static extern bool CloseDesktop(IntPtr desktop);
|
|
38
|
+
[DllImport("user32.dll")] internal static extern IntPtr GetProcessWindowStation();
|
|
39
|
+
[DllImport("user32.dll", CharSet = CharSet.Unicode)] internal static extern bool GetUserObjectInformationW(IntPtr obj, int index, out UserObjectFlags info, int length, out int needed);
|
|
40
|
+
|
|
41
|
+
internal const int DESKTOP_READOBJECTS = 0x0001;
|
|
42
|
+
internal const int UOI_FLAGS = 1;
|
|
43
|
+
internal const uint WSF_VISIBLE = 0x0001;
|
|
44
|
+
|
|
45
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
46
|
+
internal struct UserObjectFlags
|
|
47
|
+
{
|
|
48
|
+
internal int Inherit;
|
|
49
|
+
internal int Reserved;
|
|
50
|
+
internal uint Flags;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
internal sealed class Theme
|
|
55
|
+
{
|
|
56
|
+
internal float Scale = 1f;
|
|
57
|
+
internal bool Dark;
|
|
58
|
+
internal Color Accent = Color.FromArgb(0, 120, 212);
|
|
59
|
+
internal Color AccentHover;
|
|
60
|
+
internal Color OnAccent = Color.White;
|
|
61
|
+
internal Color Paper, Card, Hover, Line, Ink, Muted;
|
|
62
|
+
internal string Ui = "Segoe UI";
|
|
63
|
+
|
|
64
|
+
internal static Theme Read()
|
|
65
|
+
{
|
|
66
|
+
var t = new Theme();
|
|
67
|
+
try
|
|
68
|
+
{
|
|
69
|
+
int dpi = ReadInt(@"Control Panel\Desktop\WindowMetrics", "AppliedDPI", 96);
|
|
70
|
+
if (dpi > 96 && Native.SetProcessDPIAware()) t.Scale = dpi / 96f;
|
|
71
|
+
}
|
|
72
|
+
catch { }
|
|
73
|
+
try { t.Dark = ReadInt(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize", "AppsUseLightTheme", 1) == 0; }
|
|
74
|
+
catch { }
|
|
75
|
+
try
|
|
76
|
+
{
|
|
77
|
+
using (var key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\Accent"))
|
|
78
|
+
{
|
|
79
|
+
var palette = key == null ? null : key.GetValue("AccentPalette") as byte[];
|
|
80
|
+
if (palette != null && palette.Length >= 4) t.Accent = Color.FromArgb(palette[2], palette[1], palette[0]);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
catch { }
|
|
84
|
+
t.AccentHover = Color.FromArgb(Math.Min(255, t.Accent.R + 24), Math.Min(255, t.Accent.G + 24), Math.Min(255, t.Accent.B + 24));
|
|
85
|
+
t.OnAccent = (0.299 * t.Accent.R + 0.587 * t.Accent.G + 0.114 * t.Accent.B) > 150 ? Color.Black : Color.White;
|
|
86
|
+
if (t.Dark)
|
|
87
|
+
{
|
|
88
|
+
t.Paper = Color.FromArgb(32, 32, 32); t.Card = Color.FromArgb(43, 43, 43); t.Hover = Color.FromArgb(54, 54, 54);
|
|
89
|
+
t.Line = Color.FromArgb(66, 66, 66); t.Ink = Color.White; t.Muted = Color.FromArgb(170, 170, 170);
|
|
90
|
+
}
|
|
91
|
+
else
|
|
92
|
+
{
|
|
93
|
+
t.Paper = Color.FromArgb(243, 243, 243); t.Card = Color.White; t.Hover = Color.FromArgb(234, 234, 234);
|
|
94
|
+
t.Line = Color.FromArgb(173, 173, 173); t.Ink = Color.FromArgb(26, 26, 26); t.Muted = Color.FromArgb(96, 96, 96);
|
|
95
|
+
}
|
|
96
|
+
try
|
|
97
|
+
{
|
|
98
|
+
foreach (var family in new System.Drawing.Text.InstalledFontCollection().Families)
|
|
99
|
+
{
|
|
100
|
+
if (family.Name == "Microsoft YaHei UI") { t.Ui = family.Name; break; }
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
catch { }
|
|
104
|
+
return t;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private static int ReadInt(string path, string name, int fallback)
|
|
108
|
+
{
|
|
109
|
+
using (var key = Registry.CurrentUser.OpenSubKey(path))
|
|
110
|
+
{
|
|
111
|
+
var value = key == null ? null : key.GetValue(name);
|
|
112
|
+
return value == null ? fallback : Convert.ToInt32(value);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
internal int Px(double value) { return (int)Math.Round(value * Scale); }
|
|
117
|
+
|
|
118
|
+
internal Font Font(double size) { return new Font(Ui, (float)size, FontStyle.Regular, GraphicsUnit.Point); }
|
|
119
|
+
|
|
120
|
+
internal Font Font(double size, FontStyle style) { return new Font(Ui, (float)size, style, GraphicsUnit.Point); }
|
|
121
|
+
|
|
122
|
+
/// Whether the desktop this thread runs on is the one the user sees.
|
|
123
|
+
/// USEROBJECTFLAGS on the window station is not enough: a background
|
|
124
|
+
/// ss session has a visible station and no screen, and a locked screen
|
|
125
|
+
/// still takes a window (it is simply not shown).
|
|
126
|
+
internal static bool OnScreen()
|
|
127
|
+
{
|
|
128
|
+
try
|
|
129
|
+
{
|
|
130
|
+
Native.UserObjectFlags flags;
|
|
131
|
+
int needed;
|
|
132
|
+
if (!Native.GetUserObjectInformationW(Native.GetProcessWindowStation(), Native.UOI_FLAGS, out flags, Marshal.SizeOf(typeof(Native.UserObjectFlags)), out needed))
|
|
133
|
+
return false;
|
|
134
|
+
if ((flags.Flags & Native.WSF_VISIBLE) == 0) return false;
|
|
135
|
+
var desktop = Native.OpenInputDesktop(0, false, Native.DESKTOP_READOBJECTS);
|
|
136
|
+
if (desktop == IntPtr.Zero) return false;
|
|
137
|
+
Native.CloseDesktop(desktop);
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
catch { return false; }
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
internal static void Round(Control control, int radius)
|
|
144
|
+
{
|
|
145
|
+
int w = control.Width, h = control.Height, d = Math.Min(radius * 2, Math.Min(w, h));
|
|
146
|
+
if (d < 2) return;
|
|
147
|
+
var path = new GraphicsPath();
|
|
148
|
+
path.AddArc(0, 0, d, d, 180, 90); path.AddArc(w - d, 0, d, d, 270, 90);
|
|
149
|
+
path.AddArc(w - d, h - d, d, d, 0, 90); path.AddArc(0, h - d, d, d, 90, 90);
|
|
150
|
+
path.CloseFigure();
|
|
151
|
+
control.Region = new Region(path);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
internal sealed class Request
|
|
156
|
+
{
|
|
157
|
+
internal string Kind = "password";
|
|
158
|
+
internal string Title = "ai-vault";
|
|
159
|
+
internal string Label = "";
|
|
160
|
+
internal bool Mask;
|
|
161
|
+
internal bool Reveal;
|
|
162
|
+
internal string Ok = "OK", Cancel = "Cancel", Show = "Show", Allow = "Allow", Deny = "Deny";
|
|
163
|
+
|
|
164
|
+
internal static Request Parse(string json)
|
|
165
|
+
{
|
|
166
|
+
var r = new Request();
|
|
167
|
+
var values = Json.Objects(json);
|
|
168
|
+
r.Kind = Json.Str(values, "kind", r.Kind);
|
|
169
|
+
r.Title = Json.Str(values, "title", r.Title);
|
|
170
|
+
r.Label = Json.Str(values, "label", r.Label);
|
|
171
|
+
r.Mask = Json.Bool(values, "mask", r.Mask);
|
|
172
|
+
r.Reveal = Json.Bool(values, "reveal", r.Mask);
|
|
173
|
+
r.Ok = Json.Str(values, "ok", r.Ok);
|
|
174
|
+
r.Cancel = Json.Str(values, "cancel", r.Cancel);
|
|
175
|
+
r.Show = Json.Str(values, "show", r.Show);
|
|
176
|
+
r.Allow = Json.Str(values, "allow", r.Allow);
|
|
177
|
+
r.Deny = Json.Str(values, "deny", r.Deny);
|
|
178
|
+
return r;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/// Just enough JSON for one flat object of strings and booleans.
|
|
183
|
+
internal static class Json
|
|
184
|
+
{
|
|
185
|
+
internal static Dictionary<string, object> Objects(string text)
|
|
186
|
+
{
|
|
187
|
+
var result = new Dictionary<string, object>(StringComparer.Ordinal);
|
|
188
|
+
int i = 0;
|
|
189
|
+
SkipSpace(text, ref i);
|
|
190
|
+
if (i >= text.Length || text[i] != '{') return result;
|
|
191
|
+
i++;
|
|
192
|
+
while (i < text.Length)
|
|
193
|
+
{
|
|
194
|
+
SkipSpace(text, ref i);
|
|
195
|
+
if (i < text.Length && text[i] == '}') break;
|
|
196
|
+
if (i >= text.Length || text[i] != '"') break;
|
|
197
|
+
string key = String(text, ref i);
|
|
198
|
+
SkipSpace(text, ref i);
|
|
199
|
+
if (i >= text.Length || text[i] != ':') break;
|
|
200
|
+
i++;
|
|
201
|
+
SkipSpace(text, ref i);
|
|
202
|
+
if (i < text.Length && text[i] == '"') result[key] = String(text, ref i);
|
|
203
|
+
else if (string.CompareOrdinal(text, i, "true", 0, 4) == 0) { result[key] = true; i += 4; }
|
|
204
|
+
else if (string.CompareOrdinal(text, i, "false", 0, 5) == 0) { result[key] = false; i += 5; }
|
|
205
|
+
else { int start = i; while (i < text.Length && text[i] != ',' && text[i] != '}') i++; result[key] = text.Substring(start, i - start).Trim(); }
|
|
206
|
+
SkipSpace(text, ref i);
|
|
207
|
+
if (i < text.Length && text[i] == ',') i++;
|
|
208
|
+
}
|
|
209
|
+
return result;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
private static void SkipSpace(string text, ref int i)
|
|
213
|
+
{
|
|
214
|
+
while (i < text.Length && char.IsWhiteSpace(text[i])) i++;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
private static string String(string text, ref int i)
|
|
218
|
+
{
|
|
219
|
+
var sb = new StringBuilder();
|
|
220
|
+
i++; // opening quote
|
|
221
|
+
while (i < text.Length)
|
|
222
|
+
{
|
|
223
|
+
char c = text[i++];
|
|
224
|
+
if (c == '"') break;
|
|
225
|
+
if (c != '\\') { sb.Append(c); continue; }
|
|
226
|
+
if (i >= text.Length) break;
|
|
227
|
+
char e = text[i++];
|
|
228
|
+
switch (e)
|
|
229
|
+
{
|
|
230
|
+
case 'n': sb.Append('\n'); break;
|
|
231
|
+
case 'r': sb.Append('\r'); break;
|
|
232
|
+
case 't': sb.Append('\t'); break;
|
|
233
|
+
case 'b': sb.Append('\b'); break;
|
|
234
|
+
case 'f': sb.Append('\f'); break;
|
|
235
|
+
case 'u':
|
|
236
|
+
if (i + 4 <= text.Length) { sb.Append((char)Convert.ToInt32(text.Substring(i, 4), 16)); i += 4; }
|
|
237
|
+
break;
|
|
238
|
+
default: sb.Append(e); break;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return sb.ToString();
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
internal static string Str(Dictionary<string, object> values, string key, string fallback)
|
|
245
|
+
{
|
|
246
|
+
object value;
|
|
247
|
+
return values.TryGetValue(key, out value) && value is string ? (string)value : fallback;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
internal static bool Bool(Dictionary<string, object> values, string key, bool fallback)
|
|
251
|
+
{
|
|
252
|
+
object value;
|
|
253
|
+
return values.TryGetValue(key, out value) && value is bool ? (bool)value : fallback;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
internal static string Quote(string text)
|
|
257
|
+
{
|
|
258
|
+
var sb = new StringBuilder("\"");
|
|
259
|
+
foreach (char c in text ?? "")
|
|
260
|
+
{
|
|
261
|
+
switch (c)
|
|
262
|
+
{
|
|
263
|
+
case '"': sb.Append("\\\""); break;
|
|
264
|
+
case '\\': sb.Append("\\\\"); break;
|
|
265
|
+
case '\n': sb.Append("\\n"); break;
|
|
266
|
+
case '\r': sb.Append("\\r"); break;
|
|
267
|
+
case '\t': sb.Append("\\t"); break;
|
|
268
|
+
default:
|
|
269
|
+
if (c < 0x20) sb.Append("\\u").Append(((int)c).ToString("x4"));
|
|
270
|
+
else sb.Append(c);
|
|
271
|
+
break;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
return sb.Append('"').ToString();
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
internal sealed class Dialog : Form
|
|
279
|
+
{
|
|
280
|
+
private readonly Theme theme;
|
|
281
|
+
private readonly Request request;
|
|
282
|
+
private TextBox box;
|
|
283
|
+
private Label mask;
|
|
284
|
+
private Label caret;
|
|
285
|
+
private Panel field;
|
|
286
|
+
private Panel inner;
|
|
287
|
+
private LinkLabel show;
|
|
288
|
+
private Timer blink;
|
|
289
|
+
private bool revealed;
|
|
290
|
+
internal string Value = "";
|
|
291
|
+
internal bool Accepted;
|
|
292
|
+
/// The answer the window produced, whatever path led there: a click, a
|
|
293
|
+
internal bool Cancelled;
|
|
294
|
+
|
|
295
|
+
internal Dialog(Theme theme, Request request)
|
|
296
|
+
{
|
|
297
|
+
this.theme = theme;
|
|
298
|
+
this.request = request;
|
|
299
|
+
SuspendLayout();
|
|
300
|
+
Text = request.Title;
|
|
301
|
+
FormBorderStyle = FormBorderStyle.FixedDialog;
|
|
302
|
+
MaximizeBox = false; MinimizeBox = false;
|
|
303
|
+
AutoScaleMode = AutoScaleMode.None;
|
|
304
|
+
StartPosition = FormStartPosition.CenterScreen;
|
|
305
|
+
TopMost = true;
|
|
306
|
+
BackColor = theme.Paper;
|
|
307
|
+
Font = theme.Font(10);
|
|
308
|
+
Icon = new Icon(SystemIcons.Shield, theme.Px(16), theme.Px(16));
|
|
309
|
+
ClientSize = new Size(theme.Px(460), theme.Px(200));
|
|
310
|
+
if (theme.Dark)
|
|
311
|
+
{
|
|
312
|
+
try { int on = 1; Native.DwmSetWindowAttribute(Handle, 20, ref on, sizeof(int)); }
|
|
313
|
+
catch { }
|
|
314
|
+
}
|
|
315
|
+
Build();
|
|
316
|
+
ResumeLayout();
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
private Label AddText()
|
|
320
|
+
{
|
|
321
|
+
var lbl = new Label();
|
|
322
|
+
lbl.Text = request.Label;
|
|
323
|
+
lbl.Font = theme.Font(10.5);
|
|
324
|
+
lbl.ForeColor = theme.Ink;
|
|
325
|
+
lbl.Left = theme.Px(24); lbl.Top = theme.Px(22); lbl.Width = theme.Px(412);
|
|
326
|
+
using (var g = lbl.CreateGraphics())
|
|
327
|
+
lbl.Height = (int)g.MeasureString(lbl.Text, lbl.Font, lbl.Width).Height + theme.Px(2);
|
|
328
|
+
Controls.Add(lbl);
|
|
329
|
+
return lbl;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
private Button NewButton(string text, DialogResult result, string kind)
|
|
333
|
+
{
|
|
334
|
+
var b = new Button();
|
|
335
|
+
b.Text = text; b.DialogResult = result; b.FlatStyle = FlatStyle.Flat; b.Tag = kind;
|
|
336
|
+
b.Size = new Size(theme.Px(96), theme.Px(34));
|
|
337
|
+
b.Font = theme.Font(10, kind == "quiet" ? FontStyle.Regular : FontStyle.Bold);
|
|
338
|
+
if (kind == "accent")
|
|
339
|
+
{
|
|
340
|
+
b.FlatAppearance.BorderSize = 0; b.BackColor = theme.Accent; b.ForeColor = theme.OnAccent;
|
|
341
|
+
Theme.Round(b, theme.Px(6));
|
|
342
|
+
}
|
|
343
|
+
else if (kind == "outline")
|
|
344
|
+
{
|
|
345
|
+
b.FlatAppearance.BorderSize = 1; b.FlatAppearance.BorderColor = theme.Line;
|
|
346
|
+
b.BackColor = theme.Card; b.ForeColor = theme.Ink;
|
|
347
|
+
Theme.Round(b, theme.Px(6));
|
|
348
|
+
}
|
|
349
|
+
else
|
|
350
|
+
{
|
|
351
|
+
b.FlatAppearance.BorderSize = 0; b.BackColor = theme.Paper; b.ForeColor = theme.Ink;
|
|
352
|
+
}
|
|
353
|
+
b.MouseEnter += delegate
|
|
354
|
+
{
|
|
355
|
+
b.BackColor = kind == "accent" ? theme.AccentHover : theme.Hover;
|
|
356
|
+
};
|
|
357
|
+
b.MouseLeave += delegate
|
|
358
|
+
{
|
|
359
|
+
b.BackColor = kind == "accent" ? theme.Accent : (kind == "quiet" ? theme.Paper : theme.Card);
|
|
360
|
+
};
|
|
361
|
+
Controls.Add(b);
|
|
362
|
+
return b;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
private void Place(params Button[] buttons)
|
|
366
|
+
{
|
|
367
|
+
int top = 0;
|
|
368
|
+
foreach (var b in buttons) top = Math.Max(top, b.Top);
|
|
369
|
+
int y;
|
|
370
|
+
if (buttons.Length == 0) y = ClientSize.Height - theme.Px(24) - theme.Px(34);
|
|
371
|
+
else y = Math.Max(top, ClientSize.Height - theme.Px(24) - theme.Px(34));
|
|
372
|
+
int x = ClientSize.Width - theme.Px(24);
|
|
373
|
+
for (int i = buttons.Length - 1; i >= 0; i--)
|
|
374
|
+
{
|
|
375
|
+
buttons[i].Left = x - buttons[i].Width;
|
|
376
|
+
buttons[i].Top = y;
|
|
377
|
+
x = buttons[i].Left - theme.Px(8);
|
|
378
|
+
}
|
|
379
|
+
ClientSize = new Size(theme.Px(460), y + theme.Px(34) + theme.Px(24));
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
private void Build()
|
|
383
|
+
{
|
|
384
|
+
var label = AddText();
|
|
385
|
+
if (request.Kind == "confirm")
|
|
386
|
+
{
|
|
387
|
+
var allow = NewButton(request.Allow, DialogResult.OK, "outline");
|
|
388
|
+
var deny = NewButton(request.Deny, DialogResult.Cancel, "outline");
|
|
389
|
+
allow.Click += delegate { Accepted = true; };
|
|
390
|
+
AcceptButton = deny; CancelButton = deny;
|
|
391
|
+
Place(deny, allow);
|
|
392
|
+
Shown += delegate { deny.Focus(); };
|
|
393
|
+
FormClosed += delegate { if (DialogResult != DialogResult.OK) Cancelled = true; };
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
bool secret = request.Kind == "password" && request.Mask;
|
|
398
|
+
bool reveal = secret && request.Reveal;
|
|
399
|
+
field = new Panel();
|
|
400
|
+
field.BackColor = theme.Line;
|
|
401
|
+
field.Left = theme.Px(24); field.Top = label.Bottom + theme.Px(12);
|
|
402
|
+
field.Width = theme.Px(412); field.Height = theme.Px(40);
|
|
403
|
+
Theme.Round(field, theme.Px(6));
|
|
404
|
+
Controls.Add(field);
|
|
405
|
+
|
|
406
|
+
inner = new Panel();
|
|
407
|
+
inner.BackColor = theme.Card;
|
|
408
|
+
inner.Dock = DockStyle.Fill;
|
|
409
|
+
inner.Padding = new Padding(theme.Px(1));
|
|
410
|
+
inner.SizeChanged += delegate { Theme.Round(inner, theme.Px(5)); LayoutField(); };
|
|
411
|
+
field.Controls.Add(inner);
|
|
412
|
+
|
|
413
|
+
box = new TextBox();
|
|
414
|
+
box.Name = "value";
|
|
415
|
+
box.Font = theme.Font(11);
|
|
416
|
+
box.BorderStyle = BorderStyle.None;
|
|
417
|
+
box.AutoSize = false;
|
|
418
|
+
box.BackColor = theme.Card;
|
|
419
|
+
box.ForeColor = secret ? theme.Card : theme.Ink;
|
|
420
|
+
box.Height = box.PreferredHeight;
|
|
421
|
+
box.TextChanged += delegate { UpdateMask(); };
|
|
422
|
+
box.GotFocus += delegate { if (caret != null) caret.Visible = secret && !revealed; field.BackColor = theme.Accent; };
|
|
423
|
+
box.LostFocus += delegate { if (caret != null) caret.Visible = false; field.BackColor = theme.Line; };
|
|
424
|
+
inner.Controls.Add(box);
|
|
425
|
+
|
|
426
|
+
if (secret)
|
|
427
|
+
{
|
|
428
|
+
mask = new Label();
|
|
429
|
+
mask.BackColor = theme.Card; mask.Font = theme.Font(12); mask.ForeColor = theme.Ink;
|
|
430
|
+
mask.TextAlign = ContentAlignment.MiddleLeft;
|
|
431
|
+
mask.Padding = new Padding(theme.Px(12), 0, 0, 0);
|
|
432
|
+
mask.Click += delegate { box.Focus(); };
|
|
433
|
+
inner.Controls.Add(mask);
|
|
434
|
+
|
|
435
|
+
caret = new Label();
|
|
436
|
+
caret.BackColor = theme.Ink; caret.Width = Math.Max(1, theme.Px(1)); caret.Height = theme.Px(18);
|
|
437
|
+
caret.Visible = false;
|
|
438
|
+
inner.Controls.Add(caret);
|
|
439
|
+
// Controls.Add puts a control at the BACK: bring the mask and the
|
|
440
|
+
// caret in front of the box, or the box paints over them.
|
|
441
|
+
mask.BringToFront(); caret.BringToFront();
|
|
442
|
+
|
|
443
|
+
if (!reveal) goto buttons;
|
|
444
|
+
show = new LinkLabel();
|
|
445
|
+
show.Text = request.Show;
|
|
446
|
+
show.Font = theme.Font(9.5);
|
|
447
|
+
// A LinkLabel draws its text in ForeColor until it is visited, whatever
|
|
448
|
+
// LinkColor says: both are set, or the link is black on a dark window.
|
|
449
|
+
show.LinkColor = theme.Accent; show.ActiveLinkColor = theme.AccentHover;
|
|
450
|
+
show.VisitedLinkColor = theme.Accent; show.ForeColor = theme.Accent;
|
|
451
|
+
show.LinkBehavior = LinkBehavior.HoverUnderline;
|
|
452
|
+
show.AutoSize = true;
|
|
453
|
+
show.LinkClicked += delegate { revealed = !revealed; Look(); };
|
|
454
|
+
Controls.Add(show);
|
|
455
|
+
show.BringToFront(); // after the field, or the field paints over it
|
|
456
|
+
show.Left = field.Right - show.PreferredWidth - theme.Px(2);
|
|
457
|
+
show.Top = field.Bottom + theme.Px(10);
|
|
458
|
+
|
|
459
|
+
blink = new Timer(); blink.Interval = 530;
|
|
460
|
+
blink.Tick += delegate { if (box.Focused && !revealed) caret.Visible = !caret.Visible; };
|
|
461
|
+
blink.Start();
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
buttons:
|
|
465
|
+
var cancel = NewButton(request.Cancel, DialogResult.Cancel, "quiet");
|
|
466
|
+
var ok = NewButton(request.Ok, DialogResult.OK, "accent");
|
|
467
|
+
ok.Click += delegate { Accepted = true; };
|
|
468
|
+
AcceptButton = ok; CancelButton = cancel;
|
|
469
|
+
int bottom = (secret && show != null ? show.Bottom : field.Bottom) + theme.Px(20);
|
|
470
|
+
ClientSize = new Size(theme.Px(460), bottom + theme.Px(34) + theme.Px(24));
|
|
471
|
+
int x = ClientSize.Width - theme.Px(24);
|
|
472
|
+
for (int i = 1; i >= 0; i--)
|
|
473
|
+
{
|
|
474
|
+
var b = i == 0 ? cancel : ok;
|
|
475
|
+
b.Left = x - b.Width; b.Top = bottom;
|
|
476
|
+
x = b.Left - theme.Px(8);
|
|
477
|
+
}
|
|
478
|
+
Shown += delegate { LayoutField(); box.Focus(); };
|
|
479
|
+
FormClosed += delegate
|
|
480
|
+
{
|
|
481
|
+
if (blink != null) blink.Stop();
|
|
482
|
+
Value = box.Text;
|
|
483
|
+
Cancelled = DialogResult != DialogResult.OK;
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
private void LayoutField()
|
|
488
|
+
{
|
|
489
|
+
if (box == null) return;
|
|
490
|
+
box.Left = inner.Padding.Left;
|
|
491
|
+
box.Top = (inner.ClientSize.Height - box.Height) / 2;
|
|
492
|
+
box.Width = inner.ClientSize.Width - inner.Padding.Horizontal;
|
|
493
|
+
if (mask != null) { mask.Bounds = inner.ClientRectangle; mask.BringToFront(); caret.BringToFront(); }
|
|
494
|
+
UpdateMask();
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
private void Look()
|
|
498
|
+
{
|
|
499
|
+
if (box == null) return;
|
|
500
|
+
bool secret = request.Kind == "password" && request.Mask;
|
|
501
|
+
box.ForeColor = (revealed || !secret) ? theme.Ink : theme.Card;
|
|
502
|
+
mask.Visible = secret && !revealed;
|
|
503
|
+
if (caret != null) caret.Visible = box.Focused && secret && !revealed;
|
|
504
|
+
UpdateMask();
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
private void UpdateMask()
|
|
508
|
+
{
|
|
509
|
+
if (caret == null || mask == null) return;
|
|
510
|
+
var sb = new StringBuilder();
|
|
511
|
+
for (int i = 0; i < box.TextLength; i++) sb.Append("● ");
|
|
512
|
+
mask.Text = sb.ToString().TrimEnd();
|
|
513
|
+
string typed = mask.Text.Length > 0 ? mask.Text + " " : "";
|
|
514
|
+
int width = TextRenderer.MeasureText(typed, mask.Font).Width;
|
|
515
|
+
caret.Left = mask.Left + Math.Min(mask.Padding.Left + width, Math.Max(0, mask.Width - caret.Width));
|
|
516
|
+
caret.Top = mask.Top + (mask.Height - caret.Height) / 2;
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
internal static class Program
|
|
521
|
+
{
|
|
522
|
+
/// The answer as UTF-8 bytes on the real stdout: whichever way the console
|
|
523
|
+
/// is set, what leaves this program is UTF-8.
|
|
524
|
+
private static void WriteUtf8(string text)
|
|
525
|
+
{
|
|
526
|
+
var bytes = new UTF8Encoding(false).GetBytes(text);
|
|
527
|
+
using (var stdout = Console.OpenStandardOutput())
|
|
528
|
+
{
|
|
529
|
+
stdout.Write(bytes, 0, bytes.Length);
|
|
530
|
+
stdout.Flush();
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
[STAThread]
|
|
535
|
+
private static int Main(string[] args)
|
|
536
|
+
{
|
|
537
|
+
if (args.Length > 0 && args[0] == "--probe")
|
|
538
|
+
{
|
|
539
|
+
// "Is there a screen to put a window on?" — the CLI asks this
|
|
540
|
+
// before it decides between a window and the host's prompt.
|
|
541
|
+
WriteUtf8(Theme.OnScreen() ? "on" : "off");
|
|
542
|
+
return 0;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
// The bytes as they are: on .NET Framework a console stream decodes
|
|
546
|
+
// through the console's code page (936 here), which turns UTF-8 into
|
|
547
|
+
// replacement characters before the parser ever sees it. Reading the
|
|
548
|
+
// underlying FileStream skips that, and UTF-8 does the rest.
|
|
549
|
+
string input;
|
|
550
|
+
using (var raw = Console.OpenStandardInput())
|
|
551
|
+
using (var buffer = new MemoryStream())
|
|
552
|
+
{
|
|
553
|
+
raw.CopyTo(buffer);
|
|
554
|
+
input = Encoding.UTF8.GetString(buffer.ToArray());
|
|
555
|
+
}
|
|
556
|
+
var request = Request.Parse(input);
|
|
557
|
+
Application.EnableVisualStyles();
|
|
558
|
+
var theme = Theme.Read();
|
|
559
|
+
var answer = new StringBuilder();
|
|
560
|
+
if (!Theme.OnScreen())
|
|
561
|
+
{
|
|
562
|
+
WriteUtf8("{\"on\":\"\"}");
|
|
563
|
+
return 0;
|
|
564
|
+
}
|
|
565
|
+
var dialog = new Dialog(theme, request);
|
|
566
|
+
var result = dialog.ShowDialog();
|
|
567
|
+
// The result is what counts: a click, Enter on the default button and a
|
|
568
|
+
// test that sets it directly all end with DialogResult.OK.
|
|
569
|
+
answer.Append("{\"ok\":").Append(result == DialogResult.OK ? "true" : "false");
|
|
570
|
+
answer.Append(",\"cancelled\":").Append(dialog.Cancelled ? "true" : "false");
|
|
571
|
+
answer.Append(",\"value\":").Append(Json.Quote(dialog.Value));
|
|
572
|
+
answer.Append(",\"on\":\"a screen\"}");
|
|
573
|
+
// Bytes, not Console.Out: that encodes with the console's code page too,
|
|
574
|
+
// which is not UTF-8 on a Chinese Windows.
|
|
575
|
+
WriteUtf8(answer.ToString());
|
|
576
|
+
return 0;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xiaolikt/ai-vault",
|
|
3
|
-
"version": "0.12.
|
|
3
|
+
"version": "0.12.4",
|
|
4
4
|
"description": "One machine-wide encrypted secret store for AI coding agents: the ai-vault Rust CLI (npm-installable, Linux/macOS/Windows) plus the pi-ai-vault extension for pi and omp.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -33,11 +33,11 @@
|
|
|
33
33
|
"access": "public"
|
|
34
34
|
},
|
|
35
35
|
"optionalDependencies": {
|
|
36
|
-
"@xiaolikt/ai-vault-linux-x64": "0.12.
|
|
37
|
-
"@xiaolikt/ai-vault-linux-arm64": "0.12.
|
|
38
|
-
"@xiaolikt/ai-vault-win32-x64": "0.12.
|
|
39
|
-
"@xiaolikt/ai-vault-darwin-arm64": "0.12.
|
|
40
|
-
"@xiaolikt/ai-vault-darwin-x64": "0.12.
|
|
36
|
+
"@xiaolikt/ai-vault-linux-x64": "0.12.4",
|
|
37
|
+
"@xiaolikt/ai-vault-linux-arm64": "0.12.4",
|
|
38
|
+
"@xiaolikt/ai-vault-win32-x64": "0.12.4",
|
|
39
|
+
"@xiaolikt/ai-vault-darwin-arm64": "0.12.4",
|
|
40
|
+
"@xiaolikt/ai-vault-darwin-x64": "0.12.4"
|
|
41
41
|
},
|
|
42
42
|
"files": [
|
|
43
43
|
"bin",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-ai-vault",
|
|
3
|
-
"version": "0.12.
|
|
3
|
+
"version": "0.12.4",
|
|
4
4
|
"description": "pi extension for ai-vault: one machine-wide encrypted secret store, live Bitwarden and Ente Auth. Works in pi and in omp (the omp fork reads the same manifest).",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"security"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@xiaolikt/ai-vault": "0.12.
|
|
28
|
+
"@xiaolikt/ai-vault": "0.12.4"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
31
|
"@earendil-works/pi-coding-agent": "^1.5.0",
|