@astrale-os/cli 1.0.0-beta.0 → 1.0.0-beta.2
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/README.md +7 -11
- package/dist/astrale.js +3769 -3549
- package/dist/public/connect-core.js +396 -392
- package/dist/public/keys/index.js +371 -378
- package/dist/public/paths/index.js +371 -378
- package/dist/types/lib/idp.d.ts +6 -0
- package/dist/types/lib/meta.d.ts +2 -2
- package/package.json +6 -4
- package/src/admin/binding.ts +168 -0
- package/src/admin/catalog/__tests__/client.test.ts +2 -2
- package/src/admin/catalog/client.ts +4 -9
- package/src/admin/instance/__tests__/client.test.ts +5 -3
- package/src/admin/instance/client.ts +9 -27
- package/src/commands/__tests__/auth-login.test.ts +54 -1
- package/src/commands/__tests__/call.test.ts +5 -1
- package/src/commands/__tests__/domain-install-operation.test.ts +23 -7
- package/src/commands/__tests__/domain-install-owned.test.ts +5 -1
- package/src/commands/__tests__/domain-list.test.ts +83 -4
- package/src/commands/__tests__/install-identity-override.test.ts +30 -19
- package/src/commands/__tests__/studio-descriptor.test.ts +22 -0
- package/src/commands/__tests__/update.test.ts +73 -0
- package/src/commands/domain/install.ts +11 -15
- package/src/commands/domain/list.ts +17 -12
- package/src/commands/studio.ts +20 -0
- package/src/commands/update.ts +39 -13
- package/src/connection/__tests__/errors.test.ts +16 -12
- package/src/connection/__tests__/exchange.test.ts +6 -5
- package/src/connection/exchange.ts +5 -4
- package/src/lib/__tests__/domain-publication.test.ts +144 -0
- package/src/lib/__tests__/idp-session.test.ts +22 -0
- package/src/lib/__tests__/idp.test.ts +2 -0
- package/src/lib/__tests__/instance.test.ts +6 -0
- package/src/lib/__tests__/studio-server-deps.test.ts +53 -16
- package/src/lib/__tests__/update.test.ts +12 -0
- package/src/lib/domain-publication.ts +102 -0
- package/src/lib/idp.ts +5 -1
- package/src/lib/instance.ts +6 -1
- package/src/lib/login-flow.ts +13 -3
- package/src/lib/meta.ts +2 -2
- package/src/lib/update.ts +4 -4
- package/src/program/__tests__/program.test.ts +1 -1
- package/studio/client/dist/assets/{elk-api-D2xgMJvi.js → elk-api-lwhFo7vB.js} +1 -1
- package/studio/client/dist/assets/index-B-c-smo9.js +8 -0
- package/studio/client/dist/assets/index-BckHuAWk.js +81 -0
- package/studio/client/dist/assets/index-C4aNQ6dz.css +1 -0
- package/studio/client/dist/index.html +2 -2
- package/studio/package.json +3 -1
- package/studio/server/agent/bridge/stdio.ts +8 -1
- package/studio/server/agent/conversation.test.ts +10 -5
- package/studio/server/agent/conversation.ts +56 -8
- package/studio/server/agent/harness/codex/loadout.ts +18 -21
- package/studio/server/agent/harness/gateway/config.ts +20 -18
- package/studio/server/agent/harness/selection.ts +9 -1
- package/studio/server/agent/prompts/system.ts +3 -2
- package/studio/server/agent/prompts/turn.ts +13 -4
- package/studio/server/agent/run/completion.ts +2 -2
- package/studio/server/agent/run/preparation.ts +6 -5
- package/studio/server/agent/run/transcript.test.ts +35 -1
- package/studio/server/agent/run/transcript.ts +169 -3
- package/studio/server/agent/run/usage.test.ts +24 -0
- package/studio/server/agent/run/usage.ts +26 -1
- package/studio/server/api/canvas.ts +44 -0
- package/studio/server/api/comments.ts +85 -0
- package/studio/server/api/context.ts +61 -0
- package/studio/server/api/deployment.ts +39 -0
- package/studio/server/api/documents.ts +55 -0
- package/studio/server/api/domain.ts +59 -0
- package/studio/server/api/http.ts +45 -0
- package/studio/server/api/project.ts +57 -0
- package/studio/server/api/schema.ts +12 -0
- package/studio/server/api/views.ts +57 -0
- package/studio/server/api/workspace.ts +60 -0
- package/studio/server/api.test.ts +147 -0
- package/studio/server/api.ts +20 -422
- package/studio/server/cache.test.ts +111 -0
- package/studio/server/cache.ts +163 -9
- package/studio/server/cli-consumers.test.ts +494 -0
- package/studio/server/cli.test.ts +88 -0
- package/studio/server/cli.ts +240 -0
- package/studio/server/client-package.ts +25 -1
- package/studio/server/domain.test.ts +30 -2
- package/studio/server/domain.ts +14 -2
- package/studio/server/environment/dotenv-preview.test.ts +39 -0
- package/studio/server/environment/dotenv-preview.ts +27 -0
- package/studio/server/environment/files.test.ts +53 -0
- package/studio/server/{state/env.ts → environment/files.ts} +7 -48
- package/studio/server/{state → handoff}/copy.ts +21 -6
- package/studio/server/handoff/service.test.ts +22 -0
- package/studio/server/{state/handoff.ts → handoff/service.ts} +21 -14
- package/studio/server/instances/active.ts +72 -0
- package/studio/server/instances/deploy-record.test.ts +31 -0
- package/studio/server/instances/deploy-record.ts +37 -0
- package/studio/server/instances/deploy.ts +56 -0
- package/studio/server/{state/instance.test.ts → instances/probe.test.ts} +25 -21
- package/studio/server/instances/probe.ts +67 -0
- package/studio/server/instances/status.test.ts +12 -0
- package/studio/server/instances/status.ts +49 -0
- package/studio/server/introspect/anatomy/client-tree.ts +57 -0
- package/studio/server/introspect/anatomy/env-fields.ts +66 -0
- package/studio/server/introspect/anatomy/schema-definition.ts +53 -0
- package/studio/server/introspect/anatomy/source.ts +181 -0
- package/studio/server/introspect/anatomy/views/legacy.ts +232 -0
- package/studio/server/introspect/anatomy/views/routes.ts +105 -0
- package/studio/server/introspect/anatomy/views.ts +58 -0
- package/studio/server/introspect/anatomy-extras.test.ts +91 -4
- package/studio/server/introspect/anatomy-extras.ts +7 -726
- package/studio/server/introspect/anatomy.ts +46 -19
- package/studio/server/introspect/bundle.ts +15 -6
- package/studio/server/introspect/canonical-schema.test.ts +79 -0
- package/studio/server/introspect/canonical-schema.ts +80 -18
- package/studio/server/introspect/config-preview.test.ts +32 -0
- package/studio/server/introspect/config-preview.ts +119 -0
- package/studio/server/introspect/diff.test.ts +7 -11
- package/studio/server/introspect/diff.ts +28 -37
- package/studio/server/introspect/extractor.ts +8 -6
- package/studio/server/introspect/hash.ts +5 -2
- package/studio/server/introspect/overlay-tsmorph.ts +7 -1305
- package/studio/server/introspect/revision.test.ts +54 -0
- package/studio/server/introspect/revision.ts +49 -0
- package/studio/server/introspect/runtime.test.ts +63 -5
- package/studio/server/introspect/runtime.ts +49 -3
- package/studio/server/introspect/schema-ir-json.ts +181 -0
- package/studio/server/introspect/source-overlay/annotations.ts +14 -0
- package/studio/server/introspect/source-overlay/handlers.ts +681 -0
- package/studio/server/introspect/source-overlay/kernel-calls.ts +49 -0
- package/studio/server/introspect/source-overlay/project.ts +248 -0
- package/studio/server/introspect/source-overlay/spans.ts +360 -0
- package/studio/server/json.ts +46 -0
- package/studio/server/lifecycle.ts +5 -1
- package/studio/server/sse.test.ts +26 -0
- package/studio/server/sse.ts +4 -1
- package/studio/server/state/baseline.test.ts +212 -1
- package/studio/server/state/baseline.ts +145 -29
- package/studio/server/state/comments.test.ts +66 -1
- package/studio/server/state/comments.ts +173 -10
- package/studio/server/state/context.ts +34 -2
- package/studio/server/state/documents.ts +42 -1
- package/studio/server/state/integrations.ts +27 -1
- package/studio/server/state/layout.test.ts +34 -0
- package/studio/server/state/layout.ts +44 -17
- package/studio/server/state/settings.ts +29 -22
- package/studio/server/state/store.test.ts +122 -0
- package/studio/server/state/store.ts +66 -17
- package/studio/server/state/visibility.ts +22 -7
- package/studio/server/views/model.test.ts +57 -0
- package/studio/server/views/model.ts +66 -0
- package/studio/server/views/runtime.ts +40 -0
- package/studio/server/views/selection-repository.test.ts +31 -0
- package/studio/server/views/selection-repository.ts +60 -0
- package/studio/server/views/session.test.ts +73 -0
- package/studio/server/views/session.ts +166 -0
- package/studio/server/{state/views.test.ts → views/target.test.ts} +8 -159
- package/studio/server/views/target.ts +248 -0
- package/studio/server/watch.ts +1 -1
- package/studio/server/workspace/catalog.test.ts +17 -0
- package/studio/server/workspace/catalog.ts +43 -0
- package/studio/server/{state → workspace}/create.test.ts +1 -1
- package/studio/server/{state → workspace}/create.ts +1 -1
- package/studio/server/{state → workspace}/git.ts +1 -1
- package/studio/server/workspace/updates.ts +83 -0
- package/studio/server/workspace-state.ts +1 -1
- package/studio/shared/contracts/README.md +16 -0
- package/studio/shared/contracts/agent.ts +270 -0
- package/studio/shared/contracts/runtime.ts +50 -0
- package/studio/shared/contracts/schema.ts +462 -0
- package/studio/shared/contracts/surface.test.ts +319 -0
- package/studio/shared/contracts/workspace.ts +274 -0
- package/studio/shared/layout.test.ts +116 -0
- package/studio/shared/schema/identity.test.ts +58 -0
- package/studio/shared/schema/identity.ts +82 -0
- package/studio/shared/types.ts +11 -1099
- package/studio/tsconfig.json +1 -1
- package/viewer/dist/main.js +35 -35
- package/studio/client/dist/assets/index-BMdnsIJA.css +0 -1
- package/studio/client/dist/assets/index-D-vRV8w7.js +0 -8
- package/studio/client/dist/assets/index-LGSWRrk8.js +0 -81
- package/studio/server/state/catalog.ts +0 -117
- package/studio/server/state/instance.ts +0 -280
- package/studio/server/state/updates.ts +0 -63
- package/studio/server/state/views.ts +0 -535
|
@@ -1,535 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* View Studio state and runtime bridge.
|
|
3
|
-
*
|
|
4
|
-
* Discovery and launches deliberately go through the public `astrale view`
|
|
5
|
-
* and `astrale query` commands. The Studio therefore shares the CLI's real
|
|
6
|
-
* View resolution, identity, instance, delegation, proxy, and Shell behavior
|
|
7
|
-
* instead of maintaining a second preview implementation.
|
|
8
|
-
*/
|
|
9
|
-
import type {
|
|
10
|
-
RememberedViewTarget,
|
|
11
|
-
StudioSchemaBundle,
|
|
12
|
-
ViewInfo,
|
|
13
|
-
ViewRuntime,
|
|
14
|
-
ViewSessionResult,
|
|
15
|
-
ViewTargetCandidate,
|
|
16
|
-
ViewTargetResult,
|
|
17
|
-
} from '../../shared/types'
|
|
18
|
-
|
|
19
|
-
import { activeInstanceName } from './instance'
|
|
20
|
-
import { readJson, writeJson } from './store'
|
|
21
|
-
|
|
22
|
-
const STATE_FILE = 'views.json'
|
|
23
|
-
const TARGET_LIMIT = 200
|
|
24
|
-
const NAMED_NAME = 'kernel.astrale.ai:interface.Named.property.name'
|
|
25
|
-
const DESCRIPTABLE_DESCRIPTION = 'kernel.astrale.ai:interface.Descriptable.property.description'
|
|
26
|
-
const STATUSED_STATUS = 'kernel.astrale.ai:interface.Statused.property.status'
|
|
27
|
-
|
|
28
|
-
interface StoredViewState {
|
|
29
|
-
targets?: Record<string, Record<string, RememberedViewTarget>>
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
interface AstraleResult<T> {
|
|
33
|
-
ok: boolean
|
|
34
|
-
data: T | null
|
|
35
|
-
detail: string
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
interface RawTargetRow {
|
|
39
|
-
id?: unknown
|
|
40
|
-
props?: Record<string, unknown>
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
interface RawQueryResult {
|
|
44
|
-
graph?: { nodes?: RawTargetRow[] }
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// A modal cleanup and the next modal launch can arrive almost simultaneously.
|
|
48
|
-
// The CLI view host owns a small local port range, so serialize its lifecycle
|
|
49
|
-
// commands and make the close complete before the next open allocates a port.
|
|
50
|
-
let viewSessionCommandTail: Promise<void> = Promise.resolve()
|
|
51
|
-
|
|
52
|
-
export async function getViewRuntime(
|
|
53
|
-
root: string,
|
|
54
|
-
origin: string,
|
|
55
|
-
view: ViewInfo,
|
|
56
|
-
bundle: StudioSchemaBundle | null,
|
|
57
|
-
timeoutMs: number,
|
|
58
|
-
): Promise<ViewRuntime> {
|
|
59
|
-
const instance = await activeInstanceName()
|
|
60
|
-
const targetRequired = viewDefinitionBindings(origin, view, bundle).length > 0
|
|
61
|
-
const targets = targetRequired
|
|
62
|
-
? instance
|
|
63
|
-
? await listViewTargets(root, origin, view, bundle, instance, timeoutMs)
|
|
64
|
-
: ({
|
|
65
|
-
status: 'unavailable',
|
|
66
|
-
items: [],
|
|
67
|
-
selected: null,
|
|
68
|
-
stale: null,
|
|
69
|
-
truncated: false,
|
|
70
|
-
reason: 'No active Astrale instance.',
|
|
71
|
-
} satisfies ViewTargetResult)
|
|
72
|
-
: ({
|
|
73
|
-
status: 'available',
|
|
74
|
-
items: [],
|
|
75
|
-
selected: null,
|
|
76
|
-
stale: null,
|
|
77
|
-
truncated: false,
|
|
78
|
-
} satisfies ViewTargetResult)
|
|
79
|
-
|
|
80
|
-
return {
|
|
81
|
-
slug: view.slug,
|
|
82
|
-
instance,
|
|
83
|
-
targetRequired,
|
|
84
|
-
targets,
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export function viewSessionArgs(
|
|
89
|
-
origin: string,
|
|
90
|
-
slug: string,
|
|
91
|
-
instance: string,
|
|
92
|
-
targetRef?: string,
|
|
93
|
-
): string[] {
|
|
94
|
-
const args = ['view', `/:${assertOrigin(origin)}:view.${assertViewSlug(slug)}`]
|
|
95
|
-
if (targetRef) args.push('--target', targetRef)
|
|
96
|
-
args.push('--no-open', '--json', '-i', instance)
|
|
97
|
-
return args
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
interface OpenedViewPayload {
|
|
101
|
-
session?: {
|
|
102
|
-
id?: string
|
|
103
|
-
pageUrl?: string
|
|
104
|
-
view?: { route?: { href?: string } }
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export function readyViewSession(
|
|
109
|
-
opened: OpenedViewPayload | null,
|
|
110
|
-
target: ViewTargetCandidate | null,
|
|
111
|
-
): Extract<ViewSessionResult, { status: 'ready' }> | null {
|
|
112
|
-
const session = opened?.session
|
|
113
|
-
const viewUrl = session?.view?.route?.href
|
|
114
|
-
if (!session?.id || !session.pageUrl || !viewUrl) return null
|
|
115
|
-
return {
|
|
116
|
-
status: 'ready',
|
|
117
|
-
sessionId: session.id,
|
|
118
|
-
pageUrl: session.pageUrl,
|
|
119
|
-
viewUrl,
|
|
120
|
-
target,
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export async function launchViewSession(
|
|
125
|
-
root: string,
|
|
126
|
-
origin: string,
|
|
127
|
-
view: ViewInfo,
|
|
128
|
-
bundle: StudioSchemaBundle | null,
|
|
129
|
-
request: { targetId?: unknown },
|
|
130
|
-
timeoutMs: number,
|
|
131
|
-
): Promise<ViewSessionResult> {
|
|
132
|
-
const instance = await activeInstanceName()
|
|
133
|
-
if (!instance) return { status: 'unavailable', reason: 'No active Astrale instance.' }
|
|
134
|
-
|
|
135
|
-
let target: ViewTargetCandidate | null = null
|
|
136
|
-
if (viewDefinitionBindings(origin, view, bundle).length > 0) {
|
|
137
|
-
const targetId = typeof request.targetId === 'string' ? request.targetId.trim() : ''
|
|
138
|
-
if (!targetId) return { status: 'unavailable', reason: 'Select a target before opening.' }
|
|
139
|
-
const targets = await listViewTargets(root, origin, view, bundle, instance, timeoutMs)
|
|
140
|
-
if (targets.status !== 'available') {
|
|
141
|
-
return { status: 'unavailable', reason: targets.reason ?? 'Targets could not be queried.' }
|
|
142
|
-
}
|
|
143
|
-
target = targets.items.find((item) => item.id === targetId) ?? null
|
|
144
|
-
if (!target) {
|
|
145
|
-
return {
|
|
146
|
-
status: 'unavailable',
|
|
147
|
-
reason: 'That target no longer exists or is no longer visible. Choose another target.',
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
const args = viewSessionArgs(origin, view.slug, instance, target?.ref)
|
|
153
|
-
const opened = await runViewSessionCommand<OpenedViewPayload>(
|
|
154
|
-
args,
|
|
155
|
-
Math.max(20_000, timeoutMs + 12_000),
|
|
156
|
-
)
|
|
157
|
-
const session = opened.ok ? readyViewSession(opened.data, target) : null
|
|
158
|
-
if (!session) {
|
|
159
|
-
return {
|
|
160
|
-
status: 'unavailable',
|
|
161
|
-
reason: opened.detail || '`astrale view` could not start the preview session.',
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
if (target) rememberTarget(root, instance, view.slug, target)
|
|
166
|
-
return session
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
export async function closeViewSession(sessionId: string): Promise<{ ok: true }> {
|
|
170
|
-
if (!/^v-[0-9a-f]+$/.test(sessionId)) return { ok: true }
|
|
171
|
-
await runViewSessionCommand(['view', '--close', sessionId, '--json'], 6000)
|
|
172
|
-
return { ok: true }
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
async function listViewTargets(
|
|
176
|
-
root: string,
|
|
177
|
-
origin: string,
|
|
178
|
-
view: ViewInfo,
|
|
179
|
-
bundle: StudioSchemaBundle | null,
|
|
180
|
-
instance: string,
|
|
181
|
-
timeoutMs: number,
|
|
182
|
-
): Promise<ViewTargetResult> {
|
|
183
|
-
const bindings = viewDefinitionBindings(origin, view, bundle)
|
|
184
|
-
if (bindings.length === 0) {
|
|
185
|
-
return {
|
|
186
|
-
status: 'available',
|
|
187
|
-
items: [],
|
|
188
|
-
selected: null,
|
|
189
|
-
stale: null,
|
|
190
|
-
truncated: false,
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
const queried = await Promise.all(
|
|
195
|
-
bindings.map(async (binding) => {
|
|
196
|
-
const definition = targetDefinition(binding.className, binding.classOrigin, binding.kind)
|
|
197
|
-
const result = await runAstraleJson<RawQueryResult>(
|
|
198
|
-
[
|
|
199
|
-
'query',
|
|
200
|
-
'--definition',
|
|
201
|
-
definition,
|
|
202
|
-
'--limit',
|
|
203
|
-
String(TARGET_LIMIT + 1),
|
|
204
|
-
'--json',
|
|
205
|
-
'-i',
|
|
206
|
-
instance,
|
|
207
|
-
],
|
|
208
|
-
timeoutMs,
|
|
209
|
-
)
|
|
210
|
-
return { binding, result }
|
|
211
|
-
}),
|
|
212
|
-
)
|
|
213
|
-
const successes = queried.filter(
|
|
214
|
-
(item) => item.result.ok && Array.isArray(item.result.data?.graph?.nodes),
|
|
215
|
-
)
|
|
216
|
-
if (successes.length === 0) {
|
|
217
|
-
const reason = queried.map((item) => item.result.detail).find(Boolean)
|
|
218
|
-
return {
|
|
219
|
-
status: 'unavailable',
|
|
220
|
-
items: [],
|
|
221
|
-
selected: null,
|
|
222
|
-
stale: null,
|
|
223
|
-
truncated: false,
|
|
224
|
-
reason: reason || 'The active instance could not be queried for view targets.',
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
const byId = new Map<string, ViewTargetCandidate>()
|
|
229
|
-
let truncated = false
|
|
230
|
-
for (const { binding, result } of successes) {
|
|
231
|
-
const rows = result.data?.graph?.nodes ?? []
|
|
232
|
-
if (rows.length > TARGET_LIMIT) truncated = true
|
|
233
|
-
for (const row of rows.slice(0, TARGET_LIMIT)) {
|
|
234
|
-
const target = targetFromRow(row, binding.className, binding.classOrigin)
|
|
235
|
-
if (target) byId.set(target.id, target)
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
const items = [...byId.values()].sort(
|
|
239
|
-
(a, b) => a.label.localeCompare(b.label) || a.className.localeCompare(b.className),
|
|
240
|
-
)
|
|
241
|
-
const remembered = readRememberedTarget(root, instance, view.slug)
|
|
242
|
-
const reconciled = reconcileRememberedTarget(remembered, items)
|
|
243
|
-
return {
|
|
244
|
-
status: 'available',
|
|
245
|
-
items,
|
|
246
|
-
...reconciled,
|
|
247
|
-
truncated,
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
export function reconcileRememberedTarget(
|
|
252
|
-
remembered: RememberedViewTarget | null,
|
|
253
|
-
items: ViewTargetCandidate[],
|
|
254
|
-
): Pick<ViewTargetResult, 'selected' | 'stale'> {
|
|
255
|
-
if (!remembered) return { selected: null, stale: null }
|
|
256
|
-
const selected = items.find((item) => item.id === remembered.id) ?? null
|
|
257
|
-
return { selected, stale: selected ? null : remembered }
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
export function targetFromRow(
|
|
261
|
-
row: RawTargetRow,
|
|
262
|
-
className: string,
|
|
263
|
-
classOrigin: string,
|
|
264
|
-
): ViewTargetCandidate | null {
|
|
265
|
-
const id =
|
|
266
|
-
typeof row.id === 'string' ? row.id : typeof row.props?.id === 'string' ? row.props.id : ''
|
|
267
|
-
if (!id) return null
|
|
268
|
-
const props = row.props ?? {}
|
|
269
|
-
const firstName = stringProp(props, ['.property.firstName'])
|
|
270
|
-
const lastName = stringProp(props, ['.property.lastName'])
|
|
271
|
-
const label =
|
|
272
|
-
asNonEmptyString(props[NAMED_NAME]) ??
|
|
273
|
-
stringProp(props, ['.property.title', '.property.label', '.property.name', '.property.slug']) ??
|
|
274
|
-
([firstName, lastName].filter(Boolean).join(' ') || `${className} · ${id.slice(0, 8)}`)
|
|
275
|
-
return {
|
|
276
|
-
id,
|
|
277
|
-
ref: `@${id}`,
|
|
278
|
-
className,
|
|
279
|
-
classOrigin,
|
|
280
|
-
label,
|
|
281
|
-
description:
|
|
282
|
-
asNonEmptyString(props[DESCRIPTABLE_DESCRIPTION]) ??
|
|
283
|
-
stringProp(props, ['.property.description', '.property.email']),
|
|
284
|
-
status: asNonEmptyString(props[STATUSED_STATUS]) ?? stringProp(props, ['.property.status']),
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
export function targetDefinition(
|
|
289
|
-
className: string,
|
|
290
|
-
classOrigin: string,
|
|
291
|
-
kind: 'class' | 'interface' = 'class',
|
|
292
|
-
): string {
|
|
293
|
-
return `/:${assertOrigin(classOrigin)}:${kind}.${assertSchemaName(className)}`
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
interface ViewDefinitionBinding {
|
|
297
|
-
className: string
|
|
298
|
-
classOrigin: string
|
|
299
|
-
kind: 'class' | 'interface'
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
/** Preserve exact canonical View target coordinates. The short-name anatomy
|
|
303
|
-
* fallback exists only for legacy defineView registries. */
|
|
304
|
-
export function viewDefinitionBindings(
|
|
305
|
-
origin: string,
|
|
306
|
-
view: ViewInfo,
|
|
307
|
-
bundle: StudioSchemaBundle | null,
|
|
308
|
-
): ViewDefinitionBinding[] {
|
|
309
|
-
const target = bundle?.ir?.views?.[view.slug]?.target
|
|
310
|
-
if (target) {
|
|
311
|
-
if (target.kind === 'domain') return []
|
|
312
|
-
return uniqueViewBindings(
|
|
313
|
-
target.definitions
|
|
314
|
-
.filter(
|
|
315
|
-
(definition): definition is typeof definition & { kind: 'class' | 'interface' } =>
|
|
316
|
-
definition.kind === 'class' || definition.kind === 'interface',
|
|
317
|
-
)
|
|
318
|
-
.map((definition) => ({
|
|
319
|
-
className: definition.name,
|
|
320
|
-
classOrigin: definition.origin,
|
|
321
|
-
kind: definition.kind,
|
|
322
|
-
})),
|
|
323
|
-
)
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
const names = Array.isArray(view.viewFor) ? view.viewFor : view.viewFor ? [view.viewFor] : []
|
|
327
|
-
const ir = bundle?.ir
|
|
328
|
-
if (!ir) {
|
|
329
|
-
return uniqueViewBindings(
|
|
330
|
-
names.map((className) => ({ className, classOrigin: origin, kind: 'class' })),
|
|
331
|
-
)
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
return uniqueViewBindings(
|
|
335
|
-
names.flatMap((className) => {
|
|
336
|
-
const local: ViewDefinitionBinding[] = []
|
|
337
|
-
const localClass = ir.classes[className]
|
|
338
|
-
const localInterface = ir.interfaces[className]
|
|
339
|
-
if (localClass) {
|
|
340
|
-
local.push({
|
|
341
|
-
className,
|
|
342
|
-
classOrigin: localClass.ref?.origin ?? localClass.origin ?? ir.domain ?? origin,
|
|
343
|
-
kind: 'class',
|
|
344
|
-
})
|
|
345
|
-
}
|
|
346
|
-
if (localInterface) {
|
|
347
|
-
local.push({
|
|
348
|
-
className,
|
|
349
|
-
classOrigin: localInterface.ref?.origin ?? localInterface.origin ?? ir.domain ?? origin,
|
|
350
|
-
kind: 'interface',
|
|
351
|
-
})
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
// Presence of the exact index makes it authoritative, including when it
|
|
355
|
-
// has no candidate for this short anatomy name. Since this API is plural,
|
|
356
|
-
// retain every exact homonym instead of picking one by object order.
|
|
357
|
-
if (ir.importsByKey !== undefined) {
|
|
358
|
-
const imported = Object.keys(ir.importsByKey).flatMap((key) => {
|
|
359
|
-
const binding = viewBindingFromDefinitionKey(key)
|
|
360
|
-
return binding?.className === className ? [binding] : []
|
|
361
|
-
})
|
|
362
|
-
return [...local, ...imported]
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
const legacy = ir.imports[className]
|
|
366
|
-
if (legacy) {
|
|
367
|
-
return [
|
|
368
|
-
...local,
|
|
369
|
-
{
|
|
370
|
-
className,
|
|
371
|
-
classOrigin: legacy.origin,
|
|
372
|
-
kind: legacy.definition,
|
|
373
|
-
} satisfies ViewDefinitionBinding,
|
|
374
|
-
]
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
// Old serializer projections did not retain exact indexes or declaration
|
|
378
|
-
// refs. Preserve their historical local-class assumption only here.
|
|
379
|
-
return local.length > 0 ? local : [{ className, classOrigin: origin, kind: 'class' }]
|
|
380
|
-
}),
|
|
381
|
-
)
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
function viewBindingFromDefinitionKey(key: string): ViewDefinitionBinding | null {
|
|
385
|
-
const separator = key.lastIndexOf(':')
|
|
386
|
-
if (separator <= 0) return null
|
|
387
|
-
const classOrigin = key.slice(0, separator)
|
|
388
|
-
const ref = key.slice(separator + 1)
|
|
389
|
-
const match = /^(class|interface)\.([A-Za-z_$][\w$]*)$/.exec(ref)
|
|
390
|
-
if (!match) return null
|
|
391
|
-
return {
|
|
392
|
-
className: match[2]!,
|
|
393
|
-
classOrigin,
|
|
394
|
-
kind: match[1] as ViewDefinitionBinding['kind'],
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
function uniqueViewBindings(bindings: ViewDefinitionBinding[]): ViewDefinitionBinding[] {
|
|
399
|
-
const unique = new Map<string, ViewDefinitionBinding>()
|
|
400
|
-
for (const binding of bindings) {
|
|
401
|
-
const key = `${binding.classOrigin}:${binding.kind}.${binding.className}`
|
|
402
|
-
if (!unique.has(key)) unique.set(key, binding)
|
|
403
|
-
}
|
|
404
|
-
return [...unique.values()]
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
function rememberTarget(
|
|
408
|
-
root: string,
|
|
409
|
-
instance: string,
|
|
410
|
-
slug: string,
|
|
411
|
-
target: ViewTargetCandidate,
|
|
412
|
-
): void {
|
|
413
|
-
const stored = readJson<StoredViewState>(root, STATE_FILE, {})
|
|
414
|
-
stored.targets ??= {}
|
|
415
|
-
stored.targets[instance] ??= {}
|
|
416
|
-
stored.targets[instance][slug] = {
|
|
417
|
-
id: target.id,
|
|
418
|
-
className: target.className,
|
|
419
|
-
classOrigin: target.classOrigin,
|
|
420
|
-
label: target.label,
|
|
421
|
-
}
|
|
422
|
-
writeJson(root, STATE_FILE, stored)
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
function readRememberedTarget(
|
|
426
|
-
root: string,
|
|
427
|
-
instance: string,
|
|
428
|
-
slug: string,
|
|
429
|
-
): RememberedViewTarget | null {
|
|
430
|
-
return readJson<StoredViewState>(root, STATE_FILE, {}).targets?.[instance]?.[slug] ?? null
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
function assertOrigin(value: string): string {
|
|
434
|
-
if (!/^[a-z0-9][a-z0-9.-]*$/i.test(value)) throw new Error(`Invalid domain origin: ${value}`)
|
|
435
|
-
return value
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
function assertSchemaName(value: string): string {
|
|
439
|
-
if (!/^[A-Za-z_$][\w$]*$/.test(value)) throw new Error(`Invalid class name: ${value}`)
|
|
440
|
-
return value
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
function assertViewSlug(value: string): string {
|
|
444
|
-
if (!/^[a-z][a-z0-9-]*$/.test(value)) throw new Error(`Invalid view slug: ${value}`)
|
|
445
|
-
return value
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
function stringProp(props: Record<string, unknown>, suffixes: string[]): string | undefined {
|
|
449
|
-
for (const suffix of suffixes) {
|
|
450
|
-
for (const [key, value] of Object.entries(props)) {
|
|
451
|
-
if (!key.endsWith(suffix)) continue
|
|
452
|
-
const text = asNonEmptyString(value)
|
|
453
|
-
if (text) return text
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
return undefined
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
function asNonEmptyString(value: unknown): string | undefined {
|
|
460
|
-
return typeof value === 'string' && value.trim() ? value.trim() : undefined
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
export function conciseCliFailure(raw: string): string | undefined {
|
|
464
|
-
const lines = raw
|
|
465
|
-
.split(/\r?\n/)
|
|
466
|
-
.map((line) => line.trim())
|
|
467
|
-
.filter(Boolean)
|
|
468
|
-
const error = lines.find((line) => /^[A-Za-z_$][\w$]*(?:Error|Exception):\s+\S/.test(line))
|
|
469
|
-
if (error) return error.slice(0, 600)
|
|
470
|
-
const explicit = lines.find((line) => /^(?:error|failed):\s+\S/i.test(line))
|
|
471
|
-
if (explicit) return explicit.slice(0, 600)
|
|
472
|
-
const useful = lines.find(
|
|
473
|
-
(line) =>
|
|
474
|
-
!/^\d+\s+\|/.test(line) &&
|
|
475
|
-
line !== '^' &&
|
|
476
|
-
!/^at\s/.test(line) &&
|
|
477
|
-
!/^Bun v\d/.test(line) &&
|
|
478
|
-
!/^details?:\s*[{[]?$/i.test(line),
|
|
479
|
-
)
|
|
480
|
-
return useful?.slice(0, 600)
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
function runViewSessionCommand<T>(args: string[], timeoutMs: number): Promise<AstraleResult<T>> {
|
|
484
|
-
const command = viewSessionCommandTail.then(
|
|
485
|
-
() => runAstraleJson<T>(args, timeoutMs),
|
|
486
|
-
() => runAstraleJson<T>(args, timeoutMs),
|
|
487
|
-
)
|
|
488
|
-
viewSessionCommandTail = command.then(
|
|
489
|
-
() => undefined,
|
|
490
|
-
() => undefined,
|
|
491
|
-
)
|
|
492
|
-
return command
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
async function runAstraleJson<T>(args: string[], timeoutMs: number): Promise<AstraleResult<T>> {
|
|
496
|
-
try {
|
|
497
|
-
const proc = Bun.spawn(['astrale', ...args], { stdout: 'pipe', stderr: 'pipe' })
|
|
498
|
-
const timer = setTimeout(() => {
|
|
499
|
-
try {
|
|
500
|
-
proc.kill()
|
|
501
|
-
} catch {
|
|
502
|
-
// Already exited.
|
|
503
|
-
}
|
|
504
|
-
}, timeoutMs)
|
|
505
|
-
try {
|
|
506
|
-
const [stdout, stderr, code] = await Promise.all([
|
|
507
|
-
new Response(proc.stdout).text(),
|
|
508
|
-
new Response(proc.stderr).text(),
|
|
509
|
-
proc.exited,
|
|
510
|
-
])
|
|
511
|
-
let data: T | null = null
|
|
512
|
-
try {
|
|
513
|
-
data = JSON.parse(stdout) as T
|
|
514
|
-
} catch {
|
|
515
|
-
// The detail below remains useful for CLI failures and old versions.
|
|
516
|
-
}
|
|
517
|
-
const parsed = data as { message?: unknown; error?: unknown } | null
|
|
518
|
-
const detail =
|
|
519
|
-
asNonEmptyString(parsed?.message) ??
|
|
520
|
-
asNonEmptyString(parsed?.error) ??
|
|
521
|
-
conciseCliFailure(stderr) ??
|
|
522
|
-
conciseCliFailure(stdout) ??
|
|
523
|
-
''
|
|
524
|
-
return { ok: code === 0 && data !== null, data, detail }
|
|
525
|
-
} finally {
|
|
526
|
-
clearTimeout(timer)
|
|
527
|
-
}
|
|
528
|
-
} catch (error) {
|
|
529
|
-
return {
|
|
530
|
-
ok: false,
|
|
531
|
-
data: null,
|
|
532
|
-
detail: error instanceof Error ? error.message : String(error),
|
|
533
|
-
}
|
|
534
|
-
}
|
|
535
|
-
}
|