chore: apply new lint rules

old-agentic-v1^2
Travis Fischer 2023-06-10 17:51:46 -07:00
rodzic eed8e0534a
commit af1cc845b3
1 zmienionych plików z 17 dodań i 0 usunięć

Wyświetl plik

@ -272,6 +272,7 @@ export class SlackClient {
if (!apiKey) { if (!apiKey) {
throw new Error(`Error SlackClient missing required "apiKey"`) throw new Error(`Error SlackClient missing required "apiKey"`)
} }
this.api = ky.create({ this.api = ky.create({
prefixUrl: baseUrl, prefixUrl: baseUrl,
headers: { headers: {
@ -288,6 +289,7 @@ export class SlackClient {
if (!options.channel && !this.defaultChannel) { if (!options.channel && !this.defaultChannel) {
throw new Error('Error: No channel specified') throw new Error('Error: No channel specified')
} }
const res = await this.api.post('chat.postMessage', { const res = await this.api.post('chat.postMessage', {
json: { json: {
channel: this.defaultChannel, channel: this.defaultChannel,
@ -326,12 +328,15 @@ export class SlackClient {
let candidates: SlackMessage[] = [] let candidates: SlackMessage[] = []
const history = await this.fetchConversationHistory({ channel }) const history = await this.fetchConversationHistory({ channel })
const directReplies = await this.fetchReplies({ channel, ts }) const directReplies = await this.fetchReplies({ channel, ts })
if (directReplies.ok) { if (directReplies.ok) {
candidates = candidates.concat(directReplies.messages) candidates = candidates.concat(directReplies.messages)
} }
if (history.ok) { if (history.ok) {
candidates = candidates.concat(history.messages) candidates = candidates.concat(history.messages)
} }
// Filter out older messages before the message was sent and drop bot messages: // Filter out older messages before the message was sent and drop bot messages:
candidates = candidates.filter( candidates = candidates.filter(
(message) => message.ts > ts && !message.bot_id (message) => message.ts > ts && !message.bot_id
@ -362,6 +367,7 @@ export class SlackClient {
if (!channel) { if (!channel) {
throw new Error(`Error SlackClient missing required "channel"`) throw new Error(`Error SlackClient missing required "channel"`)
} }
let aborted = false let aborted = false
stopSignal?.addEventListener( stopSignal?.addEventListener(
'abort', 'abort',
@ -372,26 +378,34 @@ export class SlackClient {
) )
const res = await this.sendMessage({ text, channel }) const res = await this.sendMessage({ text, channel })
if (!res.ts) { if (!res.ts) {
throw new Error('Missing ts in response') throw new Error('Missing ts in response')
} }
const start = Date.now() const start = Date.now()
let nUserMessages = 0 let nUserMessages = 0
do { do {
if (aborted) { if (aborted) {
const reason = stopSignal?.reason || 'Aborted waiting for reply' const reason = stopSignal?.reason || 'Aborted waiting for reply'
if (reason instanceof Error) { if (reason instanceof Error) {
throw reason throw reason
} else { } else {
throw new Error(reason) throw new Error(reason)
} }
} }
const candidates = await this.fetchCandidates(channel, res.ts) const candidates = await this.fetchCandidates(channel, res.ts)
if (candidates.length > 0) { if (candidates.length > 0) {
const candidate = candidates[0] const candidate = candidates[0]
if (validate(candidate)) { if (validate(candidate)) {
return candidate return candidate
} }
if (nUserMessages !== candidates.length) { if (nUserMessages !== candidates.length) {
await this.sendMessage({ await this.sendMessage({
text: `Invalid response: ${candidate.text}. Please try again following the instructions.`, text: `Invalid response: ${candidate.text}. Please try again following the instructions.`,
@ -399,10 +413,13 @@ export class SlackClient {
thread_ts: candidate.ts thread_ts: candidate.ts
}) })
} }
nUserMessages = candidates.length nUserMessages = candidates.length
} }
await sleep(intervalMs) await sleep(intervalMs)
} while (Date.now() - start < timeoutMs) } while (Date.now() - start < timeoutMs)
throw new Error('Reached timeout waiting for reply') throw new Error('Reached timeout waiting for reply')
} }
} }