import type {
EnvironmentBindings,
EnvironmentModule,
EnvironmentSetupContext,
ExecutionExitState,
ExecutionInput,
ToolDocsInput,
} from "@cyrnel/sdk";
class MyEnvironment implements EnvironmentModule {
private bindings!: EnvironmentBindings;
private logger: EnvironmentSetupContext["logger"] | null = null;
private config: Record<string, unknown> = {};
async setup({ bindings, config, logger }: EnvironmentSetupContext) {
this.bindings = bindings;
this.config = config;
// Configure reduction for yourself from your own config field, then scope
// a base phase. The host manages the rest of the correlation metadata.
const patterns = (this.config.redactionPatterns as string[] | undefined) ?? [];
this.logger = logger.redact(patterns).child({ phase: "setup" });
}
async teardown() {}
async execute(input: ExecutionInput): Promise<ExecutionExitState> {
const eid = input.eid;
// input.envConfig contains environment-level configuration the host
// resolved from process defaults. At minimum it guarantees timeoutMs.
const timeoutMs =
(input.envConfig?.timeoutMs as number | undefined) ?? 30_000;
const execLogger = this.logger?.child({ executionId: eid, phase: "execution" });
execLogger?.info({ event: "execution-start" }, "Execution starting");
this.bindings.setState(eid, "running");
try {
// ...execute input.code in your runtime, emitting stdout/stderr/output
// Use timeoutMs to enforce the deadline.
return "success";
} catch (err) {
this.bindings.setError(eid, String(err));
execLogger?.error({ event: "execution-failed", err }, "Execution failed");
return "failed";
}
}
async kill(_eid: number) {}
async generateDocs() {
return "# My Environment\n\nDocument the runtime here.";
}
async generateToolDocs(_input: ToolDocsInput) {
return "# Tool\n\nDocument how to call this tool here.";
}
}
const configSchema = {
type: "object",
properties: {
redactionPatterns: {
type: "array",
items: { type: "string" },
description:
"Path patterns (dot/bracket notation) merged additively with the host-enforced baseline for this module's logs.",
},
},
} as const;
const secretsSchema = { type: "null" } as const;
export default { configSchema, secretsSchema, instantiate: () => new MyEnvironment() };