@schlenkr/ragents 0.1.15 → 0.1.16

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.
@@ -13,7 +13,7 @@ import { runtimeLibraries, webRuntimeLibraries } from "./runtime-libraries.js";
13
13
  const webRequire = createRequire(new URL("../../../../web/package.json", import.meta.url));
14
14
  const rootRequire = createRequire(new URL("../../../../../package.json", import.meta.url));
15
15
  const serverRequire = createRequire(new URL("../../../package.json", import.meta.url));
16
- const serverTypeRoot = fileURLToPath(new URL("../../../node_modules/@types", import.meta.url));
16
+ const serverTypeRoot = path.dirname(path.dirname(serverRequire.resolve("@types/node/package.json")));
17
17
  const libraryDirectory = (name: string): string => {
18
18
  const resolver = (webRuntimeLibraries as readonly string[]).includes(name) ? webRequire
19
19
  : name === "@types/node" || name === "tsx" ? serverRequire : rootRequire;
@@ -1,16 +1,55 @@
1
+ import { realpathSync } from "node:fs";
1
2
  import path from "node:path";
2
3
  import { fileURLToPath } from "node:url";
3
4
  import ts from "typescript";
4
5
 
5
- function readContracts(repoRoot: string): { names: string[]; files: Record<string, string> } {
6
- const filename = path.join(repoRoot, "apps/web/src/actor-programs/client-ui/contracts.d.ts");
6
+ export function clientUiContractPaths(directory: string, paths: typeof path.posix = path) {
7
+ const canonical = (name: string) => paths.resolve(name).replaceAll(paths.sep, "/").replace(/^[a-z]:/, (drive) => drive.toUpperCase());
8
+ const repoRoot = canonical(directory);
9
+ const root = canonical(paths.join(paths.parse(repoRoot).root, "__ragents_ui_contract__"));
10
+ const contains = (base: string, name: string) => name === base || name.startsWith(`${base}/`);
11
+ const physical = (name: string) => {
12
+ const file = canonical(name);
13
+ return paths.normalize(contains(root, file) ? repoRoot + file.slice(root.length) : file);
14
+ };
15
+ const virtual = (name: string) => {
16
+ const file = canonical(name);
17
+ return contains(repoRoot, file) ? root + file.slice(repoRoot.length) : file;
18
+ };
19
+ return { root, physical, virtual, isVirtual: (name: string) => contains(root, canonical(name)) };
20
+ }
21
+
22
+ function readContracts(directory: string): { names: string[]; files: Record<string, string> } {
23
+ const { root, physical, virtual, isVirtual } = clientUiContractPaths(realpathSync(directory));
24
+ const filename = `${root}/apps/web/src/actor-programs/client-ui/contracts.d.ts`;
7
25
  const options: ts.CompilerOptions = {
8
26
  target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.ESNext,
9
27
  moduleResolution: ts.ModuleResolutionKind.Bundler, jsx: ts.JsxEmit.ReactJSX,
10
28
  strict: true, skipLibCheck: true, esModuleInterop: true,
11
29
  declaration: true, emitDeclarationOnly: true,
12
30
  };
13
- const program = ts.createProgram([filename], options);
31
+ // TypeScript must see public sources outside node_modules, including during declaration naming.
32
+ const host = ts.createCompilerHost(options);
33
+ host.readFile = (name) => ts.sys.readFile(physical(name));
34
+ host.fileExists = (name) => ts.sys.fileExists(physical(name));
35
+ host.directoryExists = (name) => ts.sys.directoryExists(physical(name));
36
+ host.getDirectories = (name) => ts.sys.getDirectories(physical(name));
37
+ host.realpath = (name) => virtual(ts.sys.realpath!(physical(name)));
38
+ host.getSourceFile = (name, languageVersion) => {
39
+ const text = host.readFile(name);
40
+ return text === undefined ? undefined : ts.createSourceFile(virtual(name), text, languageVersion, true);
41
+ };
42
+ const resolveModule = (name: string, containingFile: string) => {
43
+ const mapped = ts.resolveModuleName(name, containingFile, options, host).resolvedModule;
44
+ if (mapped) return mapped;
45
+ const resolved = ts.resolveModuleName(name, physical(containingFile), options, ts.sys).resolvedModule;
46
+ if (!resolved) return undefined;
47
+ const resolvedFileName = virtual(resolved.resolvedFileName);
48
+ return { ...resolved, resolvedFileName, isExternalLibraryImport: isVirtual(resolvedFileName)
49
+ ? path.posix.relative(root, resolvedFileName).split("/").includes("node_modules") : resolved.isExternalLibraryImport };
50
+ };
51
+ host.resolveModuleNames = (names, containingFile) => names.map((name) => resolveModule(name, containingFile));
52
+ const program = ts.createProgram([filename], options, host);
14
53
  const source = program.getSourceFile(filename);
15
54
  if (!source) throw new Error("The public UI contract is missing.");
16
55
  const checker = program.getTypeChecker();
@@ -27,8 +66,8 @@ function readContracts(repoRoot: string): { names: string[]; files: Record<strin
27
66
  const collect = (file: ts.SourceFile) => {
28
67
  if (visited.has(file.fileName)) return;
29
68
  visited.add(file.fileName);
30
- const relative = path.relative(repoRoot, file.fileName).replaceAll(path.sep, "/");
31
- if (relative.startsWith("../") || relative.includes("node_modules/")) throw new Error("A local UI type reference leaves the public repository sources.");
69
+ const relative = path.posix.relative(root, file.fileName);
70
+ if (!isVirtual(file.fileName) || relative.includes("node_modules/")) throw new Error("A local UI type reference leaves the public repository sources.");
32
71
  diagnostics.push(...program.getSyntacticDiagnostics(file), ...program.getSemanticDiagnostics(file));
33
72
  let text = file.isDeclarationFile ? file.text : "";
34
73
  if (!file.isDeclarationFile) {
@@ -41,13 +80,13 @@ function readContracts(repoRoot: string): { names: string[]; files: Record<strin
41
80
  const references = ts.preProcessFile(text, true, true);
42
81
  for (const reference of references.importedFiles) {
43
82
  if (!reference.fileName.startsWith(".")) continue;
44
- const resolved = ts.resolveModuleName(reference.fileName, file.fileName, options, ts.sys).resolvedModule;
83
+ const resolved = resolveModule(reference.fileName, file.fileName);
45
84
  const dependency = resolved && program.getSourceFile(resolved.resolvedFileName);
46
85
  if (!dependency) throw new Error(`Unresolvable local UI type reference: ${relative}: ${reference.fileName}`);
47
86
  collect(dependency);
48
87
  }
49
88
  for (const reference of declaration.referencedFiles) {
50
- const dependency = program.getSourceFile(path.resolve(path.dirname(file.fileName), reference.fileName));
89
+ const dependency = program.getSourceFile(path.posix.resolve(path.posix.dirname(file.fileName), reference.fileName));
51
90
  if (!dependency) throw new Error(`Unresolvable UI type file reference: ${relative}: ${reference.fileName}`);
52
91
  collect(dependency);
53
92
  }
@@ -76,7 +115,7 @@ export function readClientUiComponentContracts(component: string, repoRoot?: str
76
115
  }
77
116
 
78
117
  export function readClientUiExportContracts(component: string, repoRoot?: string): Record<string, string> {
79
- const root = repoRoot ?? fileURLToPath(new URL("../../../../../", import.meta.url));
118
+ const root = realpathSync(repoRoot ?? fileURLToPath(new URL("../../../../../", import.meta.url)));
80
119
  const catalog = contracts(repoRoot);
81
120
  const files = new Map(Object.entries(catalog.files).map(([name, text]) => [path.resolve(root, name), text]));
82
121
  const options: ts.CompilerOptions = { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.ESNext, moduleResolution: ts.ModuleResolutionKind.Bundler, strict: true, skipLibCheck: true };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@schlenkr/ragents",
3
- "version": "0.1.15",
3
+ "version": "0.1.16",
4
4
  "description": "RAgents host: server, engine, plugins and tools without a source checkout",
5
5
  "keywords": [
6
6
  "agents",
@@ -70,6 +70,6 @@
70
70
  "yaml": "2.9.0"
71
71
  },
72
72
  "ragents": {
73
- "hostVersion": "9d43afb1f899a9b2bf01b46d9173d1ea901c9088"
73
+ "hostVersion": "cbd2d641afb2ae43222b6a82d08f37f5e0cd1b74"
74
74
  }
75
75
  }