MCPcopy Index your code
hub / github.com/github/copilot-sdk / sendAndWait

Method sendAndWait

nodejs/src/session.ts:282–337  ·  view source on GitHub ↗
(
        optionsOrPrompt: MessageOptions | string,
        timeout?: number
    )

Source from the content-addressed store, hash-verified

280 timeout?: number
281 ): Promise<AssistantMessageEvent | undefined>;
282 async sendAndWait(
283 optionsOrPrompt: MessageOptions | string,
284 timeout?: number
285 ): Promise<AssistantMessageEvent | undefined> {
286 const options: MessageOptions =
287 typeof optionsOrPrompt === "string" ? { prompt: optionsOrPrompt } : optionsOrPrompt;
288 const effectiveTimeout = timeout ?? 60_000;
289
290 let resolveIdle: () => void;
291 let rejectWithError: (error: Error) => void;
292 const idlePromise = new Promise<void>((resolve, reject) => {
293 resolveIdle = resolve;
294 rejectWithError = reject;
295 });
296
297 let lastAssistantMessage: AssistantMessageEvent | undefined;
298
299 // Register event handler BEFORE calling send to avoid race condition
300 // where session.idle fires before we start listening
301 const unsubscribe = this.on((event) => {
302 if (event.type === "assistant.message") {
303 lastAssistantMessage = event;
304 } else if (event.type === "session.idle") {
305 resolveIdle();
306 } else if (event.type === "session.error") {
307 const error = new Error(event.data.message);
308 error.stack = event.data.stack;
309 rejectWithError(error);
310 }
311 });
312
313 let timeoutId: ReturnType<typeof setTimeout> | undefined;
314 try {
315 await this.send(options);
316
317 const timeoutPromise = new Promise<never>((_, reject) => {
318 timeoutId = setTimeout(
319 () =>
320 reject(
321 new Error(
322 `Timeout after ${effectiveTimeout}ms waiting for session.idle`
323 )
324 ),
325 effectiveTimeout
326 );
327 });
328 await Promise.race([idlePromise, timeoutPromise]);
329
330 return lastAssistantMessage;
331 } finally {
332 if (timeoutId !== undefined) {
333 clearTimeout(timeoutId);
334 }
335 unsubscribe();
336 }
337 }
338
339 /** @internal */

Calls 3

onMethod · 0.95
sendMethod · 0.95
unsubscribeFunction · 0.50

Tested by 3

sendAndGetNextExchangeFunction · 0.36
runTurnFunction · 0.36
assertRoutingFunction · 0.36