const require_base = require("../base.cjs");
//#region src/client/runs/index.ts
var RunsClient = class extends require_base.BaseClient {
	/**
	* Create a run and stream the results.
	*
	* @param threadId The ID of the thread.
	* @param assistantId Assistant ID to use for this run.
	* @param payload Payload for creating a run.
	*/
	async *stream(threadId, assistantId, payload) {
		const json = {
			input: payload?.input,
			command: payload?.command,
			config: payload?.config,
			context: payload?.context,
			metadata: payload?.metadata,
			stream_mode: payload?.streamMode,
			stream_subgraphs: payload?.streamSubgraphs,
			stream_resumable: payload?.streamResumable,
			feedback_keys: payload?.feedbackKeys,
			assistant_id: assistantId,
			interrupt_before: payload?.interruptBefore,
			interrupt_after: payload?.interruptAfter,
			checkpoint: payload?.checkpoint,
			webhook: payload?.webhook,
			multitask_strategy: payload?.multitaskStrategy,
			on_completion: payload?.onCompletion,
			on_disconnect: payload?.onDisconnect,
			after_seconds: payload?.afterSeconds,
			if_not_exists: payload?.ifNotExists,
			checkpoint_during: payload?.checkpointDuring,
			durability: payload?.durability
		};
		yield* this.streamWithRetry({
			endpoint: threadId == null ? `/runs/stream` : `/threads/${threadId}/runs/stream`,
			method: "POST",
			json,
			signal: payload?.signal,
			idleReconnect: payload?.streamIdleReconnect,
			onInitialResponse: (response) => {
				const runMetadata = require_base.getRunMetadataFromResponse(response);
				if (runMetadata) payload?.onRunCreated?.(runMetadata);
			}
		});
	}
	/**
	* Create a run.
	*
	* @param threadId The ID of the thread.
	* @param assistantId Assistant ID to use for this run.
	* @param payload Payload for creating a run.
	* @returns The created run.
	*/
	async create(threadId, assistantId, payload) {
		const json = {
			input: payload?.input,
			command: payload?.command,
			config: payload?.config,
			context: payload?.context,
			metadata: payload?.metadata,
			stream_mode: payload?.streamMode,
			stream_subgraphs: payload?.streamSubgraphs,
			stream_resumable: payload?.streamResumable,
			feedback_keys: payload?.feedbackKeys,
			assistant_id: assistantId,
			interrupt_before: payload?.interruptBefore,
			interrupt_after: payload?.interruptAfter,
			webhook: payload?.webhook,
			checkpoint: payload?.checkpoint,
			checkpoint_id: payload?.checkpointId,
			multitask_strategy: payload?.multitaskStrategy,
			after_seconds: payload?.afterSeconds,
			if_not_exists: payload?.ifNotExists,
			checkpoint_during: payload?.checkpointDuring,
			durability: payload?.durability,
			on_completion: payload?.onCompletion,
			langsmith_tracer: payload?._langsmithTracer ? {
				project_name: payload?._langsmithTracer?.projectName,
				example_id: payload?._langsmithTracer?.exampleId
			} : void 0
		};
		const endpoint = threadId === null ? "/runs" : `/threads/${threadId}/runs`;
		const [run, response] = await this.fetch(endpoint, {
			method: "POST",
			json,
			signal: payload?.signal,
			withResponse: true
		});
		const runMetadata = require_base.getRunMetadataFromResponse(response);
		if (runMetadata) payload?.onRunCreated?.(runMetadata);
		return run;
	}
	/**
	* Create a batch of stateless background runs.
	*
	* @param payloads An array of payloads for creating runs.
	* @returns An array of created runs.
	*/
	async createBatch(payloads, options) {
		const filteredPayloads = payloads.map((payload) => ({
			...payload,
			assistant_id: payload.assistantId
		})).map((payload) => {
			return Object.fromEntries(Object.entries(payload).filter(([_, v]) => v !== void 0));
		});
		return this.fetch("/runs/batch", {
			method: "POST",
			json: filteredPayloads,
			signal: options?.signal
		});
	}
	/**
	* Create a run and wait for it to complete.
	*
	* @param threadId The ID of the thread.
	* @param assistantId Assistant ID to use for this run.
	* @param payload Payload for creating a run.
	* @returns The last values chunk of the thread.
	*/
	async wait(threadId, assistantId, payload) {
		const json = {
			input: payload?.input,
			command: payload?.command,
			config: payload?.config,
			context: payload?.context,
			metadata: payload?.metadata,
			assistant_id: assistantId,
			interrupt_before: payload?.interruptBefore,
			interrupt_after: payload?.interruptAfter,
			checkpoint: payload?.checkpoint,
			checkpoint_id: payload?.checkpointId,
			webhook: payload?.webhook,
			multitask_strategy: payload?.multitaskStrategy,
			on_completion: payload?.onCompletion,
			on_disconnect: payload?.onDisconnect,
			after_seconds: payload?.afterSeconds,
			if_not_exists: payload?.ifNotExists,
			checkpoint_during: payload?.checkpointDuring,
			durability: payload?.durability,
			langsmith_tracer: payload?._langsmithTracer ? {
				project_name: payload?._langsmithTracer?.projectName,
				example_id: payload?._langsmithTracer?.exampleId
			} : void 0
		};
		const endpoint = threadId == null ? `/runs/wait` : `/threads/${threadId}/runs/wait`;
		const [run, response] = await this.fetch(endpoint, {
			method: "POST",
			json,
			timeoutMs: null,
			signal: payload?.signal,
			withResponse: true
		});
		const runMetadata = require_base.getRunMetadataFromResponse(response);
		if (runMetadata) payload?.onRunCreated?.(runMetadata);
		if ((payload?.raiseError !== void 0 ? payload.raiseError : true) && "__error__" in run && typeof run.__error__ === "object" && run.__error__ && "error" in run.__error__ && "message" in run.__error__) throw new Error(`${run.__error__?.error}: ${run.__error__?.message}`);
		return run;
	}
	/**
	* List all runs for a thread.
	*
	* @param threadId The ID of the thread.
	* @param options Filtering and pagination options.
	* @returns List of runs.
	*/
	async list(threadId, options) {
		return this.fetch(`/threads/${threadId}/runs`, {
			params: {
				limit: options?.limit ?? 10,
				offset: options?.offset ?? 0,
				status: options?.status ?? void 0,
				select: options?.select ?? void 0
			},
			signal: options?.signal
		});
	}
	/**
	* Get a run by ID.
	*
	* @param threadId The ID of the thread.
	* @param runId The ID of the run.
	* @returns The run.
	*/
	async get(threadId, runId, options) {
		return this.fetch(`/threads/${threadId}/runs/${runId}`, { signal: options?.signal });
	}
	/**
	* Cancel a run.
	*
	* @param threadId The ID of the thread.
	* @param runId The ID of the run.
	* @param wait Whether to block when canceling
	* @param action Action to take when cancelling the run. Possible values are `interrupt` or `rollback`. Default is `interrupt`.
	* @returns
	*/
	async cancel(threadId, runId, wait = false, action = "interrupt", options = {}) {
		return this.fetch(`/threads/${threadId}/runs/${runId}/cancel`, {
			method: "POST",
			params: {
				wait: wait ? "1" : "0",
				action
			},
			signal: options?.signal
		});
	}
	/**
	* Cancel one or more runs.
	*
	* @param options Options for cancelling runs.
	* @returns
	*/
	async cancelMany(options) {
		return this.fetch(`/runs/cancel`, {
			method: "POST",
			json: {
				thread_id: options.threadId,
				run_ids: options.runIds,
				status: options.status
			},
			params: { action: options.action },
			signal: options.signal
		});
	}
	/**
	* Block until a run is done.
	*
	* @param threadId The ID of the thread.
	* @param runId The ID of the run.
	* @returns
	*/
	async join(threadId, runId, options) {
		return this.fetch(`/threads/${threadId}/runs/${runId}/join`, {
			timeoutMs: null,
			params: { cancel_on_disconnect: options?.cancelOnDisconnect ? "1" : "0" },
			signal: options?.signal
		});
	}
	/**
	* Stream output from a run in real-time, until the run is done.
	*
	* @param threadId The ID of the thread.
	* @param runId The ID of the run.
	* @param options Additional options for controlling the stream behavior.
	* @returns An async generator yielding stream parts.
	*/
	async *joinStream(threadId, runId, options) {
		const opts = typeof options === "object" && options != null && options instanceof AbortSignal ? { signal: options } : options;
		yield* this.streamWithRetry({
			endpoint: threadId != null ? `/threads/${threadId}/runs/${runId}/stream` : `/runs/${runId}/stream`,
			method: "GET",
			signal: opts?.signal,
			idleReconnect: opts?.streamIdleReconnect,
			headers: opts?.lastEventId ? { "Last-Event-ID": opts.lastEventId } : void 0,
			params: {
				cancel_on_disconnect: opts?.cancelOnDisconnect ? "1" : "0",
				stream_mode: opts?.streamMode
			}
		});
	}
	/**
	* Delete a run.
	*
	* @param threadId The ID of the thread.
	* @param runId The ID of the run.
	* @returns
	*/
	async delete(threadId, runId, options) {
		return this.fetch(`/threads/${threadId}/runs/${runId}`, {
			method: "DELETE",
			signal: options?.signal
		});
	}
};
//#endregion
exports.RunsClient = RunsClient;

//# sourceMappingURL=index.cjs.map