@nimiplatform/app-tools 0.1.3 → 0.2.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.
- package/LICENSE +198 -6
- package/README.md +83 -27
- package/bin/nimi-app.mjs +328 -45
- package/lib/app-access-declaration.mjs +49 -0
- package/lib/app-doctor-update.mjs +411 -149
- package/lib/app-pack.mjs +401 -0
- package/lib/app-project-lifecycle.mjs +1141 -0
- package/lib/app-scaffold-capabilities.mjs +769 -0
- package/lib/app-scaffold-default-source.mjs +21 -0
- package/lib/app-scaffold-profiles.mjs +1093 -0
- package/lib/app-scaffold.mjs +1266 -813
- package/lib/index.mjs +194 -42
- package/lib/simulator-conformance-ast.mjs +498 -0
- package/lib/simulator-conformance-css.mjs +116 -0
- package/lib/simulator-conformance-graph.mjs +757 -0
- package/lib/simulator-conformance.d.mts +14 -0
- package/lib/simulator-conformance.mjs +203 -0
- package/lib/simulator-css-profile.d.mts +27 -0
- package/lib/simulator-css-profile.mjs +531 -0
- package/lib/simulator-kit-export-resolution.mjs +70 -0
- package/lib/simulator-manifest.mjs +334 -0
- package/package.json +66 -3
- package/scripts/dev-shell.mjs +465 -0
- package/scripts/sync-app-source.mjs +300 -0
- package/scripts/sync-scaffold-versions.mjs +91 -0
- package/templates/app-source/LICENSE +21 -0
- package/templates/app-source/src/ai-studio-core/ai-config.ts +100 -0
- package/templates/app-source/src/ai-studio-core/ai-studio-core.css +338 -0
- package/templates/app-source/src/ai-studio-core/contexts.tsx +61 -0
- package/templates/app-source/src/ai-studio-core/history-policy.ts +447 -0
- package/templates/app-source/src/ai-studio-core/history.ts +824 -0
- package/templates/app-source/src/ai-studio-core/host-context.tsx +79 -0
- package/templates/app-source/src/ai-studio-core/index.ts +214 -0
- package/templates/app-source/src/ai-studio-core/messages/en.json +310 -0
- package/templates/app-source/src/ai-studio-core/messages/index.ts +47 -0
- package/templates/app-source/src/ai-studio-core/messages/zh.json +310 -0
- package/templates/app-source/src/ai-studio-core/module-registration.ts +109 -0
- package/templates/app-source/src/ai-studio-core/non-success-presentation.ts +65 -0
- package/templates/app-source/src/ai-studio-core/parameter-fields.tsx +247 -0
- package/templates/app-source/src/ai-studio-core/parameters.ts +110 -0
- package/templates/app-source/src/ai-studio-core/prompt-drafts.ts +69 -0
- package/templates/app-source/src/ai-studio-core/run-target.ts +123 -0
- package/templates/app-source/src/ai-studio-core/runtime-dispatcher.ts +35 -0
- package/templates/app-source/src/ai-studio-core/runtime-types.ts +86 -0
- package/templates/app-source/src/ai-studio-core/runtime.ts +306 -0
- package/templates/app-source/src/ai-studio-core/section-ai-testing-admission.ts +38 -0
- package/templates/app-source/src/ai-studio-core/section-ai-testing-ai-config.tsx +30 -0
- package/templates/app-source/src/ai-studio-core/section-ai-testing-artifact-preview.ts +40 -0
- package/templates/app-source/src/ai-studio-core/section-ai-testing-composer.tsx +257 -0
- package/templates/app-source/src/ai-studio-core/section-ai-testing-history.tsx +676 -0
- package/templates/app-source/src/ai-studio-core/section-ai-testing-input.ts +19 -0
- package/templates/app-source/src/ai-studio-core/section-ai-testing-output.tsx +291 -0
- package/templates/app-source/src/ai-studio-core/section-ai-testing-result.tsx +413 -0
- package/templates/app-source/src/ai-studio-core/section-ai-testing-run.ts +174 -0
- package/templates/app-source/src/ai-studio-core/section-ai-testing-studio-result.tsx +449 -0
- package/templates/app-source/src/ai-studio-core/section-ai-testing-surface.tsx +45 -0
- package/templates/app-source/src/ai-studio-core/section-ai-testing.tsx +494 -0
- package/templates/app-source/src/ai-studio-core/studio-directives.ts +33 -0
- package/templates/app-source/src/ai-studio-core/workspace.tsx +381 -0
- package/templates/app-source/src/product-modules/agent-center/index.tsx +27 -0
- package/templates/app-source/src/product-modules/agent-conversation/index.tsx +19 -0
- package/templates/app-source/src/product-modules/agent-realtime/index.tsx +29 -0
- package/templates/app-source/src/product-modules/kit-recipes/gallery.tsx +142 -0
- package/templates/app-source/src/product-modules/kit-recipes/index.tsx +19 -0
- package/templates/app-source/src/product-modules/kit-recipes/kit-component-gallery-data-recipes.tsx +336 -0
- package/templates/app-source/src/product-modules/kit-recipes/kit-component-gallery-demos.tsx +165 -0
- package/templates/app-source/src/product-modules/kit-recipes/kit-component-gallery-recipes.tsx +682 -0
- package/templates/app-source/src/product-modules/kit-recipes/kit-component-gallery-surface.tsx +341 -0
- package/templates/app-source/src/studio-modules/studio-create/descriptors.ts +26 -0
- package/templates/app-source/src/studio-modules/studio-create/index.ts +14 -0
- package/templates/app-source/src/studio-modules/studio-create/messages/en.json +67 -0
- package/templates/app-source/src/studio-modules/studio-create/messages/index.ts +5 -0
- package/templates/app-source/src/studio-modules/studio-create/messages/zh.json +67 -0
- package/templates/app-source/src/studio-modules/studio-create/parameter-panel.tsx +153 -0
- package/templates/app-source/src/studio-modules/studio-create/parameters.ts +70 -0
- package/templates/app-source/src/studio-modules/studio-create/registration.ts +46 -0
- package/templates/app-source/src/studio-modules/studio-create/runtime.ts +339 -0
- package/templates/app-source/src/studio-modules/studio-media/descriptors.ts +22 -0
- package/templates/app-source/src/studio-modules/studio-media/index.ts +12 -0
- package/templates/app-source/src/studio-modules/studio-media/messages/en.json +82 -0
- package/templates/app-source/src/studio-modules/studio-media/messages/index.ts +5 -0
- package/templates/app-source/src/studio-modules/studio-media/messages/zh.json +82 -0
- package/templates/app-source/src/studio-modules/studio-media/parameter-panel.tsx +283 -0
- package/templates/app-source/src/studio-modules/studio-media/parameters.ts +94 -0
- package/templates/app-source/src/studio-modules/studio-media/registration.ts +44 -0
- package/templates/app-source/src/studio-modules/studio-media/runtime.ts +113 -0
- package/templates/app-source/src/studio-modules/studio-voice/descriptors.ts +28 -0
- package/templates/app-source/src/studio-modules/studio-voice/index.ts +16 -0
- package/templates/app-source/src/studio-modules/studio-voice/messages/en.json +99 -0
- package/templates/app-source/src/studio-modules/studio-voice/messages/index.ts +5 -0
- package/templates/app-source/src/studio-modules/studio-voice/messages/zh.json +99 -0
- package/templates/app-source/src/studio-modules/studio-voice/parameter-panel.tsx +284 -0
- package/templates/app-source/src/studio-modules/studio-voice/parameters.ts +116 -0
- package/templates/app-source/src/studio-modules/studio-voice/registration.ts +59 -0
- package/templates/app-source/src/studio-modules/studio-voice/runtime.ts +347 -0
- package/templates/app-source/src/workbench-core/empty-state.tsx +31 -0
- package/templates/app-source/src/workbench-core/index.ts +17 -0
- package/templates/app-source/src/workbench-core/runtime-gate.tsx +131 -0
- package/templates/app-source/src/workbench-core/workbench-core.css +255 -0
- package/templates/app-source/src/workbench-core/workbench-core.tsx +140 -0
- package/templates/app-source.manifest.json +333 -0
- package/templates/default-starter/AGENTS.md +33 -0
- package/templates/default-starter/LICENSE +21 -0
- package/templates/default-starter/README.md +41 -0
- package/templates/default-starter/SECURITY.md +5 -0
- package/templates/default-starter/index.html +12 -0
- package/templates/default-starter/scripts/validate.mjs +26 -0
- package/templates/default-starter/src/shell/App.tsx +27 -0
- package/templates/default-starter/src/shell/auth/app-identity.ts +3 -0
- package/templates/default-starter/src/shell/auth/local-app-client.ts +18 -0
- package/templates/default-starter/src/shell/auth/runtime-platform.ts +110 -0
- package/templates/default-starter/src/shell/authenticated-shell.tsx +20 -0
- package/templates/default-starter/src/shell/workbench-target-adapter.ts +40 -0
- package/templates/default-starter/src/styles.css +26 -0
- package/templates/default-starter/src-tauri/Cargo.toml +17 -0
- package/templates/default-starter/src-tauri/build.rs +5 -0
- package/templates/default-starter/src-tauri/capabilities/default.json +7 -0
- package/templates/default-starter/src-tauri/tauri.conf.json +26 -0
- package/templates/default-starter/tsconfig.json +18 -0
- package/templates/default-starter.manifest.json +99 -0
|
@@ -0,0 +1,494 @@
|
|
|
1
|
+
import { Suspense, useContext, useEffect, useMemo, useRef, useState, type ReactNode } from 'react';
|
|
2
|
+
import { IconButton, LoadingSkeleton, nimiToast, OverlayShell, StatusBadge, Tooltip } from '@nimiplatform/kit/ui';
|
|
3
|
+
import { PanelRight, SquarePen } from 'lucide-react';
|
|
4
|
+
import { createBrowserDataUrlAttachmentAdapter, useChatComposer, type BrowserDataUrlAttachment } from '@nimiplatform/kit/features/chat/headless';
|
|
5
|
+
import { useAIStudioHost } from './host-context.js';
|
|
6
|
+
import type { StudioCapabilityRegistration } from './module-registration.js';
|
|
7
|
+
import type { StudioCapabilityRunResult, StudioRuntimeInspection } from './runtime-types.js';
|
|
8
|
+
import { getStudioRunIntentLabel, restoreStudioCapabilityRunResult, type StudioRunConfigSnapshot, type StudioRunHistory, type StudioRunHistoryRecord } from './history.js';
|
|
9
|
+
import { CapabilityRunHistory, DrawerErrorBoundary, downloadTextFile, resultPlainText, statusForCapability, type CapabilityStatus, type SectionAITestingProps } from './section-ai-testing-surface.js';
|
|
10
|
+
import { TextStudioComposer, TextStudioStartState } from './section-ai-testing-composer.js';
|
|
11
|
+
import {
|
|
12
|
+
canCancelStudioCapabilityRun,
|
|
13
|
+
hasStudioCapabilityRunInput,
|
|
14
|
+
} from './section-ai-testing-input.js';
|
|
15
|
+
import { TextStudioResultState } from './section-ai-testing-result.js';
|
|
16
|
+
import { canConfigureRunTarget, createRunConfigSnapshot, effectiveTextStudioPromptStyle, textStudioDirectiveForTarget, textStudioRunTargetIntentSummary, textStudioRuntimePrompt, useStudioRunTargetSummary, type TextStudioActiveRun } from './section-ai-testing-run.js';
|
|
17
|
+
import { StudioCapabilityParameterContext, StudioHistoryLoadContext, StudioHistoryPanelContext } from './contexts.js';
|
|
18
|
+
|
|
19
|
+
// Admission pill labels are keyed by the typed status so locale changes do not
|
|
20
|
+
// touch the admission state machine in section-ai-testing-admission.ts.
|
|
21
|
+
const ADMISSION_STATUS_LABEL_KEY: Partial<Record<CapabilityStatus['label'], string>> = {
|
|
22
|
+
configured: 'StudioShell.statusConfigured',
|
|
23
|
+
blocked: 'StudioShell.statusBlocked',
|
|
24
|
+
'not admitted': 'StudioShell.statusNotAdmitted',
|
|
25
|
+
'SDK gap': 'StudioShell.statusSdkGap',
|
|
26
|
+
checking: 'StudioShell.statusChecking',
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
function TextStudioShell({
|
|
30
|
+
registration,
|
|
31
|
+
registrations,
|
|
32
|
+
runtime,
|
|
33
|
+
lastResult,
|
|
34
|
+
onResult,
|
|
35
|
+
verboseConsole,
|
|
36
|
+
draftPersistence,
|
|
37
|
+
onOpenConfig,
|
|
38
|
+
history,
|
|
39
|
+
historySelectionRequest,
|
|
40
|
+
onSelectHistoryRun,
|
|
41
|
+
headerActions,
|
|
42
|
+
}: {
|
|
43
|
+
registration: StudioCapabilityRegistration;
|
|
44
|
+
registrations: readonly StudioCapabilityRegistration[];
|
|
45
|
+
runtime: StudioRuntimeInspection | null;
|
|
46
|
+
lastResult: StudioCapabilityRunResult | null;
|
|
47
|
+
onResult: (result: StudioCapabilityRunResult, prompt: string, runConfig?: StudioRunConfigSnapshot) => StudioRunHistoryRecord | null | Promise<StudioRunHistoryRecord | null>;
|
|
48
|
+
verboseConsole: boolean;
|
|
49
|
+
draftPersistence: boolean;
|
|
50
|
+
onOpenConfig?: () => void;
|
|
51
|
+
history: StudioRunHistory | null;
|
|
52
|
+
historySelectionRequest: { requestId: number; record: StudioRunHistoryRecord } | null;
|
|
53
|
+
onSelectHistoryRun: (record: StudioRunHistoryRecord) => void;
|
|
54
|
+
headerActions?: ReactNode;
|
|
55
|
+
}) {
|
|
56
|
+
const rendererHost = useAIStudioHost();
|
|
57
|
+
const t = rendererHost.translate;
|
|
58
|
+
const capability = registration.descriptor;
|
|
59
|
+
const historyLoad = useContext(StudioHistoryLoadContext);
|
|
60
|
+
const parameterStore = useContext(StudioCapabilityParameterContext);
|
|
61
|
+
const ParameterPanel = registration.parameterPanel;
|
|
62
|
+
const profile = registration.profile;
|
|
63
|
+
const preset = registration.preset;
|
|
64
|
+
const [prompt, setPrompt] = useState(() => (
|
|
65
|
+
rendererHost.app.projection.promptDraft({
|
|
66
|
+
surfaceId: 'ai-capabilities',
|
|
67
|
+
capabilityId: capability.id,
|
|
68
|
+
scenarioId: preset.id,
|
|
69
|
+
}, draftPersistence).prompt ?? preset.prompt
|
|
70
|
+
));
|
|
71
|
+
const [context, setContext] = useState('');
|
|
72
|
+
const [running, setRunning] = useState(false);
|
|
73
|
+
const [cancelRequested, setCancelRequested] = useState(false);
|
|
74
|
+
const [streamingText, setStreamingText] = useState<string | null>(null);
|
|
75
|
+
const [activeRun, setActiveRun] = useState<TextStudioActiveRun | null>(null);
|
|
76
|
+
const [sessionRuns, setSessionRuns] = useState<Record<string, TextStudioActiveRun>>({});
|
|
77
|
+
const historyPanel = useContext(StudioHistoryPanelContext);
|
|
78
|
+
const historyCollapsed = historyPanel?.collapsed ?? true;
|
|
79
|
+
const runSeqRef = useRef(0);
|
|
80
|
+
const abortControllerRef = useRef<AbortController | null>(null);
|
|
81
|
+
const expandedHistoryErrorRef = useRef<string | null>(null);
|
|
82
|
+
const attachmentAdapter = useMemo(
|
|
83
|
+
() => createBrowserDataUrlAttachmentAdapter({ idPrefix: 'studio-attachment' }),
|
|
84
|
+
[],
|
|
85
|
+
);
|
|
86
|
+
const composerState = useChatComposer<BrowserDataUrlAttachment>({
|
|
87
|
+
adapter: { submit: async () => {} },
|
|
88
|
+
attachmentAdapter,
|
|
89
|
+
text: prompt,
|
|
90
|
+
onTextChange: updatePrompt,
|
|
91
|
+
disabled: running,
|
|
92
|
+
});
|
|
93
|
+
const hasActiveRun = Boolean(activeRun);
|
|
94
|
+
const currentResult = activeRun
|
|
95
|
+
? activeRun.result ?? (activeRun.record
|
|
96
|
+
? restoreStudioCapabilityRunResult(activeRun.record, (id) => (
|
|
97
|
+
registrations.find((item) => item.descriptor.id === id)?.descriptor.label ?? null
|
|
98
|
+
))
|
|
99
|
+
: null)
|
|
100
|
+
: lastResult?.capabilityId === capability.id ? lastResult : null;
|
|
101
|
+
const headerResult = hasActiveRun ? currentResult : null;
|
|
102
|
+
const runTarget = useStudioRunTargetSummary(registration, runtime);
|
|
103
|
+
const admission = statusForCapability(registration, runTarget, headerResult, t);
|
|
104
|
+
const requiresPrompt = profile.inputKind !== 'none';
|
|
105
|
+
const supportsMedia = profile.supportsAttachments;
|
|
106
|
+
const capabilityParameters = parameterStore?.state[capability.id] ?? registration.parameters.initial();
|
|
107
|
+
const effectiveCapabilityParameters = useMemo(() => registration.parameters.project(
|
|
108
|
+
runTarget.source,
|
|
109
|
+
capabilityParameters,
|
|
110
|
+
), [capabilityParameters, registration.parameters, runTarget.source]);
|
|
111
|
+
const parameterSummary = registration.parameters.summarize(effectiveCapabilityParameters);
|
|
112
|
+
const hasAlternativeInput = registration.parameters.hasAlternativeInput(capabilityParameters);
|
|
113
|
+
|
|
114
|
+
useEffect(() => {
|
|
115
|
+
const error = historyLoad?.error ?? null;
|
|
116
|
+
if (!error) {
|
|
117
|
+
expandedHistoryErrorRef.current = null;
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (expandedHistoryErrorRef.current === error) return;
|
|
121
|
+
expandedHistoryErrorRef.current = error;
|
|
122
|
+
historyPanel?.setCollapsed(false);
|
|
123
|
+
}, [historyLoad?.error, historyPanel]);
|
|
124
|
+
|
|
125
|
+
function updatePrompt(nextPrompt: string) {
|
|
126
|
+
setPrompt(nextPrompt);
|
|
127
|
+
void rendererHost.app.commands.savePromptDraft({
|
|
128
|
+
surfaceId: 'ai-capabilities',
|
|
129
|
+
capabilityId: capability.id,
|
|
130
|
+
scenarioId: preset.id,
|
|
131
|
+
}, nextPrompt, draftPersistence);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
useEffect(() => {
|
|
135
|
+
abortControllerRef.current?.abort('studio-capability-changed');
|
|
136
|
+
abortControllerRef.current = null;
|
|
137
|
+
const draft = rendererHost.app.projection.promptDraft({
|
|
138
|
+
surfaceId: 'ai-capabilities',
|
|
139
|
+
capabilityId: capability.id,
|
|
140
|
+
scenarioId: preset.id,
|
|
141
|
+
}, draftPersistence);
|
|
142
|
+
runSeqRef.current += 1;
|
|
143
|
+
setPrompt(draft.prompt ?? preset.prompt);
|
|
144
|
+
setContext('');
|
|
145
|
+
setActiveRun(null);
|
|
146
|
+
setSessionRuns({});
|
|
147
|
+
setRunning(false);
|
|
148
|
+
setCancelRequested(false);
|
|
149
|
+
setStreamingText(null);
|
|
150
|
+
}, [capability.id, draftPersistence, preset, rendererHost]);
|
|
151
|
+
|
|
152
|
+
async function run(nextPrompt = prompt, nextContext = context) {
|
|
153
|
+
const displayPrompt = nextPrompt.trim();
|
|
154
|
+
if (!hasStudioCapabilityRunInput({ requiresPrompt, prompt: displayPrompt, hasAlternativeInput })) return;
|
|
155
|
+
if (!runTarget.canDispatch) return;
|
|
156
|
+
const runSeq = runSeqRef.current + 1;
|
|
157
|
+
runSeqRef.current = runSeq;
|
|
158
|
+
const abortController = new AbortController();
|
|
159
|
+
abortControllerRef.current = abortController;
|
|
160
|
+
const startedAt = rendererHost.clock.now();
|
|
161
|
+
const pendingRun: TextStudioActiveRun = {
|
|
162
|
+
id: `pending-${startedAt}`,
|
|
163
|
+
prompt: displayPrompt || preset.prompt,
|
|
164
|
+
context: nextContext.trim(),
|
|
165
|
+
createdAt: new Date(startedAt).toISOString(),
|
|
166
|
+
result: null,
|
|
167
|
+
record: null,
|
|
168
|
+
error: null,
|
|
169
|
+
};
|
|
170
|
+
setActiveRun(pendingRun);
|
|
171
|
+
setRunning(true);
|
|
172
|
+
setCancelRequested(false);
|
|
173
|
+
try {
|
|
174
|
+
let result: StudioCapabilityRunResult;
|
|
175
|
+
try {
|
|
176
|
+
const isStreaming = capability.id === 'chat.stream';
|
|
177
|
+
if (isStreaming) setStreamingText('');
|
|
178
|
+
const directive = textStudioDirectiveForTarget(runTarget, profile);
|
|
179
|
+
result = await rendererHost.sdk.runCapability({
|
|
180
|
+
capabilityId: capability.id,
|
|
181
|
+
prompt: capability.id === 'audio.transcribe'
|
|
182
|
+
? displayPrompt
|
|
183
|
+
: textStudioRuntimePrompt(displayPrompt, nextContext, directive),
|
|
184
|
+
scenarioId: preset.id,
|
|
185
|
+
onPartial: isStreaming ? setStreamingText : undefined,
|
|
186
|
+
attachments: supportsMedia ? [...composerState.attachments] : undefined,
|
|
187
|
+
parameters: effectiveCapabilityParameters,
|
|
188
|
+
signal: abortController.signal,
|
|
189
|
+
});
|
|
190
|
+
} catch (error) {
|
|
191
|
+
const message = error instanceof Error ? error.message : String(error || t('NonSuccess.title.runtimeCallFailed'));
|
|
192
|
+
result = {
|
|
193
|
+
ok: false,
|
|
194
|
+
capabilityId: capability.id,
|
|
195
|
+
reason: 'runtime-call-failed',
|
|
196
|
+
message,
|
|
197
|
+
actionHint: t('NonSuccess.hint.runtimeCallFailed'),
|
|
198
|
+
missingSurface: capability.missingSurface,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
const runConfig = createRunConfigSnapshot({
|
|
202
|
+
target: runTarget,
|
|
203
|
+
promptStyle: profile.controls.includes('tone') || profile.controls.includes('length')
|
|
204
|
+
? effectiveTextStudioPromptStyle(runTarget)
|
|
205
|
+
: null,
|
|
206
|
+
context: nextContext,
|
|
207
|
+
attachmentCount: supportsMedia ? composerState.attachments.length : 0,
|
|
208
|
+
requestParameters: parameterSummary,
|
|
209
|
+
});
|
|
210
|
+
const record = await onResult(result, displayPrompt, runConfig);
|
|
211
|
+
// A result reaches the visible completed stage only after onResult has
|
|
212
|
+
// finished its required custody/history work. Persistence failure throws
|
|
213
|
+
// and is projected by the outer failure path instead of showing success.
|
|
214
|
+
if (runSeq !== runSeqRef.current) return;
|
|
215
|
+
const finishedRun: TextStudioActiveRun = {
|
|
216
|
+
...pendingRun,
|
|
217
|
+
id: record?.id ?? pendingRun.id,
|
|
218
|
+
createdAt: record?.createdAt ?? pendingRun.createdAt,
|
|
219
|
+
result,
|
|
220
|
+
record,
|
|
221
|
+
};
|
|
222
|
+
setSessionRuns((current) => ({ ...current, [finishedRun.id]: finishedRun }));
|
|
223
|
+
setActiveRun(finishedRun);
|
|
224
|
+
} catch (error) {
|
|
225
|
+
if (runSeq !== runSeqRef.current) return;
|
|
226
|
+
const message = error instanceof Error ? error.message : String(error || 'Runtime call failed.');
|
|
227
|
+
setActiveRun({ ...pendingRun, error: message });
|
|
228
|
+
} finally {
|
|
229
|
+
if (runSeq === runSeqRef.current) {
|
|
230
|
+
if (abortControllerRef.current === abortController) abortControllerRef.current = null;
|
|
231
|
+
setRunning(false);
|
|
232
|
+
setCancelRequested(false);
|
|
233
|
+
setStreamingText(null);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function handleCopy() {
|
|
239
|
+
if (!currentResult) return;
|
|
240
|
+
const text = resultPlainText(currentResult, t);
|
|
241
|
+
if (!text) return;
|
|
242
|
+
void rendererHost.app.commands.copyText(text)
|
|
243
|
+
.then((result) => {
|
|
244
|
+
if (result.ok) {
|
|
245
|
+
nimiToast.success(t('Common.copied'));
|
|
246
|
+
} else {
|
|
247
|
+
nimiToast.danger(t('Common.copyFailed'));
|
|
248
|
+
}
|
|
249
|
+
})
|
|
250
|
+
.catch(() => {
|
|
251
|
+
nimiToast.danger(t('Common.copyFailed'));
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
async function handleDownload() {
|
|
256
|
+
if (!currentResult) return;
|
|
257
|
+
if (currentResult.ok && currentResult.output.kind === 'artifacts') {
|
|
258
|
+
const artifact = currentResult.output.firstArtifact;
|
|
259
|
+
if (artifact?.relativePath) {
|
|
260
|
+
await rendererHost.sdk.revealLocalAppAsset(artifact.relativePath);
|
|
261
|
+
}
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
const stamp = new Date(rendererHost.clock.now()).toISOString().replace(/[:.]/g, '-');
|
|
265
|
+
const text = resultPlainText(currentResult, t);
|
|
266
|
+
if (!text) return;
|
|
267
|
+
await downloadTextFile(rendererHost.app.commands, `${capability.id}-${stamp}.txt`, text);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Selecting a history record is a read-only preview: it never writes the
|
|
271
|
+
// composer draft. Reusing a record's prompt is an explicit action instead.
|
|
272
|
+
function selectHistoryRun(record: StudioRunHistoryRecord) {
|
|
273
|
+
const sessionRun = sessionRuns[record.id];
|
|
274
|
+
if (sessionRun) {
|
|
275
|
+
setActiveRun(sessionRun);
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
setActiveRun({
|
|
279
|
+
id: record.id,
|
|
280
|
+
prompt: record.prompt,
|
|
281
|
+
context: record.runConfig?.promptControls.context ?? '',
|
|
282
|
+
createdAt: record.createdAt,
|
|
283
|
+
result: null,
|
|
284
|
+
record,
|
|
285
|
+
error: null,
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function useHistoryRunAsDraft(record: StudioRunHistoryRecord) {
|
|
290
|
+
updatePrompt(record.prompt);
|
|
291
|
+
setContext(record.runConfig?.promptControls.context ?? '');
|
|
292
|
+
setActiveRun(null);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
useEffect(() => {
|
|
296
|
+
const record = historySelectionRequest?.record;
|
|
297
|
+
if (!record || record.capabilityId !== capability.id) return;
|
|
298
|
+
selectHistoryRun(record);
|
|
299
|
+
}, [historySelectionRequest?.requestId, capability.id]);
|
|
300
|
+
|
|
301
|
+
const composer = (
|
|
302
|
+
<TextStudioComposer
|
|
303
|
+
registration={registration}
|
|
304
|
+
prompt={prompt}
|
|
305
|
+
context={context}
|
|
306
|
+
intentLabel={textStudioRunTargetIntentSummary(runTarget, t)}
|
|
307
|
+
running={running}
|
|
308
|
+
attachments={composerState.attachments}
|
|
309
|
+
onOpenAttachmentPicker={composerState.openAttachmentPicker}
|
|
310
|
+
onRemoveAttachment={composerState.removeAttachment}
|
|
311
|
+
canDispatch={runTarget.canDispatch}
|
|
312
|
+
canConfigureIntent={canConfigureRunTarget(runTarget)}
|
|
313
|
+
intentConfigurable={capability.execution === 'runtime-sdk'}
|
|
314
|
+
compact={Boolean(activeRun)}
|
|
315
|
+
parameterPanel={parameterStore && capability.execution === 'runtime-sdk' && ParameterPanel ? (
|
|
316
|
+
<ParameterPanel
|
|
317
|
+
capabilityId={capability.id}
|
|
318
|
+
contract={registration.parameters}
|
|
319
|
+
source={runTarget.source}
|
|
320
|
+
parameters={capabilityParameters}
|
|
321
|
+
disabled={running}
|
|
322
|
+
onChange={(next) => parameterStore.setParameters(capability.id, next)}
|
|
323
|
+
/>
|
|
324
|
+
) : undefined}
|
|
325
|
+
parametersActive={Object.keys(parameterSummary).length > 0}
|
|
326
|
+
hasAlternativeInput={hasAlternativeInput}
|
|
327
|
+
onPromptChange={updatePrompt}
|
|
328
|
+
onContextChange={setContext}
|
|
329
|
+
onOpenIntentConfig={onOpenConfig}
|
|
330
|
+
onSubmit={() => void run()}
|
|
331
|
+
/>
|
|
332
|
+
);
|
|
333
|
+
const showAdmissionBadge = true;
|
|
334
|
+
|
|
335
|
+
function handleHistoryCollapseToggle() {
|
|
336
|
+
historyPanel?.setCollapsed(!historyCollapsed);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
function handleNewRun() {
|
|
340
|
+
setActiveRun(null);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function handleCancel() {
|
|
344
|
+
if (cancelRequested) return;
|
|
345
|
+
setCancelRequested(true);
|
|
346
|
+
abortControllerRef.current?.abort('studio-user-canceled');
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
return (
|
|
350
|
+
<div className={hasActiveRun ? 'studio studio--has-run' : 'studio studio--landing'}>
|
|
351
|
+
<div className="studio__workspace studio__workspace--with-history">
|
|
352
|
+
<div className="studio__primary">
|
|
353
|
+
<header className="studio__head">
|
|
354
|
+
<div className="studio__title">
|
|
355
|
+
{showAdmissionBadge ? (
|
|
356
|
+
<StatusBadge tone={admission.tone} shape="dot">
|
|
357
|
+
{t(registration.profile.statusLabelKey ?? ADMISSION_STATUS_LABEL_KEY[admission.label] ?? admission.label)}
|
|
358
|
+
</StatusBadge>
|
|
359
|
+
) : null}
|
|
360
|
+
</div>
|
|
361
|
+
<div className="studio__head-actions">
|
|
362
|
+
{headerActions}
|
|
363
|
+
{hasActiveRun ? (
|
|
364
|
+
<Tooltip content={t('StudioShell.newRun')} placement="bottom">
|
|
365
|
+
<IconButton
|
|
366
|
+
type="button"
|
|
367
|
+
className="studio-history-toggle"
|
|
368
|
+
aria-label={t('StudioShell.newRun')}
|
|
369
|
+
onClick={handleNewRun}
|
|
370
|
+
disabled={running}
|
|
371
|
+
icon={<SquarePen size={17} strokeWidth={1.8} aria-hidden="true" />}
|
|
372
|
+
/>
|
|
373
|
+
</Tooltip>
|
|
374
|
+
) : null}
|
|
375
|
+
<Tooltip
|
|
376
|
+
content={historyCollapsed ? t('StudioShell.showHistory') : t('StudioShell.hideHistory')}
|
|
377
|
+
placement="bottom"
|
|
378
|
+
>
|
|
379
|
+
<IconButton
|
|
380
|
+
type="button"
|
|
381
|
+
className={historyCollapsed ? 'studio-history-toggle' : 'studio-history-toggle studio-history-toggle--expanded'}
|
|
382
|
+
aria-label={historyCollapsed ? t('StudioShell.showHistory') : t('StudioShell.hideHistory')}
|
|
383
|
+
aria-expanded={!historyCollapsed}
|
|
384
|
+
onClick={handleHistoryCollapseToggle}
|
|
385
|
+
icon={<PanelRight size={17} strokeWidth={1.8} aria-hidden="true" />}
|
|
386
|
+
/>
|
|
387
|
+
</Tooltip>
|
|
388
|
+
</div>
|
|
389
|
+
</header>
|
|
390
|
+
<main className="studio__stage">
|
|
391
|
+
{hasActiveRun && activeRun ? (
|
|
392
|
+
<TextStudioResultState
|
|
393
|
+
registration={registration}
|
|
394
|
+
activeRun={activeRun}
|
|
395
|
+
admission={admission}
|
|
396
|
+
intentLabel={activeRun.record ? getStudioRunIntentLabel(activeRun.record) : runTarget.intentLabel}
|
|
397
|
+
running={running}
|
|
398
|
+
cancelRequested={cancelRequested}
|
|
399
|
+
streamingText={streamingText}
|
|
400
|
+
verboseConsole={verboseConsole}
|
|
401
|
+
composer={composer}
|
|
402
|
+
onCopy={handleCopy}
|
|
403
|
+
onDownload={handleDownload}
|
|
404
|
+
onRegenerate={() => void run(activeRun.prompt, activeRun.context)}
|
|
405
|
+
onCancel={canCancelStudioCapabilityRun({
|
|
406
|
+
capabilityId: capability.id,
|
|
407
|
+
resultKind: profile.resultKind,
|
|
408
|
+
})
|
|
409
|
+
? handleCancel
|
|
410
|
+
: undefined}
|
|
411
|
+
onUseAsDraft={useHistoryRunAsDraft}
|
|
412
|
+
/>
|
|
413
|
+
) : (
|
|
414
|
+
<TextStudioStartState
|
|
415
|
+
registration={registration}
|
|
416
|
+
composer={composer}
|
|
417
|
+
/>
|
|
418
|
+
)}
|
|
419
|
+
</main>
|
|
420
|
+
</div>
|
|
421
|
+
<CapabilityRunHistory
|
|
422
|
+
history={history}
|
|
423
|
+
activeRunId={activeRun?.id ?? null}
|
|
424
|
+
onSelectRun={onSelectHistoryRun}
|
|
425
|
+
collapsed={historyCollapsed}
|
|
426
|
+
currentCapabilityId={capability.id}
|
|
427
|
+
registrations={registrations}
|
|
428
|
+
/>
|
|
429
|
+
</div>
|
|
430
|
+
</div>
|
|
431
|
+
);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
export function SectionAITesting({
|
|
435
|
+
registration,
|
|
436
|
+
registrations,
|
|
437
|
+
runtime,
|
|
438
|
+
onResult,
|
|
439
|
+
history,
|
|
440
|
+
lastResult,
|
|
441
|
+
historySelectionRequest,
|
|
442
|
+
onSelectHistoryRun,
|
|
443
|
+
verboseConsole,
|
|
444
|
+
draftPersistence,
|
|
445
|
+
headerActions,
|
|
446
|
+
renderAIConfigPanel,
|
|
447
|
+
rootTestId,
|
|
448
|
+
}: SectionAITestingProps) {
|
|
449
|
+
const { translate: t, appTitle } = useAIStudioHost();
|
|
450
|
+
const capability = registration.descriptor;
|
|
451
|
+
const [configOpen, setConfigOpen] = useState(false);
|
|
452
|
+
|
|
453
|
+
return (
|
|
454
|
+
<div
|
|
455
|
+
className="section-ai-testing"
|
|
456
|
+
data-testid={rootTestId}
|
|
457
|
+
data-config-open={configOpen ? '' : undefined}
|
|
458
|
+
>
|
|
459
|
+
<div className="section-ai-testing__main">
|
|
460
|
+
<TextStudioShell
|
|
461
|
+
registration={registration}
|
|
462
|
+
registrations={registrations}
|
|
463
|
+
runtime={runtime}
|
|
464
|
+
lastResult={lastResult}
|
|
465
|
+
onResult={onResult}
|
|
466
|
+
verboseConsole={verboseConsole}
|
|
467
|
+
draftPersistence={draftPersistence}
|
|
468
|
+
onOpenConfig={renderAIConfigPanel ? () => setConfigOpen(true) : undefined}
|
|
469
|
+
history={history}
|
|
470
|
+
historySelectionRequest={historySelectionRequest}
|
|
471
|
+
onSelectHistoryRun={onSelectHistoryRun}
|
|
472
|
+
headerActions={headerActions}
|
|
473
|
+
/>
|
|
474
|
+
</div>
|
|
475
|
+
|
|
476
|
+
{renderAIConfigPanel ? <OverlayShell
|
|
477
|
+
open={configOpen}
|
|
478
|
+
kind="drawer"
|
|
479
|
+
size="M"
|
|
480
|
+
title={t('StudioModelConfig.drawerTitle')}
|
|
481
|
+
description={t('StudioModelConfig.drawerDescription', { appTitle })}
|
|
482
|
+
panelClassName="flex flex-col"
|
|
483
|
+
contentClassName="min-h-0 flex-1 overflow-y-auto p-0"
|
|
484
|
+
onClose={() => setConfigOpen(false)}
|
|
485
|
+
>
|
|
486
|
+
<DrawerErrorBoundary onClose={() => setConfigOpen(false)} translate={t}>
|
|
487
|
+
<Suspense fallback={<div className="p-5"><LoadingSkeleton lines={4} label={t('Common.loading')} /></div>}>
|
|
488
|
+
{renderAIConfigPanel({ runtime, capabilityId: capability.capabilityContract ?? capability.id })}
|
|
489
|
+
</Suspense>
|
|
490
|
+
</DrawerErrorBoundary>
|
|
491
|
+
</OverlayShell> : null}
|
|
492
|
+
</div>
|
|
493
|
+
);
|
|
494
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export type StudioDirectiveOption = {
|
|
2
|
+
readonly value: string;
|
|
3
|
+
readonly label: string;
|
|
4
|
+
readonly directive: string;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export const TONE_OPTIONS: readonly StudioDirectiveOption[] = Object.freeze([
|
|
8
|
+
{ value: 'clear', label: 'Clear', directive: 'a clear, plain tone' },
|
|
9
|
+
{ value: 'warm', label: 'Warm', directive: 'a warm, friendly tone' },
|
|
10
|
+
{ value: 'formal', label: 'Formal', directive: 'a formal, professional tone' },
|
|
11
|
+
{ value: 'short', label: 'Short', directive: 'a concise, direct tone' },
|
|
12
|
+
]);
|
|
13
|
+
|
|
14
|
+
export const LENGTH_OPTIONS: readonly StudioDirectiveOption[] = Object.freeze([
|
|
15
|
+
{ value: 'short', label: 'Short', directive: 'short' },
|
|
16
|
+
{ value: 'medium', label: 'Medium', directive: 'medium length' },
|
|
17
|
+
{ value: 'detailed', label: 'Detailed', directive: 'detailed and thorough' },
|
|
18
|
+
]);
|
|
19
|
+
|
|
20
|
+
export const DEFAULT_TONE_VALUE = 'clear';
|
|
21
|
+
export const DEFAULT_LENGTH_VALUE = 'medium';
|
|
22
|
+
|
|
23
|
+
export function composeStudioDirective(toneValue: string, lengthValue: string): string {
|
|
24
|
+
const tone = TONE_OPTIONS.find((item) => item.value === toneValue) ?? TONE_OPTIONS[0];
|
|
25
|
+
const length = LENGTH_OPTIONS.find((item) => item.value === lengthValue) ?? LENGTH_OPTIONS[1];
|
|
26
|
+
return `Write the response in ${tone.directive} and keep it ${length.directive}.`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function countStudioWords(text: string): number {
|
|
30
|
+
const trimmed = text.trim();
|
|
31
|
+
if (!trimmed) return 0;
|
|
32
|
+
return trimmed.split(/\s+/).filter(Boolean).length;
|
|
33
|
+
}
|