Part V — The Modern Experience Layer · Chapter 31 · Lesson 1 of 2
Generating Python, TypeScript and Java exports
Interop in PeaLang splits into two halves: calling out (the interop namespace) and being called (code generation from your PeaLang definitions). This lesson is the second half. The in-script generators here live in the built-in analysis namespace — no import line needed; generate_sdk_from_source is a host/CLI API described below.
Registering a host function
let reg = analysis.register_host_function(
"score_lead", "scoring", "score_lead", ["lead"], "number")
print reg["registered"]
The exact signature is register_host_function(name, module, function, arg_types=None, return_type="any"). It records a host callable by name, module path and function name; it does not install a PeaLang callback and it does not scan your project at startup. The return value is a record with registered set to true — check it when you build hosting glue.
Generate the exports
let py = analysis.generate_python_export("score_lead", ["lead"], "number")
let ts = analysis.generate_typescript_export("score_lead", ["lead"], "number")
let java = analysis.generate_java_export("score_lead", ["lead"], "Object")
write("exports/pea_export.py", py)
These three helpers return source text: a Python @pea_export decorator, a TypeScript peaExport decorator and a Java @PeaExport annotation, each with a usage comment for the named function. Their signature is (func_name, arg_types=None, return_type=...); arg_types is a list of type-name strings. Nothing is written to disk for you — save the text where your host project expects it.
Scaffolding whole SDKs
analysis.scaffold_go_sdk(target_dir)/analysis.scaffold_node_sdk(target_dir)write starter packages with layout, README and an example client.generate_sdk_from_source(source, language, target_dir=None)parsesfunctiondeclarations from.peasource and writes a typed client. It is a host/CLI API — usepea sdk-codegen <source.pea> <lang> <dir>— not ananalysisbuilt-in. Targets are exactlynode, browser, go, python, java, csharp, rust, ruby, php, library; there is no COBOL target.pea sdk listprints the same set.analysis.import_openapi(spec)accepts a dict or a JSON string and returns a record withtitle,version,base_url,connectorsandcount.analysis.generate_connector_package(imported)turns that record into PeaLang source text: onefunctionper operation callinghttp_get,http_postand friends. Save it and review it before shipping; this is the reverse of the Chapter 28 flow.
Runtime interop — calling out
let out = interop.call_python("json", "dumps", {"a": 1})
let result = interop.run("git", ["status", "--porcelain"])
let ok = interop.which("docker") != null
The built-in interop namespace (catalog name std.interop; import requires a quoted .pea file path) covers Python (call_python, import_python), native libraries (load_library, call_native, ctypes-based), processes (run, pipe_json), HTTP (http_get, http_post, json_rpc) and utilities such as which. This is trusted-context power: no capability guard sits in front of it — run it in the host when you control the machine, not on untrusted scripts.
Guided practice
- Register one host function and generate all three export snippets; write the Python one to disk and read the decorator it contains.
- Compare
interop.call_python("json", "dumps", {"a": 1})withjson_dump— know which one you are choosing and why. - Import a small OpenAPI spec with
analysis.import_openapi, pass the result toanalysis.generate_connector_package, and read the generatedfunctionbodies.