apcore-js 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +11 -0
- package/.gitmessage +60 -0
- package/.pre-commit-config.yaml +28 -0
- package/CHANGELOG.md +47 -0
- package/CLAUDE.md +68 -0
- package/README.md +131 -0
- package/apcore-logo.svg +79 -0
- package/package.json +37 -0
- package/planning/acl-system/overview.md +54 -0
- package/planning/acl-system/plan.md +92 -0
- package/planning/acl-system/state.json +76 -0
- package/planning/acl-system/tasks/acl-core.md +226 -0
- package/planning/acl-system/tasks/acl-rule.md +92 -0
- package/planning/acl-system/tasks/conditional-rules.md +259 -0
- package/planning/acl-system/tasks/pattern-matching.md +152 -0
- package/planning/acl-system/tasks/yaml-loading.md +271 -0
- package/planning/core-executor/overview.md +53 -0
- package/planning/core-executor/plan.md +88 -0
- package/planning/core-executor/state.json +76 -0
- package/planning/core-executor/tasks/async-support.md +106 -0
- package/planning/core-executor/tasks/execution-pipeline.md +113 -0
- package/planning/core-executor/tasks/redaction.md +85 -0
- package/planning/core-executor/tasks/safety-checks.md +65 -0
- package/planning/core-executor/tasks/setup.md +75 -0
- package/planning/decorator-bindings/overview.md +62 -0
- package/planning/decorator-bindings/plan.md +104 -0
- package/planning/decorator-bindings/state.json +87 -0
- package/planning/decorator-bindings/tasks/binding-directory.md +79 -0
- package/planning/decorator-bindings/tasks/binding-loader.md +148 -0
- package/planning/decorator-bindings/tasks/explicit-schemas.md +85 -0
- package/planning/decorator-bindings/tasks/function-module.md +127 -0
- package/planning/decorator-bindings/tasks/module-factory.md +89 -0
- package/planning/decorator-bindings/tasks/schema-modes.md +142 -0
- package/planning/middleware-system/overview.md +48 -0
- package/planning/middleware-system/plan.md +102 -0
- package/planning/middleware-system/state.json +65 -0
- package/planning/middleware-system/tasks/adapters.md +170 -0
- package/planning/middleware-system/tasks/base.md +115 -0
- package/planning/middleware-system/tasks/logging-middleware.md +304 -0
- package/planning/middleware-system/tasks/manager.md +313 -0
- package/planning/observability/overview.md +53 -0
- package/planning/observability/plan.md +119 -0
- package/planning/observability/state.json +98 -0
- package/planning/observability/tasks/context-logger.md +201 -0
- package/planning/observability/tasks/exporters.md +121 -0
- package/planning/observability/tasks/metrics-collector.md +162 -0
- package/planning/observability/tasks/metrics-middleware.md +141 -0
- package/planning/observability/tasks/obs-logging-middleware.md +179 -0
- package/planning/observability/tasks/span-model.md +120 -0
- package/planning/observability/tasks/tracing-middleware.md +179 -0
- package/planning/overview.md +81 -0
- package/planning/registry-system/overview.md +57 -0
- package/planning/registry-system/plan.md +114 -0
- package/planning/registry-system/state.json +109 -0
- package/planning/registry-system/tasks/dependencies.md +157 -0
- package/planning/registry-system/tasks/entry-point.md +148 -0
- package/planning/registry-system/tasks/metadata.md +198 -0
- package/planning/registry-system/tasks/registry-core.md +323 -0
- package/planning/registry-system/tasks/scanner.md +172 -0
- package/planning/registry-system/tasks/schema-export.md +261 -0
- package/planning/registry-system/tasks/types.md +124 -0
- package/planning/registry-system/tasks/validation.md +177 -0
- package/planning/schema-system/overview.md +56 -0
- package/planning/schema-system/plan.md +121 -0
- package/planning/schema-system/state.json +98 -0
- package/planning/schema-system/tasks/exporter.md +153 -0
- package/planning/schema-system/tasks/loader.md +106 -0
- package/planning/schema-system/tasks/ref-resolver.md +133 -0
- package/planning/schema-system/tasks/strict-mode.md +140 -0
- package/planning/schema-system/tasks/typebox-generation.md +133 -0
- package/planning/schema-system/tasks/types-and-annotations.md +160 -0
- package/planning/schema-system/tasks/validator.md +149 -0
- package/src/acl.ts +188 -0
- package/src/bindings.ts +208 -0
- package/src/config.ts +24 -0
- package/src/context.ts +75 -0
- package/src/decorator.ts +110 -0
- package/src/errors.ts +369 -0
- package/src/executor.ts +348 -0
- package/src/index.ts +81 -0
- package/src/middleware/adapters.ts +54 -0
- package/src/middleware/base.ts +33 -0
- package/src/middleware/index.ts +6 -0
- package/src/middleware/logging.ts +103 -0
- package/src/middleware/manager.ts +105 -0
- package/src/module.ts +41 -0
- package/src/observability/context-logger.ts +201 -0
- package/src/observability/index.ts +4 -0
- package/src/observability/metrics.ts +212 -0
- package/src/observability/tracing.ts +187 -0
- package/src/registry/dependencies.ts +99 -0
- package/src/registry/entry-point.ts +64 -0
- package/src/registry/index.ts +8 -0
- package/src/registry/metadata.ts +111 -0
- package/src/registry/registry.ts +314 -0
- package/src/registry/scanner.ts +150 -0
- package/src/registry/schema-export.ts +177 -0
- package/src/registry/types.ts +32 -0
- package/src/registry/validation.ts +38 -0
- package/src/schema/annotations.ts +67 -0
- package/src/schema/exporter.ts +93 -0
- package/src/schema/index.ts +14 -0
- package/src/schema/loader.ts +270 -0
- package/src/schema/ref-resolver.ts +235 -0
- package/src/schema/strict.ts +128 -0
- package/src/schema/types.ts +73 -0
- package/src/schema/validator.ts +82 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/pattern.ts +30 -0
- package/tests/helpers.ts +30 -0
- package/tests/integration/test-acl-safety.test.ts +268 -0
- package/tests/integration/test-binding-executor.test.ts +194 -0
- package/tests/integration/test-e2e-flow.test.ts +117 -0
- package/tests/integration/test-error-propagation.test.ts +259 -0
- package/tests/integration/test-middleware-chain.test.ts +120 -0
- package/tests/integration/test-observability-integration.test.ts +438 -0
- package/tests/observability/test-context-logger.test.ts +123 -0
- package/tests/observability/test-metrics.test.ts +89 -0
- package/tests/observability/test-tracing.test.ts +131 -0
- package/tests/registry/test-dependencies.test.ts +70 -0
- package/tests/registry/test-entry-point.test.ts +133 -0
- package/tests/registry/test-metadata.test.ts +265 -0
- package/tests/registry/test-registry.test.ts +140 -0
- package/tests/registry/test-scanner.test.ts +257 -0
- package/tests/registry/test-schema-export.test.ts +224 -0
- package/tests/registry/test-validation.test.ts +75 -0
- package/tests/schema/test-loader.test.ts +97 -0
- package/tests/schema/test-ref-resolver.test.ts +105 -0
- package/tests/schema/test-strict.test.ts +139 -0
- package/tests/schema/test-validator.test.ts +64 -0
- package/tests/test-acl.test.ts +206 -0
- package/tests/test-bindings.test.ts +227 -0
- package/tests/test-config.test.ts +76 -0
- package/tests/test-context.test.ts +151 -0
- package/tests/test-decorator.test.ts +173 -0
- package/tests/test-errors.test.ts +204 -0
- package/tests/test-executor.test.ts +252 -0
- package/tests/test-middleware-manager.test.ts +185 -0
- package/tests/test-middleware.test.ts +86 -0
- package/tsconfig.build.json +8 -0
- package/tsconfig.json +20 -0
- package/vitest.config.ts +18 -0
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* $ref resolution for JSON Schema documents following Algorithm A05.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { readFileSync, existsSync } from 'node:fs';
|
|
6
|
+
import { resolve, dirname, join } from 'node:path';
|
|
7
|
+
import yaml from 'js-yaml';
|
|
8
|
+
import { SchemaCircularRefError, SchemaNotFoundError, SchemaParseError } from '../errors.js';
|
|
9
|
+
|
|
10
|
+
const INLINE_SENTINEL = '__inline__';
|
|
11
|
+
|
|
12
|
+
function deepCopy<T>(obj: T): T {
|
|
13
|
+
return JSON.parse(JSON.stringify(obj));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export class RefResolver {
|
|
17
|
+
private _schemasDir: string;
|
|
18
|
+
private _maxDepth: number;
|
|
19
|
+
private _fileCache: Map<string, Record<string, unknown>> = new Map();
|
|
20
|
+
|
|
21
|
+
constructor(schemasDir: string, maxDepth: number = 32) {
|
|
22
|
+
this._schemasDir = resolve(schemasDir);
|
|
23
|
+
this._maxDepth = maxDepth;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
resolve(schema: Record<string, unknown>, currentFile?: string | null): Record<string, unknown> {
|
|
27
|
+
const result = deepCopy(schema);
|
|
28
|
+
this._fileCache.set(INLINE_SENTINEL, result);
|
|
29
|
+
try {
|
|
30
|
+
this._resolveNode(result, currentFile ?? null, new Set(), 0);
|
|
31
|
+
} finally {
|
|
32
|
+
this._fileCache.delete(INLINE_SENTINEL);
|
|
33
|
+
}
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
resolveRef(
|
|
38
|
+
refString: string,
|
|
39
|
+
currentFile: string | null,
|
|
40
|
+
visitedRefs?: Set<string>,
|
|
41
|
+
depth: number = 0,
|
|
42
|
+
siblingKeys?: Record<string, unknown> | null,
|
|
43
|
+
): unknown {
|
|
44
|
+
const visited = visitedRefs ?? new Set<string>();
|
|
45
|
+
|
|
46
|
+
if (visited.has(refString)) {
|
|
47
|
+
throw new SchemaCircularRefError(refString);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (depth >= this._maxDepth) {
|
|
51
|
+
throw new SchemaCircularRefError(
|
|
52
|
+
`Maximum reference depth ${this._maxDepth} exceeded resolving: ${refString}`,
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
visited.add(refString);
|
|
57
|
+
|
|
58
|
+
const [filePath, jsonPointer] = this._parseRef(refString, currentFile);
|
|
59
|
+
const document = this._loadFile(filePath);
|
|
60
|
+
const target = this._resolveJsonPointer(document, jsonPointer, refString);
|
|
61
|
+
|
|
62
|
+
let result: unknown = deepCopy(target);
|
|
63
|
+
|
|
64
|
+
if (siblingKeys && typeof result === 'object' && result !== null && !Array.isArray(result)) {
|
|
65
|
+
Object.assign(result as Record<string, unknown>, siblingKeys);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const effectiveFile = filePath === INLINE_SENTINEL ? currentFile : filePath;
|
|
69
|
+
|
|
70
|
+
if (typeof result === 'object' && result !== null && !Array.isArray(result)) {
|
|
71
|
+
const resultObj = result as Record<string, unknown>;
|
|
72
|
+
if ('$ref' in resultObj) {
|
|
73
|
+
const nestedRef = resultObj['$ref'] as string;
|
|
74
|
+
delete resultObj['$ref'];
|
|
75
|
+
const nestedSiblings = Object.keys(resultObj).length > 0 ? { ...resultObj } : null;
|
|
76
|
+
result = this.resolveRef(nestedRef, effectiveFile, visited, depth + 1, nestedSiblings);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
this._resolveNode(result, effectiveFile, visited, depth + 1);
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private _resolveNode(
|
|
85
|
+
node: unknown,
|
|
86
|
+
currentFile: string | null,
|
|
87
|
+
visitedRefs: Set<string>,
|
|
88
|
+
depth: number,
|
|
89
|
+
): unknown {
|
|
90
|
+
if (typeof node === 'object' && node !== null && !Array.isArray(node)) {
|
|
91
|
+
const nodeObj = node as Record<string, unknown>;
|
|
92
|
+
if ('$ref' in nodeObj) {
|
|
93
|
+
const refString = nodeObj['$ref'] as string;
|
|
94
|
+
const siblingKeys: Record<string, unknown> = {};
|
|
95
|
+
for (const [k, v] of Object.entries(nodeObj)) {
|
|
96
|
+
if (k !== '$ref') siblingKeys[k] = v;
|
|
97
|
+
}
|
|
98
|
+
const resolved = this.resolveRef(
|
|
99
|
+
refString,
|
|
100
|
+
currentFile,
|
|
101
|
+
new Set(visitedRefs),
|
|
102
|
+
depth,
|
|
103
|
+
Object.keys(siblingKeys).length > 0 ? siblingKeys : null,
|
|
104
|
+
);
|
|
105
|
+
// Clear and replace
|
|
106
|
+
for (const key of Object.keys(nodeObj)) delete nodeObj[key];
|
|
107
|
+
if (typeof resolved === 'object' && resolved !== null && !Array.isArray(resolved)) {
|
|
108
|
+
Object.assign(nodeObj, resolved as Record<string, unknown>);
|
|
109
|
+
} else {
|
|
110
|
+
return resolved;
|
|
111
|
+
}
|
|
112
|
+
} else {
|
|
113
|
+
for (const key of Object.keys(nodeObj)) {
|
|
114
|
+
const result = this._resolveNode(nodeObj[key], currentFile, visitedRefs, depth);
|
|
115
|
+
if (result !== nodeObj[key]) {
|
|
116
|
+
nodeObj[key] = result;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
} else if (Array.isArray(node)) {
|
|
121
|
+
for (let i = 0; i < node.length; i++) {
|
|
122
|
+
const result = this._resolveNode(node[i], currentFile, visitedRefs, depth);
|
|
123
|
+
if (result !== node[i]) {
|
|
124
|
+
node[i] = result;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return node;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
private _parseRef(refString: string, currentFile: string | null): [string, string] {
|
|
132
|
+
if (refString.startsWith('#')) {
|
|
133
|
+
const pointer = refString.slice(1);
|
|
134
|
+
if (currentFile) return [currentFile, pointer];
|
|
135
|
+
return [INLINE_SENTINEL, pointer];
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (refString.startsWith('apcore://')) {
|
|
139
|
+
return this._convertCanonicalToPath(refString);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (refString.includes('#')) {
|
|
143
|
+
const [filePart, pointer] = refString.split('#', 2);
|
|
144
|
+
const base = currentFile ? dirname(currentFile) : this._schemasDir;
|
|
145
|
+
return [resolve(base, filePart), pointer];
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const base = currentFile ? dirname(currentFile) : this._schemasDir;
|
|
149
|
+
return [resolve(base, refString), ''];
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
private _convertCanonicalToPath(uri: string): [string, string] {
|
|
153
|
+
const remainder = uri.slice('apcore://'.length);
|
|
154
|
+
const parts = remainder.split('/');
|
|
155
|
+
const canonicalId = parts[0];
|
|
156
|
+
const pointerParts = parts.slice(1);
|
|
157
|
+
|
|
158
|
+
const fileRel = canonicalId.replace(/\./g, '/') + '.schema.yaml';
|
|
159
|
+
const filePath = resolve(this._schemasDir, fileRel);
|
|
160
|
+
|
|
161
|
+
const pointer = pointerParts.length > 0 ? '/' + pointerParts.join('/') : '';
|
|
162
|
+
return [filePath, pointer];
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
private _resolveJsonPointer(document: unknown, pointer: string, refString: string): unknown {
|
|
166
|
+
if (!pointer) return document;
|
|
167
|
+
|
|
168
|
+
let segments = pointer.split('/');
|
|
169
|
+
if (segments.length > 0 && segments[0] === '') {
|
|
170
|
+
segments = segments.slice(1);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
let current = document;
|
|
174
|
+
for (const rawSegment of segments) {
|
|
175
|
+
const segment = rawSegment.replace(/~1/g, '/').replace(/~0/g, '~');
|
|
176
|
+
if (typeof current === 'object' && current !== null && !Array.isArray(current)) {
|
|
177
|
+
const obj = current as Record<string, unknown>;
|
|
178
|
+
if (segment in obj) {
|
|
179
|
+
current = obj[segment];
|
|
180
|
+
} else {
|
|
181
|
+
throw new SchemaNotFoundError(`${refString} (segment '${segment}' not found)`);
|
|
182
|
+
}
|
|
183
|
+
} else {
|
|
184
|
+
throw new SchemaNotFoundError(`${refString} (segment '${segment}' not found)`);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return current;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
private _loadFile(filePath: string): Record<string, unknown> {
|
|
191
|
+
if (filePath === INLINE_SENTINEL) {
|
|
192
|
+
return this._fileCache.get(INLINE_SENTINEL) ?? {};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const resolved = resolve(filePath);
|
|
196
|
+
const cached = this._fileCache.get(resolved);
|
|
197
|
+
if (cached !== undefined) return cached;
|
|
198
|
+
|
|
199
|
+
if (!existsSync(resolved)) {
|
|
200
|
+
throw new SchemaNotFoundError(resolved);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const content = readFileSync(resolved, 'utf-8');
|
|
204
|
+
if (!content.trim()) {
|
|
205
|
+
this._fileCache.set(resolved, {});
|
|
206
|
+
return {};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
let parsed: unknown;
|
|
210
|
+
try {
|
|
211
|
+
parsed = yaml.load(content);
|
|
212
|
+
} catch (e) {
|
|
213
|
+
throw new SchemaParseError(`Invalid YAML in ${resolved}: ${e}`);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (parsed === null || parsed === undefined) {
|
|
217
|
+
this._fileCache.set(resolved, {});
|
|
218
|
+
return {};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
222
|
+
throw new SchemaParseError(
|
|
223
|
+
`Schema file ${resolved} must be a YAML mapping, got ${typeof parsed}`,
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const result = parsed as Record<string, unknown>;
|
|
228
|
+
this._fileCache.set(resolved, result);
|
|
229
|
+
return result;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
clearCache(): void {
|
|
233
|
+
this._fileCache.clear();
|
|
234
|
+
}
|
|
235
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Strict mode conversion for JSON Schemas (Algorithm A23).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
function deepCopy<T>(obj: T): T {
|
|
6
|
+
return JSON.parse(JSON.stringify(obj));
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function toStrictSchema(schema: Record<string, unknown>): Record<string, unknown> {
|
|
10
|
+
const result = deepCopy(schema);
|
|
11
|
+
stripExtensions(result);
|
|
12
|
+
convertToStrict(result);
|
|
13
|
+
return result;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function applyLlmDescriptions(node: unknown): void {
|
|
17
|
+
if (typeof node !== 'object' || node === null || Array.isArray(node)) return;
|
|
18
|
+
|
|
19
|
+
const obj = node as Record<string, unknown>;
|
|
20
|
+
if ('x-llm-description' in obj && 'description' in obj) {
|
|
21
|
+
obj['description'] = obj['x-llm-description'];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if ('properties' in obj && typeof obj['properties'] === 'object' && obj['properties'] !== null) {
|
|
25
|
+
for (const prop of Object.values(obj['properties'] as Record<string, unknown>)) {
|
|
26
|
+
applyLlmDescriptions(prop);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if ('items' in obj && typeof obj['items'] === 'object') {
|
|
30
|
+
applyLlmDescriptions(obj['items']);
|
|
31
|
+
}
|
|
32
|
+
for (const keyword of ['oneOf', 'anyOf', 'allOf']) {
|
|
33
|
+
if (keyword in obj && Array.isArray(obj[keyword])) {
|
|
34
|
+
for (const sub of obj[keyword] as unknown[]) {
|
|
35
|
+
applyLlmDescriptions(sub);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
for (const defsKey of ['definitions', '$defs']) {
|
|
40
|
+
if (defsKey in obj && typeof obj[defsKey] === 'object' && obj[defsKey] !== null) {
|
|
41
|
+
for (const defn of Object.values(obj[defsKey] as Record<string, unknown>)) {
|
|
42
|
+
applyLlmDescriptions(defn);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function stripExtensions(node: unknown): void {
|
|
49
|
+
if (typeof node !== 'object' || node === null || Array.isArray(node)) return;
|
|
50
|
+
|
|
51
|
+
const obj = node as Record<string, unknown>;
|
|
52
|
+
const keysToRemove = Object.keys(obj).filter(
|
|
53
|
+
(k) => (typeof k === 'string' && k.startsWith('x-')) || k === 'default',
|
|
54
|
+
);
|
|
55
|
+
for (const k of keysToRemove) {
|
|
56
|
+
delete obj[k];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
for (const value of Object.values(obj)) {
|
|
60
|
+
if (typeof value === 'object' && value !== null) {
|
|
61
|
+
if (Array.isArray(value)) {
|
|
62
|
+
for (const item of value) {
|
|
63
|
+
if (typeof item === 'object' && item !== null) {
|
|
64
|
+
stripExtensions(item);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
} else {
|
|
68
|
+
stripExtensions(value);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function convertToStrict(node: unknown): void {
|
|
75
|
+
if (typeof node !== 'object' || node === null || Array.isArray(node)) return;
|
|
76
|
+
|
|
77
|
+
const obj = node as Record<string, unknown>;
|
|
78
|
+
|
|
79
|
+
if (obj['type'] === 'object' && 'properties' in obj) {
|
|
80
|
+
obj['additionalProperties'] = false;
|
|
81
|
+
const existingRequired = new Set(
|
|
82
|
+
Array.isArray(obj['required']) ? (obj['required'] as string[]) : [],
|
|
83
|
+
);
|
|
84
|
+
const properties = obj['properties'] as Record<string, unknown>;
|
|
85
|
+
const allNames = Object.keys(properties);
|
|
86
|
+
const optionalNames = allNames.filter((n) => !existingRequired.has(n));
|
|
87
|
+
|
|
88
|
+
for (const name of optionalNames) {
|
|
89
|
+
const prop = properties[name] as Record<string, unknown>;
|
|
90
|
+
if ('type' in prop) {
|
|
91
|
+
if (typeof prop['type'] === 'string') {
|
|
92
|
+
prop['type'] = [prop['type'], 'null'];
|
|
93
|
+
} else if (Array.isArray(prop['type'])) {
|
|
94
|
+
if (!(prop['type'] as string[]).includes('null')) {
|
|
95
|
+
(prop['type'] as string[]).push('null');
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
} else {
|
|
99
|
+
properties[name] = { oneOf: [prop, { type: 'null' }] };
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
obj['required'] = [...allNames].sort();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if ('properties' in obj && typeof obj['properties'] === 'object' && obj['properties'] !== null) {
|
|
107
|
+
for (const prop of Object.values(obj['properties'] as Record<string, unknown>)) {
|
|
108
|
+
convertToStrict(prop);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if ('items' in obj && typeof obj['items'] === 'object') {
|
|
112
|
+
convertToStrict(obj['items']);
|
|
113
|
+
}
|
|
114
|
+
for (const keyword of ['oneOf', 'anyOf', 'allOf']) {
|
|
115
|
+
if (keyword in obj && Array.isArray(obj[keyword])) {
|
|
116
|
+
for (const sub of obj[keyword] as unknown[]) {
|
|
117
|
+
convertToStrict(sub);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
for (const defsKey of ['definitions', '$defs']) {
|
|
122
|
+
if (defsKey in obj && typeof obj[defsKey] === 'object' && obj[defsKey] !== null) {
|
|
123
|
+
for (const defn of Object.values(obj[defsKey] as Record<string, unknown>)) {
|
|
124
|
+
convertToStrict(defn);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Schema type definitions and data structures for the apcore schema system.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { TSchema } from '@sinclair/typebox';
|
|
6
|
+
import { SchemaValidationError } from '../errors.js';
|
|
7
|
+
|
|
8
|
+
export enum SchemaStrategy {
|
|
9
|
+
YamlFirst = 'yaml_first',
|
|
10
|
+
NativeFirst = 'native_first',
|
|
11
|
+
YamlOnly = 'yaml_only',
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export enum ExportProfile {
|
|
15
|
+
MCP = 'mcp',
|
|
16
|
+
OpenAI = 'openai',
|
|
17
|
+
Anthropic = 'anthropic',
|
|
18
|
+
Generic = 'generic',
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface SchemaDefinition {
|
|
22
|
+
moduleId: string;
|
|
23
|
+
description: string;
|
|
24
|
+
inputSchema: Record<string, unknown>;
|
|
25
|
+
outputSchema: Record<string, unknown>;
|
|
26
|
+
errorSchema?: Record<string, unknown> | null;
|
|
27
|
+
definitions: Record<string, unknown>;
|
|
28
|
+
version: string;
|
|
29
|
+
documentation?: string | null;
|
|
30
|
+
schemaUrl?: string | null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface ResolvedSchema {
|
|
34
|
+
jsonSchema: Record<string, unknown>;
|
|
35
|
+
schema: TSchema;
|
|
36
|
+
moduleId: string;
|
|
37
|
+
direction: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface SchemaValidationErrorDetail {
|
|
41
|
+
path: string;
|
|
42
|
+
message: string;
|
|
43
|
+
constraint?: string | null;
|
|
44
|
+
expected?: unknown;
|
|
45
|
+
actual?: unknown;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface SchemaValidationResult {
|
|
49
|
+
valid: boolean;
|
|
50
|
+
errors: SchemaValidationErrorDetail[];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function validationResultToError(result: SchemaValidationResult): SchemaValidationError {
|
|
54
|
+
if (result.valid) {
|
|
55
|
+
throw new Error('Cannot convert valid result to error');
|
|
56
|
+
}
|
|
57
|
+
const errorDicts = result.errors.map((e) => ({
|
|
58
|
+
path: e.path,
|
|
59
|
+
message: e.message,
|
|
60
|
+
constraint: e.constraint ?? null,
|
|
61
|
+
expected: e.expected ?? null,
|
|
62
|
+
actual: e.actual ?? null,
|
|
63
|
+
}));
|
|
64
|
+
return new SchemaValidationError('Schema validation failed', errorDicts);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface LLMExtensions {
|
|
68
|
+
llmDescription?: string | null;
|
|
69
|
+
examples?: unknown[] | null;
|
|
70
|
+
sensitive: boolean;
|
|
71
|
+
constraints?: string | null;
|
|
72
|
+
deprecated?: Record<string, unknown> | null;
|
|
73
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SchemaValidator — validates runtime data against TypeBox schemas.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { type TSchema } from '@sinclair/typebox';
|
|
6
|
+
import { Value, type ValueError } from '@sinclair/typebox/value';
|
|
7
|
+
import type { SchemaValidationErrorDetail, SchemaValidationResult } from './types.js';
|
|
8
|
+
import { validationResultToError } from './types.js';
|
|
9
|
+
|
|
10
|
+
export class SchemaValidator {
|
|
11
|
+
private _coerceTypes: boolean;
|
|
12
|
+
|
|
13
|
+
constructor(coerceTypes: boolean = true) {
|
|
14
|
+
this._coerceTypes = coerceTypes;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
validate(data: Record<string, unknown>, schema: TSchema): SchemaValidationResult {
|
|
18
|
+
if (this._coerceTypes) {
|
|
19
|
+
try {
|
|
20
|
+
Value.Decode(schema, data);
|
|
21
|
+
return { valid: true, errors: [] };
|
|
22
|
+
} catch {
|
|
23
|
+
return { valid: false, errors: this._collectErrors(schema, data) };
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (Value.Check(schema, data)) {
|
|
28
|
+
return { valid: true, errors: [] };
|
|
29
|
+
}
|
|
30
|
+
return { valid: false, errors: this._collectErrors(schema, data) };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
validateInput(data: Record<string, unknown>, schema: TSchema): Record<string, unknown> {
|
|
34
|
+
return this._validateAndReturn(data, schema);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
validateOutput(data: Record<string, unknown>, schema: TSchema): Record<string, unknown> {
|
|
38
|
+
return this._validateAndReturn(data, schema);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
private _validateAndReturn(data: Record<string, unknown>, schema: TSchema): Record<string, unknown> {
|
|
42
|
+
if (this._coerceTypes) {
|
|
43
|
+
try {
|
|
44
|
+
return Value.Decode(schema, data) as Record<string, unknown>;
|
|
45
|
+
} catch {
|
|
46
|
+
const result: SchemaValidationResult = {
|
|
47
|
+
valid: false,
|
|
48
|
+
errors: this._collectErrors(schema, data),
|
|
49
|
+
};
|
|
50
|
+
throw validationResultToError(result);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (Value.Check(schema, data)) {
|
|
55
|
+
return data;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const result: SchemaValidationResult = {
|
|
59
|
+
valid: false,
|
|
60
|
+
errors: this._collectErrors(schema, data),
|
|
61
|
+
};
|
|
62
|
+
throw validationResultToError(result);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private _collectErrors(schema: TSchema, data: unknown): SchemaValidationErrorDetail[] {
|
|
66
|
+
const errors: SchemaValidationErrorDetail[] = [];
|
|
67
|
+
for (const error of Value.Errors(schema, data)) {
|
|
68
|
+
errors.push(this._typeboxErrorToDetail(error));
|
|
69
|
+
}
|
|
70
|
+
return errors;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
private _typeboxErrorToDetail(error: ValueError): SchemaValidationErrorDetail {
|
|
74
|
+
return {
|
|
75
|
+
path: error.path || '/',
|
|
76
|
+
message: error.message,
|
|
77
|
+
constraint: String(error.type),
|
|
78
|
+
expected: error.schema,
|
|
79
|
+
actual: error.value,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { matchPattern } from './pattern.js';
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wildcard pattern matching for module IDs (Algorithm A08).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export function matchPattern(pattern: string, moduleId: string): boolean {
|
|
6
|
+
if (pattern === '*') return true;
|
|
7
|
+
if (!pattern.includes('*')) return pattern === moduleId;
|
|
8
|
+
|
|
9
|
+
const segments = pattern.split('*');
|
|
10
|
+
let pos = 0;
|
|
11
|
+
|
|
12
|
+
if (!pattern.startsWith('*')) {
|
|
13
|
+
if (!moduleId.startsWith(segments[0])) return false;
|
|
14
|
+
pos = segments[0].length;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
for (let i = 1; i < segments.length; i++) {
|
|
18
|
+
const segment = segments[i];
|
|
19
|
+
if (!segment) continue;
|
|
20
|
+
const idx = moduleId.indexOf(segment, pos);
|
|
21
|
+
if (idx === -1) return false;
|
|
22
|
+
pos = idx + segment.length;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (!pattern.endsWith('*')) {
|
|
26
|
+
if (!moduleId.endsWith(segments[segments.length - 1])) return false;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return true;
|
|
30
|
+
}
|
package/tests/helpers.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared test fixtures and helpers.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { Type } from '@sinclair/typebox';
|
|
6
|
+
import { Context, createIdentity } from '../src/context.js';
|
|
7
|
+
import { FunctionModule } from '../src/decorator.js';
|
|
8
|
+
import { Registry } from '../src/registry/registry.js';
|
|
9
|
+
|
|
10
|
+
export function createTestModule(options?: {
|
|
11
|
+
moduleId?: string;
|
|
12
|
+
description?: string;
|
|
13
|
+
execute?: (inputs: Record<string, unknown>, context: Context) => Record<string, unknown>;
|
|
14
|
+
}): FunctionModule {
|
|
15
|
+
return new FunctionModule({
|
|
16
|
+
execute: options?.execute ?? ((inputs) => ({ echo: inputs['name'] ?? 'world' })),
|
|
17
|
+
moduleId: options?.moduleId ?? 'test.module',
|
|
18
|
+
inputSchema: Type.Object({ name: Type.Optional(Type.String()) }),
|
|
19
|
+
outputSchema: Type.Object({ echo: Type.String() }),
|
|
20
|
+
description: options?.description ?? 'A test module',
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function createTestRegistry(): Registry {
|
|
25
|
+
return new Registry();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function createTestContext(executor?: unknown): Context {
|
|
29
|
+
return Context.create(executor, createIdentity('test-user'));
|
|
30
|
+
}
|