-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathtaskService.ts
More file actions
214 lines (192 loc) · 6.23 KB
/
Copy pathtaskService.ts
File metadata and controls
214 lines (192 loc) · 6.23 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { CLOUD_USAGE_LIMIT_ERROR_MESSAGE } from "@posthog/api-client/posthog-client";
import {
SESSION_SERVICE,
type SessionService,
} from "@posthog/core/sessions/sessionService";
import { ROOT_LOGGER, type RootLogger } from "@posthog/di/logger";
import type {
SagaResult,
TaskCreationInput,
TaskCreationOutput,
} from "@posthog/shared";
import type { Task } from "@posthog/shared/domain-types";
import { inject, injectable } from "inversify";
import { TASK_CREATION_EFFECTS, TASK_CREATION_HOST } from "./identifiers";
import type { TaskCreationEffects } from "./taskCreationEffects";
import type { ITaskCreationHost } from "./taskCreationHost";
import { TaskCreationSaga } from "./taskCreationSaga";
export type { TaskCreationInput, TaskCreationOutput };
export { TASK_SERVICE } from "./identifiers";
export type CreateTaskResult = SagaResult<TaskCreationOutput>;
/**
* True when a failed createTask was blocked by the usage limit. The upgrade modal is
* already shown in this case, so callers should suppress their own error toast.
*/
export function isUsageLimitResult(result: CreateTaskResult): boolean {
return (
!result.success &&
(result.failedStep === "usage_limit" ||
result.error === CLOUD_USAGE_LIMIT_ERROR_MESSAGE)
);
}
@injectable()
export class TaskService {
constructor(
@inject(TASK_CREATION_HOST)
private readonly host: ITaskCreationHost,
@inject(SESSION_SERVICE)
private readonly sessionService: SessionService,
@inject(TASK_CREATION_EFFECTS)
private readonly effects: TaskCreationEffects,
@inject(ROOT_LOGGER)
rootLogger: RootLogger,
) {
this.log = rootLogger.scope("task-service");
}
private readonly log: ReturnType<RootLogger["scope"]>;
public async createTask(
input: TaskCreationInput,
onTaskReady?: (output: TaskCreationOutput) => void,
options?: { skipCloudUsagePreflight?: boolean },
): Promise<CreateTaskResult> {
this.log.info("Creating task", {
workspaceMode: input.workspaceMode,
hasContent: !!input.content,
hasRepo: !!input.repository,
});
// Promptless flows (imported Claude Code sessions, worktree adoption)
// synthesize a taskDescription instead of typed content; either one names
// the task, so either satisfies validation.
const hasDescription =
!!input.content?.trim() || !!input.taskDescription?.trim();
if (!hasDescription) {
return {
success: false,
error: "Task description cannot be empty",
failedStep: "validation",
};
}
const posthogClient = await this.host.getAuthenticatedClient();
if (!posthogClient) {
return {
success: false,
error: "Not authenticated",
failedStep: "validation",
};
}
// Backstop for callers that bypass useTaskCreation (e.g. inbox); the helper shows the modal.
// Callers that already pre-flighted pass skipCloudUsagePreflight to avoid a second fetch.
if (!options?.skipCloudUsagePreflight && input.workspaceMode === "cloud") {
try {
await this.host.assertCloudUsageAvailable();
} catch (error) {
if (
error instanceof Error &&
error.message === CLOUD_USAGE_LIMIT_ERROR_MESSAGE
) {
return {
success: false,
error: CLOUD_USAGE_LIMIT_ERROR_MESSAGE,
failedStep: "usage_limit",
};
}
throw error;
}
}
const saga = new TaskCreationSaga(
{
posthogClient,
host: this.host,
sessionService: this.sessionService,
track: (event, props) => this.host.track(event, props),
onTaskReady: onTaskReady
? (output) => {
this.effects.onWorkspaceCreated(output);
this.effects.onCreateSuccess(output, input);
onTaskReady(output);
}
: undefined,
},
this.log,
);
const result = await saga.run(input);
if (result.success) {
this.effects.onWorkspaceCreated(result.data);
if (!onTaskReady) {
this.effects.onCreateSuccess(result.data, input);
}
}
return result;
}
public async openTask(
taskId: string,
taskRunId?: string,
): Promise<CreateTaskResult> {
this.log.info("Opening existing task", { taskId, taskRunId });
const posthogClient = await this.host.getAuthenticatedClient();
if (!posthogClient) {
return {
success: false,
error: "Not authenticated",
failedStep: "validation",
};
}
const existingWorkspace = await this.host.getWorkspace(taskId);
if (existingWorkspace) {
this.log.info("Workspace already exists, fetching task only", { taskId });
try {
const task = await posthogClient.getTask(taskId);
if (taskRunId) {
this.log.info("Fetching specific task run", { taskId, taskRunId });
const run = await posthogClient.getTaskRun(taskId, taskRunId);
task.latest_run = run;
}
return {
success: true,
data: {
task: task as unknown as Task,
workspace: existingWorkspace,
},
};
} catch (error) {
return {
success: false,
error:
error instanceof Error ? error.message : "Failed to fetch task",
failedStep: "fetch_task",
};
}
}
const saga = new TaskCreationSaga(
{
posthogClient,
host: this.host,
sessionService: this.sessionService,
track: (event, props) => this.host.track(event, props),
},
this.log,
);
const result = await saga.run({ taskId });
if (result.success) {
this.effects.onWorkspaceCreated(result.data);
this.effects.onCreateSuccess(result.data);
if (taskRunId && result.data.task) {
try {
this.log.info("Fetching specific task run for new workspace", {
taskId,
taskRunId,
});
const run = await posthogClient.getTaskRun(taskId, taskRunId);
result.data.task.latest_run = run;
} catch (error) {
this.log.warn("Failed to fetch specific task run, using latest", {
taskId,
taskRunId,
error,
});
}
}
}
return result;
}
}