Skip to content

Runtime API

Import Context from the generated workspace support files:

import type { Context } from "@ccode/context";
interface Context {
println(message: string): void;
setScope(scopeName: string): void;
scope(): string;
renderTemplate(templatePath: string, data: any): string;
generate(templatePath: string, filePath: string, data: any): void;
accelerate(
id: string,
templatePath: string,
data: any,
instructionsPath?: string,
): void;
parseJSONFromBytes(jsonBytes: number[]): Record<string, any>;
parseJSONFromString(jsonString: string): Record<string, any>;
parseJSONFromFile(filePath: string): Record<string, any>;
parseOpenAPIFromBytes(specBytes: number[]): OpenAPIDocument;
parseOpenAPIFromString(spec: string): OpenAPIDocument;
parseOpenAPIFromFile(filePath: string): OpenAPIDocument;
}

Use the local generated context.ts as the final contract for the installed workspace version.

Writes to process stdout.

ctx.println("building docs");

Reads or overrides the active accelerator scope.

ctx.println(ctx.scope());
ctx.setScope("api-handlers");

Renders a Gonja template and returns a string.

const text = ctx.renderTemplate("templates/readme.tpl", model);

Renders a template and writes it to an output file.

ctx.generate("templates/readme.tpl", "README.generated.md", model);

Renders a template, tracks generated state, and writes safely to output_path/<id>.

ctx.accelerate("src/handlers.ts", "templates/handlers.tpl", model, "instructions/handlers.md");

Parses JSON and returns a JavaScript object. The root JSON value must be an object.

const settings = ctx.parseJSONFromFile("data/settings.json");

Parses OpenAPI v3 input and returns a JSON-like object. parseOpenAPIFromFile resolves paths relative to ccode_path.

const spec = ctx.parseOpenAPIFromFile("specs/api.yaml");