apcore-js 0.4.0 → 0.6.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.
Files changed (306) hide show
  1. package/dist/acl.d.ts +27 -0
  2. package/dist/acl.d.ts.map +1 -0
  3. package/dist/acl.js +175 -0
  4. package/dist/acl.js.map +1 -0
  5. package/dist/async-task.d.ts +90 -0
  6. package/dist/async-task.d.ts.map +1 -0
  7. package/dist/async-task.js +215 -0
  8. package/dist/async-task.js.map +1 -0
  9. package/dist/bindings.d.ts +12 -0
  10. package/dist/bindings.d.ts.map +1 -0
  11. package/dist/bindings.js +185 -0
  12. package/dist/bindings.js.map +1 -0
  13. package/dist/cancel.d.ts +14 -0
  14. package/dist/cancel.d.ts.map +1 -0
  15. package/dist/cancel.js +27 -0
  16. package/dist/cancel.js.map +1 -0
  17. package/dist/config.d.ts +9 -0
  18. package/dist/config.d.ts.map +1 -0
  19. package/dist/config.js +23 -0
  20. package/dist/config.js.map +1 -0
  21. package/dist/context.d.ts +50 -0
  22. package/dist/context.d.ts.map +1 -0
  23. package/dist/context.js +87 -0
  24. package/dist/context.js.map +1 -0
  25. package/dist/decorator.d.ts +57 -0
  26. package/dist/decorator.d.ts.map +1 -0
  27. package/dist/decorator.js +74 -0
  28. package/dist/decorator.js.map +1 -0
  29. package/dist/errors.d.ts +215 -0
  30. package/dist/errors.d.ts.map +1 -0
  31. package/dist/errors.js +246 -0
  32. package/dist/errors.js.map +1 -0
  33. package/dist/executor.d.ts +67 -0
  34. package/dist/executor.d.ts.map +1 -0
  35. package/dist/executor.js +372 -0
  36. package/dist/executor.js.map +1 -0
  37. package/dist/extensions.d.ts +58 -0
  38. package/dist/extensions.d.ts.map +1 -0
  39. package/dist/extensions.js +220 -0
  40. package/dist/extensions.js.map +1 -0
  41. package/{src/index.ts → dist/index.d.ts} +14 -59
  42. package/dist/index.d.ts.map +1 -0
  43. package/dist/index.js +43 -0
  44. package/dist/index.js.map +1 -0
  45. package/dist/middleware/adapters.d.ts +18 -0
  46. package/dist/middleware/adapters.d.ts.map +1 -0
  47. package/dist/middleware/adapters.js +25 -0
  48. package/dist/middleware/adapters.js.map +1 -0
  49. package/dist/middleware/base.d.ts +10 -0
  50. package/dist/middleware/base.d.ts.map +1 -0
  51. package/dist/middleware/base.js +15 -0
  52. package/dist/middleware/base.js.map +1 -0
  53. package/{src/middleware/index.ts → dist/middleware/index.d.ts} +1 -0
  54. package/dist/middleware/index.d.ts.map +1 -0
  55. package/dist/middleware/index.js +5 -0
  56. package/dist/middleware/index.js.map +1 -0
  57. package/dist/middleware/logging.d.ts +25 -0
  58. package/dist/middleware/logging.d.ts.map +1 -0
  59. package/dist/middleware/logging.js +64 -0
  60. package/dist/middleware/logging.js.map +1 -0
  61. package/dist/middleware/manager.d.ts +21 -0
  62. package/dist/middleware/manager.d.ts.map +1 -0
  63. package/dist/middleware/manager.js +77 -0
  64. package/dist/middleware/manager.js.map +1 -0
  65. package/dist/module.d.ts +31 -0
  66. package/dist/module.d.ts.map +1 -0
  67. package/dist/module.js +12 -0
  68. package/dist/module.js.map +1 -0
  69. package/dist/observability/context-logger.d.ts +54 -0
  70. package/dist/observability/context-logger.d.ts.map +1 -0
  71. package/dist/observability/context-logger.js +151 -0
  72. package/dist/observability/context-logger.js.map +1 -0
  73. package/{src/observability/index.ts → dist/observability/index.d.ts} +1 -0
  74. package/dist/observability/index.d.ts.map +1 -0
  75. package/dist/observability/index.js +4 -0
  76. package/dist/observability/index.js.map +1 -0
  77. package/dist/observability/metrics.d.ts +30 -0
  78. package/dist/observability/metrics.d.ts.map +1 -0
  79. package/dist/observability/metrics.js +177 -0
  80. package/dist/observability/metrics.js.map +1 -0
  81. package/dist/observability/tracing.d.ts +62 -0
  82. package/dist/observability/tracing.d.ts.map +1 -0
  83. package/dist/observability/tracing.js +184 -0
  84. package/dist/observability/tracing.js.map +1 -0
  85. package/dist/registry/dependencies.d.ts +6 -0
  86. package/dist/registry/dependencies.d.ts.map +1 -0
  87. package/dist/registry/dependencies.js +83 -0
  88. package/dist/registry/dependencies.js.map +1 -0
  89. package/dist/registry/entry-point.d.ts +6 -0
  90. package/dist/registry/entry-point.d.ts.map +1 -0
  91. package/dist/registry/entry-point.js +55 -0
  92. package/dist/registry/entry-point.js.map +1 -0
  93. package/{src/registry/index.ts → dist/registry/index.d.ts} +2 -0
  94. package/dist/registry/index.d.ts.map +1 -0
  95. package/dist/registry/index.js +8 -0
  96. package/dist/registry/index.js.map +1 -0
  97. package/dist/registry/metadata.d.ts +9 -0
  98. package/dist/registry/metadata.d.ts.map +1 -0
  99. package/dist/registry/metadata.js +105 -0
  100. package/dist/registry/metadata.js.map +1 -0
  101. package/dist/registry/registry.d.ts +102 -0
  102. package/dist/registry/registry.d.ts.map +1 -0
  103. package/dist/registry/registry.js +534 -0
  104. package/dist/registry/registry.js.map +1 -0
  105. package/dist/registry/scanner.d.ts +7 -0
  106. package/dist/registry/scanner.d.ts.map +1 -0
  107. package/dist/registry/scanner.js +164 -0
  108. package/dist/registry/scanner.js.map +1 -0
  109. package/dist/registry/schema-export.d.ts +9 -0
  110. package/dist/registry/schema-export.d.ts.map +1 -0
  111. package/dist/registry/schema-export.js +132 -0
  112. package/dist/registry/schema-export.js.map +1 -0
  113. package/dist/registry/types.d.ts +29 -0
  114. package/dist/registry/types.d.ts.map +1 -0
  115. package/dist/registry/types.js +5 -0
  116. package/dist/registry/types.js.map +1 -0
  117. package/dist/registry/validation.d.ts +9 -0
  118. package/dist/registry/validation.d.ts.map +1 -0
  119. package/dist/registry/validation.js +33 -0
  120. package/dist/registry/validation.js.map +1 -0
  121. package/dist/schema/annotations.d.ts +8 -0
  122. package/dist/schema/annotations.d.ts.map +1 -0
  123. package/dist/schema/annotations.js +52 -0
  124. package/dist/schema/annotations.js.map +1 -0
  125. package/dist/schema/exporter.d.ts +13 -0
  126. package/dist/schema/exporter.d.ts.map +1 -0
  127. package/dist/schema/exporter.js +71 -0
  128. package/dist/schema/exporter.js.map +1 -0
  129. package/dist/schema/index.d.ts +9 -0
  130. package/dist/schema/index.d.ts.map +1 -0
  131. package/{src/schema/index.ts → dist/schema/index.js} +1 -7
  132. package/dist/schema/index.js.map +1 -0
  133. package/dist/schema/loader.d.ts +30 -0
  134. package/dist/schema/loader.d.ts.map +1 -0
  135. package/dist/schema/loader.js +260 -0
  136. package/dist/schema/loader.js.map +1 -0
  137. package/dist/schema/ref-resolver.d.ts +19 -0
  138. package/dist/schema/ref-resolver.d.ts.map +1 -0
  139. package/dist/schema/ref-resolver.js +212 -0
  140. package/dist/schema/ref-resolver.js.map +1 -0
  141. package/dist/schema/strict.d.ts +7 -0
  142. package/dist/schema/strict.d.ts.map +1 -0
  143. package/dist/schema/strict.js +127 -0
  144. package/dist/schema/strict.js.map +1 -0
  145. package/dist/schema/types.d.ts +53 -0
  146. package/dist/schema/types.d.ts.map +1 -0
  147. package/dist/schema/types.js +31 -0
  148. package/dist/schema/types.js.map +1 -0
  149. package/dist/schema/validator.d.ts +16 -0
  150. package/dist/schema/validator.d.ts.map +1 -0
  151. package/dist/schema/validator.js +71 -0
  152. package/dist/schema/validator.js.map +1 -0
  153. package/dist/trace-context.d.ts +35 -0
  154. package/dist/trace-context.d.ts.map +1 -0
  155. package/dist/trace-context.js +86 -0
  156. package/dist/trace-context.js.map +1 -0
  157. package/dist/utils/index.d.ts +11 -0
  158. package/dist/utils/index.d.ts.map +1 -0
  159. package/dist/utils/index.js +32 -0
  160. package/dist/utils/index.js.map +1 -0
  161. package/dist/utils/pattern.d.ts +5 -0
  162. package/dist/utils/pattern.d.ts.map +1 -0
  163. package/dist/utils/pattern.js +31 -0
  164. package/dist/utils/pattern.js.map +1 -0
  165. package/package.json +8 -2
  166. package/.claude/settings.local.json +0 -12
  167. package/.github/workflows/ci.yml +0 -39
  168. package/.gitmessage +0 -60
  169. package/.pre-commit-config.yaml +0 -28
  170. package/CHANGELOG.md +0 -183
  171. package/CLAUDE.md +0 -68
  172. package/apcore-logo.svg +0 -79
  173. package/planning/acl-system/overview.md +0 -54
  174. package/planning/acl-system/plan.md +0 -92
  175. package/planning/acl-system/state.json +0 -76
  176. package/planning/acl-system/tasks/acl-core.md +0 -226
  177. package/planning/acl-system/tasks/acl-rule.md +0 -92
  178. package/planning/acl-system/tasks/conditional-rules.md +0 -259
  179. package/planning/acl-system/tasks/pattern-matching.md +0 -152
  180. package/planning/acl-system/tasks/yaml-loading.md +0 -271
  181. package/planning/core-executor/overview.md +0 -53
  182. package/planning/core-executor/plan.md +0 -88
  183. package/planning/core-executor/state.json +0 -76
  184. package/planning/core-executor/tasks/async-support.md +0 -106
  185. package/planning/core-executor/tasks/execution-pipeline.md +0 -113
  186. package/planning/core-executor/tasks/redaction.md +0 -85
  187. package/planning/core-executor/tasks/safety-checks.md +0 -65
  188. package/planning/core-executor/tasks/setup.md +0 -75
  189. package/planning/decorator-bindings/overview.md +0 -62
  190. package/planning/decorator-bindings/plan.md +0 -104
  191. package/planning/decorator-bindings/state.json +0 -87
  192. package/planning/decorator-bindings/tasks/binding-directory.md +0 -79
  193. package/planning/decorator-bindings/tasks/binding-loader.md +0 -148
  194. package/planning/decorator-bindings/tasks/explicit-schemas.md +0 -85
  195. package/planning/decorator-bindings/tasks/function-module.md +0 -127
  196. package/planning/decorator-bindings/tasks/module-factory.md +0 -89
  197. package/planning/decorator-bindings/tasks/schema-modes.md +0 -142
  198. package/planning/middleware-system/overview.md +0 -48
  199. package/planning/middleware-system/plan.md +0 -102
  200. package/planning/middleware-system/state.json +0 -65
  201. package/planning/middleware-system/tasks/adapters.md +0 -170
  202. package/planning/middleware-system/tasks/base.md +0 -115
  203. package/planning/middleware-system/tasks/logging-middleware.md +0 -304
  204. package/planning/middleware-system/tasks/manager.md +0 -313
  205. package/planning/observability/overview.md +0 -53
  206. package/planning/observability/plan.md +0 -119
  207. package/planning/observability/state.json +0 -98
  208. package/planning/observability/tasks/context-logger.md +0 -201
  209. package/planning/observability/tasks/exporters.md +0 -121
  210. package/planning/observability/tasks/metrics-collector.md +0 -162
  211. package/planning/observability/tasks/metrics-middleware.md +0 -141
  212. package/planning/observability/tasks/obs-logging-middleware.md +0 -179
  213. package/planning/observability/tasks/span-model.md +0 -120
  214. package/planning/observability/tasks/tracing-middleware.md +0 -179
  215. package/planning/overview.md +0 -81
  216. package/planning/registry-system/overview.md +0 -57
  217. package/planning/registry-system/plan.md +0 -114
  218. package/planning/registry-system/state.json +0 -109
  219. package/planning/registry-system/tasks/dependencies.md +0 -157
  220. package/planning/registry-system/tasks/entry-point.md +0 -148
  221. package/planning/registry-system/tasks/metadata.md +0 -198
  222. package/planning/registry-system/tasks/registry-core.md +0 -323
  223. package/planning/registry-system/tasks/scanner.md +0 -172
  224. package/planning/registry-system/tasks/schema-export.md +0 -261
  225. package/planning/registry-system/tasks/types.md +0 -124
  226. package/planning/registry-system/tasks/validation.md +0 -177
  227. package/planning/schema-system/overview.md +0 -56
  228. package/planning/schema-system/plan.md +0 -121
  229. package/planning/schema-system/state.json +0 -98
  230. package/planning/schema-system/tasks/exporter.md +0 -153
  231. package/planning/schema-system/tasks/loader.md +0 -106
  232. package/planning/schema-system/tasks/ref-resolver.md +0 -133
  233. package/planning/schema-system/tasks/strict-mode.md +0 -140
  234. package/planning/schema-system/tasks/typebox-generation.md +0 -133
  235. package/planning/schema-system/tasks/types-and-annotations.md +0 -160
  236. package/planning/schema-system/tasks/validator.md +0 -149
  237. package/src/acl.ts +0 -200
  238. package/src/bindings.ts +0 -207
  239. package/src/config.ts +0 -24
  240. package/src/context.ts +0 -78
  241. package/src/decorator.ts +0 -110
  242. package/src/errors.ts +0 -425
  243. package/src/executor.ts +0 -475
  244. package/src/middleware/adapters.ts +0 -54
  245. package/src/middleware/base.ts +0 -33
  246. package/src/middleware/logging.ts +0 -103
  247. package/src/middleware/manager.ts +0 -105
  248. package/src/module.ts +0 -43
  249. package/src/observability/context-logger.ts +0 -203
  250. package/src/observability/metrics.ts +0 -214
  251. package/src/observability/tracing.ts +0 -188
  252. package/src/registry/dependencies.ts +0 -99
  253. package/src/registry/entry-point.ts +0 -64
  254. package/src/registry/metadata.ts +0 -111
  255. package/src/registry/registry.ts +0 -360
  256. package/src/registry/scanner.ts +0 -168
  257. package/src/registry/schema-export.ts +0 -181
  258. package/src/registry/types.ts +0 -32
  259. package/src/registry/validation.ts +0 -38
  260. package/src/schema/annotations.ts +0 -68
  261. package/src/schema/exporter.ts +0 -90
  262. package/src/schema/loader.ts +0 -273
  263. package/src/schema/ref-resolver.ts +0 -244
  264. package/src/schema/strict.ts +0 -136
  265. package/src/schema/types.ts +0 -73
  266. package/src/schema/validator.ts +0 -82
  267. package/src/utils/index.ts +0 -5
  268. package/src/utils/pattern.ts +0 -30
  269. package/tests/helpers.ts +0 -30
  270. package/tests/integration/test-acl-safety.test.ts +0 -269
  271. package/tests/integration/test-binding-executor.test.ts +0 -194
  272. package/tests/integration/test-e2e-flow.test.ts +0 -117
  273. package/tests/integration/test-error-propagation.test.ts +0 -259
  274. package/tests/integration/test-middleware-chain.test.ts +0 -120
  275. package/tests/integration/test-observability-integration.test.ts +0 -438
  276. package/tests/observability/test-context-logger.test.ts +0 -123
  277. package/tests/observability/test-metrics.test.ts +0 -186
  278. package/tests/observability/test-tracing.test.ts +0 -131
  279. package/tests/registry/test-dependencies.test.ts +0 -70
  280. package/tests/registry/test-entry-point.test.ts +0 -133
  281. package/tests/registry/test-metadata.test.ts +0 -265
  282. package/tests/registry/test-registry.test.ts +0 -1008
  283. package/tests/registry/test-scanner.test.ts +0 -257
  284. package/tests/registry/test-schema-export.test.ts +0 -355
  285. package/tests/registry/test-validation.test.ts +0 -75
  286. package/tests/schema/test-annotations.test.ts +0 -137
  287. package/tests/schema/test-exporter.test.ts +0 -172
  288. package/tests/schema/test-loader.test.ts +0 -461
  289. package/tests/schema/test-ref-resolver.test.ts +0 -530
  290. package/tests/schema/test-strict.test.ts +0 -348
  291. package/tests/schema/test-validator.test.ts +0 -64
  292. package/tests/test-acl.test.ts +0 -423
  293. package/tests/test-bindings.test.ts +0 -227
  294. package/tests/test-config.test.ts +0 -76
  295. package/tests/test-context.test.ts +0 -151
  296. package/tests/test-decorator.test.ts +0 -173
  297. package/tests/test-errors.test.ts +0 -647
  298. package/tests/test-executor-stream.test.ts +0 -208
  299. package/tests/test-executor.test.ts +0 -252
  300. package/tests/test-logging-middleware.test.ts +0 -150
  301. package/tests/test-middleware-manager.test.ts +0 -185
  302. package/tests/test-middleware.test.ts +0 -86
  303. package/tests/utils/test-pattern.test.ts +0 -109
  304. package/tsconfig.build.json +0 -8
  305. package/tsconfig.json +0 -20
  306. package/vitest.config.ts +0 -18
@@ -1,188 +0,0 @@
1
- /**
2
- * Tracing system: Span, SpanExporter implementations, and TracingMiddleware.
3
- */
4
-
5
- import { randomBytes } from 'node:crypto';
6
- import type { Context } from '../context.js';
7
- import { Middleware } from '../middleware/base.js';
8
-
9
- export interface Span {
10
- traceId: string;
11
- name: string;
12
- startTime: number;
13
- spanId: string;
14
- parentSpanId: string | null;
15
- attributes: Record<string, unknown>;
16
- endTime: number | null;
17
- status: string;
18
- events: Array<Record<string, unknown>>;
19
- }
20
-
21
- export function createSpan(options: {
22
- traceId: string;
23
- name: string;
24
- startTime: number;
25
- spanId?: string;
26
- parentSpanId?: string | null;
27
- attributes?: Record<string, unknown>;
28
- }): Span {
29
- return {
30
- traceId: options.traceId,
31
- name: options.name,
32
- startTime: options.startTime,
33
- spanId: options.spanId ?? randomBytes(8).toString('hex'),
34
- parentSpanId: options.parentSpanId ?? null,
35
- attributes: options.attributes ?? {},
36
- endTime: null,
37
- status: 'ok',
38
- events: [],
39
- };
40
- }
41
-
42
- export interface SpanExporter {
43
- export(span: Span): void;
44
- }
45
-
46
- export class StdoutExporter implements SpanExporter {
47
- export(span: Span): void {
48
- process.stdout.write(JSON.stringify(span) + '\n');
49
- }
50
- }
51
-
52
- export class InMemoryExporter implements SpanExporter {
53
- private _spans: Span[] = [];
54
- private _maxSpans: number;
55
-
56
- constructor(maxSpans: number = 10_000) {
57
- this._maxSpans = maxSpans;
58
- }
59
-
60
- export(span: Span): void {
61
- if (this._spans.length >= this._maxSpans) {
62
- // Drop oldest half to amortize the cost instead of O(n) shift per insert
63
- this._spans = this._spans.slice(Math.floor(this._maxSpans / 2));
64
- }
65
- this._spans.push(span);
66
- }
67
-
68
- getSpans(): Span[] {
69
- return [...this._spans];
70
- }
71
-
72
- clear(): void {
73
- this._spans = [];
74
- }
75
- }
76
-
77
- const VALID_STRATEGIES = new Set(['full', 'proportional', 'error_first', 'off']);
78
-
79
- export class TracingMiddleware extends Middleware {
80
- private _exporter: SpanExporter;
81
- private _samplingRate: number;
82
- private _samplingStrategy: string;
83
-
84
- constructor(
85
- exporter: SpanExporter,
86
- samplingRate: number = 1.0,
87
- samplingStrategy: string = 'full',
88
- ) {
89
- super();
90
- if (samplingRate < 0.0 || samplingRate > 1.0) {
91
- throw new Error(`sampling_rate must be between 0.0 and 1.0, got ${samplingRate}`);
92
- }
93
- if (!VALID_STRATEGIES.has(samplingStrategy)) {
94
- throw new Error(`sampling_strategy must be one of ${[...VALID_STRATEGIES].join(', ')}, got '${samplingStrategy}'`);
95
- }
96
- this._exporter = exporter;
97
- this._samplingRate = samplingRate;
98
- this._samplingStrategy = samplingStrategy;
99
- }
100
-
101
- private _shouldSample(context: Context): boolean {
102
- const existing = context.data['_tracing_sampled'];
103
- if (typeof existing === 'boolean') return existing;
104
-
105
- let decision: boolean;
106
- if (this._samplingStrategy === 'full') {
107
- decision = true;
108
- } else if (this._samplingStrategy === 'off') {
109
- decision = false;
110
- } else {
111
- decision = Math.random() < this._samplingRate;
112
- }
113
-
114
- context.data['_tracing_sampled'] = decision;
115
- return decision;
116
- }
117
-
118
- override before(
119
- moduleId: string,
120
- _inputs: Record<string, unknown>,
121
- context: Context,
122
- ): null {
123
- this._shouldSample(context);
124
-
125
- const spansStack = (context.data['_tracing_spans'] as Span[]) ?? [];
126
- context.data['_tracing_spans'] = spansStack;
127
- const parentSpanId = spansStack.length > 0 ? spansStack[spansStack.length - 1].spanId : null;
128
-
129
- const span = createSpan({
130
- traceId: context.traceId,
131
- name: 'apcore.module.execute',
132
- startTime: performance.now(),
133
- parentSpanId,
134
- attributes: {
135
- moduleId,
136
- method: 'execute',
137
- callerId: context.callerId,
138
- },
139
- });
140
- spansStack.push(span);
141
- return null;
142
- }
143
-
144
- override after(
145
- moduleId: string,
146
- _inputs: Record<string, unknown>,
147
- _output: Record<string, unknown>,
148
- context: Context,
149
- ): null {
150
- const spansStack = (context.data['_tracing_spans'] as Span[]) ?? [];
151
- if (spansStack.length === 0) return null;
152
-
153
- const span = spansStack.pop()!;
154
- span.endTime = performance.now();
155
- span.status = 'ok';
156
- span.attributes['duration_ms'] = span.endTime - span.startTime;
157
- span.attributes['success'] = true;
158
-
159
- if (context.data['_tracing_sampled']) {
160
- this._exporter.export(span);
161
- }
162
- return null;
163
- }
164
-
165
- override onError(
166
- moduleId: string,
167
- _inputs: Record<string, unknown>,
168
- error: Error,
169
- context: Context,
170
- ): null {
171
- const spansStack = (context.data['_tracing_spans'] as Span[]) ?? [];
172
- if (spansStack.length === 0) return null;
173
-
174
- const span = spansStack.pop()!;
175
- span.endTime = performance.now();
176
- span.status = 'error';
177
- span.attributes['duration_ms'] = span.endTime - span.startTime;
178
- span.attributes['success'] = false;
179
- span.attributes['error_code'] = (error as unknown as Record<string, unknown>)['code'] ?? error.constructor.name;
180
-
181
- const shouldExport =
182
- this._samplingStrategy === 'error_first' || context.data['_tracing_sampled'];
183
- if (shouldExport) {
184
- this._exporter.export(span);
185
- }
186
- return null;
187
- }
188
- }
@@ -1,99 +0,0 @@
1
- /**
2
- * Dependency resolution via Kahn's topological sort.
3
- */
4
-
5
- import { CircularDependencyError, ModuleLoadError } from '../errors.js';
6
- import type { DependencyInfo } from './types.js';
7
-
8
- export function resolveDependencies(
9
- modules: Array<[string, DependencyInfo[]]>,
10
- knownIds?: Set<string> | null,
11
- ): string[] {
12
- if (modules.length === 0) return [];
13
-
14
- const ids = knownIds ?? new Set(modules.map(([id]) => id));
15
-
16
- // Build graph and in-degree
17
- const graph = new Map<string, Set<string>>();
18
- const inDegree = new Map<string, number>();
19
-
20
- for (const [modId] of modules) {
21
- inDegree.set(modId, 0);
22
- }
23
-
24
- for (const [moduleId, deps] of modules) {
25
- for (const dep of deps) {
26
- if (!ids.has(dep.moduleId)) {
27
- if (dep.optional) continue;
28
- throw new ModuleLoadError(moduleId, `Required dependency '${dep.moduleId}' not found`);
29
- }
30
- if (!graph.has(dep.moduleId)) graph.set(dep.moduleId, new Set());
31
- graph.get(dep.moduleId)!.add(moduleId);
32
- inDegree.set(moduleId, (inDegree.get(moduleId) ?? 0) + 1);
33
- }
34
- }
35
-
36
- // Initialize queue with zero-in-degree nodes (sorted for determinism)
37
- const queue: string[] = [...inDegree.entries()]
38
- .filter(([, deg]) => deg === 0)
39
- .map(([id]) => id)
40
- .sort();
41
-
42
- const loadOrder: string[] = [];
43
- while (queue.length > 0) {
44
- const modId = queue.shift()!;
45
- loadOrder.push(modId);
46
- const dependents = graph.get(modId);
47
- if (dependents) {
48
- for (const dependent of [...dependents].sort()) {
49
- const newDeg = (inDegree.get(dependent) ?? 1) - 1;
50
- inDegree.set(dependent, newDeg);
51
- if (newDeg === 0) {
52
- queue.push(dependent);
53
- }
54
- }
55
- }
56
- }
57
-
58
- // Check for cycles
59
- if (loadOrder.length < modules.length) {
60
- const ordered = new Set(loadOrder);
61
- const remaining = new Set(modules.filter(([id]) => !ordered.has(id)).map(([id]) => id));
62
- const cyclePath = extractCycle(modules, remaining);
63
- throw new CircularDependencyError(cyclePath);
64
- }
65
-
66
- return loadOrder;
67
- }
68
-
69
- function extractCycle(
70
- modules: Array<[string, DependencyInfo[]]>,
71
- remaining: Set<string>,
72
- ): string[] {
73
- const depMap = new Map<string, string[]>();
74
- for (const [modId, deps] of modules) {
75
- if (remaining.has(modId)) {
76
- depMap.set(modId, deps.filter((d) => remaining.has(d.moduleId)).map((d) => d.moduleId));
77
- }
78
- }
79
-
80
- const start = remaining.values().next().value as string;
81
- const visited: string[] = [start];
82
- const visitedSet = new Set([start]);
83
- let current = start;
84
-
85
- while (true) {
86
- const nexts = depMap.get(current) ?? [];
87
- if (nexts.length === 0) break;
88
- const nxt = nexts[0];
89
- if (visitedSet.has(nxt)) {
90
- const idx = visited.indexOf(nxt);
91
- return [...visited.slice(idx), nxt];
92
- }
93
- visited.push(nxt);
94
- visitedSet.add(nxt);
95
- current = nxt;
96
- }
97
-
98
- return [...remaining, remaining.values().next().value as string];
99
- }
@@ -1,64 +0,0 @@
1
- /**
2
- * Entry point resolution for discovered module files.
3
- */
4
-
5
- import { ModuleLoadError } from '../errors.js';
6
- import { validateModule } from './validation.js';
7
-
8
- export function snakeToPascal(name: string): string {
9
- if (!name) return '';
10
- return name.split('_').map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join('');
11
- }
12
-
13
- function isModuleClass(obj: unknown): boolean {
14
- if (typeof obj !== 'object' || obj === null) return false;
15
- const record = obj as Record<string, unknown>;
16
- return (
17
- record['inputSchema'] != null &&
18
- typeof record['inputSchema'] === 'object' &&
19
- record['outputSchema'] != null &&
20
- typeof record['outputSchema'] === 'object' &&
21
- typeof record['description'] === 'string' &&
22
- typeof record['execute'] === 'function'
23
- );
24
- }
25
-
26
- export async function resolveEntryPoint(
27
- filePath: string,
28
- meta?: Record<string, unknown> | null,
29
- ): Promise<unknown> {
30
- let loaded: Record<string, unknown>;
31
- try {
32
- loaded = await import(filePath);
33
- } catch (e) {
34
- throw new ModuleLoadError(filePath, `Failed to import module: ${e}`);
35
- }
36
-
37
- // Meta override mode
38
- if (meta && 'entry_point' in meta) {
39
- const className = (meta['entry_point'] as string).split(':').pop()!;
40
- const cls = loaded[className];
41
- if (cls == null) {
42
- throw new ModuleLoadError(filePath, `Entry point class '${className}' not found`);
43
- }
44
- return cls;
45
- }
46
-
47
- // Auto-infer: look for default export first, then named exports
48
- if (loaded['default'] && isModuleClass(loaded['default'])) {
49
- return loaded['default'];
50
- }
51
-
52
- const candidates: unknown[] = [];
53
- for (const [, value] of Object.entries(loaded)) {
54
- if (isModuleClass(value)) {
55
- candidates.push(value);
56
- }
57
- }
58
-
59
- if (candidates.length === 1) return candidates[0];
60
- if (candidates.length === 0) {
61
- throw new ModuleLoadError(filePath, 'No Module subclass found in file');
62
- }
63
- throw new ModuleLoadError(filePath, 'Ambiguous entry point: multiple Module subclasses found');
64
- }
@@ -1,111 +0,0 @@
1
- /**
2
- * Metadata and ID map loading for the registry system.
3
- */
4
-
5
- import { readFileSync, existsSync } from 'node:fs';
6
- import yaml from 'js-yaml';
7
- import { ConfigError, ConfigNotFoundError } from '../errors.js';
8
- import type { DependencyInfo } from './types.js';
9
-
10
- export function loadMetadata(metaPath: string): Record<string, unknown> {
11
- if (!existsSync(metaPath)) return {};
12
-
13
- const content = readFileSync(metaPath, 'utf-8');
14
- let parsed: unknown;
15
- try {
16
- parsed = yaml.load(content);
17
- } catch (e) {
18
- throw new ConfigError(`Invalid YAML in metadata file: ${metaPath}`);
19
- }
20
-
21
- if (parsed === null || parsed === undefined) return {};
22
- if (typeof parsed !== 'object' || Array.isArray(parsed)) {
23
- throw new ConfigError(`Metadata file must be a YAML mapping: ${metaPath}`);
24
- }
25
-
26
- return parsed as Record<string, unknown>;
27
- }
28
-
29
- export function parseDependencies(depsRaw: Array<Record<string, unknown>>): DependencyInfo[] {
30
- if (!depsRaw || depsRaw.length === 0) return [];
31
-
32
- const result: DependencyInfo[] = [];
33
- for (const dep of depsRaw) {
34
- const moduleId = dep['module_id'] as string | undefined;
35
- if (!moduleId) {
36
- console.warn(`[apcore:metadata] Dependency entry missing 'module_id', skipping`);
37
- continue;
38
- }
39
- result.push({
40
- moduleId,
41
- version: (dep['version'] as string) ?? null,
42
- optional: (dep['optional'] as boolean) ?? false,
43
- });
44
- }
45
- return result;
46
- }
47
-
48
- export function mergeModuleMetadata(
49
- moduleObj: Record<string, unknown>,
50
- meta: Record<string, unknown>,
51
- ): Record<string, unknown> {
52
- const codeDesc = (moduleObj['description'] as string) ?? '';
53
- const codeName = (moduleObj['name'] as string) ?? null;
54
- const codeTags = (moduleObj['tags'] as string[]) ?? [];
55
- const codeVersion = (moduleObj['version'] as string) ?? '1.0.0';
56
- const codeAnnotations = moduleObj['annotations'] ?? null;
57
- const codeExamples = (moduleObj['examples'] as unknown[]) ?? [];
58
- const codeMetadata = (moduleObj['metadata'] as Record<string, unknown>) ?? {};
59
- const codeDocs = (moduleObj['documentation'] as string) ?? null;
60
-
61
- const yamlMetadata = (meta['metadata'] as Record<string, unknown>) ?? {};
62
- const mergedMetadata = { ...codeMetadata, ...yamlMetadata };
63
-
64
- return {
65
- description: (meta['description'] as string) || codeDesc,
66
- name: (meta['name'] as string) || codeName,
67
- tags: meta['tags'] != null ? meta['tags'] : codeTags || [],
68
- version: (meta['version'] as string) || codeVersion,
69
- annotations: meta['annotations'] != null ? meta['annotations'] : codeAnnotations,
70
- examples: meta['examples'] != null ? meta['examples'] : codeExamples || [],
71
- metadata: mergedMetadata,
72
- documentation: (meta['documentation'] as string) || codeDocs,
73
- };
74
- }
75
-
76
- export function loadIdMap(idMapPath: string): Record<string, Record<string, unknown>> {
77
- if (!existsSync(idMapPath)) {
78
- throw new ConfigNotFoundError(idMapPath);
79
- }
80
-
81
- const content = readFileSync(idMapPath, 'utf-8');
82
- let parsed: unknown;
83
- try {
84
- parsed = yaml.load(content);
85
- } catch (e) {
86
- throw new ConfigError(`Invalid YAML in ID map file: ${idMapPath}`);
87
- }
88
-
89
- if (typeof parsed !== 'object' || parsed === null || !('mappings' in (parsed as Record<string, unknown>))) {
90
- throw new ConfigError("ID map must contain a 'mappings' list");
91
- }
92
-
93
- const mappings = (parsed as Record<string, unknown>)['mappings'];
94
- if (!Array.isArray(mappings)) {
95
- throw new ConfigError("ID map must contain a 'mappings' list");
96
- }
97
-
98
- const result: Record<string, Record<string, unknown>> = {};
99
- for (const entry of mappings) {
100
- const filePath = (entry as Record<string, unknown>)['file'] as string;
101
- if (!filePath) {
102
- console.warn(`[apcore:metadata] ID map entry missing 'file' field, skipping`);
103
- continue;
104
- }
105
- result[filePath] = {
106
- id: ((entry as Record<string, unknown>)['id'] as string) ?? filePath,
107
- class: (entry as Record<string, unknown>)['class'] ?? null,
108
- };
109
- }
110
- return result;
111
- }