-
Notifications
You must be signed in to change notification settings - Fork 16.4k
Expand file tree
/
Copy pathenvironment.ts
More file actions
90 lines (79 loc) · 2.75 KB
/
Copy pathenvironment.ts
File metadata and controls
90 lines (79 loc) · 2.75 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
import { config } from "../config";
import {
storeCreateEnvironment,
storeCreateSession,
storeGetEnvironment,
storeUpdateEnvironment,
storeListActiveEnvironments,
storeListActiveEnvironmentsByUsername,
storeListSessionsByEnvironment,
} from "../store";
import type { RegisterEnvironmentRequest, EnvironmentResponse } from "../types/api";
import type { EnvironmentRecord } from "../store";
function toResponse(row: EnvironmentRecord): EnvironmentResponse {
return {
id: row.id,
machine_name: row.machineName,
directory: row.directory,
branch: row.branch,
status: row.status,
username: row.username,
last_poll_at: row.lastPollAt ? row.lastPollAt.getTime() / 1000 : null,
worker_type: row.workerType,
channel_group_id: row.bridgeId,
capabilities: row.capabilities,
};
}
export function registerEnvironment(req: RegisterEnvironmentRequest & { metadata?: { worker_type?: string }; username?: string }) {
const secret = config.apiKeys[0] || "";
const workerType = req.worker_type || req.metadata?.worker_type;
const record = storeCreateEnvironment({
secret,
machineName: req.machine_name,
directory: req.directory,
branch: req.branch,
gitRepoUrl: req.git_repo_url,
maxSessions: req.max_sessions,
workerType,
bridgeId: req.bridge_id,
username: req.username,
capabilities: req.capabilities,
});
let sessionId: string | undefined;
// ACP agents: reuse existing session or create one
if (workerType === "acp") {
const existing = storeListSessionsByEnvironment(record.id);
if (existing.length > 0) {
sessionId = existing[0].id;
} else {
const session = storeCreateSession({
environmentId: record.id,
title: req.machine_name || "ACP Agent",
source: "acp",
});
sessionId = session.id;
}
}
return { environment_id: record.id, environment_secret: record.secret, status: record.status as "active", session_id: sessionId };
}
export function deregisterEnvironment(envId: string) {
storeUpdateEnvironment(envId, { status: "deregistered" });
}
export function getEnvironment(envId: string) {
return storeGetEnvironment(envId);
}
export function updatePollTime(envId: string) {
storeUpdateEnvironment(envId, { lastPollAt: new Date() });
}
export function listActiveEnvironments() {
return storeListActiveEnvironments();
}
export function listActiveEnvironmentsResponse(): EnvironmentResponse[] {
return storeListActiveEnvironments().map(toResponse);
}
export function listActiveEnvironmentsByUsername(username: string): EnvironmentResponse[] {
return storeListActiveEnvironmentsByUsername(username).map(toResponse);
}
export function reconnectEnvironment(envId: string) {
storeUpdateEnvironment(envId, { status: "active" });
}