@arcforge/platform 2.0.219 → 2.0.245

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,218 @@
1
+ import { existsSync, readFileSync } from "node:fs"
2
+ import { basename, dirname, join, relative } from "node:path"
3
+ import { err } from "@arcforge/err"
4
+ import type { ServiceUse } from "@arcforge/types"
5
+ import type { Source } from "../compile"
6
+ import { fail, fieldType, protoName, typescript, type Answer, type Contract, type File } from "../components"
7
+
8
+ /** An interface file: its name and major version in the file's own name. */
9
+ const INTERFACE_FILE = /^([a-z][a-z0-9-]*)@(\d+)\.interface\.ts$/
10
+
11
+ /** Where the standard library is imported from, by authors and by each other. */
12
+ const STANDARD = "@arcforge/cognet/interfaces/"
13
+
14
+ export type InterfacesOpts = {
15
+ /** The cognet's source directory. */
16
+ root: string
17
+ /** The cognet's package name — the scope its own interfaces are named in. */
18
+ cognet: string
19
+ /** What the cognet uses: each names the interface it needs compiled. */
20
+ uses: Readonly<Record<string, ServiceUse>>
21
+ /** The standard library's file for `chat@1`, or null when there is none — resolved from the agent's installed runtime. */
22
+ standard(name: string): string | null
23
+ /** Every component file, read: value types an interface may import. */
24
+ components: readonly File[]
25
+ }
26
+
27
+ /** One interface, compiled: its full name, the use-facing name a use writes, its proto file, and where its conformance cases are. */
28
+ export type Compiled = { name: string; written: string; named: string; conformance: string | null }
29
+
30
+ /**
31
+ * Interfaces — the contracts a cognet's uses name, compiled to the same
32
+ * protobuf image as its components. Each becomes a `service` whose methods'
33
+ * streaming says their kind, its arguments a `…Request` message, and its
34
+ * value types messages like any component's — so every language a system is
35
+ * written in gets the same types, and the runtime reads each method's kind
36
+ * from the image rather than from the source.
37
+ *
38
+ * Compiled: every interface in the cognet's own `interfaces/`, and each
39
+ * standard one a use names, with whatever they import.
40
+ */
41
+ export function Interfaces(opts: InterfacesOpts) {
42
+ const own = join(opts.root, "interfaces")
43
+
44
+ return {
45
+ sources(): { sources: Source[]; compiled: Compiled[] } {
46
+ const wanted = new Set<string>()
47
+ const compiled: Compiled[] = []
48
+ for (const [use, declared] of Object.entries(opts.uses)) {
49
+ const path = locate(declared.interface)
50
+ if (!path) {
51
+ throw err("INTERFACE_NOT_FOUND", {
52
+ detail: `uses.${use}: no interface ${declared.interface} — ${declared.interface.startsWith("./") ? `expected ${join(opts.root, `${declared.interface.slice(2)}.interface.ts`)}` : "the standard library has none by that name and version"}`,
53
+ context: { use: use, interface: declared.interface },
54
+ })
55
+ }
56
+ wanted.add(path)
57
+ }
58
+ if (existsSync(own)) {
59
+ for (const entry of new Bun.Glob("**/*.ts").scanSync({ cwd: own })) {
60
+ if (!entry.endsWith(".d.ts") && !entry.endsWith(".test.ts")) wanted.add(join(own, entry))
61
+ }
62
+ }
63
+
64
+ // Each wanted file, and what it imports, read once.
65
+ const files = new Map<string, File & { contracts: Contract[] }>()
66
+ const pending = [...wanted]
67
+ while (pending.length) {
68
+ const path = pending.pop()!
69
+ if (files.has(path) || opts.components.some(file => file.absPath === path)) continue
70
+ const parsed = typescript(path, readFileSync(path, "utf8"), { contract: true, resolve: specifier => resolveStandard(specifier) })
71
+ const named = protoFile(path)
72
+ files.set(path, { absPath: path, relPath: relative(opts.root, path), named: named, package: protoPackage(named), shapes: parsed.shapes, imports: parsed.imports, contracts: parsed.contracts })
73
+ for (const imported of Object.values(parsed.imports)) pending.push(imported.path)
74
+ }
75
+
76
+ const known: File[] = [...files.values(), ...opts.components]
77
+ const sources = [...files.values()].map(file => {
78
+ const name = interfaceName(file.absPath)
79
+ if (name && file.contracts.length !== 1) fail(file.absPath, `an interface file holds exactly one interface — an exported interface with methods; found ${file.contracts.length}`)
80
+ if (!name && file.contracts.length) fail(file.absPath, `an interface is written in a file named for it and its major version — ${file.contracts[0]!.name.toLowerCase()}@1.interface.ts`)
81
+ if (name) {
82
+ const cases = file.absPath.replace(/\.interface\.ts$/, ".conformance")
83
+ compiled.push({ name: name, written: written(file.absPath), named: file.named, conformance: existsSync(join(cases, "cases.json")) ? cases : null })
84
+ }
85
+ return { absPath: file.absPath, named: file.named, content: emit(file, known, name) }
86
+ })
87
+ return { sources: sources, compiled: compiled }
88
+ },
89
+ }
90
+
91
+ /** The file a use's `interface` names, or null when there is none. */
92
+ function locate(declared: string): string | null {
93
+ if (declared.startsWith("./")) {
94
+ const path = join(opts.root, `${declared.slice(2)}.interface.ts`)
95
+ return existsSync(path) ? path : null
96
+ }
97
+ return opts.standard(declared)
98
+ }
99
+
100
+ function resolveStandard(specifier: string): string | null {
101
+ if (!specifier.startsWith(STANDARD)) return null
102
+ const path = opts.standard(specifier.slice(STANDARD.length))
103
+ if (!path) fail(specifier, `no standard interface ${specifier.slice(STANDARD.length)}`)
104
+ return path
105
+ }
106
+
107
+ function isOwn(path: string): boolean {
108
+ return path.startsWith(`${own}/`)
109
+ }
110
+
111
+ /** `chat@1` for a standard interface; `@cody/terry/scene@1` for the cognet's own. Null for a file of value types. */
112
+ function interfaceName(path: string): string | null {
113
+ const match = basename(path).match(INTERFACE_FILE)
114
+ if (!match) return null
115
+ return isOwn(path) ? `${opts.cognet}/${match[1]}@${match[2]}` : `${match[1]}@${match[2]}`
116
+ }
117
+
118
+ /** What a use writes to name it: `chat@1`, or `./interfaces/scene@1`. */
119
+ function written(path: string): string {
120
+ return isOwn(path) ? `./${relative(opts.root, path).replace(/\.interface\.ts$/, "")}` : basename(path).replace(/\.interface\.ts$/, "")
121
+ }
122
+
123
+ /** `interfaces/scene.v1.proto` for the cognet's own; `axon/interfaces/chat.v1.proto` for the standard library's. */
124
+ function protoFile(path: string): string {
125
+ const file = basename(path).replace(INTERFACE_FILE, "$1.v$2.proto").replace(/\.ts$/, ".proto")
126
+ return isOwn(path) ? join("interfaces", dirname(relative(own, path)), file) : join("axon/interfaces", file)
127
+ }
128
+ }
129
+
130
+ function protoPackage(named: string): string {
131
+ return named.slice(0, -".proto".length).split(/[/.]/).map(part => part.replace(/[^A-Za-z0-9_]/g, "_").replace(/^(?=\d)/, "_")).join(".")
132
+ }
133
+
134
+ /** The proto for one interface file: its value types and handles as messages, its contract as a service. */
135
+ function emit(file: File & { contracts: Contract[] }, known: readonly File[], name: string | null): string {
136
+ const imports = new Set<string>()
137
+ const blocks: string[] = []
138
+ const taken = new Set(file.shapes.map(shape => shape.name))
139
+
140
+ const resolve = (owner: File, type: string): string => {
141
+ // Always fully qualified: inside a service a method's name shadows a message's, and `rpc Surface(…) returns (Surface)` would name the method.
142
+ if (owner.shapes.some(shape => shape.name === type)) return `.${owner.package}.${type}`
143
+ const imported = owner.imports[type]
144
+ if (!imported) fail(owner.absPath, `${type} is not declared here or imported`)
145
+ const from = known.find(one => one.absPath === imported.path)
146
+ if (!from || !from.shapes.some(shape => shape.name === imported.name)) fail(owner.absPath, `${type}: ${imported.path} declares no ${imported.name}`)
147
+ if (from !== owner) imports.add(from.named)
148
+ return `.${from.package}.${imported.name}`
149
+ }
150
+
151
+ const message = (messageName: string, fields: File["shapes"][number]["fields"], doc?: string): void => {
152
+ const lines = fields.map((field, index) => {
153
+ const type = fieldType(field.type, file, resolve)
154
+ const options = [...type.options]
155
+ if (field.optional && type.repeated) options.push("(cognet.optional) = true")
156
+ if (field.nullable) options.push("(cognet.nullable) = true")
157
+ return ` ${field.optional && !type.repeated ? "optional " : ""}${type.proto} ${protoName(field.name)} = ${index + 1}${options.length ? ` [${options.join(", ")}]` : ""};`
158
+ })
159
+ blocks.push(...comment(doc), `message ${messageName} {`, ...lines, "}", "")
160
+ }
161
+
162
+ for (const shape of file.shapes) {
163
+ if (shape.alias) fail(file.absPath, `${shape.name}: an interface's value types are object shapes`)
164
+ if (shape.handle) blocks.push(...comment(shape.doc), `message ${shape.name} {`, ` option (cognet.handle) = ${JSON.stringify(shape.name)};`, " string id = 1;", "}", "")
165
+ else message(shape.name, shape.fields, shape.doc)
166
+ }
167
+
168
+ const contract = file.contracts[0]
169
+ if (contract && name) {
170
+ const generated = (suffix: string, method: string): string => {
171
+ const generatedName = `${pascal(method)}${suffix}`
172
+ if (taken.has(generatedName)) fail(file.absPath, `${generatedName} is generated for ${contract.name}.${method} — rename the type`)
173
+ taken.add(generatedName)
174
+ return generatedName
175
+ }
176
+ const answered = (answer: Answer, method: string): string => {
177
+ if (answer === null) return ".cognet.Nothing"
178
+ if ("named" in answer) return resolve(file, answer.named)
179
+ const answerName = generated(method === "describe" ? "" : "Answer", method === "describe" ? `${contract.name}Describe` : method)
180
+ message(answerName, answer.inline)
181
+ return `.${file.package}.${answerName}`
182
+ }
183
+ const rpcs = contract.methods.map(method => {
184
+ const answer = answered(method.answer, method.name)
185
+ const request = method.kind === "describe" || method.kind === "session" ? null : generated("Request", method.name)
186
+ if (request) message(request, method.params)
187
+ const input = method.kind === "describe" ? ".cognet.Nothing" : method.kind === "session" ? `stream ${resolve(file, method.push!)}` : `.${file.package}.${request!}`
188
+ const output = method.kind === "stream" || method.kind === "session" ? `stream ${answer}` : answer
189
+ return [...comment(method.doc, " "), ` rpc ${pascal(method.name)}(${input}) returns (${output});`]
190
+ })
191
+ blocks.push(...comment(contract.doc), `service ${contract.name} {`, ` option (cognet.interface) = ${JSON.stringify(name)};`, ...rpcs.flat(), "}", "")
192
+ }
193
+
194
+ const head = [
195
+ `// Generated by axon prepare from ${file.relPath}.`,
196
+ 'syntax = "proto3";',
197
+ `package ${file.package};`,
198
+ 'import "cognet/cognet.proto";',
199
+ ...[...imports].sort().map(one => `import ${JSON.stringify(one)};`),
200
+ "",
201
+ ]
202
+ return [...head, ...blocks].join("\n")
203
+ }
204
+
205
+ function pascal(name: string): string {
206
+ return name[0]!.toUpperCase() + name.slice(1)
207
+ }
208
+
209
+ function comment(doc: string | undefined, indent = ""): string[] {
210
+ return doc ? doc.split("\n").map(line => `${indent}// ${line}`) : []
211
+ }
212
+
213
+ /** The contract an interface file declares — its exported interface with methods — by name. Throws when it holds none. */
214
+ export function contractOf(file: string): string {
215
+ const contract = typescript(file, readFileSync(file, "utf8"), { contract: true, resolve: () => null }).contracts[0]
216
+ if (!contract) fail(file, "an interface file holds an exported interface with methods; this one holds none")
217
+ return contract.name
218
+ }
@@ -0,0 +1,260 @@
1
+ import { getOption, ScalarType, type DescEnum, type DescField, type DescFile, type DescMessage, type FileRegistry } from "@bufbuild/protobuf"
2
+ import { err } from "@arcforge/err"
3
+ import { rust } from "./rust"
4
+
5
+ const KEYWORDS = new Set(["False", "None", "True", "and", "as", "assert", "async", "await", "break", "class", "continue", "def", "del", "elif", "else", "except", "finally", "for", "from", "global", "if", "import", "in", "is", "lambda", "nonlocal", "not", "or", "pass", "raise", "return", "try", "while", "with", "yield"])
6
+
7
+ /** Python schema modules and editor stubs from the same descriptor image as Rust. */
8
+ export const python = {
9
+ files(files: readonly DescFile[], registry: FileRegistry): Array<[string, string]> {
10
+ const components = registry.getExtension("cognet.components")
11
+ const frames = registry.getExtension("cognet.frames")
12
+ const union = registry.getExtension("cognet.union")
13
+ const optional = registry.getExtension("cognet.optional")
14
+ const nullable = registry.getExtension("cognet.nullable")
15
+ const array = registry.getExtension("cognet.array")
16
+ const fixed = registry.getExtension("cognet.fixed")
17
+ const text = registry.getExtension("cognet.text")
18
+ if (!components || !frames || !union || !optional || !nullable || !array || !fixed || !text) {
19
+ throw err("COGNET_SCHEMA_INVALID", { detail: "the runtime is missing Python schema options" })
20
+ }
21
+ const generated: Array<[string, string]> = []
22
+ const accessors: Array<{ name: string; type: string; path: string; field: DescField }> = []
23
+
24
+ for (const file of files) {
25
+ const path = file.name.replace(/\.proto$/, "").split("/").map(rust.segment).join("/")
26
+ const module = `cognet_generated.schema.${path.replaceAll("/", ".")}`
27
+ const imports = new Map<string, Set<string>>()
28
+ const type = (field: DescField): string => {
29
+ if (getOption(field, array) === true) return "array.array"
30
+ const named = (desc: DescMessage | DescEnum): string => {
31
+ const name = local(desc)
32
+ if (desc.file === file) return name
33
+ const foreign = `cognet_generated.schema.${desc.file.name.replace(/\.proto$/, "").split("/").map(rust.segment).join(".")}`
34
+ const names = imports.get(foreign) ?? new Set<string>()
35
+ names.add(name)
36
+ imports.set(foreign, names)
37
+ return name
38
+ }
39
+ const scalar = (kind: ScalarType): string => {
40
+ switch (kind) {
41
+ case ScalarType.DOUBLE:
42
+ case ScalarType.FLOAT: return "float"
43
+ case ScalarType.INT32:
44
+ case ScalarType.SINT32:
45
+ case ScalarType.SFIXED32:
46
+ case ScalarType.UINT32:
47
+ case ScalarType.FIXED32: return "int"
48
+ case ScalarType.BOOL: return "bool"
49
+ case ScalarType.STRING: return "str"
50
+ case ScalarType.BYTES: return "array.array"
51
+ default: throw err("COGNET_SCHEMA_INVALID", { detail: `${file.name}.${field.name}: Python cannot represent a 64-bit integer in the cognet JSON contract` })
52
+ }
53
+ }
54
+ switch (field.fieldKind) {
55
+ case "scalar": return scalar(field.scalar)
56
+ case "enum": return named(field.enum)
57
+ case "message": return named(field.message)
58
+ case "list": {
59
+ const element = field.listKind === "scalar" ? scalar(field.scalar) : field.listKind === "enum" ? named(field.enum) : named(field.message)
60
+ const size = getOption(field, fixed)
61
+ return typeof size === "number" && size > 0 ? `tuple[${new Array<string>(size).fill(element).join(", ")}]` : `list[${element}]`
62
+ }
63
+ case "map": return `dict[${field.mapKey === ScalarType.STRING ? "str" : "int"}, ${field.mapKind === "scalar" ? scalar(field.scalar) : field.mapKind === "enum" ? named(field.enum) : named(field.message)}]`
64
+ }
65
+ }
66
+ const lines: string[] = []
67
+ const emitEnum = (desc: DescEnum): void => {
68
+ const values = desc.values.filter(value => !(value.number === 0 && value.name.endsWith("_UNSPECIFIED")))
69
+ lines.push(`class ${local(desc)}(str, Enum):`)
70
+ for (const value of values) {
71
+ const prefix = `${desc.name.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toUpperCase()}_`
72
+ const rest = value.name.startsWith(prefix) ? value.name.slice(prefix.length) : value.name
73
+ const fallback = rest.toLowerCase().replace(/_([a-z0-9])/g, (_, next: string) => next.toUpperCase())
74
+ const valueText = getOption(value, text)
75
+ lines.push(` ${value.name} = ${JSON.stringify(typeof valueText === "string" && valueText ? valueText : fallback)}`)
76
+ }
77
+ lines.push("")
78
+ }
79
+ const emitMessage = (desc: DescMessage): void => {
80
+ for (const nested of desc.nestedEnums) emitEnum(nested)
81
+ for (const nested of desc.nestedMessages) emitMessage(nested)
82
+ if (getOption(desc, frames) === true) return
83
+ if (getOption(desc, components) === true) {
84
+ for (const field of desc.fields) {
85
+ const name = `${file.name.includes("/") ? file.name.slice(0, file.name.lastIndexOf("/")) + "/" : ""}${field.jsonName}`
86
+ const owner = field.fieldKind === "message" ? field.message.file : field.fieldKind === "enum" ? field.enum.file : file
87
+ const ownerPath = owner.name.replace(/\.proto$/, "").split("/").map(rust.segment).join(".")
88
+ accessors.push({ name: name, type: type(field), path: `cognet_generated.schema.${ownerPath}`, field: field })
89
+ }
90
+ return
91
+ }
92
+ const tag = getOption(desc, union)
93
+ if (typeof tag === "string" && tag) {
94
+ const variants: string[] = []
95
+ for (const field of desc.fields) {
96
+ if (field.fieldKind !== "message") throw err("COGNET_SCHEMA_INVALID", { detail: `${file.name}.${desc.name}: a Python union member must be a message` })
97
+ const variant = `${local(desc)}${field.jsonName[0]!.toUpperCase()}${field.jsonName.slice(1)}`
98
+ lines.push("@dataclass(kw_only=True, slots=True)", `class ${variant}:`, ` ${tag}: Literal[${JSON.stringify(field.jsonName)}] = ${JSON.stringify(field.jsonName)}`)
99
+ lines.push(...field.message.fields.map(member => emitField(member)))
100
+ lines.push("")
101
+ variants.push(variant)
102
+ }
103
+ lines.push(`${local(desc)} = ${variants.join(" | ")}`, "")
104
+ return
105
+ }
106
+ lines.push("@dataclass(kw_only=True, slots=True)", `class ${local(desc)}:`)
107
+ if (desc.fields.length === 0) lines.push(" pass")
108
+ lines.push(...desc.fields.map(field => emitField(field)))
109
+ lines.push("")
110
+ }
111
+ const emitField = (field: DescField): string => {
112
+ const absent = field.proto.proto3Optional || getOption(field, optional) === true
113
+ const isNullable = getOption(field, nullable) === true
114
+ const hint = `${type(field)}${isNullable ? " | None" : ""}${absent ? " | _Unset" : ""}`
115
+ const name = KEYWORDS.has(field.jsonName) ? `${field.jsonName}_` : field.jsonName
116
+ const metadata = name === field.jsonName ? "" : `metadata={"json": ${JSON.stringify(field.jsonName)}}`
117
+ const suffix = absent ? ` = field(default=UNSET${metadata ? `, ${metadata}` : ""})` : metadata ? ` = field(${metadata})` : ""
118
+ return ` ${name}: ${hint}${suffix}`
119
+ }
120
+ for (const value of file.enums) emitEnum(value)
121
+ for (const value of file.messages) emitMessage(value)
122
+ const head = [
123
+ "# Generated by axon prepare from this cognet's component schema.",
124
+ "from __future__ import annotations",
125
+ "from dataclasses import dataclass, field",
126
+ "from enum import Enum",
127
+ "from typing import TYPE_CHECKING, Literal",
128
+ "import array",
129
+ "from cognet.runtime import UNSET, _Unset",
130
+ "if TYPE_CHECKING:",
131
+ ...([...imports].length ? [...imports].sort().map(([foreign, names]) => ` from ${foreign} import ${[...names].sort().join(", ")}`) : [" pass"]),
132
+ "",
133
+ ]
134
+ generated.push([`cognet_generated/schema/${path}.py`, [...head, ...lines].join("\n")])
135
+ }
136
+
137
+ const count = new Map<string, number>()
138
+ for (const one of accessors) {
139
+ const short = one.name.split("/").at(-1)!
140
+ count.set(short, (count.get(short) ?? 0) + 1)
141
+ }
142
+ const unique = accessors.filter(one => count.get(one.name.split("/").at(-1)!) === 1)
143
+ const modules = [...new Set(unique.filter(one => /^[A-Z][A-Za-z0-9_]*$/.test(one.type)).map(one => one.path))]
144
+ const aliases = new Map(modules.map((module, index) => [module, `schema${index}`]))
145
+ const hint = (one: typeof unique[number]): string => /^[A-Z][A-Za-z0-9_]*$/.test(one.type) ? `${aliases.get(one.path)}.${one.type}` : one.type.includes("[") && /[A-Z]/.test(one.type) ? "Any" : one.type
146
+ const models = new Map(files.map((file, at) => [file, `model${at}`]))
147
+ const model = (file: DescFile): string => {
148
+ const found = models.get(file)
149
+ if (!found) throw err("COGNET_SCHEMA_INVALID", { detail: `${file.name}: Python schema has no generated module` })
150
+ return found
151
+ }
152
+ const messages: DescMessage[] = []
153
+ const visit = (message: DescMessage): void => {
154
+ for (const nested of message.nestedMessages) visit(nested)
155
+ if (getOption(message, frames) !== true && getOption(message, components) !== true) messages.push(message)
156
+ }
157
+ for (const file of files) for (const message of file.messages) visit(message)
158
+ const decoders = new Map(messages.map((message, at) => [message, `decode_${at}`]))
159
+ const convert = (field: DescField, value: string): string => {
160
+ const enumeration = (desc: DescEnum, input: string): string => {
161
+ return `${model(desc.file)}.${local(desc)}(${input})`
162
+ }
163
+ const message = (desc: DescMessage, input: string): string => {
164
+ const decoder = decoders.get(desc)
165
+ if (!decoder) throw err("COGNET_SCHEMA_INVALID", { detail: `${desc.file.name}.${desc.name}: Python has no value decoder` })
166
+ return `${decoder}(${input})`
167
+ }
168
+ let result: string
169
+ if (getOption(field, array) === true) result = value
170
+ else switch (field.fieldKind) {
171
+ case "scalar": result = value; break
172
+ case "enum": result = enumeration(field.enum, value); break
173
+ case "message": result = message(field.message, value); break
174
+ case "list": {
175
+ const item = field.listKind === "scalar" ? "item" : field.listKind === "enum" ? enumeration(field.enum, "item") : message(field.message, "item")
176
+ const size = getOption(field, fixed)
177
+ result = typeof size === "number" && size > 0 ? `tuple(${item} for item in fixed(${value}, ${size}))` : `[${item} for item in ${value}]`
178
+ break
179
+ }
180
+ case "map": {
181
+ const item = field.mapKind === "scalar" ? "item" : field.mapKind === "enum" ? enumeration(field.enum, "item") : message(field.message, "item")
182
+ result = `{key: ${item} for key, item in ${value}.items()}`
183
+ break
184
+ }
185
+ }
186
+ return getOption(field, nullable) === true ? `(${value} if ${value} is None else ${result})` : result
187
+ }
188
+ const fields = (members: readonly DescField[], indent: string): string[] => members.flatMap(field => {
189
+ const key = JSON.stringify(field.jsonName)
190
+ const name = JSON.stringify(KEYWORDS.has(field.jsonName) ? `${field.jsonName}_` : field.jsonName)
191
+ const absent = field.proto.proto3Optional || getOption(field, optional) === true
192
+ return [
193
+ `${indent}if ${key} in value:`,
194
+ `${indent} values[${name}] = ${convert(field, `value[${key}]`)}`,
195
+ ...(!absent ? [`${indent}else:`, `${indent} raise KeyError(${JSON.stringify(`missing ${field.jsonName}`)})`] : []),
196
+ ]
197
+ })
198
+ const decode = (message: DescMessage): string[] => {
199
+ const name = decoders.get(message)!
200
+ const module = model(message.file)
201
+ const tag = getOption(message, union)
202
+ const lines = [`def ${name}(value):`, " if not isinstance(value, dict):", ` raise TypeError(${JSON.stringify(`${message.name} must be an object`)})`]
203
+ if (typeof tag === "string" && tag) {
204
+ for (const field of message.fields) {
205
+ if (field.fieldKind !== "message") throw err("COGNET_SCHEMA_INVALID", { detail: `${message.file.name}.${message.name}: Python union member is not a message` })
206
+ const variant = `${local(message)}${field.jsonName[0]!.toUpperCase()}${field.jsonName.slice(1)}`
207
+ lines.push(` if value.get(${JSON.stringify(tag)}) == ${JSON.stringify(field.jsonName)}:`, " values = {}", ...fields(field.message.fields, " "), ` return ${module}.${variant}(**values)`)
208
+ }
209
+ lines.push(` raise ValueError(${JSON.stringify(`${message.name} has an unknown ${tag}`)})`, "")
210
+ } else {
211
+ lines.push(" values = {}", ...fields(message.fields, " "), ` return ${module}.${local(message)}(**values)`, "")
212
+ }
213
+ return lines
214
+ }
215
+ const world = [
216
+ "# Generated by axon prepare. Runtime access is enforced by cognet.Run.",
217
+ "from cognet import Run",
218
+ ...files.map((file, at) => `import cognet_generated.schema.${file.name.replace(/\.proto$/, "").split("/").map(rust.segment).join(".")} as model${at}`),
219
+ "",
220
+ "def fixed(value, size):",
221
+ " if not isinstance(value, (list, tuple)) or len(value) != size:",
222
+ " raise ValueError(f'fixed component tuple needs {size} values')",
223
+ " return value",
224
+ "",
225
+ ...messages.flatMap(decode),
226
+ "HYDRATE = {",
227
+ ...accessors.map(one => ` ${JSON.stringify(one.name)}: lambda value: ${convert(one.field, "value")},`),
228
+ "}",
229
+ "",
230
+ ].join("\n")
231
+ const stub = [
232
+ "# Generated by axon prepare. Component names and values from types/.",
233
+ "from typing import Generic, TypeVar, Any",
234
+ "from cognet import Run as _Run",
235
+ ...modules.map(module => `import ${module} as ${aliases.get(module)}`),
236
+ "T = TypeVar('T')",
237
+ "class ComponentView(Generic[T]):",
238
+ " def get(self, entity: str) -> T | None: ...",
239
+ " def first(self) -> tuple[str, T] | None: ...",
240
+ " def all(self) -> list[tuple[str, T]]: ...",
241
+ "class Writer:",
242
+ ...unique.map(one => ` def ${one.name.split("/").at(-1)}(self, value: ${hint(one)}) -> Writer: ...`),
243
+ " def remove(self, name: str) -> Writer: ...",
244
+ " def commit(self) -> None: ...",
245
+ "class Ecs:",
246
+ ...unique.map(one => ` ${one.name.split("/").at(-1)}: ComponentView[${hint(one)}]`),
247
+ " def has(self, entity: str) -> bool: ...",
248
+ " def write(self, entity: str) -> Writer: ...",
249
+ "class Run(_Run):",
250
+ " ecs: Ecs",
251
+ "",
252
+ ].join("\n")
253
+ generated.push(["cognet_generated/world.py", world], ["cognet_generated/world.pyi", stub])
254
+ return generated
255
+ },
256
+ }
257
+
258
+ function local(desc: DescMessage | DescEnum): string {
259
+ return desc.parent ? `${local(desc.parent)}${desc.name}` : desc.name
260
+ }