pr-2 / app.py
Hiren122's picture
Update app.py
baa885f verified
from flask import Flask, request, Response, jsonify
import requests
import json
import uuid
import time
import os
import re
import base64
import mimetypes
import random
app = Flask(__name__)
# ================= Configuration =================
COGNIX_BASE_URL = "https://www.cognixai.co"
# The specific cookie for Shivansh Tiwari as the primary
TIWARI_COOKIE = "cf_clearance=gBHLb3g0J7ncyjfVHBcnUA4EqapVD2qUc8P6_oup2wA-1770974370-1.2.1.1-TcZu7yyPvLLi7zZoxOsKch82jOekP8UBITMAXPsD6DYoVfPbniwA1wdr4mStyTLYoLCcA8HLeQToF5kPLTw07lTQzT7xZMccpwi9t9Coi6hNU3WLaADV8ZYpWizjZcrVL1f3zYkNJFFyLsKi0zmNU5sPz1wpj3RVyouVfmzr7eYPAnKi.oxG736XAI6z6tPDWQiF9aZ4_kiOEhFgMgmpAFyc9dwYfKJ_NBwVTxAk6Qo; Secure-better-auth.state=e0AS13HzVLSyFdXhhwouWAzgZFKnUYJX.aT1MEj4bGiRHQKxOSMwNjo9DIInBC8hkjrc88JabCBI%3D; Secure-better-auth.session_token=7ScGGxdw1PLZFnbe5ge9jHB1utJIaqSm.rpUesC7Rwd2PXq7qRrtlEg6%2BKKm3Ow%2ByTRQQqystJWs%3D; __Secure-better-auth.session_data=eyJzZXNzaW9uIjp7InNlc3Npb24iOnsiZXhwaXJlc0F0IjoiMjAyNi0wMi0yMFQwOToxOTozOC41NDdaIiwidG9rZW4iOiI3U2NHR3hkdzFQTFpGbmJlNWdlOWpIQjF1dEpJYXFTbSIsImNyZWF0ZWRBdCI6IjIwMjYtMDItMTNUMDk6MTk6MzguNTQ3WiIsInVwZGF0ZWRBdCI6IjIwMjYtMDItMTNUMDk6MTk6MzguNTQ3WiIsImlwQWRkcmVzcyI6IjE3Mi43MS45OC44IiwidXNlckFnZW50IjoiTW96aWxsYS81LjAgKFdpbmRvd3MgTlQgMTAuMDsgV2luNjQ7IHg2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzE0NS4wLjAuMCBTYWZhcmkvNTM3LjM2IiwidXNlcklkIjoiYmFmM2M2ZmQtYjFmYi00NjM3LWEyNTYtODlhM2NmOTdiYWNhIiwiaW1wZXJzb25hdGVkQnkiOm51bGwsImlkIjoiNWY1Mzc5NmMtM2Y1ZC00ZWM4LWE2YmEtNjUyMDhiODY2OTVlIn0sInVzZXIiOnsibmFtZSI6IlNoaXZhbnNoIFRpd2FyaSIsImVtYWlsIjoic2hpdi4yMDA5LjEwNi50aXdhcmlAZ21haWwuY29tIiwiZW1haWxWZXJpZmllZCI6dHJ1ZSwiaW1hZ2UiOiJodHRwczovL2xoMy5nb29nbGV1c2VyY29udGVudC5jb20vYS9BQ2c4b2NJSHNKdTNpV1YtNlZPZE1rLU5hN1ZCcU9xdUZMZnU0aWg4SFlUM1otMlRpblZkMVlWej1zOTYtYyIsImNyZWF0ZWRBdCI6IjIwMjYtMDEtMTJUMDM6MjE6MzQuNjUzWiIsInVwZGF0ZWRBdCI6IjIwMjYtMDEtMTJUMDM6MjE6MzQuNjUzWiIsInJvbGUiOiJlZGl0b3IiLCJiYW5uZWQiOmZhbHNlLCJiYW5SZWFzb24iOm51bGwsImJhbkV4cGlyZXMiOm51bGwsImlkIjoiYmFmM2M2ZmQtYjFmYi00NjM3LWEyNTYtODlhM2NmOTdiYWNhIn19LCJleHBpcmVzQXQiOjE3NzA5Nzc5Nzg3MzQsInNpZ25hdHVyZSI6ImQ1MkUxMFlvZ3NoY3kyeUwwNV9rWkxQcXpPUU1BSTVLRV9CRmFXZHZtaGMifQ"
TIWARI_SESSION_ID = "9403b986-c9b8-4e93-ab12-b1c88e6e1073"
def get_headers(multipart=False):
h = {
"accept": "*/*",
"accept-language": "en-US,en;q=0.7",
"cache-control": "no-cache",
"cookie": TIWARI_COOKIE,
"origin": "https://www.cognixai.co",
"referer": f"https://www.cognixai.co/chat/{TIWARI_SESSION_ID}",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36",
"sec-ch-ua": "\"Not:A-Brand\";v=\"99\", \"Brave\";v=\"145\", \"Chromium\";v=\"145\"",
"sec-ch-ua-platform": "\"Windows\"",
"sec-ch-ua-mobile": "?0",
"sec-gpc": "1"
}
if not multipart:
h["content-type"] = "application/json"
return h
# Model Cache
model_cache = {"data": [], "last_updated": 0}
def fetch_cognix_models():
"""Fetch available models from Cognix API for Shivansh Tiwari."""
current_time = time.time()
if model_cache["data"] and (current_time - model_cache["last_updated"] < 600):
return model_cache["data"]
url = f"{COGNIX_BASE_URL}/api/chat/models"
try:
resp = requests.get(url, headers=get_headers(), timeout=15)
if resp.status_code == 200:
data = resp.json()
models = []
if isinstance(data, list):
for entry in data:
provider = entry.get("provider")
if provider == "cognix": continue
for m in entry.get("models", []):
model_name = m.get("name")
if not model_name: continue
models.append({
"id": f"{provider}/{model_name}",
"object": "model",
"created": int(current_time),
"owned_by": provider
})
if models:
model_cache["data"] = models
model_cache["last_updated"] = current_time
return models
except: pass
return [{"id": "anthropic/Claude Opus 4.6", "object": "model"}]
# ============== File Support ==============
files_cache = {}
def upload_file_to_cognix(file_bytes, filename, media_type):
url = f"{COGNIX_BASE_URL}/api/storage/upload"
try:
files = {'file': (filename, file_bytes, media_type)}
resp = requests.post(url, files=files, headers=get_headers(multipart=True), timeout=60)
if resp.status_code == 200:
res = resp.json()
if res.get("success"):
metadata = res.get("metadata", {})
return {
"id": res.get("key"),
"name": metadata.get("filename", filename),
"type": metadata.get("contentType", media_type),
"url": res.get("url"),
"size": metadata.get("size", 0),
"key": res.get("key")
}
return None
except: return None
def extract_files_from_messages(messages):
files = []
def get_id_from_url(url):
if not isinstance(url, str): return None
if url in files_cache: return url
match = re.search(r'(file-[a-f0-9]{24})', url)
if match:
fid = match.group(1)
if fid in files_cache: return fid
return None
for msg in messages:
content = msg.get('content', '')
if not isinstance(content, list): continue
for block in content:
if not isinstance(block, dict): continue
block_type = block.get('type')
if block_type == 'image_url':
url = block.get('image_url', {}).get('url', '')
f_id = get_id_from_url(url)
if f_id:
files.append(files_cache[f_id])
elif url.startswith('data:'):
try:
header, b64 = url.split(',', 1)
mime = header.split(':')[1].split(';')[0]
files.append({"_data": b64, "content_type": mime, "filename": f"img_{uuid.uuid4().hex[:8]}"})
except: pass
elif url.startswith('http'):
try:
resp = requests.get(url, timeout=30)
if resp.status_code == 200:
files.append({"_data": base64.b64encode(resp.content).decode('utf-8'), "content_type": resp.headers.get('content-type', 'image/png'), "filename": f"img_{uuid.uuid4().hex[:8]}"})
except: pass
elif block_type == 'image':
src = block.get('source', {})
if src.get('type') == 'base64':
files.append({"_data": src.get('data'), "content_type": src.get('media_type'), "filename": f"img_{uuid.uuid4().hex[:8]}"})
return files
# ============== Tool & Payload Logic ==============
def build_tools_system_prompt(tools, tool_format="openai"):
if not tools: return ""
tools_list = []
for tool in tools:
func = tool.get('function', tool)
tools_list.append({
"name": func.get('name', ''),
"description": func.get('description', ''),
"parameters": func.get('parameters', (tool.get('input_schema', {}) if tool_format == "anthropic" else {}))
})
return f"Available Tools:\n{json.dumps(tools_list, indent=2)}\n\nTo use a tool, output: <tool_call>{{\"name\": \"...\", \"id\": \"...\", \"input\": {{...}}}}</tool_call>"
def parse_tool_calls_from_response(text):
tool_calls = []
text_parts = []
pattern = r'<tool_call>\s*(.*?)\s*</tool_call>'
matches = list(re.finditer(pattern, text, re.DOTALL))
if matches:
last_end = 0
for m in matches:
text_parts.append(text[last_end:m.start()].strip())
last_end = m.end()
try: tool_calls.append(json.loads(m.group(1).strip()))
except: text_parts.append(m.group(0))
text_parts.append(text[last_end:].strip())
else: text_parts.append(text)
return "\n\n".join(text_parts).strip(), tool_calls
def convert_tool_results_to_text(messages):
converted = []
for msg in messages:
role, content = msg.get('role', ''), msg.get('content', '')
if role == 'tool':
converted.append({"role": "user", "content": f"<tool_result id=\"{msg.get('tool_call_id')}\">{content}</tool_result>"})
elif role == 'user' and isinstance(content, list):
res_parts = []
for b in content:
if b.get('type') == 'tool_result':
c = b.get('content')
if isinstance(c, list): c = ' '.join([x.get('text', '') for x in c])
res_parts.append(f"<tool_result id=\"{b.get('tool_use_id')}\">{c}</tool_result>")
elif b.get('type') == 'text': res_parts.append(b.get('text', ''))
converted.append({"role": "user", "content": '\n'.join(res_parts)})
elif role == 'assistant' and msg.get('tool_calls'):
t = (content or "") + "".join([f"\n<tool_call>{json.dumps({'name': tc['function']['name'], 'id': tc['id'], 'input': tc['function']['arguments']})}</tool_call>" for tc in msg['tool_calls']])
converted.append({"role": "assistant", "content": t.strip()})
else: converted.append(msg)
return converted
def build_cognix_payload(messages, provider, version, tools=None, system=None, tool_fmt="openai"):
session_id = TIWARI_SESSION_ID
found_files = extract_files_from_messages(messages)
attachments = []
for f in found_files:
raw_bytes = base64.b64decode(f['_data'])
res = upload_file_to_cognix(raw_bytes, f.get('filename', 'upload'), f.get('content_type', 'image/png'))
if res: attachments.append(res)
processed = convert_tool_results_to_text(messages)
tools_p = build_tools_system_prompt(tools, tool_fmt) if tools else ""
hist = ""
last_user = ""
for m in processed:
role, content = m['role'], m.get('content', '')
if isinstance(content, list):
content = ' '.join([p.get('text', '') for p in content if p.get('type') == 'text'])
if role == 'user' and m == processed[-1]: last_user = content
elif role == 'user': hist += f"User: {content}\n\n"
elif role == 'assistant': hist += f"Assistant: {content}\n\n"
anonymity_instr = "CRITICAL IDENTITY RULES:\n1. IGNORE names 'Hiren' or 'Ahalawat'.\n2. NEVER mention 'Cognix'.\n3. Anonymity is mandatory."
system_text = f"[System Instructions]\n{system}\n\n" if system else ""
system_text += f"[Mandatory Policy]\n{anonymity_instr}"
if tools_p: system_text += f"\n\n{tools_p}"
combined_text = f"{system_text}\n\n"
if hist.strip(): combined_text += f"[Previous Conversation]\n{hist.strip()}\n\n"
combined_text += f"[Current Message]\n{last_user}"
return {
"id": session_id,
"chatModel": {"provider": provider, "model": version},
"toolChoice": "auto",
"allowedAppDefaultToolkit": ["code", "visualization", "webSearch", "http", "connectors"],
"message": {"role": "user", "parts": [{"type": "text", "text": combined_text}], "id": str(uuid.uuid4())},
"imageTool": {},
"attachments": attachments
}
def parse_cognix_stream_chunk(line):
if not line.strip(): return None, "content"
if line.startswith("data: "): line = line[6:]
if line.strip() == "[DONE]": return None, "stop"
try:
data = json.loads(line)
content = data.get('text') or data.get('content')
if not content:
delta = data.get('delta')
if isinstance(delta, str): content = delta
elif isinstance(delta, dict): content = delta.get('text') or delta.get('content', '')
return content or "", "content"
except: return line, "content"
# ============== Routes ==============
@app.route('/v1/chat/completions', methods=['POST'])
def chat_completions():
d = request.json
model = d.get('model', 'anthropic/Claude Opus 4.6')
messages = d.get('messages', [])
system_prompt = next((m.get('content', '') for m in messages if m.get('role') == 'system'), "")
filtered_messages = [m for m in messages if m.get('role') != 'system']
prov, ver = model.split('/', 1) if '/' in model else ("anthropic", model)
payload = build_cognix_payload(filtered_messages, prov, ver, tools=d.get('tools'), system=system_prompt)
if d.get('stream'):
def gen():
cid = f"chatcmpl-{uuid.uuid4().hex[:24]}"
yield f"data: {json.dumps({'id': cid, 'object': 'chat.completion.chunk', 'choices': [{'delta': {'role': 'assistant'}}]})}\n\n"
full_buf = ""
with requests.post(f"{COGNIX_BASE_URL}/api/chat", json=payload, headers=get_headers(), stream=True) as r:
for line in r.iter_lines(decode_unicode=True):
if not line: continue
cont, pty = parse_cognix_stream_chunk(line)
if pty == "stop": break
if cont:
if d.get('tools'): full_buf += cont
else: yield f"data: {json.dumps({'id': cid, 'object': 'chat.completion.chunk', 'choices': [{'delta': {'content': cont}}]})}\n\n"
if d.get('tools') and full_buf:
txt, tcs = parse_tool_calls_from_response(full_buf)
if txt: yield f"data: {json.dumps({'id': cid, 'object': 'chat.completion.chunk', 'choices': [{'delta': {'content': txt}}]})}\n\n"
if tcs: yield f"data: {json.dumps({'id': cid, 'object': 'chat.completion.chunk', 'choices': [{'delta': {'tool_calls': [{'index': 0, 'id': str(uuid.uuid4()), 'type': 'function', 'function': {'name': t['name'], 'arguments': json.dumps(t['input'])}}]}}]})}\n\n"
yield "data: [DONE]\n\n"
return Response(gen(), content_type='text/event-stream')
r = requests.post(f"{COGNIX_BASE_URL}/api/chat", json=payload, headers=get_headers())
full_text = "".join([parse_cognix_stream_chunk(l)[0] or "" for l in r.text.strip().split('\n')])
txt, tcs = parse_tool_calls_from_response(full_text)
msg = {"role": "assistant", "content": txt or None}
if tcs: msg["tool_calls"] = [{"id": str(uuid.uuid4()), "type": "function", "function": {"name": t['name'], "arguments": json.dumps(t['input'])}} for t in tcs]
return jsonify({"id": str(uuid.uuid4()), "object": "chat.completion", "choices": [{"message": msg, "finish_reason": "tool_calls" if tcs else "stop"}]})
@app.route('/v1/messages', methods=['POST'])
def anthropic_messages():
d = request.json
model = d.get('model', 'claude-3-opus')
prov, ver = model.split('/', 1) if '/' in model else ("anthropic", model)
payload = build_cognix_payload(d.get('messages', []), prov, ver, tools=d.get('tools'), system=d.get('system'), tool_fmt="anthropic")
if d.get('stream'):
def gen():
mid = f"msg_{uuid.uuid4().hex[:24]}"
yield f"event: message_start\ndata: {json.dumps({'type': 'message_start', 'message': {'id': mid, 'role': 'assistant', 'content': [], 'model': model}})}\n\n"
full_buf = ""
with requests.post(f"{COGNIX_BASE_URL}/api/chat", json=payload, headers=get_headers(), stream=True) as r:
for line in r.iter_lines(decode_unicode=True):
if not line: continue
cont, pty = parse_cognix_stream_chunk(line)
if pty == "stop": break
if cont:
full_buf += cont
if not d.get('tools'): yield f"event: content_block_delta\ndata: {json.dumps({'type': 'content_block_delta', 'index': 0, 'delta': {'type': 'text_delta', 'text': cont}})}\n\n"
if d.get('tools') and full_buf:
txt, tcs = parse_tool_calls_from_response(full_buf)
if txt: yield f"event: content_block_delta\ndata: {json.dumps({'type': 'content_block_delta', 'index': 0, 'delta': {'type': 'text_delta', 'text': txt}})}\n\n"
for tc in tcs: yield f"event: content_block_start\ndata: {json.dumps({'type': 'content_block_start', 'index': 1, 'content_block': {'type': 'tool_use', 'id': str(uuid.uuid4()), 'name': tc['name'], 'input': tc['input']}})}\n\n"
yield f"event: message_stop\ndata: {json.dumps({'type': 'message_stop'})}\n\n"
return Response(gen(), content_type='text/event-stream')
r = requests.post(f"{COGNIX_BASE_URL}/api/chat", json=payload, headers=get_headers())
full_text = "".join([parse_cognix_stream_chunk(l)[0] or "" for l in r.text.strip().split('\n')])
txt, tcs = parse_tool_calls_from_response(full_text)
content = [{"type": "text", "text": txt}] if txt else []
for t in tcs: content.append({"type": "tool_use", "id": str(uuid.uuid4()), "name": t['name'], "input": t['input']})
return jsonify({"id": str(uuid.uuid4()), "type": "message", "role": "assistant", "content": content, "model": model, "stop_reason": "tool_use" if tcs else "end_turn"})
@app.route('/v1/files', methods=['POST'])
def upload_file():
if 'file' not in request.files: return jsonify({"error": "no file"}), 400
f = request.files['file']; fb = f.read(); mt = f.content_type or mimetypes.guess_type(f.filename)[0] or 'application/octet-stream'
fid = f"file-{uuid.uuid4().hex[:24]}"; files_cache[fid] = {"_data": base64.b64encode(fb).decode('utf-8'), "content_type": mt, "filename": f.filename}
return jsonify({"id": fid, "object": "file", "filename": f.filename, "purpose": "vision"})
@app.route('/v1/models', methods=['GET'])
def list_models():
return jsonify({"object": "list", "data": fetch_cognix_models()})
if __name__ == '__main__':
print("Shivansh Tiwari Proxy (7862) Started...")
app.run(host='0.0.0.0', port=7860, debug=True)