Spaces:
Paused
Paused
File size: 5,728 Bytes
5cd6458 e6f4b4a 5cd6458 0e39520 e6f4b4a 5cd6458 0e39520 5cd6458 0e39520 e6f4b4a 5cd6458 0e39520 5cd6458 0e39520 e6f4b4a 5cd6458 0e39520 5cd6458 0e39520 5cd6458 bff1373 6351668 bff1373 5cd6458 93326ac 5cd6458 93326ac 5cd6458 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
const { v4: uuidv4 } = require('uuid');
const https = require('https');
const WebSocket = require('ws');
const { readBody, genHeaders, createBaseForm, convertToUnixTime, currentTime, buildPrompt } = require('./utils');
async function sendPromptMessage(config, prompt) {
const form = createBaseForm(config);
const headers = genHeaders(config);
form.append('ts', convertToUnixTime(new Date()));
form.append('type', 'message');
form.append('xArgs', '{}');
form.append('unfurl', '[]');
form.append('blocks', JSON.stringify([{ "type": "rich_text", "elements": [{ "type": "rich_text_section", "elements": [{ "type": "text", "text": `${prompt}` }] }] }]));
form.append('include_channel_perm_error', 'true');
form.append('client_msg_id', uuidv4());
form.append('_x_reason', 'webapp_message_send');
const options = {
method: 'POST',
headers: {
...headers,
...form.getHeaders(),
},
};
const req = https.request(`https://${config.teamId}.slack.com/api/chat.postMessage`, options, async (res) => {
try {
const response = await readBody(res, true);
console.log(response);
} catch (error) {
console.error(error);
}
});
req.on('error', (error) => {
console.error(error);
});
form.pipe(req);
}
async function sendChatReset(config) {
const form = createBaseForm(config);
const headers = genHeaders(config);
form.append('command', '/reset');
form.append('disp', '/reset');
form.append('client_token', `${new Date().getTime()}`);
form.append('_x_reason', 'executeCommand');
const options = {
method: 'POST',
headers: {
...headers,
...form.getHeaders(),
},
};
const req = https.request(`https://${config.teamId}.slack.com/api/chat.command`, options, async (res) => {
try {
const response = await readBody(res, true);
console.log(response);
} catch (error) {
console.error(error);
}
});
req.on('error', (error) => {
console.error(error);
});
form.pipe(req);
}
async function waitForWebSocketResponse(config, messages, onData) {
const headers = genHeaders(config);
return new Promise(async (resolve, reject) => {
const websocketURL = `wss://wss-primary.slack.com/?token=${config.token}`;
const websocket = new WebSocket(websocketURL, {
headers: headers,
});
const waitForConnection = new Promise((connectionResolve) => {
websocket.on('open', () => {
console.log('Connected to WebSocket');
connectionResolve();
});
});
await waitForConnection;
let messageIndex = 0;
const sendNextPrompt = async () => {
if (messageIndex < messages.length) {
const prompt = buildPrompt(messages[messageIndex]);
await sendPromptMessage(config, prompt);
messageIndex++;
}
};
await sendNextPrompt();
websocket.on('message', async (message) => {
try {
const data = JSON.parse(message);
if (data.subtype === 'message_changed') {
if (!data.message.text.endsWith("_Typing…_")) {
if (messageIndex < messages.length) {
await sendNextPrompt();
} else {
websocket.close();
resolve(data.message.text);
}
} else {
console.log(`${currentTime()} fetched ${data.message.text.length} characters...`);
if (onData) {
onData(data.message.text.split('_Typing…_')[0]);
}
}
}
} catch (error) {
console.error('Error parsing message:', error);
reject(error);
}
});
websocket.on('error', (error) => {
console.error('WebSocket error:', error.toString());
reject(error);
});
websocket.on('close', (code, reason) => {
console.log(`WebSocket closed with code ${code} and reason: ${reason.toString()}`);
});
});
}
function deleteAllMessages(config) {
const form = createBaseForm(config);
const headers = genHeaders(config);
const requestOptions = {
method: 'POST',
path: `/api/conversations.history?channel=${config.claudeId}`,
headers: {
...headers,
...form.getHeaders(),
},
};
const req = https.request(requestOptions, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
const messages = JSON.parse(data).messages;
messages.forEach((message) => {
const deleteOptions = {
method: 'POST',
path: '/api/chat.delete',
headers: {
'Content-Type': 'application/json',
},
};
const deleteReq = https.request(deleteOptions, (deleteRes) => { });
deleteReq.write(JSON.stringify({ channel: channelId, ts: message.ts }));
deleteReq.end();
});
});
});
req.end();
}
module.exports = {
sendPromptMessage,
sendChatReset,
waitForWebSocketResponse,
deleteAllMessages,
}; |