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

	const isNil = value => value === undefined || value === null;
	const isPlainObject = value => value !== null && typeof value === "object" && !Array.isArray(value);
	const positionalOptions = (name, args, keys, example) => {
		for (let index = keys.length; index < args.length; index++) {
			if (!isNil(args[index])) {
				throw new TypeError(
					`${name}() accepts at most ${keys.length} positional optional args; got ${args.length}. Pass ${name}(..., ${example}) for named options.`,
				);
			}
		}
		const options = {};
		for (let index = 0; index < keys.length && index < args.length; index++) {
			const value = args[index];
			if (!isNil(value)) options[keys[index]] = value;
		}
		return options;
	};
	const optionsArg = (name, value, rest, keys, example) => {
		if (isNil(value)) return positionalOptions(name, [value, ...rest], keys, example);
		if (isPlainObject(value)) {
			if (rest.some(arg => !isNil(arg))) {
				throw new TypeError(
					`${name}() takes either a single trailing options object like ${example} or positional optional args; do not mix both forms.`,
				);
			}
			return value;
		}
		if (typeof value === "object") {
			const kind = Array.isArray(value) ? "an array" : value.constructor?.name ?? "object";
			throw new TypeError(
				`${name}() options must be a plain object like ${example}, null/undefined, or positional optional args, not ${kind}.`,
			);
		}
		return positionalOptions(name, [value, ...rest], keys, example);
	};
	const callHelper = (name, ...args) => globalThis.__omp_helpers__[name](...args);
	const hasScheme = path => typeof path === "string" && /^[A-Za-z][A-Za-z0-9+.-]*:\/\//.test(path);
	const shouldDelegateRead = path => hasScheme(path) && !path.toLowerCase().startsWith("local://");
	const withReadLineSelector = (path, options) => {
		const offset = typeof options.offset === "number" ? options.offset : 1;
		const limit = typeof options.limit === "number" ? options.limit : undefined;
		if (offset <= 1 && limit === undefined) return path;
		if (limit !== undefined && limit <= 0) return null;
		const start = Math.max(1, offset);
		if (limit === undefined) return `${path}:${start}-`;
		return `${path}:${start}-${start + limit - 1}`;
	};
	const readToolText = async path => {
		const res = await globalThis.__omp_call_tool__("read", { path });
		return res && typeof res === "object" && "text" in res ? res.text : res;
	};


	const read = async (path, opts, ...rest) => {
		const options = optionsArg("read", opts, rest, ["offset", "limit"], "{ offset, limit }");
		if (shouldDelegateRead(path)) {
			const toolPath = withReadLineSelector(path, options);
			return toolPath === null ? "" : readToolText(toolPath);
		}
		return callHelper("read", path, options);
	};
	const write = async (path, data) => callHelper("writeFile", path, data);
	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 completion = async (prompt, opts, ...rest) => {
		const o = optionsArg("completion", opts, rest, ["model", "system", "schema"], "{ model, system, schema }");
		const res = await globalThis.__omp_call_tool__("__completion__", { prompt, ...o });
		const text = res && typeof res === "object" ? res.text : res;
		return hasOwn(o, "schema") ? JSON.parse(text) : text;
	};

	const agent = async (prompt, opts, ...rest) => {
		const o = optionsArg(
			"agent",
			opts,
			rest,
			["agent", "model", "label", "schema", "isolated", "apply", "merge", "schemaMode"],
			"{ agent, model, label, schema, isolated, apply, merge, schemaMode, handle }",
		);
		const { handle, ...callArgs } = o;
		const res = await globalThis.__omp_call_tool__("__agent__", { prompt, ...callArgs, handle: Boolean(handle) });
		const text = res && typeof res === "object" ? res.text : res;
		const hasData = res && typeof res === "object" && hasOwn(res, "data");
		const parsed = hasData ? res.data : hasOwn(callArgs, "schema") ? JSON.parse(text) : text;
		if (!handle) return parsed;
		const details = res && typeof res === "object" ? res.details : undefined;
		if (!details || typeof details !== "object" || details.id == null) {
			return { text, output: text, handle: null, id: null, agent: null };
		}
		const node = { text, output: text, handle: `agent://${details.id}`, id: details.id, agent: details.agent ?? null };
		if (hasData || hasOwn(callArgs, "schema")) node.data = parsed;
		for (const key of ["isolated", "patchPath", "branchName", "nestedPatches", "changesApplied", "isolationSummary"]) {
			if (details[key] !== undefined) node[key] = details[key];
		}
		return node;
	};

	// 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);
		// Barrier semantics (mirrors the Python _pool_map): every item settles
		// before we return or throw, then the lowest-index error propagates.
		// Early-rejecting would orphan in-flight thunks (e.g. live agent()
		// subagents) whose worker-side promises would never be observed.
		const errors = new Map();
		let next = 0;
		const worker = async () => {
			while (true) {
				const index = next++;
				if (index >= list.length) return;
				try {
					results[index] = await fn(list[index], index);
				} catch (error) {
					errors.set(index, error);
				}
			}
		};
		await Promise.all(Array.from({ length: concurrency }, () => worker()));
		if (errors.size > 0) throw errors.get(Math.min(...errors.keys()));
		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 consoleTimers = new Map();
	const consoleCounts = new Map();
	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),
		dir: (value, _options) => globalThis.__omp_log__("log", value),
		dirxml: (...args) => globalThis.__omp_log__("log", ...formatArgs(args)),
		trace: (...args) => {
			const stack = (new Error().stack ?? "").split("\n").slice(2).join("\n");
			globalThis.__omp_log__("log", args.length > 0 ? `Trace: ${formatArgs(args).join(" ")}` : "Trace", `\n${stack}`);
		},
		assert: (condition, ...args) => {
			if (condition) return;
			if (args.length > 0) globalThis.__omp_log__("error", "Assertion failed:", ...formatArgs(args));
			else globalThis.__omp_log__("error", "Assertion failed");
		},
		group: (...args) => {
			if (args.length > 0) globalThis.__omp_log__("log", ...formatArgs(args));
		},
		groupCollapsed: (...args) => {
			if (args.length > 0) globalThis.__omp_log__("log", ...formatArgs(args));
		},
		groupEnd: () => {},
		time: label => {
			consoleTimers.set(String(label ?? "default"), Date.now());
		},
		timeLog: (label, ...args) => {
			const key = String(label ?? "default");
			const start = consoleTimers.get(key);
			if (start === undefined) {
				globalThis.__omp_log__("warn", `Timer '${key}' does not exist`);
				return;
			}
			globalThis.__omp_log__("log", `${key}: ${Date.now() - start}ms`, ...formatArgs(args));
		},
		timeEnd: label => {
			const key = String(label ?? "default");
			const start = consoleTimers.get(key);
			if (start === undefined) {
				globalThis.__omp_log__("warn", `Timer '${key}' does not exist`);
				return;
			}
			consoleTimers.delete(key);
			globalThis.__omp_log__("log", `${key}: ${Date.now() - start}ms`);
		},
		count: label => {
			const key = String(label ?? "default");
			const next = (consoleCounts.get(key) ?? 0) + 1;
			consoleCounts.set(key, next);
			globalThis.__omp_log__("log", `${key}: ${next}`);
		},
		countReset: label => {
			consoleCounts.delete(String(label ?? "default"));
		},
	};

	globalThis.console = consoleBridge;
	globalThis.print = consoleBridge.log;
	globalThis.display = display;
	globalThis.tool = tool;
	globalThis.completion = completion;
	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.env = env;
}
