MCPcopy
hub / github.com/CopilotKit/CopilotKit / CopilotTask

Class CopilotTask

packages/react-core/src/lib/copilot-task.ts:93–186  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

91}
92
93export class CopilotTask<T = any> {
94 private instructions: string;
95 private actions: FrontendAction<any>[];
96 private includeCopilotReadable: boolean;
97 private includeCopilotActions: boolean;
98 private forwardedParameters?: ForwardedParametersInput;
99 constructor(config: CopilotTaskConfig) {
100 this.instructions = config.instructions;
101 this.actions = config.actions || [];
102 this.includeCopilotReadable = config.includeCopilotReadable !== false;
103 this.includeCopilotActions = config.includeCopilotActions !== false;
104 this.forwardedParameters = config.forwardedParameters;
105 }
106
107 /**
108 * Run the task.
109 * @param context The CopilotContext to use for the task. Use `useCopilotContext` to obtain the current context.
110 * @param data The data to use for the task.
111 */
112 async run(context: CopilotContextParams, data?: T): Promise<void> {
113 const actions = this.includeCopilotActions
114 ? Object.assign({}, context.actions)
115 : {};
116
117 // merge functions into entry points
118 for (const fn of this.actions) {
119 actions[fn.name] = fn;
120 }
121
122 let contextString = "";
123
124 if (data) {
125 contextString =
126 (typeof data === "string" ? data : JSON.stringify(data)) + "\n\n";
127 }
128
129 if (this.includeCopilotReadable) {
130 contextString += context.getContextString(
131 [],
132 defaultCopilotContextCategories,
133 );
134 }
135
136 const systemMessage = new TextMessage({
137 content: taskSystemMessage(contextString, this.instructions),
138 role: Role.System,
139 });
140
141 const messages: Message[] = [systemMessage];
142
143 const runtimeClient = new CopilotRuntimeClient({
144 url: context.copilotApiConfig.chatApiEndpoint,
145 publicApiKey: context.copilotApiConfig.publicApiKey,
146 headers: context.copilotApiConfig.headers,
147 credentials: context.copilotApiConfig.credentials,
148 });
149
150 const response = await runtimeClient

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…