Skip to content

Commit 37b66fe

Browse files
zoomote[bot]roomoteedelauna
authored
[Refactor] Make Task.create share its startup path with Task.run (#2146)
* refactor(task): make Task.create delegate startup to Task.run (#2145) * fix(task): document create input precedence and test startup contract --------- Co-authored-by: Roomote <[email protected]> Co-authored-by: Elliott de Launay <[email protected]>
1 parent f1be1c0 commit 37b66fe

2 files changed

Lines changed: 34 additions & 12 deletions

File tree

‎src/core/task/Task.ts‎

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,22 +1380,15 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
13801380
this.blanketDeniedCommandThisTurn = true
13811381
}
13821382

1383+
/** When provided, historyItem overrides task and images in the startup metadata. */
13831384
static create(options: TaskOptions): [Task, Promise<void>] {
1384-
const instance = new Task({ ...options, startTask: false })
1385-
const { images, task, historyItem } = options
1386-
let promise
1387-
1388-
instance.startIdleTelemetryCheck()
1389-
1390-
if (images || task) {
1391-
promise = instance.startTask(task, images)
1392-
} else if (historyItem) {
1393-
promise = instance.resumeTaskFromHistory()
1394-
} else {
1385+
if (!options.task && !options.images && !options.historyItem) {
13951386
throw new Error("Either historyItem or task/images must be provided")
13961387
}
13971388

1398-
return [instance, promise]
1389+
const instance = new Task({ ...options, startTask: false })
1390+
1391+
return [instance, instance.run()]
13991392
}
14001393

14011394
// API Messages

‎src/core/task/__tests__/Task.spec.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,35 @@ describe("Cline", () => {
14401440
})
14411441
})
14421442

1443+
describe("create", () => {
1444+
it("rejects missing task inputs before constructing the task", () => {
1445+
const onCreated = vi.fn()
1446+
1447+
expect(() => Task.create({ provider: mockProvider, apiConfiguration: mockApiConfig, onCreated })).toThrow(
1448+
"Either historyItem or task/images must be provided",
1449+
)
1450+
expect(onCreated).not.toHaveBeenCalled()
1451+
})
1452+
1453+
it("returns the cached run promise", async () => {
1454+
const startTaskSpy = vi.spyOn(getTaskTestAccess(Task.prototype), "startTask").mockResolvedValue(undefined)
1455+
1456+
try {
1457+
const [task, promise] = Task.create({
1458+
provider: mockProvider,
1459+
apiConfiguration: mockApiConfig,
1460+
task: "x",
1461+
})
1462+
1463+
expect(task.run()).toBe(promise)
1464+
expect(startTaskSpy).toHaveBeenCalledExactlyOnceWith("x", undefined)
1465+
await promise
1466+
} finally {
1467+
startTaskSpy.mockRestore()
1468+
}
1469+
})
1470+
})
1471+
14431472
describe("constructor", () => {
14441473
it.each([{ apiConfigName: "parent-local-profile" }, { apiConfigName: undefined }])(
14451474
"uses an explicit delegated-child context without shared state or startup persistence",

0 commit comments

Comments
 (0)