Copilot.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. from __future__ import annotations
  2. import json
  3. import asyncio
  4. from http.cookiejar import CookieJar
  5. from urllib.parse import quote
  6. try:
  7. from curl_cffi.requests import Session, CurlWsFlag
  8. has_curl_cffi = True
  9. except ImportError:
  10. has_curl_cffi = False
  11. try:
  12. import nodriver
  13. has_nodriver = True
  14. except ImportError:
  15. has_nodriver = False
  16. from .base_provider import AbstractProvider, ProviderModelMixin, BaseConversation
  17. from .helper import format_prompt
  18. from ..typing import CreateResult, Messages, ImageType
  19. from ..errors import MissingRequirementsError, NoValidHarFileError
  20. from ..requests.raise_for_status import raise_for_status
  21. from ..providers.asyncio import get_running_loop
  22. from .openai.har_file import get_headers, get_har_files
  23. from ..requests import get_nodriver
  24. from ..image import ImageResponse, to_bytes, is_accepted_format
  25. from .. import debug
  26. class Conversation(BaseConversation):
  27. conversation_id: str
  28. def __init__(self, conversation_id: str):
  29. self.conversation_id = conversation_id
  30. class Copilot(AbstractProvider, ProviderModelMixin):
  31. label = "Microsoft Copilot"
  32. url = "https://copilot.microsoft.com"
  33. working = True
  34. supports_stream = True
  35. default_model = "Copilot"
  36. models = [default_model]
  37. model_aliases = {
  38. "gpt-4": "Copilot",
  39. }
  40. websocket_url = "wss://copilot.microsoft.com/c/api/chat?api-version=2"
  41. conversation_url = f"{url}/c/api/conversations"
  42. _access_token: str = None
  43. _cookies: CookieJar = None
  44. @classmethod
  45. def create_completion(
  46. cls,
  47. model: str,
  48. messages: Messages,
  49. stream: bool = False,
  50. proxy: str = None,
  51. timeout: int = 900,
  52. image: ImageType = None,
  53. conversation: Conversation = None,
  54. return_conversation: bool = False,
  55. web_search: bool = True,
  56. **kwargs
  57. ) -> CreateResult:
  58. if not has_curl_cffi:
  59. raise MissingRequirementsError('Install or update "curl_cffi" package | pip install -U curl_cffi')
  60. websocket_url = cls.websocket_url
  61. headers = None
  62. if cls.needs_auth or image is not None:
  63. if cls._access_token is None:
  64. try:
  65. cls._access_token, cls._cookies = readHAR(cls.url)
  66. except NoValidHarFileError as h:
  67. debug.log(f"Copilot: {h}")
  68. try:
  69. get_running_loop(check_nested=True)
  70. cls._access_token, cls._cookies = asyncio.run(get_access_token_and_cookies(cls.url, proxy))
  71. except MissingRequirementsError:
  72. raise h
  73. debug.log(f"Copilot: Access token: {cls._access_token[:7]}...{cls._access_token[-5:]}")
  74. websocket_url = f"{websocket_url}&accessToken={quote(cls._access_token)}"
  75. headers = {"authorization": f"Bearer {cls._access_token}"}
  76. with Session(
  77. timeout=timeout,
  78. proxy=proxy,
  79. impersonate="chrome",
  80. headers=headers,
  81. cookies=cls._cookies,
  82. ) as session:
  83. if cls._access_token is not None:
  84. cls._cookies = session.cookies.jar
  85. response = session.get("https://copilot.microsoft.com/c/api/user")
  86. raise_for_status(response)
  87. user = response.json().get('firstName')
  88. if user is None:
  89. cls._access_token = None
  90. debug.log(f"Copilot: User: {user or 'null'}")
  91. if conversation is None:
  92. response = session.post(cls.conversation_url)
  93. raise_for_status(response)
  94. conversation_id = response.json().get("id")
  95. if return_conversation:
  96. yield Conversation(conversation_id)
  97. prompt = format_prompt(messages)
  98. debug.log(f"Copilot: Created conversation: {conversation_id}")
  99. else:
  100. conversation_id = conversation.conversation_id
  101. prompt = messages[-1]["content"]
  102. debug.log(f"Copilot: Use conversation: {conversation_id}")
  103. images = []
  104. if image is not None:
  105. data = to_bytes(image)
  106. response = session.post(
  107. "https://copilot.microsoft.com/c/api/attachments",
  108. headers={"content-type": is_accepted_format(data)},
  109. data=data
  110. )
  111. raise_for_status(response)
  112. images.append({"type":"image", "url": response.json().get("url")})
  113. wss = session.ws_connect(cls.websocket_url)
  114. wss.send(json.dumps({
  115. "event": "send",
  116. "conversationId": conversation_id,
  117. "content": [*images, {
  118. "type": "text",
  119. "text": prompt,
  120. }],
  121. "mode": "chat"
  122. }).encode(), CurlWsFlag.TEXT)
  123. is_started = False
  124. msg = None
  125. image_prompt: str = None
  126. last_msg = None
  127. while True:
  128. try:
  129. msg = wss.recv()[0]
  130. msg = json.loads(msg)
  131. except:
  132. break
  133. last_msg = msg
  134. if msg.get("event") == "appendText":
  135. is_started = True
  136. yield msg.get("text")
  137. elif msg.get("event") == "generatingImage":
  138. image_prompt = msg.get("prompt")
  139. elif msg.get("event") == "imageGenerated":
  140. yield ImageResponse(msg.get("url"), image_prompt, {"preview": msg.get("thumbnailUrl")})
  141. elif msg.get("event") == "done":
  142. break
  143. elif msg.get("event") == "error":
  144. raise RuntimeError(f"Error: {msg}")
  145. elif msg.get("event") not in ["received", "startMessage", "citation", "partCompleted"]:
  146. debug.log(f"Copilot Message: {msg}")
  147. if not is_started:
  148. raise RuntimeError(f"Invalid response: {last_msg}")
  149. async def get_access_token_and_cookies(url: str, proxy: str = None, target: str = "ChatAI",):
  150. browser = await get_nodriver(proxy=proxy, user_data_dir="copilot")
  151. page = await browser.get(url)
  152. access_token = None
  153. while access_token is None:
  154. access_token = await page.evaluate("""
  155. (() => {
  156. for (var i = 0; i < localStorage.length; i++) {
  157. try {
  158. item = JSON.parse(localStorage.getItem(localStorage.key(i)));
  159. if (item.credentialType == "AccessToken"
  160. && item.expiresOn > Math.floor(Date.now() / 1000)
  161. && item.target.includes("target")) {
  162. return item.secret;
  163. }
  164. } catch(e) {}
  165. }
  166. })()
  167. """.replace('"target"', json.dumps(target)))
  168. if access_token is None:
  169. await asyncio.sleep(1)
  170. cookies = {}
  171. for c in await page.send(nodriver.cdp.network.get_cookies([url])):
  172. cookies[c.name] = c.value
  173. await page.close()
  174. return access_token, cookies
  175. def readHAR(url: str):
  176. api_key = None
  177. cookies = None
  178. for path in get_har_files():
  179. with open(path, 'rb') as file:
  180. try:
  181. harFile = json.loads(file.read())
  182. except json.JSONDecodeError:
  183. # Error: not a HAR file!
  184. continue
  185. for v in harFile['log']['entries']:
  186. if v['request']['url'].startswith(url):
  187. v_headers = get_headers(v)
  188. if "authorization" in v_headers:
  189. api_key = v_headers["authorization"].split(maxsplit=1).pop()
  190. if v['request']['cookies']:
  191. cookies = {c['name']: c['value'] for c in v['request']['cookies']}
  192. if api_key is None:
  193. raise NoValidHarFileError("No access token found in .har files")
  194. return api_key, cookies