command_tower 0.21.1 → 0.22.0

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.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/concerns/command_tower/execution/http_boundary.rb +2 -1
  3. data/docs/api_reference.md +7 -3
  4. data/docs/bootstrap/11-auth-client-path.md +2 -0
  5. data/docs/extending.md +1 -1
  6. data/docs/messaging_integration_guide.md +2 -0
  7. data/docs/ui.md +138 -0
  8. data/docs/upgrades/0.22.0.md +29 -0
  9. data/docs/upgrades/README.md +1 -0
  10. data/lib/command_tower/configuration/config.rb +6 -0
  11. data/lib/command_tower/configuration/ui/config.rb +47 -0
  12. data/lib/command_tower/current.rb +1 -0
  13. data/lib/command_tower/execution.rb +2 -1
  14. data/lib/command_tower/ui/builder/frame.rb +19 -0
  15. data/lib/command_tower/ui/builder/item_builder.rb +27 -0
  16. data/lib/command_tower/ui/builder/token_resolver.rb +54 -0
  17. data/lib/command_tower/ui/builder.rb +157 -0
  18. data/lib/command_tower/ui/catalog.rb +18 -0
  19. data/lib/command_tower/ui/composer.rb +45 -0
  20. data/lib/command_tower/ui/document.rb +167 -0
  21. data/lib/command_tower/ui/email/layout.rb +95 -0
  22. data/lib/command_tower/ui/email/renderer/containers.rb +72 -0
  23. data/lib/command_tower/ui/email/renderer/inline_nodes.rb +70 -0
  24. data/lib/command_tower/ui/email/renderer/styling.rb +91 -0
  25. data/lib/command_tower/ui/email/renderer/text_nodes.rb +78 -0
  26. data/lib/command_tower/ui/email/renderer.rb +89 -0
  27. data/lib/command_tower/ui/email/styles.rb +77 -0
  28. data/lib/command_tower/ui/fragment.rb +54 -0
  29. data/lib/command_tower/ui/fragments/cta_group.rb +60 -0
  30. data/lib/command_tower/ui/fragments/fact_row.rb +20 -0
  31. data/lib/command_tower/ui/fragments/facts.rb +35 -0
  32. data/lib/command_tower/ui/fragments/header.rb +51 -0
  33. data/lib/command_tower/ui/fragments/notice_frame.rb +36 -0
  34. data/lib/command_tower/ui/hardening.rb +73 -0
  35. data/lib/command_tower/ui/hostname.rb +124 -0
  36. data/lib/command_tower/ui/legacy_mapping.rb +66 -0
  37. data/lib/command_tower/ui/limits.rb +19 -0
  38. data/lib/command_tower/ui/nodes/action.rb +53 -0
  39. data/lib/command_tower/ui/nodes/avatar.rb +35 -0
  40. data/lib/command_tower/ui/nodes/badge.rb +17 -0
  41. data/lib/command_tower/ui/nodes/banner.rb +17 -0
  42. data/lib/command_tower/ui/nodes/base.rb +125 -0
  43. data/lib/command_tower/ui/nodes/button.rb +34 -0
  44. data/lib/command_tower/ui/nodes/card.rb +19 -0
  45. data/lib/command_tower/ui/nodes/code.rb +28 -0
  46. data/lib/command_tower/ui/nodes/component.rb +39 -0
  47. data/lib/command_tower/ui/nodes/divider.rb +11 -0
  48. data/lib/command_tower/ui/nodes/facts.rb +14 -0
  49. data/lib/command_tower/ui/nodes/image.rb +41 -0
  50. data/lib/command_tower/ui/nodes/instant.rb +59 -0
  51. data/lib/command_tower/ui/nodes/key_value.rb +63 -0
  52. data/lib/command_tower/ui/nodes/span.rb +76 -0
  53. data/lib/command_tower/ui/nodes/stack.rb +24 -0
  54. data/lib/command_tower/ui/nodes/stat_tile.rb +20 -0
  55. data/lib/command_tower/ui/nodes/text.rb +24 -0
  56. data/lib/command_tower/ui/text_derivation.rb +89 -0
  57. data/lib/command_tower/ui/time_formatter.rb +59 -0
  58. data/lib/command_tower/ui/tokens/ui_token_mapping.json +277 -0
  59. data/lib/command_tower/ui/tokens.rb +56 -0
  60. data/lib/command_tower/ui/zone_abbreviation.rb +130 -0
  61. data/lib/command_tower/ui.rb +80 -0
  62. data/lib/command_tower/version.rb +1 -1
  63. data/lib/command_tower.rb +1 -0
  64. data/lib/tasks/ui.rake +16 -0
  65. metadata +67 -1
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module UI
5
+ module Nodes
6
+ class StatTile < Node
7
+ TYPE = "stat_tile"
8
+
9
+ field :value, :string
10
+ field :label, :string
11
+ field :tone, :string
12
+ field :icon, :string
13
+
14
+ validates :value, :label, presence: true
15
+ validates :tone, inclusion: { in: Tokens::TONE }, allow_nil: true
16
+ validates :icon, format: { with: ICON_FORMAT }, allow_nil: true
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module UI
5
+ module Nodes
6
+ # `textTone` accepts only primary, secondary, muted and accent (council
7
+ # A2): success, warning and danger fail contrast as text and stay tone
8
+ # tokens for badges, tiles, banners and cards.
9
+ class Text < Node
10
+ TYPE = "text"
11
+
12
+ field :variant, :string, default: "body"
13
+ field :text_tone, :string, default: "primary"
14
+ field :max_lines, :integer
15
+ include Spanned
16
+
17
+ validates :variant, inclusion: { in: Tokens::VARIANT }
18
+ validates :text_tone, inclusion: { in: Tokens::TEXT_TONE }
19
+ validates :max_lines, numericality: { only_integer: true, greater_than: 0, less_than_or_equal_to: 100 },
20
+ allow_nil: true
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module UI
5
+ # Derives plain text from a node tree for `textFallback`, logs and
6
+ # text-only channels.
7
+ #
8
+ # - text and banner: spans concatenated (banner title first, on its own line)
9
+ # - time: TimeFormatter with the zone label always shown
10
+ # - component: its fallback tree
11
+ # - key_value: "Label: value" lines; stat_tile: "label: value"
12
+ # - code: excluded (design P16: a code value never reaches derived text)
13
+ # - containers (stack, card, facts): non-empty children, one per line
14
+ #
15
+ # Any node can be derived alone, so `facts` works without its document.
16
+ module TextDerivation
17
+ HANDLERS = {
18
+ Nodes::Container => :container_text,
19
+ Nodes::Text => :text_text,
20
+ Nodes::Banner => :banner_text,
21
+ Nodes::Instant => :instant_text,
22
+ Nodes::KeyValue => :key_value_text,
23
+ Nodes::Component => :component_text,
24
+ Nodes::StatTile => :stat_tile_text,
25
+ Nodes::Badge => :label_text,
26
+ Nodes::Button => :label_text,
27
+ Nodes::Image => :image_text
28
+ }.freeze
29
+
30
+ class << self
31
+ def call(node)
32
+ _, handler = HANDLERS.find { |klass, _| node.is_a?(klass) }
33
+ handler ? send(handler, node) : "" # divider, avatar, code
34
+ end
35
+
36
+ private
37
+
38
+ def container_text(node)
39
+ lines(node.children.map { |child| call(child) })
40
+ end
41
+
42
+ def text_text(node)
43
+ spans_text(node.spans)
44
+ end
45
+
46
+ def banner_text(node)
47
+ lines([node.title, spans_text(node.spans)])
48
+ end
49
+
50
+ def instant_text(node)
51
+ TimeFormatter.call(at: node.at, zone: node.zone, format: node.format, label: :always)
52
+ end
53
+
54
+ def key_value_text(node)
55
+ lines(node.items.map { |item| item_text(item) })
56
+ end
57
+
58
+ def component_text(node)
59
+ call(node.fallback)
60
+ end
61
+
62
+ def stat_tile_text(node)
63
+ "#{node.label}: #{node.value}"
64
+ end
65
+
66
+ def label_text(node)
67
+ node.label.to_s
68
+ end
69
+
70
+ def image_text(node)
71
+ node.decorative ? "" : node.alt.to_s
72
+ end
73
+
74
+ def item_text(item)
75
+ value = item.value.is_a?(Nodes::Instant) ? instant_text(item.value) : item.value.to_s
76
+ item.label.to_s.empty? ? value : "#{item.label}: #{value}"
77
+ end
78
+
79
+ def spans_text(spans)
80
+ spans.map(&:text).join
81
+ end
82
+
83
+ def lines(parts)
84
+ parts.map(&:to_s).reject(&:empty?).join("\n")
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "time"
4
+ require "tzinfo"
5
+
6
+ module CommandTower
7
+ module UI
8
+ # Server-side rendering of an instant for a zone (design section 3.2, states section 5.I).
9
+ #
10
+ # en-US, 12-hour clock, no leading zeros, uppercase AM/PM, plain U+0020 separators.
11
+ # `:countdown` is app-only; the server renders it as `:short`. `:date` is never labelled.
12
+ # Label `:auto` is always shown on the server. The label comes from {ZoneAbbreviation}.
13
+ module TimeFormatter
14
+ PATTERNS = {
15
+ short: "%a, %b %-d, %-l:%M %p",
16
+ long: "%A, %B %-d, %Y at %-l:%M %p",
17
+ date: "%a, %b %-d",
18
+ time: "%-l:%M %p"
19
+ }.freeze
20
+ FORMATS = (PATTERNS.keys + [:countdown]).freeze
21
+ LABELS = %i[auto always never].freeze
22
+
23
+ class << self
24
+ # `at` is a Time or a UTC ISO-8601 string; `zone` a TZInfo identifier.
25
+ def call(at:, zone:, format:, label: :auto)
26
+ format = pick(format, FORMATS, "format")
27
+ label = pick(label, LABELS, "label")
28
+ instant = parse(at)
29
+ timezone = TZInfo::Timezone.get(zone.to_s)
30
+ text = timezone.to_local(instant).strftime(PATTERNS.fetch(format == :countdown ? :short : format))
31
+ return text unless show_label?(format, label)
32
+
33
+ "#{text} #{ZoneAbbreviation.label(timezone.identifier, instant, mapping: abbreviations)}"
34
+ end
35
+
36
+ private
37
+
38
+ def pick(value, allowed, name)
39
+ symbol = value.to_sym
40
+ allowed.include?(symbol) ? symbol : raise(ArgumentError, "unknown time #{name}")
41
+ end
42
+
43
+ def show_label?(format, label)
44
+ return false if format == :date
45
+
46
+ label != :never
47
+ end
48
+
49
+ def abbreviations
50
+ Tokens.mapping.fetch("zoneAbbreviations")
51
+ end
52
+
53
+ def parse(at)
54
+ (at.is_a?(String) ? Time.iso8601(at) : at).utc
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,277 @@
1
+ {
2
+ "fixtureVersion": 1,
3
+ "spacing": {
4
+ "none": 0,
5
+ "xs": 4,
6
+ "sm": 8,
7
+ "md": 12,
8
+ "lg": 16,
9
+ "xl": 24
10
+ },
11
+ "typeSteps": {
12
+ "display": { "typography": "display", "size": 36, "lineHeight": 40, "weight": 700, "tracking": "-0.025em", "uppercase": false, "mono": false, "defaultTextTone": "primary" },
13
+ "heading_xl": { "typography": "headingXl", "size": 30, "lineHeight": 36, "weight": 600, "tracking": "-0.025em", "uppercase": false, "mono": false, "defaultTextTone": "primary" },
14
+ "heading_l": { "typography": "headingL", "size": 24, "lineHeight": 32, "weight": 600, "tracking": null, "uppercase": false, "mono": false, "defaultTextTone": "primary" },
15
+ "heading_m": { "typography": "headingM", "size": 20, "lineHeight": 28, "weight": 600, "tracking": null, "uppercase": false, "mono": false, "defaultTextTone": "primary" },
16
+ "title": { "typography": "title", "size": 18, "lineHeight": 28, "weight": 500, "tracking": null, "uppercase": false, "mono": false, "defaultTextTone": "primary" },
17
+ "subtitle": { "typography": "subtitle", "size": 16, "lineHeight": 24, "weight": 500, "tracking": null, "uppercase": false, "mono": false, "defaultTextTone": "secondary" },
18
+ "body": { "typography": "body", "size": 16, "lineHeight": 24, "weight": 400, "tracking": null, "uppercase": false, "mono": false, "defaultTextTone": "primary" },
19
+ "caption": { "typography": "caption", "size": 14, "lineHeight": 20, "weight": 400, "tracking": null, "uppercase": false, "mono": false, "defaultTextTone": "secondary" },
20
+ "overline": { "typography": "overline", "size": 12, "lineHeight": 16, "weight": 500, "tracking": "0.1em", "uppercase": true, "mono": false, "defaultTextTone": "secondary" },
21
+ "mono": { "typography": "mono", "size": 14, "lineHeight": 20, "weight": 400, "tracking": null, "uppercase": false, "mono": true, "defaultTextTone": "primary" }
22
+ },
23
+ "fonts": {
24
+ "sans": "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif",
25
+ "mono": "ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace"
26
+ },
27
+ "tones": {
28
+ "neutral": { "paletteKey": "muted", "borderPaletteKey": "border" },
29
+ "accent": { "paletteKey": "primary", "borderPaletteKey": "primary" },
30
+ "success": { "paletteKey": "success", "borderPaletteKey": "success" },
31
+ "warning": { "paletteKey": "warning", "borderPaletteKey": "warning" },
32
+ "danger": { "paletteKey": "danger", "borderPaletteKey": "danger" }
33
+ },
34
+ "tintBackgroundAlpha": 0.10,
35
+ "tintBorderAlpha": 0.30,
36
+ "neutralTintBackgroundAlpha": 0.40,
37
+ "neutralTintBorder": "border",
38
+ "textTones": {
39
+ "primary": "textPrimary",
40
+ "secondary": "textSecondary",
41
+ "muted": "mutedForeground",
42
+ "accent": "primary"
43
+ },
44
+ "avatarSizes": {
45
+ "sm": 24,
46
+ "md": 32,
47
+ "lg": 48
48
+ },
49
+ "timeCases": [
50
+ { "at": "2025-12-04T14:00:00Z", "zone": "America/Los_Angeles", "format": "short", "label": "always", "expected": "Thu, Dec 4, 6:00 AM PT" },
51
+ { "at": "2025-12-04T14:00:00Z", "zone": "America/New_York", "format": "short", "label": "always", "expected": "Thu, Dec 4, 9:00 AM ET" },
52
+ { "at": "2025-12-04T14:00:00Z", "zone": "America/Los_Angeles", "format": "long", "label": "always", "expected": "Thursday, December 4, 2025 at 6:00 AM PT" },
53
+ { "at": "2025-12-04T14:00:00Z", "zone": "America/Los_Angeles", "format": "date", "label": "always", "expected": "Thu, Dec 4" },
54
+ { "at": "2025-12-04T14:00:00Z", "zone": "America/Los_Angeles", "format": "time", "label": "never", "expected": "6:00 AM" },
55
+ { "at": "2025-12-05T07:30:00Z", "zone": "America/Anchorage", "format": "short", "label": "always", "expected": "Thu, Dec 4, 10:30 PM AKT" },
56
+ { "at": "2025-07-01T19:30:00Z", "zone": "America/Phoenix", "format": "time", "label": "always", "expected": "12:30 PM MST" },
57
+ { "at": "2025-12-04T14:00:00Z", "zone": "Pacific/Honolulu", "format": "short", "label": "always", "expected": "Thu, Dec 4, 4:00 AM HST" },
58
+ { "at": "2025-12-04T14:00:00Z", "zone": "Asia/Kolkata", "format": "long", "label": "always", "expected": "Thursday, December 4, 2025 at 7:30 PM GMT+5:30" },
59
+ { "at": "2025-12-04T14:00:00Z", "zone": "Europe/London", "format": "time", "label": "always", "expected": "2:00 PM GMT" },
60
+ { "at": "2025-07-01T14:00:00Z", "zone": "Europe/London", "format": "time", "label": "always", "expected": "3:00 PM GMT+1" },
61
+ { "at": "2025-07-01T14:00:00Z", "zone": "Europe/Paris", "format": "time", "label": "always", "expected": "4:00 PM GMT+2" },
62
+ { "at": "2025-12-04T14:00:00Z", "zone": "UTC", "format": "short", "label": "always", "expected": "Thu, Dec 4, 2:00 PM UTC" },
63
+ { "at": "2025-12-05T08:00:00Z", "zone": "America/Los_Angeles", "format": "short", "label": "always", "expected": "Fri, Dec 5, 12:00 AM PT" },
64
+ { "at": "2025-12-04T20:00:00Z", "zone": "America/Los_Angeles", "format": "time", "label": "always", "expected": "12:00 PM PT" },
65
+ { "at": "2025-12-04T14:00:00Z", "zone": "Europe/Dublin", "format": "time", "label": "always", "expected": "2:00 PM GMT" },
66
+ { "at": "2025-07-01T14:00:00Z", "zone": "Europe/Dublin", "format": "time", "label": "always", "expected": "3:00 PM GMT+1" },
67
+ { "at": "2025-03-09T10:00:00Z", "zone": "America/Los_Angeles", "format": "short", "label": "always", "expected": "Sun, Mar 9, 3:00 AM PT" },
68
+ { "at": "2025-11-02T09:00:00Z", "zone": "America/Los_Angeles", "format": "short", "label": "always", "expected": "Sun, Nov 2, 1:00 AM PT" },
69
+ { "at": "2025-07-01T14:00:00Z", "zone": "America/New_York", "format": "time", "label": "always", "expected": "10:00 AM ET" },
70
+ { "at": "2025-07-01T14:00:00Z", "zone": "America/Anchorage", "format": "time", "label": "always", "expected": "6:00 AM AKT" }
71
+ ],
72
+ "zoneAbbreviations": {
73
+ "Africa/Abidjan": "GMT",
74
+ "Africa/Accra": "GMT",
75
+ "Africa/Addis_Ababa": "EAT",
76
+ "Africa/Algiers": "CET",
77
+ "Africa/Asmara": "EAT",
78
+ "Africa/Bamako": "GMT",
79
+ "Africa/Bangui": "WAT",
80
+ "Africa/Banjul": "GMT",
81
+ "Africa/Bissau": "GMT",
82
+ "Africa/Blantyre": "CAT",
83
+ "Africa/Brazzaville": "WAT",
84
+ "Africa/Bujumbura": "CAT",
85
+ "Africa/Conakry": "GMT",
86
+ "Africa/Dakar": "GMT",
87
+ "Africa/Dar_es_Salaam": "EAT",
88
+ "Africa/Djibouti": "EAT",
89
+ "Africa/Douala": "WAT",
90
+ "Africa/Freetown": "GMT",
91
+ "Africa/Gaborone": "CAT",
92
+ "Africa/Harare": "CAT",
93
+ "Africa/Johannesburg": "SAST",
94
+ "Africa/Juba": "CAT",
95
+ "Africa/Kampala": "EAT",
96
+ "Africa/Khartoum": "CAT",
97
+ "Africa/Kigali": "CAT",
98
+ "Africa/Kinshasa": "WAT",
99
+ "Africa/Lagos": "WAT",
100
+ "Africa/Libreville": "WAT",
101
+ "Africa/Lome": "GMT",
102
+ "Africa/Luanda": "WAT",
103
+ "Africa/Lubumbashi": "CAT",
104
+ "Africa/Lusaka": "CAT",
105
+ "Africa/Malabo": "WAT",
106
+ "Africa/Maputo": "CAT",
107
+ "Africa/Maseru": "SAST",
108
+ "Africa/Mbabane": "SAST",
109
+ "Africa/Mogadishu": "EAT",
110
+ "Africa/Monrovia": "GMT",
111
+ "Africa/Nairobi": "EAT",
112
+ "Africa/Ndjamena": "WAT",
113
+ "Africa/Niamey": "WAT",
114
+ "Africa/Nouakchott": "GMT",
115
+ "Africa/Ouagadougou": "GMT",
116
+ "Africa/Porto-Novo": "WAT",
117
+ "Africa/Sao_Tome": "GMT",
118
+ "Africa/Timbuktu": "GMT",
119
+ "Africa/Tripoli": "EET",
120
+ "Africa/Tunis": "CET",
121
+ "Africa/Windhoek": "CAT",
122
+ "America/Adak": "HT",
123
+ "America/Anchorage": "AKT",
124
+ "America/Anguilla": "AST",
125
+ "America/Antigua": "AST",
126
+ "America/Aruba": "AST",
127
+ "America/Atikokan": "EST",
128
+ "America/Atka": "HT",
129
+ "America/Barbados": "AST",
130
+ "America/Blanc-Sablon": "AST",
131
+ "America/Boise": "MT",
132
+ "America/Cambridge_Bay": "MT",
133
+ "America/Cancun": "EST",
134
+ "America/Cayman": "EST",
135
+ "America/Chicago": "CT",
136
+ "America/Ciudad_Juarez": "MT",
137
+ "America/Coral_Harbour": "EST",
138
+ "America/Creston": "MST",
139
+ "America/Curacao": "AST",
140
+ "America/Danmarkshavn": "GMT",
141
+ "America/Dawson": "MST",
142
+ "America/Dawson_Creek": "MST",
143
+ "America/Denver": "MT",
144
+ "America/Detroit": "ET",
145
+ "America/Dominica": "AST",
146
+ "America/Edmonton": "MT",
147
+ "America/Ensenada": "PT",
148
+ "America/Fort_Nelson": "MST",
149
+ "America/Glace_Bay": "AT",
150
+ "America/Goose_Bay": "AT",
151
+ "America/Grand_Turk": "ET",
152
+ "America/Grenada": "AST",
153
+ "America/Guadeloupe": "AST",
154
+ "America/Halifax": "AT",
155
+ "America/Havana": "CT",
156
+ "America/Hermosillo": "MST",
157
+ "America/Indiana/Indianapolis": "ET",
158
+ "America/Indiana/Knox": "CT",
159
+ "America/Indiana/Marengo": "ET",
160
+ "America/Indiana/Petersburg": "ET",
161
+ "America/Indiana/Tell_City": "CT",
162
+ "America/Indiana/Vevay": "ET",
163
+ "America/Indiana/Vincennes": "ET",
164
+ "America/Indiana/Winamac": "ET",
165
+ "America/Inuvik": "MT",
166
+ "America/Iqaluit": "ET",
167
+ "America/Jamaica": "EST",
168
+ "America/Juneau": "AKT",
169
+ "America/Kentucky/Louisville": "ET",
170
+ "America/Kentucky/Monticello": "ET",
171
+ "America/Kralendijk": "AST",
172
+ "America/Los_Angeles": "PT",
173
+ "America/Lower_Princes": "AST",
174
+ "America/Marigot": "AST",
175
+ "America/Martinique": "AST",
176
+ "America/Matamoros": "CT",
177
+ "America/Mazatlan": "MST",
178
+ "America/Menominee": "CT",
179
+ "America/Metlakatla": "AKT",
180
+ "America/Moncton": "AT",
181
+ "America/Montreal": "ET",
182
+ "America/Montserrat": "AST",
183
+ "America/Nassau": "ET",
184
+ "America/New_York": "ET",
185
+ "America/Nipigon": "ET",
186
+ "America/Nome": "AKT",
187
+ "America/North_Dakota/Beulah": "CT",
188
+ "America/North_Dakota/Center": "CT",
189
+ "America/North_Dakota/New_Salem": "CT",
190
+ "America/Ojinaga": "CT",
191
+ "America/Panama": "EST",
192
+ "America/Pangnirtung": "ET",
193
+ "America/Phoenix": "MST",
194
+ "America/Port-au-Prince": "ET",
195
+ "America/Port_of_Spain": "AST",
196
+ "America/Puerto_Rico": "AST",
197
+ "America/Rainy_River": "CT",
198
+ "America/Rankin_Inlet": "CT",
199
+ "America/Resolute": "CT",
200
+ "America/Santa_Isabel": "PT",
201
+ "America/Santo_Domingo": "AST",
202
+ "America/Shiprock": "MT",
203
+ "America/Sitka": "AKT",
204
+ "America/St_Barthelemy": "AST",
205
+ "America/St_Johns": "NT",
206
+ "America/St_Kitts": "AST",
207
+ "America/St_Lucia": "AST",
208
+ "America/St_Thomas": "AST",
209
+ "America/St_Vincent": "AST",
210
+ "America/Thule": "AT",
211
+ "America/Thunder_Bay": "ET",
212
+ "America/Tijuana": "PT",
213
+ "America/Toronto": "ET",
214
+ "America/Tortola": "AST",
215
+ "America/Vancouver": "PT",
216
+ "America/Virgin": "AST",
217
+ "America/Whitehorse": "MST",
218
+ "America/Winnipeg": "CT",
219
+ "America/Yakutat": "AKT",
220
+ "America/Yellowknife": "MT",
221
+ "Antarctica/Macquarie": "AET",
222
+ "Antarctica/McMurdo": "NZT",
223
+ "Asia/Hong_Kong": "HKT",
224
+ "Asia/Jakarta": "WIB",
225
+ "Asia/Jayapura": "WIT",
226
+ "Asia/Jerusalem": "IT",
227
+ "Asia/Karachi": "PKT",
228
+ "Asia/Makassar": "WITA",
229
+ "Asia/Pontianak": "WIB",
230
+ "Asia/Pyongyang": "KST",
231
+ "Asia/Seoul": "KST",
232
+ "Asia/Tel_Aviv": "IT",
233
+ "Asia/Tokyo": "JST",
234
+ "Atlantic/Bermuda": "AT",
235
+ "Atlantic/Reykjavik": "GMT",
236
+ "Atlantic/St_Helena": "GMT",
237
+ "Australia/Adelaide": "ACT",
238
+ "Australia/Brisbane": "AEST",
239
+ "Australia/Broken_Hill": "ACT",
240
+ "Australia/Canberra": "AET",
241
+ "Australia/Currie": "AET",
242
+ "Australia/Darwin": "ACST",
243
+ "Australia/Hobart": "AET",
244
+ "Australia/Lindeman": "AEST",
245
+ "Australia/Melbourne": "AET",
246
+ "Australia/Perth": "AWST",
247
+ "Australia/Sydney": "AET",
248
+ "Australia/Yancowinna": "ACT",
249
+ "Etc/GMT": "GMT",
250
+ "Etc/GMT+0": "GMT",
251
+ "Etc/GMT-0": "GMT",
252
+ "Etc/GMT0": "GMT",
253
+ "Etc/Greenwich": "GMT",
254
+ "Etc/UCT": "UTC",
255
+ "Etc/UTC": "UTC",
256
+ "Etc/Universal": "UTC",
257
+ "Etc/Zulu": "UTC",
258
+ "Europe/Kaliningrad": "EET",
259
+ "Europe/Kirov": "MSK",
260
+ "Europe/Moscow": "MSK",
261
+ "Europe/Simferopol": "MSK",
262
+ "Europe/Volgograd": "MSK",
263
+ "GMT": "GMT",
264
+ "Indian/Antananarivo": "EAT",
265
+ "Indian/Comoro": "EAT",
266
+ "Indian/Mayotte": "EAT",
267
+ "Pacific/Auckland": "NZT",
268
+ "Pacific/Honolulu": "HST",
269
+ "Pacific/Johnston": "HST",
270
+ "Pacific/Midway": "SST",
271
+ "Pacific/Pago_Pago": "SST",
272
+ "Pacific/Samoa": "SST",
273
+ "UTC": "UTC"
274
+ },
275
+ "ambiguousAbbreviations": ["CDT","CST","IST","PST"],
276
+ "tzdataVersion": "2026a"
277
+ }
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module CommandTower
6
+ module UI
7
+ # Token enums and the shared design mapping (spacing px, type steps, tone
8
+ # palette keys, tint alphas). The mapping JSON is runtime data, parsed once
9
+ # and deep-frozen. Spec: design ux/states.md section 5.
10
+ module Tokens
11
+ MAPPING_PATH = File.expand_path("tokens/ui_token_mapping.json", __dir__)
12
+
13
+ GAP = %w[none xs sm md lg xl].freeze
14
+ TONE = %w[neutral accent success warning danger].freeze
15
+ # Only these four text tones are allowed (council A2): warning/success/danger fail contrast as text.
16
+ TEXT_TONE = %w[primary secondary muted accent].freeze
17
+ VARIANT = %w[display heading_xl heading_l heading_m title subtitle body caption overline mono].freeze
18
+ SURFACE = %w[raised tinted plain].freeze
19
+ ALIGN = %w[start center end between].freeze
20
+ AVATAR_SIZE = %w[sm md lg].freeze
21
+ BUTTON_VARIANT = %w[primary secondary ghost].freeze
22
+
23
+ class << self
24
+ def mapping
25
+ @mapping ||= deep_freeze(JSON.parse(File.read(MAPPING_PATH)))
26
+ end
27
+
28
+ def spacing_px(token)
29
+ mapping.fetch("spacing").fetch(token.to_s) { raise InvalidTokenError, "unknown spacing token" }
30
+ end
31
+
32
+ def type_step(variant)
33
+ mapping.fetch("typeSteps").fetch(variant.to_s) { raise InvalidTokenError, "unknown type variant" }
34
+ end
35
+
36
+ def tone_palette_key(tone)
37
+ mapping.fetch("tones").fetch(tone.to_s) { raise InvalidTokenError, "unknown tone" }.fetch("paletteKey")
38
+ end
39
+
40
+ def avatar_px(size)
41
+ mapping.fetch("avatarSizes").fetch(size.to_s) { raise InvalidTokenError, "unknown avatar size" }
42
+ end
43
+
44
+ private
45
+
46
+ def deep_freeze(value)
47
+ case value
48
+ when Hash then value.each { |k, v| k.freeze && deep_freeze(v) }
49
+ when Array then value.each { |v| deep_freeze(v) }
50
+ end
51
+ value.freeze
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,130 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "tzinfo"
5
+
6
+ module CommandTower
7
+ module UI
8
+ # Time-zone label rule for server-rendered times (design section 3.2, states section 8 C).
9
+ #
10
+ # Rule, evaluated over the window 1 Jan of the generation year to 31 Dec of the next year:
11
+ # 1. The zone observes DST in the window and its standard/daylight abbreviations are
12
+ # alphabetic `<P>ST` / `<P>DT`: label is `<P>T` (PST/PDT -> PT).
13
+ # 2. No DST in the window, a single alphabetic abbreviation that maps to one UTC offset
14
+ # across all zones (unambiguous): the abbreviation as is (HST, MST in Phoenix, UTC).
15
+ # 3. Anything else: `GMT` for a zero offset, otherwise `GMT+h[:mm]` / `GMT-h[:mm]` at the instant.
16
+ #
17
+ # Rules 1 and 2 are generated into the token mapping (`zoneAbbreviations`, a zone id to label
18
+ # map); rule 3 zones are absent from it and are labelled from the offset at the instant.
19
+ # Historical DST does not count. Generate with `rake command_tower:ui:zone_abbreviations`.
20
+ module ZoneAbbreviation
21
+ MAPPING_PATH = File.expand_path("tokens/ui_token_mapping.json", __dir__)
22
+ GENERATED_KEYS = %w[zoneAbbreviations ambiguousAbbreviations tzdataVersion].freeze
23
+ ALPHABETIC = /\A[A-Z]+\z/
24
+ STANDARD = /\A([A-Z]+)ST\z/
25
+ FALLBACK_ZONEINFO_DIR = "/usr/share/zoneinfo"
26
+
27
+ class << self
28
+ # Label for `zone_id` at the UTC instant `at` (Time), using the generated mapping.
29
+ def label(zone_id, at, mapping: nil)
30
+ mapping ||= JSON.parse(File.read(MAPPING_PATH)).fetch("zoneAbbreviations")
31
+ mapping.fetch(zone_id) { gmt_label(TZInfo::Timezone.get(zone_id).period_for_utc(at.utc).offset) }
32
+ end
33
+
34
+ # `GMT`, `GMT+5:30`, `GMT-3` from a TZInfo offset.
35
+ def gmt_label(offset)
36
+ total = offset.observed_utc_offset
37
+ return "GMT" if total.zero?
38
+
39
+ minutes = total.abs / 60
40
+ hours, rest = minutes.divmod(60)
41
+ minutes_part = rest.zero? ? nil : format(":%02d", rest)
42
+ "GMT#{total.negative? ? '-' : '+'}#{hours}#{minutes_part}"
43
+ end
44
+
45
+ # The three generated keys for a generation year.
46
+ def generate(year: Time.now.utc.year)
47
+ zones = window_offsets(year)
48
+ ambiguous = ambiguous_abbreviations(zones)
49
+ labels = zones.to_h { |zone_id, offsets| [zone_id, fixed_label(offsets, ambiguous)] }.compact
50
+
51
+ { "zoneAbbreviations" => labels.sort.to_h, "ambiguousAbbreviations" => ambiguous.sort,
52
+ "tzdataVersion" => tzdata_version }
53
+ end
54
+
55
+ # Splices the generated keys into the mapping JSON as its last three keys and leaves
56
+ # every other byte of the file alone. Deterministic: same year and tzdata, same bytes.
57
+ def write!(year: Time.now.utc.year, path: MAPPING_PATH)
58
+ generated = generate(year: year)
59
+ text = File.read(path)
60
+ generated_block = /,\n "zoneAbbreviations":.*\z/m
61
+ head = text.match?(generated_block) ? text.sub(generated_block, "") : text.sub(/\s*\}\s*\z/, "")
62
+ File.write(path, "#{head},\n#{render(generated)}\n}\n")
63
+ generated
64
+ end
65
+
66
+ def tzdata_version
67
+ source = TZInfo::DataSource.get
68
+ dir = source.respond_to?(:zoneinfo_dir) ? source.zoneinfo_dir : FALLBACK_ZONEINFO_DIR
69
+ line = File.foreach(File.join(dir, "tzdata.zi")).first.to_s
70
+ line[/\A#\s*version\s+(\S+)/, 1] || raise("tzdata.zi has no version line")
71
+ end
72
+
73
+ private
74
+
75
+ # zone id => offsets (TZInfo::TimezoneOffset) in effect during the window.
76
+ def window_offsets(year)
77
+ from = Time.utc(year, 1, 1)
78
+ to = Time.utc(year + 2, 1, 1)
79
+ TZInfo::Timezone.all_identifiers.to_h do |zone_id|
80
+ zone = TZInfo::Timezone.get(zone_id)
81
+ offsets = [zone.period_for_utc(from).offset] + zone.transitions_up_to(to, from).map(&:offset)
82
+ [zone_id, offsets.uniq]
83
+ end
84
+ end
85
+
86
+ # Alphabetic abbreviations that map to more than one UTC offset across zones.
87
+ def ambiguous_abbreviations(zones)
88
+ seen = Hash.new { |hash, key| hash[key] = [] }
89
+ zones.each_value do |offsets|
90
+ offsets.each do |offset|
91
+ abbreviation = offset.abbreviation.to_s
92
+ seen[abbreviation] << offset.observed_utc_offset if abbreviation.match?(ALPHABETIC)
93
+ end
94
+ end
95
+ seen.select { |_, utc_offsets| utc_offsets.uniq.size > 1 }.keys
96
+ end
97
+
98
+ def fixed_label(offsets, ambiguous)
99
+ daylight, standard = offsets.partition(&:dst?)
100
+ return daylight_label(standard, daylight) if daylight.any?
101
+
102
+ fixed_standard_label(standard.map(&:abbreviation).uniq, ambiguous)
103
+ end
104
+
105
+ def fixed_standard_label(abbreviations, ambiguous)
106
+ only = abbreviations.first
107
+ only if abbreviations.one? && only.match?(ALPHABETIC) && ambiguous.exclude?(only)
108
+ end
109
+
110
+ def daylight_label(standard, daylight)
111
+ standard_abbreviations = standard.map(&:abbreviation).uniq
112
+ daylight_abbreviations = daylight.map(&:abbreviation).uniq
113
+ return unless standard_abbreviations.one? && daylight_abbreviations.one?
114
+
115
+ prefix = standard_abbreviations.first[STANDARD, 1]
116
+ "#{prefix}T" if prefix && daylight_abbreviations.first == "#{prefix}DT"
117
+ end
118
+
119
+ def render(generated)
120
+ zones = generated.fetch("zoneAbbreviations").map { |id, label| " #{id.to_json}: #{label.to_json}" }
121
+ [
122
+ %( "zoneAbbreviations": {\n#{zones.join(",\n")}\n },),
123
+ %( "ambiguousAbbreviations": #{generated.fetch('ambiguousAbbreviations').to_json},),
124
+ %( "tzdataVersion": #{generated.fetch('tzdataVersion').to_json})
125
+ ].join("\n")
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end