@nimiplatform/app-tools 0.1.2 → 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.
Files changed (120) 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 +401 -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 +194 -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/package.json +66 -3
  23. package/scripts/dev-shell.mjs +465 -0
  24. package/scripts/sync-app-source.mjs +300 -0
  25. package/scripts/sync-scaffold-versions.mjs +91 -0
  26. package/templates/app-source/LICENSE +21 -0
  27. package/templates/app-source/src/ai-studio-core/ai-config.ts +100 -0
  28. package/templates/app-source/src/ai-studio-core/ai-studio-core.css +338 -0
  29. package/templates/app-source/src/ai-studio-core/contexts.tsx +61 -0
  30. package/templates/app-source/src/ai-studio-core/history-policy.ts +447 -0
  31. package/templates/app-source/src/ai-studio-core/history.ts +824 -0
  32. package/templates/app-source/src/ai-studio-core/host-context.tsx +79 -0
  33. package/templates/app-source/src/ai-studio-core/index.ts +214 -0
  34. package/templates/app-source/src/ai-studio-core/messages/en.json +310 -0
  35. package/templates/app-source/src/ai-studio-core/messages/index.ts +47 -0
  36. package/templates/app-source/src/ai-studio-core/messages/zh.json +310 -0
  37. package/templates/app-source/src/ai-studio-core/module-registration.ts +109 -0
  38. package/templates/app-source/src/ai-studio-core/non-success-presentation.ts +65 -0
  39. package/templates/app-source/src/ai-studio-core/parameter-fields.tsx +247 -0
  40. package/templates/app-source/src/ai-studio-core/parameters.ts +110 -0
  41. package/templates/app-source/src/ai-studio-core/prompt-drafts.ts +69 -0
  42. package/templates/app-source/src/ai-studio-core/run-target.ts +123 -0
  43. package/templates/app-source/src/ai-studio-core/runtime-dispatcher.ts +35 -0
  44. package/templates/app-source/src/ai-studio-core/runtime-types.ts +86 -0
  45. package/templates/app-source/src/ai-studio-core/runtime.ts +306 -0
  46. package/templates/app-source/src/ai-studio-core/section-ai-testing-admission.ts +38 -0
  47. package/templates/app-source/src/ai-studio-core/section-ai-testing-ai-config.tsx +30 -0
  48. package/templates/app-source/src/ai-studio-core/section-ai-testing-artifact-preview.ts +40 -0
  49. package/templates/app-source/src/ai-studio-core/section-ai-testing-composer.tsx +257 -0
  50. package/templates/app-source/src/ai-studio-core/section-ai-testing-history.tsx +676 -0
  51. package/templates/app-source/src/ai-studio-core/section-ai-testing-input.ts +19 -0
  52. package/templates/app-source/src/ai-studio-core/section-ai-testing-output.tsx +291 -0
  53. package/templates/app-source/src/ai-studio-core/section-ai-testing-result.tsx +413 -0
  54. package/templates/app-source/src/ai-studio-core/section-ai-testing-run.ts +174 -0
  55. package/templates/app-source/src/ai-studio-core/section-ai-testing-studio-result.tsx +449 -0
  56. package/templates/app-source/src/ai-studio-core/section-ai-testing-surface.tsx +45 -0
  57. package/templates/app-source/src/ai-studio-core/section-ai-testing.tsx +494 -0
  58. package/templates/app-source/src/ai-studio-core/studio-directives.ts +33 -0
  59. package/templates/app-source/src/ai-studio-core/workspace.tsx +381 -0
  60. package/templates/app-source/src/product-modules/agent-center/index.tsx +27 -0
  61. package/templates/app-source/src/product-modules/agent-conversation/index.tsx +19 -0
  62. package/templates/app-source/src/product-modules/agent-realtime/index.tsx +29 -0
  63. package/templates/app-source/src/product-modules/kit-recipes/gallery.tsx +142 -0
  64. package/templates/app-source/src/product-modules/kit-recipes/index.tsx +19 -0
  65. package/templates/app-source/src/product-modules/kit-recipes/kit-component-gallery-data-recipes.tsx +336 -0
  66. package/templates/app-source/src/product-modules/kit-recipes/kit-component-gallery-demos.tsx +165 -0
  67. package/templates/app-source/src/product-modules/kit-recipes/kit-component-gallery-recipes.tsx +682 -0
  68. package/templates/app-source/src/product-modules/kit-recipes/kit-component-gallery-surface.tsx +341 -0
  69. package/templates/app-source/src/studio-modules/studio-create/descriptors.ts +26 -0
  70. package/templates/app-source/src/studio-modules/studio-create/index.ts +14 -0
  71. package/templates/app-source/src/studio-modules/studio-create/messages/en.json +67 -0
  72. package/templates/app-source/src/studio-modules/studio-create/messages/index.ts +5 -0
  73. package/templates/app-source/src/studio-modules/studio-create/messages/zh.json +67 -0
  74. package/templates/app-source/src/studio-modules/studio-create/parameter-panel.tsx +153 -0
  75. package/templates/app-source/src/studio-modules/studio-create/parameters.ts +70 -0
  76. package/templates/app-source/src/studio-modules/studio-create/registration.ts +46 -0
  77. package/templates/app-source/src/studio-modules/studio-create/runtime.ts +339 -0
  78. package/templates/app-source/src/studio-modules/studio-media/descriptors.ts +22 -0
  79. package/templates/app-source/src/studio-modules/studio-media/index.ts +12 -0
  80. package/templates/app-source/src/studio-modules/studio-media/messages/en.json +82 -0
  81. package/templates/app-source/src/studio-modules/studio-media/messages/index.ts +5 -0
  82. package/templates/app-source/src/studio-modules/studio-media/messages/zh.json +82 -0
  83. package/templates/app-source/src/studio-modules/studio-media/parameter-panel.tsx +283 -0
  84. package/templates/app-source/src/studio-modules/studio-media/parameters.ts +94 -0
  85. package/templates/app-source/src/studio-modules/studio-media/registration.ts +44 -0
  86. package/templates/app-source/src/studio-modules/studio-media/runtime.ts +113 -0
  87. package/templates/app-source/src/studio-modules/studio-voice/descriptors.ts +28 -0
  88. package/templates/app-source/src/studio-modules/studio-voice/index.ts +16 -0
  89. package/templates/app-source/src/studio-modules/studio-voice/messages/en.json +99 -0
  90. package/templates/app-source/src/studio-modules/studio-voice/messages/index.ts +5 -0
  91. package/templates/app-source/src/studio-modules/studio-voice/messages/zh.json +99 -0
  92. package/templates/app-source/src/studio-modules/studio-voice/parameter-panel.tsx +284 -0
  93. package/templates/app-source/src/studio-modules/studio-voice/parameters.ts +116 -0
  94. package/templates/app-source/src/studio-modules/studio-voice/registration.ts +59 -0
  95. package/templates/app-source/src/studio-modules/studio-voice/runtime.ts +347 -0
  96. package/templates/app-source/src/workbench-core/empty-state.tsx +31 -0
  97. package/templates/app-source/src/workbench-core/index.ts +17 -0
  98. package/templates/app-source/src/workbench-core/runtime-gate.tsx +131 -0
  99. package/templates/app-source/src/workbench-core/workbench-core.css +255 -0
  100. package/templates/app-source/src/workbench-core/workbench-core.tsx +140 -0
  101. package/templates/app-source.manifest.json +333 -0
  102. package/templates/default-starter/AGENTS.md +33 -0
  103. package/templates/default-starter/LICENSE +21 -0
  104. package/templates/default-starter/README.md +41 -0
  105. package/templates/default-starter/SECURITY.md +5 -0
  106. package/templates/default-starter/index.html +12 -0
  107. package/templates/default-starter/scripts/validate.mjs +26 -0
  108. package/templates/default-starter/src/shell/App.tsx +27 -0
  109. package/templates/default-starter/src/shell/auth/app-identity.ts +3 -0
  110. package/templates/default-starter/src/shell/auth/local-app-client.ts +18 -0
  111. package/templates/default-starter/src/shell/auth/runtime-platform.ts +110 -0
  112. package/templates/default-starter/src/shell/authenticated-shell.tsx +20 -0
  113. package/templates/default-starter/src/shell/workbench-target-adapter.ts +40 -0
  114. package/templates/default-starter/src/styles.css +26 -0
  115. package/templates/default-starter/src-tauri/Cargo.toml +17 -0
  116. package/templates/default-starter/src-tauri/build.rs +5 -0
  117. package/templates/default-starter/src-tauri/capabilities/default.json +7 -0
  118. package/templates/default-starter/src-tauri/tauri.conf.json +26 -0
  119. package/templates/default-starter/tsconfig.json +18 -0
  120. package/templates/default-starter.manifest.json +99 -0
@@ -0,0 +1,247 @@
1
+ import type { ReactNode } from 'react';
2
+ import { Button, TextField, Toggle } from '@nimiplatform/kit/ui';
3
+ import { ChevronDown } from 'lucide-react';
4
+
5
+ import type {
6
+ StudioParameterContract,
7
+ StudioParameterValue,
8
+ StudioRunTargetSource,
9
+ } from './parameters.js';
10
+
11
+ export type StudioParameterTranslate = (
12
+ key: string,
13
+ values?: Readonly<Record<string, unknown>>,
14
+ ) => string;
15
+
16
+ export type StudioParameterPanelProps = {
17
+ readonly capabilityId: string;
18
+ readonly contract: StudioParameterContract;
19
+ readonly source: StudioRunTargetSource;
20
+ readonly parameters: StudioParameterValue;
21
+ readonly disabled: boolean;
22
+ readonly onChange: (parameters: StudioParameterValue) => void;
23
+ };
24
+
25
+ export type StudioParameterFieldDefinition = {
26
+ readonly field: string;
27
+ readonly label: string;
28
+ readonly render: (routeDisabled: boolean) => ReactNode;
29
+ };
30
+
31
+ export function StudioParameterPanelFrame({
32
+ translate,
33
+ disabled,
34
+ onReset,
35
+ children,
36
+ }: {
37
+ readonly translate: StudioParameterTranslate;
38
+ readonly disabled: boolean;
39
+ readonly onReset: () => void;
40
+ readonly children: ReactNode;
41
+ }) {
42
+ return (
43
+ <section className="studio-parameters" aria-label={translate('Studio.parameters.title')}>
44
+ <div className="studio-parameters__head">
45
+ <span>{translate('Studio.parameters.presenceHint')}</span>
46
+ <Button type="button" tone="ghost" size="sm" disabled={disabled} onClick={onReset}>
47
+ {translate('Studio.parameters.reset')}
48
+ </Button>
49
+ </div>
50
+ {children}
51
+ </section>
52
+ );
53
+ }
54
+
55
+ export function StudioParameterField({
56
+ label,
57
+ children,
58
+ }: {
59
+ readonly label: string;
60
+ readonly children: ReactNode;
61
+ }) {
62
+ return (
63
+ <label className="studio-parameters__field">
64
+ <span className="studio-parameters__label">{label}</span>
65
+ {children}
66
+ </label>
67
+ );
68
+ }
69
+
70
+ export function StudioRouteAwareParameterFields({
71
+ contract,
72
+ source,
73
+ fields,
74
+ translate,
75
+ contentClassName = 'studio-parameters__grid',
76
+ }: {
77
+ readonly contract: StudioParameterContract;
78
+ readonly source: StudioRunTargetSource;
79
+ readonly fields: readonly StudioParameterFieldDefinition[];
80
+ readonly translate: StudioParameterTranslate;
81
+ readonly contentClassName?: string;
82
+ }) {
83
+ const presentation = new Map(
84
+ contract.presentation(source).map((item) => [item.field, item]),
85
+ );
86
+ const available = fields.filter((field) => presentation.get(field.field)?.state !== 'disabled');
87
+ const routeUnavailable = fields.filter((field) => (
88
+ presentation.get(field.field)?.state === 'disabled'
89
+ && presentation.get(field.field)?.unavailableBecause === 'route'
90
+ ));
91
+ const surfaceUnavailable = fields.filter((field) => (
92
+ presentation.get(field.field)?.state === 'disabled'
93
+ && presentation.get(field.field)?.unavailableBecause === 'local-app-surface'
94
+ ));
95
+ const renderAvailable = (field: StudioParameterFieldDefinition) => {
96
+ const item = presentation.get(field.field);
97
+ if (item?.state === 'fixed') {
98
+ return (
99
+ <StudioParameterField key={field.field} label={field.label}>
100
+ <span className="studio-parameters__fixed" role="status">
101
+ {translate('Studio.parameters.fixedValue', { value: item.fixedValue })}
102
+ </span>
103
+ </StudioParameterField>
104
+ );
105
+ }
106
+ return <div className="studio-parameters__field-slot" key={field.field}>{field.render(false)}</div>;
107
+ };
108
+ return (
109
+ <>
110
+ <div className={contentClassName}>{available.map(renderAvailable)}</div>
111
+ {routeUnavailable.length > 0 && (source === 'local' || source === 'cloud') ? (
112
+ <details className="studio-parameters__route-group">
113
+ <summary>
114
+ <span>
115
+ <strong>{translate(source === 'local' ? 'Studio.parameters.cloudOnlyGroup' : 'Studio.parameters.localOnlyGroup')}</strong>
116
+ <small>{translate(source === 'local' ? 'Studio.parameters.switchToCloudHint' : 'Studio.parameters.switchToLocalHint')}</small>
117
+ </span>
118
+ <ChevronDown size={15} aria-hidden="true" />
119
+ </summary>
120
+ <div className={contentClassName} aria-disabled="true">
121
+ {routeUnavailable.map((field) => (
122
+ <div className="studio-parameters__field-slot" key={field.field}>{field.render(true)}</div>
123
+ ))}
124
+ </div>
125
+ </details>
126
+ ) : null}
127
+ {surfaceUnavailable.length > 0 ? (
128
+ <details className="studio-parameters__route-group">
129
+ <summary>
130
+ <span>
131
+ <strong>{translate('Studio.parameters.localAppUnavailableGroup')}</strong>
132
+ <small>{translate('Studio.parameters.localAppUnavailableHint')}</small>
133
+ </span>
134
+ <ChevronDown size={15} aria-hidden="true" />
135
+ </summary>
136
+ <div className={contentClassName} aria-disabled="true">
137
+ {surfaceUnavailable.map((field) => (
138
+ <div className="studio-parameters__field-slot" key={field.field}>{field.render(true)}</div>
139
+ ))}
140
+ </div>
141
+ </details>
142
+ ) : null}
143
+ </>
144
+ );
145
+ }
146
+
147
+ export function optionalStudioText<T extends object>(current: T, key: keyof T, value: string): T {
148
+ if (value.length > 0) return { ...current, [key]: value };
149
+ const next = { ...current };
150
+ delete next[key];
151
+ return next;
152
+ }
153
+
154
+ export function optionalStudioNumber<T extends object>(current: T, key: keyof T, value: string): T {
155
+ if (value !== '' && Number.isFinite(Number(value))) return { ...current, [key]: Number(value) };
156
+ const next = { ...current };
157
+ delete next[key];
158
+ return next;
159
+ }
160
+
161
+ export function StudioNumberParameter<T extends object>({
162
+ current,
163
+ field,
164
+ label,
165
+ onChange,
166
+ disabled,
167
+ min,
168
+ max,
169
+ step = 'any',
170
+ }: {
171
+ readonly current: T;
172
+ readonly field: keyof T;
173
+ readonly label: string;
174
+ readonly onChange: (next: T) => void;
175
+ readonly disabled: boolean;
176
+ readonly min?: number;
177
+ readonly max?: number;
178
+ readonly step?: number | 'any';
179
+ }) {
180
+ const value = current[field];
181
+ return (
182
+ <StudioParameterField label={label}>
183
+ <TextField
184
+ type="number"
185
+ value={typeof value === 'number' ? value : ''}
186
+ min={min}
187
+ max={max}
188
+ step={step}
189
+ disabled={disabled}
190
+ onChange={(event) => onChange(optionalStudioNumber(current, field, event.currentTarget.value))}
191
+ />
192
+ </StudioParameterField>
193
+ );
194
+ }
195
+
196
+ export function StudioTextParameter<T extends object>({
197
+ current,
198
+ field,
199
+ label,
200
+ onChange,
201
+ disabled,
202
+ placeholder,
203
+ }: {
204
+ readonly current: T;
205
+ readonly field: keyof T;
206
+ readonly label: string;
207
+ readonly onChange: (next: T) => void;
208
+ readonly disabled: boolean;
209
+ readonly placeholder?: string;
210
+ }) {
211
+ const value = current[field];
212
+ return (
213
+ <StudioParameterField label={label}>
214
+ <TextField
215
+ value={typeof value === 'string' ? value : ''}
216
+ placeholder={placeholder}
217
+ disabled={disabled}
218
+ onChange={(event) => onChange(optionalStudioText(current, field, event.currentTarget.value))}
219
+ />
220
+ </StudioParameterField>
221
+ );
222
+ }
223
+
224
+ export function StudioBooleanParameter<T extends object>({
225
+ current,
226
+ field,
227
+ label,
228
+ onChange,
229
+ disabled,
230
+ }: {
231
+ readonly current: T;
232
+ readonly field: keyof T;
233
+ readonly label: string;
234
+ readonly onChange: (next: T) => void;
235
+ readonly disabled: boolean;
236
+ }) {
237
+ return (
238
+ <StudioParameterField label={label}>
239
+ <Toggle
240
+ checked={current[field] === true}
241
+ disabled={disabled}
242
+ ariaLabel={label}
243
+ onChange={(checked) => onChange({ ...current, [field]: checked })}
244
+ />
245
+ </StudioParameterField>
246
+ );
247
+ }
@@ -0,0 +1,110 @@
1
+ export type StudioRunTargetSource = 'local' | 'cloud' | 'unknown';
2
+
3
+ export type StudioParameterRouteState =
4
+ | { readonly kind: 'supported' }
5
+ | { readonly kind: 'unsupported' }
6
+ | { readonly kind: 'fixed'; readonly value: string | number };
7
+
8
+ export type StudioParameterPresentation = {
9
+ readonly field: string;
10
+ readonly state: 'enabled' | 'disabled' | 'fixed';
11
+ readonly fixedValue?: string | number;
12
+ readonly unavailableBecause?: 'route' | 'local-app-surface';
13
+ };
14
+
15
+ export type StudioParameterValue = Readonly<Record<string, unknown>>;
16
+ export type StudioParameterState = Readonly<Record<string, StudioParameterValue>>;
17
+ export type StudioParameterRouteMatrix = Readonly<Record<
18
+ string,
19
+ Readonly<Record<'local' | 'cloud', StudioParameterRouteState>>
20
+ >>;
21
+
22
+ export type StudioParameterContract = {
23
+ readonly initial: () => StudioParameterValue;
24
+ readonly summarize: (parameters: StudioParameterValue) => Readonly<Record<string, unknown>>;
25
+ readonly hasAlternativeInput: (parameters: StudioParameterValue) => boolean;
26
+ readonly presentation: (source: StudioRunTargetSource) => readonly StudioParameterPresentation[];
27
+ readonly project: (source: StudioRunTargetSource, parameters: StudioParameterValue) => StudioParameterValue;
28
+ };
29
+
30
+ export type TypedStudioParameterContract<TParameters extends object> = {
31
+ readonly initial: () => TParameters;
32
+ readonly summarize?: (parameters: TParameters) => Readonly<Record<string, unknown>>;
33
+ readonly hasAlternativeInput?: (parameters: TParameters) => boolean;
34
+ readonly routeMatrix: StudioParameterRouteMatrix;
35
+ };
36
+
37
+ export const SUPPORTED_STUDIO_PARAMETER = Object.freeze({ kind: 'supported' } as const);
38
+ export const UNSUPPORTED_STUDIO_PARAMETER = Object.freeze({ kind: 'unsupported' } as const);
39
+ export const LOCAL_AND_CLOUD_STUDIO_PARAMETER = Object.freeze({
40
+ local: SUPPORTED_STUDIO_PARAMETER,
41
+ cloud: SUPPORTED_STUDIO_PARAMETER,
42
+ });
43
+ export const LOCAL_ONLY_STUDIO_PARAMETER = Object.freeze({
44
+ local: SUPPORTED_STUDIO_PARAMETER,
45
+ cloud: UNSUPPORTED_STUDIO_PARAMETER,
46
+ });
47
+ export const CLOUD_ONLY_STUDIO_PARAMETER = Object.freeze({
48
+ local: UNSUPPORTED_STUDIO_PARAMETER,
49
+ cloud: SUPPORTED_STUDIO_PARAMETER,
50
+ });
51
+
52
+ export function defineStudioParameters<TParameters extends object>(
53
+ typed: TypedStudioParameterContract<TParameters>,
54
+ ): StudioParameterContract {
55
+ const initial = () => typed.initial() as StudioParameterValue;
56
+ const summarize = (parameters: StudioParameterValue) => (
57
+ typed.summarize?.(parameters as TParameters) ?? { ...parameters }
58
+ );
59
+ const hasAlternativeInput = (parameters: StudioParameterValue) => (
60
+ typed.hasAlternativeInput?.(parameters as TParameters) ?? false
61
+ );
62
+ const presentation = (source: StudioRunTargetSource) => (
63
+ studioParameterPresentation(typed.routeMatrix, source)
64
+ );
65
+ const project = (source: StudioRunTargetSource, parameters: StudioParameterValue) => (
66
+ projectStudioParameters(typed.routeMatrix, source, parameters)
67
+ );
68
+ return Object.freeze({ initial, summarize, hasAlternativeInput, presentation, project });
69
+ }
70
+
71
+ export const EMPTY_STUDIO_PARAMETERS = defineStudioParameters({
72
+ initial: () => ({}),
73
+ routeMatrix: {},
74
+ });
75
+
76
+ function studioParameterPresentation(
77
+ matrix: StudioParameterRouteMatrix,
78
+ source: StudioRunTargetSource,
79
+ ): readonly StudioParameterPresentation[] {
80
+ return Object.entries(matrix).map(([field, routes]) => {
81
+ if (source !== 'local' && source !== 'cloud') return { field, state: 'enabled' };
82
+ const routeState = routes[source];
83
+ if (routeState.kind === 'fixed') return { field, state: 'fixed', fixedValue: routeState.value };
84
+ if (routeState.kind === 'supported') return { field, state: 'enabled' };
85
+ const otherRoute = source === 'local' ? routes.cloud : routes.local;
86
+ return {
87
+ field,
88
+ state: 'disabled',
89
+ unavailableBecause: otherRoute.kind === 'unsupported' ? 'local-app-surface' : 'route',
90
+ };
91
+ });
92
+ }
93
+
94
+ function projectStudioParameters(
95
+ matrix: StudioParameterRouteMatrix,
96
+ source: StudioRunTargetSource,
97
+ parameters: StudioParameterValue,
98
+ ): StudioParameterValue {
99
+ const presentation = new Map(
100
+ studioParameterPresentation(matrix, source).map((item) => [item.field, item]),
101
+ );
102
+ const projected: Record<string, unknown> = {};
103
+ for (const [field, value] of Object.entries(parameters)) {
104
+ if (presentation.get(field)?.state !== 'disabled') projected[field] = value;
105
+ }
106
+ for (const item of presentation.values()) {
107
+ if (item.state === 'fixed') projected[item.field] = item.fixedValue;
108
+ }
109
+ return projected;
110
+ }
@@ -0,0 +1,69 @@
1
+ export const STUDIO_PROMPT_DRAFTS_SCHEMA_VERSION = 1;
2
+
3
+ export type StudioPromptDraftKey = {
4
+ readonly surfaceId: string;
5
+ readonly capabilityId: string;
6
+ readonly scenarioId: string;
7
+ };
8
+
9
+ export type StudioPromptDraftStore = {
10
+ readonly schemaVersion: typeof STUDIO_PROMPT_DRAFTS_SCHEMA_VERSION;
11
+ readonly drafts: Readonly<Record<string, string>>;
12
+ };
13
+
14
+ export function createEmptyStudioPromptDraftStore(): StudioPromptDraftStore {
15
+ return { schemaVersion: STUDIO_PROMPT_DRAFTS_SCHEMA_VERSION, drafts: {} };
16
+ }
17
+
18
+ export function studioPromptDraftId(key: StudioPromptDraftKey): string {
19
+ return `${key.surfaceId}:${key.capabilityId}:${key.scenarioId}`;
20
+ }
21
+
22
+ export function parseStudioPromptDraftStore(value: unknown): StudioPromptDraftStore {
23
+ if (!value || typeof value !== 'object' || Array.isArray(value)) {
24
+ throw new Error('Stored prompt draft schema is invalid.');
25
+ }
26
+ const parsed = value as Partial<StudioPromptDraftStore>;
27
+ if (
28
+ parsed.schemaVersion !== STUDIO_PROMPT_DRAFTS_SCHEMA_VERSION
29
+ || !parsed.drafts
30
+ || typeof parsed.drafts !== 'object'
31
+ || Array.isArray(parsed.drafts)
32
+ ) {
33
+ throw new Error('Stored prompt draft schema is invalid.');
34
+ }
35
+ for (const [draftId, prompt] of Object.entries(parsed.drafts)) {
36
+ if (!draftId || typeof prompt !== 'string') {
37
+ throw new Error('Stored prompt draft entry is invalid.');
38
+ }
39
+ }
40
+ return {
41
+ schemaVersion: STUDIO_PROMPT_DRAFTS_SCHEMA_VERSION,
42
+ drafts: { ...parsed.drafts },
43
+ };
44
+ }
45
+
46
+ export function readStudioPromptDraft(
47
+ store: StudioPromptDraftStore,
48
+ key: StudioPromptDraftKey,
49
+ enabled: boolean,
50
+ ): string | null {
51
+ if (!enabled) return null;
52
+ return store.drafts[studioPromptDraftId(key)] ?? null;
53
+ }
54
+
55
+ export function updateStudioPromptDraftStore(
56
+ store: StudioPromptDraftStore,
57
+ key: StudioPromptDraftKey,
58
+ prompt: string,
59
+ enabled: boolean,
60
+ ): StudioPromptDraftStore {
61
+ if (!enabled) return store;
62
+ return {
63
+ schemaVersion: STUDIO_PROMPT_DRAFTS_SCHEMA_VERSION,
64
+ drafts: {
65
+ ...store.drafts,
66
+ [studioPromptDraftId(key)]: prompt,
67
+ },
68
+ };
69
+ }
@@ -0,0 +1,123 @@
1
+ import type { NimiPortableAppAIConfig } from '@nimiplatform/sdk/ai';
2
+
3
+ import {
4
+ findStudioCapabilityIntent,
5
+ studioCloudIntentHasExactTarget,
6
+ } from './ai-config.js';
7
+ import type { StudioRunTargetSummary } from './history.js';
8
+ import type { StudioCapabilityDescriptor } from './module-registration.js';
9
+ import type { StudioRuntimeInspection } from './runtime-types.js';
10
+
11
+ export function createStudioRunTargetSummary(input: {
12
+ readonly capability: StudioCapabilityDescriptor;
13
+ readonly runtime: StudioRuntimeInspection | null;
14
+ readonly config: NimiPortableAppAIConfig | null;
15
+ readonly configState?: 'loading' | 'loaded' | 'failed';
16
+ readonly configError?: string | null;
17
+ }): StudioRunTargetSummary {
18
+ const { capability, runtime, config } = input;
19
+ const capabilityContract = capability.capabilityContract ?? null;
20
+ const base = {
21
+ capabilityId: capability.id,
22
+ capabilityContract,
23
+ section: capability.section,
24
+ params: {},
25
+ paramsSummary: [],
26
+ profileOrigin: null,
27
+ } as const;
28
+
29
+ if (capability.execution === 'standalone-tauri') {
30
+ throw new Error('Standalone capability run targets must be projected by an app-specific host adapter.');
31
+ }
32
+ if (capability.execution === 'typed-unavailable' || !capabilityContract) {
33
+ return {
34
+ ...base,
35
+ status: 'sdk-gap',
36
+ source: 'unknown',
37
+ intentLabel: 'Capability unavailable',
38
+ detail: capability.missingSurface || 'No admitted typed SDK method is available for this capability.',
39
+ canDispatch: false,
40
+ };
41
+ }
42
+ if (!runtime) {
43
+ return {
44
+ ...base,
45
+ status: 'checking',
46
+ source: 'unknown',
47
+ intentLabel: 'Checking configuration',
48
+ detail: 'Reading the current App AIConfig and Runtime connection.',
49
+ canDispatch: false,
50
+ };
51
+ }
52
+ if (runtime.status !== 'connected') {
53
+ return {
54
+ ...base,
55
+ status: 'not-admitted',
56
+ source: 'unknown',
57
+ intentLabel: 'Runtime unavailable',
58
+ detail: runtime.detail,
59
+ canDispatch: false,
60
+ };
61
+ }
62
+ if (input.configState === 'loading') {
63
+ return {
64
+ ...base,
65
+ status: 'checking',
66
+ source: 'unknown',
67
+ intentLabel: 'Reading App AIConfig',
68
+ detail: 'Reading the current Runtime-owned App AIConfig.',
69
+ canDispatch: false,
70
+ };
71
+ }
72
+ if (input.configState === 'failed') {
73
+ return {
74
+ ...base,
75
+ status: 'blocked',
76
+ source: 'unknown',
77
+ intentLabel: 'AIConfig unavailable',
78
+ detail: input.configError || 'The current App AIConfig could not be read.',
79
+ canDispatch: false,
80
+ };
81
+ }
82
+
83
+ const intent = findStudioCapabilityIntent(config, capabilityContract);
84
+ if (!intent) {
85
+ return {
86
+ ...base,
87
+ status: 'blocked',
88
+ source: 'unknown',
89
+ intentLabel: 'Not configured',
90
+ detail: 'This App AIConfig has no intent for the capability. Configure it here or in Nimi.',
91
+ canDispatch: false,
92
+ };
93
+ }
94
+ if (intent.route.oneofKind === 'local') {
95
+ return {
96
+ ...base,
97
+ status: 'configured',
98
+ source: 'local',
99
+ intentLabel: 'Local',
100
+ detail: 'The App owner committed the Local route. Runtime uses the current machine-selected Loadout when execution begins.',
101
+ canDispatch: true,
102
+ };
103
+ }
104
+ if (intent.route.oneofKind === 'cloud' && studioCloudIntentHasExactTarget(intent)) {
105
+ return {
106
+ ...base,
107
+ status: 'configured',
108
+ source: 'cloud',
109
+ intentLabel: 'Cloud',
110
+ detail: 'The App owner committed an exact Cloud Connector and provider-model target. Runtime validates those exact references when execution begins.',
111
+ canDispatch: true,
112
+ };
113
+ }
114
+
115
+ return {
116
+ ...base,
117
+ status: 'blocked',
118
+ source: 'unknown',
119
+ intentLabel: 'Invalid configuration',
120
+ detail: 'The capability has no supported Local or Cloud intent.',
121
+ canDispatch: false,
122
+ };
123
+ }
@@ -0,0 +1,35 @@
1
+ import type { StudioCapabilityRunResult } from './runtime-types.js';
2
+ import type { StudioCapabilityRuntimeContext } from './runtime.js';
3
+
4
+ // @nimi-authority: rule.nimi.platform.app-ecosystem.p-scaf-019c
5
+
6
+ export type StudioCapabilityRuntimeHandler = (
7
+ context: StudioCapabilityRuntimeContext,
8
+ ) => Promise<StudioCapabilityRunResult>;
9
+
10
+ export type StudioCapabilityRuntimeHandlers = Readonly<
11
+ Record<string, StudioCapabilityRuntimeHandler>
12
+ >;
13
+
14
+ export function composeStudioCapabilityRuntimeHandlers(
15
+ groups: readonly StudioCapabilityRuntimeHandlers[],
16
+ ): StudioCapabilityRuntimeHandlers {
17
+ const handlers: Record<string, StudioCapabilityRuntimeHandler> = {};
18
+ for (const group of groups) {
19
+ for (const [capabilityId, handler] of Object.entries(group)) {
20
+ if (!capabilityId || typeof handler !== 'function' || handlers[capabilityId]) {
21
+ throw new Error(`Duplicate or invalid Studio Runtime handler: ${capabilityId || 'missing'}`);
22
+ }
23
+ handlers[capabilityId] = handler;
24
+ }
25
+ }
26
+ return Object.freeze(handlers);
27
+ }
28
+
29
+ export function dispatchStudioCapabilityRuntime(
30
+ handlers: StudioCapabilityRuntimeHandlers,
31
+ context: StudioCapabilityRuntimeContext,
32
+ ): Promise<StudioCapabilityRunResult> | null {
33
+ const handler = handlers[context.capability.id];
34
+ return handler ? handler(context) : null;
35
+ }
@@ -0,0 +1,86 @@
1
+ import type { BrowserDataUrlAttachment } from '@nimiplatform/kit/features/chat/headless';
2
+ import type { StudioParameterValue } from './parameters.js';
3
+
4
+ export type StudioRuntimeCapabilityDescriptor = {
5
+ readonly id: string;
6
+ readonly label: string;
7
+ readonly missingSurface?: string;
8
+ };
9
+
10
+ export type StudioTrace = {
11
+ readonly traceId?: string;
12
+ };
13
+
14
+ export type StudioManagedArtifact = {
15
+ readonly relativePath: string;
16
+ readonly mediaType?: string;
17
+ readonly sizeBytes: number;
18
+ readonly sha256: string;
19
+ readonly displayName?: string;
20
+ readonly previewSource: 'managed-asset';
21
+ };
22
+
23
+ export type StudioTypedOutput =
24
+ | { readonly kind: 'text'; readonly text: string; readonly finishReason: string; readonly inputTokens?: number; readonly outputTokens?: number; readonly totalTokens?: number; readonly streamed: boolean }
25
+ | { readonly kind: 'embedding'; readonly vectorCount: number; readonly dimensions: number; readonly sample: number[]; readonly totalTokens?: number }
26
+ | { readonly kind: 'artifacts'; readonly jobId: string; readonly jobState: string; readonly artifactCount: number; readonly artifacts: StudioManagedArtifact[]; readonly firstArtifact?: StudioManagedArtifact }
27
+ | { readonly kind: 'transcript'; readonly text: string; readonly jobId: string; readonly jobState: string; readonly artifactCount: number }
28
+ | { readonly kind: 'voice-asset'; readonly jobId: string; readonly jobState: string; readonly voiceAssetId: string; readonly creationSource: 'reference-audio' | 'text-description'; readonly assetStatus: string; readonly voiceReference: { readonly kind: 'voice_asset_id'; readonly voiceAssetId: string } }
29
+ | { readonly kind: 'voice-catalog'; readonly voiceCount: number; readonly sample: Array<{ readonly voiceId: string; readonly creationSource: string; readonly status: string }> };
30
+
31
+ export type StudioTypedSuccess = {
32
+ readonly ok: true;
33
+ readonly capabilityId: string;
34
+ readonly capabilityLabel: string;
35
+ readonly message: string;
36
+ readonly output: StudioTypedOutput;
37
+ readonly trace?: StudioTrace;
38
+ };
39
+
40
+ export type StudioNonSuccessReason =
41
+ | 'runtime-unavailable'
42
+ | 'input-invalid'
43
+ | 'sdk-method-unavailable'
44
+ | 'principal-unauthorized'
45
+ | 'operation-aborted'
46
+ | 'runtime-canceled'
47
+ | 'runtime-timeout'
48
+ | 'stream-interrupted'
49
+ | 'runtime-call-failed';
50
+
51
+ export type StudioNonSuccessDiagnostics = {
52
+ readonly reasonCode: string;
53
+ readonly actionHint?: string;
54
+ readonly traceId?: string;
55
+ readonly retryable?: boolean;
56
+ readonly source?: string;
57
+ };
58
+
59
+ export type StudioNonSuccess = {
60
+ readonly ok: false;
61
+ readonly capabilityId: string;
62
+ readonly reason: StudioNonSuccessReason;
63
+ readonly message: string;
64
+ readonly actionHint: string;
65
+ readonly missingSurface?: string;
66
+ readonly diagnostics?: StudioNonSuccessDiagnostics;
67
+ };
68
+
69
+ export type StudioCapabilityRunResult = StudioTypedSuccess | StudioNonSuccess;
70
+
71
+ export type StudioRuntimeInspection = {
72
+ readonly status: 'connected' | 'unavailable';
73
+ readonly mode: string;
74
+ readonly detail: string;
75
+ };
76
+
77
+ export type StudioCapabilityRunInput = {
78
+ readonly capabilityId: string;
79
+ readonly prompt: string;
80
+ readonly scenarioId?: string;
81
+ readonly signal?: AbortSignal;
82
+ readonly onPartial?: (accumulatedText: string) => void;
83
+ readonly attachments?: BrowserDataUrlAttachment[];
84
+ readonly directive?: string;
85
+ readonly parameters?: StudioParameterValue;
86
+ };