MCPcopy Create free account
hub / github.com/CopilotKit/OpenTag / watchForReply

Function watchForReply

e2e/slack-api.ts:92–132  ·  view source on GitHub ↗
(args: {
  channel: string;
  parentTs: string;
  intervalMs: number;
  timeoutMs: number;
  onSample: (sample: {
    elapsedMs: number;
    text: string | undefined;
    message: SlackMessage | undefined;
  }) => Promise<void> | void;
})

Source from the content-addressed store, hash-verified

90 * settled (no length change across two consecutive samples).
91 */
92export async function watchForReply(args: {
93 channel: string;
94 parentTs: string;
95 intervalMs: number;
96 timeoutMs: number;
97 onSample: (sample: {
98 elapsedMs: number;
99 text: string | undefined;
100 message: SlackMessage | undefined;
101 }) => Promise<void> | void;
102}): Promise<{
103 finalText: string | undefined;
104 finalMessage: SlackMessage | undefined;
105}> {
106 const start = Date.now();
107 let lastMessage: SlackMessage | undefined;
108 let stableSamples = 0;
109 let lastLen = -1;
110 while (Date.now() - start < args.timeoutMs) {
111 const replies = await threadReplies(args.channel, args.parentTs);
112 // The first bot reply in the thread.
113 lastMessage = replies.find((m) => m.user === BOT_USER_ID);
114 const text = lastMessage?.text;
115 await args.onSample({
116 elapsedMs: Date.now() - start,
117 text,
118 message: lastMessage,
119 });
120 const len = text?.length ?? 0;
121 if (len === lastLen && len > 0) {
122 stableSamples++;
123 // After 3 consecutive stable samples, assume the stream has settled.
124 if (stableSamples >= 3) break;
125 } else {
126 stableSamples = 0;
127 lastLen = len;
128 }
129 await new Promise((r) => setTimeout(r, args.intervalMs));
130 }
131 return { finalText: lastMessage?.text, finalMessage: lastMessage };
132}
133
134/**
135 * Wait for a NEW bot reply in the thread, beyond the first `seenCount`

Callers 2

runCaseFunction · 0.70
runCaseFunction · 0.70

Calls 1

threadRepliesFunction · 0.85

Tested by

no test coverage detected