177 lines
		
	
	
		
			6.9 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			177 lines
		
	
	
		
			6.9 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
 | 
						|
import { APIResource } from "../../../../core/resource.mjs";
 | 
						|
import * as StepsAPI from "./steps.mjs";
 | 
						|
import { Steps, } from "./steps.mjs";
 | 
						|
import { CursorPage } from "../../../../core/pagination.mjs";
 | 
						|
import { buildHeaders } from "../../../../internal/headers.mjs";
 | 
						|
import { AssistantStream } from "../../../../lib/AssistantStream.mjs";
 | 
						|
import { sleep } from "../../../../internal/utils/sleep.mjs";
 | 
						|
import { path } from "../../../../internal/utils/path.mjs";
 | 
						|
/**
 | 
						|
 * @deprecated The Assistants API is deprecated in favor of the Responses API
 | 
						|
 */
 | 
						|
export class Runs extends APIResource {
 | 
						|
    constructor() {
 | 
						|
        super(...arguments);
 | 
						|
        this.steps = new StepsAPI.Steps(this._client);
 | 
						|
    }
 | 
						|
    create(threadID, params, options) {
 | 
						|
        const { include, ...body } = params;
 | 
						|
        return this._client.post(path `/threads/${threadID}/runs`, {
 | 
						|
            query: { include },
 | 
						|
            body,
 | 
						|
            ...options,
 | 
						|
            headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),
 | 
						|
            stream: params.stream ?? false,
 | 
						|
        });
 | 
						|
    }
 | 
						|
    /**
 | 
						|
     * Retrieves a run.
 | 
						|
     *
 | 
						|
     * @deprecated The Assistants API is deprecated in favor of the Responses API
 | 
						|
     */
 | 
						|
    retrieve(runID, params, options) {
 | 
						|
        const { thread_id } = params;
 | 
						|
        return this._client.get(path `/threads/${thread_id}/runs/${runID}`, {
 | 
						|
            ...options,
 | 
						|
            headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),
 | 
						|
        });
 | 
						|
    }
 | 
						|
    /**
 | 
						|
     * Modifies a run.
 | 
						|
     *
 | 
						|
     * @deprecated The Assistants API is deprecated in favor of the Responses API
 | 
						|
     */
 | 
						|
    update(runID, params, options) {
 | 
						|
        const { thread_id, ...body } = params;
 | 
						|
        return this._client.post(path `/threads/${thread_id}/runs/${runID}`, {
 | 
						|
            body,
 | 
						|
            ...options,
 | 
						|
            headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),
 | 
						|
        });
 | 
						|
    }
 | 
						|
    /**
 | 
						|
     * Returns a list of runs belonging to a thread.
 | 
						|
     *
 | 
						|
     * @deprecated The Assistants API is deprecated in favor of the Responses API
 | 
						|
     */
 | 
						|
    list(threadID, query = {}, options) {
 | 
						|
        return this._client.getAPIList(path `/threads/${threadID}/runs`, (CursorPage), {
 | 
						|
            query,
 | 
						|
            ...options,
 | 
						|
            headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),
 | 
						|
        });
 | 
						|
    }
 | 
						|
    /**
 | 
						|
     * Cancels a run that is `in_progress`.
 | 
						|
     *
 | 
						|
     * @deprecated The Assistants API is deprecated in favor of the Responses API
 | 
						|
     */
 | 
						|
    cancel(runID, params, options) {
 | 
						|
        const { thread_id } = params;
 | 
						|
        return this._client.post(path `/threads/${thread_id}/runs/${runID}/cancel`, {
 | 
						|
            ...options,
 | 
						|
            headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),
 | 
						|
        });
 | 
						|
    }
 | 
						|
    /**
 | 
						|
     * A helper to create a run an poll for a terminal state. More information on Run
 | 
						|
     * lifecycles can be found here:
 | 
						|
     * https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
 | 
						|
     */
 | 
						|
    async createAndPoll(threadId, body, options) {
 | 
						|
        const run = await this.create(threadId, body, options);
 | 
						|
        return await this.poll(run.id, { thread_id: threadId }, options);
 | 
						|
    }
 | 
						|
    /**
 | 
						|
     * Create a Run stream
 | 
						|
     *
 | 
						|
     * @deprecated use `stream` instead
 | 
						|
     */
 | 
						|
    createAndStream(threadId, body, options) {
 | 
						|
        return AssistantStream.createAssistantStream(threadId, this._client.beta.threads.runs, body, options);
 | 
						|
    }
 | 
						|
    /**
 | 
						|
     * A helper to poll a run status until it reaches a terminal state. More
 | 
						|
     * information on Run lifecycles can be found here:
 | 
						|
     * https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
 | 
						|
     */
 | 
						|
    async poll(runId, params, options) {
 | 
						|
        const headers = buildHeaders([
 | 
						|
            options?.headers,
 | 
						|
            {
 | 
						|
                'X-Stainless-Poll-Helper': 'true',
 | 
						|
                'X-Stainless-Custom-Poll-Interval': options?.pollIntervalMs?.toString() ?? undefined,
 | 
						|
            },
 | 
						|
        ]);
 | 
						|
        while (true) {
 | 
						|
            const { data: run, response } = await this.retrieve(runId, params, {
 | 
						|
                ...options,
 | 
						|
                headers: { ...options?.headers, ...headers },
 | 
						|
            }).withResponse();
 | 
						|
            switch (run.status) {
 | 
						|
                //If we are in any sort of intermediate state we poll
 | 
						|
                case 'queued':
 | 
						|
                case 'in_progress':
 | 
						|
                case 'cancelling':
 | 
						|
                    let sleepInterval = 5000;
 | 
						|
                    if (options?.pollIntervalMs) {
 | 
						|
                        sleepInterval = options.pollIntervalMs;
 | 
						|
                    }
 | 
						|
                    else {
 | 
						|
                        const headerInterval = response.headers.get('openai-poll-after-ms');
 | 
						|
                        if (headerInterval) {
 | 
						|
                            const headerIntervalMs = parseInt(headerInterval);
 | 
						|
                            if (!isNaN(headerIntervalMs)) {
 | 
						|
                                sleepInterval = headerIntervalMs;
 | 
						|
                            }
 | 
						|
                        }
 | 
						|
                    }
 | 
						|
                    await sleep(sleepInterval);
 | 
						|
                    break;
 | 
						|
                //We return the run in any terminal state.
 | 
						|
                case 'requires_action':
 | 
						|
                case 'incomplete':
 | 
						|
                case 'cancelled':
 | 
						|
                case 'completed':
 | 
						|
                case 'failed':
 | 
						|
                case 'expired':
 | 
						|
                    return run;
 | 
						|
            }
 | 
						|
        }
 | 
						|
    }
 | 
						|
    /**
 | 
						|
     * Create a Run stream
 | 
						|
     */
 | 
						|
    stream(threadId, body, options) {
 | 
						|
        return AssistantStream.createAssistantStream(threadId, this._client.beta.threads.runs, body, options);
 | 
						|
    }
 | 
						|
    submitToolOutputs(runID, params, options) {
 | 
						|
        const { thread_id, ...body } = params;
 | 
						|
        return this._client.post(path `/threads/${thread_id}/runs/${runID}/submit_tool_outputs`, {
 | 
						|
            body,
 | 
						|
            ...options,
 | 
						|
            headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),
 | 
						|
            stream: params.stream ?? false,
 | 
						|
        });
 | 
						|
    }
 | 
						|
    /**
 | 
						|
     * A helper to submit a tool output to a run and poll for a terminal run state.
 | 
						|
     * More information on Run lifecycles can be found here:
 | 
						|
     * https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
 | 
						|
     */
 | 
						|
    async submitToolOutputsAndPoll(runId, params, options) {
 | 
						|
        const run = await this.submitToolOutputs(runId, params, options);
 | 
						|
        return await this.poll(run.id, params, options);
 | 
						|
    }
 | 
						|
    /**
 | 
						|
     * Submit the tool outputs from a previous run and stream the run to a terminal
 | 
						|
     * state. More information on Run lifecycles can be found here:
 | 
						|
     * https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
 | 
						|
     */
 | 
						|
    submitToolOutputsStream(runId, params, options) {
 | 
						|
        return AssistantStream.createToolAssistantStream(runId, this._client.beta.threads.runs, params, options);
 | 
						|
    }
 | 
						|
}
 | 
						|
Runs.Steps = Steps;
 | 
						|
//# sourceMappingURL=runs.mjs.map
 |