-
Notifications
You must be signed in to change notification settings - Fork 606
Expand file tree
/
Copy pathsession-utils.ts
More file actions
50 lines (44 loc) · 1.48 KB
/
session-utils.ts
File metadata and controls
50 lines (44 loc) · 1.48 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
import type { OrchestratorConfig } from "@composio/ao-core";
export function escapeRegex(str: string): string {
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
/** Check whether a session name matches a project prefix (strict: prefix-\d+ only). */
export function matchesPrefix(sessionName: string, prefix: string): boolean {
return new RegExp(`^${escapeRegex(prefix)}-\\d+$`).test(sessionName);
}
/** Find which project a session belongs to by matching its name against session prefixes. */
export function findProjectForSession(
config: OrchestratorConfig,
sessionName: string,
): string | null {
for (const [id, project] of Object.entries(config.projects) as Array<
[string, OrchestratorConfig["projects"][string]]
>) {
const prefix = project.sessionPrefix || id;
if (matchesPrefix(sessionName, prefix)) {
return id;
}
}
return null;
}
export function isOrchestratorSessionName(
config: OrchestratorConfig,
sessionName: string,
projectId?: string,
): boolean {
if (projectId) {
const project = config.projects[projectId];
if (project && sessionName === `${project.sessionPrefix || projectId}-orchestrator`) {
return true;
}
}
for (const [id, project] of Object.entries(config.projects) as Array<
[string, OrchestratorConfig["projects"][string]]
>) {
const prefix = project.sessionPrefix || id;
if (sessionName === `${prefix}-orchestrator`) {
return true;
}
}
return sessionName.endsWith("-orchestrator");
}