@nimiplatform/app-tools 0.1.3 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/LICENSE +198 -6
  2. package/README.md +83 -27
  3. package/bin/nimi-app.mjs +328 -45
  4. package/lib/app-access-declaration.mjs +49 -0
  5. package/lib/app-doctor-update.mjs +411 -149
  6. package/lib/app-pack.mjs +402 -0
  7. package/lib/app-project-lifecycle.mjs +1141 -0
  8. package/lib/app-scaffold-capabilities.mjs +769 -0
  9. package/lib/app-scaffold-default-source.mjs +21 -0
  10. package/lib/app-scaffold-profiles.mjs +1093 -0
  11. package/lib/app-scaffold.mjs +1266 -813
  12. package/lib/index.mjs +195 -42
  13. package/lib/simulator-conformance-ast.mjs +498 -0
  14. package/lib/simulator-conformance-css.mjs +116 -0
  15. package/lib/simulator-conformance-graph.mjs +757 -0
  16. package/lib/simulator-conformance.d.mts +14 -0
  17. package/lib/simulator-conformance.mjs +203 -0
  18. package/lib/simulator-css-profile.d.mts +27 -0
  19. package/lib/simulator-css-profile.mjs +531 -0
  20. package/lib/simulator-kit-export-resolution.mjs +70 -0
  21. package/lib/simulator-manifest.mjs +334 -0
  22. package/lib/windows-powershell.mjs +7 -0
  23. package/package.json +66 -3
  24. package/scripts/dev-shell.mjs +465 -0
  25. package/scripts/sync-app-source.mjs +300 -0
  26. package/scripts/sync-scaffold-versions.mjs +91 -0
  27. package/templates/app-source/LICENSE +21 -0
  28. package/templates/app-source/src/ai-studio-core/ai-config.ts +100 -0
  29. package/templates/app-source/src/ai-studio-core/ai-studio-core.css +338 -0
  30. package/templates/app-source/src/ai-studio-core/contexts.tsx +61 -0
  31. package/templates/app-source/src/ai-studio-core/history-policy.ts +447 -0
  32. package/templates/app-source/src/ai-studio-core/history.ts +824 -0
  33. package/templates/app-source/src/ai-studio-core/host-context.tsx +79 -0
  34. package/templates/app-source/src/ai-studio-core/index.ts +214 -0
  35. package/templates/app-source/src/ai-studio-core/messages/en.json +310 -0
  36. package/templates/app-source/src/ai-studio-core/messages/index.ts +47 -0
  37. package/templates/app-source/src/ai-studio-core/messages/zh.json +310 -0
  38. package/templates/app-source/src/ai-studio-core/module-registration.ts +109 -0
  39. package/templates/app-source/src/ai-studio-core/non-success-presentation.ts +65 -0
  40. package/templates/app-source/src/ai-studio-core/parameter-fields.tsx +247 -0
  41. package/templates/app-source/src/ai-studio-core/parameters.ts +110 -0
  42. package/templates/app-source/src/ai-studio-core/prompt-drafts.ts +69 -0
  43. package/templates/app-source/src/ai-studio-core/run-target.ts +123 -0
  44. package/templates/app-source/src/ai-studio-core/runtime-dispatcher.ts +35 -0
  45. package/templates/app-source/src/ai-studio-core/runtime-types.ts +86 -0
  46. package/templates/app-source/src/ai-studio-core/runtime.ts +306 -0
  47. package/templates/app-source/src/ai-studio-core/section-ai-testing-admission.ts +38 -0
  48. package/templates/app-source/src/ai-studio-core/section-ai-testing-ai-config.tsx +30 -0
  49. package/templates/app-source/src/ai-studio-core/section-ai-testing-artifact-preview.ts +40 -0
  50. package/templates/app-source/src/ai-studio-core/section-ai-testing-composer.tsx +257 -0
  51. package/templates/app-source/src/ai-studio-core/section-ai-testing-history.tsx +676 -0
  52. package/templates/app-source/src/ai-studio-core/section-ai-testing-input.ts +19 -0
  53. package/templates/app-source/src/ai-studio-core/section-ai-testing-output.tsx +291 -0
  54. package/templates/app-source/src/ai-studio-core/section-ai-testing-result.tsx +413 -0
  55. package/templates/app-source/src/ai-studio-core/section-ai-testing-run.ts +174 -0
  56. package/templates/app-source/src/ai-studio-core/section-ai-testing-studio-result.tsx +449 -0
  57. package/templates/app-source/src/ai-studio-core/section-ai-testing-surface.tsx +45 -0
  58. package/templates/app-source/src/ai-studio-core/section-ai-testing.tsx +494 -0
  59. package/templates/app-source/src/ai-studio-core/studio-directives.ts +33 -0
  60. package/templates/app-source/src/ai-studio-core/workspace.tsx +381 -0
  61. package/templates/app-source/src/product-modules/agent-center/index.tsx +27 -0
  62. package/templates/app-source/src/product-modules/agent-conversation/index.tsx +19 -0
  63. package/templates/app-source/src/product-modules/agent-realtime/index.tsx +29 -0
  64. package/templates/app-source/src/product-modules/kit-recipes/gallery.tsx +142 -0
  65. package/templates/app-source/src/product-modules/kit-recipes/index.tsx +19 -0
  66. package/templates/app-source/src/product-modules/kit-recipes/kit-component-gallery-data-recipes.tsx +336 -0
  67. package/templates/app-source/src/product-modules/kit-recipes/kit-component-gallery-demos.tsx +165 -0
  68. package/templates/app-source/src/product-modules/kit-recipes/kit-component-gallery-recipes.tsx +682 -0
  69. package/templates/app-source/src/product-modules/kit-recipes/kit-component-gallery-surface.tsx +341 -0
  70. package/templates/app-source/src/studio-modules/studio-create/descriptors.ts +26 -0
  71. package/templates/app-source/src/studio-modules/studio-create/index.ts +14 -0
  72. package/templates/app-source/src/studio-modules/studio-create/messages/en.json +67 -0
  73. package/templates/app-source/src/studio-modules/studio-create/messages/index.ts +5 -0
  74. package/templates/app-source/src/studio-modules/studio-create/messages/zh.json +67 -0
  75. package/templates/app-source/src/studio-modules/studio-create/parameter-panel.tsx +153 -0
  76. package/templates/app-source/src/studio-modules/studio-create/parameters.ts +70 -0
  77. package/templates/app-source/src/studio-modules/studio-create/registration.ts +46 -0
  78. package/templates/app-source/src/studio-modules/studio-create/runtime.ts +339 -0
  79. package/templates/app-source/src/studio-modules/studio-media/descriptors.ts +22 -0
  80. package/templates/app-source/src/studio-modules/studio-media/index.ts +12 -0
  81. package/templates/app-source/src/studio-modules/studio-media/messages/en.json +82 -0
  82. package/templates/app-source/src/studio-modules/studio-media/messages/index.ts +5 -0
  83. package/templates/app-source/src/studio-modules/studio-media/messages/zh.json +82 -0
  84. package/templates/app-source/src/studio-modules/studio-media/parameter-panel.tsx +283 -0
  85. package/templates/app-source/src/studio-modules/studio-media/parameters.ts +94 -0
  86. package/templates/app-source/src/studio-modules/studio-media/registration.ts +44 -0
  87. package/templates/app-source/src/studio-modules/studio-media/runtime.ts +113 -0
  88. package/templates/app-source/src/studio-modules/studio-voice/descriptors.ts +28 -0
  89. package/templates/app-source/src/studio-modules/studio-voice/index.ts +16 -0
  90. package/templates/app-source/src/studio-modules/studio-voice/messages/en.json +99 -0
  91. package/templates/app-source/src/studio-modules/studio-voice/messages/index.ts +5 -0
  92. package/templates/app-source/src/studio-modules/studio-voice/messages/zh.json +99 -0
  93. package/templates/app-source/src/studio-modules/studio-voice/parameter-panel.tsx +284 -0
  94. package/templates/app-source/src/studio-modules/studio-voice/parameters.ts +116 -0
  95. package/templates/app-source/src/studio-modules/studio-voice/registration.ts +59 -0
  96. package/templates/app-source/src/studio-modules/studio-voice/runtime.ts +347 -0
  97. package/templates/app-source/src/workbench-core/empty-state.tsx +31 -0
  98. package/templates/app-source/src/workbench-core/index.ts +17 -0
  99. package/templates/app-source/src/workbench-core/runtime-gate.tsx +131 -0
  100. package/templates/app-source/src/workbench-core/workbench-core.css +255 -0
  101. package/templates/app-source/src/workbench-core/workbench-core.tsx +140 -0
  102. package/templates/app-source.manifest.json +333 -0
  103. package/templates/default-starter/AGENTS.md +33 -0
  104. package/templates/default-starter/LICENSE +21 -0
  105. package/templates/default-starter/README.md +41 -0
  106. package/templates/default-starter/SECURITY.md +5 -0
  107. package/templates/default-starter/index.html +12 -0
  108. package/templates/default-starter/scripts/validate.mjs +26 -0
  109. package/templates/default-starter/src/shell/App.tsx +27 -0
  110. package/templates/default-starter/src/shell/auth/app-identity.ts +3 -0
  111. package/templates/default-starter/src/shell/auth/local-app-client.ts +18 -0
  112. package/templates/default-starter/src/shell/auth/runtime-platform.ts +110 -0
  113. package/templates/default-starter/src/shell/authenticated-shell.tsx +20 -0
  114. package/templates/default-starter/src/shell/workbench-target-adapter.ts +40 -0
  115. package/templates/default-starter/src/styles.css +26 -0
  116. package/templates/default-starter/src-tauri/Cargo.toml +17 -0
  117. package/templates/default-starter/src-tauri/build.rs +5 -0
  118. package/templates/default-starter/src-tauri/capabilities/default.json +7 -0
  119. package/templates/default-starter/src-tauri/tauri.conf.json +26 -0
  120. package/templates/default-starter/tsconfig.json +18 -0
  121. package/templates/default-starter.manifest.json +99 -0
@@ -0,0 +1,676 @@
1
+ import { useContext, useMemo, useState } from 'react';
2
+ import { Button, IconButton, InlineAlert, nimiToast, Popover, PopoverContent, PopoverTrigger, Tooltip } from '@nimiplatform/kit/ui';
3
+ import { Check, ChevronRight, Funnel, RefreshCw, Search, Sparkles, Trash2 } from 'lucide-react';
4
+ import { useAIStudioHost } from './host-context.js';
5
+ import type { StudioCapabilityRegistration } from './module-registration.js';
6
+ import {
7
+ flattenStudioRunHistory,
8
+ formatStudioRunHistoryTimestamp,
9
+ getStudioRunMetricSummary,
10
+ getStudioRunIntentLabel,
11
+ getStudioRunIntentSource,
12
+ getStudioRunPromptSummary,
13
+ getStudioRunResultSummary,
14
+ getStudioRunStatusTone,
15
+ type StudioFlatRunRecord,
16
+ type StudioRunHistory,
17
+ type StudioRunHistoryRecord,
18
+ } from './history.js';
19
+ import {
20
+ StudioHistoryActionsContext,
21
+ StudioHistoryLoadContext,
22
+ StudioHistoryPanelContext,
23
+ type StudioHistoryPanelScope,
24
+ type StudioMediaHistoryRecord,
25
+ } from './contexts.js';
26
+
27
+ type HistoryStatusFilter = 'all' | StudioRunHistoryRecord['status'];
28
+ type HistoryEnvironmentFilter = 'all' | 'local' | 'cloud' | 'remote-control';
29
+ type HistoryActivityFilter = 'all' | 'today' | '7d' | '30d';
30
+ type HistoryGroupBy = 'none' | 'date' | 'capability';
31
+ type HistorySortBy = 'recency' | 'oldest';
32
+ type HistoryFilterMenuId = 'status' | 'capability' | 'environment' | 'activity' | 'group' | 'sort';
33
+
34
+ // Date group formatters follow the active UI locale. Frozen literal with the
35
+ // conformance-whitelisted Intl.DateTimeFormat constructor — module-scope
36
+ // Map/Set caches are forbidden in the canonical local closure.
37
+ const historyDateGroupFormatters = Object.freeze({
38
+ 'zh-CN:year': new Intl.DateTimeFormat('zh-CN', { year: 'numeric', month: 'short', day: 'numeric' }),
39
+ 'zh-CN:plain': new Intl.DateTimeFormat('zh-CN', { month: 'short', day: 'numeric' }),
40
+ 'en-US:year': new Intl.DateTimeFormat('en-US', { year: 'numeric', month: 'short', day: 'numeric' }),
41
+ 'en-US:plain': new Intl.DateTimeFormat('en-US', { month: 'short', day: 'numeric' }),
42
+ } as Record<string, Intl.DateTimeFormat>);
43
+ function historyDateGroupFormatter(locale: string, withYear: boolean): Intl.DateTimeFormat {
44
+ const suffix = withYear ? 'year' : 'plain';
45
+ return historyDateGroupFormatters[`${locale}:${suffix}`] ?? historyDateGroupFormatters[`en-US:${suffix}`];
46
+ }
47
+
48
+ const HISTORY_STATUS_OPTIONS: ReadonlyArray<{ id: HistoryStatusFilter; labelKey: string }> = [
49
+ { id: 'all', labelKey: 'History.filters.status.all' },
50
+ { id: 'ready', labelKey: 'History.filters.status.ready' },
51
+ { id: 'failed', labelKey: 'History.filters.status.failed' },
52
+ { id: 'canceled', labelKey: 'History.filters.status.canceled' },
53
+ { id: 'timed-out', labelKey: 'History.filters.status.timedOut' },
54
+ { id: 'unavailable', labelKey: 'History.filters.status.unavailable' },
55
+ ];
56
+
57
+ const HISTORY_ENVIRONMENT_OPTIONS: ReadonlyArray<{ id: HistoryEnvironmentFilter; labelKey: string }> = [
58
+ { id: 'all', labelKey: 'History.filters.environment.all' },
59
+ { id: 'local', labelKey: 'History.filters.environment.local' },
60
+ { id: 'cloud', labelKey: 'History.filters.environment.cloud' },
61
+ { id: 'remote-control', labelKey: 'History.filters.environment.remoteControl' },
62
+ ];
63
+
64
+ const HISTORY_ACTIVITY_OPTIONS: ReadonlyArray<{ id: HistoryActivityFilter; labelKey: string }> = [
65
+ { id: 'all', labelKey: 'History.filters.activity.all' },
66
+ { id: 'today', labelKey: 'History.filters.activity.today' },
67
+ { id: '7d', labelKey: 'History.filters.activity.last7Days' },
68
+ { id: '30d', labelKey: 'History.filters.activity.last30Days' },
69
+ ];
70
+
71
+ const HISTORY_GROUP_OPTIONS: ReadonlyArray<{ id: HistoryGroupBy; labelKey: string }> = [
72
+ { id: 'none', labelKey: 'History.filters.group.none' },
73
+ { id: 'date', labelKey: 'History.filters.group.date' },
74
+ { id: 'capability', labelKey: 'History.filters.group.capability' },
75
+ ];
76
+
77
+ const HISTORY_SORT_OPTIONS: ReadonlyArray<{ id: HistorySortBy; labelKey: string }> = [
78
+ { id: 'recency', labelKey: 'History.filters.sort.recency' },
79
+ { id: 'oldest', labelKey: 'History.filters.sort.oldest' },
80
+ ];
81
+
82
+ // Local run history recovered from the host-owned history panel. Reads only
83
+ // the app-owned history store; it does not claim Runtime or Realm truth.
84
+ function historyToneForRun(record: StudioRunHistoryRecord): 'success' | 'warning' | 'info' | 'neutral' {
85
+ const tone = getStudioRunStatusTone(record.status);
86
+ if (tone === 'success') return 'success';
87
+ if (tone === 'info') return 'info';
88
+ if (tone === 'danger' || tone === 'warning') return 'warning';
89
+ return 'neutral';
90
+ }
91
+
92
+ function historyTitleForRun(record: StudioRunHistoryRecord): string {
93
+ const prompt = getStudioRunPromptSummary(record).trim();
94
+ return prompt || getStudioRunResultSummary(record);
95
+ }
96
+
97
+ function historyFailureReasonForRun(record: StudioRunHistoryRecord): string {
98
+ const result = record.result;
99
+ if (result && result.ok === false) {
100
+ return result.reason || result.summary || record.message;
101
+ }
102
+ return getStudioRunResultSummary(record);
103
+ }
104
+
105
+ function isSameHistoryDate(left: Date, right: Date): boolean {
106
+ return left.getFullYear() === right.getFullYear() && left.getMonth() === right.getMonth() && left.getDate() === right.getDate();
107
+ }
108
+
109
+ function matchesHistoryStatus(record: StudioRunHistoryRecord, status: HistoryStatusFilter): boolean {
110
+ if (status === 'all') return true;
111
+ return record.status === status;
112
+ }
113
+
114
+ function isFailureHistoryStatus(record: StudioRunHistoryRecord): boolean {
115
+ return record.status === 'failed' || record.status === 'unavailable';
116
+ }
117
+
118
+ function matchesHistorySearch(record: StudioRunHistoryRecord, query: string): boolean {
119
+ if (!query) return true;
120
+ const haystacks = [
121
+ record.prompt,
122
+ record.message,
123
+ record.result?.summary ?? '',
124
+ getStudioRunIntentLabel(record),
125
+ ];
126
+ return haystacks.some((value) => value.toLowerCase().includes(query));
127
+ }
128
+
129
+ function matchesMediaSearch(record: StudioMediaHistoryRecord, query: string): boolean {
130
+ if (!query) return true;
131
+ return [record.title, record.capabilityLabel ?? '', record.artifactLabel ?? '', record.message ?? '']
132
+ .some((value) => value.toLowerCase().includes(query));
133
+ }
134
+
135
+ function matchesHistoryEnvironment(record: StudioRunHistoryRecord, environment: HistoryEnvironmentFilter): boolean {
136
+ if (environment === 'all') return true;
137
+ const source = getStudioRunIntentSource(record);
138
+ if (environment === 'local' || environment === 'cloud') return source === environment;
139
+ const targetSource = String(record.runConfig?.target.source || '').toLowerCase();
140
+ return targetSource.includes('remote');
141
+ }
142
+
143
+ function matchesHistoryActivity(record: Pick<StudioRunHistoryRecord, 'createdAt'>, activity: HistoryActivityFilter, now: Date): boolean {
144
+ if (activity === 'all') return true;
145
+ const createdAt = new Date(record.createdAt);
146
+ if (Number.isNaN(createdAt.valueOf())) return false;
147
+ if (activity === 'today') return isSameHistoryDate(createdAt, now);
148
+ const days = activity === '7d' ? 7 : 30;
149
+ return now.valueOf() - createdAt.valueOf() <= days * 24 * 60 * 60 * 1000;
150
+ }
151
+
152
+ function HistoryFilterOption({
153
+ label,
154
+ selected,
155
+ onSelect,
156
+ }: {
157
+ label: string;
158
+ selected: boolean;
159
+ onSelect: () => void;
160
+ }) {
161
+ return (
162
+ <Button
163
+ type="button"
164
+ tone="ghost"
165
+ size="sm"
166
+ className={selected ? 'studio-history-filter__option studio-history-filter__option--selected' : 'studio-history-filter__option'}
167
+ onClick={onSelect}
168
+ >
169
+ <span>{label}</span>
170
+ {selected ? <Check size={17} strokeWidth={2} aria-hidden="true" /> : null}
171
+ </Button>
172
+ );
173
+ }
174
+
175
+ export function CapabilityRunHistory({
176
+ history,
177
+ activeRunId,
178
+ onSelectRun,
179
+ collapsed,
180
+ currentCapabilityId,
181
+ registrations,
182
+ }: {
183
+ history: StudioRunHistory | null;
184
+ activeRunId: string | null;
185
+ onSelectRun: (record: StudioRunHistoryRecord) => void;
186
+ collapsed: boolean;
187
+ currentCapabilityId: string;
188
+ registrations: readonly StudioCapabilityRegistration[];
189
+ }) {
190
+ const rendererHost = useAIStudioHost();
191
+ const { translate: t } = rendererHost;
192
+ const historyLoad = useContext(StudioHistoryLoadContext);
193
+ const historyActions = useContext(StudioHistoryActionsContext);
194
+ const historyPanel = useContext(StudioHistoryPanelContext);
195
+ const locale = rendererHost.locale.startsWith('zh') ? 'zh-CN' : 'en-US';
196
+ const runtimeHistoryCapabilities = useMemo(() => registrations.filter((item) => (
197
+ item.descriptor.execution === 'runtime-sdk' || item.descriptor.execution === 'standalone-tauri'
198
+ )), [registrations]);
199
+ const resolveCapabilityLabel = (id: string) => (
200
+ registrations.find((item) => item.descriptor.id === id)?.descriptor.label ?? null
201
+ );
202
+ const [filterOpen, setFilterOpen] = useState(false);
203
+ const [activeMenu, setActiveMenu] = useState<HistoryFilterMenuId | null>(null);
204
+ const [searchQuery, setSearchQuery] = useState('');
205
+ const [clearArmed, setClearArmed] = useState(false);
206
+ const scope: StudioHistoryPanelScope = historyPanel?.scope ?? 'capability';
207
+ const hideFailures = historyPanel?.hideFailures ?? false;
208
+ const setScope = (next: StudioHistoryPanelScope) => {
209
+ setClearArmed(false);
210
+ historyPanel?.setScope(next);
211
+ };
212
+ const setHideFailures = (next: boolean) => historyPanel?.setHideFailures(next);
213
+ const [statusFilter, setStatusFilter] = useState<HistoryStatusFilter>('all');
214
+ const [capabilityFilter, setCapabilityFilter] = useState<string | 'all'>('all');
215
+ const [environmentFilter, setEnvironmentFilter] = useState<HistoryEnvironmentFilter>('all');
216
+ const [activityFilter, setActivityFilter] = useState<HistoryActivityFilter>('all');
217
+ const [groupBy, setGroupBy] = useState<HistoryGroupBy>('none');
218
+ const [sortBy, setSortBy] = useState<HistorySortBy>('recency');
219
+ const now = useMemo(() => new Date(rendererHost.clock.now()), [history, rendererHost]);
220
+ const query = searchQuery.trim().toLowerCase();
221
+ const records = useMemo(() => flattenStudioRunHistory(history, resolveCapabilityLabel)
222
+ .filter((record) => (scope === 'capability'
223
+ ? record.capabilityId === currentCapabilityId
224
+ : capabilityFilter === 'all' || record.capabilityId === capabilityFilter))
225
+ .filter((record) => !hideFailures || !isFailureHistoryStatus(record))
226
+ .filter((record) => matchesHistorySearch(record, query))
227
+ .filter((record) => matchesHistoryStatus(record, statusFilter))
228
+ .filter((record) => matchesHistoryEnvironment(record, environmentFilter))
229
+ .filter((record) => matchesHistoryActivity(record, activityFilter, now))
230
+ .sort((left, right) => sortBy === 'recency'
231
+ ? right.createdAt.localeCompare(left.createdAt)
232
+ : left.createdAt.localeCompare(right.createdAt)), [activityFilter, capabilityFilter, currentCapabilityId, environmentFilter, hideFailures, history, now, query, registrations, scope, sortBy, statusFilter]);
233
+ const mediaRecords = useMemo(() => (historyPanel?.imageRecords ?? [])
234
+ .filter((record) => !hideFailures || record.status === 'ready')
235
+ .filter((record) => matchesMediaSearch(record, query))
236
+ .filter((record) => matchesHistoryActivity(record, activityFilter, now)), [activityFilter, hideFailures, historyPanel?.imageRecords, now, query]);
237
+
238
+ function handleSelectMediaRecord(record: StudioMediaHistoryRecord) {
239
+ const linkageId = record.runId || record.id;
240
+ const runRecord = flattenStudioRunHistory(history, resolveCapabilityLabel).find((entry) => entry.id === linkageId);
241
+ if (!runRecord) {
242
+ nimiToast.warning(t('History.linkedRunMissing'));
243
+ return;
244
+ }
245
+ onSelectRun(runRecord);
246
+ }
247
+
248
+ function handleRemoveRecord(record: StudioRunHistoryRecord) {
249
+ void historyActions?.removeRecord(record.id);
250
+ }
251
+
252
+ function handleRemoveMediaRecord(record: StudioMediaHistoryRecord) {
253
+ void historyActions?.removeRecord(record.runId || record.id, true);
254
+ }
255
+
256
+ function handleClearScope(deleteAssets: boolean) {
257
+ setClearArmed(false);
258
+ void historyActions?.clearScope(scope === 'capability' ? currentCapabilityId : null, deleteAssets);
259
+ }
260
+
261
+ function historySourceLabelForRun(record: StudioRunHistoryRecord): string {
262
+ const source = getStudioRunIntentSource(record);
263
+ if (source === 'local') return t('History.source.local');
264
+ if (source === 'cloud') return t('History.source.cloud');
265
+ return t('History.source.unknown');
266
+ }
267
+
268
+ function historySubtitleForRun(record: StudioRunHistoryRecord): string {
269
+ const intent = getStudioRunIntentLabel(record);
270
+ const source = historySourceLabelForRun(record);
271
+ if (record.status === 'canceled') {
272
+ const reason = historyFailureReasonForRun(record);
273
+ return [t(reason === 'operation-aborted' ? 'History.stoppedWaiting' : 'History.canceled'), intent, source, reason].filter(Boolean).join(' / ');
274
+ }
275
+ if (record.status === 'timed-out') {
276
+ return [t('History.timedOut'), intent, source, historyFailureReasonForRun(record)].filter(Boolean).join(' / ');
277
+ }
278
+ if (isFailureHistoryStatus(record)) {
279
+ return [t('History.failed'), intent, source, historyFailureReasonForRun(record)].filter(Boolean).join(' / ');
280
+ }
281
+ return [intent, source].filter(Boolean).join(' / ');
282
+ }
283
+
284
+ function historyLabelForRun(record: StudioFlatRunRecord): string {
285
+ const prompt = historyTitleForRun(record);
286
+ const intent = getStudioRunIntentLabel(record);
287
+ const source = getStudioRunIntentSource(record);
288
+ const metrics = getStudioRunMetricSummary(record);
289
+ return [
290
+ prompt ? t('History.runAriaPrompt', { prompt }) : '',
291
+ source === 'unknown' ? intent : t('History.runAriaIntent', { source: historySourceLabelForRun(record), intent }),
292
+ historyCapabilityLabel(record.capabilityId as string),
293
+ formatStudioRunHistoryTimestamp(record.createdAt, now),
294
+ metrics,
295
+ ].filter(Boolean).join(' / ');
296
+ }
297
+
298
+ function optionLabel<T extends string>(options: ReadonlyArray<{ id: T; labelKey: string }>, id: T): string {
299
+ const option = options.find((entry) => entry.id === id);
300
+ return option ? t(option.labelKey) : id;
301
+ }
302
+
303
+ function historyCapabilityLabel(id: string | 'all'): string {
304
+ if (id === 'all') return t('History.filters.capability.all');
305
+ const capability = runtimeHistoryCapabilities.find((entry) => entry.descriptor.id === id);
306
+ return capability ? t(capability.descriptor.labelKey) : id;
307
+ }
308
+
309
+ function historyDateGroupLabel(value: string): string {
310
+ const date = new Date(value);
311
+ if (Number.isNaN(date.valueOf())) return t('History.unknownDate');
312
+ if (isSameHistoryDate(date, now)) return t('History.today');
313
+ const yesterday = new Date(now);
314
+ yesterday.setDate(now.getDate() - 1);
315
+ if (isSameHistoryDate(date, yesterday)) return t('History.yesterday');
316
+ if (date.getFullYear() === now.getFullYear()) return historyDateGroupFormatter(locale, false).format(date);
317
+ return historyDateGroupFormatter(locale, true).format(date);
318
+ }
319
+
320
+ const groups = useMemo(() => {
321
+ if (groupBy === 'none') return [{ id: 'all', label: '', records }];
322
+ const grouped = new Map<string, { id: string; label: string; records: StudioFlatRunRecord[] }>();
323
+ for (const record of records) {
324
+ const label = groupBy === 'date' ? historyDateGroupLabel(record.createdAt) : historyCapabilityLabel(record.capabilityId as string);
325
+ const id = groupBy === 'date' ? `date:${label}` : `capability:${record.capabilityId}`;
326
+ const existing = grouped.get(id);
327
+ if (existing) {
328
+ existing.records.push(record);
329
+ } else {
330
+ grouped.set(id, { id, label, records: [record] });
331
+ }
332
+ }
333
+ return [...grouped.values()];
334
+ }, [groupBy, now, records, locale, t]);
335
+ const activeMenuLabel = {
336
+ status: optionLabel(HISTORY_STATUS_OPTIONS, statusFilter),
337
+ capability: historyCapabilityLabel(capabilityFilter),
338
+ environment: optionLabel(HISTORY_ENVIRONMENT_OPTIONS, environmentFilter),
339
+ activity: optionLabel(HISTORY_ACTIVITY_OPTIONS, activityFilter),
340
+ group: optionLabel(HISTORY_GROUP_OPTIONS, groupBy),
341
+ sort: optionLabel(HISTORY_SORT_OPTIONS, sortBy),
342
+ } satisfies Record<HistoryFilterMenuId, string>;
343
+ const hasActiveHistoryFilters = statusFilter !== 'all'
344
+ || (scope === 'all' && capabilityFilter !== 'all')
345
+ || environmentFilter !== 'all'
346
+ || activityFilter !== 'all'
347
+ || groupBy !== 'none'
348
+ || sortBy !== 'recency';
349
+ const hasRecords = records.length > 0;
350
+
351
+ function renderSubmenu() {
352
+ if (!activeMenu) {
353
+ return null;
354
+ }
355
+ if (activeMenu === 'status') {
356
+ return HISTORY_STATUS_OPTIONS.map((option) => (
357
+ <HistoryFilterOption key={option.id} label={t(option.labelKey)} selected={statusFilter === option.id} onSelect={() => setStatusFilter(option.id)} />
358
+ ));
359
+ }
360
+ if (activeMenu === 'capability') {
361
+ return [
362
+ <HistoryFilterOption key="all" label={t('History.filters.capability.allCapabilities')} selected={capabilityFilter === 'all'} onSelect={() => setCapabilityFilter('all')} />,
363
+ ...runtimeHistoryCapabilities.map((capability) => (
364
+ <HistoryFilterOption key={capability.descriptor.id} label={t(capability.descriptor.labelKey)} selected={capabilityFilter === capability.descriptor.id} onSelect={() => setCapabilityFilter(capability.descriptor.id)} />
365
+ )),
366
+ ];
367
+ }
368
+ if (activeMenu === 'environment') {
369
+ return HISTORY_ENVIRONMENT_OPTIONS.map((option) => (
370
+ <HistoryFilterOption key={option.id} label={t(option.labelKey)} selected={environmentFilter === option.id} onSelect={() => setEnvironmentFilter(option.id)} />
371
+ ));
372
+ }
373
+ if (activeMenu === 'activity') {
374
+ return HISTORY_ACTIVITY_OPTIONS.map((option) => (
375
+ <HistoryFilterOption key={option.id} label={t(option.labelKey)} selected={activityFilter === option.id} onSelect={() => setActivityFilter(option.id)} />
376
+ ));
377
+ }
378
+ if (activeMenu === 'group') {
379
+ return HISTORY_GROUP_OPTIONS.map((option) => (
380
+ <HistoryFilterOption key={option.id} label={t(option.labelKey)} selected={groupBy === option.id} onSelect={() => setGroupBy(option.id)} />
381
+ ));
382
+ }
383
+ return HISTORY_SORT_OPTIONS.map((option) => (
384
+ <HistoryFilterOption key={option.id} label={t(option.labelKey)} selected={sortBy === option.id} onSelect={() => setSortBy(option.id)} />
385
+ ));
386
+ }
387
+
388
+ function clearHistoryFilters() {
389
+ setStatusFilter('all');
390
+ setCapabilityFilter('all');
391
+ setEnvironmentFilter('all');
392
+ setActivityFilter('all');
393
+ setGroupBy('none');
394
+ setSortBy('recency');
395
+ setActiveMenu(null);
396
+ }
397
+
398
+ return (
399
+ <div className={collapsed ? 'studio-history-shell studio-history-shell--collapsed' : 'studio-history-shell'}>
400
+ <aside className={collapsed ? 'studio-history studio-history--collapsed' : 'studio-history'} aria-label={t('History.panelAriaLabel')}>
401
+ <div className="studio-recent__head">
402
+ <div className="studio-history__title">
403
+ <strong>{t('History.title')}</strong>
404
+ </div>
405
+ <div className="studio-history__actions">
406
+ {scope !== 'media' ? (
407
+ <Popover
408
+ open={filterOpen && !collapsed}
409
+ onOpenChange={(open) => {
410
+ setFilterOpen(open);
411
+ if (!open) setActiveMenu(null);
412
+ }}
413
+ >
414
+ <PopoverTrigger asChild>
415
+ <IconButton
416
+ type="button"
417
+ tone="ghost"
418
+ size="sm"
419
+ className={filterOpen ? 'studio-history__filter-trigger studio-history__filter-trigger--active' : 'studio-history__filter-trigger'}
420
+ aria-label={t('History.filterAriaLabel')}
421
+ aria-expanded={filterOpen}
422
+ icon={<Funnel size={18} strokeWidth={1.9} aria-hidden="true" />}
423
+ />
424
+ </PopoverTrigger>
425
+ <PopoverContent align="end" side="bottom" sideOffset={8} className="studio-history-filter-popover p-2" aria-label={t('History.filtersAriaLabel')}>
426
+ <div className="studio-history-filter">
427
+ <div className="studio-history-filter__menu">
428
+ {([
429
+ ['status', t('History.menus.status')],
430
+ ...(scope === 'all' ? [['capability', t('History.menus.capability')] as const] : []),
431
+ ['environment', t('History.menus.environment')],
432
+ ['activity', t('History.menus.activity')],
433
+ ['group', t('History.menus.group')],
434
+ ['sort', t('History.menus.sort')],
435
+ ] as const).map(([id, label]) => (
436
+ <Button
437
+ key={id}
438
+ type="button"
439
+ tone="ghost"
440
+ size="sm"
441
+ className={activeMenu === id ? 'studio-history-filter__row studio-history-filter__row--active' : 'studio-history-filter__row'}
442
+ aria-expanded={activeMenu === id}
443
+ data-divider={id === 'group' ? '' : undefined}
444
+ onClick={() => setActiveMenu((value) => value === id ? null : id)}
445
+ >
446
+ <span>{label}</span>
447
+ <strong>{activeMenuLabel[id]}</strong>
448
+ <ChevronRight size={17} strokeWidth={1.9} aria-hidden="true" />
449
+ </Button>
450
+ ))}
451
+ {hasActiveHistoryFilters ? (
452
+ <Button
453
+ type="button"
454
+ tone="ghost"
455
+ size="sm"
456
+ className="studio-history-filter__clear"
457
+ onClick={clearHistoryFilters}
458
+ >
459
+ <RefreshCw size={14} strokeWidth={1.9} aria-hidden="true" />
460
+ <span>{t('History.clearFilters')}</span>
461
+ </Button>
462
+ ) : null}
463
+ </div>
464
+ {activeMenu ? (
465
+ <div className="studio-history-filter__submenu">
466
+ {renderSubmenu()}
467
+ </div>
468
+ ) : null}
469
+ </div>
470
+ </PopoverContent>
471
+ </Popover>
472
+ ) : null}
473
+ </div>
474
+ </div>
475
+ <div className="studio-history__search">
476
+ <Search size={14} strokeWidth={1.9} aria-hidden="true" />
477
+ <input
478
+ type="search"
479
+ value={searchQuery}
480
+ onChange={(event) => setSearchQuery(event.currentTarget.value)}
481
+ placeholder={t('History.searchPlaceholder')}
482
+ aria-label={t('History.searchPlaceholder')}
483
+ />
484
+ </div>
485
+ <div className="studio-history__toolbar">
486
+ <div className="studio-history__scope" role="group" aria-label={t('History.scopeAriaLabel')}>
487
+ <Button
488
+ type="button"
489
+ tone="ghost"
490
+ size="sm"
491
+ className={scope === 'capability' ? 'studio-history__scope-option studio-history__scope-option--active' : 'studio-history__scope-option'}
492
+ aria-pressed={scope === 'capability'}
493
+ onClick={() => setScope('capability')}
494
+ >
495
+ {t('History.scope.capability')}
496
+ </Button>
497
+ <Button
498
+ type="button"
499
+ tone="ghost"
500
+ size="sm"
501
+ className={scope === 'all' ? 'studio-history__scope-option studio-history__scope-option--active' : 'studio-history__scope-option'}
502
+ aria-pressed={scope === 'all'}
503
+ onClick={() => setScope('all')}
504
+ >
505
+ {t('History.scope.all')}
506
+ </Button>
507
+ <Button
508
+ type="button"
509
+ tone="ghost"
510
+ size="sm"
511
+ className={scope === 'media' ? 'studio-history__scope-option studio-history__scope-option--active' : 'studio-history__scope-option'}
512
+ aria-pressed={scope === 'media'}
513
+ onClick={() => setScope('media')}
514
+ >
515
+ {t('History.scope.media')}
516
+ </Button>
517
+ </div>
518
+ <Button
519
+ type="button"
520
+ tone="ghost"
521
+ size="sm"
522
+ className={hideFailures ? 'studio-history__scope-option studio-history__scope-option--active' : 'studio-history__scope-option'}
523
+ aria-pressed={hideFailures}
524
+ onClick={() => setHideFailures(!hideFailures)}
525
+ >
526
+ {hideFailures ? t('History.showFailures') : t('History.hideFailures')}
527
+ </Button>
528
+ {scope !== 'media' && historyActions ? (
529
+ clearArmed ? (
530
+ <div role="group" aria-label={t('History.clearChoiceAriaLabel')}>
531
+ <Button type="button" tone="ghost" size="sm" className="studio-history__scope-option" onClick={() => handleClearScope(false)}>
532
+ {t('History.clearRecordsOnly')}
533
+ </Button>
534
+ <Button type="button" tone="ghost" size="sm" className="studio-history__scope-option studio-history__scope-option--danger" onClick={() => handleClearScope(true)}>
535
+ {t('History.clearRecordsAndAssets')}
536
+ </Button>
537
+ </div>
538
+ ) : (
539
+ <Button type="button" tone="ghost" size="sm" className="studio-history__scope-option" onClick={() => setClearArmed(true)}>
540
+ {t('History.clearScope')}
541
+ </Button>
542
+ )
543
+ ) : null}
544
+ </div>
545
+ <div className="studio-history__runs">
546
+ {historyLoad?.error && scope !== 'media' ? (
547
+ <InlineAlert
548
+ tone="danger"
549
+ className="studio-history__load-error"
550
+ action={(
551
+ <Button type="button" tone="secondary" size="sm" onClick={historyLoad.retry}>
552
+ {t('Common.retry')}
553
+ </Button>
554
+ )}
555
+ >
556
+ <div className="studio-history__load-error-copy">
557
+ <strong>{historyLoad.title}</strong>
558
+ <span>{historyLoad.error}</span>
559
+ </div>
560
+ </InlineAlert>
561
+ ) : scope === 'media' ? (
562
+ mediaRecords.length > 0 ? (
563
+ <ul className="studio-recent__rows">
564
+ {mediaRecords.map((record) => (
565
+ <li key={record.id}>
566
+ <Button
567
+ type="button"
568
+ tone="ghost"
569
+ size="sm"
570
+ className="studio-recent__row"
571
+ onClick={() => handleSelectMediaRecord(record)}
572
+ aria-label={record.title}
573
+ >
574
+ <span className={`studio-recent__dot studio-recent__dot--${record.status === 'ready' ? 'success' : 'warning'}`} aria-hidden="true" />
575
+ <span className="studio-recent__icon studio-recent__icon--media" aria-hidden="true">
576
+ <Sparkles size={16} strokeWidth={1.9} />
577
+ </span>
578
+ <span className="studio-recent__copy">
579
+ <span className="studio-recent__summary">
580
+ <span className="studio-recent__title">
581
+ <Tooltip content={record.title} placement="top" className="studio-recent__intent-tooltip">
582
+ <span className="studio-recent__intent-name">{record.title}</span>
583
+ </Tooltip>
584
+ <time dateTime={record.createdAt}>{formatStudioRunHistoryTimestamp(record.createdAt, now)}</time>
585
+ </span>
586
+ </span>
587
+ <span className="studio-recent__detail">
588
+ {[record.capabilityLabel, record.artifactLabel].filter(Boolean).join(' / ')}
589
+ </span>
590
+ </span>
591
+ </Button>
592
+ {historyActions ? (
593
+ <Tooltip content={t('History.deleteRecordAndAsset')} placement="left">
594
+ <IconButton
595
+ type="button"
596
+ tone="ghost"
597
+ size="sm"
598
+ className="studio-recent__row-delete"
599
+ aria-label={t('History.deleteRecordAndAsset')}
600
+ onClick={() => handleRemoveMediaRecord(record)}
601
+ icon={<Trash2 size={13} strokeWidth={1.9} aria-hidden="true" />}
602
+ />
603
+ </Tooltip>
604
+ ) : null}
605
+ </li>
606
+ ))}
607
+ </ul>
608
+ ) : (
609
+ <p className="studio-history__empty">{t('History.emptyMedia')}</p>
610
+ )
611
+ ) : hasRecords ? (
612
+ groups.map((group) => (
613
+ <section key={group.id} className="studio-history__group" aria-label={group.label ? t('History.groupRunsAriaLabel', { group: group.label }) : t('History.recentRuns')}>
614
+ {group.label ? <h2 className="studio-history__group-title">{group.label}</h2> : null}
615
+ <ul className="studio-recent__rows">
616
+ {group.records.map((record) => {
617
+ const capability = runtimeHistoryCapabilities.find((item) => item.descriptor.id === record.capabilityId);
618
+ const Icon = capability?.icon ?? Sparkles;
619
+ return (
620
+ <li key={record.id}>
621
+ <Button
622
+ type="button"
623
+ tone="ghost"
624
+ size="sm"
625
+ className={record.id === activeRunId ? 'studio-recent__row studio-recent__row--active' : 'studio-recent__row'}
626
+ onClick={() => onSelectRun(record)}
627
+ aria-current={record.id === activeRunId ? 'true' : undefined}
628
+ aria-label={historyLabelForRun(record)}
629
+ >
630
+ <span className={`studio-recent__dot studio-recent__dot--${historyToneForRun(record)}`} aria-hidden="true" />
631
+ <span className="studio-recent__icon" aria-hidden="true">
632
+ <Icon size={16} strokeWidth={1.9} />
633
+ </span>
634
+ <span className="studio-recent__copy">
635
+ <span className="studio-recent__summary">
636
+ <span className="studio-recent__title">
637
+ <Tooltip content={historyTitleForRun(record)} placement="top" className="studio-recent__intent-tooltip">
638
+ <span className="studio-recent__intent-name">{historyTitleForRun(record)}</span>
639
+ </Tooltip>
640
+ <time dateTime={record.createdAt}>{formatStudioRunHistoryTimestamp(record.createdAt, now)}</time>
641
+ </span>
642
+ </span>
643
+ <Tooltip content={historySubtitleForRun(record)} placement="top" className="studio-recent__detail-tooltip">
644
+ <span className={isFailureHistoryStatus(record) ? 'studio-recent__detail studio-recent__detail--failed' : 'studio-recent__detail'}>
645
+ {historySubtitleForRun(record)}
646
+ </span>
647
+ </Tooltip>
648
+ </span>
649
+ </Button>
650
+ {historyActions ? (
651
+ <Tooltip content={t('History.deleteRecordOnly')} placement="left">
652
+ <IconButton
653
+ type="button"
654
+ tone="ghost"
655
+ size="sm"
656
+ className="studio-recent__row-delete"
657
+ aria-label={t('History.deleteRecordOnly')}
658
+ onClick={() => handleRemoveRecord(record)}
659
+ icon={<Trash2 size={13} strokeWidth={1.9} aria-hidden="true" />}
660
+ />
661
+ </Tooltip>
662
+ ) : null}
663
+ </li>
664
+ );
665
+ })}
666
+ </ul>
667
+ </section>
668
+ ))
669
+ ) : (
670
+ <p className="studio-history__empty">{history ? t('History.emptyFiltered') : t('History.emptyNone')}</p>
671
+ )}
672
+ </div>
673
+ </aside>
674
+ </div>
675
+ );
676
+ }