返回提交历史
Modified
g4f/Provider/Copilot.py
+14
-10
Modified
g4f/gui/client/static/js/chat.v1.js
+2
-0
XFEstudio/gpt4free
Ask for auth on image upload
b79a4d6a
代码差异
2 个文件
+16
-10
@@ -69,7 +69,7 @@ class Copilot(AbstractProvider):
69
69
access_token = None
70
70
headers = None
71
71
cookies = conversation.cookie_jar if conversation is not None else None
72
if cls.needs_auth:
72
if cls.needs_auth or image is not None:
73
73
if conversation is None or conversation.access_token is None:
74
74
access_token, cookies = asyncio.run(cls.get_access_token_and_cookies(proxy))
75
75
else:
@@ -109,6 +109,7 @@ class Copilot(AbstractProvider):
109
109
headers={"content-type": is_accepted_format(data)},
110
110
data=data
111
111
)
112
raise_for_status(response)
112
113
images.append({"type":"image", "url": response.json().get("url")})
113
114
114
115
wss = session.ws_connect(cls.websocket_url)
@@ -121,24 +122,27 @@ class Copilot(AbstractProvider):
121
122
}],
122
123
"mode": "chat"
123
124
}).encode(), CurlWsFlag.TEXT)
125
126
is_started = False
127
msg = None
124
128
while True:
125
129
try:
126
msg = json.loads(wss.recv()[0])
130
msg = wss.recv()[0]
131
msg = json.loads(msg)
127
132
except:
128
133
break
129
134
if msg.get("event") == "appendText":
130
135
yield msg.get("text")
131
136
elif msg.get("event") in ["done", "partCompleted"]:
132
137
break
138
if not is_started:
139
raise RuntimeError("Last message: {msg}")
133
140
134
141
@classmethod
135
142
async def get_access_token_and_cookies(cls, proxy: str = None):
136
143
if not has_nodriver:
137
144
raise MissingRequirementsError('Install "nodriver" package | pip install -U nodriver')
138
if has_platformdirs:
139
user_data_dir = user_config_dir("g4f-nodriver")
140
else:
141
user_data_dir = None
145
user_data_dir = user_config_dir("g4f-nodriver") if has_platformdirs else None
142
146
if debug.logging:
143
147
print(f"Copilot: Open nodriver with user_dir: {user_data_dir}")
144
148
browser = await nodriver.start(
@@ -146,7 +150,8 @@ class Copilot(AbstractProvider):
146
150
browser_args=None if proxy is None else [f"--proxy-server={proxy}"],
147
151
)
148
152
page = await browser.get(cls.url)
149
while True:
153
access_token = None
154
while access_token is None:
150
155
access_token = await page.evaluate("""
151
156
(() => {
152
157
for (var i = 0; i < localStorage.length; i++) {
@@ -159,9 +164,8 @@ class Copilot(AbstractProvider):
159
164
}
160
165
})()
161
166
""")
162
if access_token:
163
break
164
asyncio.sleep(1)
167
if access_token is None:
168
asyncio.sleep(1)
165
169
cookies = {}
166
170
for c in await page.send(nodriver.cdp.network.get_cookies([cls.url])):
167
171
cookies[c.name] = c.value
@@ -781,6 +781,7 @@ async function save_system_message() {
781
781
}
782
782
const hide_message = async (conversation_id, message_index =- 1) => {
783
783
const conversation = await get_conversation(conversation_id)
784
if (!conversation) return;
784
785
message_index = message_index == -1 ? conversation.items.length - 1 : message_index
785
786
const last_message = message_index in conversation.items ? conversation.items[message_index] : null;
786
787
if (last_message !== null) {
@@ -817,6 +818,7 @@ const get_message = async (conversation_id, index) => {
817
818
818
819
const add_message = async (conversation_id, role, content, provider) => {
819
820
const conversation = await get_conversation(conversation_id);
821
if (!conversation) return;
820
822
conversation.items.push({
821
823
role: role,
822
824
content: content,