MCPcopy Create free account
hub / github.com/contextgo/contextgo / WorkerTaskManager

Class WorkerTaskManager

src/process/task/WorkerTaskManager.ts:14–70  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12import type { TChatConversation } from '@/common/config/storage';
13
14export class WorkerTaskManager implements IWorkerTaskManager {
15 private taskList: Array<{ id: string; task: IAgentManager }> = [];
16
17 constructor(
18 private readonly factory: IAgentFactory,
19 private readonly repo: IConversationRepository
20 ) {}
21
22 getTask(id: string): IAgentManager | undefined {
23 return this.taskList.find((item) => item.id === id)?.task;
24 }
25
26 async getOrBuildTask(id: string, options?: BuildConversationOptions): Promise<IAgentManager> {
27 if (!options?.skipCache) {
28 const existing = this.getTask(id);
29 if (existing) return existing;
30 }
31
32 const conversation = await this.repo.getConversation(id);
33 if (conversation) return this._buildAndCache(conversation, options);
34
35 return Promise.reject(new Error(`Conversation not found: ${id}`));
36 }
37
38 private _buildAndCache(conversation: TChatConversation, options?: BuildConversationOptions): IAgentManager {
39 const task = this.factory.create(conversation, options);
40 if (!options?.skipCache) {
41 this.taskList.push({ id: conversation.id, task });
42 }
43 return task;
44 }
45
46 addTask(id: string, task: IAgentManager): void {
47 const existing = this.taskList.find((item) => item.id === id);
48 if (existing) {
49 existing.task = task;
50 } else {
51 this.taskList.push({ id, task });
52 }
53 }
54
55 kill(id: string): void {
56 const index = this.taskList.findIndex((item) => item.id === id);
57 if (index === -1) return;
58 this.taskList[index]?.task.kill();
59 this.taskList.splice(index, 1);
60 }
61
62 clear(): void {
63 this.taskList.forEach((item) => item.task.kill());
64 this.taskList = [];
65 }
66
67 listTasks(): Array<{ id: string; type: AgentType }> {
68 return this.taskList.map((t) => ({ id: t.id, type: t.task.type }));
69 }
70}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected