@fugood/bricks-ctor 2.25.0-beta.10 → 2.25.0-beta.12

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.
@@ -0,0 +1,278 @@
1
+ import { generateCalulationMap, validateConfig } from '../util'
2
+
3
+ const baseConfig = (overrides = {}) => ({
4
+ inputs: {},
5
+ enable_async: false,
6
+ disabled_triggers: {},
7
+ output: null,
8
+ outputs: {},
9
+ error: null,
10
+ code: 'return inputs',
11
+ ...overrides,
12
+ })
13
+
14
+ const SANDBOX_PREFIX = 'PROPERTY_BANK_COMMAND_NODE_'
15
+
16
+ const sandboxNodeIds = (map) =>
17
+ Object.entries(map)
18
+ .filter(([id, node]) => id.startsWith(SANDBOX_PREFIX) && node.type === 'command-node-sandbox')
19
+ .map(([id, node]) => ({ id, command: node.properties.command }))
20
+
21
+ const findSandboxIds = (map) => {
22
+ const sandbox = sandboxNodeIds(map)
23
+ return {
24
+ run: sandbox.find((s) => s.command === 'SANDBOX_RUN_JAVASCRIPT')?.id,
25
+ error: sandbox.find((s) => s.command === 'SANDBOX_GET_ERROR')?.id,
26
+ result: sandbox.find((s) => s.command === 'SANDBOX_GET_RETURN_VALUE')?.id,
27
+ }
28
+ }
29
+
30
+ describe('validateConfig', () => {
31
+ test('returns without throwing for a clean config', () => {
32
+ expect(() => validateConfig(baseConfig({ inputs: { a: 'foo' } }))).not.toThrow()
33
+ })
34
+
35
+ test('skips overlap checks in manual trigger mode', () => {
36
+ const config = baseConfig({
37
+ trigger_mode: 'manual',
38
+ inputs: { a: 'foo' },
39
+ output: 'a',
40
+ error: 'a',
41
+ outputs: { x: ['a'] },
42
+ })
43
+ expect(() => validateConfig(config)).not.toThrow()
44
+ })
45
+
46
+ test('throws when error key collides with an input id', () => {
47
+ const config = baseConfig({ inputs: { a: 'foo' }, error: 'a' })
48
+ expect(() => validateConfig(config)).toThrow(/key: error/)
49
+ })
50
+
51
+ test('throws when output key collides with an input id', () => {
52
+ const config = baseConfig({ inputs: { a: 'foo' }, output: 'a' })
53
+ expect(() => validateConfig(config)).toThrow(/key: output/)
54
+ })
55
+
56
+ test('throws when any outputs entry references an input id', () => {
57
+ const config = baseConfig({
58
+ inputs: { a: 'foo', b: 'bar' },
59
+ outputs: { x: ['c', 'b'] },
60
+ })
61
+ expect(() => validateConfig(config)).toThrow(/key: outputs/)
62
+ })
63
+
64
+ test('does not throw when error/output are falsy and inputs are empty', () => {
65
+ expect(() => validateConfig(baseConfig())).not.toThrow()
66
+ })
67
+ })
68
+
69
+ describe('generateCalulationMap', () => {
70
+ test('produces only the three sandbox nodes for an empty config', () => {
71
+ const result = generateCalulationMap(baseConfig())
72
+
73
+ expect(Object.keys(result.map)).toHaveLength(3)
74
+ const { run, error, result: returnValue } = findSandboxIds(result.map)
75
+
76
+ expect(result.map[run].in.inputs).toEqual([])
77
+ expect(result.map[error].out.result).toEqual([])
78
+ expect(result.map[returnValue].out.result).toEqual([])
79
+ expect(Object.keys(result.editor_info)).toHaveLength(3)
80
+ })
81
+
82
+ test('chains multiple inputs through OBJECT_SET commands', () => {
83
+ const result = generateCalulationMap(baseConfig({ inputs: { a: 'foo.bar', b: 'baz' } }))
84
+
85
+ const { run } = findSandboxIds(result.map)
86
+
87
+ // Each input gets a data-node + an OBJECT_SET command-node.
88
+ expect(result.map.a.type).toBe('data-node')
89
+ expect(result.map.b.type).toBe('data-node')
90
+
91
+ const aCommandId = result.map.a.out.value[0].id
92
+ const bCommandId = result.map.b.out.value[0].id
93
+ expect(aCommandId).not.toBe(bCommandId)
94
+
95
+ const aCmd = result.map[aCommandId]
96
+ const bCmd = result.map[bCommandId]
97
+ expect(aCmd.properties.command).toBe('OBJECT_SET')
98
+ expect(aCmd.properties.args.path).toBe('foo.bar')
99
+ expect(bCmd.properties.command).toBe('OBJECT_SET')
100
+ expect(bCmd.properties.args.path).toBe('baz')
101
+
102
+ // First command has no upstream obj; second command's obj input is the first command.
103
+ expect(aCmd.in.obj).toBeNull()
104
+ expect(bCmd.in.obj).toEqual([{ id: aCommandId, port: 'result' }])
105
+ // First command forwards its result to the second command's `obj` input.
106
+ expect(aCmd.out.result).toEqual([{ id: bCommandId, port: 'obj' }])
107
+ // The last command feeds the SANDBOX_RUN_JAVASCRIPT `inputs` port.
108
+ expect(bCmd.out.result).toEqual([{ id: run, port: 'inputs' }])
109
+ expect(result.map[run].in.inputs).toEqual([{ id: bCommandId, port: 'result' }])
110
+ })
111
+
112
+ test('builds OBJECT_GET commands and target data-nodes for outputs', () => {
113
+ const result = generateCalulationMap(baseConfig({ outputs: { resultPath: ['pb1', 'pb2'] } }))
114
+
115
+ const { result: returnValue } = findSandboxIds(result.map)
116
+
117
+ // Both target property-bank nodes are created as data-nodes.
118
+ expect(result.map.pb1.type).toBe('data-node')
119
+ expect(result.map.pb2.type).toBe('data-node')
120
+
121
+ // The SANDBOX_GET_RETURN_VALUE forwards to the OBJECT_GET command for the output entry.
122
+ const objectGetRefs = result.map[returnValue].out.result
123
+ expect(objectGetRefs).toHaveLength(1)
124
+ const getCommandId = objectGetRefs[0].id
125
+ expect(objectGetRefs[0].port).toBe('obj')
126
+
127
+ const getCmd = result.map[getCommandId]
128
+ expect(getCmd.type).toBe('command-node-object')
129
+ expect(getCmd.properties.command).toBe('OBJECT_GET')
130
+ expect(getCmd.properties.args.path).toBe('resultPath')
131
+ expect(getCmd.in.obj).toEqual([{ id: returnValue, port: 'result' }])
132
+
133
+ // OBJECT_GET feeds both target data-nodes' `change` ports.
134
+ expect(getCmd.out.result).toEqual([
135
+ { id: 'pb1', port: 'change' },
136
+ { id: 'pb2', port: 'change' },
137
+ ])
138
+ // Target data-nodes consume the OBJECT_GET result via their `change` port.
139
+ expect(result.map.pb1.in.change).toEqual([{ id: getCommandId, port: 'result' }])
140
+ expect(result.map.pb2.in.change).toEqual([{ id: getCommandId, port: 'result' }])
141
+
142
+ // Without input usage their `out.value` defaults to null.
143
+ expect(result.map.pb1.out.value).toBeNull()
144
+ expect(result.map.pb2.out.value).toBeNull()
145
+ })
146
+
147
+ // Manual mode is the only mode that lets an output target reuse an input id
148
+ // (see validateConfig tests). When that happens, generateCalulationMap must
149
+ // keep the input-side `out.value` so the data-node remains a usable input.
150
+ test.each([
151
+ ['outputs', { outputs: { result: ['shared'] } }],
152
+ ['output', { output: 'shared' }],
153
+ ['error', { error: 'shared' }],
154
+ ])('preserves input out.value when %s target reuses an input id', (_, overrides) => {
155
+ const result = generateCalulationMap(
156
+ baseConfig({ trigger_mode: 'manual', inputs: { shared: 'foo' }, ...overrides }),
157
+ )
158
+ expect(Array.isArray(result.map.shared.out.value)).toBe(true)
159
+ expect(result.map[result.map.shared.out.value[0].id].properties.command).toBe('OBJECT_SET')
160
+ })
161
+
162
+ test('also rewires in.change to OBJECT_GET when an outputs target reuses an input id', () => {
163
+ const result = generateCalulationMap(
164
+ baseConfig({
165
+ trigger_mode: 'manual',
166
+ inputs: { shared: 'foo' },
167
+ outputs: { result: ['shared'] },
168
+ }),
169
+ )
170
+ expect(result.map.shared.in.change).toHaveLength(1)
171
+ expect(result.map[result.map.shared.in.change[0].id].properties.command).toBe('OBJECT_GET')
172
+ })
173
+
174
+ test('preserves out.value when the same pb appears in multiple outputs entries', () => {
175
+ // Two outputs entries both target `pb1`. The reduce visits each entry in turn
176
+ // and must preserve the accumulated `out.value` from the first iteration via
177
+ // `acc.map[pb]` rather than wiping it on the second.
178
+ const result = generateCalulationMap(
179
+ baseConfig({ outputs: { first: ['pb1'], second: ['pb1'] } }),
180
+ )
181
+ expect(result.map.pb1.type).toBe('data-node')
182
+ // Without an input, out.value remains null after both passes.
183
+ expect(result.map.pb1.out.value).toBeNull()
184
+ // The latest OBJECT_GET wins as the change source — each iteration overwrites
185
+ // `in.change`, so we end up pointing at the second outputs entry's command.
186
+ expect(result.map.pb1.in.change).toHaveLength(1)
187
+ })
188
+
189
+ test('wires the error data-node when error is configured', () => {
190
+ const result = generateCalulationMap(baseConfig({ error: 'errNode' }))
191
+
192
+ const { error } = findSandboxIds(result.map)
193
+ // SANDBOX_GET_ERROR now broadcasts to the error data-node's `change` port.
194
+ expect(result.map[error].out.result).toEqual([{ id: 'errNode', port: 'change' }])
195
+ expect(result.map.errNode.type).toBe('data-node')
196
+ expect(result.map.errNode.in.change).toEqual([{ id: error, port: 'result' }])
197
+ // No upstream input → out.value falls back to null.
198
+ expect(result.map.errNode.out.value).toBeNull()
199
+ // editor_info contains the new node.
200
+ expect(result.editor_info.errNode).toBeDefined()
201
+ })
202
+
203
+ test('wires the output data-node when output is configured', () => {
204
+ const result = generateCalulationMap(baseConfig({ output: 'outNode' }))
205
+
206
+ const { result: returnValue } = findSandboxIds(result.map)
207
+ expect(result.map[returnValue].out.result).toEqual([{ id: 'outNode', port: 'change' }])
208
+ expect(result.map.outNode.type).toBe('data-node')
209
+ expect(result.map.outNode.in.change).toEqual([{ id: returnValue, port: 'result' }])
210
+ expect(result.editor_info.outNode).toBeDefined()
211
+ })
212
+
213
+ test('manual trigger mode sets disable_trigger_command on input value ports', () => {
214
+ const result = generateCalulationMap(
215
+ baseConfig({
216
+ trigger_mode: 'manual',
217
+ inputs: { a: 'foo', b: 'bar' },
218
+ }),
219
+ )
220
+ const aCmdId = result.map.a.out.value[0].id
221
+ const bCmdId = result.map.b.out.value[0].id
222
+ expect(result.map[aCmdId].in.value[0].disable_trigger_command).toBe(true)
223
+ expect(result.map[bCmdId].in.value[0].disable_trigger_command).toBe(true)
224
+ })
225
+
226
+ test('auto trigger mode honours per-key disabled_triggers', () => {
227
+ const result = generateCalulationMap(
228
+ baseConfig({
229
+ inputs: { a: 'foo', b: 'bar' },
230
+ disabled_triggers: { a: true, b: false },
231
+ }),
232
+ )
233
+ const aCmdId = result.map.a.out.value[0].id
234
+ const bCmdId = result.map.b.out.value[0].id
235
+ expect(result.map[aCmdId].in.value[0].disable_trigger_command).toBe(true)
236
+ // Falsy disabled_triggers entry → property is not set (or set to undefined).
237
+ expect(result.map[bCmdId].in.value[0].disable_trigger_command).toBeUndefined()
238
+ })
239
+
240
+ test('auto trigger mode without disabled_triggers leaves disable_trigger_command undefined', () => {
241
+ const result = generateCalulationMap(baseConfig({ inputs: { a: 'foo' } }))
242
+ const aCmdId = result.map.a.out.value[0].id
243
+ expect(result.map[aCmdId].in.value[0].disable_trigger_command).toBeUndefined()
244
+ })
245
+
246
+ test('snapshotMode forwards to makeId so generated ids are sequential', () => {
247
+ const result = generateCalulationMap(baseConfig({ inputs: { a: 'foo' } }), {
248
+ snapshotMode: true,
249
+ })
250
+ const sandboxIds = sandboxNodeIds(result.map).map((s) => s.id)
251
+ // Snapshot-mode ids embed a 12-digit counter — stable suffix lets us assert order.
252
+ sandboxIds.forEach((id) => {
253
+ expect(id).toMatch(/^PROPERTY_BANK_COMMAND_NODE_00000000-0000-0000-0000-\d{12}$/)
254
+ })
255
+ })
256
+
257
+ test('validateConfig errors propagate out of generateCalulationMap', () => {
258
+ expect(() => generateCalulationMap(baseConfig({ inputs: { a: 'foo' }, error: 'a' }))).toThrow(
259
+ /key: error/,
260
+ )
261
+ })
262
+
263
+ test('SANDBOX_GET_RETURN_VALUE broadcasts to both output target and outputs commands', () => {
264
+ const result = generateCalulationMap(
265
+ baseConfig({
266
+ output: 'outNode',
267
+ outputs: { foo: ['pb1'] },
268
+ }),
269
+ )
270
+ const { result: returnValue } = findSandboxIds(result.map)
271
+ const refs = result.map[returnValue].out.result
272
+ // First entry is the output data-node's `change` port; remainder are OBJECT_GET ids.
273
+ expect(refs[0]).toEqual({ id: 'outNode', port: 'change' })
274
+ expect(refs).toHaveLength(2)
275
+ expect(refs[1].port).toBe('obj')
276
+ expect(result.map[refs[1].id].properties.command).toBe('OBJECT_GET')
277
+ })
278
+ })
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@fugood/bricks-ctor",
3
- "version": "2.25.0-beta.10",
3
+ "version": "2.25.0-beta.12",
4
4
  "main": "index.ts",
5
5
  "scripts": {
6
6
  "typecheck": "tsc --noEmit",
7
7
  "build": "bun scripts/build.js"
8
8
  },
9
9
  "dependencies": {
10
- "@fugood/bricks-cli": "^2.25.0-beta.10",
10
+ "@fugood/bricks-cli": "^2.25.0-beta.12",
11
11
  "@huggingface/gguf": "^0.3.2",
12
12
  "@iarna/toml": "^3.0.0",
13
13
  "@modelcontextprotocol/sdk": "^1.15.0",
@@ -25,5 +25,5 @@
25
25
  "peerDependencies": {
26
26
  "oxfmt": "^0.36.0"
27
27
  },
28
- "gitHead": "7b909dc28862872743cf0459ff65d16af67721c6"
28
+ "gitHead": "a732a16341bd23fb7ee8e83dbbe0f0c07bad0eb3"
29
29
  }
package/tools/deploy.ts CHANGED
@@ -141,6 +141,10 @@ await writeFile(configPath, JSON.stringify(releaseConfig))
141
141
 
142
142
  const args = ['bricks', command, 'release', app.id, '-c', configPath, '--json']
143
143
 
144
+ if (app.name) {
145
+ args.push('--name', app.name)
146
+ }
147
+
144
148
  if (version) {
145
149
  args.push('--version', version)
146
150
  }
@@ -0,0 +1,110 @@
1
+ import { readFile, writeFile } from 'node:fs/promises'
2
+ import { parseArgs } from 'util'
3
+ import { sh } from './_shell'
4
+ import { buildCommitArgs } from './_git-author'
5
+
6
+ const cwd = process.cwd()
7
+
8
+ const readJson = async (p: string) => JSON.parse(await readFile(p, 'utf8'))
9
+
10
+ const {
11
+ values: { 'auto-commit': autoCommit, 'no-check': noCheck, 'no-validate': noValidate, yes, help },
12
+ } = parseArgs({
13
+ args: process.argv.slice(2),
14
+ options: {
15
+ 'auto-commit': { type: 'boolean' },
16
+ 'no-check': { type: 'boolean' },
17
+ 'no-validate': { type: 'boolean' },
18
+ yes: { type: 'boolean', short: 'y' },
19
+ help: { type: 'boolean', short: 'h' },
20
+ },
21
+ allowPositionals: true,
22
+ })
23
+
24
+ if (help) {
25
+ console.log(`Push compiled config to BRICKS without creating a release.
26
+
27
+ Options:
28
+ --auto-commit Auto-commit unstaged changes before pushing
29
+ --no-check Skip the conflict guard (don't pass --last-commit-id)
30
+ --no-validate Skip server-side config schema validation
31
+ -y, --yes Skip all prompts
32
+ -h, --help Show this help message`)
33
+ process.exit(0)
34
+ }
35
+
36
+ // Detect git repo (mirrors deploy.ts)
37
+ const { exitCode } = await sh`cd ${cwd} && git status`.quiet().nothrow()
38
+ const isGitRepo = exitCode === 0
39
+
40
+ if (!isGitRepo && !yes) {
41
+ const confirmContinue = prompt('No git repository found, continue? (y/n)')
42
+ if (confirmContinue !== 'y') throw new Error('Update cancelled')
43
+ }
44
+
45
+ // Read application.json + compiled config
46
+ const app = await readJson(`${cwd}/application.json`)
47
+ const config = await readJson(`${cwd}/.bricks/build/application-config.json`)
48
+
49
+ // Handle unstaged changes the same way deploy.ts does.
50
+ let commitId = ''
51
+ let parentCommitId = ''
52
+ if (isGitRepo) {
53
+ const unstagedChanges = await sh`cd ${cwd} && git diff --name-only --diff-filter=ACMR`.text()
54
+ if (unstagedChanges) {
55
+ if (autoCommit) {
56
+ // Capture the pre-commit HEAD so we can use it as the conflict-check
57
+ // baseline (the server should still hold it from the prior deploy).
58
+ parentCommitId = (await sh`cd ${cwd} && git rev-parse HEAD`.nothrow().text()).trim()
59
+ await sh`cd ${cwd} && git add -A`
60
+ const commitArgs = await buildCommitArgs(cwd, ['chore: update bricks config'])
61
+ await sh`cd ${cwd} && git ${commitArgs}`
62
+ } else {
63
+ throw new Error('Unstaged changes found, please commit or stash your changes before updating')
64
+ }
65
+ }
66
+ commitId = (await sh`cd ${cwd} && git rev-parse HEAD`.text()).trim()
67
+ }
68
+
69
+ // Auto-derive --last-commit-id for the server-side conflict guard.
70
+ // - parent of the auto-commit (server still holds it from the prior deploy)
71
+ // - current HEAD (clean tree — server should be at the same commit too)
72
+ let lastCommitId: string | undefined
73
+ if (!noCheck) {
74
+ if (parentCommitId) lastCommitId = parentCommitId
75
+ else if (commitId) lastCommitId = commitId
76
+ }
77
+
78
+ if (!yes) {
79
+ const confirm = prompt('Are you sure you want to push the new config? (y/n)')
80
+ if (confirm !== 'y') throw new Error('Update cancelled')
81
+ }
82
+
83
+ const isModule = app.type === 'module'
84
+ const command = isModule ? 'module' : 'app'
85
+
86
+ const updateConfig = {
87
+ ...config,
88
+ bricks_project_last_commit_id: commitId || undefined,
89
+ }
90
+ const configPath = `${cwd}/.bricks/build/update-config.json`
91
+ await writeFile(configPath, JSON.stringify(updateConfig))
92
+
93
+ const args = ['bricks', command, 'update', app.id, '-f', configPath, '--json']
94
+ if (noValidate) args.push('--no-validate')
95
+ if (lastCommitId) args.push('--last-commit-id', lastCommitId)
96
+
97
+ const result = await sh`${args}`.quiet().nothrow()
98
+
99
+ if (result.exitCode !== 0) {
100
+ const output = result.stderr.toString() || result.stdout.toString()
101
+ try {
102
+ const json = JSON.parse(output)
103
+ throw new Error(json.error?.message || json.error || 'Update failed')
104
+ } catch {
105
+ throw new Error(output || 'Update failed')
106
+ }
107
+ }
108
+
109
+ const output = JSON.parse(result.stdout.toString())
110
+ console.log(`${isModule ? 'Module' : 'App'} config updated: ${output.target?.name || app.name}`)