-
Notifications
You must be signed in to change notification settings - Fork 855
Expand file tree
/
Copy pathindex.mjs
More file actions
188 lines (173 loc) · 7.48 KB
/
index.mjs
File metadata and controls
188 lines (173 loc) · 7.48 KB
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
188
// reference: https://github.com/muharamdani/poe
import { connectWs, disconnectWs, listenWs } from './websocket.js'
import chatViewQuery from './graphql/ChatViewQuery.graphql'
import addMessageBreakMutation from './graphql/AddMessageBreakMutation.graphql'
import addHumanMessageMutation from './graphql/AddHumanMessageMutation.graphql'
import Browser from 'webextension-polyfill'
import md5 from 'md5'
const queries = {
chatViewQuery: chatViewQuery.loc.source.body,
addMessageBreakMutation: addMessageBreakMutation.loc.source.body,
addHumanMessageMutation: addHumanMessageMutation.loc.source.body,
}
export default class PoeAiClient {
constructor(chatId = null) {
this.headers = {
'Content-Type': 'application/json',
Accept: 'application/json',
Origin: 'https://poe.com',
}
this.settings = null
this.ws = null
this.chatId = chatId
this.bot = null
}
async ask(message, model, onMessage, onComplete) {
if (!this.settings) {
await this.getCredentials()
}
if (!this.bot) {
await this.initBot(model || 'Assistant')
}
if (!this.chatId) {
await this.getChatId(this.bot)
}
if (!this.ws) {
this.ws = await connectWs(this.settings)
await this.subscribe()
listenWs(this.ws, onMessage, onComplete)
}
await this.sendMsg(message)
}
async close() {
if (this.ws) {
await disconnectWs(this.ws)
this.ws = null
}
}
async getFormkey() {
const encoded = (await (await fetch('https://poe.com')).text()).match(
/<script>if\(.+\)throw new Error;(.+)<\/script>/,
)[1]
const codebook = encoded.match(/var .="([0-9a-f]+)"/)[1]
const dict = Array.from(encoded.matchAll(/\[(\d+)\]=.\[(\d+)\]/g))
let result = new Array(dict.length)
dict.forEach(([, k, v]) => {
result[k] = codebook[v]
})
return result.join('')
}
async getCredentials() {
this.headers['Cookie'] = (await Browser.cookies.getAll({ url: 'https://poe.com/' }))
.map((cookie) => {
return `${cookie.name}=${cookie.value}`
})
.join('; ')
this.settings = await (
await fetch('https://poe.com/api/settings', { headers: this.headers })
).json()
console.debug('poe settings', this.settings)
if (this.settings.tchannelData.channel)
this.headers['poe-tchannel'] = this.settings.tchannelData.channel
this.headers['poe-formkey'] = await this.getFormkey()
console.debug('poe formkey', this.headers['poe-formkey'])
}
async subscribe() {
const query = {
queryName: 'subscriptionsMutation',
variables: {
subscriptions: [
{
subscriptionName: 'messageAdded',
query:
'subscription subscriptions_messageAdded_Subscription(\n $chatId: BigInt!\n) {\n messageAdded(chatId: $chatId) {\n id\n messageId\n creationTime\n state\n ...ChatMessage_message\n ...chatHelpers_isBotMessage\n }\n}\n\nfragment ChatMessageDownvotedButton_message on Message {\n ...MessageFeedbackReasonModal_message\n ...MessageFeedbackOtherModal_message\n}\n\nfragment ChatMessageDropdownMenu_message on Message {\n id\n messageId\n vote\n text\n ...chatHelpers_isBotMessage\n}\n\nfragment ChatMessageFeedbackButtons_message on Message {\n id\n messageId\n vote\n voteReason\n ...ChatMessageDownvotedButton_message\n}\n\nfragment ChatMessageOverflowButton_message on Message {\n text\n ...ChatMessageDropdownMenu_message\n ...chatHelpers_isBotMessage\n}\n\nfragment ChatMessageSuggestedReplies_SuggestedReplyButton_message on Message {\n messageId\n}\n\nfragment ChatMessageSuggestedReplies_message on Message {\n suggestedReplies\n ...ChatMessageSuggestedReplies_SuggestedReplyButton_message\n}\n\nfragment ChatMessage_message on Message {\n id\n messageId\n text\n author\n linkifiedText\n state\n ...ChatMessageSuggestedReplies_message\n ...ChatMessageFeedbackButtons_message\n ...ChatMessageOverflowButton_message\n ...chatHelpers_isHumanMessage\n ...chatHelpers_isBotMessage\n ...chatHelpers_isChatBreak\n ...chatHelpers_useTimeoutLevel\n ...MarkdownLinkInner_message\n}\n\nfragment MarkdownLinkInner_message on Message {\n messageId\n}\n\nfragment MessageFeedbackOtherModal_message on Message {\n id\n messageId\n}\n\nfragment MessageFeedbackReasonModal_message on Message {\n id\n messageId\n}\n\nfragment chatHelpers_isBotMessage on Message {\n ...chatHelpers_isHumanMessage\n ...chatHelpers_isChatBreak\n}\n\nfragment chatHelpers_isChatBreak on Message {\n author\n}\n\nfragment chatHelpers_isHumanMessage on Message {\n author\n}\n\nfragment chatHelpers_useTimeoutLevel on Message {\n id\n state\n text\n messageId\n}\n',
},
{
subscriptionName: 'viewerStateUpdated',
query:
'subscription subscriptions_viewerStateUpdated_Subscription {\n viewerStateUpdated {\n id\n ...ChatPageBotSwitcher_viewer\n }\n}\n\nfragment BotHeader_bot on Bot {\n displayName\n ...BotImage_bot\n}\n\nfragment BotImage_bot on Bot {\n profilePicture\n displayName\n}\n\nfragment BotLink_bot on Bot {\n displayName\n}\n\nfragment ChatPageBotSwitcher_viewer on Viewer {\n availableBots {\n id\n ...BotLink_bot\n ...BotHeader_bot\n }\n}\n',
},
],
},
query:
'mutation subscriptionsMutation(\n $subscriptions: [AutoSubscriptionQuery!]!\n) {\n autoSubscribe(subscriptions: $subscriptions) {\n viewer {\n id\n }\n }\n}\n',
}
await this.makeRequest(query)
}
async makeRequest(request) {
request = JSON.stringify(request)
this.headers['poe-tag-id'] = md5(request + this.headers['poe-formkey'] + 'WpuLMiXEKKE98j56k')
const response = await fetch('https://poe.com/api/gql_POST', {
method: 'POST',
headers: this.headers,
body: request,
})
return await response.json()
}
async getChatId(bot) {
const {
data: {
chatOfBot: { chatId },
},
} = await this.makeRequest({
query: queries.chatViewQuery,
variables: {
bot,
},
})
this.chatId = chatId
return chatId
}
async initBot(bot) {
if (bot === 'Assistant') {
bot = 'capybara'
} else if (bot === 'gpt-4') {
bot = 'beaver'
} else if (bot === 'gpt-4-32k') {
bot = 'vizcacha'
} else if (bot === 'claude-instant-100k') {
bot = 'a2_100k'
} else if (bot === 'claude-2-100k') {
bot = 'a2_2'
} else if (bot === 'claude-instant') {
bot = 'a2'
} else if (bot === 'chatgpt') {
bot = 'chinchilla'
} else if (bot === 'chatgpt-16k') {
bot = 'agouti'
} else if (bot === 'Google-PaLM') {
bot = 'acouchy'
} else if (bot === 'Llama-2-7b') {
bot = 'llama_2_7b_chat'
} else if (bot === 'Llama-2-13b') {
bot = 'llama_2_13b_chat'
} else if (bot === 'Llama-2-70b') {
bot = 'llama_2_70b_chat'
} else if (bot === 'Code-Llama-7b') {
bot = 'code_llama_7b_instruct'
} else if (bot === 'Code-Llama-13b') {
bot = 'code_llama_13b_instruct'
} else if (bot === 'Code-Llama-34b') {
bot = 'code_llama_34b_instruct'
}
this.bot = bot
}
async breakMsg() {
await this.makeRequest({
query: queries.addMessageBreakMutation,
variables: { chatId: this.chatId },
})
}
async sendMsg(query) {
await this.makeRequest({
query: queries.addHumanMessageMutation,
variables: {
bot: this.bot,
chatId: this.chatId,
query: query,
source: null,
withChatBreak: false,
},
})
}
}