Repository navigation
Expand file tree
/
Copy pathworkbench.html
More file actions
2024 lines (1875 loc) · 90.5 KB
/
Copy pathworkbench.html
File metadata and controls
2024 lines (1875 loc) · 90.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Workbench — Hugin</title>
<meta name="description" content="Hugin's driver-authoring workbench. Scan, probe, draft, test, submit — Lua drivers for forty-two-watts in minutes.">
<meta name="robots" content="noindex">
<link href="https://api.fontshare.com/css?f[]=satoshi@300,400,500,700,900&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;500;700&display=swap" rel="stylesheet">
<script src="https://cdn.tailwindcss.com"></script>
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
<!-- CodeMirror 5 via cdnjs.
Why 5 and not 6: CM6 ships as ESM and is built around a bundler. With
no build step the import-map dance is awkward and brittle. CM5 has a
plain UMD bundle and a single-file lua mode — drops in cleanly. Same
editor as the desktop app's existing surface, so contributors see a
familiar UI either way. -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/codemirror.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/theme/material-darker.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/codemirror.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/mode/lua/lua.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/addon/edit/matchbrackets.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/addon/selection/active-line.min.js"></script>
<style>
:root {
--color-ground-paper: #0A0A0A;
--color-ground-cream: #161616;
--color-ground-ink: #F5F2E1;
--color-signal-orange: #E85D1F;
--color-signal-orange-600: #FF7A3D;
--color-confirmed-green: #22C55E;
--color-text-near-black: #F5F2E1;
--color-text-muted: #9CA3AF;
--color-border-whisper: rgba(245, 242, 225, 0.12);
--color-border-subtle: rgba(245, 242, 225, 0.06);
}
html { font-family: "Satoshi", system-ui, sans-serif; background: var(--color-ground-paper); color: var(--color-text-near-black); color-scheme: dark; }
body { background: var(--color-ground-paper); }
.font-mono { font-family: "JetBrains Mono", monospace; }
.micro-label { font-family: "JetBrains Mono", monospace; font-size: 11px; letter-spacing: 0.18em; text-transform: uppercase; }
[x-cloak] { display: none !important; }
/* CodeMirror styling — dark, sharp corners, JetBrains Mono. */
.CodeMirror {
height: 100%;
font-family: "JetBrains Mono", monospace;
font-size: 13px;
line-height: 1.55;
background: var(--color-ground-paper);
color: var(--color-text-near-black);
border-radius: 0;
}
.CodeMirror-gutters {
background: var(--color-ground-paper);
border-right: 1px solid var(--color-border-subtle);
}
.CodeMirror-linenumber { color: var(--color-text-muted); opacity: 0.5; }
.CodeMirror-cursor { border-left-color: var(--color-signal-orange); }
.CodeMirror-selected { background: rgba(232, 93, 31, 0.18) !important; }
.CodeMirror-focused .CodeMirror-selected { background: rgba(232, 93, 31, 0.22) !important; }
.CodeMirror-activeline-background { background: rgba(245, 242, 225, 0.025); }
.cm-s-material-darker.CodeMirror { background: var(--color-ground-paper); }
textarea { resize: none; }
/* Pulsing dots */
.status-dot { width: 8px; height: 8px; border-radius: 9999px; display: inline-block; }
.status-dot.green { background: var(--color-confirmed-green); animation: pulse-green 2s infinite; }
.status-dot.orange { background: var(--color-signal-orange); }
.status-dot.red { background: var(--color-destructive-red, #FF6B6B); }
@keyframes pulse-green {
0% { box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.6); }
70% { box-shadow: 0 0 0 6px rgba(34, 197, 94, 0); }
100% { box-shadow: 0 0 0 0 rgba(34, 197, 94, 0); }
}
/* Stream-into-editor indicator */
.ai-streaming-pill {
position: absolute; right: 12px; top: 12px;
display: inline-flex; align-items: center; gap: 6px;
background: rgba(232, 93, 31, 0.12);
color: var(--color-signal-orange-600);
padding: 4px 10px; font-family: "JetBrains Mono", monospace;
font-size: 10px; letter-spacing: 0.18em; text-transform: uppercase;
pointer-events: none;
}
.ai-streaming-pill .blink {
width: 6px; height: 6px; background: var(--color-signal-orange); border-radius: 9999px;
animation: blink 0.8s step-end infinite;
}
@keyframes blink { 50% { opacity: 0; } }
/* AI-Ask mini-chat bubble */
.ai-bubble { background: var(--color-ground-cream); border-left: 2px solid var(--color-signal-orange); padding: 8px 12px; }
.ai-bubble.user { border-left-color: var(--color-text-muted); }
/* Hide scrollbar styling drift in panels */
.scroll-y { overflow-y: auto; }
.scroll-y::-webkit-scrollbar { width: 6px; }
.scroll-y::-webkit-scrollbar-thumb { background: var(--color-border-whisper); }
.scroll-y::-webkit-scrollbar-track { background: transparent; }
</style>
<script>
tailwind.config = {
theme: {
extend: {
colors: {
paper: 'var(--color-ground-paper)',
cream: 'var(--color-ground-cream)',
ink: 'var(--color-ground-ink)',
orange: {
50:'#FFF4ED',100:'#FFE8D9',200:'#FFD1B3',300:'#FFBA8C',400:'#FF9966',
500:'#E85D1F',600:'#FF7A3D',700:'#C0470F',800:'#8C3409',900:'#582106',950:'#3B1205',
},
'confirmed-green':'var(--color-confirmed-green)',
'destructive-red':'#FF6B6B',
'text-primary':'var(--color-text-near-black)',
'text-muted':'var(--color-text-muted)',
'border-whisper':'var(--color-border-whisper)',
'border-subtle':'var(--color-border-subtle)',
},
fontFamily: {
sans: '"Satoshi", system-ui, sans-serif',
mono: '"JetBrains Mono", monospace',
},
},
},
};
</script>
</head>
<body class="min-h-dvh flex flex-col bg-paper text-text-primary antialiased"
x-data="workbench()" x-init="init()">
<!-- =========================================================
NAV
========================================================= -->
<nav class="max-w-[1600px] mx-auto w-full px-6 py-5 flex items-center justify-between gap-4 flex-shrink-0 border-b border-border-whisper">
<div class="flex items-center gap-6">
<a href="/" class="flex items-center gap-2 text-lg font-bold tracking-tight text-ink no-underline">
<span class="w-3 h-3 bg-orange-500 inline-block"></span>
Hugin
</a>
<a href="/catalog.html" class="hidden sm:inline text-sm font-medium text-text-muted hover:text-ink transition-colors no-underline">Catalog</a>
<a href="/workbench.html" class="hidden sm:inline text-sm font-medium text-orange-500 transition-colors no-underline">Workbench</a>
</div>
<div class="flex items-center gap-5">
<!-- Agent pill — local mode (HTTP-direct) -->
<template x-if="agentKind === 'local' && agentURL">
<a href="/settings.html" :title="agentURL"
class="hidden sm:inline-flex items-center gap-1.5 font-mono text-xs text-text-muted hover:text-ink transition-colors no-underline">
<span class="w-1.5 h-1.5 rounded-full bg-confirmed-green inline-block"></span>
agent · local
</a>
</template>
<!-- Agent pill — remote mode (NATS). Green when presence
heartbeat seen in last 30s, amber while we wait for the
first beat / on reconnect, orange when stale. -->
<template x-if="agentKind === 'remote' && agentId">
<a href="/settings.html"
:title="agentId + ' · ' + agentNatsURL + (agentOnline ? '' : ' (no presence — agent offline?)')"
class="hidden sm:inline-flex items-center gap-1.5 font-mono text-xs text-text-muted hover:text-ink transition-colors no-underline">
<span class="w-1.5 h-1.5 rounded-full inline-block"
:class="agentOnline ? 'bg-confirmed-green' : 'bg-orange-500'"></span>
<span x-text="agentOnline ? 'agent · remote' : 'agent · remote (offline)'"></span>
</a>
</template>
<!-- Agent pill — nothing paired -->
<template x-if="(!agentURL || agentKind !== 'local') && (!agentId || agentKind !== 'remote')">
<a href="/settings.html"
class="hidden sm:inline-flex items-center gap-1.5 font-mono text-xs text-text-muted hover:text-ink transition-colors no-underline">
<span class="w-1.5 h-1.5 rounded-full bg-orange-500 inline-block"></span>
no agent
</a>
</template>
<!-- User -->
<template x-if="user">
<div class="flex items-center gap-3">
<span class="hidden sm:inline font-mono text-xs text-text-muted" x-text="'@' + user"></span>
<button @click="signOut()" class="text-sm font-medium text-text-muted hover:text-orange-500 transition-colors">Sign out</button>
</div>
</template>
<template x-if="!user && token">
<span class="hidden sm:inline font-mono text-xs text-text-muted">@…</span>
</template>
</div>
</nav>
<!-- =========================================================
SIGN-IN GATE
========================================================= -->
<section x-show="!token" x-cloak class="flex-1 flex items-center justify-center px-6 py-12">
<div class="max-w-xl text-center">
<div class="micro-label text-orange-500 mb-3">Workbench</div>
<h1 class="text-4xl md:text-6xl font-black tracking-tight leading-[1.05] mb-6">
Open the workbench.
</h1>
<p class="text-lg text-text-muted leading-relaxed mb-12 max-w-prose mx-auto">
Scan a device, probe its registers, draft a Lua driver with AI, test
against real hardware, submit a PR to
<a href="https://github.com/frahlg/forty-two-watts" class="text-ink underline decoration-orange-500 underline-offset-4 hover:text-orange-500">forty-two-watts</a>.
</p>
<a :href="apiBase + '/v1/auth/login'"
class="inline-flex items-center gap-3 bg-ink text-paper px-6 py-4 text-base font-medium rounded-none hover:bg-orange-500 hover:text-paper transition-colors no-underline">
<svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
<path d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.4 3-.405 1.02.005 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"/>
</svg>
Sign in with GitHub
</a>
<p class="mt-6 text-xs font-mono text-text-muted">Scope: <span class="text-ink">public_repo</span> — only used to fork + open PRs in your name.</p>
</div>
</section>
<!-- =========================================================
EMPTY STATE — pick a starting point
========================================================= -->
<section x-show="token && !workbenchActive" x-cloak class="flex-1 flex items-center justify-center px-6 py-12">
<div class="w-full max-w-2xl">
<div class="border border-border-whisper bg-paper p-8 md:p-10">
<div class="micro-label text-orange-500 mb-3">Start</div>
<h2 class="text-2xl md:text-4xl font-black tracking-tight mb-2">Pick a starting point.</h2>
<p class="text-text-muted text-sm mb-8 leading-relaxed">
Scan your network for unknown devices, paste a manual / register list,
or restore a draft you started earlier.
</p>
<!-- No-agent install CTA. Most first-time users land here without
realising they need to install a local helper. This panel
tells them why and how, with the install one-liners visible
so they don't have to dig through docs first. -->
<template x-if="!agentReady">
<div class="border border-orange-500 bg-orange-500/5 p-5 md:p-6 mb-8">
<div class="flex items-start gap-3">
<span class="text-orange-500 text-xl leading-none mt-0.5" aria-hidden="true">⚠</span>
<div class="flex-1 min-w-0">
<div class="font-bold text-ink mb-1">No agent paired.</div>
<p class="text-xs text-text-muted leading-relaxed mb-4">
Scan and live-test need a small helper running on the device
that's on the same LAN as the inverter (your laptop, a Pi,
a 42W box). Install one, then pair — or skip ahead with
<em>Paste device specs</em> if you only need AI drafting.
</p>
<div class="grid grid-cols-1 sm:grid-cols-3 gap-2 mb-3 text-[11px]">
<div class="bg-paper border border-border-whisper p-2.5 font-mono break-all">
<div class="micro-label text-text-muted mb-1 text-[9px]">macOS · Linux</div>
brew install srcfl/tap/hugin-agent
</div>
<div class="bg-paper border border-border-whisper p-2.5 font-mono break-all">
<div class="micro-label text-text-muted mb-1 text-[9px]">Windows</div>
scoop install srcfl/hugin-agent
</div>
<div class="bg-paper border border-border-whisper p-2.5 font-mono break-all">
<div class="micro-label text-text-muted mb-1 text-[9px]">Docker</div>
ghcr.io/srcfl/hugin-agent
</div>
</div>
<div class="flex flex-wrap items-center gap-3 text-xs">
<a href="/docs/" class="text-orange-500 hover:text-ink font-medium no-underline">
Full install walkthrough →
</a>
<a href="/settings.html" class="text-text-muted hover:text-ink no-underline">
Already installed? Pair it →
</a>
</div>
</div>
</div>
</div>
</template>
<!-- Two main actions -->
<div class="grid sm:grid-cols-2 gap-px bg-border-whisper border border-border-whisper mb-8">
<button @click="scanLAN()"
:disabled="!agentReady || scanning"
:title="!agentReady ? 'Pair an agent in /settings.html' : 'Scan your LAN with the paired agent'"
class="bg-paper p-6 flex flex-col items-start gap-3 hover:bg-cream transition-colors text-left disabled:opacity-40 disabled:cursor-not-allowed">
<span class="text-2xl" aria-hidden="true">🔍</span>
<span class="font-bold text-ink">
<span x-show="!scanning">Scan my LAN</span>
<span x-show="scanning" x-cloak>Scanning…</span>
</span>
<span class="text-xs text-text-muted leading-relaxed">
<template x-if="agentReady">
<span>ARP + Modbus + HTTP probe across your subnet via the paired agent.</span>
</template>
<template x-if="!agentReady">
<span>Requires an agent. <a href="/settings.html" class="underline text-orange-500">Pair one →</a></span>
</template>
</span>
</button>
<button @click="openManualMode()"
class="bg-paper p-6 flex flex-col items-start gap-3 hover:bg-cream transition-colors text-left">
<span class="text-2xl" aria-hidden="true">📋</span>
<span class="font-bold text-ink">Paste device specs / manual</span>
<span class="text-xs text-text-muted leading-relaxed">No agent? Drop in a register list, manual snippet, or vendor doc. AI starts from there.</span>
</button>
</div>
<!-- Manual paste form -->
<div x-show="manualOpen" x-cloak class="mt-6 border border-border-whisper p-5 space-y-3">
<div class="micro-label text-orange-500">Manual mode</div>
<input x-model="manual.label" type="text" placeholder="Device label, e.g. Sungrow SH10RT"
class="w-full bg-transparent text-sm font-mono text-ink py-2 px-0 border-0 border-b border-border-whisper focus:border-orange-500 focus:outline-none focus:ring-0" />
<textarea x-model="manual.specs" rows="8"
placeholder="Paste a register table, a manual excerpt, or a description of the device. Anything Claude should see."
class="w-full bg-transparent text-sm font-mono text-ink py-2 px-0 border border-border-whisper focus:border-orange-500 focus:outline-none focus:ring-0 p-3"></textarea>
<div class="flex justify-end gap-3">
<button @click="manualOpen = false" class="text-xs font-mono text-text-muted hover:text-ink">cancel</button>
<button @click="enterManual()"
:disabled="!manual.specs.trim()"
class="bg-orange-500 hover:bg-orange-600 text-paper px-4 py-2 text-sm font-medium rounded-none transition-colors disabled:opacity-40 disabled:cursor-not-allowed">
Open workbench
</button>
</div>
</div>
<!-- Scan results -->
<div x-show="lastScan && !workbenchActive && !manualOpen" x-cloak class="mt-6 border border-border-whisper">
<div class="px-5 py-3 border-b border-border-whisper flex items-center justify-between">
<span class="micro-label text-orange-500">Scan results</span>
<span class="font-mono text-xs text-text-muted" x-text="(lastScan?.devices?.length || 0) + ' devices'"></span>
</div>
<template x-if="!(lastScan?.devices?.length)">
<div class="px-5 py-6 text-sm text-text-muted">No devices found.</div>
</template>
<template x-for="d in (lastScan?.devices || [])" :key="(d.ip || '') + (d.mac || '')">
<button @click="enterFromScan(d)"
class="w-full text-left px-5 py-3 flex items-center justify-between gap-4 border-b border-border-subtle last:border-b-0 hover:bg-cream transition-colors">
<div class="min-w-0">
<div class="font-mono text-sm text-ink" x-text="d.ip || '?'"></div>
<div class="font-mono text-[11px] text-text-muted truncate"
x-text="[d.modbus?.fc43_make, d.modbus?.fc43_model, d.vendor_oui, d.mac].filter(Boolean).join(' · ') || 'unknown'"></div>
</div>
<div class="font-mono text-[11px] text-text-muted flex-shrink-0"
x-text="(d.open_ports || []).join(',')"></div>
</button>
</template>
</div>
<!-- Recent drafts -->
<div x-show="drafts.length > 0" x-cloak class="mt-8">
<div class="micro-label text-text-muted mb-3 flex items-center justify-between">
<span>Recent drafts</span>
<span class="font-mono text-text-muted normal-case tracking-normal" x-text="drafts.length + ' / 20'"></span>
</div>
<div class="border border-border-whisper">
<template x-for="d in drafts" :key="d.id">
<div class="px-5 py-3 flex items-center justify-between gap-4 border-b border-border-subtle last:border-b-0 hover:bg-cream transition-colors group">
<button @click="restoreDraft(d.id)" class="flex-1 text-left min-w-0">
<div class="font-bold text-sm text-ink truncate"
x-text="(((d.device?.manufacturer || '') + ' ' + (d.device?.model || '')).trim()) || (d.label || 'Untitled draft')"></div>
<div class="font-mono text-[11px] text-text-muted truncate">
<span x-text="relTime(d.updatedAt)"></span>
<span x-show="d.lastTestRun"> · <span x-text="d.lastTestRun?.ok ? 'last test ok' : 'last test failed'"></span></span>
<span x-show="d.prURL"> · <span class="text-confirmed-green">PR opened</span></span>
</div>
</button>
<button @click="deleteDraft(d.id)"
class="font-mono text-[11px] text-text-muted hover:text-destructive-red opacity-0 group-hover:opacity-100 transition-opacity">
delete
</button>
</div>
</template>
</div>
</div>
</div>
<!-- Empty-state error toast -->
<div x-show="error" x-cloak class="mt-4 border border-destructive-red/50 bg-destructive-red/5 p-4">
<div class="micro-label text-destructive-red mb-1">Error</div>
<div class="font-mono text-xs text-destructive-red break-words" x-text="error"></div>
</div>
</div>
</section>
<!-- =========================================================
WORKBENCH (3-pane)
========================================================= -->
<section x-show="token && workbenchActive" x-cloak class="flex-1 flex flex-col min-h-0">
<!-- Top bar -->
<div class="border-b border-border-whisper px-6 py-3 flex items-center gap-4 flex-shrink-0">
<button @click="exitWorkbench()" class="font-mono text-xs text-text-muted hover:text-orange-500">← back</button>
<div class="font-mono text-xs text-text-muted truncate flex-1">
<span class="text-text-muted">Hugin</span>
<span class="mx-2">·</span>
<span class="text-text-muted">Workbench</span>
<span class="mx-2">·</span>
<span class="text-ink" x-text="breadcrumbLabel()"></span>
</div>
<div class="font-mono text-[11px] text-text-muted hidden md:inline">
<span x-show="savedFlash" class="text-confirmed-green" x-cloak>saved •</span>
<span x-show="!savedFlash" x-cloak>autosaved</span>
</div>
<button @click="runTest()"
:disabled="!agentReady || testing || mode === 'manual'"
:title="mode === 'manual' ? 'Manual mode — no live device' : (!agentReady ? 'Pair an agent in /settings.html' : 'Run driver against ' + (config.host || 'device'))"
class="border border-orange-500 text-orange-500 hover:bg-orange-500 hover:text-paper px-4 py-1.5 text-xs font-mono rounded-none transition-colors disabled:opacity-30 disabled:cursor-not-allowed">
<span x-show="!testing">▶ Test against device</span>
<span x-show="testing" x-cloak>Running…</span>
</button>
<button @click="submitPR()"
:disabled="!canSubmit() || submitting"
class="bg-orange-500 hover:bg-orange-600 text-paper px-4 py-1.5 text-xs font-mono font-bold rounded-none transition-colors disabled:opacity-30 disabled:cursor-not-allowed">
<span x-show="!submitting">Submit as PR</span>
<span x-show="submitting" x-cloak>Submitting…</span>
</button>
</div>
<!-- Tab bar (mobile only) -->
<div class="md:hidden border-b border-border-whisper flex flex-shrink-0">
<button @click="mobileTab = 'device'"
:class="mobileTab === 'device' ? 'text-orange-500 border-orange-500' : 'text-text-muted border-transparent'"
class="flex-1 py-2 text-xs font-mono uppercase tracking-wider border-b-2">Device</button>
<button @click="mobileTab = 'driver'"
:class="mobileTab === 'driver' ? 'text-orange-500 border-orange-500' : 'text-text-muted border-transparent'"
class="flex-1 py-2 text-xs font-mono uppercase tracking-wider border-b-2">Driver</button>
<button @click="mobileTab = 'live'"
:class="mobileTab === 'live' ? 'text-orange-500 border-orange-500' : 'text-text-muted border-transparent'"
class="flex-1 py-2 text-xs font-mono uppercase tracking-wider border-b-2">Live</button>
</div>
<!-- Three columns -->
<div class="flex-1 grid grid-cols-1 md:grid-cols-[28%_44%_28%] min-h-0">
<!-- ==============================================
DEVICE PANEL (left)
============================================== -->
<aside class="border-r border-border-whisper flex flex-col min-h-0"
:class="(mobileTab !== 'device' && 'hidden md:flex') || ''">
<div class="px-5 py-3 border-b border-border-whisper flex items-center justify-between">
<span class="micro-label text-orange-500">Device</span>
<span class="font-mono text-[11px] text-text-muted" x-text="mode === 'manual' ? 'manual mode' : (config.host || '—')"></span>
</div>
<div class="flex-1 scroll-y px-5 py-4 space-y-6">
<!-- Picked device summary -->
<div x-show="device" x-cloak class="border border-border-whisper bg-cream p-3 space-y-2">
<div class="font-bold text-sm text-ink leading-snug" x-text="(device?.manufacturer || 'Unknown') + ' ' + (device?.model || '')"></div>
<div class="font-mono text-[11px] text-text-muted space-y-0.5">
<div x-show="device?.ip"><span class="text-text-muted">ip</span> <span class="text-ink" x-text="device?.ip"></span></div>
<div x-show="device?.mac"><span class="text-text-muted">mac</span> <span class="text-ink" x-text="device?.mac"></span></div>
<div x-show="device?.ports?.length"><span class="text-text-muted">ports</span> <span class="text-ink" x-text="(device?.ports || []).join(',')"></span></div>
<div x-show="device?.modbusFingerprint?.fc43_make">
<span class="text-text-muted">fc43</span>
<span class="text-ink" x-text="(device?.modbusFingerprint?.fc43_make || '') + ' ' + (device?.modbusFingerprint?.fc43_model || '')"></span>
</div>
</div>
</div>
<!-- Connection -->
<div>
<div class="micro-label text-text-muted mb-2">Connection</div>
<div class="space-y-2">
<label class="block">
<span class="micro-label text-text-muted text-[10px]">host</span>
<input x-model="config.host" @input="markDirty()" type="text" placeholder="192.168.1.50"
class="mt-1 w-full bg-transparent text-xs font-mono text-ink py-1.5 px-2 border border-border-whisper focus:border-orange-500 focus:outline-none focus:ring-0" />
</label>
<div class="grid grid-cols-2 gap-2">
<label class="block">
<span class="micro-label text-text-muted text-[10px]">port</span>
<input x-model.number="config.port" @input="markDirty()" type="number" placeholder="502"
class="mt-1 w-full bg-transparent text-xs font-mono text-ink py-1.5 px-2 border border-border-whisper focus:border-orange-500 focus:outline-none focus:ring-0" />
</label>
<label class="block">
<span class="micro-label text-text-muted text-[10px]">slave</span>
<input x-model.number="config.slave_id" @input="markDirty()" type="number" placeholder="1"
class="mt-1 w-full bg-transparent text-xs font-mono text-ink py-1.5 px-2 border border-border-whisper focus:border-orange-500 focus:outline-none focus:ring-0" />
</label>
</div>
</div>
</div>
<!-- Probed registers -->
<div>
<div class="micro-label text-text-muted mb-2 flex items-center justify-between">
<span>Probed registers</span>
<button @click="openProbe()"
:disabled="!agentReady || mode === 'manual'"
class="font-mono text-[11px] text-orange-500 hover:text-orange-600 normal-case tracking-normal disabled:opacity-30 disabled:cursor-not-allowed">+ Probe register</button>
</div>
<template x-if="!probedRegisters.length">
<div class="font-mono text-[11px] text-text-muted">No probes yet.</div>
</template>
<div class="space-y-1.5">
<template x-for="(r, idx) in probedRegisters" :key="idx">
<div class="border border-border-subtle px-2 py-1.5 font-mono text-[11px] flex items-center justify-between gap-2">
<div class="min-w-0">
<div class="text-ink" x-text="'0x' + (r.addr || 0).toString(16).padStart(4,'0') + ' · ' + (r.kind || 'u16')"></div>
<div class="text-text-muted truncate" x-text="(r.value !== undefined ? 'val ' + r.value : '') + (r.raw_hex ? ' · ' + r.raw_hex : '')"></div>
</div>
<button @click="probedRegisters.splice(idx,1); markDirty()"
class="text-text-muted hover:text-destructive-red text-[10px]">×</button>
</div>
</template>
</div>
</div>
<!-- Notes -->
<div>
<div class="micro-label text-text-muted mb-2">Notes</div>
<textarea x-model="notes" @input="markDirty()" rows="3"
placeholder="Anything worth remembering."
class="w-full bg-transparent text-xs font-mono text-ink p-2 border border-border-whisper focus:border-orange-500 focus:outline-none focus:ring-0"></textarea>
</div>
<!-- AI-Ask -->
<div class="border-t border-border-subtle pt-4">
<div class="micro-label text-text-muted mb-2 flex items-center justify-between">
<span>AI-Ask</span>
<span class="font-mono text-[10px] text-text-muted normal-case tracking-normal" x-text="aiThread.length + ' turns'"></span>
</div>
<div class="space-y-2 mb-3 max-h-72 scroll-y">
<template x-for="(m, i) in aiThread" :key="i">
<div class="ai-bubble" :class="m.role === 'user' ? 'user' : ''">
<div class="micro-label mb-1"
:class="m.role === 'user' ? 'text-text-muted' : 'text-orange-500'"
x-text="m.role"></div>
<div class="text-xs whitespace-pre-wrap break-words" x-text="m.content"></div>
</div>
</template>
<div x-show="askStreaming" x-cloak class="ai-bubble">
<div class="micro-label text-orange-500 mb-1">claude</div>
<div class="text-xs text-text-muted">thinking…</div>
</div>
</div>
<textarea x-model="askInput"
@keydown.meta.enter.prevent="askAI()"
@keydown.ctrl.enter.prevent="askAI()"
rows="2"
placeholder="Ask anything about this device, the registers, or the driver. Cmd+Enter to send."
class="w-full bg-transparent text-xs font-mono text-ink p-2 border border-border-whisper focus:border-orange-500 focus:outline-none focus:ring-0"></textarea>
<div class="flex justify-end mt-2">
<button @click="askAI()"
:disabled="!askInput.trim() || askStreaming"
class="bg-orange-500 hover:bg-orange-600 text-paper px-3 py-1 text-[11px] font-mono rounded-none transition-colors disabled:opacity-30 disabled:cursor-not-allowed">
<span x-show="!askStreaming">Send</span>
<span x-show="askStreaming" x-cloak>…</span>
</button>
</div>
</div>
</div>
</aside>
<!-- ==============================================
DRIVER PANEL (center)
============================================== -->
<main class="flex flex-col min-h-0 border-r border-border-whisper md:border-r"
:class="(mobileTab !== 'driver' && 'hidden md:flex') || ''">
<div class="px-5 py-3 border-b border-border-whisper flex items-center justify-between flex-shrink-0">
<div class="flex items-center gap-3 min-w-0">
<span class="micro-label text-orange-500 flex-shrink-0">Driver</span>
<span class="font-mono text-xs text-ink truncate" x-text="(driverFilename() || '—.lua')"></span>
</div>
<span class="font-mono text-[10px] text-text-muted flex-shrink-0" x-text="luaSource.length + ' bytes'"></span>
</div>
<div class="flex-1 min-h-0 relative">
<textarea x-ref="editor" id="cm-editor" class="hidden"></textarea>
<div x-show="aiStreamingToEditor" x-cloak class="ai-streaming-pill">
<span class="blink"></span>AI editing
</div>
</div>
<div class="border-t border-border-whisper flex flex-shrink-0 flex-wrap items-center">
<button @click="runTest()"
:disabled="!agentReady || testing || mode === 'manual'"
:title="mode === 'manual' ? 'Manual mode — no live device' : (!agentReady ? 'Pair an agent' : 'Run for ' + runDurationSec + 's')"
class="px-4 py-2 text-xs font-mono text-text-muted hover:text-orange-500 border-r border-border-whisper transition-colors disabled:opacity-30 disabled:cursor-not-allowed">
<span x-show="!testing" x-text="'▶ Run · ' + runDurationSec + 's'"></span>
<span x-show="testing" x-cloak>Running…</span>
</button>
<label class="px-3 py-2 text-[10px] font-mono text-text-muted border-r border-border-whisper flex items-center gap-2"
:class="testing || mode === 'manual' ? 'opacity-40' : ''"
title="How long to run the driver. The agent caps internally; this only affects the in-browser timer.">
<span>dur</span>
<input type="range" min="5" max="120" step="5"
x-model.number="runDurationSec"
:disabled="testing || mode === 'manual'"
class="accent-orange-500 w-16 cursor-pointer disabled:cursor-not-allowed">
</label>
<button @click="aiFixSelection()"
:disabled="aiBusy"
class="px-4 py-2 text-xs font-mono text-text-muted hover:text-orange-500 border-r border-border-whisper transition-colors disabled:opacity-30 disabled:cursor-not-allowed"
title="Replace the selected text with AI's fix">
✨ AI: Fix selection
</button>
<button @click="aiGenerate()"
:disabled="aiBusy"
class="px-4 py-2 text-xs font-mono text-text-muted hover:text-orange-500 border-r border-border-whisper transition-colors disabled:opacity-30 disabled:cursor-not-allowed"
title="Draft the whole driver from probed registers + device info">
✨ AI: Generate from probed
</button>
<button @click="resetDriver()"
:disabled="!savedSnapshot || luaSource === savedSnapshot"
class="px-4 py-2 text-xs font-mono text-text-muted hover:text-destructive-red transition-colors disabled:opacity-30 disabled:cursor-not-allowed">
↺ Reset
</button>
<div class="ml-auto flex items-center px-4 py-2 text-[10px] font-mono text-text-muted">
<span class="w-1.5 h-1.5 rounded-full mr-1.5"
:class="dirty ? 'bg-orange-500' : 'bg-confirmed-green'"></span>
<span x-text="dirty ? 'unsaved' : 'saved'"></span>
</div>
</div>
</main>
<!-- ==============================================
LIVE PANEL (right)
============================================== -->
<aside class="flex flex-col min-h-0"
:class="(mobileTab !== 'live' && 'hidden md:flex') || ''">
<div class="px-5 py-3 border-b border-border-whisper flex items-center justify-between flex-shrink-0">
<span class="micro-label text-orange-500">Live</span>
<span class="font-mono text-[11px]"
:class="testing ? 'text-orange-500' : (lastTest?.ok ? 'text-confirmed-green' : (lastTest ? 'text-destructive-red' : 'text-text-muted'))"
x-text="testing ? 'running…' : (lastTest?.ok ? 'ok' : (lastTest ? 'failed' : 'idle'))"></span>
</div>
<div class="flex-1 scroll-y px-5 py-4 space-y-4">
<!-- No-runs empty state — live mode -->
<div x-show="!lastTest && !testing && mode !== 'manual'" x-cloak class="text-center py-8">
<div class="micro-label text-text-muted mb-2">No runs yet</div>
<p class="text-xs text-text-muted leading-relaxed">
Click <span class="text-orange-500">▶ Run</span> to execute the driver against
<span x-text="config.host || 'the device'"></span>.
</p>
</div>
<!-- No-runs empty state — manual mode (no hardware to test
against, so Run is disabled. The user should submit
once the spec looks right). -->
<div x-show="!lastTest && !testing && mode === 'manual'" x-cloak class="text-center py-8">
<div class="micro-label text-text-muted mb-2">Manual mode</div>
<p class="text-xs text-text-muted leading-relaxed max-w-xs mx-auto">
No live device paired. Edit the driver until the spec looks
right, then use
<span class="text-orange-500">Submit as PR</span> to ship it.
Real-device testing happens after the PR lands, by a maintainer
with the hardware.
</p>
</div>
<!-- Status block -->
<div x-show="lastTest" x-cloak class="border border-border-whisper p-3 grid grid-cols-3 gap-3">
<div>
<div class="micro-label text-text-muted text-[10px]">init</div>
<div class="font-mono text-sm" :class="lastTest?.init_ok ? 'text-confirmed-green' : 'text-destructive-red'"
x-text="(lastTest?.init_ok ? '✓' : '✗') + ' ' + (lastTest?.init_ms || 0) + 'ms'"></div>
</div>
<div>
<div class="micro-label text-text-muted text-[10px]">emissions</div>
<div class="font-mono text-sm text-ink" x-text="(lastTest?.emissions?.length || 0)"></div>
</div>
<div>
<div class="micro-label text-text-muted text-[10px]">errors</div>
<div class="font-mono text-sm" :class="(lastTest?.errors?.length || 0) ? 'text-destructive-red' : 'text-confirmed-green'"
x-text="(lastTest?.errors?.length || 0)"></div>
</div>
</div>
<!-- Errors -->
<div x-show="lastTest?.errors?.length" x-cloak class="border border-destructive-red/50 bg-destructive-red/5 p-3">
<div class="micro-label text-destructive-red mb-2">Errors</div>
<ul class="space-y-1">
<template x-for="(e, i) in (lastTest?.errors || [])" :key="i">
<li class="font-mono text-[11px] text-destructive-red break-words" x-text="e"></li>
</template>
</ul>
</div>
<!-- Emissions -->
<div x-show="lastTest?.emissions?.length" x-cloak>
<div class="micro-label text-text-muted mb-2 flex items-center gap-2">
<span>Emissions <span class="font-mono normal-case tracking-normal" x-text="'(' + (lastTest?.emissions?.length || 0) + ')'"></span></span>
<template x-if="lastTest?._streaming">
<span class="font-mono normal-case tracking-normal text-[10px] text-orange-500 flex items-center gap-1">
<span class="w-1.5 h-1.5 rounded-full bg-orange-500 inline-block animate-pulse"></span>
live
</span>
</template>
</div>
<div class="space-y-2">
<template x-for="(em, i) in (lastTest?.emissions || []).slice().reverse()" :key="i">
<div class="border border-border-subtle p-2.5">
<div class="flex items-center justify-between mb-1.5">
<span class="micro-label text-orange-500" x-text="em.channel || em.kind || 'emit'"></span>
<span class="font-mono text-[10px] text-text-muted" x-text="em.ts ? new Date(em.ts).toLocaleTimeString() : ''"></span>
</div>
<table class="w-full font-mono text-[11px]">
<template x-for="[k, v] in Object.entries(em.data || em.payload || em)" :key="k">
<tr x-show="!['channel','kind','ts','timestamp','data','payload'].includes(k)">
<td class="text-text-muted pr-2 align-top" x-text="k"></td>
<td class="text-ink break-all" x-text="(typeof v === 'object' ? JSON.stringify(v) : String(v))"></td>
</tr>
</template>
</table>
</div>
</template>
</div>
</div>
<!-- Metrics — driver-emitted gauges/counters via
host.emit_metric. Renders as a simple two-column table
when the driver published any. -->
<div x-show="lastTest?.metrics && Object.keys(lastTest.metrics).length" x-cloak>
<div class="micro-label text-text-muted mb-2">Metrics</div>
<div class="border border-border-whisper bg-cream p-3 font-mono text-[11px]">
<template x-for="[name, value] in Object.entries(lastTest?.metrics || {})" :key="name">
<div class="flex justify-between gap-3 py-0.5">
<span class="text-text-muted truncate" x-text="name"></span>
<span class="text-ink shrink-0" x-text="value"></span>
</div>
</template>
</div>
</div>
<!-- Logs -->
<div x-show="lastTest?.logs?.length" x-cloak>
<button @click="logsOpen = !logsOpen"
class="micro-label text-text-muted hover:text-ink flex items-center gap-2">
<span x-text="logsOpen ? '▼' : '▶'"></span>
<span>Logs <span class="font-mono normal-case tracking-normal" x-text="'(' + (lastTest?.logs?.length || 0) + ')'"></span></span>
</button>
<pre x-show="logsOpen" x-cloak class="mt-2 font-mono text-[11px] text-text-muted bg-cream border border-border-whisper p-3 overflow-x-auto whitespace-pre-wrap"
x-text="(lastTest?.logs || []).join('\n')"></pre>
</div>
<!-- PR success -->
<div x-show="prURL" x-cloak class="border border-confirmed-green p-3">
<div class="micro-label text-confirmed-green mb-2">PR opened</div>
<a :href="prURL" target="_blank" rel="noopener"
class="font-mono text-xs text-ink hover:text-orange-500 break-all" x-text="prURL"></a>
</div>
<!-- Error toast -->
<div x-show="error" x-cloak class="border border-destructive-red/50 bg-destructive-red/5 p-3">
<div class="micro-label text-destructive-red mb-1">Error</div>
<div class="font-mono text-[11px] text-destructive-red break-words" x-text="error"></div>
</div>
</div>
</aside>
</div>
</section>
<!-- =========================================================
PROBE MODAL
========================================================= -->
<div x-show="probeOpen" x-cloak
@keydown.escape.window="probeOpen = false"
class="fixed inset-0 bg-paper/95 backdrop-blur-sm flex items-center justify-center z-50 px-6"
@click.self="probeOpen = false">
<div class="bg-paper border border-border-whisper p-6 max-w-md w-full">
<div class="micro-label text-orange-500 mb-4">Probe register</div>
<div class="space-y-3">
<label class="block">
<span class="micro-label text-text-muted">Address (decimal or 0x…)</span>
<input x-model="probe.addrInput" type="text" placeholder="0x4000"
class="mt-1 w-full bg-transparent text-sm font-mono text-ink py-2 px-2 border border-border-whisper focus:border-orange-500 focus:outline-none focus:ring-0" />
<!-- Live parse preview. Empty input is silent; valid input
shows decimal + hex + the resulting register range;
invalid input shows a red warning. Catches typos
before the user pays the round-trip cost. -->
<p class="mt-1 text-[11px] font-mono break-words"
:class="probePreview().ok ? 'text-text-muted' : (probe.addrInput ? 'text-destructive-red' : 'text-text-muted')"
x-text="probePreview().label"></p>
</label>
<div class="grid grid-cols-2 gap-3">
<label class="block">
<span class="micro-label text-text-muted">Count</span>
<input x-model.number="probe.count" type="number" min="1" max="125"
class="mt-1 w-full bg-transparent text-sm font-mono text-ink py-2 px-2 border border-border-whisper focus:border-orange-500 focus:outline-none focus:ring-0" />
</label>
<label class="block">
<span class="micro-label text-text-muted">Decode</span>
<select x-model="probe.kind"
class="mt-1 w-full bg-paper text-sm font-mono text-ink py-2 px-2 border border-border-whisper focus:border-orange-500 focus:outline-none focus:ring-0">
<option>u16</option><option>i16</option>
<option>u32_be</option><option>u32_le</option>
<option>i32_be</option><option>i32_le</option>
<option>f32_be</option><option>f32_le</option>
</select>
</label>
</div>
</div>
<div class="mt-5 flex justify-end gap-3">
<button @click="probeOpen = false" class="text-xs font-mono text-text-muted hover:text-ink">cancel</button>
<button @click="runProbe()"
:disabled="probing"
class="bg-orange-500 hover:bg-orange-600 text-paper px-4 py-2 text-sm font-medium rounded-none transition-colors disabled:opacity-40 disabled:cursor-not-allowed">
<span x-show="!probing">Probe</span>
<span x-show="probing" x-cloak>Probing…</span>
</button>
</div>
<div x-show="probeError" x-cloak class="mt-4 border border-destructive-red/50 bg-destructive-red/5 p-3 font-mono text-[11px] text-destructive-red break-words" x-text="probeError"></div>
</div>
</div>
<!-- =========================================================
SCRIPT
========================================================= -->
<script>
const API_BASE = 'https://api.hugin.sourceful-labs.net';
const FETCH_TIMEOUT_MS = 60000;
const DRAFTS_KEY = 'hugin.drafts';
const DRAFT_LIMIT = 20;
// System prompt — copied verbatim from chat.html so the AI keeps the same
// behaviour & sign conventions. The driver-format spec, host API surface,
// and "treat agent results as ground truth" rule all live here.
const SYSTEM_PROMPT = `You are Hugin's driver-authoring assistant. You help users write Lua drivers for forty-two-watts (an open-source EMS) — drivers that get submitted as PRs to frahlg/forty-two-watts.
You operate inside the Hugin **Workbench**: a 3-pane UI (Device · Driver · Live). The user can scan their LAN, probe registers, edit Lua in a code editor, and run the driver against real hardware. You will be invoked from any of the panels — read the context block carefully to know what's being asked.
## Driver format
A forty-two-watts driver is a single .lua file with:
\`\`\`lua
DRIVER = {
id = "manufacturer_model",
name = "Manufacturer Model Name",
manufacturer = "Manufacturer",
version = "1.0.0",
protocols = { "modbus" },
capabilities = { "meter", "pv" },
tested_models = { "MODEL-X" },
verification_status = "experimental",
verification_notes = "Drafted via Hugin workbench; not yet verified on live hardware.",
description = "One-sentence purpose.",
homepage = "https://manufacturer.com/product",
connection_defaults = { port = 502, slave_id = 1 },
}
PROTOCOL = "modbus"
function driver_init(config)
host.set_make("Manufacturer")
host.set_sn(config.serial or "unknown")
end
function driver_poll()
local power = host.modbus_read(0x4000, 2, "i32_be")
if power then host.emit("meter", { power_w = -power }) end
return 5000
end
function driver_command(action, power_w, cmd) end
function driver_default_mode() end
function driver_cleanup() end
\`\`\`
## Sign convention (DON'T BREAK)
- Positive watts = flowing INTO the site at the grid meter boundary
- Grid import = +, grid export = −
- PV generation = always negative
- Battery: charge = +, discharge = −
- The driver is the ONLY layer that flips signs
## Host API
- host.log(level, msg)
- host.millis()
- host.set_poll_interval(ms), host.set_make(s), host.set_sn(s)
- host.emit("meter"|"pv"|"battery"|"ev", { ... })
- host.emit_metric(name, value)
- host.modbus_read(addr, count, kind) — kind ∈ u16|i16|u32_be|u32_le|i32_be|i32_le|f32_be|f32_le
- host.modbus_write(addr, value), host.modbus_write_multi(addr, values)
- host.mqtt_sub(topic), host.mqtt_pub(topic, payload), host.mqtt_messages()
- host.http_get(url, headers), host.http_post(url, body, headers)
- host.json_encode, host.json_decode
- host.decode_u32_le, host.decode_u32_be, host.decode_i32_le, host.decode_i32_be, host.decode_i16
## How to behave
- Be terse. Energy-tech users are technical; skip pleasantries.
- When asked to produce a driver, output **one full \`\`\`lua block** (no diffs, no partial files).
- When asked to fix a snippet, output ONLY the replacement Lua — no prose, no fences, no explanations. The result will be pasted directly into the editor.
- Treat probed-register values & test results as **ground truth from real hardware** — they override anything the user previously stated. If a register read fails, change addr or word order. If sign looks off, flip it.
- Don't invent registers. If you don't know, say so and ask the user to probe / paste the manual.`;
function workbench() {
return {
// Auth
apiBase: API_BASE,
token: null,
user: null,
// Agent pairing
//
// Two pairing kinds:
// - 'local' : agentURL + agentToken (HTTP-direct against
// an agent on the user's own machine).
// - 'remote' : agentId + agentNatsURL + agentCreds (NATS
// request/reply against the broker).
//
// localStorage is the source of truth — settings.html writes,
// workbench.html reads. Storage events keep tabs in sync.
agentKind: 'local', // 'local' | 'remote'
agentURL: '', // local mode
agentToken: '', // local mode
agentId: '', // remote mode
agentNatsURL: '', // remote mode
agentCreds: null, // remote mode {jwt, seed}
agentCapabilities: [],
// Remote-mode runtime — populated lazily on first agentCall.
// Not persisted; reconstructed from agentCreds on every page
// load. agentOnline reflects 30s-windowed presence heartbeat.
_natsConn: null,
_natsConnect: null, // in-flight promise during dial
_presenceUnsub: null,
_presenceTimer: null,
agentOnline: false,
agentPresenceTs: 0,
// agentReady is the transport-agnostic "we can issue calls"
// flag. UI guards bind to this instead of agentURL so remote
// pairings light the same buttons up.
agentReady: false,
// Top-level
error: null,
mobileTab: 'driver',
// Empty state
manualOpen: false,
manual: { label: '', specs: '' },
lastScan: null,
scanning: false,
drafts: [],
// Workbench state
workbenchActive: false,
mode: 'live', // 'live' | 'manual'
draftId: null,
device: null,
config: { host: '', port: 502, slave_id: 1 },
probedRegisters: [],
notes: '',
luaSource: '',
savedSnapshot: '',
dirty: false,
savedFlash: false,
_saveTimer: null,
// Driver editor
cm: null,
aiStreamingToEditor: false,
aiBusy: false,
// AI-Ask thread (DEVICE panel)
aiThread: [],
askInput: '',
askStreaming: false,
// Test
testing: false,
lastTest: null,
logsOpen: false,
// Run-lua duration, user-adjustable. The agent caps internally
// (5min hard timeout); we keep the UI to 5–120s as the sweet
// spot — long enough to see a few poll cycles, short enough
// that "I made a typo" isn't punished with a 2-minute wait.
runDurationSec: 30,
// Probe modal
probeOpen: false,
probing: false,
probeError: null,
probe: { addrInput: '0x4000', count: 2, kind: 'i32_be' },
// PR
submitting: false,
prURL: null,
// ===========================================================
// INIT
// ===========================================================
init() {
this.token = localStorage.getItem('hugin.gh_token');
if (this.token) this.fetchMe();
this.refreshAgentPairing();
this.loadDrafts();
// Other-tab pairing or token changes
window.addEventListener('storage', (e) => {