-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathuseWarmTask.ts
More file actions
112 lines (102 loc) · 3.5 KB
/
Copy pathuseWarmTask.ts
File metadata and controls
112 lines (102 loc) · 3.5 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
import { TASKS_PREWARM_SANDBOX_FLAG } from "@posthog/shared";
import { useFeatureFlag } from "posthog-react-native";
import { useEffect, useRef } from "react";
import { warmTask } from "@/features/tasks/api";
import { logger } from "@/lib/logger";
const log = logger.scope("warm-task");
const WARM_DEBOUNCE_MS = 600;
interface UseWarmTaskOptions {
repository?: string | null;
githubIntegrationId?: number | null;
branch?: string | null;
composerIsEmpty: boolean;
runtimeAdapter?: string | null;
model?: string | null;
reasoningEffort?: string | null;
sandboxEnvironmentId?: string | null;
customImageId?: string | null;
}
export function useWarmTask({
repository,
githubIntegrationId,
branch,
composerIsEmpty,
runtimeAdapter,
model,
reasoningEffort,
sandboxEnvironmentId,
customImageId,
}: UseWarmTaskOptions): void {
const enabled = useFeatureFlag(TASKS_PREWARM_SANDBOX_FLAG);
const debounceRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const lastWarmedKeyRef = useRef<string | null>(null);
const normalizedBranch = branch ?? null;
const normalizedRuntimeAdapter = runtimeAdapter ?? null;
const normalizedModel = model ?? null;
const normalizedReasoningEffort = reasoningEffort ?? null;
const normalizedSandboxEnvironmentId = sandboxEnvironmentId ?? null;
const normalizedCustomImageId = customImageId ?? null;
const eligible =
!!enabled &&
!!repository &&
githubIntegrationId != null &&
!composerIsEmpty;
const key =
repository && githubIntegrationId != null
? `${githubIntegrationId}:${repository}:${normalizedBranch ?? ""}:${normalizedRuntimeAdapter ?? ""}:${normalizedModel ?? ""}:${normalizedReasoningEffort ?? ""}:${normalizedSandboxEnvironmentId ?? ""}:${normalizedCustomImageId ?? ""}`
: null;
useEffect(() => {
const clearDebounce = (): void => {
if (debounceRef.current) {
clearTimeout(debounceRef.current);
debounceRef.current = null;
}
};
if (!eligible || !key || !repository || githubIntegrationId == null) {
clearDebounce();
return;
}
if (lastWarmedKeyRef.current === key || debounceRef.current) {
return;
}
const repo = repository;
const githubIntegration = githubIntegrationId;
const warmBranch = normalizedBranch;
const warmRuntimeAdapter = normalizedRuntimeAdapter;
const warmModel = normalizedModel;
const warmReasoningEffort = normalizedReasoningEffort;
const warmSandboxEnvironmentId = normalizedSandboxEnvironmentId;
const warmCustomImageId = normalizedCustomImageId;
debounceRef.current = setTimeout(() => {
debounceRef.current = null;
lastWarmedKeyRef.current = key;
void warmTask({
repository: repo,
github_integration: githubIntegration,
branch: warmBranch,
runtime_adapter: warmRuntimeAdapter,
model: warmModel,
reasoning_effort: warmReasoningEffort,
...(warmSandboxEnvironmentId
? { sandbox_environment_id: warmSandboxEnvironmentId }
: {}),
...(warmCustomImageId ? { custom_image_id: warmCustomImageId } : {}),
}).catch((error) => {
lastWarmedKeyRef.current = null;
log.warn("Failed to warm task", error);
});
}, WARM_DEBOUNCE_MS);
return clearDebounce;
}, [
eligible,
key,
repository,
githubIntegrationId,
normalizedBranch,
normalizedRuntimeAdapter,
normalizedModel,
normalizedReasoningEffort,
normalizedSandboxEnvironmentId,
normalizedCustomImageId,
]);
}