@arcforge/platform 2.0.219 → 2.0.244

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 (127) hide show
  1. package/package.json +27 -19
  2. package/src/build/blueprint/blueprint.ts +48 -64
  3. package/src/build/blueprint/cognet/abi.ts +3 -257
  4. package/src/build/blueprint/cognet/bundle-worker.ts +11 -0
  5. package/src/build/blueprint/cognet/bundle.ts +294 -89
  6. package/src/build/blueprint/cognet/cognet.ts +8 -7
  7. package/src/build/blueprint/cognet/config/config.ts +52 -0
  8. package/src/build/blueprint/cognet/config/index.ts +4 -0
  9. package/src/build/blueprint/cognet/config/literal.ts +83 -0
  10. package/src/build/blueprint/cognet/config/provider.ts +43 -0
  11. package/src/build/blueprint/cognet/config/shape.ts +183 -0
  12. package/src/build/blueprint/cognet/index.ts +4 -2
  13. package/src/build/blueprint/cognet/native/cargo.ts +31 -0
  14. package/src/build/blueprint/cognet/native/crate.ts +126 -0
  15. package/src/build/blueprint/cognet/native/index.ts +2 -0
  16. package/src/build/blueprint/cognet/native/library.ts +125 -0
  17. package/src/build/blueprint/cognet/native/manifest.ts +137 -0
  18. package/src/build/blueprint/cognet/python/index.ts +1 -0
  19. package/src/build/blueprint/cognet/python/project.ts +119 -0
  20. package/src/build/blueprint/cognet/schema/arrays.ts +23 -0
  21. package/src/build/blueprint/cognet/schema/authored.ts +273 -0
  22. package/src/build/blueprint/cognet/schema/compile.ts +78 -0
  23. package/src/build/blueprint/cognet/schema/components/external.ts +15 -0
  24. package/src/build/blueprint/cognet/schema/components/index.ts +3 -0
  25. package/src/build/blueprint/cognet/schema/components/lock.ts +58 -0
  26. package/src/build/blueprint/cognet/schema/components/model.ts +34 -0
  27. package/src/build/blueprint/cognet/schema/components/python.ts +15 -0
  28. package/src/build/blueprint/cognet/schema/components/python_source.py +69 -0
  29. package/src/build/blueprint/cognet/schema/components/rust.ts +33 -0
  30. package/src/build/blueprint/cognet/schema/components/rust_source/Cargo.lock +117 -0
  31. package/src/build/blueprint/cognet/schema/components/rust_source/Cargo.toml +11 -0
  32. package/src/build/blueprint/cognet/schema/components/rust_source/src/main.rs +104 -0
  33. package/src/build/blueprint/cognet/schema/components/source.ts +220 -0
  34. package/src/build/blueprint/cognet/schema/components/tags.ts +66 -0
  35. package/src/build/blueprint/cognet/schema/components/typescript.ts +157 -0
  36. package/src/build/blueprint/cognet/schema/index.ts +5 -0
  37. package/src/build/blueprint/cognet/schema/interfaces/index.ts +3 -0
  38. package/src/build/blueprint/cognet/schema/interfaces/source.ts +218 -0
  39. package/src/build/blueprint/cognet/schema/python.ts +260 -0
  40. package/src/build/blueprint/cognet/schema/rust.ts +316 -0
  41. package/src/build/blueprint/cognet/schema/schema.ts +248 -0
  42. package/src/build/blueprint/cognet/schema/typescript.ts +352 -0
  43. package/src/build/blueprint/index.ts +1 -1
  44. package/src/build/blueprint/modules/index.ts +7 -5
  45. package/src/build/blueprint/modules/meta.ts +47 -3
  46. package/src/build/blueprint/scan/config.ts +22 -16
  47. package/src/build/blueprint/scan/provider-globals.ts +46 -0
  48. package/src/build/blueprint/scan/providers.ts +25 -0
  49. package/src/build/blueprint/scan/tools.ts +21 -15
  50. package/src/build/extensions/edit.ts +66 -2
  51. package/src/build/extensions/extensions.ts +16 -3
  52. package/src/build/extensions/index.ts +1 -1
  53. package/src/build/project/bundle/agent.ts +26 -17
  54. package/src/build/project/bundle/source.ts +2 -2
  55. package/src/build/project/create/cognet.ts +84 -33
  56. package/src/build/project/create/profile.ts +13 -3
  57. package/src/build/project/index.ts +1 -1
  58. package/src/build/project/installer.ts +6 -5
  59. package/src/build/project/kinds.ts +4 -0
  60. package/src/build/project/manifest/env.ts +10 -0
  61. package/src/build/project/models/blobs/blobs.ts +57 -0
  62. package/src/build/project/models/blobs/index.ts +1 -0
  63. package/src/build/project/models/fetch.ts +46 -51
  64. package/src/build/project/models/index.ts +2 -1
  65. package/src/build/project/models/models.ts +12 -38
  66. package/src/build/project/models/onnx.ts +82 -0
  67. package/src/build/project/models/specifier.ts +9 -3
  68. package/src/build/project/models/storage/index-store.ts +27 -0
  69. package/src/build/project/models/storage/index.ts +1 -0
  70. package/src/build/project/models/store.ts +21 -16
  71. package/src/build/project/prepare.ts +82 -27
  72. package/src/build/project/project.ts +8 -14
  73. package/src/build/project/projects.ts +12 -1
  74. package/src/build/project/tree.ts +13 -3
  75. package/src/build/project/treecache.ts +8 -4
  76. package/src/build/project/typegen/axon-dts.ts +18 -0
  77. package/src/build/project/typegen/cognet-dts.ts +248 -59
  78. package/src/build/project/typegen/prompt-dts.ts +2 -3
  79. package/src/build/project/typegen/registry-dts.ts +2 -2
  80. package/src/build/project/typegen/tsconfig.ts +13 -13
  81. package/src/build/project/typegen/tui-dts.ts +3 -4
  82. package/src/build/project/typegen/workspace-scripts.ts +8 -7
  83. package/src/build/project/typegen/write.ts +15 -2
  84. package/src/build/project/watcher.ts +27 -0
  85. package/src/build/runtime/agent.ts +102 -63
  86. package/src/build/runtime/conformance.ts +25 -0
  87. package/src/build/runtime/escalations.ts +3 -8
  88. package/src/build/runtime/index.ts +2 -0
  89. package/src/build/runtime/instances.ts +7 -4
  90. package/src/build/runtime/progress.ts +6 -2
  91. package/src/build/runtime/reload-watch.ts +11 -8
  92. package/src/debug/README.md +398 -0
  93. package/src/debug/debug.ts +101 -0
  94. package/src/debug/events.ts +156 -0
  95. package/src/debug/graph.ts +94 -0
  96. package/src/debug/index.ts +5 -0
  97. package/src/debug/indexer.ts +286 -0
  98. package/src/debug/inference.ts +53 -0
  99. package/src/debug/log.ts +68 -0
  100. package/src/debug/overview.ts +78 -0
  101. package/src/debug/perf.ts +75 -0
  102. package/src/debug/resources.ts +125 -0
  103. package/src/debug/ring.ts +31 -0
  104. package/src/debug/sessions.ts +90 -0
  105. package/src/debug/signs.ts +119 -0
  106. package/src/debug/tally.ts +44 -0
  107. package/src/debug/types.ts +146 -0
  108. package/src/index.ts +9 -0
  109. package/src/link/agent-main.ts +36 -10
  110. package/src/link/index.ts +1 -1
  111. package/src/link/services.ts +88 -57
  112. package/src/platform.ts +17 -25
  113. package/src/services/cloud.ts +1 -4
  114. package/src/services/control/client.ts +1 -1
  115. package/src/services/control/server.ts +1 -1
  116. package/src/services/index.ts +1 -0
  117. package/src/services/ollama/daemon.ts +23 -0
  118. package/src/services/ollama/http.ts +7 -1
  119. package/src/services/ollama/ollama.ts +8 -14
  120. package/src/services/probe/ask.ts +139 -0
  121. package/src/services/probe/index.ts +3 -0
  122. package/src/services/providers.ts +18 -0
  123. package/src/services/resources/hardware.ts +15 -18
  124. package/src/services/resources/resources.ts +4 -5
  125. package/src/services/store/types.ts +3 -0
  126. package/src/services/running/index.ts +0 -1
  127. package/src/services/running/running.ts +0 -216
@@ -0,0 +1,15 @@
1
+ import { readFileSync } from "node:fs"
2
+ import type { Source } from "../compile"
3
+ import { fail } from "./model"
4
+
5
+ export type External = { name: string; package: string; named: string; absPath: string }
6
+
7
+ /** Top-level message and enum names already supplied by annotated TS or .proto schemas. */
8
+ export function external(sources: readonly Source[]): External[] {
9
+ return sources.flatMap(source => {
10
+ const content = source.content ?? readFileSync(source.absPath, "utf8")
11
+ const pkg = content.match(/^package\s+([A-Za-z_]\w*(?:\.[A-Za-z_]\w*)*)\s*;/m)?.[1]
12
+ if (!pkg) fail(source.absPath, "legacy schema has no protobuf package")
13
+ return [...content.matchAll(/^(?:message|enum)\s+([A-Za-z_]\w*)\s*\{/gm)].map(match => ({ name: match[1]!, package: pkg, named: source.named, absPath: source.absPath }))
14
+ })
15
+ }
@@ -0,0 +1,3 @@
1
+ export { Components, fieldType, protoName } from "./source"
2
+ export { typescript } from "./typescript"
3
+ export { fail, type File, type Shape, type Contract, type Method, type Answer } from "./model"
@@ -0,0 +1,58 @@
1
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"
2
+ import { dirname } from "node:path"
3
+ import { err } from "@arcforge/err"
4
+
5
+ type Entry = { number: number; signature: string; removed?: true }
6
+ type Image = { version: 1; messages: Record<string, Record<string, Entry>> }
7
+
8
+ /** Protobuf numbers and tombstones retained in the agent frame between prepares. */
9
+ export function Lock(opts: { path: string }) {
10
+ let loaded: Image | null = null
11
+ const image = (): Image => {
12
+ if (loaded) return loaded
13
+ try { loaded = existsSync(opts.path) ? JSON.parse(readFileSync(opts.path, "utf8")) as Image : { version: 1, messages: {} } }
14
+ catch (cause) { throw err("COGNET_SCHEMA_INVALID", { detail: `${opts.path}: invalid JSON in component schema lock: ${String(cause)}` }) }
15
+ if (loaded.version !== 1 || !loaded.messages || typeof loaded.messages !== "object" || Array.isArray(loaded.messages)) throw err("COGNET_SCHEMA_INVALID", { detail: `${opts.path}: invalid component schema lock` })
16
+ for (const [owner, fields] of Object.entries(loaded.messages)) {
17
+ if (!fields || typeof fields !== "object" || Array.isArray(fields)) throw err("COGNET_SCHEMA_INVALID", { detail: `${opts.path}: invalid fields for ${owner}` })
18
+ const used = new Set<number>()
19
+ for (const [name, field] of Object.entries(fields)) {
20
+ if (!field || !Number.isSafeInteger(field.number) || field.number < 1 || typeof field.signature !== "string" || used.has(field.number)) throw err("COGNET_SCHEMA_INVALID", { detail: `${opts.path}: invalid field ${owner}.${name}` })
21
+ used.add(field.number)
22
+ }
23
+ }
24
+ return loaded
25
+ }
26
+ return {
27
+ field(owner: string, name: string, signature: string): number {
28
+ const fields = image().messages[owner] ??= {}
29
+ const current = fields[name]
30
+ if (current?.removed) throw err("COGNET_SCHEMA_INVALID", { detail: `${owner}.${name}: this field was removed; give the new field a new name` })
31
+ if (current && current.signature !== signature) throw err("COGNET_SCHEMA_INVALID", { detail: `${owner}.${name}: wire type changed from ${current.signature} to ${signature}; migrate the component explicitly` })
32
+ if (current) return current.number
33
+ let next = Math.max(0, ...Object.values(fields).map(field => field.number)) + 1
34
+ if (next >= 19000 && next <= 19999) next = 20000
35
+ if (next > 536870911) throw err("COGNET_SCHEMA_INVALID", { detail: `${owner}: protobuf has no field numbers left` })
36
+ fields[name] = { number: next, signature: signature }
37
+ return next
38
+ },
39
+ finish(owner: string, active: string[]): number[] {
40
+ const fields = image().messages[owner] ??= {}
41
+ const present = new Set(active)
42
+ for (const [name, entry] of Object.entries(fields)) if (!present.has(name)) entry.removed = true
43
+ return Object.values(fields).filter(entry => entry.removed).map(entry => entry.number).sort((a, b) => a - b)
44
+ },
45
+ finishAll(active: string[]): void {
46
+ const present = new Set(active)
47
+ for (const [owner, fields] of Object.entries(image().messages)) {
48
+ if (present.has(owner)) continue
49
+ for (const entry of Object.values(fields)) entry.removed = true
50
+ }
51
+ },
52
+ save(): void {
53
+ const data = `${JSON.stringify(image(), null, 2)}\n`
54
+ mkdirSync(dirname(opts.path), { recursive: true })
55
+ if (!existsSync(opts.path) || readFileSync(opts.path, "utf8") !== data) writeFileSync(opts.path, data)
56
+ },
57
+ }
58
+ }
@@ -0,0 +1,34 @@
1
+ import { err } from "@arcforge/err"
2
+
3
+ export type Field = { name: string; type: string; optional: boolean; nullable?: boolean }
4
+ export type Shape = { name: string; fields: Field[]; component: boolean; alias?: string; doc?: string; handle?: true }
5
+ /** What a method answers: a named type, fields written inline, or nothing. */
6
+ export type Answer = { named: string } | { inline: Field[] } | null
7
+ /** One method of an interface. `describe` is the provider reporting itself — every interface has one. */
8
+ export type Method = { name: string; doc?: string; params: Field[]; kind: "describe" | "call" | "stream" | "session"; answer: Answer; push?: string }
9
+ /** An interface's contract: the one exported interface in its file with methods. */
10
+ export type Contract = { name: string; doc?: string; methods: Method[] }
11
+ export type Import = { path: string; name: string }
12
+ export type File = { absPath: string; relPath: string; named: string; package: string; shapes: Shape[]; imports: Record<string, Import> }
13
+
14
+ /** Validate a parser process's JSON before the schema compiler trusts it. */
15
+ export function parseShapes(path: string, output: string): Shape[] {
16
+ let value: unknown
17
+ try { value = JSON.parse(output) }
18
+ catch { fail(path, "component parser returned invalid JSON") }
19
+ if (!Array.isArray(value)) fail(path, "component parser returned no declaration list")
20
+ return value.map((shape: unknown) => {
21
+ if (!record(shape) || typeof shape.name !== "string" || typeof shape.component !== "boolean" || !Array.isArray(shape.fields) || (shape.doc !== undefined && shape.doc !== null && typeof shape.doc !== "string")) fail(path, "component parser returned an invalid declaration")
22
+ const fields = shape.fields.map((field: unknown) => {
23
+ if (!record(field) || typeof field.name !== "string" || typeof field.type !== "string" || typeof field.optional !== "boolean" || (field.nullable !== undefined && typeof field.nullable !== "boolean")) fail(path, `${shape.name}: parser returned an invalid field`)
24
+ return { name: field.name, type: field.type, optional: field.optional, nullable: field.nullable }
25
+ })
26
+ return { name: shape.name, fields: fields, component: shape.component, doc: shape.doc ?? undefined }
27
+ })
28
+ }
29
+
30
+ function record(value: unknown): value is Record<string, unknown> { return !!value && typeof value === "object" && !Array.isArray(value) }
31
+
32
+ export function fail(file: string, detail: string): never {
33
+ throw err("COGNET_SCHEMA_INVALID", { detail: `${file}: ${detail}` })
34
+ }
@@ -0,0 +1,15 @@
1
+ import { fail, parseShapes, type Shape } from "./model"
2
+ import { fileURLToPath } from "node:url"
3
+
4
+ /** Python's own AST reads annotations without importing or executing source code. */
5
+ export function PythonSource(_opts: {}) {
6
+ return {
7
+ read(path: string): Shape[] {
8
+ let run: ReturnType<typeof Bun.spawnSync>
9
+ try { run = Bun.spawnSync(["python3", "-I", fileURLToPath(new URL("./python_source.py", import.meta.url)), path], { stdout: "pipe", stderr: "pipe" }) }
10
+ catch (cause) { fail(path, `cannot start Python component parser: ${String(cause)}`) }
11
+ if (run.exitCode !== 0) fail(path, new TextDecoder().decode(run.stderr).trim() || "Python component parser failed")
12
+ return parseShapes(path, new TextDecoder().decode(run.stdout))
13
+ },
14
+ }
15
+ }
@@ -0,0 +1,69 @@
1
+ """Read Python component declarations without importing or executing them."""
2
+
3
+ import ast
4
+ import json
5
+ import sys
6
+
7
+
8
+ def fail(node: ast.AST, detail: str) -> None:
9
+ raise ValueError(f"line {getattr(node, 'lineno', '?')}: {detail}")
10
+
11
+
12
+ def annotation(node: ast.expr) -> str:
13
+ if isinstance(node, ast.Name):
14
+ builtins = {"float": "f64", "str": "string", "bool": "boolean", "int": "i32"}
15
+ return builtins.get(node.id, node.id)
16
+ if isinstance(node, ast.Subscript) and isinstance(node.value, ast.Name):
17
+ name = node.value.id
18
+ if name in ("list", "List"):
19
+ return f"Vec<{annotation(node.slice)}>"
20
+ if name in ("dict", "Dict") and isinstance(node.slice, ast.Tuple) and len(node.slice.elts) == 2:
21
+ return f"Record<{annotation(node.slice.elts[0])},{annotation(node.slice.elts[1])}>"
22
+ if name in ("tuple", "Tuple") and isinstance(node.slice, ast.Tuple) and node.slice.elts:
23
+ elements = [annotation(element) for element in node.slice.elts]
24
+ if len(set(elements)) != 1:
25
+ fail(node, "fixed tuples must use one element type")
26
+ return f"Fixed<{elements[0]},{len(elements)}>"
27
+ fail(node, f"unsupported Python field type {ast.unparse(node)}")
28
+
29
+
30
+ def field_annotation(node: ast.expr) -> tuple[str, bool]:
31
+ if isinstance(node, ast.Subscript) and isinstance(node.value, ast.Name) and node.value.id == "Optional":
32
+ return annotation(node.slice), True
33
+ if isinstance(node, ast.BinOp) and isinstance(node.op, ast.BitOr):
34
+ if isinstance(node.right, ast.Constant) and node.right.value is None:
35
+ return annotation(node.left), True
36
+ if isinstance(node.left, ast.Constant) and node.left.value is None:
37
+ return annotation(node.right), True
38
+ return annotation(node), False
39
+
40
+
41
+ def read(path: str) -> list[dict]:
42
+ tree = ast.parse(open(path, encoding="utf8").read(), filename=path)
43
+ shapes = []
44
+ for node in tree.body:
45
+ if isinstance(node, (ast.Import, ast.ImportFrom)):
46
+ continue
47
+ if not isinstance(node, ast.ClassDef):
48
+ fail(node, "component files may contain only imports and annotated classes")
49
+ if node.bases or node.decorator_list or node.keywords:
50
+ fail(node, f"{node.name}: inheritance and decorators are not part of the component schema")
51
+ fields = []
52
+ for item in node.body:
53
+ if isinstance(item, ast.Expr) and isinstance(item.value, ast.Constant) and isinstance(item.value.value, str):
54
+ continue
55
+ if isinstance(item, ast.Pass):
56
+ continue
57
+ if not isinstance(item, ast.AnnAssign) or not isinstance(item.target, ast.Name) or item.value is not None:
58
+ fail(item, f"{node.name}: only annotated fields without defaults are supported")
59
+ kind, nullable = field_annotation(item.annotation)
60
+ fields.append({"name": item.target.id, "type": kind, "optional": False, "nullable": nullable})
61
+ shapes.append({"name": node.name, "fields": fields, "component": not node.name.startswith("_"), "doc": ast.get_docstring(node)})
62
+ return shapes
63
+
64
+
65
+ try:
66
+ print(json.dumps(read(sys.argv[1])))
67
+ except (SyntaxError, ValueError) as error:
68
+ print(error, file=sys.stderr)
69
+ sys.exit(1)
@@ -0,0 +1,33 @@
1
+ import { createHash } from "node:crypto"
2
+ import { readFileSync } from "node:fs"
3
+ import { tmpdir } from "node:os"
4
+ import { join } from "node:path"
5
+ import { fileURLToPath } from "node:url"
6
+ import { fail, parseShapes, type Shape } from "./model"
7
+
8
+ const MANIFEST = fileURLToPath(new URL("./rust_source/Cargo.toml", import.meta.url))
9
+ const PARSER = fileURLToPath(new URL("./rust_source/src/main.rs", import.meta.url))
10
+ const LOCK = fileURLToPath(new URL("./rust_source/Cargo.lock", import.meta.url))
11
+
12
+ /** `syn` parses Rust's syntax; an unsupported declaration fails at prepare. */
13
+ export function RustSource(_opts: {}) {
14
+ let binary: string | null = null
15
+ return {
16
+ read(path: string): Shape[] {
17
+ if (!binary) {
18
+ const version = createHash("sha256").update(readFileSync(MANIFEST)).update(readFileSync(PARSER)).update(readFileSync(LOCK)).digest("hex").slice(0, 16)
19
+ const target = join(tmpdir(), `axon-component-rust-parser-${version}`)
20
+ let build: ReturnType<typeof Bun.spawnSync>
21
+ try { build = Bun.spawnSync(["cargo", "build", "--locked", "--quiet", "--manifest-path", MANIFEST, "--target-dir", target], { stderr: "pipe", stdout: "pipe" }) }
22
+ catch (cause) { fail(path, `cannot start Cargo for Rust component parsing: ${String(cause)}`) }
23
+ if (build.exitCode !== 0) fail(path, `Rust component parser could not build: ${new TextDecoder().decode(build.stderr).trim()}`)
24
+ binary = join(target, "debug", "cognet-component-source")
25
+ }
26
+ let run: ReturnType<typeof Bun.spawnSync>
27
+ try { run = Bun.spawnSync([binary, path], { stderr: "pipe", stdout: "pipe" }) }
28
+ catch (cause) { fail(path, `cannot start Rust component parser: ${String(cause)}`) }
29
+ if (run.exitCode !== 0) fail(path, new TextDecoder().decode(run.stderr).trim() || "Rust component parser failed")
30
+ return parseShapes(path, new TextDecoder().decode(run.stdout))
31
+ },
32
+ }
33
+ }
@@ -0,0 +1,117 @@
1
+ # This file is automatically @generated by Cargo.
2
+ # It is not intended for manual editing.
3
+ version = 4
4
+
5
+ [[package]]
6
+ name = "cognet-component-source"
7
+ version = "0.1.0"
8
+ dependencies = [
9
+ "serde_json",
10
+ "syn 2.0.119",
11
+ ]
12
+
13
+ [[package]]
14
+ name = "itoa"
15
+ version = "1.0.18"
16
+ source = "registry+https://github.com/rust-lang/crates.io-index"
17
+ checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
18
+
19
+ [[package]]
20
+ name = "memchr"
21
+ version = "2.8.3"
22
+ source = "registry+https://github.com/rust-lang/crates.io-index"
23
+ checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
24
+
25
+ [[package]]
26
+ name = "proc-macro2"
27
+ version = "1.0.107"
28
+ source = "registry+https://github.com/rust-lang/crates.io-index"
29
+ checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
30
+ dependencies = [
31
+ "unicode-ident",
32
+ ]
33
+
34
+ [[package]]
35
+ name = "quote"
36
+ version = "1.0.47"
37
+ source = "registry+https://github.com/rust-lang/crates.io-index"
38
+ checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
39
+ dependencies = [
40
+ "proc-macro2",
41
+ ]
42
+
43
+ [[package]]
44
+ name = "serde"
45
+ version = "1.0.229"
46
+ source = "registry+https://github.com/rust-lang/crates.io-index"
47
+ checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba"
48
+ dependencies = [
49
+ "serde_core",
50
+ ]
51
+
52
+ [[package]]
53
+ name = "serde_core"
54
+ version = "1.0.229"
55
+ source = "registry+https://github.com/rust-lang/crates.io-index"
56
+ checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48"
57
+ dependencies = [
58
+ "serde_derive",
59
+ ]
60
+
61
+ [[package]]
62
+ name = "serde_derive"
63
+ version = "1.0.229"
64
+ source = "registry+https://github.com/rust-lang/crates.io-index"
65
+ checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348"
66
+ dependencies = [
67
+ "proc-macro2",
68
+ "quote",
69
+ "syn 3.0.6",
70
+ ]
71
+
72
+ [[package]]
73
+ name = "serde_json"
74
+ version = "1.0.151"
75
+ source = "registry+https://github.com/rust-lang/crates.io-index"
76
+ checksum = "c841b55ecdae098c80dcae9cf767f6f8a0c2cdb3416bbef72181df4d0fe73f14"
77
+ dependencies = [
78
+ "itoa",
79
+ "memchr",
80
+ "serde",
81
+ "serde_core",
82
+ "zmij",
83
+ ]
84
+
85
+ [[package]]
86
+ name = "syn"
87
+ version = "2.0.119"
88
+ source = "registry+https://github.com/rust-lang/crates.io-index"
89
+ checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297"
90
+ dependencies = [
91
+ "proc-macro2",
92
+ "quote",
93
+ "unicode-ident",
94
+ ]
95
+
96
+ [[package]]
97
+ name = "syn"
98
+ version = "3.0.6"
99
+ source = "registry+https://github.com/rust-lang/crates.io-index"
100
+ checksum = "8593e8e72159ed2257d083c7a454a85cbf854f37a0966d8d483aff8c8a3ebcee"
101
+ dependencies = [
102
+ "proc-macro2",
103
+ "quote",
104
+ "unicode-ident",
105
+ ]
106
+
107
+ [[package]]
108
+ name = "unicode-ident"
109
+ version = "1.0.26"
110
+ source = "registry+https://github.com/rust-lang/crates.io-index"
111
+ checksum = "d245f478577f809a851594d02313b640fb437e0bb33866753cff937863096954"
112
+
113
+ [[package]]
114
+ name = "zmij"
115
+ version = "1.0.23"
116
+ source = "registry+https://github.com/rust-lang/crates.io-index"
117
+ checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b"
@@ -0,0 +1,11 @@
1
+ [package]
2
+ name = "cognet-component-source"
3
+ version = "0.1.0"
4
+ edition = "2021"
5
+
6
+ [dependencies]
7
+ syn = { version = "=2.0.119", features = ["full"] }
8
+ serde_json = "=1.0.151"
9
+
10
+ [profile.dev]
11
+ debug = 0
@@ -0,0 +1,104 @@
1
+ use serde_json::{json, Value};
2
+ use std::{env, fs, process};
3
+ use syn::{GenericArgument, Item, PathArguments, Type, Visibility};
4
+
5
+ fn main() {
6
+ if let Err(message) = read() {
7
+ eprintln!("{message}");
8
+ process::exit(1);
9
+ }
10
+ }
11
+
12
+ fn read() -> Result<(), String> {
13
+ let path = env::args().nth(1).ok_or("expected a Rust source path")?;
14
+ let text = fs::read_to_string(&path).map_err(|error| error.to_string())?;
15
+ let source = syn::parse_file(&text).map_err(|error| error.to_string())?;
16
+ let mut shapes = Vec::<Value>::new();
17
+ for item in source.items {
18
+ if matches!(item, Item::Use(_)) { continue; }
19
+ let Item::Struct(shape) = item else {
20
+ return Err("component files may contain only structs and use declarations".into());
21
+ };
22
+ if !shape.generics.params.is_empty() || shape.generics.where_clause.is_some() {
23
+ return Err(format!("{}: generic structs are not component schemas", shape.ident));
24
+ }
25
+ if shape.attrs.iter().any(|attr| !attr.path().is_ident("doc")) {
26
+ return Err(format!("{}: schema attributes are not supported; the file and fields define the component", shape.ident));
27
+ }
28
+ let fields = match shape.fields {
29
+ syn::Fields::Named(fields) => fields.named,
30
+ _ => return Err(format!("{}: expected a struct with named fields", shape.ident)),
31
+ };
32
+ let mut values = Vec::<Value>::new();
33
+ for field in fields {
34
+ let name = field.ident.ok_or("expected a named field")?.to_string();
35
+ if field.attrs.iter().any(|attr| !attr.path().is_ident("doc")) {
36
+ return Err(format!("{}.{}: field attributes are not supported", shape.ident, name));
37
+ }
38
+ if !matches!(field.vis, Visibility::Public(_)) {
39
+ return Err(format!("{}.{}: schema fields must be public", shape.ident, name));
40
+ }
41
+ let (type_name, nullable) = kind(&field.ty)?;
42
+ values.push(json!({ "name": name, "type": type_name, "optional": false, "nullable": nullable }));
43
+ }
44
+ shapes.push(json!({
45
+ "name": shape.ident.to_string(),
46
+ "fields": values,
47
+ "component": matches!(shape.vis, Visibility::Public(_)),
48
+ "doc": doc(&shape.attrs),
49
+ }));
50
+ }
51
+ println!("{}", Value::Array(shapes));
52
+ Ok(())
53
+ }
54
+
55
+ /// A declaration's `///` lines, joined as written: the tags in them say what the component is.
56
+ fn doc(attrs: &[syn::Attribute]) -> Option<String> {
57
+ let lines: Vec<String> = attrs.iter().filter_map(|attr| match &attr.meta {
58
+ syn::Meta::NameValue(pair) if pair.path.is_ident("doc") => match &pair.value {
59
+ syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Str(text), .. }) => Some(text.value().trim().to_string()),
60
+ _ => None,
61
+ },
62
+ _ => None,
63
+ }).collect();
64
+ if lines.is_empty() { None } else { Some(lines.join("\n")) }
65
+ }
66
+
67
+ fn kind(value: &Type) -> Result<(String, bool), String> {
68
+ if let Type::Array(array) = value {
69
+ let syn::Expr::Lit(length) = &array.len else { return Err("Rust component arrays need a literal length".into()); };
70
+ let syn::Lit::Int(length) = &length.lit else { return Err("Rust component arrays need an integer length".into()); };
71
+ let (element, optional) = kind(&array.elem)?;
72
+ if optional { return Err("array elements cannot be Option<T>".into()); }
73
+ return Ok((format!("Fixed<{element},{}>", length.base10_digits()), false));
74
+ }
75
+ let Type::Path(path) = value else { return Err("unsupported Rust field type; use a named type, Option<T>, Vec<T> or a map".into()); };
76
+ if path.qself.is_some() || path.path.segments.len() != 1 {
77
+ return Err("Rust component field types must use unqualified names".into());
78
+ }
79
+ let segment = path.path.segments.first().ok_or("missing Rust type name")?;
80
+ let name = segment.ident.to_string();
81
+ let args = match &segment.arguments {
82
+ PathArguments::None => Vec::new(),
83
+ PathArguments::AngleBracketed(args) => args.args.iter().map(|arg| match arg {
84
+ GenericArgument::Type(inner) => kind(inner).and_then(|(name, optional)| {
85
+ if optional { Err("Option<T> is only supported as a field's outer type".into()) }
86
+ else { Ok(name) }
87
+ }),
88
+ _ => Err("component type arguments must be types".into()),
89
+ }).collect::<Result<Vec<_>, _>>()?,
90
+ _ => return Err(format!("unsupported arguments on {name}")),
91
+ };
92
+ match (name.as_str(), args.as_slice()) {
93
+ ("Option", [_]) => Ok((args[0].clone(), true)),
94
+ ("Vec", [_]) => Ok((format!("Vec<{}>", args[0]), false)),
95
+ ("BTreeMap" | "HashMap", [_, _]) => Ok((format!("Record<{},{}>", args[0], args[1]), false)),
96
+ (_, []) => Ok((match name.as_str() {
97
+ "String" | "str" => "string".into(),
98
+ "bool" => "boolean".into(),
99
+ "f64" | "f32" | "u32" | "i32" => name,
100
+ _ => name,
101
+ }, false)),
102
+ _ => Err(format!("unsupported generic Rust type {name}")),
103
+ }
104
+ }