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,32 +0,0 @@
1
- /**
2
- * Registry types: ModuleDescriptor, DiscoveredModule, DependencyInfo.
3
- */
4
-
5
- import type { ModuleAnnotations, ModuleExample } from '../module.js';
6
-
7
- export interface ModuleDescriptor {
8
- moduleId: string;
9
- name: string | null;
10
- description: string;
11
- documentation: string | null;
12
- inputSchema: Record<string, unknown>;
13
- outputSchema: Record<string, unknown>;
14
- version: string;
15
- tags: string[];
16
- annotations: ModuleAnnotations | null;
17
- examples: ModuleExample[];
18
- metadata: Record<string, unknown>;
19
- }
20
-
21
- export interface DiscoveredModule {
22
- filePath: string;
23
- canonicalId: string;
24
- metaPath: string | null;
25
- namespace: string | null;
26
- }
27
-
28
- export interface DependencyInfo {
29
- moduleId: string;
30
- version: string | null;
31
- optional: boolean;
32
- }
@@ -1,38 +0,0 @@
1
- /**
2
- * Module validation for the registry system.
3
- *
4
- * In TypeScript, modules use TypeBox TSchema (which are plain objects) instead of
5
- * Pydantic BaseModel classes. Duck-type checks validate that the module has
6
- * inputSchema, outputSchema, description, and execute.
7
- */
8
-
9
- export function validateModule(moduleOrClass: unknown): string[] {
10
- const errors: string[] = [];
11
- const obj = moduleOrClass as Record<string, unknown>;
12
-
13
- // Check inputSchema
14
- const inputSchema = obj['inputSchema'] ?? (obj.constructor as unknown as Record<string, unknown>)?.['inputSchema'];
15
- if (inputSchema == null || typeof inputSchema !== 'object') {
16
- errors.push('Missing or invalid inputSchema: must be a TSchema object');
17
- }
18
-
19
- // Check outputSchema
20
- const outputSchema = obj['outputSchema'] ?? (obj.constructor as unknown as Record<string, unknown>)?.['outputSchema'];
21
- if (outputSchema == null || typeof outputSchema !== 'object') {
22
- errors.push('Missing or invalid outputSchema: must be a TSchema object');
23
- }
24
-
25
- // Check description
26
- const description = obj['description'];
27
- if (!description || typeof description !== 'string') {
28
- errors.push('Missing or empty description');
29
- }
30
-
31
- // Check execute
32
- const execute = obj['execute'];
33
- if (execute == null || typeof execute !== 'function') {
34
- errors.push('Missing execute method');
35
- }
36
-
37
- return errors;
38
- }
@@ -1,68 +0,0 @@
1
- /**
2
- * Annotation conflict resolution — merge YAML and code metadata.
3
- */
4
-
5
- import type { ModuleAnnotations, ModuleExample } from '../module.js';
6
- import { DEFAULT_ANNOTATIONS } from '../module.js';
7
-
8
- const ANNOTATION_FIELDS: ReadonlyArray<keyof ModuleAnnotations> = [
9
- 'readonly',
10
- 'destructive',
11
- 'idempotent',
12
- 'requiresApproval',
13
- 'openWorld',
14
- 'streaming',
15
- ];
16
-
17
- export function mergeAnnotations(
18
- yamlAnnotations: Record<string, unknown> | null | undefined,
19
- codeAnnotations: ModuleAnnotations | null | undefined,
20
- ): ModuleAnnotations {
21
- const values: Record<string, unknown> = {};
22
- for (const f of ANNOTATION_FIELDS) {
23
- values[f] = DEFAULT_ANNOTATIONS[f];
24
- }
25
-
26
- if (codeAnnotations != null) {
27
- for (const f of ANNOTATION_FIELDS) {
28
- values[f] = codeAnnotations[f];
29
- }
30
- }
31
-
32
- if (yamlAnnotations != null) {
33
- for (const [key, val] of Object.entries(yamlAnnotations)) {
34
- if ((ANNOTATION_FIELDS as readonly string[]).includes(key)) {
35
- values[key] = val;
36
- }
37
- }
38
- }
39
-
40
- return values as unknown as ModuleAnnotations;
41
- }
42
-
43
- export function mergeExamples(
44
- yamlExamples: Array<Record<string, unknown>> | null | undefined,
45
- codeExamples: ModuleExample[] | null | undefined,
46
- ): ModuleExample[] {
47
- if (yamlExamples != null) {
48
- return yamlExamples.map((d) => ({
49
- title: d['title'] as string,
50
- inputs: (d['inputs'] as Record<string, unknown>) ?? {},
51
- output: (d['output'] as Record<string, unknown>) ?? {},
52
- description: d['description'] as string | undefined,
53
- }));
54
- }
55
- if (codeExamples != null) return codeExamples;
56
- return [];
57
- }
58
-
59
- export function mergeMetadata(
60
- yamlMetadata: Record<string, unknown> | null | undefined,
61
- codeMetadata: Record<string, unknown> | null | undefined,
62
- ): Record<string, unknown> {
63
- const result = codeMetadata != null ? { ...codeMetadata } : {};
64
- if (yamlMetadata != null) {
65
- Object.assign(result, yamlMetadata);
66
- }
67
- return result;
68
- }
@@ -1,90 +0,0 @@
1
- /**
2
- * SchemaExporter — converts schemas to platform-specific export formats.
3
- */
4
-
5
- import type { ModuleAnnotations, ModuleExample } from '../module.js';
6
- import { deepCopy } from '../utils/index.js';
7
- import { applyLlmDescriptions, stripExtensions, toStrictSchema } from './strict.js';
8
- import { ExportProfile, type SchemaDefinition } from './types.js';
9
-
10
- export class SchemaExporter {
11
- export(
12
- schemaDef: SchemaDefinition,
13
- profile: ExportProfile,
14
- annotations?: ModuleAnnotations | null,
15
- examples?: ModuleExample[] | null,
16
- name?: string | null,
17
- ): Record<string, unknown> {
18
- if (profile === ExportProfile.MCP) {
19
- return this.exportMcp(schemaDef, annotations, name);
20
- }
21
- if (profile === ExportProfile.OpenAI) {
22
- return this.exportOpenai(schemaDef);
23
- }
24
- if (profile === ExportProfile.Anthropic) {
25
- return this.exportAnthropic(schemaDef, examples);
26
- }
27
- return this.exportGeneric(schemaDef);
28
- }
29
-
30
- exportMcp(
31
- schemaDef: SchemaDefinition,
32
- annotations?: ModuleAnnotations | null,
33
- name?: string | null,
34
- ): Record<string, unknown> {
35
- return {
36
- name: name ?? schemaDef.moduleId,
37
- description: schemaDef.description,
38
- inputSchema: schemaDef.inputSchema,
39
- annotations: {
40
- readOnlyHint: annotations?.readonly ?? false,
41
- destructiveHint: annotations?.destructive ?? false,
42
- idempotentHint: annotations?.idempotent ?? false,
43
- openWorldHint: annotations?.openWorld ?? true,
44
- },
45
- };
46
- }
47
-
48
- exportOpenai(schemaDef: SchemaDefinition): Record<string, unknown> {
49
- const schema = deepCopy(schemaDef.inputSchema);
50
- applyLlmDescriptions(schema);
51
- const strictSchema = toStrictSchema(schema);
52
- return {
53
- type: 'function',
54
- function: {
55
- name: schemaDef.moduleId.replace(/\./g, '_'),
56
- description: schemaDef.description,
57
- parameters: strictSchema,
58
- strict: true,
59
- },
60
- };
61
- }
62
-
63
- exportAnthropic(
64
- schemaDef: SchemaDefinition,
65
- examples?: ModuleExample[] | null,
66
- ): Record<string, unknown> {
67
- const schema = deepCopy(schemaDef.inputSchema);
68
- applyLlmDescriptions(schema);
69
- stripExtensions(schema);
70
- const result: Record<string, unknown> = {
71
- name: schemaDef.moduleId.replace(/\./g, '_'),
72
- description: schemaDef.description,
73
- input_schema: schema,
74
- };
75
- if (examples && examples.length > 0) {
76
- result['input_examples'] = examples.map((ex) => ex.inputs);
77
- }
78
- return result;
79
- }
80
-
81
- exportGeneric(schemaDef: SchemaDefinition): Record<string, unknown> {
82
- return {
83
- module_id: schemaDef.moduleId,
84
- description: schemaDef.description,
85
- input_schema: schemaDef.inputSchema,
86
- output_schema: schemaDef.outputSchema,
87
- definitions: schemaDef.definitions,
88
- };
89
- }
90
- }
@@ -1,273 +0,0 @@
1
- /**
2
- * SchemaLoader — primary entry point for the schema system.
3
- *
4
- * Uses TypeBox for schema representation. Since TypeBox schemas ARE JSON Schema,
5
- * the conversion layer is minimal.
6
- */
7
-
8
- import { readFileSync, existsSync } from 'node:fs';
9
- import { resolve, join } from 'node:path';
10
- import { Type, type TSchema } from '@sinclair/typebox';
11
- import yaml from 'js-yaml';
12
- import type { Config } from '../config.js';
13
- import { SchemaNotFoundError, SchemaParseError } from '../errors.js';
14
- import { RefResolver } from './ref-resolver.js';
15
- import type { ResolvedSchema, SchemaDefinition } from './types.js';
16
- import { SchemaStrategy } from './types.js';
17
-
18
- export class SchemaLoader {
19
- private _config: Config;
20
- private _schemasDir: string;
21
- private _resolver: RefResolver;
22
- private _schemaCache: Map<string, SchemaDefinition> = new Map();
23
- private _modelCache: Map<string, [ResolvedSchema, ResolvedSchema]> = new Map();
24
-
25
- constructor(config: Config, schemasDir?: string | null) {
26
- this._config = config;
27
- if (schemasDir != null) {
28
- this._schemasDir = resolve(schemasDir);
29
- } else {
30
- this._schemasDir = resolve(config.get('schema.root', './schemas') as string);
31
- }
32
- const maxDepth = (config.get('schema.max_ref_depth', 32) as number);
33
- this._resolver = new RefResolver(this._schemasDir, maxDepth);
34
- }
35
-
36
- load(moduleId: string): SchemaDefinition {
37
- const cached = this._schemaCache.get(moduleId);
38
- if (cached) return cached;
39
-
40
- const filePath = join(this._schemasDir, moduleId.replace(/\./g, '/') + '.schema.yaml');
41
- if (!existsSync(filePath)) {
42
- throw new SchemaNotFoundError(moduleId);
43
- }
44
-
45
- let data: unknown;
46
- try {
47
- data = yaml.load(readFileSync(filePath, 'utf-8'));
48
- } catch (e) {
49
- throw new SchemaParseError(`Invalid YAML in schema for '${moduleId}': ${e}`);
50
- }
51
-
52
- if (data === null || data === undefined || typeof data !== 'object' || Array.isArray(data)) {
53
- throw new SchemaParseError(`Schema file for '${moduleId}' is empty or not a mapping`);
54
- }
55
-
56
- const dataObj = data as Record<string, unknown>;
57
- for (const fieldName of ['input_schema', 'output_schema', 'description']) {
58
- if (!(fieldName in dataObj)) {
59
- throw new SchemaParseError(`Missing required field: ${fieldName} in schema for '${moduleId}'`);
60
- }
61
- }
62
-
63
- const definitions: Record<string, unknown> = {
64
- ...((dataObj['definitions'] as Record<string, unknown>) ?? {}),
65
- ...((dataObj['$defs'] as Record<string, unknown>) ?? {}),
66
- };
67
-
68
- const sd: SchemaDefinition = {
69
- moduleId: (dataObj['module_id'] as string) ?? moduleId,
70
- description: dataObj['description'] as string,
71
- inputSchema: dataObj['input_schema'] as Record<string, unknown>,
72
- outputSchema: dataObj['output_schema'] as Record<string, unknown>,
73
- errorSchema: (dataObj['error_schema'] as Record<string, unknown>) ?? null,
74
- definitions,
75
- version: (dataObj['version'] as string) ?? '1.0.0',
76
- documentation: (dataObj['documentation'] as string) ?? null,
77
- schemaUrl: (dataObj['$schema'] as string) ?? null,
78
- };
79
-
80
- this._schemaCache.set(moduleId, sd);
81
- return sd;
82
- }
83
-
84
- resolve(schemaDef: SchemaDefinition): [ResolvedSchema, ResolvedSchema] {
85
- const resolvedInput = this._resolver.resolve(schemaDef.inputSchema);
86
- const resolvedOutput = this._resolver.resolve(schemaDef.outputSchema);
87
-
88
- const inputSchema = jsonSchemaToTypeBox(resolvedInput);
89
- const outputSchema = jsonSchemaToTypeBox(resolvedOutput);
90
-
91
- const inputRs: ResolvedSchema = {
92
- jsonSchema: resolvedInput,
93
- schema: inputSchema,
94
- moduleId: schemaDef.moduleId,
95
- direction: 'input',
96
- };
97
- const outputRs: ResolvedSchema = {
98
- jsonSchema: resolvedOutput,
99
- schema: outputSchema,
100
- moduleId: schemaDef.moduleId,
101
- direction: 'output',
102
- };
103
- return [inputRs, outputRs];
104
- }
105
-
106
- getSchema(
107
- moduleId: string,
108
- nativeInputSchema?: TSchema | null,
109
- nativeOutputSchema?: TSchema | null,
110
- ): [ResolvedSchema, ResolvedSchema] {
111
- const cached = this._modelCache.get(moduleId);
112
- if (cached) return cached;
113
-
114
- const strategyMap: Record<string, SchemaStrategy> = {
115
- yaml_first: SchemaStrategy.YamlFirst,
116
- native_first: SchemaStrategy.NativeFirst,
117
- yaml_only: SchemaStrategy.YamlOnly,
118
- };
119
- const rawStrategy = this._config.get('schema.strategy', 'yaml_first') as string;
120
- const strategy = strategyMap[rawStrategy] ?? SchemaStrategy.YamlFirst;
121
-
122
- let result: [ResolvedSchema, ResolvedSchema] | null = null;
123
-
124
- if (strategy === SchemaStrategy.YamlFirst) {
125
- try {
126
- result = this._loadAndResolve(moduleId);
127
- } catch (e) {
128
- if (e instanceof SchemaNotFoundError && nativeInputSchema && nativeOutputSchema) {
129
- result = this._wrapNative(moduleId, nativeInputSchema, nativeOutputSchema);
130
- } else {
131
- throw e;
132
- }
133
- }
134
- } else if (strategy === SchemaStrategy.NativeFirst) {
135
- if (nativeInputSchema && nativeOutputSchema) {
136
- result = this._wrapNative(moduleId, nativeInputSchema, nativeOutputSchema);
137
- } else {
138
- result = this._loadAndResolve(moduleId);
139
- }
140
- } else if (strategy === SchemaStrategy.YamlOnly) {
141
- result = this._loadAndResolve(moduleId);
142
- }
143
-
144
- if (result === null) {
145
- throw new SchemaNotFoundError(moduleId);
146
- }
147
-
148
- this._modelCache.set(moduleId, result);
149
- return result;
150
- }
151
-
152
- private _loadAndResolve(moduleId: string): [ResolvedSchema, ResolvedSchema] {
153
- const cached = this._modelCache.get(moduleId);
154
- if (cached) return cached;
155
- const sd = this.load(moduleId);
156
- const result = this.resolve(sd);
157
- this._modelCache.set(moduleId, result);
158
- return result;
159
- }
160
-
161
- private _wrapNative(
162
- moduleId: string,
163
- inputSchema: TSchema,
164
- outputSchema: TSchema,
165
- ): [ResolvedSchema, ResolvedSchema] {
166
- const inputRs: ResolvedSchema = {
167
- jsonSchema: inputSchema as unknown as Record<string, unknown>,
168
- schema: inputSchema,
169
- moduleId,
170
- direction: 'input',
171
- };
172
- const outputRs: ResolvedSchema = {
173
- jsonSchema: outputSchema as unknown as Record<string, unknown>,
174
- schema: outputSchema,
175
- moduleId,
176
- direction: 'output',
177
- };
178
- return [inputRs, outputRs];
179
- }
180
-
181
- clearCache(): void {
182
- this._schemaCache.clear();
183
- this._modelCache.clear();
184
- this._resolver.clearCache();
185
- }
186
- }
187
-
188
- /**
189
- * Convert a JSON Schema dict to a TypeBox TSchema.
190
- * Since TypeBox schemas ARE JSON Schema, this wraps the raw object
191
- * so it can be used with Value.Check/Value.Decode.
192
- */
193
- export function jsonSchemaToTypeBox(schema: Record<string, unknown>): TSchema {
194
- const schemaType = schema['type'] as string | undefined;
195
-
196
- let result: TSchema;
197
- if (schemaType === 'object') result = convertObjectSchema(schema);
198
- else if (schemaType === 'array') result = convertArraySchema(schema);
199
- else if (schemaType === 'string') result = convertStringSchema(schema);
200
- else if (schemaType === 'integer') result = convertNumericSchema(schema, Type.Integer);
201
- else if (schemaType === 'number') result = convertNumericSchema(schema, Type.Number);
202
- else if (schemaType === 'boolean') result = Type.Boolean();
203
- else if (schemaType === 'null') result = Type.Null();
204
- else result = convertCombinatorSchema(schema);
205
-
206
- // Preserve JSON Schema metadata
207
- if (typeof schema['description'] === 'string') result['description'] = schema['description'];
208
- if (typeof schema['title'] === 'string') result['title'] = schema['title'];
209
-
210
- return result;
211
- }
212
-
213
- function convertObjectSchema(schema: Record<string, unknown>): TSchema {
214
- const properties = schema['properties'] as Record<string, Record<string, unknown>> | undefined;
215
- const required = new Set((schema['required'] as string[]) ?? []);
216
-
217
- if (properties) {
218
- const typeboxProps: Record<string, TSchema> = {};
219
- for (const [name, propSchema] of Object.entries(properties)) {
220
- const propType = jsonSchemaToTypeBox(propSchema);
221
- typeboxProps[name] = required.has(name) ? propType : Type.Optional(propType);
222
- }
223
- return Type.Object(typeboxProps);
224
- }
225
- return Type.Record(Type.String(), Type.Unknown());
226
- }
227
-
228
- function convertArraySchema(schema: Record<string, unknown>): TSchema {
229
- const items = schema['items'] as Record<string, unknown> | undefined;
230
- return items ? Type.Array(jsonSchemaToTypeBox(items)) : Type.Array(Type.Unknown());
231
- }
232
-
233
- function convertStringSchema(schema: Record<string, unknown>): TSchema {
234
- const opts: Record<string, unknown> = {};
235
- for (const key of ['minLength', 'maxLength', 'pattern', 'format']) {
236
- if (key in schema) opts[key] = schema[key];
237
- }
238
- return Type.String(opts);
239
- }
240
-
241
- function convertNumericSchema(
242
- schema: Record<string, unknown>,
243
- factory: (opts?: Record<string, unknown>) => TSchema,
244
- ): TSchema {
245
- const opts: Record<string, unknown> = {};
246
- for (const key of ['minimum', 'maximum', 'exclusiveMinimum', 'exclusiveMaximum', 'multipleOf']) {
247
- if (key in schema) opts[key] = schema[key];
248
- }
249
- return factory(opts);
250
- }
251
-
252
- function convertCombinatorSchema(schema: Record<string, unknown>): TSchema {
253
- if ('enum' in schema) {
254
- const values = schema['enum'] as unknown[];
255
- return Type.Union(values.map((v) =>
256
- v === null ? Type.Null() : Type.Literal(v as string | number | boolean),
257
- ));
258
- }
259
- if ('const' in schema) {
260
- const value = schema['const'];
261
- return value === null ? Type.Null() : Type.Literal(value as string | number | boolean);
262
- }
263
- if ('oneOf' in schema) {
264
- return Type.Union((schema['oneOf'] as Record<string, unknown>[]).map(jsonSchemaToTypeBox));
265
- }
266
- if ('anyOf' in schema) {
267
- return Type.Union((schema['anyOf'] as Record<string, unknown>[]).map(jsonSchemaToTypeBox));
268
- }
269
- if ('allOf' in schema) {
270
- return Type.Intersect((schema['allOf'] as Record<string, unknown>[]).map(jsonSchemaToTypeBox));
271
- }
272
- return Type.Unknown();
273
- }