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

Function watchForChannelReply

e2e/slack-api.ts:185–225  ·  view source on GitHub ↗
(args: {
  channel: string;
  sinceTs: 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

183 * channel directly (DMs / slash commands) rather than threaded.
184 */
185export async function watchForChannelReply(args: {
186 channel: string;
187 sinceTs: string;
188 intervalMs: number;
189 timeoutMs: number;
190 onSample: (sample: {
191 elapsedMs: number;
192 text: string | undefined;
193 message: SlackMessage | undefined;
194 }) => Promise<void> | void;
195}): Promise<{
196 finalText: string | undefined;
197 finalMessage: SlackMessage | undefined;
198}> {
199 const start = Date.now();
200 let lastMessage: SlackMessage | undefined;
201 let stable = 0;
202 let lastLen = -1;
203 while (Date.now() - start < args.timeoutMs) {
204 const history = await channelHistory(args.channel, 5);
205 lastMessage = history.find(
206 (m) => m.user === BOT_USER_ID && Number(m.ts) > Number(args.sinceTs),
207 );
208 const text = lastMessage?.text;
209 await args.onSample({
210 elapsedMs: Date.now() - start,
211 text,
212 message: lastMessage,
213 });
214 const len = text?.length ?? 0;
215 if (len === lastLen && len > 0) {
216 stable++;
217 if (stable >= 3) break;
218 } else {
219 stable = 0;
220 lastLen = len;
221 }
222 await new Promise((r) => setTimeout(r, args.intervalMs));
223 }
224 return { finalText: lastMessage?.text, finalMessage: lastMessage };
225}
226
227/**
228 * Bracket-balance check.

Callers 1

runCaseFunction · 0.85

Calls 1

channelHistoryFunction · 0.85

Tested by

no test coverage detected