Repository navigation
Expand file tree
/
Copy pathhackbot.ts
More file actions
127 lines (112 loc) · 3.8 KB
/
Copy pathhackbot.ts
File metadata and controls
127 lines (112 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import "server-only";
import type { AgentDescriptor, RunAction, RunDoc, RunRef } from "./types";
// Thin server-side client for the hackbot-api. The API key lives here and is
// never exposed to the browser — every browser request goes through the
// /api/* route handlers, which call into this module.
export class HackbotError extends Error {
constructor(
message: string,
readonly status: number
) {
super(message);
this.name = "HackbotError";
}
}
function config(): { baseUrl: string; apiKey: string } {
const baseUrl = process.env.HACKBOT_API_URL;
const apiKey = process.env.HACKBOT_API_KEY;
if (!baseUrl) {
throw new HackbotError("HACKBOT_API_URL is not configured", 500);
}
if (!apiKey) {
throw new HackbotError("HACKBOT_API_KEY is not configured", 500);
}
return { baseUrl: baseUrl.replace(/\/$/, ""), apiKey };
}
async function request<T>(path: string, init: RequestInit = {}): Promise<T> {
const { baseUrl, apiKey } = config();
const res = await fetch(`${baseUrl}${path}`, {
...init,
headers: {
"X-API-Key": apiKey,
"Content-Type": "application/json",
...(init.headers ?? {}),
},
// Always hit the upstream fresh; run state changes over time.
cache: "no-store",
});
if (!res.ok) {
let detail = `${res.status} ${res.statusText}`;
try {
const body = await res.json();
if (body?.detail) {
detail =
typeof body.detail === "string"
? body.detail
: JSON.stringify(body.detail);
}
} catch {
// non-JSON error body; keep the status line
}
throw new HackbotError(detail, res.status);
}
if (res.status === 204) {
return undefined as T;
}
return (await res.json()) as T;
}
export function listAgents(): Promise<AgentDescriptor[]> {
return request<AgentDescriptor[]>("/agents");
}
export function createRun(
agentName: string,
inputs: Record<string, unknown>,
requestedBy?: string | null
): Promise<RunRef> {
return request<RunRef>(`/agents/${encodeURIComponent(agentName)}/runs`, {
method: "POST",
body: JSON.stringify(inputs),
headers: requestedBy ? { "X-On-Behalf-Of": requestedBy } : undefined,
});
}
export function getRun(runId: string): Promise<RunDoc> {
return request<RunDoc>(`/runs/${encodeURIComponent(runId)}`);
}
export interface ListRunsParams {
limit?: number;
offset?: number;
agent?: string;
status?: string;
requestedBy?: string;
}
export function listRuns(params: ListRunsParams = {}): Promise<RunDoc[]> {
const qs = new URLSearchParams();
qs.set("limit", String(params.limit ?? 50));
if (params.offset) qs.set("offset", String(params.offset));
if (params.agent) qs.set("agent", params.agent);
if (params.status) qs.set("status", params.status);
if (params.requestedBy) qs.set("requested_by", params.requestedBy);
return request<RunDoc[]>(`/runs?${qs.toString()}`);
}
export function listRunActions(runId: string): Promise<RunAction[]> {
return request<RunAction[]>(`/runs/${encodeURIComponent(runId)}/actions`);
}
// Manually apply all of a run's pending actions; returns their updated state.
export function applyRunActions(runId: string): Promise<RunAction[]> {
return request<RunAction[]>(
`/runs/${encodeURIComponent(runId)}/actions/apply`,
{ method: "POST" }
);
}
// Ask hackbot-api for a short-lived signed download URL for one artifact.
// `artifactName` may contain slashes; each segment is encoded individually so
// the upstream `{artifact_path:path}` route still sees the directory structure.
export function getArtifactDownloadUrl(
runId: string,
artifactName: string
): Promise<{ url: string }> {
const encodedPath = artifactName.split("/").map(encodeURIComponent).join("/");
return request<{ url: string }>(
`/runs/${encodeURIComponent(runId)}/artifacts/${encodedPath}`
);
}