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
package/src/acl.ts DELETED
@@ -1,200 +0,0 @@
1
- /**
2
- * ACL (Access Control List) types and implementation for apcore.
3
- */
4
-
5
- import { readFileSync, existsSync } from 'node:fs';
6
- import yaml from 'js-yaml';
7
- import type { Context } from './context.js';
8
- import { ACLRuleError, ConfigNotFoundError } from './errors.js';
9
- import { matchPattern } from './utils/pattern.js';
10
-
11
- export interface ACLRule {
12
- callers: string[];
13
- targets: string[];
14
- effect: string;
15
- description: string;
16
- conditions?: Record<string, unknown> | null;
17
- }
18
-
19
- function parseAclRule(rawRule: unknown, index: number): ACLRule {
20
- if (typeof rawRule !== 'object' || rawRule === null || Array.isArray(rawRule)) {
21
- throw new ACLRuleError(`Rule ${index} must be a mapping, got ${typeof rawRule}`);
22
- }
23
-
24
- const ruleObj = rawRule as Record<string, unknown>;
25
- for (const key of ['callers', 'targets', 'effect']) {
26
- if (!(key in ruleObj)) {
27
- throw new ACLRuleError(`Rule ${index} missing required key '${key}'`);
28
- }
29
- }
30
-
31
- const effect = ruleObj['effect'] as string;
32
- if (effect !== 'allow' && effect !== 'deny') {
33
- throw new ACLRuleError(`Rule ${index} has invalid effect '${effect}', must be 'allow' or 'deny'`);
34
- }
35
-
36
- const callers = ruleObj['callers'];
37
- if (!Array.isArray(callers)) {
38
- throw new ACLRuleError(`Rule ${index} 'callers' must be a list, got ${typeof callers}`);
39
- }
40
-
41
- const targets = ruleObj['targets'];
42
- if (!Array.isArray(targets)) {
43
- throw new ACLRuleError(`Rule ${index} 'targets' must be a list, got ${typeof targets}`);
44
- }
45
-
46
- return {
47
- callers: callers as string[],
48
- targets: targets as string[],
49
- effect,
50
- description: (ruleObj['description'] as string) ?? '',
51
- conditions: (ruleObj['conditions'] as Record<string, unknown>) ?? null,
52
- };
53
- }
54
-
55
- export class ACL {
56
- private _rules: ACLRule[];
57
- private _defaultEffect: string;
58
- private _yamlPath: string | null = null;
59
- debug: boolean = false;
60
-
61
- constructor(rules: ACLRule[], defaultEffect: string = 'deny') {
62
- if (defaultEffect !== 'allow' && defaultEffect !== 'deny') {
63
- throw new ACLRuleError(`Invalid default_effect '${defaultEffect}', must be 'allow' or 'deny'`);
64
- }
65
- this._rules = [...rules];
66
- this._defaultEffect = defaultEffect;
67
- }
68
-
69
- static load(yamlPath: string): ACL {
70
- if (!existsSync(yamlPath)) {
71
- throw new ConfigNotFoundError(yamlPath);
72
- }
73
-
74
- let data: unknown;
75
- try {
76
- const content = readFileSync(yamlPath, 'utf-8');
77
- data = yaml.load(content);
78
- } catch (e) {
79
- if (e instanceof ConfigNotFoundError) throw e;
80
- throw new ACLRuleError(`Invalid YAML in ${yamlPath}: ${e}`);
81
- }
82
-
83
- if (typeof data !== 'object' || data === null || Array.isArray(data)) {
84
- throw new ACLRuleError(`ACL config must be a mapping, got ${typeof data}`);
85
- }
86
-
87
- const dataObj = data as Record<string, unknown>;
88
- if (!('rules' in dataObj)) {
89
- throw new ACLRuleError("ACL config missing required 'rules' key");
90
- }
91
-
92
- const rawRules = dataObj['rules'];
93
- if (!Array.isArray(rawRules)) {
94
- throw new ACLRuleError(`'rules' must be a list, got ${typeof rawRules}`);
95
- }
96
-
97
- const defaultEffect = (dataObj['default_effect'] as string) ?? 'deny';
98
- const rules = rawRules.map((raw, i) => parseAclRule(raw, i));
99
-
100
- const acl = new ACL(rules, defaultEffect);
101
- acl._yamlPath = yamlPath;
102
- return acl;
103
- }
104
-
105
- check(callerId: string | null, targetId: string, context?: Context | null): boolean {
106
- const effectiveCaller = callerId === null ? '@external' : callerId;
107
-
108
- for (const rule of this._rules) {
109
- if (this._matchesRule(rule, effectiveCaller, targetId, context ?? null)) {
110
- return rule.effect === 'allow';
111
- }
112
- }
113
-
114
- return this._defaultEffect === 'allow';
115
- }
116
-
117
- private _matchPattern(pattern: string, value: string, context: Context | null): boolean {
118
- if (pattern === '@external') return value === '@external';
119
- if (pattern === '@system') {
120
- return context !== null && context.identity !== null && context.identity.type === 'system';
121
- }
122
- return matchPattern(pattern, value);
123
- }
124
-
125
- private _matchesRule(rule: ACLRule, caller: string, target: string, context: Context | null): boolean {
126
- const callerMatch = rule.callers.some((p) => this._matchPattern(p, caller, context));
127
- if (!callerMatch) return false;
128
-
129
- const targetMatch = rule.targets.some((p) => this._matchPattern(p, target, context));
130
- if (!targetMatch) return false;
131
-
132
- if (rule.conditions != null) {
133
- if (!this._checkConditions(rule.conditions, context)) return false;
134
- }
135
-
136
- return true;
137
- }
138
-
139
- private _checkConditions(conditions: Record<string, unknown>, context: Context | null): boolean {
140
- if (context === null) return false;
141
-
142
- if ('identity_types' in conditions) {
143
- const types = conditions['identity_types'];
144
- if (!Array.isArray(types)) {
145
- console.warn('[apcore:acl] identity_types condition must be an array');
146
- return false;
147
- }
148
- if (context.identity === null || !types.includes(context.identity.type)) return false;
149
- }
150
-
151
- if ('roles' in conditions) {
152
- const roles = conditions['roles'];
153
- if (!Array.isArray(roles)) {
154
- console.warn('[apcore:acl] roles condition must be an array');
155
- return false;
156
- }
157
- if (context.identity === null) return false;
158
- const identityRoles = new Set(context.identity.roles);
159
- if (!roles.some((r: string) => identityRoles.has(r))) return false;
160
- }
161
-
162
- if ('max_call_depth' in conditions) {
163
- const maxDepth = conditions['max_call_depth'];
164
- if (typeof maxDepth !== 'number') {
165
- console.warn('[apcore:acl] max_call_depth condition must be a number');
166
- return false;
167
- }
168
- if (context.callChain.length > maxDepth) return false;
169
- }
170
-
171
- return true;
172
- }
173
-
174
- addRule(rule: ACLRule): void {
175
- this._rules.unshift(rule);
176
- }
177
-
178
- removeRule(callers: string[], targets: string[]): boolean {
179
- for (let i = 0; i < this._rules.length; i++) {
180
- const rule = this._rules[i];
181
- if (
182
- JSON.stringify(rule.callers) === JSON.stringify(callers) &&
183
- JSON.stringify(rule.targets) === JSON.stringify(targets)
184
- ) {
185
- this._rules.splice(i, 1);
186
- return true;
187
- }
188
- }
189
- return false;
190
- }
191
-
192
- reload(): void {
193
- if (this._yamlPath === null) {
194
- throw new ACLRuleError('Cannot reload: ACL was not loaded from a YAML file');
195
- }
196
- const reloaded = ACL.load(this._yamlPath);
197
- this._rules = reloaded._rules;
198
- this._defaultEffect = reloaded._defaultEffect;
199
- }
200
- }
package/src/bindings.ts DELETED
@@ -1,207 +0,0 @@
1
- /**
2
- * YAML binding loader for zero-code-modification module integration.
3
- */
4
-
5
- import { readFileSync, existsSync, readdirSync, statSync } from 'node:fs';
6
- import { resolve, dirname, join, basename, isAbsolute } from 'node:path';
7
- import { Type, type TSchema } from '@sinclair/typebox';
8
- import yaml from 'js-yaml';
9
- import { FunctionModule } from './decorator.js';
10
- import {
11
- BindingCallableNotFoundError,
12
- BindingFileInvalidError,
13
- BindingInvalidTargetError,
14
- BindingModuleNotFoundError,
15
- BindingNotCallableError,
16
- BindingSchemaMissingError,
17
- } from './errors.js';
18
- import type { Registry } from './registry/registry.js';
19
- import { jsonSchemaToTypeBox } from './schema/loader.js';
20
-
21
- export class BindingLoader {
22
- async loadBindings(filePath: string, registry: Registry): Promise<FunctionModule[]> {
23
- const bindingFileDir = dirname(filePath);
24
-
25
- let content: string;
26
- try {
27
- content = readFileSync(filePath, 'utf-8');
28
- } catch (e) {
29
- throw new BindingFileInvalidError(filePath, String(e));
30
- }
31
-
32
- let data: unknown;
33
- try {
34
- data = yaml.load(content);
35
- } catch (e) {
36
- throw new BindingFileInvalidError(filePath, `YAML parse error: ${e}`);
37
- }
38
-
39
- if (data === null || data === undefined) {
40
- throw new BindingFileInvalidError(filePath, 'File is empty');
41
- }
42
-
43
- const dataObj = data as Record<string, unknown>;
44
- if (!('bindings' in dataObj)) {
45
- throw new BindingFileInvalidError(filePath, "Missing 'bindings' key");
46
- }
47
-
48
- const bindings = dataObj['bindings'];
49
- if (!Array.isArray(bindings)) {
50
- throw new BindingFileInvalidError(filePath, "'bindings' must be a list");
51
- }
52
-
53
- const results: FunctionModule[] = [];
54
- for (const entry of bindings) {
55
- const entryObj = entry as Record<string, unknown>;
56
- if (!('module_id' in entryObj)) {
57
- throw new BindingFileInvalidError(filePath, "Binding entry missing 'module_id'");
58
- }
59
- if (!('target' in entryObj)) {
60
- throw new BindingFileInvalidError(filePath, "Binding entry missing 'target'");
61
- }
62
-
63
- const fm = await this._createModuleFromBinding(entryObj, bindingFileDir);
64
- registry.register(entryObj['module_id'] as string, fm);
65
- results.push(fm);
66
- }
67
-
68
- return results;
69
- }
70
-
71
- async loadBindingDir(
72
- dirPath: string,
73
- registry: Registry,
74
- pattern: string = '*.binding.yaml',
75
- ): Promise<FunctionModule[]> {
76
- if (!existsSync(dirPath) || !statSync(dirPath).isDirectory()) {
77
- throw new BindingFileInvalidError(dirPath, 'Directory does not exist');
78
- }
79
-
80
- const files = readdirSync(dirPath)
81
- .filter((f) => {
82
- // Simple glob matching for *.binding.yaml
83
- const suffix = pattern.replace('*', '');
84
- return f.endsWith(suffix);
85
- })
86
- .sort();
87
-
88
- const results: FunctionModule[] = [];
89
- for (const f of files) {
90
- const fms = await this.loadBindings(join(dirPath, f), registry);
91
- results.push(...fms);
92
- }
93
- return results;
94
- }
95
-
96
- async resolveTarget(targetString: string): Promise<(...args: unknown[]) => unknown> {
97
- if (!targetString.includes(':')) {
98
- throw new BindingInvalidTargetError(targetString);
99
- }
100
-
101
- const [modulePath, callableName] = targetString.split(':', 2);
102
-
103
- if (modulePath.includes('..')) {
104
- throw new BindingInvalidTargetError(
105
- `Module path '${modulePath}' must not contain '..' segments`,
106
- );
107
- }
108
-
109
- if (modulePath.startsWith('file:')) {
110
- throw new BindingInvalidTargetError(
111
- `Module path '${modulePath}' must not use file: URLs`,
112
- );
113
- }
114
-
115
- let mod: Record<string, unknown>;
116
- try {
117
- mod = await import(modulePath);
118
- } catch (e) {
119
- throw new BindingModuleNotFoundError(modulePath);
120
- }
121
-
122
- if (callableName.includes('.')) {
123
- const [className, methodName] = callableName.split('.', 2);
124
- const cls = mod[className];
125
- if (cls == null) {
126
- throw new BindingCallableNotFoundError(className, modulePath);
127
- }
128
- let instance: Record<string, unknown>;
129
- try {
130
- instance = new (cls as new () => Record<string, unknown>)();
131
- } catch {
132
- throw new BindingCallableNotFoundError(callableName, modulePath);
133
- }
134
- const method = instance[methodName];
135
- if (method == null) {
136
- throw new BindingCallableNotFoundError(callableName, modulePath);
137
- }
138
- if (typeof method !== 'function') {
139
- throw new BindingNotCallableError(targetString);
140
- }
141
- return method.bind(instance) as (...args: unknown[]) => unknown;
142
- }
143
-
144
- const result = mod[callableName];
145
- if (result == null) {
146
- throw new BindingCallableNotFoundError(callableName, modulePath);
147
- }
148
- if (typeof result !== 'function') {
149
- throw new BindingNotCallableError(targetString);
150
- }
151
- return result as (...args: unknown[]) => unknown;
152
- }
153
-
154
- private async _createModuleFromBinding(
155
- binding: Record<string, unknown>,
156
- bindingFileDir: string,
157
- ): Promise<FunctionModule> {
158
- const func = await this.resolveTarget(binding['target'] as string);
159
- const moduleId = binding['module_id'] as string;
160
-
161
- let inputSchema: TSchema;
162
- let outputSchema: TSchema;
163
-
164
- if ('input_schema' in binding || 'output_schema' in binding) {
165
- const inputSchemaDict = (binding['input_schema'] as Record<string, unknown>) ?? {};
166
- const outputSchemaDict = (binding['output_schema'] as Record<string, unknown>) ?? {};
167
- inputSchema = jsonSchemaToTypeBox(inputSchemaDict);
168
- outputSchema = jsonSchemaToTypeBox(outputSchemaDict);
169
- } else if ('schema_ref' in binding) {
170
- const refPath = resolve(bindingFileDir, binding['schema_ref'] as string);
171
- if (!existsSync(refPath)) {
172
- throw new BindingFileInvalidError(refPath, 'Schema reference file not found');
173
- }
174
- let refData: Record<string, unknown>;
175
- try {
176
- refData = (yaml.load(readFileSync(refPath, 'utf-8')) as Record<string, unknown>) ?? {};
177
- } catch (e) {
178
- throw new BindingFileInvalidError(refPath, `YAML parse error: ${e}`);
179
- }
180
- inputSchema = jsonSchemaToTypeBox(
181
- (refData['input_schema'] as Record<string, unknown>) ?? {},
182
- );
183
- outputSchema = jsonSchemaToTypeBox(
184
- (refData['output_schema'] as Record<string, unknown>) ?? {},
185
- );
186
- } else {
187
- // No schema, use permissive
188
- inputSchema = Type.Record(Type.String(), Type.Unknown());
189
- outputSchema = Type.Record(Type.String(), Type.Unknown());
190
- }
191
-
192
- return new FunctionModule({
193
- execute: async (inputs, context) => {
194
- const result = await func(inputs, context);
195
- if (result === null || result === undefined) return {};
196
- if (typeof result === 'object' && !Array.isArray(result)) return result as Record<string, unknown>;
197
- return { result };
198
- },
199
- moduleId,
200
- inputSchema,
201
- outputSchema,
202
- description: (binding['description'] as string) ?? undefined,
203
- tags: (binding['tags'] as string[]) ?? null,
204
- version: (binding['version'] as string) ?? '1.0.0',
205
- });
206
- }
207
- }
package/src/config.ts DELETED
@@ -1,24 +0,0 @@
1
- /**
2
- * Configuration accessor with dot-path key support.
3
- */
4
-
5
- export class Config {
6
- private _data: Record<string, unknown>;
7
-
8
- constructor(data?: Record<string, unknown>) {
9
- this._data = data ?? {};
10
- }
11
-
12
- get(key: string, defaultValue?: unknown): unknown {
13
- const parts = key.split('.');
14
- let current: unknown = this._data;
15
- for (const part of parts) {
16
- if (current !== null && typeof current === 'object' && part in (current as Record<string, unknown>)) {
17
- current = (current as Record<string, unknown>)[part];
18
- } else {
19
- return defaultValue;
20
- }
21
- }
22
- return current;
23
- }
24
- }
package/src/context.ts DELETED
@@ -1,78 +0,0 @@
1
-
2
- import { v4 as uuidv4 } from 'uuid';
3
-
4
- /**
5
- * Execution context, identity, and context creation.
6
- */
7
-
8
- export interface Identity {
9
- readonly id: string;
10
- readonly type: string;
11
- readonly roles: readonly string[];
12
- readonly attrs: Readonly<Record<string, unknown>>;
13
- }
14
-
15
- export function createIdentity(
16
- id: string,
17
- type: string = 'user',
18
- roles: string[] = [],
19
- attrs: Record<string, unknown> = {},
20
- ): Identity {
21
- return Object.freeze({ id, type, roles: Object.freeze([...roles]), attrs: Object.freeze({ ...attrs }) });
22
- }
23
-
24
- export class Context {
25
- readonly traceId: string;
26
- readonly callerId: string | null;
27
- readonly callChain: readonly string[];
28
- readonly executor: unknown;
29
- readonly identity: Identity | null;
30
- redactedInputs: Record<string, unknown> | null;
31
- readonly data: Record<string, unknown>;
32
-
33
- constructor(
34
- traceId: string,
35
- callerId: string | null = null,
36
- callChain: string[] = [],
37
- executor: unknown = null,
38
- identity: Identity | null = null,
39
- redactedInputs: Record<string, unknown> | null = null,
40
- data: Record<string, unknown> = {},
41
- ) {
42
- this.traceId = traceId;
43
- this.callerId = callerId;
44
- this.callChain = Object.freeze([...callChain]);
45
- this.executor = executor;
46
- this.identity = identity;
47
- this.redactedInputs = redactedInputs;
48
- this.data = data;
49
- }
50
-
51
- static create(
52
- executor: unknown = null,
53
- identity: Identity | null = null,
54
- data?: Record<string, unknown>,
55
- ): Context {
56
- return new Context(
57
- uuidv4(),
58
- null,
59
- [],
60
- executor,
61
- identity,
62
- null,
63
- data ?? {},
64
- );
65
- }
66
-
67
- child(targetModuleId: string): Context {
68
- return new Context(
69
- this.traceId,
70
- this.callChain.length > 0 ? this.callChain[this.callChain.length - 1] : null,
71
- [...this.callChain, targetModuleId],
72
- this.executor,
73
- this.identity,
74
- null,
75
- this.data, // shared reference
76
- );
77
- }
78
- }
package/src/decorator.ts DELETED
@@ -1,110 +0,0 @@
1
- /**
2
- * Module factory, FunctionModule wrapper.
3
- *
4
- * TypeScript version uses explicit TypeBox schemas instead of runtime type inference.
5
- */
6
-
7
- import type { TSchema } from '@sinclair/typebox';
8
- import type { Context } from './context.js';
9
- import type { ModuleAnnotations, ModuleExample } from './module.js';
10
-
11
- export function normalizeResult(result: unknown): Record<string, unknown> {
12
- if (result === null || result === undefined) return {};
13
- if (typeof result === 'object' && !Array.isArray(result)) return result as Record<string, unknown>;
14
- return { result };
15
- }
16
-
17
- export class FunctionModule {
18
- readonly moduleId: string;
19
- readonly inputSchema: TSchema;
20
- readonly outputSchema: TSchema;
21
- readonly description: string;
22
- readonly documentation: string | null;
23
- readonly tags: string[] | null;
24
- readonly version: string;
25
- readonly annotations: ModuleAnnotations | null;
26
- readonly metadata: Record<string, unknown> | null;
27
- readonly examples: ModuleExample[] | null;
28
-
29
- private _executeFn: (inputs: Record<string, unknown>, context: Context) => Promise<Record<string, unknown>> | Record<string, unknown>;
30
-
31
- constructor(options: {
32
- execute: (inputs: Record<string, unknown>, context: Context) => Promise<Record<string, unknown>> | Record<string, unknown>;
33
- moduleId: string;
34
- inputSchema: TSchema;
35
- outputSchema: TSchema;
36
- description?: string;
37
- documentation?: string | null;
38
- tags?: string[] | null;
39
- version?: string;
40
- annotations?: ModuleAnnotations | null;
41
- metadata?: Record<string, unknown> | null;
42
- examples?: ModuleExample[] | null;
43
- }) {
44
- this.moduleId = options.moduleId;
45
- this.inputSchema = options.inputSchema;
46
- this.outputSchema = options.outputSchema;
47
- this.description = options.description ?? `Module ${options.moduleId}`;
48
- this.documentation = options.documentation ?? null;
49
- this.tags = options.tags ?? null;
50
- this.version = options.version ?? '1.0.0';
51
- this.annotations = options.annotations ?? null;
52
- this.metadata = options.metadata ?? null;
53
- this.examples = options.examples ?? null;
54
- this._executeFn = options.execute;
55
- }
56
-
57
- async execute(inputs: Record<string, unknown>, context: Context): Promise<Record<string, unknown>> {
58
- const result = await this._executeFn(inputs, context);
59
- return normalizeResult(result);
60
- }
61
- }
62
-
63
- export function makeAutoId(name: string): string {
64
- let raw = name.toLowerCase();
65
- raw = raw.replace(/[^a-z0-9_.]/g, '_');
66
- const segments = raw.split('.');
67
- return segments
68
- .map((s) => (s && s[0] >= '0' && s[0] <= '9' ? '_' + s : s))
69
- .join('.');
70
- }
71
-
72
- /**
73
- * Create a FunctionModule from options. TypeScript version requires explicit schemas.
74
- */
75
- export function module(options: {
76
- id?: string;
77
- inputSchema: TSchema;
78
- outputSchema: TSchema;
79
- description?: string;
80
- documentation?: string | null;
81
- annotations?: ModuleAnnotations | null;
82
- tags?: string[] | null;
83
- version?: string;
84
- metadata?: Record<string, unknown> | null;
85
- examples?: ModuleExample[] | null;
86
- execute: (inputs: Record<string, unknown>, context: Context) => Promise<Record<string, unknown>> | Record<string, unknown>;
87
- registry?: { register(moduleId: string, module: unknown): void } | null;
88
- }): FunctionModule {
89
- const moduleId = options.id ?? makeAutoId('anonymous');
90
-
91
- const fm = new FunctionModule({
92
- execute: options.execute,
93
- moduleId,
94
- inputSchema: options.inputSchema,
95
- outputSchema: options.outputSchema,
96
- description: options.description,
97
- documentation: options.documentation,
98
- tags: options.tags,
99
- version: options.version,
100
- annotations: options.annotations,
101
- metadata: options.metadata,
102
- examples: options.examples,
103
- });
104
-
105
- if (options.registry) {
106
- options.registry.register(fm.moduleId, fm);
107
- }
108
-
109
- return fm;
110
- }