mirror of
https://github.com/xtekky/gpt4free.git
synced 2025-10-17 13:50:59 +08:00
Ask for auth on image upload
This commit is contained in:
@@ -69,7 +69,7 @@ class Copilot(AbstractProvider):
|
|||||||
access_token = None
|
access_token = None
|
||||||
headers = None
|
headers = None
|
||||||
cookies = conversation.cookie_jar if conversation is not None else None
|
cookies = conversation.cookie_jar if conversation is not None else None
|
||||||
if cls.needs_auth:
|
if cls.needs_auth or image is not None:
|
||||||
if conversation is None or conversation.access_token is None:
|
if conversation is None or conversation.access_token is None:
|
||||||
access_token, cookies = asyncio.run(cls.get_access_token_and_cookies(proxy))
|
access_token, cookies = asyncio.run(cls.get_access_token_and_cookies(proxy))
|
||||||
else:
|
else:
|
||||||
@@ -109,6 +109,7 @@ class Copilot(AbstractProvider):
|
|||||||
headers={"content-type": is_accepted_format(data)},
|
headers={"content-type": is_accepted_format(data)},
|
||||||
data=data
|
data=data
|
||||||
)
|
)
|
||||||
|
raise_for_status(response)
|
||||||
images.append({"type":"image", "url": response.json().get("url")})
|
images.append({"type":"image", "url": response.json().get("url")})
|
||||||
|
|
||||||
wss = session.ws_connect(cls.websocket_url)
|
wss = session.ws_connect(cls.websocket_url)
|
||||||
@@ -121,24 +122,27 @@ class Copilot(AbstractProvider):
|
|||||||
}],
|
}],
|
||||||
"mode": "chat"
|
"mode": "chat"
|
||||||
}).encode(), CurlWsFlag.TEXT)
|
}).encode(), CurlWsFlag.TEXT)
|
||||||
|
|
||||||
|
is_started = False
|
||||||
|
msg = None
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
msg = json.loads(wss.recv()[0])
|
msg = wss.recv()[0]
|
||||||
|
msg = json.loads(msg)
|
||||||
except:
|
except:
|
||||||
break
|
break
|
||||||
if msg.get("event") == "appendText":
|
if msg.get("event") == "appendText":
|
||||||
yield msg.get("text")
|
yield msg.get("text")
|
||||||
elif msg.get("event") in ["done", "partCompleted"]:
|
elif msg.get("event") in ["done", "partCompleted"]:
|
||||||
break
|
break
|
||||||
|
if not is_started:
|
||||||
|
raise RuntimeError("Last message: {msg}")
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def get_access_token_and_cookies(cls, proxy: str = None):
|
async def get_access_token_and_cookies(cls, proxy: str = None):
|
||||||
if not has_nodriver:
|
if not has_nodriver:
|
||||||
raise MissingRequirementsError('Install "nodriver" package | pip install -U nodriver')
|
raise MissingRequirementsError('Install "nodriver" package | pip install -U nodriver')
|
||||||
if has_platformdirs:
|
user_data_dir = user_config_dir("g4f-nodriver") if has_platformdirs else None
|
||||||
user_data_dir = user_config_dir("g4f-nodriver")
|
|
||||||
else:
|
|
||||||
user_data_dir = None
|
|
||||||
if debug.logging:
|
if debug.logging:
|
||||||
print(f"Copilot: Open nodriver with user_dir: {user_data_dir}")
|
print(f"Copilot: Open nodriver with user_dir: {user_data_dir}")
|
||||||
browser = await nodriver.start(
|
browser = await nodriver.start(
|
||||||
@@ -146,7 +150,8 @@ class Copilot(AbstractProvider):
|
|||||||
browser_args=None if proxy is None else [f"--proxy-server={proxy}"],
|
browser_args=None if proxy is None else [f"--proxy-server={proxy}"],
|
||||||
)
|
)
|
||||||
page = await browser.get(cls.url)
|
page = await browser.get(cls.url)
|
||||||
while True:
|
access_token = None
|
||||||
|
while access_token is None:
|
||||||
access_token = await page.evaluate("""
|
access_token = await page.evaluate("""
|
||||||
(() => {
|
(() => {
|
||||||
for (var i = 0; i < localStorage.length; i++) {
|
for (var i = 0; i < localStorage.length; i++) {
|
||||||
@@ -159,9 +164,8 @@ class Copilot(AbstractProvider):
|
|||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
""")
|
""")
|
||||||
if access_token:
|
if access_token is None:
|
||||||
break
|
asyncio.sleep(1)
|
||||||
asyncio.sleep(1)
|
|
||||||
cookies = {}
|
cookies = {}
|
||||||
for c in await page.send(nodriver.cdp.network.get_cookies([cls.url])):
|
for c in await page.send(nodriver.cdp.network.get_cookies([cls.url])):
|
||||||
cookies[c.name] = c.value
|
cookies[c.name] = c.value
|
||||||
|
@@ -781,6 +781,7 @@ async function save_system_message() {
|
|||||||
}
|
}
|
||||||
const hide_message = async (conversation_id, message_index =- 1) => {
|
const hide_message = async (conversation_id, message_index =- 1) => {
|
||||||
const conversation = await get_conversation(conversation_id)
|
const conversation = await get_conversation(conversation_id)
|
||||||
|
if (!conversation) return;
|
||||||
message_index = message_index == -1 ? conversation.items.length - 1 : message_index
|
message_index = message_index == -1 ? conversation.items.length - 1 : message_index
|
||||||
const last_message = message_index in conversation.items ? conversation.items[message_index] : null;
|
const last_message = message_index in conversation.items ? conversation.items[message_index] : null;
|
||||||
if (last_message !== null) {
|
if (last_message !== null) {
|
||||||
@@ -817,6 +818,7 @@ const get_message = async (conversation_id, index) => {
|
|||||||
|
|
||||||
const add_message = async (conversation_id, role, content, provider) => {
|
const add_message = async (conversation_id, role, content, provider) => {
|
||||||
const conversation = await get_conversation(conversation_id);
|
const conversation = await get_conversation(conversation_id);
|
||||||
|
if (!conversation) return;
|
||||||
conversation.items.push({
|
conversation.items.push({
|
||||||
role: role,
|
role: role,
|
||||||
content: content,
|
content: content,
|
||||||
|
Reference in New Issue
Block a user