@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.
- package/package.json +27 -19
- package/src/build/blueprint/blueprint.ts +48 -64
- package/src/build/blueprint/cognet/abi.ts +3 -257
- package/src/build/blueprint/cognet/bundle-worker.ts +11 -0
- package/src/build/blueprint/cognet/bundle.ts +294 -89
- package/src/build/blueprint/cognet/cognet.ts +8 -7
- package/src/build/blueprint/cognet/config/config.ts +52 -0
- package/src/build/blueprint/cognet/config/index.ts +4 -0
- package/src/build/blueprint/cognet/config/literal.ts +83 -0
- package/src/build/blueprint/cognet/config/provider.ts +43 -0
- package/src/build/blueprint/cognet/config/shape.ts +183 -0
- package/src/build/blueprint/cognet/index.ts +4 -2
- package/src/build/blueprint/cognet/native/cargo.ts +31 -0
- package/src/build/blueprint/cognet/native/crate.ts +126 -0
- package/src/build/blueprint/cognet/native/index.ts +2 -0
- package/src/build/blueprint/cognet/native/library.ts +125 -0
- package/src/build/blueprint/cognet/native/manifest.ts +137 -0
- package/src/build/blueprint/cognet/python/index.ts +1 -0
- package/src/build/blueprint/cognet/python/project.ts +119 -0
- package/src/build/blueprint/cognet/schema/arrays.ts +23 -0
- package/src/build/blueprint/cognet/schema/authored.ts +273 -0
- package/src/build/blueprint/cognet/schema/compile.ts +78 -0
- package/src/build/blueprint/cognet/schema/components/external.ts +15 -0
- package/src/build/blueprint/cognet/schema/components/index.ts +3 -0
- package/src/build/blueprint/cognet/schema/components/lock.ts +58 -0
- package/src/build/blueprint/cognet/schema/components/model.ts +34 -0
- package/src/build/blueprint/cognet/schema/components/python.ts +15 -0
- package/src/build/blueprint/cognet/schema/components/python_source.py +69 -0
- package/src/build/blueprint/cognet/schema/components/rust.ts +33 -0
- package/src/build/blueprint/cognet/schema/components/rust_source/Cargo.lock +117 -0
- package/src/build/blueprint/cognet/schema/components/rust_source/Cargo.toml +11 -0
- package/src/build/blueprint/cognet/schema/components/rust_source/src/main.rs +104 -0
- package/src/build/blueprint/cognet/schema/components/source.ts +220 -0
- package/src/build/blueprint/cognet/schema/components/tags.ts +66 -0
- package/src/build/blueprint/cognet/schema/components/typescript.ts +157 -0
- package/src/build/blueprint/cognet/schema/index.ts +5 -0
- package/src/build/blueprint/cognet/schema/interfaces/index.ts +3 -0
- package/src/build/blueprint/cognet/schema/interfaces/source.ts +218 -0
- package/src/build/blueprint/cognet/schema/python.ts +260 -0
- package/src/build/blueprint/cognet/schema/rust.ts +316 -0
- package/src/build/blueprint/cognet/schema/schema.ts +248 -0
- package/src/build/blueprint/cognet/schema/typescript.ts +352 -0
- package/src/build/blueprint/index.ts +1 -1
- package/src/build/blueprint/modules/index.ts +7 -5
- package/src/build/blueprint/modules/meta.ts +47 -3
- package/src/build/blueprint/scan/config.ts +22 -16
- package/src/build/blueprint/scan/provider-globals.ts +46 -0
- package/src/build/blueprint/scan/providers.ts +25 -0
- package/src/build/blueprint/scan/tools.ts +21 -15
- package/src/build/extensions/edit.ts +66 -2
- package/src/build/extensions/extensions.ts +16 -3
- package/src/build/extensions/index.ts +1 -1
- package/src/build/project/bundle/agent.ts +26 -17
- package/src/build/project/bundle/source.ts +2 -2
- package/src/build/project/create/cognet.ts +84 -33
- package/src/build/project/create/profile.ts +13 -3
- package/src/build/project/index.ts +1 -1
- package/src/build/project/installer.ts +6 -5
- package/src/build/project/kinds.ts +4 -0
- package/src/build/project/manifest/env.ts +10 -0
- package/src/build/project/models/blobs/blobs.ts +57 -0
- package/src/build/project/models/blobs/index.ts +1 -0
- package/src/build/project/models/fetch.ts +46 -51
- package/src/build/project/models/index.ts +2 -1
- package/src/build/project/models/models.ts +12 -38
- package/src/build/project/models/onnx.ts +82 -0
- package/src/build/project/models/specifier.ts +9 -3
- package/src/build/project/models/storage/index-store.ts +27 -0
- package/src/build/project/models/storage/index.ts +1 -0
- package/src/build/project/models/store.ts +21 -16
- package/src/build/project/prepare.ts +82 -27
- package/src/build/project/project.ts +8 -14
- package/src/build/project/projects.ts +12 -1
- package/src/build/project/tree.ts +13 -3
- package/src/build/project/treecache.ts +8 -4
- package/src/build/project/typegen/axon-dts.ts +18 -0
- package/src/build/project/typegen/cognet-dts.ts +248 -59
- package/src/build/project/typegen/prompt-dts.ts +2 -3
- package/src/build/project/typegen/registry-dts.ts +2 -2
- package/src/build/project/typegen/tsconfig.ts +13 -13
- package/src/build/project/typegen/tui-dts.ts +3 -4
- package/src/build/project/typegen/workspace-scripts.ts +8 -7
- package/src/build/project/typegen/write.ts +15 -2
- package/src/build/project/watcher.ts +27 -0
- package/src/build/runtime/agent.ts +102 -63
- package/src/build/runtime/conformance.ts +25 -0
- package/src/build/runtime/escalations.ts +3 -8
- package/src/build/runtime/index.ts +2 -0
- package/src/build/runtime/instances.ts +7 -4
- package/src/build/runtime/progress.ts +6 -2
- package/src/build/runtime/reload-watch.ts +11 -8
- package/src/debug/README.md +398 -0
- package/src/debug/debug.ts +101 -0
- package/src/debug/events.ts +156 -0
- package/src/debug/graph.ts +94 -0
- package/src/debug/index.ts +5 -0
- package/src/debug/indexer.ts +286 -0
- package/src/debug/inference.ts +53 -0
- package/src/debug/log.ts +68 -0
- package/src/debug/overview.ts +78 -0
- package/src/debug/perf.ts +75 -0
- package/src/debug/resources.ts +125 -0
- package/src/debug/ring.ts +31 -0
- package/src/debug/sessions.ts +90 -0
- package/src/debug/signs.ts +119 -0
- package/src/debug/tally.ts +44 -0
- package/src/debug/types.ts +146 -0
- package/src/index.ts +9 -0
- package/src/link/agent-main.ts +36 -10
- package/src/link/index.ts +1 -1
- package/src/link/services.ts +88 -57
- package/src/platform.ts +17 -25
- package/src/services/cloud.ts +1 -4
- package/src/services/control/client.ts +1 -1
- package/src/services/control/server.ts +1 -1
- package/src/services/index.ts +1 -0
- package/src/services/ollama/daemon.ts +23 -0
- package/src/services/ollama/http.ts +7 -1
- package/src/services/ollama/ollama.ts +8 -14
- package/src/services/probe/ask.ts +139 -0
- package/src/services/probe/index.ts +3 -0
- package/src/services/providers.ts +18 -0
- package/src/services/resources/hardware.ts +15 -18
- package/src/services/resources/resources.ts +4 -5
- package/src/services/store/types.ts +3 -0
- package/src/services/running/index.ts +0 -1
- package/src/services/running/running.ts +0 -216
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs"
|
|
2
|
+
import { dirname, join, relative, resolve } from "node:path"
|
|
3
|
+
import { err } from "@arcforge/err"
|
|
4
|
+
|
|
5
|
+
/** The generated crate's manifest — private to Crate. */
|
|
6
|
+
export const manifest = {
|
|
7
|
+
/** The author-facing Cargo package created when Rust systems first appear. */
|
|
8
|
+
source(sourceDir: string, agentRoot: string, out: string): string {
|
|
9
|
+
const target = relative(sourceDir, join(out, "systems", "src", "lib.rs"))
|
|
10
|
+
const runtime = relative(sourceDir, join(agentRoot, "node_modules", "@arcforge", "cognet", "rust", "cognet"))
|
|
11
|
+
return [
|
|
12
|
+
"[package]",
|
|
13
|
+
'name = "cognet_systems"',
|
|
14
|
+
'version = "0.0.0"',
|
|
15
|
+
'edition = "2021"',
|
|
16
|
+
"publish = false",
|
|
17
|
+
"",
|
|
18
|
+
"[lib]",
|
|
19
|
+
`path = ${JSON.stringify(target)}`,
|
|
20
|
+
"",
|
|
21
|
+
"[dependencies]",
|
|
22
|
+
`cognet = { path = ${JSON.stringify(runtime)} }`,
|
|
23
|
+
'serde = { version = "1", features = ["derive"] }',
|
|
24
|
+
'serde_json = "1"',
|
|
25
|
+
"",
|
|
26
|
+
"[profile.release]",
|
|
27
|
+
"overflow-checks = true",
|
|
28
|
+
"",
|
|
29
|
+
'[profile.dev.package."*"]',
|
|
30
|
+
"opt-level = 3",
|
|
31
|
+
"",
|
|
32
|
+
].join("\n")
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
/** Keep Cargo and rust-analyzer's build output in the agent frame. */
|
|
36
|
+
config(sourceDir: string, out: string): string {
|
|
37
|
+
return `[build]\ntarget-dir = ${JSON.stringify(relative(sourceDir, join(out, "target")))}\n`
|
|
38
|
+
},
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The cognet's own `Cargo.toml`'s `[dependencies]`, as TOML. A crate listed by a
|
|
42
|
+
* relative `path` is resolved against the cognet, where its author wrote it.
|
|
43
|
+
*/
|
|
44
|
+
dependencies(path: string, runtime: string, owned: boolean): { text: string; runtime: string } {
|
|
45
|
+
if (!existsSync(path)) throw err("COGNET_NATIVE_FAILED", { detail: `${path} is required for a Rust cognet; add a Cargo package manifest` })
|
|
46
|
+
const parsed: unknown = Bun.TOML.parse(readFileSync(path, "utf8"))
|
|
47
|
+
if (!isTable(parsed)) throw err("COGNET_NATIVE_FAILED", { detail: `${path} is not a TOML table` })
|
|
48
|
+
if (!isTable(parsed.package) || !isTable(parsed.lib)) throw err("COGNET_NATIVE_FAILED", { detail: `${path} must declare [package] and [lib]` })
|
|
49
|
+
const dependencies = parsed.dependencies ?? {}
|
|
50
|
+
if (!isTable(dependencies)) throw err("COGNET_NATIVE_FAILED", { detail: `${path}: [dependencies] is not a table` })
|
|
51
|
+
const cognet = dependencies.cognet
|
|
52
|
+
if (!isTable(cognet) || typeof cognet.path !== "string") throw err("COGNET_NATIVE_FAILED", { detail: `${path}: [dependencies].cognet must be a path dependency` })
|
|
53
|
+
const resolvedRuntime = owned ? resolve(dirname(path), cognet.path) : runtime
|
|
54
|
+
// The crate is generated elsewhere: a crate the cognet lists by a path relative to itself is listed by where it is.
|
|
55
|
+
const located = (value: unknown): unknown => (isTable(value) && typeof value.path === "string" ? { ...value, path: resolve(dirname(path), value.path) } : value)
|
|
56
|
+
const text = Object.entries(dependencies).map(([name, value]) => `${toml.key(name)} = ${toml.value(name === "cognet" ? { ...cognet, path: resolvedRuntime } : located(value), `${path}, ${name}`)}`).join("\n")
|
|
57
|
+
return { text: text, runtime: resolvedRuntime }
|
|
58
|
+
},
|
|
59
|
+
|
|
60
|
+
/** The generated workspace: the module, plus a mirrored systems crate for installed cognets. */
|
|
61
|
+
workspace(owned: boolean): string {
|
|
62
|
+
return [
|
|
63
|
+
"# Generated by axon prepare — a cognet's Rust. List crates in the cognet's own Cargo.toml.",
|
|
64
|
+
"[workspace]",
|
|
65
|
+
'resolver = "2"',
|
|
66
|
+
`members = [${owned ? '"module"' : '"systems", "module"'}]`,
|
|
67
|
+
"",
|
|
68
|
+
// The systems are what a cognet runs every tick: optimised as a release build is, and an overflow still a fault.
|
|
69
|
+
"[profile.release]",
|
|
70
|
+
"overflow-checks = true",
|
|
71
|
+
"",
|
|
72
|
+
// A test build of the cognet's own code stays quick to compile; what it calls into — small_gicp's C++, a
|
|
73
|
+
// solver — is optimised even there, or a test of one fit takes a second.
|
|
74
|
+
'[profile.dev.package."*"]',
|
|
75
|
+
"opt-level = 3",
|
|
76
|
+
"",
|
|
77
|
+
].join("\n")
|
|
78
|
+
},
|
|
79
|
+
|
|
80
|
+
/** The systems crate: the cognet's Rust, its schemas' types, and the crates it lists. */
|
|
81
|
+
systems(dependencies: string): string {
|
|
82
|
+
return [
|
|
83
|
+
"[package]",
|
|
84
|
+
'name = "cognet_systems"',
|
|
85
|
+
'version = "0.0.0"',
|
|
86
|
+
'edition = "2021"',
|
|
87
|
+
"publish = false",
|
|
88
|
+
"",
|
|
89
|
+
"[dependencies]",
|
|
90
|
+
dependencies,
|
|
91
|
+
"",
|
|
92
|
+
].join("\n")
|
|
93
|
+
},
|
|
94
|
+
|
|
95
|
+
/** The module crate: the napi boundary over the systems crate, built as the `.node` the bundle loads. */
|
|
96
|
+
module(runtime: string, sourceDir: string | null): string {
|
|
97
|
+
return [
|
|
98
|
+
"[package]",
|
|
99
|
+
'name = "cognet_native"',
|
|
100
|
+
'version = "0.0.0"',
|
|
101
|
+
'edition = "2021"',
|
|
102
|
+
"publish = false",
|
|
103
|
+
"",
|
|
104
|
+
"[lib]",
|
|
105
|
+
'crate-type = ["cdylib"]',
|
|
106
|
+
"",
|
|
107
|
+
"[dependencies]",
|
|
108
|
+
`cognet_systems = { path = ${JSON.stringify(sourceDir ?? "../systems")} }`,
|
|
109
|
+
`cognet = { path = ${JSON.stringify(runtime)} }`,
|
|
110
|
+
'napi = { version = "3", default-features = false, features = ["napi6", "serde-json"] }',
|
|
111
|
+
'napi-derive = "3"',
|
|
112
|
+
"",
|
|
113
|
+
"[build-dependencies]",
|
|
114
|
+
'napi-build = "2"',
|
|
115
|
+
"",
|
|
116
|
+
].join("\n")
|
|
117
|
+
},
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** TOML written back from what `Bun.TOML` read — private to the manifest: strings, numbers, booleans, arrays and inline tables. */
|
|
121
|
+
const toml = {
|
|
122
|
+
key(name: string): string {
|
|
123
|
+
return /^[A-Za-z0-9_-]+$/.test(name) ? name : JSON.stringify(name)
|
|
124
|
+
},
|
|
125
|
+
|
|
126
|
+
value(value: unknown, where: string): string {
|
|
127
|
+
if (typeof value === "string") return JSON.stringify(value)
|
|
128
|
+
if (typeof value === "number" || typeof value === "boolean") return String(value)
|
|
129
|
+
if (Array.isArray(value)) return `[${value.map(one => toml.value(one, where)).join(", ")}]`
|
|
130
|
+
if (isTable(value)) return `{ ${Object.entries(value).map(([key, one]) => `${toml.key(key)} = ${toml.value(one, where)}`).join(", ")} }`
|
|
131
|
+
throw err("COGNET_NATIVE_FAILED", { detail: `${where}: ${JSON.stringify(value)} is no value a dependency is listed with` })
|
|
132
|
+
},
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function isTable(value: unknown): value is Record<string, unknown> {
|
|
136
|
+
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
137
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { PythonProject } from "./project"
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { copyFileSync, existsSync, mkdirSync, readFileSync, rmSync } from "node:fs"
|
|
2
|
+
import { dirname, join, relative, sep } from "node:path"
|
|
3
|
+
import { err } from "@arcforge/err"
|
|
4
|
+
import type { PythonSystem } from "@arcforge/cognet"
|
|
5
|
+
import { fsx } from "../../../../utils/fs"
|
|
6
|
+
import { writeIfChanged } from "../../../project/typegen/write"
|
|
7
|
+
|
|
8
|
+
const SKIP = ["node_modules", ".git", ".agent", ".cognet", ".venv", "__pycache__", ".pytest_cache", "target"]
|
|
9
|
+
|
|
10
|
+
/** PythonProject — discover systems, stage their source, and sync their environment. */
|
|
11
|
+
export function PythonProject(opts: { sourceDir: string; agentRoot: string; out: string; owned: boolean }) {
|
|
12
|
+
return {
|
|
13
|
+
async sources(): Promise<string[]> {
|
|
14
|
+
const files = await fsx.walk(opts.sourceDir, { skipDirs: SKIP })
|
|
15
|
+
return files.filter(file => file.relPath.endsWith(".system.py")).map(file => file.relPath).sort()
|
|
16
|
+
},
|
|
17
|
+
|
|
18
|
+
/** Prepared systems and the interpreter relative to the agent's generated frame. */
|
|
19
|
+
async prepare(sources: readonly string[]): Promise<{ systems: PythonSystem[]; executable: string }> {
|
|
20
|
+
if (sources.length === 0) throw err("COGNET_PYTHON_FAILED", { detail: "PythonProject.prepare needs Python systems" })
|
|
21
|
+
const sdk = Bun.resolveSync("@arcforge/cognet/python/declare.py", opts.agentRoot)
|
|
22
|
+
let declared: ReturnType<typeof Bun.spawnSync>
|
|
23
|
+
try {
|
|
24
|
+
declared = Bun.spawnSync(["python3", sdk, opts.sourceDir, ...sources], { stdout: "pipe", stderr: "pipe" })
|
|
25
|
+
} catch (cause) {
|
|
26
|
+
throw err("COGNET_PYTHON_FAILED", { detail: "Python systems need python3 on PATH to read their declarations", cause: cause })
|
|
27
|
+
}
|
|
28
|
+
if (declared.exitCode !== 0) {
|
|
29
|
+
throw err("COGNET_PYTHON_FAILED", { detail: `Python declaration failed:\n${new TextDecoder().decode(declared.stderr).trim()}` })
|
|
30
|
+
}
|
|
31
|
+
let systems: unknown
|
|
32
|
+
try {
|
|
33
|
+
systems = JSON.parse(new TextDecoder().decode(declared.stdout))
|
|
34
|
+
} catch (cause) {
|
|
35
|
+
throw err("COGNET_PYTHON_FAILED", { detail: "Python declaration returned invalid JSON", cause: cause })
|
|
36
|
+
}
|
|
37
|
+
if (!Array.isArray(systems) || systems.length !== sources.length || !systems.every(validSystem)) {
|
|
38
|
+
throw err("COGNET_PYTHON_FAILED", { detail: "Python declaration returned invalid system metadata" })
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const stage = join(opts.out, "source")
|
|
42
|
+
rmSync(stage, { recursive: true, force: true })
|
|
43
|
+
const files = await fsx.walk(opts.sourceDir, { skipDirs: SKIP })
|
|
44
|
+
for (const file of files) {
|
|
45
|
+
const target = join(stage, file.relPath)
|
|
46
|
+
mkdirSync(dirname(target), { recursive: true })
|
|
47
|
+
copyFileSync(file.absPath, target)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const project = join(opts.sourceDir, "pyproject.toml")
|
|
51
|
+
let executable = "python3"
|
|
52
|
+
if (existsSync(project)) {
|
|
53
|
+
const lock = join(opts.sourceDir, "uv.lock")
|
|
54
|
+
if (!existsSync(lock)) {
|
|
55
|
+
if (!opts.owned) throw err("COGNET_PYTHON_FAILED", { detail: `${project} has no uv.lock; published Python cognets need a committed lockfile` })
|
|
56
|
+
await command(["uv", "lock", "--project", opts.sourceDir], opts.sourceDir)
|
|
57
|
+
}
|
|
58
|
+
const environment = join(opts.out, ".venv")
|
|
59
|
+
await command(["uv", "sync", "--project", opts.sourceDir, "--locked", "--no-install-project"], opts.sourceDir, { UV_PROJECT_ENVIRONMENT: environment })
|
|
60
|
+
executable = process.platform === "win32" ? ".venv/Scripts/python.exe" : ".venv/bin/python"
|
|
61
|
+
if (!existsSync(join(opts.out, executable))) throw err("COGNET_PYTHON_FAILED", { detail: `uv sync succeeded but left no interpreter at ${join(opts.out, executable)}` })
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (opts.owned) {
|
|
65
|
+
const settingsRoot = new Set([opts.sourceDir, opts.agentRoot])
|
|
66
|
+
for (const root of settingsRoot) {
|
|
67
|
+
const settings = join(root, ".vscode", "settings.json")
|
|
68
|
+
const current = existsSync(settings) ? Bun.JSONC.parse(readFileSync(settings, "utf8")) as unknown : {}
|
|
69
|
+
if (typeof current !== "object" || current === null || Array.isArray(current)) {
|
|
70
|
+
throw err("COGNET_PYTHON_FAILED", { detail: `${settings} must be a JSON object to configure Python` })
|
|
71
|
+
}
|
|
72
|
+
const configured = current as Record<string, unknown>
|
|
73
|
+
const path = executable === "python3" ? "python3" : relative(root, join(opts.out, executable)).split(sep).join("/")
|
|
74
|
+
const extra = configured["python.analysis.extraPaths"]
|
|
75
|
+
if (extra !== undefined && (!Array.isArray(extra) || !extra.every(entry => typeof entry === "string"))) {
|
|
76
|
+
throw err("COGNET_PYTHON_FAILED", { detail: `${settings}: python.analysis.extraPaths must be a list of paths` })
|
|
77
|
+
}
|
|
78
|
+
const paths: string[] = Array.isArray(extra) ? extra : []
|
|
79
|
+
const generated = relative(root, join(opts.out, "generated")).split(sep).join("/")
|
|
80
|
+
const sdkPath = relative(root, dirname(dirname(sdk))).split(sep).join("/")
|
|
81
|
+
const added = [generated, sdkPath].filter(value => !paths.includes(value))
|
|
82
|
+
if (configured["python.defaultInterpreterPath"] === path && added.length === 0) continue
|
|
83
|
+
mkdirSync(dirname(settings), { recursive: true })
|
|
84
|
+
writeIfChanged(settings, `${JSON.stringify({ ...configured, "python.defaultInterpreterPath": path, "python.analysis.extraPaths": [...paths, ...added] }, null, 4)}\n`)
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return { systems: systems, executable: executable }
|
|
88
|
+
},
|
|
89
|
+
|
|
90
|
+
clear(): void {
|
|
91
|
+
rmSync(opts.out, { recursive: true, force: true })
|
|
92
|
+
},
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function validSystem(value: unknown): value is PythonSystem {
|
|
97
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return false
|
|
98
|
+
if (!("name" in value) || typeof value.name !== "string" || !("file" in value) || typeof value.file !== "string"
|
|
99
|
+
|| !("function" in value) || typeof value.function !== "string" || !("declaration" in value)) return false
|
|
100
|
+
const spec = value.declaration
|
|
101
|
+
if (typeof spec !== "object" || spec === null || Array.isArray(spec)) return false
|
|
102
|
+
const fields = spec as Record<string, unknown>
|
|
103
|
+
const names = (name: string): boolean => Array.isArray(fields[name]) && fields[name].every((entry: unknown) => typeof entry === "string")
|
|
104
|
+
return names("reads") && names("recalls") && names("streams") && names("writes")
|
|
105
|
+
&& (fields.on === undefined || names("on")) && (fields.uses === undefined || names("uses")) && typeof fields.once === "boolean"
|
|
106
|
+
&& (fields.lag === undefined || (typeof fields.lag === "number" && Number.isSafeInteger(fields.lag) && fields.lag >= 0))
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async function command(args: string[], cwd: string, env: Record<string, string> = {}): Promise<void> {
|
|
110
|
+
let child: ReturnType<typeof Bun.spawn>
|
|
111
|
+
try {
|
|
112
|
+
child = Bun.spawn(args, { cwd: cwd, env: { ...process.env, ...env }, stdout: "pipe", stderr: "pipe" })
|
|
113
|
+
} catch (cause) {
|
|
114
|
+
throw err("COGNET_PYTHON_FAILED", { detail: `Python systems with pyproject.toml require uv; could not run ${args[0]}`, cause: cause })
|
|
115
|
+
}
|
|
116
|
+
if (!(child.stderr instanceof ReadableStream)) throw err("COGNET_PYTHON_FAILED", { detail: `${args[0]} did not provide a stderr stream` })
|
|
117
|
+
const [stderr, exit] = await Promise.all([new Response(child.stderr).text(), child.exited])
|
|
118
|
+
if (exit !== 0) throw err("COGNET_PYTHON_FAILED", { detail: `${args.join(" ")} failed: ${stderr.trim()}` })
|
|
119
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ScalarType, type DescField } from "@bufbuild/protobuf"
|
|
2
|
+
import { err } from "@arcforge/err"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* What a `(cognet.array)` field of each number type is held as, in every
|
|
6
|
+
* language — one row a type, read by both emitters, so a block of memory is
|
|
7
|
+
* the same block on either side of the boundary.
|
|
8
|
+
*/
|
|
9
|
+
export const ARRAYS: ReadonlyMap<ScalarType, { typescript: string; rust: string }> = new Map([
|
|
10
|
+
[ScalarType.DOUBLE, { typescript: "Float64Array", rust: "f64" }],
|
|
11
|
+
[ScalarType.FLOAT, { typescript: "Float32Array", rust: "f32" }],
|
|
12
|
+
[ScalarType.INT32, { typescript: "Int32Array", rust: "i32" }],
|
|
13
|
+
[ScalarType.UINT32, { typescript: "Uint32Array", rust: "u32" }],
|
|
14
|
+
])
|
|
15
|
+
|
|
16
|
+
/** The row for a `(cognet.array)` field — which must be a plain repeated number of one of these types. */
|
|
17
|
+
export function arrayOf(field: DescField, fixed: number): { typescript: string; rust: string } {
|
|
18
|
+
const found = field.fieldKind === "list" && field.listKind === "scalar" ? ARRAYS.get(field.scalar) : undefined
|
|
19
|
+
const where = `${field.parent.file.name}.proto, ${field.parent.name}.${field.name}`
|
|
20
|
+
if (!found) throw err("COGNET_SCHEMA_INVALID", { detail: `${where}: (cognet.array) is for a repeated double, float, int32 or uint32` })
|
|
21
|
+
if (fixed > 0) throw err("COGNET_SCHEMA_INVALID", { detail: `${where}: a field is (cognet.array) — a block of any length — or (cognet.fixed), not both` })
|
|
22
|
+
return found
|
|
23
|
+
}
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs"
|
|
2
|
+
import { dirname, relative, resolve } from "node:path"
|
|
3
|
+
import ts from "typescript"
|
|
4
|
+
import { err } from "@arcforge/err"
|
|
5
|
+
import type { Source } from "./compile"
|
|
6
|
+
|
|
7
|
+
/** A deliberately small, lossless TypeScript schema dialect. Unsupported types fail at prepare. */
|
|
8
|
+
export function Authored(opts: { root: string; files: readonly { absPath: string; relPath: string }[] }) {
|
|
9
|
+
return {
|
|
10
|
+
sources(): Source[] {
|
|
11
|
+
const files = new Map<string, { named: string; package: string; ast: ts.SourceFile }>()
|
|
12
|
+
for (const file of opts.files) {
|
|
13
|
+
const named = file.relPath.slice("types/".length).replace(/(?:\.components)?\.ts$/, ".proto")
|
|
14
|
+
const source = readFileSync(file.absPath, "utf8")
|
|
15
|
+
const diagnostic = ts.transpileModule(source, { fileName: file.absPath, reportDiagnostics: true }).diagnostics?.find(one => one.category === ts.DiagnosticCategory.Error)
|
|
16
|
+
if (diagnostic) fail(file.absPath, ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"))
|
|
17
|
+
const ast = ts.createSourceFile(file.absPath, source, ts.ScriptTarget.Latest, true)
|
|
18
|
+
const pkg = ast.text.match(/@package\s+([^\s*]+)/)?.[1]
|
|
19
|
+
if (!pkg || !/^[A-Za-z_]\w*(\.[A-Za-z_]\w*)*$/.test(pkg)) fail(file.absPath, "a types file needs /** @package name.path */")
|
|
20
|
+
files.set(resolve(file.absPath), { named: named, package: pkg, ast: ast })
|
|
21
|
+
}
|
|
22
|
+
return [...files].map(([absPath, file]) => ({ absPath: absPath, named: file.named, content: emit(opts.root, files, absPath, file) }))
|
|
23
|
+
},
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function emit(root: string, files: Map<string, { named: string; package: string; ast: ts.SourceFile }>, path: string, file: { named: string; package: string; ast: ts.SourceFile }): string {
|
|
28
|
+
const imports = new Map<string, { name: string; package: string; file: string }>()
|
|
29
|
+
const dependencies = new Set<string>()
|
|
30
|
+
for (const node of file.ast.statements) {
|
|
31
|
+
if (!ts.isImportDeclaration(node) || !ts.isStringLiteral(node.moduleSpecifier)) continue
|
|
32
|
+
if (node.moduleSpecifier.text === "@arcforge/cognet/schema") continue
|
|
33
|
+
const target = resolve(dirname(path), node.moduleSpecifier.text + ".ts")
|
|
34
|
+
const imported = files.get(target)
|
|
35
|
+
if (!imported) fail(path, `schema import ${node.moduleSpecifier.text} does not resolve to a types/ file`)
|
|
36
|
+
dependencies.add(imported.named)
|
|
37
|
+
for (const element of node.importClause?.namedBindings && ts.isNamedImports(node.importClause.namedBindings) ? node.importClause.namedBindings.elements : []) {
|
|
38
|
+
imports.set(element.name.text, { name: element.propertyName?.text ?? element.name.text, package: imported.package, file: imported.named })
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const declarations = new Map<string, ts.TypeNode>()
|
|
42
|
+
const declared = new Map<string, ts.Statement>()
|
|
43
|
+
for (const node of file.ast.statements) {
|
|
44
|
+
if (ts.isImportDeclaration(node)) {
|
|
45
|
+
if (node.importClause?.name || (node.importClause?.namedBindings && ts.isNamespaceImport(node.importClause.namedBindings))) fail(path, "schema imports must name their types explicitly")
|
|
46
|
+
continue
|
|
47
|
+
}
|
|
48
|
+
if (ts.isTypeAliasDeclaration(node)) { declarations.set(node.name.text, node.type); declared.set(node.name.text, node); continue }
|
|
49
|
+
if (ts.isInterfaceDeclaration(node)) { declarations.set(node.name.text, ts.factory.createTypeLiteralNode(node.members)); declared.set(node.name.text, node); continue }
|
|
50
|
+
fail(path, `types/ may contain only imports, interfaces and type aliases; found ${ts.SyntaxKind[node.kind]}`)
|
|
51
|
+
}
|
|
52
|
+
const messages = new Map<string, string[]>()
|
|
53
|
+
const enums = new Map<string, string[]>()
|
|
54
|
+
const generated = new Set<string>()
|
|
55
|
+
|
|
56
|
+
function nestedName(name: string, ast: ts.SourceFile): string {
|
|
57
|
+
const declaration = ast.statements.find(statement => (ts.isTypeAliasDeclaration(statement) || ts.isInterfaceDeclaration(statement)) && statement.name.text === name)
|
|
58
|
+
const parent = declaration ? tag(declaration, ast, "nested") : undefined
|
|
59
|
+
return parent ? `${nestedName(parent, ast)}.${name.slice(parent.length)}` : name
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function typename(node: ts.TypeNode, owner: string): { type: string; suffix: string; options: string[] } {
|
|
63
|
+
if (ts.isParenthesizedTypeNode(node)) return typename(node.type, owner)
|
|
64
|
+
if (ts.isUnionTypeNode(node)) {
|
|
65
|
+
const nonNull = node.types.filter(part => part.kind !== ts.SyntaxKind.NullKeyword && !(ts.isLiteralTypeNode(part) && part.literal.kind === ts.SyntaxKind.NullKeyword))
|
|
66
|
+
if (nonNull.length === 1 && nonNull.length !== node.types.length) {
|
|
67
|
+
const inner = typename(nonNull[0]!, owner)
|
|
68
|
+
return { ...inner, options: [...inner.options, "(cognet.nullable) = true"] }
|
|
69
|
+
}
|
|
70
|
+
if (node.types.every(part => ts.isLiteralTypeNode(part) && ts.isStringLiteral(part.literal))) {
|
|
71
|
+
enumeration(owner, node.types.map(part => (part as ts.LiteralTypeNode).literal as ts.StringLiteral))
|
|
72
|
+
return { type: owner, suffix: "", options: [] }
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (ts.isTypeLiteralNode(node)) {
|
|
76
|
+
message(owner, node.members)
|
|
77
|
+
return { type: owner, suffix: "", options: [] }
|
|
78
|
+
}
|
|
79
|
+
if (ts.isTupleTypeNode(node)) {
|
|
80
|
+
if (!node.elements.length) fail(path, `${owner}: empty tuple is not a schema value`)
|
|
81
|
+
const first = typename(node.elements[0]!, owner)
|
|
82
|
+
if (node.elements.some(part => part.getText(file.ast) !== node.elements[0]!.getText(file.ast))) fail(path, `${owner}: tuples must have one element type`)
|
|
83
|
+
return { type: first.type, suffix: "repeated", options: [...first.options, `(cognet.fixed) = ${node.elements.length}`] }
|
|
84
|
+
}
|
|
85
|
+
if (ts.isArrayTypeNode(node)) {
|
|
86
|
+
const inner = typename(node.elementType, owner)
|
|
87
|
+
if (inner.suffix) fail(path, `${owner}: nested arrays are not supported`)
|
|
88
|
+
return { ...inner, suffix: "repeated" }
|
|
89
|
+
}
|
|
90
|
+
if (ts.isTypeReferenceNode(node)) {
|
|
91
|
+
const name = node.typeName.getText(file.ast)
|
|
92
|
+
const scalar: Record<string, string> = { u32: "uint32", i32: "int32", f32: "float", f64: "double", Float32Array: "float", Float64Array: "double", Int32Array: "int32", Uint32Array: "uint32", Uint8Array: "bytes" }
|
|
93
|
+
if (name in scalar) return { type: scalar[name]!, suffix: name.endsWith("Array") && name !== "Uint8Array" ? "repeated" : "", options: name.endsWith("Array") && name !== "Uint8Array" ? ["(cognet.array) = true"] : [] }
|
|
94
|
+
if ((name === "Record" || name === "Partial") && node.typeArguments) {
|
|
95
|
+
const record = name === "Partial" ? node.typeArguments[0] : node
|
|
96
|
+
if (!record || !ts.isTypeReferenceNode(record) || record.typeName.getText(file.ast) !== "Record" || record.typeArguments?.length !== 2) fail(path, `${owner}: expected Record<Key, Value>`)
|
|
97
|
+
const key = record.typeArguments[0]!
|
|
98
|
+
const value = typename(record.typeArguments[1]!, owner)
|
|
99
|
+
if (value.suffix) fail(path, `${owner}: map values cannot be lists`)
|
|
100
|
+
const keyed = key.kind === ts.SyntaxKind.StringKeyword || key.kind === ts.SyntaxKind.NumberKeyword ? [] : [`(cognet.keys) = ${JSON.stringify(qualified(key).replace(/^\./, ""))}`]
|
|
101
|
+
return { type: `map<${key.kind === ts.SyntaxKind.NumberKeyword ? "int32" : "string"}, ${value.type}>`, suffix: "", options: keyed }
|
|
102
|
+
}
|
|
103
|
+
const local = declarations.get(name)
|
|
104
|
+
if (local && ts.isTypeReferenceNode(local) && ["number", "string", "boolean"].includes(local.typeName.getText(file.ast))) return typename(local, owner)
|
|
105
|
+
return { type: qualified(node), suffix: "", options: [] }
|
|
106
|
+
}
|
|
107
|
+
if (node.kind === ts.SyntaxKind.NumberKeyword) return { type: "double", suffix: "", options: [] }
|
|
108
|
+
if (node.kind === ts.SyntaxKind.StringKeyword) return { type: "string", suffix: "", options: [] }
|
|
109
|
+
if (node.kind === ts.SyntaxKind.BooleanKeyword) return { type: "bool", suffix: "", options: [] }
|
|
110
|
+
fail(path, `${owner}: unsupported TypeScript type ${node.getText(file.ast)}`)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function qualified(node: ts.TypeNode): string {
|
|
114
|
+
if (!ts.isTypeReferenceNode(node)) fail(path, `expected a named type, got ${node.getText(file.ast)}`)
|
|
115
|
+
const name = node.typeName.getText(file.ast)
|
|
116
|
+
const from = imports.get(name)
|
|
117
|
+
if (from) {
|
|
118
|
+
const imported = [...files.values()].find(file => file.named === from.file)
|
|
119
|
+
return `.${from.package}.${imported ? nestedName(from.name, imported.ast) : from.name}`
|
|
120
|
+
}
|
|
121
|
+
if (declarations.has(name)) {
|
|
122
|
+
return nestedName(name, file.ast)
|
|
123
|
+
}
|
|
124
|
+
fail(path, `unknown type ${name}`)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function enumeration(name: string, values: readonly ts.StringLiteral[]): void {
|
|
128
|
+
if (generated.has(name)) fail(path, `${name}: duplicate generated type`)
|
|
129
|
+
generated.add(name)
|
|
130
|
+
const parent = declared.has(name) ? tag(declared.get(name)!, file.ast, "nested") : undefined
|
|
131
|
+
const shortName = parent ? name.slice(parent.length) : name
|
|
132
|
+
const prefix = shortName.replace(/([a-z])([A-Z])/g, "$1_$2").toUpperCase()
|
|
133
|
+
const lines = [...docs(declared.get(name), file.ast, ""), `enum ${name} {`, ` ${prefix}_UNSPECIFIED = 0;`]
|
|
134
|
+
const used = new Set<string>()
|
|
135
|
+
values.forEach((value, index) => {
|
|
136
|
+
const key = value.text.replace(/([a-z])([A-Z])/g, "$1_$2").replace(/[^A-Za-z0-9]+/g, "_").toUpperCase()
|
|
137
|
+
if (!key || used.has(key)) fail(path, `${name}: enum values need distinct protobuf names`)
|
|
138
|
+
used.add(key)
|
|
139
|
+
const natural = key.toLowerCase().replace(/_([a-z])/g, (_, letter: string) => letter.toUpperCase())
|
|
140
|
+
const option = natural === value.text ? "" : ` [(cognet.text) = ${JSON.stringify(value.text)}]`
|
|
141
|
+
lines.push(` ${prefix}_${key} = ${index + 1}${option};`)
|
|
142
|
+
})
|
|
143
|
+
enums.set(name, [...lines, "}"])
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function message(name: string, members: ts.NodeArray<ts.TypeElement>): void {
|
|
147
|
+
if (generated.has(name)) fail(path, `${name}: duplicate generated type`)
|
|
148
|
+
generated.add(name)
|
|
149
|
+
const lines = [...docs(declared.get(name), file.ast, ""), `message ${name} {`]
|
|
150
|
+
const declaration = declared.get(name)
|
|
151
|
+
// A container is said by its tag, or by what every member is — the same rule for components and frames.
|
|
152
|
+
const all = (wrapper: string) => members.length > 0 && members.every(member => ts.isPropertySignature(member) && member.type && ts.isTypeReferenceNode(member.type) && member.type.typeName.getText(file.ast) === wrapper)
|
|
153
|
+
const componentContainer = declaration && (tag(declaration, file.ast, "components") !== undefined || all("Component"))
|
|
154
|
+
const frameContainer = declaration && (tag(declaration, file.ast, "frames") !== undefined || all("Frame"))
|
|
155
|
+
const container = declaration && (componentContainer || frameContainer)
|
|
156
|
+
const kind = componentContainer ? "Components" : frameContainer ? "Frames" : null
|
|
157
|
+
if (container && kind) lines.push(` option (cognet.${kind.toLowerCase()}) = true;`)
|
|
158
|
+
let next = 1
|
|
159
|
+
for (const member of members) {
|
|
160
|
+
if (!ts.isPropertySignature(member) || !member.type || !member.name) fail(path, `${name}: only typed properties can be schema fields`)
|
|
161
|
+
const key = member.name.getText(file.ast).replace(/^['"]|['"]$/g, "")
|
|
162
|
+
const number = tag(member, file.ast, "field")
|
|
163
|
+
if (number && !/^[1-9]\d*$/.test(number)) fail(path, `${name}.${key}: invalid protobuf field number ${number}`)
|
|
164
|
+
const fieldNumber = number ? Number(number) : next
|
|
165
|
+
next = fieldNumber + 1
|
|
166
|
+
const wrapper = container && ts.isTypeReferenceNode(member.type) ? member.type : undefined
|
|
167
|
+
const wrapped = wrapper?.typeName.getText(file.ast)
|
|
168
|
+
if (container && wrapped !== (kind === "Components" ? "Component" : "Frame")) fail(path, `${name}.${key}: expected ${kind === "Components" ? "Component<T, Options>" : "Frame<Options>"}`)
|
|
169
|
+
const value = kind === "Frames" ? { type: "cognet.Frame", suffix: "", options: [] } : typename(container ? wrapper!.typeArguments?.[0] ?? member.type : member.type, `${name}${key[0]!.toUpperCase()}${key.slice(1)}`)
|
|
170
|
+
const fieldName = key.replace(/([a-z])([A-Z])/g, "$1_$2").toLowerCase()
|
|
171
|
+
const extra = [...value.options]
|
|
172
|
+
if (member.questionToken && value.suffix) extra.push("(cognet.optional) = true")
|
|
173
|
+
if (container) {
|
|
174
|
+
const options = kind === "Frames" ? wrapper!.typeArguments?.[0] : wrapper!.typeArguments?.[1]
|
|
175
|
+
if (!options) fail(path, `${name}.${key}: supply options`)
|
|
176
|
+
extra.push(`(cognet.${kind === "Frames" ? "frame" : "component"}) = ${option(options, kind === "Frames" ? "frame" : "component")}`)
|
|
177
|
+
}
|
|
178
|
+
const required = member.questionToken && !value.suffix ? "optional " : ""
|
|
179
|
+
const repeated = value.suffix ? `${value.suffix} ` : ""
|
|
180
|
+
const options = extra.length ? ` [${extra.join(", ")}]` : ""
|
|
181
|
+
lines.push(...docs(member, file.ast, " "), ` ${required}${repeated}${value.type} ${fieldName} = ${fieldNumber}${options};`)
|
|
182
|
+
}
|
|
183
|
+
messages.set(name, [...lines, "}"])
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function union(name: string, node: ts.UnionTypeNode, key: string): void {
|
|
187
|
+
const lines = [`message ${name} {`, ` option (cognet.union) = ${JSON.stringify(key)};`, ` oneof ${name.toLowerCase()} {`]
|
|
188
|
+
node.types.forEach((part, index) => {
|
|
189
|
+
const variant = ts.isParenthesizedTypeNode(part) ? part.type : part
|
|
190
|
+
if (!ts.isIntersectionTypeNode(variant) || variant.types.length !== 2 || !ts.isTypeLiteralNode(variant.types[0]!) || !ts.isTypeReferenceNode(variant.types[1]!)) fail(path, `${name}: union members must be { ${key}: "name" } & Message`)
|
|
191
|
+
const tag = variant.types[0]!.members[0]
|
|
192
|
+
if (!tag || !ts.isPropertySignature(tag) || tag.name?.getText(file.ast) !== key || !tag.type || !ts.isLiteralTypeNode(tag.type) || !ts.isStringLiteral(tag.type.literal)) fail(path, `${name}: each member needs a ${key} string literal`)
|
|
193
|
+
const field = tag.type.literal.text.replace(/([a-z])([A-Z])/g, "$1_$2").replace(/-/g, "_").toLowerCase()
|
|
194
|
+
lines.push(` ${qualified(variant.types[1]!)} ${field} = ${index + 1};`)
|
|
195
|
+
})
|
|
196
|
+
messages.set(name, [...lines, " }", "}"])
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function option(node: ts.TypeNode, context: string): string {
|
|
200
|
+
if (!ts.isTypeLiteralNode(node)) fail(path, `${context}: options must be a type literal`)
|
|
201
|
+
const values = node.members.map(member => {
|
|
202
|
+
if (!ts.isPropertySignature(member) || !member.type || !member.name) fail(path, `${context}: invalid option`)
|
|
203
|
+
const key = member.name.getText(file.ast)
|
|
204
|
+
const value = member.type
|
|
205
|
+
if (ts.isTypeLiteralNode(value)) return `${key}: ${option(value, key)}`
|
|
206
|
+
if (ts.isTupleTypeNode(value)) return `${key}: [${value.elements.map(element => element.getText(file.ast)).join(", ")}]`
|
|
207
|
+
if (ts.isLiteralTypeNode(value)) {
|
|
208
|
+
if (value.literal.kind === ts.SyntaxKind.TrueKeyword) return `${key}: true`
|
|
209
|
+
if (value.literal.kind === ts.SyntaxKind.FalseKeyword) return `${key}: false`
|
|
210
|
+
if (ts.isNumericLiteral(value.literal)) return `${key}: ${value.literal.text}`
|
|
211
|
+
if (ts.isPrefixUnaryExpression(value.literal)) return `${key}: ${value.literal.getText(file.ast)}`
|
|
212
|
+
if (ts.isStringLiteral(value.literal)) {
|
|
213
|
+
const text = value.literal.text
|
|
214
|
+
const enums: Record<string, string> = { domain: "DOMAIN", shape: "SHAPE", archetype: "ARCHETYPE", instrument: "INSTRUMENT", kind: "KIND" }
|
|
215
|
+
const code = enums[key]
|
|
216
|
+
return `${key.replace(/([a-z])([A-Z])/g, "$1_$2").toLowerCase()}: ${code ? `${code}_${text.replace(/-/g, "_").toUpperCase()}` : JSON.stringify(text)}`
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
fail(path, `${context}.${key}: options need literal values`)
|
|
220
|
+
})
|
|
221
|
+
return `{ ${values.join(", ")} }`
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
for (const [name, node] of declarations) {
|
|
225
|
+
if (["u32", "i32", "f32", "f64"].includes(name)) continue
|
|
226
|
+
const declaration = file.ast.statements.find(statement => (ts.isTypeAliasDeclaration(statement) || ts.isInterfaceDeclaration(statement)) && statement.name.text === name)
|
|
227
|
+
const unionKey = declaration ? tag(declaration, file.ast, "union") : undefined
|
|
228
|
+
if (unionKey && ts.isUnionTypeNode(node)) union(name, node, unionKey)
|
|
229
|
+
else if (ts.isTypeLiteralNode(node)) message(name, node.members)
|
|
230
|
+
else if (ts.isUnionTypeNode(node) && node.types.every(part => ts.isLiteralTypeNode(part) && ts.isStringLiteral(part.literal))) enumeration(name, node.types.map(part => (part as ts.LiteralTypeNode).literal as ts.StringLiteral))
|
|
231
|
+
else fail(path, `${name}: top-level aliases must be object shapes or string literal unions`)
|
|
232
|
+
}
|
|
233
|
+
const importLines = [...dependencies].sort().map(name => `import ${JSON.stringify(name)};`)
|
|
234
|
+
const allBlocks = new Map([...enums, ...messages])
|
|
235
|
+
const parentOf = (name: string) => declared.has(name) ? tag(declared.get(name)!, file.ast, "nested") : undefined
|
|
236
|
+
for (const name of allBlocks.keys()) {
|
|
237
|
+
const parent = parentOf(name)
|
|
238
|
+
if (parent && !allBlocks.has(parent)) fail(path, `${name}: nested parent ${parent} is not declared`)
|
|
239
|
+
}
|
|
240
|
+
const render = (name: string): string[] => {
|
|
241
|
+
const block = allBlocks.get(name)
|
|
242
|
+
if (!block) fail(path, `${name}: nested type has no declaration`)
|
|
243
|
+
const parent = parentOf(name)
|
|
244
|
+
const own = parent ? block.map(line => line.replace(new RegExp(`^(message|enum) ${name} \\{`), `$1 ${name.slice(parent.length)} {`)) : block
|
|
245
|
+
const children = [...allBlocks.keys()].filter(child => parentOf(child) === name)
|
|
246
|
+
const inside = children.flatMap(child => render(child).map(line => line ? ` ${line}` : ""))
|
|
247
|
+
return [...own.slice(0, -1), ...inside, own.at(-1)!, ""]
|
|
248
|
+
}
|
|
249
|
+
const declarationsOut = [...allBlocks.keys()].filter(name => parentOf(name) === undefined).flatMap(render)
|
|
250
|
+
const runtime = declarationsOut.some(line => line.includes("(cognet.")) ? ['import "cognet/cognet.proto";'] : []
|
|
251
|
+
const head = file.ast.text.slice(0, file.ast.statements[0]?.getStart(file.ast) ?? 0)
|
|
252
|
+
const fileDocs = [...head.matchAll(/\/\*\*([\s\S]*?)\*\//g)].filter(match => !match[1]?.includes("@package"))
|
|
253
|
+
.flatMap(match => match[1]!.split("\n").map(line => line.replace(/^\s*\*?\s?/, "").trim()))
|
|
254
|
+
return [...fileDocs.map(line => line ? `// ${line}` : "//"), `// Generated by axon prepare from ${relative(root, path)}.`, 'syntax = "proto3";', `package ${file.package};`, "", ...runtime, ...importLines, "", ...declarationsOut].join("\n")
|
|
255
|
+
}
|
|
256
|
+
function tag(node: ts.Node, source: ts.SourceFile, name: string): string | undefined {
|
|
257
|
+
const match = node.getFullText(source).slice(0, node.getStart(source) - node.getFullStart()).match(new RegExp(`@${name}(?:\\s+([^\\s*]+))?`))
|
|
258
|
+
return match ? match[1] ?? "" : undefined
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function docs(node: ts.Node | undefined, source: ts.SourceFile, indent: string): string[] {
|
|
262
|
+
if (!node) return []
|
|
263
|
+
const before = node.getFullText(source).slice(0, node.getStart(source) - node.getFullStart())
|
|
264
|
+
const comments = [...before.matchAll(/\/\*\*([\s\S]*?)\*\//g)]
|
|
265
|
+
return comments.flatMap(comment => comment[1]!.split("\n"))
|
|
266
|
+
.map(line => line.replace(/^\s*\*?\s?/, "").replace(/@(field|components|frames|union|nested)\s*[^\s*]*/, "").trim())
|
|
267
|
+
.filter(line => line.length > 0)
|
|
268
|
+
.map(line => `${indent}// ${line}`)
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function fail(file: string, detail: string): never {
|
|
272
|
+
throw err("COGNET_SCHEMA_INVALID", { detail: `${file}: ${detail}` })
|
|
273
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { copyFileSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"
|
|
2
|
+
import { dirname, join, resolve } from "node:path"
|
|
3
|
+
import { createFileRegistry, fromBinary, type FileRegistry } from "@bufbuild/protobuf"
|
|
4
|
+
import { FileDescriptorSetSchema } from "@bufbuild/protobuf/wkt"
|
|
5
|
+
import { err } from "@arcforge/err"
|
|
6
|
+
|
|
7
|
+
/** One schema of the cognet's: where it is, and the path it is imported and named by. */
|
|
8
|
+
export type Source = { absPath: string; named: string; content?: string }
|
|
9
|
+
|
|
10
|
+
/** The runtime's own schema, as every cognet schema imports it. */
|
|
11
|
+
const RUNTIME = "cognet/cognet.proto"
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Compile — a cognet's schemas into one image: every file, its types, its
|
|
15
|
+
* options and its comments. Plain functions over the filesystem and `buf`.
|
|
16
|
+
*
|
|
17
|
+
* The schemas are staged first, each at the path it is NAMED by (its path
|
|
18
|
+
* under `src/`, as components are named), beside the runtime's own
|
|
19
|
+
* `cognet/cognet.proto` from the `@arcforge/cognet` the agent runs — so a
|
|
20
|
+
* schema imports another as `import "control/control.proto"` and the
|
|
21
|
+
* runtime's as `import "cognet/cognet.proto"`, wherever either lives on disk.
|
|
22
|
+
*/
|
|
23
|
+
export const compile = {
|
|
24
|
+
/** `stage` emptied and refilled: the cognet's schemas, and the runtime's, from `agentRoot`'s installed `@arcforge/cognet`. */
|
|
25
|
+
stage(stage: string, sources: readonly Source[], agentRoot: string): void {
|
|
26
|
+
rmSync(stage, { recursive: true, force: true })
|
|
27
|
+
mkdirSync(stage, { recursive: true })
|
|
28
|
+
const runtime = runtimeSchema(agentRoot)
|
|
29
|
+
const runtimeAt = join(stage, RUNTIME)
|
|
30
|
+
mkdirSync(dirname(runtimeAt), { recursive: true })
|
|
31
|
+
copyFileSync(runtime, runtimeAt)
|
|
32
|
+
for (const source of sources) {
|
|
33
|
+
const at = join(stage, source.named)
|
|
34
|
+
mkdirSync(dirname(at), { recursive: true })
|
|
35
|
+
if (source.content !== undefined) writeFileSync(at, source.content)
|
|
36
|
+
else copyFileSync(source.absPath, at)
|
|
37
|
+
}
|
|
38
|
+
// `cognet` names a runtime, not an API with versions: its compatibility is the cognet ABI's.
|
|
39
|
+
writeFileSync(join(stage, "buf.yaml"), "version: v2\nlint:\n use: [STANDARD]\n except: [PACKAGE_VERSION_SUFFIX, PACKAGE_DIRECTORY_MATCH]\n")
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
/** Every staged schema compiled, with source info: what each says, and the comments that say why. Throws with `buf`'s own words. */
|
|
43
|
+
image(stage: string): FileRegistry {
|
|
44
|
+
const out = join(stage, "image.binpb")
|
|
45
|
+
const run = Bun.spawnSync([buf(), "build", stage, "-o", out], { stderr: "pipe", stdout: "pipe" })
|
|
46
|
+
if (run.exitCode !== 0) {
|
|
47
|
+
throw err("COGNET_SCHEMA_INVALID", { detail: `${new TextDecoder().decode(run.stderr).trim()}`, context: { stage: stage } })
|
|
48
|
+
}
|
|
49
|
+
return createFileRegistry(fromBinary(FileDescriptorSetSchema, readFileSync(out)))
|
|
50
|
+
},
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The runtime's `cognet.proto`, from the `@arcforge/cognet` installed for `agentRoot`.
|
|
55
|
+
*
|
|
56
|
+
* Found on disk rather than through `Bun.resolveSync`, because Bun's resolver
|
|
57
|
+
* caches directory lookups for the life of the process: a process that
|
|
58
|
+
* resolved from `agentRoot` before its install (the TUI, Fleet, the test
|
|
59
|
+
* runner — anything that scaffolds and then compiles) is told the package
|
|
60
|
+
* does not exist after it does.
|
|
61
|
+
*/
|
|
62
|
+
function runtimeSchema(agentRoot: string): string {
|
|
63
|
+
for (let dir = resolve(agentRoot); ; dir = dirname(dir)) {
|
|
64
|
+
const pkg = join(dir, "node_modules", "@arcforge", "cognet")
|
|
65
|
+
if (existsSync(join(pkg, "package.json"))) {
|
|
66
|
+
const manifest = JSON.parse(readFileSync(join(pkg, "package.json"), "utf-8")) as { exports?: Record<string, string> }
|
|
67
|
+
const entry = manifest.exports?.["./proto/cognet.proto"]
|
|
68
|
+
if (!entry) throw err("COGNET_RUNTIME_UNAVAILABLE", { detail: `${pkg} does not export ./proto/cognet.proto — it predates cognet schemas` })
|
|
69
|
+
return join(pkg, entry)
|
|
70
|
+
}
|
|
71
|
+
if (dirname(dir) === dir) throw err("COGNET_RUNTIME_UNAVAILABLE", { detail: `@arcforge/cognet is not installed for ${agentRoot}` })
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The `buf` this platform was installed with. */
|
|
76
|
+
function buf(): string {
|
|
77
|
+
return Bun.resolveSync("@bufbuild/buf/bin/buf", import.meta.dir)
|
|
78
|
+
}
|