@mfdaves/okf-odoo 0.0.0-stage → 0.1.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/CHANGELOG.md +44 -0
- package/LICENSE +21 -0
- package/README.md +368 -2
- package/okf/okf-odoo/index.md +11 -0
- package/okf/okf-odoo/overview/okf-odoo.md +51 -0
- package/okf/okf-odoo/reference/access-rights.md +33 -0
- package/okf/okf-odoo/reference/configuration.md +41 -0
- package/okf/okf-odoo/reference/data-tools.md +38 -0
- package/okf/okf-odoo/reference/output.md +35 -0
- package/package.json +54 -4
- package/src/catalog.js +704 -0
- package/src/data-api.js +254 -0
- package/src/index.js +9 -0
- package/src/markdown.js +82 -0
- package/src/odoo.js +340 -0
- package/src/producer.js +280 -0
- package/src/render.js +638 -0
- package/src/schema.js +66 -0
- package/src/tool-pack.js +49 -0
- package/src/tools.js +520 -0
package/src/producer.js
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// Producer API v1 descriptor for okf-mcp and other hosts: validateConfig()
|
|
4
|
+
// checks and normalises the trusted project configuration, generate() reads
|
|
5
|
+
// the catalog and returns candidate files without writing anything.
|
|
6
|
+
|
|
7
|
+
const packageJson = require("../package.json");
|
|
8
|
+
const { DEFAULT_SECTIONS, MODEL_SECTIONS, SECTIONS, readCatalog } = require("./catalog");
|
|
9
|
+
const { createOdooClient, normalizeBaseUrl } = require("./odoo");
|
|
10
|
+
const { RELATION_TYPES, buildBundle, normalizeGeneratedAt } = require("./render");
|
|
11
|
+
|
|
12
|
+
const CONFIG_KEYS = new Set([
|
|
13
|
+
"url",
|
|
14
|
+
"api",
|
|
15
|
+
"database",
|
|
16
|
+
"login",
|
|
17
|
+
"apiKeyEnv",
|
|
18
|
+
"source",
|
|
19
|
+
"lang",
|
|
20
|
+
"sections",
|
|
21
|
+
"modules",
|
|
22
|
+
"otherModules",
|
|
23
|
+
]);
|
|
24
|
+
const IDENTIFIER = /^[A-Za-z0-9_.-]+$/;
|
|
25
|
+
const ENV_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
26
|
+
const LANG = /^[a-z]{2,3}(_[A-Za-z0-9]{2,4})?(@[a-z]+)?$/;
|
|
27
|
+
const MODULE_PATTERN = /^[a-z0-9_*]+$/;
|
|
28
|
+
const OTHER_MODULES = new Set(["summary", "none"]);
|
|
29
|
+
const APIS = new Set(["json2", "jsonrpc"]);
|
|
30
|
+
|
|
31
|
+
function isPlainObject(value) {
|
|
32
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
33
|
+
const prototype = Object.getPrototypeOf(value);
|
|
34
|
+
return prototype === Object.prototype || prototype === null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function validateConfig(value = {}) {
|
|
38
|
+
const diagnostics = [];
|
|
39
|
+
const report = (code, field, message) => diagnostics.push({ code, field, message });
|
|
40
|
+
const input = isPlainObject(value) ? value : {};
|
|
41
|
+
if (!isPlainObject(value))
|
|
42
|
+
report("invalid_config", "config", "Producer config must be an object.");
|
|
43
|
+
for (const key of Object.keys(input)
|
|
44
|
+
.filter((name) => !CONFIG_KEYS.has(name))
|
|
45
|
+
.sort()) {
|
|
46
|
+
report("unknown_config_key", key, `Unsupported Odoo producer config key: ${key}.`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
let url = null;
|
|
50
|
+
if (input.url === undefined) {
|
|
51
|
+
report("missing_url", "url", "url is required.");
|
|
52
|
+
} else {
|
|
53
|
+
try {
|
|
54
|
+
url = normalizeBaseUrl(input.url);
|
|
55
|
+
} catch {
|
|
56
|
+
report(
|
|
57
|
+
"invalid_url",
|
|
58
|
+
"url",
|
|
59
|
+
"url must be an https URL (http only for a loopback host) without credentials, query, or fragment.",
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// The protocol is configured, never guessed: JSON-2 for Odoo 19 and later,
|
|
65
|
+
// JSON-RPC for Odoo 18, which also needs the database and the key's login.
|
|
66
|
+
const api = input.api === undefined ? "json2" : input.api;
|
|
67
|
+
if (!APIS.has(api)) report("invalid_api", "api", "api must be json2 or jsonrpc.");
|
|
68
|
+
|
|
69
|
+
const database = input.database === undefined ? null : input.database;
|
|
70
|
+
if (database !== null && (typeof database !== "string" || !IDENTIFIER.test(database))) {
|
|
71
|
+
report("invalid_database", "database", "database must be an Odoo database name.");
|
|
72
|
+
} else if (database === null && api === "jsonrpc") {
|
|
73
|
+
report("missing_database", "database", "database is required with api jsonrpc.");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const login = input.login === undefined ? null : input.login;
|
|
77
|
+
if (login !== null && (typeof login !== "string" || !login.trim() || /\p{Cc}/u.test(login))) {
|
|
78
|
+
report("invalid_login", "login", "login must be the login of the API key's user.");
|
|
79
|
+
} else if (login === null && api === "jsonrpc") {
|
|
80
|
+
report("missing_login", "login", "login is required with api jsonrpc.");
|
|
81
|
+
} else if (login !== null && api !== "jsonrpc") {
|
|
82
|
+
report("login_requires_jsonrpc", "login", "login is used only with api jsonrpc.");
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Required: a host such as okf-mcp hands over only the secrets whose
|
|
86
|
+
// variable names appear in the configuration it was given.
|
|
87
|
+
const apiKeyEnv = input.apiKeyEnv === undefined ? null : input.apiKeyEnv;
|
|
88
|
+
if (apiKeyEnv === null) {
|
|
89
|
+
report("missing_api_key_env", "apiKeyEnv", "apiKeyEnv is required.");
|
|
90
|
+
} else if (typeof apiKeyEnv !== "string" || !ENV_NAME.test(apiKeyEnv)) {
|
|
91
|
+
report("invalid_api_key_env", "apiKeyEnv", "apiKeyEnv must be an environment-variable name.");
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const source = input.source === undefined ? "odoo" : input.source;
|
|
95
|
+
if (typeof source !== "string" || !IDENTIFIER.test(source)) {
|
|
96
|
+
report(
|
|
97
|
+
"invalid_source",
|
|
98
|
+
"source",
|
|
99
|
+
"source may contain only letters, numbers, dots, underscores, and hyphens.",
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const lang = input.lang === undefined ? "en_US" : input.lang;
|
|
104
|
+
if (typeof lang !== "string" || !LANG.test(lang)) {
|
|
105
|
+
report("invalid_lang", "lang", "lang must be an Odoo language code such as en_US.");
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
let sections = [...DEFAULT_SECTIONS];
|
|
109
|
+
if (input.sections !== undefined) {
|
|
110
|
+
if (
|
|
111
|
+
!Array.isArray(input.sections) ||
|
|
112
|
+
input.sections.length === 0 ||
|
|
113
|
+
input.sections.some((section) => !SECTIONS.includes(section))
|
|
114
|
+
) {
|
|
115
|
+
report("invalid_sections", "sections", `sections must list some of: ${SECTIONS.join(", ")}.`);
|
|
116
|
+
} else {
|
|
117
|
+
sections = SECTIONS.filter((section) => input.sections.includes(section));
|
|
118
|
+
const orphan = MODEL_SECTIONS.find(
|
|
119
|
+
(section) => sections.includes(section) && !sections.includes("models"),
|
|
120
|
+
);
|
|
121
|
+
if (orphan) {
|
|
122
|
+
report(
|
|
123
|
+
"section_requires_models",
|
|
124
|
+
"sections",
|
|
125
|
+
`The ${orphan} section is rendered on model concepts and requires the models section.`,
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const modules = { include: [], exclude: [] };
|
|
132
|
+
if (input.modules !== undefined) {
|
|
133
|
+
if (!isPlainObject(input.modules)) {
|
|
134
|
+
report("invalid_modules", "modules", "modules must be a mapping with include and exclude.");
|
|
135
|
+
} else {
|
|
136
|
+
for (const key of Object.keys(input.modules).sort()) {
|
|
137
|
+
if (key !== "include" && key !== "exclude") {
|
|
138
|
+
report("unknown_config_key", `modules.${key}`, `Unsupported modules key: ${key}.`);
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
const patterns = input.modules[key];
|
|
142
|
+
if (
|
|
143
|
+
!Array.isArray(patterns) ||
|
|
144
|
+
patterns.some((pattern) => typeof pattern !== "string" || !MODULE_PATTERN.test(pattern))
|
|
145
|
+
) {
|
|
146
|
+
report(
|
|
147
|
+
"invalid_module_patterns",
|
|
148
|
+
`modules.${key}`,
|
|
149
|
+
`modules.${key} must list module names; * matches any characters.`,
|
|
150
|
+
);
|
|
151
|
+
} else {
|
|
152
|
+
modules[key] = [...new Set(patterns)].sort();
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const otherModules = input.otherModules === undefined ? "summary" : input.otherModules;
|
|
159
|
+
if (!OTHER_MODULES.has(otherModules)) {
|
|
160
|
+
report("invalid_other_modules", "otherModules", "otherModules must be summary or none.");
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return {
|
|
164
|
+
valid: diagnostics.length === 0,
|
|
165
|
+
config: {
|
|
166
|
+
url,
|
|
167
|
+
api,
|
|
168
|
+
database,
|
|
169
|
+
login,
|
|
170
|
+
apiKeyEnv,
|
|
171
|
+
source,
|
|
172
|
+
lang,
|
|
173
|
+
sections,
|
|
174
|
+
modules,
|
|
175
|
+
otherModules,
|
|
176
|
+
},
|
|
177
|
+
diagnostics,
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function requireValidConfig(value) {
|
|
182
|
+
const validation = validateConfig(value);
|
|
183
|
+
if (!validation.valid) {
|
|
184
|
+
const error = new Error(validation.diagnostics.map((entry) => entry.message).join(" "));
|
|
185
|
+
error.code = "invalid_config";
|
|
186
|
+
error.diagnostics = validation.diagnostics;
|
|
187
|
+
throw error;
|
|
188
|
+
}
|
|
189
|
+
return validation.config;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function throwIfAborted(signal) {
|
|
193
|
+
if (signal?.aborted) {
|
|
194
|
+
throw signal.reason instanceof Error
|
|
195
|
+
? signal.reason
|
|
196
|
+
: new Error("Odoo production was aborted.");
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function summarize(catalog, files) {
|
|
201
|
+
const models = catalog.models;
|
|
202
|
+
const count = (list) => models.reduce((total, model) => total + model[list].length, 0);
|
|
203
|
+
return {
|
|
204
|
+
apps: catalog.apps.length,
|
|
205
|
+
accessEntries: count("access"),
|
|
206
|
+
fields: count("fields"),
|
|
207
|
+
files: files.size,
|
|
208
|
+
groups: catalog.groups.length,
|
|
209
|
+
models: models.length,
|
|
210
|
+
modules: catalog.modules.length,
|
|
211
|
+
reports: count("reports"),
|
|
212
|
+
scheduledJobs: count("crons"),
|
|
213
|
+
serverActions: count("serverActions"),
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
async function generateOdoo(context = {}, dependencies = {}) {
|
|
218
|
+
const config = requireValidConfig(context.config || {});
|
|
219
|
+
const bundle = String(context.bundleId || "").trim();
|
|
220
|
+
if (!IDENTIFIER.test(bundle)) {
|
|
221
|
+
throw new Error("bundleId may contain only letters, numbers, dots, underscores, and hyphens.");
|
|
222
|
+
}
|
|
223
|
+
throwIfAborted(context.signal);
|
|
224
|
+
if (typeof context.getSecret !== "function") {
|
|
225
|
+
throw new Error("Odoo production requires getSecret(name).");
|
|
226
|
+
}
|
|
227
|
+
const apiKey = await context.getSecret(config.apiKeyEnv);
|
|
228
|
+
if (typeof apiKey !== "string" || !apiKey) {
|
|
229
|
+
throw new Error(`API key environment variable ${config.apiKeyEnv} is empty.`);
|
|
230
|
+
}
|
|
231
|
+
const generatedAt = normalizeGeneratedAt(
|
|
232
|
+
context.generatedAt === undefined ? new Date() : context.generatedAt,
|
|
233
|
+
);
|
|
234
|
+
const createClient = dependencies.createClient || createOdooClient;
|
|
235
|
+
const client = createClient({
|
|
236
|
+
api: config.api,
|
|
237
|
+
url: config.url,
|
|
238
|
+
database: config.database,
|
|
239
|
+
login: config.login,
|
|
240
|
+
apiKey,
|
|
241
|
+
signal: context.signal,
|
|
242
|
+
});
|
|
243
|
+
const catalog = await readCatalog(client, {
|
|
244
|
+
sections: config.sections,
|
|
245
|
+
modules: config.modules,
|
|
246
|
+
otherModules: config.otherModules,
|
|
247
|
+
lang: config.lang,
|
|
248
|
+
});
|
|
249
|
+
throwIfAborted(context.signal);
|
|
250
|
+
const files = buildBundle(catalog, {
|
|
251
|
+
source: config.source,
|
|
252
|
+
bundle,
|
|
253
|
+
generatedAt,
|
|
254
|
+
producerVersion: packageJson.version,
|
|
255
|
+
});
|
|
256
|
+
return {
|
|
257
|
+
files: [...files.entries()]
|
|
258
|
+
.sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0))
|
|
259
|
+
.map(([path, content]) => ({ path, content })),
|
|
260
|
+
summary: summarize(catalog, files),
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const okfProducer = Object.freeze({
|
|
265
|
+
apiVersion: "1",
|
|
266
|
+
okfVersion: "0.2",
|
|
267
|
+
id: "odoo",
|
|
268
|
+
name: "okf-odoo",
|
|
269
|
+
version: packageJson.version,
|
|
270
|
+
relationTypes: RELATION_TYPES,
|
|
271
|
+
validateConfig,
|
|
272
|
+
generate: generateOdoo,
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
module.exports = {
|
|
276
|
+
generateOdoo,
|
|
277
|
+
odooProducer: okfProducer,
|
|
278
|
+
okfProducer,
|
|
279
|
+
validateConfig,
|
|
280
|
+
};
|