@things-factory/figure-service 10.1.287 → 10.1.290
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/dist-server/routers/figure-capability-module-router.d.ts +11 -0
- package/dist-server/routers/figure-capability-module-router.js +44 -0
- package/dist-server/routes.js +3 -0
- package/dist-server/service/capability-module/capability-module-entity.d.ts +52 -0
- package/dist-server/service/capability-module/capability-module-entity.js +173 -0
- package/dist-server/service/capability-module/capability-module-resolver.d.ts +24 -0
- package/dist-server/service/capability-module/capability-module-resolver.js +195 -0
- package/dist-server/service/capability-module/capability-module-store.d.ts +109 -0
- package/dist-server/service/capability-module/capability-module-store.js +325 -0
- package/dist-server/service/capability-module/capability-release-check.d.ts +8 -0
- package/dist-server/service/capability-module/capability-release-check.js +57 -0
- package/dist-server/service/figure/figure-format.d.ts +9 -1
- package/dist-server/service/figure/figure-format.js +4 -2
- package/dist-server/service/figure/figure-mcp-agent.d.ts +6 -1
- package/dist-server/service/figure/figure-mcp-agent.js +2 -1
- package/dist-server/service/figure/figure-mutation.js +6 -1
- package/dist-server/service/figure/figure-propose-steps.d.ts +23 -2
- package/dist-server/service/figure/figure-propose-steps.js +56 -7
- package/dist-server/service/figure/figure-query.js +6 -3
- package/dist-server/service/figure/figure-tools.js +37 -2
- package/dist-server/service/index.d.ts +8 -2
- package/dist-server/service/index.js +15 -3
- package/dist-server/service/install-key/figure-install-key.d.ts +5 -2
- package/dist-server/service/install-key/figure-install-key.js +22 -6
- package/package.json +11 -8
- package/translations/en.json +22 -1
- package/translations/es.json +18 -1
- package/translations/ja.json +18 -1
- package/translations/ko.json +22 -1
- package/translations/ms.json +18 -1
- package/translations/zh.json +18 -1
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.preparedDescriptors = exports.moduleFolder = exports.MODULE_MAX_BYTES = exports.MODULE_MIMETYPE = exports.MODULE_REF_TYPE = void 0;
|
|
4
|
+
exports.useModuleFolder = useModuleFolder;
|
|
5
|
+
exports.loadModuleBytes = loadModuleBytes;
|
|
6
|
+
exports.moduleContractOf = moduleContractOf;
|
|
7
|
+
exports.moduleAttachmentOf = moduleAttachmentOf;
|
|
8
|
+
exports.registerCapabilityModule = registerCapabilityModule;
|
|
9
|
+
exports.keepCapabilityChoices = keepCapabilityChoices;
|
|
10
|
+
exports.chosenCapabilityChoices = chosenCapabilityChoices;
|
|
11
|
+
exports.setCurrentCapabilityModule = setCurrentCapabilityModule;
|
|
12
|
+
exports.chooseProvider = chooseProvider;
|
|
13
|
+
exports.chosenModules = chosenModules;
|
|
14
|
+
exports.providersFor = providersFor;
|
|
15
|
+
exports.chosenModuleBytes = chosenModuleBytes;
|
|
16
|
+
exports.prepareCapabilityModules = prepareCapabilityModules;
|
|
17
|
+
exports.forgetCapabilityModules = forgetCapabilityModules;
|
|
18
|
+
const fs_1 = require("fs");
|
|
19
|
+
const path_1 = require("path");
|
|
20
|
+
const typeorm_1 = require("typeorm");
|
|
21
|
+
const figure_model_1 = require("@hatiolab/figure-model");
|
|
22
|
+
const attachment_base_1 = require("@things-factory/attachment-base");
|
|
23
|
+
const env_1 = require("@things-factory/env");
|
|
24
|
+
const shell_1 = require("@things-factory/shell");
|
|
25
|
+
const figure_thumbnail_store_js_1 = require("../figure/figure-thumbnail-store.js");
|
|
26
|
+
const capability_module_entity_js_1 = require("./capability-module-entity.js");
|
|
27
|
+
/*
|
|
28
|
+
* Capability modules (ADR-0172): one provider's descriptors in one ESM file, registered by a superuser for the whole
|
|
29
|
+
* server (decision 4) or pushed by a domain's consuming app over its link (amendment 1, decisions 13-15), chosen by a
|
|
30
|
+
* domain (decision 5), loaded by the modeller in the browser (decision 6) and by this server to check a release
|
|
31
|
+
* (decision 9). The file is kept in attachment-base under its SHA-256, as a mesh file is; to load it, the server writes
|
|
32
|
+
* it to its cache folder as `<sha256>.mjs` and requires it (Node 22.12+ requires an ES module; tsc turns `import()` into
|
|
33
|
+
* `require`).
|
|
34
|
+
*/
|
|
35
|
+
exports.MODULE_REF_TYPE = 'figure-capability-module';
|
|
36
|
+
exports.MODULE_MIMETYPE = 'text/javascript';
|
|
37
|
+
/** A module is a few descriptors: a file larger than this is not one. */
|
|
38
|
+
exports.MODULE_MAX_BYTES = 512 * 1024;
|
|
39
|
+
/** The actor as a row's creator: by its id alone, so saving the row never writes the user. */
|
|
40
|
+
const byId = (user) => (user?.id ? { id: user.id } : undefined);
|
|
41
|
+
/*
|
|
42
|
+
* A run-time import: a static `import … from`, a bare `import '…'`, `export … from`, `import(…)` or `require(…)`. The
|
|
43
|
+
* contract is one file (decision 3): an import of anything outside it would load a second copy of the scene's registry
|
|
44
|
+
* or of figure-model, so none is taken.
|
|
45
|
+
*/
|
|
46
|
+
const RUNTIME_IMPORT = [/^\s*import\s*[\w{*'"`]/m, /^\s*export\s*(\*|\{[^}]*\})\s*from\b/m, /\bimport\s*\(/, /\brequire\s*\(/];
|
|
47
|
+
let folderInUse;
|
|
48
|
+
/**
|
|
49
|
+
* The folder this server loads modules from: the app's `cache` (a copy of what attachment-base keeps, written again when
|
|
50
|
+
* missing), unless a test gives another.
|
|
51
|
+
*/
|
|
52
|
+
const moduleFolder = () => folderInUse ?? env_1.config.getPath(null, 'cache/figure-capability-modules');
|
|
53
|
+
exports.moduleFolder = moduleFolder;
|
|
54
|
+
/** For tests: load modules from this folder until set back; returns the one it replaced. */
|
|
55
|
+
function useModuleFolder(folder) {
|
|
56
|
+
const before = folderInUse;
|
|
57
|
+
folderInUse = folder;
|
|
58
|
+
return before;
|
|
59
|
+
}
|
|
60
|
+
/*
|
|
61
|
+
* Node's own require, which loads an ES module (22.12+): taken from the built-in module so that a loader in front of
|
|
62
|
+
* this file's `require` (a test runner's) does not stand between the file and Node.
|
|
63
|
+
*/
|
|
64
|
+
const nodeRequire = process.getBuiltinModule?.('node:module')?.createRequire(__filename) ?? require;
|
|
65
|
+
/* Loaded modules by hash: the content is its hash, so a loaded one never changes. */
|
|
66
|
+
const loaded = new Map();
|
|
67
|
+
/**
|
|
68
|
+
* Loads the module of these bytes: written to the data folder as `<sha256>.mjs` (when not there yet) and required. The
|
|
69
|
+
* bytes are checked against the hash: a file that is not its address is not loaded.
|
|
70
|
+
*/
|
|
71
|
+
function loadModuleBytes(sha256, bytes, folder) {
|
|
72
|
+
const known = loaded.get(sha256);
|
|
73
|
+
if (known)
|
|
74
|
+
return known;
|
|
75
|
+
folder ??= (0, exports.moduleFolder)();
|
|
76
|
+
const file = (0, path_1.join)(folder, `${sha256}.mjs`);
|
|
77
|
+
if (!(0, fs_1.existsSync)(file) || (0, figure_thumbnail_store_js_1.sha256Of)((0, fs_1.readFileSync)(file)) !== sha256) {
|
|
78
|
+
if (!bytes)
|
|
79
|
+
throw new Error(`capability module ${sha256} is not in ${folder}`);
|
|
80
|
+
if ((0, figure_thumbnail_store_js_1.sha256Of)(bytes) !== sha256)
|
|
81
|
+
throw new Error(`capability module bytes are not ${sha256}`);
|
|
82
|
+
(0, fs_1.mkdirSync)(folder, { recursive: true });
|
|
83
|
+
const partial = `${file}.${process.pid}.tmp`;
|
|
84
|
+
(0, fs_1.writeFileSync)(partial, bytes);
|
|
85
|
+
(0, fs_1.renameSync)(partial, file);
|
|
86
|
+
}
|
|
87
|
+
const module = nodeRequire(file);
|
|
88
|
+
loaded.set(sha256, module);
|
|
89
|
+
return module;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Why these bytes are not a capability module (decision 3): too large, not text, a run-time import, a file that does not
|
|
93
|
+
* load, or exports that break the contract. Empty when it is one; the module, loaded, comes with it.
|
|
94
|
+
*/
|
|
95
|
+
function moduleContractOf(bytes, folder) {
|
|
96
|
+
const sha256 = (0, figure_thumbnail_store_js_1.sha256Of)(bytes);
|
|
97
|
+
if (bytes.length > exports.MODULE_MAX_BYTES)
|
|
98
|
+
return { sha256, issues: [`the file is larger than ${exports.MODULE_MAX_BYTES} bytes`] };
|
|
99
|
+
const source = bytes.toString('utf8');
|
|
100
|
+
if (source.includes('\u0000'))
|
|
101
|
+
return { sha256, issues: ['the file is not text'] };
|
|
102
|
+
const imports = RUNTIME_IMPORT.filter(re => re.test(source));
|
|
103
|
+
if (imports.length)
|
|
104
|
+
return { sha256, issues: ['the file imports at run time: a capability module is one file and imports types only'] };
|
|
105
|
+
let module;
|
|
106
|
+
try {
|
|
107
|
+
module = loadModuleBytes(sha256, bytes, folder);
|
|
108
|
+
}
|
|
109
|
+
catch (e) {
|
|
110
|
+
return { sha256, issues: [`the file does not load as an ES module: ${e?.message ?? String(e)}`] };
|
|
111
|
+
}
|
|
112
|
+
const issues = (0, figure_model_1.capabilityModuleIssues)(module);
|
|
113
|
+
if (issues.length)
|
|
114
|
+
loaded.delete(sha256);
|
|
115
|
+
return { sha256, issues, ...(issues.length ? {} : { module }) };
|
|
116
|
+
}
|
|
117
|
+
/** The system domain, which keeps the files of the system's modules; the asker's when there is none. */
|
|
118
|
+
async function keeperDomain(tx, fallback) {
|
|
119
|
+
return (await tx.getRepository(shell_1.Domain).findOne({ where: { systemFlag: true } })) ?? fallback;
|
|
120
|
+
}
|
|
121
|
+
/** The attachment holding a module's file, wherever it is kept. */
|
|
122
|
+
async function moduleAttachmentOf(tx, sha256) {
|
|
123
|
+
return (await tx.getRepository(attachment_base_1.Attachment).findOne({ where: { refType: exports.MODULE_REF_TYPE, refBy: sha256 } })) ?? undefined;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Registers a module (decisions 4 and 15): checked by the contract, kept by its hash, and made its provider's current
|
|
127
|
+
* version. `owner` is the domain whose app pushed it over its link, or none for the system's. Refused: a module that
|
|
128
|
+
* breaks the contract, and a version not later than every version the same owner has of the provider. The same file
|
|
129
|
+
* again is the version already kept. A pushed provider is chosen in its domain at once (decision 15).
|
|
130
|
+
*/
|
|
131
|
+
async function registerCapabilityModule(tx, input, files = (0, figure_thumbnail_store_js_1.thumbnailFiles)(), folder) {
|
|
132
|
+
const { issues, module, sha256 } = moduleContractOf(input.bytes, folder);
|
|
133
|
+
if (issues.length || !module)
|
|
134
|
+
throw new shell_1.Refusal('figure-capability-module-invalid', { issues: issues.join(' · ') }, 'the file is not a capability module: {issues}');
|
|
135
|
+
const { provider, version } = module;
|
|
136
|
+
if (input.provider && provider !== input.provider)
|
|
137
|
+
throw new shell_1.Refusal('figure-capability-module-provider', { provider, allowed: input.provider }, 'this key may push the module of {allowed}, not of {provider}.');
|
|
138
|
+
const repo = tx.getRepository(capability_module_entity_js_1.FigureCapabilityModule);
|
|
139
|
+
const ownerWhere = input.owner ? { id: input.owner.id } : (0, typeorm_1.IsNull)();
|
|
140
|
+
const versions = await repo.find({ where: { provider, domain: ownerWhere } });
|
|
141
|
+
const same = versions.find(v => v.sha256 === sha256);
|
|
142
|
+
if (same) {
|
|
143
|
+
if (input.owner)
|
|
144
|
+
await chooseProvider(tx, input.owner, provider, true, input.user);
|
|
145
|
+
return same;
|
|
146
|
+
}
|
|
147
|
+
const later = versions.find(v => (0, figure_model_1.compareCapabilityVersions)(v.version, version) >= 0);
|
|
148
|
+
if (later)
|
|
149
|
+
throw new shell_1.Refusal('figure-capability-module-version', { provider, version, kept: later.version }, '{provider} {kept} is kept already: raise the module version above it (now {version}).');
|
|
150
|
+
if (!(await moduleAttachmentOf(tx, sha256))) {
|
|
151
|
+
const domain = input.owner ?? (await keeperDomain(tx, input.asker));
|
|
152
|
+
const name = `${sha256}.mjs`;
|
|
153
|
+
/* The file first: it cannot be rolled back. The row that names it comes after, in the caller's transaction. */
|
|
154
|
+
const put = await files.put(input.bytes, exports.MODULE_MIMETYPE, name, { state: { domain, user: input.user, tx } });
|
|
155
|
+
await tx.getRepository(attachment_base_1.Attachment).save({
|
|
156
|
+
domain,
|
|
157
|
+
creator: byId(input.user),
|
|
158
|
+
updater: byId(input.user),
|
|
159
|
+
id: put.id,
|
|
160
|
+
name,
|
|
161
|
+
mimetype: exports.MODULE_MIMETYPE,
|
|
162
|
+
encoding: 'utf8',
|
|
163
|
+
refType: exports.MODULE_REF_TYPE,
|
|
164
|
+
refBy: sha256,
|
|
165
|
+
category: 'module',
|
|
166
|
+
size: String(put.size),
|
|
167
|
+
path: put.path,
|
|
168
|
+
...(put.contents ? { contents: put.contents } : {})
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
for (const v of versions.filter(v => v.current))
|
|
172
|
+
await repo.save({ ...v, current: false });
|
|
173
|
+
const saved = await repo.save({
|
|
174
|
+
domain: input.owner,
|
|
175
|
+
provider,
|
|
176
|
+
version,
|
|
177
|
+
sha256,
|
|
178
|
+
capabilities: module.capabilities.map(d => d.name),
|
|
179
|
+
current: true,
|
|
180
|
+
creator: byId(input.user)
|
|
181
|
+
});
|
|
182
|
+
if (input.owner)
|
|
183
|
+
await chooseProvider(tx, input.owner, provider, true, input.user);
|
|
184
|
+
return saved;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Keeps the choice lists a domain's provider pushed (ADR-0172 amendment 2, decisions 19 and 20): the last ones replace
|
|
188
|
+
* the ones before, and the same ones are not written again. They are compared apart from the module, which keeps its
|
|
189
|
+
* version when only the lists changed. Refused when they break the shape or name another provider's capability.
|
|
190
|
+
* Answers whether they were written.
|
|
191
|
+
*/
|
|
192
|
+
async function keepCapabilityChoices(tx, domain, provider, choices) {
|
|
193
|
+
const issues = (0, figure_model_1.capabilityChoicesIssues)(choices, provider);
|
|
194
|
+
if (issues.length)
|
|
195
|
+
throw new shell_1.Refusal('figure-capability-choices-invalid', { issues: issues.slice(0, 5).join(' · ') }, 'the choice lists cannot be taken: {issues}');
|
|
196
|
+
const repo = tx.getRepository(capability_module_entity_js_1.FigureCapabilityChoiceLists);
|
|
197
|
+
const found = await repo.findOne({ where: { domain: { id: domain.id }, provider } });
|
|
198
|
+
if (found && (0, figure_thumbnail_store_js_1.sha256Of)(Buffer.from(JSON.stringify(found.choices ?? {}))) === (0, figure_thumbnail_store_js_1.sha256Of)(Buffer.from(JSON.stringify(choices))))
|
|
199
|
+
return false;
|
|
200
|
+
await repo.save({ ...(found ?? { domain, provider }), choices });
|
|
201
|
+
return true;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* The choice lists of the providers a domain uses, by `<capability>.<setting>` (decision 21): those its own providers
|
|
205
|
+
* pushed. A system provider has none.
|
|
206
|
+
*/
|
|
207
|
+
async function chosenCapabilityChoices(tx, domain) {
|
|
208
|
+
const providers = new Set((await chosenModules(tx, domain)).map(m => m.provider));
|
|
209
|
+
const rows = await tx.getRepository(capability_module_entity_js_1.FigureCapabilityChoiceLists).find({ where: { domain: { id: domain.id } } });
|
|
210
|
+
return Object.assign({}, ...rows.filter(r => providers.has(r.provider)).map(r => r.choices ?? {}));
|
|
211
|
+
}
|
|
212
|
+
/** Makes one kept version of a system provider its current one again (a step back). */
|
|
213
|
+
async function setCurrentCapabilityModule(tx, provider, version) {
|
|
214
|
+
const repo = tx.getRepository(capability_module_entity_js_1.FigureCapabilityModule);
|
|
215
|
+
const versions = await repo.find({ where: { provider, domain: (0, typeorm_1.IsNull)() } });
|
|
216
|
+
const target = versions.find(v => v.version === version);
|
|
217
|
+
if (!target)
|
|
218
|
+
throw new shell_1.Refusal('figure-capability-module-not-found', { provider, version }, 'there is no {provider} {version}.');
|
|
219
|
+
for (const v of versions)
|
|
220
|
+
if (v.current !== (v === target))
|
|
221
|
+
await repo.save({ ...v, current: v === target });
|
|
222
|
+
return { ...target, current: true };
|
|
223
|
+
}
|
|
224
|
+
/** Uses a provider in a domain, or stops using it (decision 5). Refused for a provider nobody registered for it. */
|
|
225
|
+
async function chooseProvider(tx, domain, provider, on, user) {
|
|
226
|
+
const uses = tx.getRepository(capability_module_entity_js_1.FigureCapabilityUse);
|
|
227
|
+
const found = await uses.findOne({ where: { domain: { id: domain.id }, provider } });
|
|
228
|
+
if (!on) {
|
|
229
|
+
if (found)
|
|
230
|
+
await uses.delete(found.id);
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
if (found)
|
|
234
|
+
return true;
|
|
235
|
+
if (!(await currentOf(tx, domain, provider)))
|
|
236
|
+
throw new shell_1.Refusal('figure-capability-provider-unknown', { provider }, 'no capability module of {provider} is registered.');
|
|
237
|
+
await uses.save({ domain, provider, creator: byId(user) });
|
|
238
|
+
return true;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* The current version of a provider for a domain: the domain's own, pushed over its link, before the system's. A
|
|
242
|
+
* domain whose app pushes a provider uses its own, even where the system has one of the same name (ADR-0172 amendment
|
|
243
|
+
* 1, decisions 15 and 18).
|
|
244
|
+
*/
|
|
245
|
+
async function currentOf(tx, domain, provider) {
|
|
246
|
+
const repo = tx.getRepository(capability_module_entity_js_1.FigureCapabilityModule);
|
|
247
|
+
return ((await repo.findOne({ where: { provider, current: true, domain: { id: domain.id } } })) ??
|
|
248
|
+
(await repo.findOne({ where: { provider, current: true, domain: (0, typeorm_1.IsNull)() } })) ??
|
|
249
|
+
undefined);
|
|
250
|
+
}
|
|
251
|
+
/** The modules a domain uses: the current version of every provider it chose. A parent's choice is not inherited. */
|
|
252
|
+
async function chosenModules(tx, domain) {
|
|
253
|
+
const uses = await tx.getRepository(capability_module_entity_js_1.FigureCapabilityUse).find({ where: { domain: { id: domain.id } }, order: { provider: 'ASC' } });
|
|
254
|
+
const out = [];
|
|
255
|
+
for (const use of uses) {
|
|
256
|
+
const m = await currentOf(tx, domain, use.provider);
|
|
257
|
+
if (m)
|
|
258
|
+
out.push({ provider: m.provider, version: m.version, sha256: m.sha256, capabilities: m.capabilities ?? [] });
|
|
259
|
+
}
|
|
260
|
+
return out;
|
|
261
|
+
}
|
|
262
|
+
/** Every provider this domain may choose: the system's, and its own pushed ones in their place. */
|
|
263
|
+
async function providersFor(tx, domain) {
|
|
264
|
+
const repo = tx.getRepository(capability_module_entity_js_1.FigureCapabilityModule);
|
|
265
|
+
const all = await repo.find({ where: [{ domain: (0, typeorm_1.IsNull)() }, { domain: { id: domain.id } }], relations: { domain: true }, order: { provider: 'ASC', createdAt: 'DESC' } });
|
|
266
|
+
const chosen = new Set((await tx.getRepository(capability_module_entity_js_1.FigureCapabilityUse).find({ where: { domain: { id: domain.id } } })).map(u => u.provider));
|
|
267
|
+
const out = [];
|
|
268
|
+
for (const provider of [...new Set(all.map(m => m.provider))]) {
|
|
269
|
+
const own = all.filter(m => m.provider === provider && m.domain);
|
|
270
|
+
const versions = own.length ? own : all.filter(m => m.provider === provider && !m.domain);
|
|
271
|
+
const current = versions.find(m => m.current);
|
|
272
|
+
if (current)
|
|
273
|
+
out.push({ provider, current, own: own.length > 0, chosen: chosen.has(provider), versions });
|
|
274
|
+
}
|
|
275
|
+
return out;
|
|
276
|
+
}
|
|
277
|
+
/** The bytes of a module this domain uses, or undefined: the file door serves only those (decision 6). */
|
|
278
|
+
async function chosenModuleBytes(tx, domain, sha256, files = (0, figure_thumbnail_store_js_1.thumbnailFiles)()) {
|
|
279
|
+
if (!(await chosenModules(tx, domain)).some(m => m.sha256 === sha256))
|
|
280
|
+
return undefined;
|
|
281
|
+
const kept = await moduleAttachmentOf(tx, sha256);
|
|
282
|
+
return kept ? Buffer.from(await files.read(kept.path)) : undefined;
|
|
283
|
+
}
|
|
284
|
+
/* The descriptors each domain's release check reads, made ready before the check (which is synchronous) runs. */
|
|
285
|
+
const prepared = new Map();
|
|
286
|
+
/**
|
|
287
|
+
* Loads the modules a domain uses, for the release check to read (decision 9): each file from the data folder, or from
|
|
288
|
+
* attachment-base when the folder does not have it yet. A module that does not load is left out, and the figure's
|
|
289
|
+
* capabilities of it are then not verified -- the check blocks them.
|
|
290
|
+
*/
|
|
291
|
+
async function prepareCapabilityModules(tx, domain, files = (0, figure_thumbnail_store_js_1.thumbnailFiles)(), folder) {
|
|
292
|
+
const descriptors = new Map();
|
|
293
|
+
let modules = [];
|
|
294
|
+
try {
|
|
295
|
+
modules = await chosenModules(tx, domain);
|
|
296
|
+
}
|
|
297
|
+
catch (e) {
|
|
298
|
+
console.warn(`[figure] the capability modules of the domain could not be read: ${e?.message ?? e}`);
|
|
299
|
+
}
|
|
300
|
+
for (const m of modules) {
|
|
301
|
+
try {
|
|
302
|
+
let module = loaded.get(m.sha256);
|
|
303
|
+
if (!module) {
|
|
304
|
+
const kept = (0, fs_1.existsSync)((0, path_1.join)(folder ?? (0, exports.moduleFolder)(), `${m.sha256}.mjs`)) ? undefined : await moduleAttachmentOf(tx, m.sha256);
|
|
305
|
+
module = loadModuleBytes(m.sha256, kept ? Buffer.from(await files.read(kept.path)) : undefined, folder);
|
|
306
|
+
}
|
|
307
|
+
for (const d of module.capabilities)
|
|
308
|
+
descriptors.set(d.name, d);
|
|
309
|
+
}
|
|
310
|
+
catch (e) {
|
|
311
|
+
console.warn(`[figure] capability module ${m.provider} ${m.version} did not load: ${e?.message ?? e}`);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
prepared.set(domain.id, descriptors);
|
|
315
|
+
return descriptors;
|
|
316
|
+
}
|
|
317
|
+
/** The descriptors last prepared for a domain; none when it was never prepared. */
|
|
318
|
+
const preparedDescriptors = (domainId) => (domainId && prepared.get(domainId)) || new Map();
|
|
319
|
+
exports.preparedDescriptors = preparedDescriptors;
|
|
320
|
+
/** For tests: forget loaded and prepared modules. */
|
|
321
|
+
function forgetCapabilityModules() {
|
|
322
|
+
loaded.clear();
|
|
323
|
+
prepared.clear();
|
|
324
|
+
}
|
|
325
|
+
//# sourceMappingURL=capability-module-store.js.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type FigureAsset, type FigureCapabilityDescriptor } from '@hatiolab/figure-model';
|
|
2
|
+
import { type FigureReleaseCheckInput, type GateFinding } from '../figure/figure-format.js';
|
|
3
|
+
type Say = (key: string, params: Record<string, unknown>, english: string) => string;
|
|
4
|
+
/** The findings for a figure's consumer capabilities, by these descriptors, in these words. */
|
|
5
|
+
export declare function capabilityFindings(asset: FigureAsset, descriptors: ReadonlyMap<string, FigureCapabilityDescriptor>, say: Say, lang?: string): GateFinding[];
|
|
6
|
+
/** The check as the release door runs it: the releasing domain's prepared descriptors, the reader's words. */
|
|
7
|
+
export declare function capabilityReleaseCheck(input: FigureReleaseCheckInput): GateFinding[];
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.capabilityFindings = capabilityFindings;
|
|
4
|
+
exports.capabilityReleaseCheck = capabilityReleaseCheck;
|
|
5
|
+
const figure_model_1 = require("@hatiolab/figure-model");
|
|
6
|
+
const figure_format_js_1 = require("../figure/figure-format.js");
|
|
7
|
+
const capability_module_store_js_1 = require("./capability-module-store.js");
|
|
8
|
+
/** The findings for a figure's consumer capabilities, by these descriptors, in these words. */
|
|
9
|
+
function capabilityFindings(asset, descriptors, say, lang = 'en') {
|
|
10
|
+
const names = (asset.document.capabilities.names ?? []).filter(figure_model_1.isFigureConsumerCapability);
|
|
11
|
+
if (!names.length)
|
|
12
|
+
return [];
|
|
13
|
+
const { issues } = (0, figure_model_1.consumerCapabilityVerdict)(descriptors, asset.document.capabilities.names ?? [], asset);
|
|
14
|
+
const label = (name) => (0, figure_model_1.capabilityText)(descriptors.get(name)?.label, lang) || name;
|
|
15
|
+
return issues.map(issue => {
|
|
16
|
+
const name = label(issue.name);
|
|
17
|
+
if (issue.code === 'CAPABILITY_UNREGISTERED') {
|
|
18
|
+
const provider = (0, figure_model_1.capabilityProviderOf)(issue.name) ?? '';
|
|
19
|
+
return {
|
|
20
|
+
code: 'capability-unverified',
|
|
21
|
+
message: say('figure.gate.capability-unverified.message', { name, provider }, `The capability '${name}' is of ${provider}, which this domain does not use.`),
|
|
22
|
+
why: say('figure.gate.capability-unverified.why', {}, 'A release promises what was checked: a capability this domain cannot check is not released.'),
|
|
23
|
+
how: say('figure.gate.capability-unverified.how', { provider }, `Choose ${provider} in Settings > capability providers, or turn the capability off.`),
|
|
24
|
+
at: issue.name,
|
|
25
|
+
blocking: true
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
if (issue.code === 'CAPABILITY_NEEDS') {
|
|
29
|
+
const needs = (issue.reasons ?? []).map(r => (0, figure_model_1.capabilityText)(r, lang)).join(', ');
|
|
30
|
+
return {
|
|
31
|
+
code: 'capability-needs',
|
|
32
|
+
message: say('figure.gate.capability-needs.message', { name, needs }, `The capability '${name}' needs ${needs}.`),
|
|
33
|
+
how: say('figure.gate.capability-needs.how', { needs }, `Turn on ${needs} in the capability tab, or turn the capability off.`),
|
|
34
|
+
at: issue.name,
|
|
35
|
+
blocking: true
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
const reasons = (issue.reasons ?? []).map(r => (0, figure_model_1.capabilityText)(r, lang)).join(' ');
|
|
39
|
+
return {
|
|
40
|
+
code: 'capability-settings',
|
|
41
|
+
message: say('figure.gate.capability-settings.message', { name, reasons }, `The settings of '${name}' are not taken: ${reasons}`),
|
|
42
|
+
how: say('figure.gate.capability-settings.how', {}, 'Fix the settings in the capability tab.'),
|
|
43
|
+
at: issue.name,
|
|
44
|
+
blocking: true
|
|
45
|
+
};
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
/** The check as the release door runs it: the releasing domain's prepared descriptors, the reader's words. */
|
|
49
|
+
function capabilityReleaseCheck(input) {
|
|
50
|
+
const t = input.t;
|
|
51
|
+
const say = (key, params, english) => (t ? t(key, { ...params, defaultValue: english, interpolation: { escapeValue: false } }) : english);
|
|
52
|
+
/* The language of the reader's translations, for the module's own words. */
|
|
53
|
+
const lang = t ? t('figure.gate.capability-language', { defaultValue: 'en' }) : 'en';
|
|
54
|
+
return capabilityFindings(input.asset, (0, capability_module_store_js_1.preparedDescriptors)(input.domain?.id), say, lang);
|
|
55
|
+
}
|
|
56
|
+
(0, figure_format_js_1.registerFigureReleaseCheck)(capabilityReleaseCheck);
|
|
57
|
+
//# sourceMappingURL=capability-release-check.js.map
|
|
@@ -47,6 +47,10 @@ export interface FigureReleaseCheckInput {
|
|
|
47
47
|
type?: string;
|
|
48
48
|
category?: string | null;
|
|
49
49
|
t?: Translate;
|
|
50
|
+
/** The releasing domain: the capability providers it uses check the figure's consumer capabilities (ADR-0172 9). */
|
|
51
|
+
domain?: {
|
|
52
|
+
id: string;
|
|
53
|
+
};
|
|
50
54
|
}
|
|
51
55
|
/**
|
|
52
56
|
* A check an app adds to releasing (LV figure-server design G10): findings in the gate's own shape, blocking or said.
|
|
@@ -78,7 +82,11 @@ export declare function inspectSource(sourceJson?: string | null, releasedJson?:
|
|
|
78
82
|
/** The row's `type` -- the only place the name lives, since the asset carries none. */
|
|
79
83
|
rowType?: string,
|
|
80
84
|
/** The row's category, for an app's release checks (G10). */
|
|
81
|
-
rowCategory?: string | null
|
|
85
|
+
rowCategory?: string | null,
|
|
86
|
+
/** The releasing domain, for the capability check (ADR-0172 decision 9). */
|
|
87
|
+
domain?: {
|
|
88
|
+
id: string;
|
|
89
|
+
}): Inspection;
|
|
82
90
|
/**
|
|
83
91
|
* What a release records about itself (ADR-0104 decision 5): the format its asset is written in, what it measured
|
|
84
92
|
* (its cost at its largest, whether resizing is stretching), its plan from above, and what changed against the previous release -- the kernel's own comparison, kept as it was then so a consumer that pins releases
|
|
@@ -168,7 +168,9 @@ function inspectSource(sourceJson, releasedJson, t,
|
|
|
168
168
|
/** The row's `type` -- the only place the name lives, since the asset carries none. */
|
|
169
169
|
rowType,
|
|
170
170
|
/** The row's category, for an app's release checks (G10). */
|
|
171
|
-
rowCategory
|
|
171
|
+
rowCategory,
|
|
172
|
+
/** The releasing domain, for the capability check (ADR-0172 decision 9). */
|
|
173
|
+
domain) {
|
|
172
174
|
if (!sourceJson) {
|
|
173
175
|
return { blocked: true, findings: [{ code: 'no-source', message: '저장된 정본이 없다. 저장하면 발행할 수 있다', blocking: true }] };
|
|
174
176
|
}
|
|
@@ -217,7 +219,7 @@ rowCategory) {
|
|
|
217
219
|
findings.push(...itemFindings(asset, say));
|
|
218
220
|
if (releasedJson)
|
|
219
221
|
findings.push(...figureContractFindings(asset, releasedJson, say));
|
|
220
|
-
findings.push(...appFindings({ asset: asset, type: rowType, category: rowCategory, t }));
|
|
222
|
+
findings.push(...appFindings({ asset: asset, type: rowType, category: rowCategory, t, domain }));
|
|
221
223
|
return { blocked: findings.some(finding => finding.blocking), findings };
|
|
222
224
|
}
|
|
223
225
|
const PLANE_ENGLISH = {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { FigureAsset, FigureProposalFinding } from '@hatiolab/figure-model';
|
|
2
2
|
import { type ToolAccessCheck } from '@things-factory/ai-client-base';
|
|
3
|
-
import type
|
|
3
|
+
import { type ProposeFigureCapability, type ProposeFigureResult } from './figure-propose-steps.js';
|
|
4
|
+
import type { FigureCapabilityChoices } from '@hatiolab/figure-model';
|
|
4
5
|
/**
|
|
5
6
|
* On by FIGURE_BUILD=mcp-agent, or figure/build in the server's config -- a development server's own choice -- and only
|
|
6
7
|
* where Claude Code may answer at all: NODE_ENV development with the CLI logged in (claudeCodeAvailable). A production
|
|
@@ -52,6 +53,10 @@ export interface McpAgentInput {
|
|
|
52
53
|
onTool?: (tool: FigureMcpTool, count: number) => void;
|
|
53
54
|
/** The person who asked; absent, the agent has the kernel tools only. */
|
|
54
55
|
caller?: McpAgentCaller;
|
|
56
|
+
/** The product capabilities the domain uses (ADR-0172 amendment 1, decision 16), told with how to work. */
|
|
57
|
+
capabilities?: readonly ProposeFigureCapability[];
|
|
58
|
+
/** Their settings' choice lists (ADR-0172 amendment 2, decision 21). */
|
|
59
|
+
choices?: FigureCapabilityChoices;
|
|
55
60
|
}
|
|
56
61
|
export interface McpAgentResult extends ProposeFigureResult {
|
|
57
62
|
/** What is still off by measure, against the figure the request started from. */
|
|
@@ -23,6 +23,7 @@ const readline_1 = require("readline");
|
|
|
23
23
|
const figure_model_1 = require("@hatiolab/figure-model");
|
|
24
24
|
const ai_client_base_1 = require("@things-factory/ai-client-base");
|
|
25
25
|
const env_1 = require("@things-factory/env");
|
|
26
|
+
const figure_propose_steps_js_1 = require("./figure-propose-steps.js");
|
|
26
27
|
/**
|
|
27
28
|
* On by FIGURE_BUILD=mcp-agent, or figure/build in the server's config -- a development server's own choice -- and only
|
|
28
29
|
* where Claude Code may answer at all: NODE_ENV development with the CLI logged in (claudeCodeAvailable). A production
|
|
@@ -87,7 +88,7 @@ async function buildByMcpAgent(input) {
|
|
|
87
88
|
(0, fs_1.writeFileSync)((0, path_1.join)(work, 'meta.json'), JSON.stringify({ name: input.name ?? '' }));
|
|
88
89
|
if (input.base)
|
|
89
90
|
(0, fs_1.writeFileSync)((0, path_1.join)(work, 'base.json'), (0, figure_model_1.serializeFigureAsset)(input.base));
|
|
90
|
-
(0, fs_1.writeFileSync)((0, path_1.join)(work, 'system.txt'), MCP_AGENT_SYSTEM);
|
|
91
|
+
(0, fs_1.writeFileSync)((0, path_1.join)(work, 'system.txt'), [MCP_AGENT_SYSTEM, ...(0, figure_propose_steps_js_1.capabilitiesPrompt)(input.capabilities ?? [], input.choices)].join('\n'));
|
|
91
92
|
(0, fs_1.writeFileSync)((0, path_1.join)(work, 'mcp.json'), JSON.stringify({
|
|
92
93
|
mcpServers: {
|
|
93
94
|
figure: {
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.FigureMutation = void 0;
|
|
4
4
|
exports.assertNames = assertNames;
|
|
5
5
|
const tslib_1 = require("tslib");
|
|
6
|
+
const capability_module_store_js_1 = require("../capability-module/capability-module-store.js");
|
|
6
7
|
const typeorm_1 = require("typeorm");
|
|
7
8
|
const type_graphql_1 = require("type-graphql");
|
|
8
9
|
const figure_model_1 = require("@hatiolab/figure-model");
|
|
@@ -339,7 +340,8 @@ let FigureMutation = class FigureMutation {
|
|
|
339
340
|
.orderBy('history.version', 'DESC')
|
|
340
341
|
.limit(1)
|
|
341
342
|
.getOne();
|
|
342
|
-
|
|
343
|
+
await (0, capability_module_store_js_1.prepareCapabilityModules)({ getRepository: shell_1.getRepository }, context.state.domain);
|
|
344
|
+
const inspection = (0, figure_format_js_1.inspectSource)(figure.source, released?.source, context.t, figure.type, figure.category, context.state.domain);
|
|
343
345
|
if (inspection.blocked) {
|
|
344
346
|
const reasons = inspection.findings
|
|
345
347
|
.filter(finding => finding.blocking)
|
|
@@ -487,6 +489,9 @@ let FigureMutation = class FigureMutation {
|
|
|
487
489
|
prompt: request.prompt,
|
|
488
490
|
base: request.base ? (0, figure_model_1.parseFigureAsset)(request.base) : undefined,
|
|
489
491
|
name: request.name,
|
|
492
|
+
/* The product capabilities the domain uses (ADR-0172 amendment 1, decision 16). */
|
|
493
|
+
capabilities: [...(await (0, capability_module_store_js_1.prepareCapabilityModules)({ getRepository: shell_1.getRepository }, context.state.domain)).values()],
|
|
494
|
+
choices: await (0, capability_module_store_js_1.chosenCapabilityChoices)({ getRepository: shell_1.getRepository }, context.state.domain),
|
|
490
495
|
...(image ? { images: [image] } : {})
|
|
491
496
|
});
|
|
492
497
|
return {
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { figureCostOf, figureScoreOf } from '@hatiolab/figure-model';
|
|
2
|
-
import type { FigureAsset, FigureProposalFinding, FigureProposalStep } from '@hatiolab/figure-model';
|
|
2
|
+
import type { FigureAsset, FigureCapabilityChoices, FigureCapabilityDescriptor, FigureProposalFinding, FigureProposalStep } from '@hatiolab/figure-model';
|
|
3
3
|
import { type AIClient } from '@things-factory/ai-client-base';
|
|
4
4
|
import type { AIImageMediaType, AIMessage } from '@things-factory/ai-client-base';
|
|
5
5
|
import { type FigureRelationCheck, type FigureRelationVerdict } from './figure-relation-checks.js';
|
|
6
6
|
import { type FigureReadRequest, type FigureView } from './figure-context.js';
|
|
7
|
+
/** A capability a provider the domain uses gives (ADR-0172 amendment 1, decision 16), as the model is told it. */
|
|
8
|
+
export type ProposeFigureCapability = Pick<FigureCapabilityDescriptor, 'name' | 'label' | 'note' | 'needs' | 'data' | 'settings'>;
|
|
7
9
|
export interface ProposeFigureInput {
|
|
8
10
|
/** The domain's AI agent that answers (aiClientFor). */
|
|
9
11
|
client: AIClient;
|
|
@@ -13,6 +15,13 @@ export interface ProposeFigureInput {
|
|
|
13
15
|
base?: FigureAsset;
|
|
14
16
|
/** The name of a new figure. */
|
|
15
17
|
name?: string;
|
|
18
|
+
/**
|
|
19
|
+
* The consumer capabilities of the providers the domain uses (ADR-0172 amendment 1, decision 16): told beside the guide,
|
|
20
|
+
* so a plain request ("a rack") is made wearing the capability its thing has in this domain.
|
|
21
|
+
*/
|
|
22
|
+
capabilities?: readonly ProposeFigureCapability[];
|
|
23
|
+
/** Their settings' choice lists, by <capability>.<setting> (ADR-0172 amendment 2, decision 21). */
|
|
24
|
+
choices?: FigureCapabilityChoices;
|
|
16
25
|
/** Reference pictures, in the order the person gave them: proportions and masses, not surface detail. */
|
|
17
26
|
images?: {
|
|
18
27
|
data: Buffer;
|
|
@@ -39,6 +48,8 @@ export interface ProposeFigureInput {
|
|
|
39
48
|
read?: FigureReadRequest;
|
|
40
49
|
/** Handed the request's course once it ends, made or not: what was asked, answered, refused, observed and decided. */
|
|
41
50
|
trace?: (record: FigureAITrace) => void;
|
|
51
|
+
/** Told before each request to the model, for the person waiting: which stage, and which request it is. */
|
|
52
|
+
progress?: (stage: FigureProposeProgress) => void;
|
|
42
53
|
/**
|
|
43
54
|
* The relations the request asks for, written from it beforehand (figure-relation-checks.ts). When given, the figure is
|
|
44
55
|
* judged against them, and a relation it breaks is said back with the observations: what was asked, what it gives.
|
|
@@ -69,6 +80,15 @@ export interface FigureRefinement {
|
|
|
69
80
|
/** The request's relations the figure returned does not keep: broken, or not confirmed (said with what could not be judged) -- never counted as met. */
|
|
70
81
|
unresolved?: string[];
|
|
71
82
|
}
|
|
83
|
+
/**
|
|
84
|
+
* A stage of a request, said before the model is asked: draft (the first answer), retry (asked again after the kernel
|
|
85
|
+
* refused an answer or it was not JSON), revise (asked to look at what was measured), recover (a revision refused, asked
|
|
86
|
+
* again). call counts every request to the model, the one about to be sent included.
|
|
87
|
+
*/
|
|
88
|
+
export interface FigureProposeProgress {
|
|
89
|
+
stage: 'draft' | 'retry' | 'revise' | 'recover';
|
|
90
|
+
call: number;
|
|
91
|
+
}
|
|
72
92
|
/** One request's course, for finding where the guide or the observations fail the model. */
|
|
73
93
|
export interface FigureAITrace {
|
|
74
94
|
at: string;
|
|
@@ -165,7 +185,8 @@ export declare function proposalFailureForPerson(failure: ProposeFigureFailure,
|
|
|
165
185
|
* What the model is told before the task: the answer's form, then figure-model's own guide and grammar, both made from
|
|
166
186
|
* the kernel and held to it by its tests. Nothing here designs a thing for it.
|
|
167
187
|
*/
|
|
168
|
-
export declare function rulesPromptFigure(): string;
|
|
188
|
+
export declare function rulesPromptFigure(capabilities?: readonly ProposeFigureCapability[], choices?: FigureCapabilityChoices): string;
|
|
189
|
+
export declare function capabilitiesPrompt(capabilities: readonly ProposeFigureCapability[], choices?: FigureCapabilityChoices): string[];
|
|
169
190
|
/**
|
|
170
191
|
* The observations on a figure the kernel and the gate took, said to the model once: what was measured, how, and that
|
|
171
192
|
* the model judges them -- keep, or answer again -- and may say where the guide or these observations failed it.
|