if (!globalThis.__omp_js_prelude_loaded__) {
	globalThis.__omp_js_prelude_loaded__ = true;

	const toOptions = value => (value && typeof value === "object" && !Array.isArray(value) ? value : {});
	const callHelper = (name, ...args) => globalThis.__omp_helpers__[name](...args);

	const read = (path, opts = {}) => callHelper("read", path, toOptions(opts));
	const write = async (path, data) => callHelper("writeFile", path, data);
	const append = (path, content) => callHelper("append", path, content);
	const sort = (text, opts = {}) => callHelper("sortText", text, toOptions(opts));
	const uniq = (text, opts = {}) => callHelper("uniqText", text, toOptions(opts));
	const counter = (items, opts = {}) => callHelper("counter", items, toOptions(opts));
	const diff = (a, b) => callHelper("diff", a, b);
	const tree = (path = ".", opts = {}) => callHelper("tree", path, toOptions(opts));
	const env = (key, value) => callHelper("env", key, value);

	const tool = new Proxy(
		{},
		{
			get(_target, prop) {
				if (typeof prop !== "string") return undefined;
				return async args => globalThis.__omp_call_tool__(prop, args ?? {});
			},
		},
	);

	const output = async (...args) => {
		let opts = {};
		let ids = args;
		if (args.length > 0) {
			const last = args.at(-1);
			if (last && typeof last === "object" && !Array.isArray(last)) {
				opts = last;
				ids = args.slice(0, -1);
			}
		}
		const reads = ids.map(id => tool.read({ path: `agent://${id}`, ...opts }));
		const values = await Promise.all(reads);
		return values.length === 1 ? values[0] : values;
	};

	const hasOwn = (object, key) => Object.prototype.hasOwnProperty.call(object, key);

	const llm = async (prompt, opts = {}) => {
		const o = toOptions(opts);
		const res = await globalThis.__omp_call_tool__("__llm__", { prompt, ...o });
		const text = res && typeof res === "object" ? res.text : res;
		return hasOwn(o, "schema") ? JSON.parse(text) : text;
	};

	const agent = async (prompt, opts = {}) => {
		const o = toOptions(opts);
		const res = await globalThis.__omp_call_tool__("__agent__", { prompt, ...o });
		const text = res && typeof res === "object" ? res.text : res;
		return hasOwn(o, "schema") ? JSON.parse(text) : text;
	};

	// Pool ceiling mirrors the task tool's `task.maxConcurrency` setting so an
	// eval fan-out runs as wide as a `task` batch would. 0 (and any non-finite
	// reply) means unbounded — run every item at once.
	const __concurrencyLimit = async () => {
		try {
			const r = await globalThis.__omp_call_tool__("__concurrency__", {});
			const n = Math.trunc(Number(r && typeof r === "object" ? r.limit : r));
			return Number.isFinite(n) && n > 0 ? n : 0;
		} catch {
			return 0;
		}
	};

	const __pool = async (items, fn) => {
		const list = Array.from(items ?? []);
		if (list.length === 0) return [];
		const limit = await __concurrencyLimit();
		const concurrency = limit > 0 ? Math.min(limit, list.length) : list.length;
		const results = new Array(list.length);
		let next = 0;
		const worker = async () => {
			while (true) {
				const index = next++;
				if (index >= list.length) return;
				results[index] = await fn(list[index], index);
			}
		};
		await Promise.all(Array.from({ length: concurrency }, () => worker()));
		return results;
	};

	const parallel = async thunks =>
		__pool(thunks, (thunk, index) => {
			if (typeof thunk !== "function") throw new TypeError("parallel() expects an iterable of functions");
			return thunk(index);
		});

	const pipeline = async (items, ...stages) => {
		let current = Array.from(items ?? []);
		for (const stage of stages) {
			if (typeof stage !== "function") throw new TypeError("pipeline() stages must be functions");
			current = await __pool(current, stage);
		}
		return current;
	};

	const log = message => globalThis.__omp_emit_status__("log", { message: String(message) });

	const phase = title => {
		globalThis.__omp_phase__ = String(title);
		globalThis.__omp_emit_status__("phase", { title: String(title) });
	};

	const __budgetSnap = async () => {
		const r = await globalThis.__omp_call_tool__("__budget__", {});
		return r && typeof r === "object" ? r : {};
	};

	const budget = {
		total: async () => {
			const s = await __budgetSnap();
			return s.total ?? null;
		},
		spent: async () => Number((await __budgetSnap()).spent ?? 0),
		remaining: async () => {
			const s = await __budgetSnap();
			return s.total == null ? Infinity : Math.max(0, Number(s.total) - Number(s.spent ?? 0));
		},
		hard: async () => Boolean((await __budgetSnap()).hard),
	};

	const display = value => {
		globalThis.__omp_display__(value);
	};

	const formatArgs = args => args.map(arg => (typeof arg === "string" ? arg : arg));

	const consoleBridge = {
		log: (...args) => globalThis.__omp_log__("log", ...formatArgs(args)),
		info: (...args) => globalThis.__omp_log__("info", ...formatArgs(args)),
		warn: (...args) => globalThis.__omp_log__("warn", ...formatArgs(args)),
		error: (...args) => globalThis.__omp_log__("error", ...formatArgs(args)),
		debug: (...args) => globalThis.__omp_log__("debug", ...formatArgs(args)),
		table: (data, columns) =>
			columns === undefined
				? globalThis.__omp_table__(data)
				: globalThis.__omp_table__(data, columns),
	};

	globalThis.console = consoleBridge;
	globalThis.print = consoleBridge.log;
	globalThis.display = display;
	globalThis.tool = tool;
	globalThis.llm = llm;
	globalThis.output = output;
	globalThis.agent = agent;
	globalThis.parallel = parallel;
	globalThis.pipeline = pipeline;
	globalThis.log = log;
	globalThis.phase = phase;
	globalThis.budget = budget;
	globalThis.__pool = __pool;
	globalThis.read = read;
	globalThis.write = write;
	globalThis.append = append;
	globalThis.sort = sort;
	globalThis.uniq = uniq;
	globalThis.counter = counter;
	globalThis.diff = diff;
	globalThis.tree = tree;
	globalThis.env = env;
}
