0
List runs
One script reply has been approved by the moderators Verified

Returns a list of runs belonging to a thread.

Created by hugo697 156 days ago Viewed 5526 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 156 days ago
1
type Openai = {
2
  api_key: string;
3
  organization_id: string;
4
};
5
/**
6
 * List runs
7
 * Returns a list of runs belonging to a thread.
8
 */
9
export async function main(
10
  auth: Openai,
11
  thread_id: string,
12
  limit: string | undefined,
13
  order: "asc" | "desc" | undefined,
14
  after: string | undefined,
15
  before: string | undefined
16
) {
17
  const url = new URL(`https://api.openai.com/v1/threads/${thread_id}/runs`);
18
  for (const [k, v] of [
19
    ["limit", limit],
20
    ["order", order],
21
    ["after", after],
22
    ["before", before],
23
  ]) {
24
    if (v !== undefined && v !== "") {
25
      url.searchParams.append(k, v);
26
    }
27
  }
28
  const response = await fetch(url, {
29
    method: "GET",
30
    headers: {
31
      "OpenAI-Organization": auth.organization_id,
32
      Authorization: "Bearer " + auth.api_key,
33
    },
34
    body: undefined,
35
  });
36
  if (!response.ok) {
37
    const text = await response.text();
38
    throw new Error(`${response.status} ${text}`);
39
  }
40
  return await response.json();
41
}
42