Copilot.py 10 KB

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