Spaces:
Paused
Paused
CBV01 commited on
Commit ·
79e85da
1
Parent(s): 4eb7bcd
Standardize on OpenRouter with glowing connection, dynamic 400+ model catalogue selector, 100 non-repeating keyword suggestions, and tab-preserving keyword search
Browse files- backend/main.py +134 -163
- frontend/src/app/scraper/page.tsx +430 -161
backend/main.py
CHANGED
|
@@ -1987,26 +1987,26 @@ async def _scrape_ai_web_discovery(
|
|
| 1987 |
country: Optional[str] = None,
|
| 1988 |
queue: Optional[asyncio.Queue] = None,
|
| 1989 |
user_id: str = "",
|
| 1990 |
-
provider: str = "
|
| 1991 |
api_key: str = "",
|
| 1992 |
model: Optional[str] = None,
|
| 1993 |
rows: list = []
|
| 1994 |
) -> List[Dict[str, Any]]:
|
| 1995 |
"""
|
| 1996 |
AI Scraper: Scours the web and Telegram directories for active, public Telegram groups and channels
|
| 1997 |
-
matching base_query and country using the user's
|
| 1998 |
Discovered channels are verified in real-time via Telegram t.me metadata and streamed.
|
| 1999 |
"""
|
| 2000 |
results = []
|
| 2001 |
seen = set()
|
| 2002 |
-
prov = (provider or "
|
| 2003 |
key = (api_key or "").strip()
|
| 2004 |
|
| 2005 |
if not key:
|
| 2006 |
if queue:
|
| 2007 |
await queue.put({
|
| 2008 |
"type": "error",
|
| 2009 |
-
"msg": "AI Scraper requires an API key. Please configure
|
| 2010 |
})
|
| 2011 |
return []
|
| 2012 |
|
|
@@ -2014,7 +2014,7 @@ async def _scrape_ai_web_discovery(
|
|
| 2014 |
if queue:
|
| 2015 |
await queue.put({
|
| 2016 |
"type": "progress",
|
| 2017 |
-
"msg": f"AI Web Discovery: Asking
|
| 2018 |
})
|
| 2019 |
|
| 2020 |
prompt = (
|
|
@@ -2320,7 +2320,7 @@ async def scrape_keyword_stream(
|
|
| 2320 |
country=country,
|
| 2321 |
queue=queue,
|
| 2322 |
user_id=user_id,
|
| 2323 |
-
provider=ai_provider or "
|
| 2324 |
api_key=ai_key or "",
|
| 2325 |
model=ai_model,
|
| 2326 |
rows=rows
|
|
@@ -3805,81 +3805,112 @@ async def admin_get_users(admin_id: str = Depends(get_current_admin)):
|
|
| 3805 |
|
| 3806 |
# --- AI Suggested Keywords & Model Connect Endpoints ---
|
| 3807 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3808 |
class AIValidateRequest(BaseModel):
|
| 3809 |
-
provider: str
|
| 3810 |
api_key: str
|
| 3811 |
model: Optional[str] = None
|
| 3812 |
|
| 3813 |
@app.post("/api/ai/validate")
|
| 3814 |
async def validate_ai_credentials(req: AIValidateRequest, user_id: str = Depends(get_current_user_id)):
|
| 3815 |
-
provider = req.provider.lower().strip()
|
| 3816 |
key = req.api_key.strip()
|
| 3817 |
if not key:
|
| 3818 |
-
raise HTTPException(status_code=400, detail="API key is required.")
|
| 3819 |
|
| 3820 |
async with httpx.AsyncClient(timeout=15.0) as client:
|
| 3821 |
try:
|
| 3822 |
-
if provider == "
|
| 3823 |
-
res = await client.get(
|
| 3824 |
-
"https://api.openai.com/v1/models",
|
| 3825 |
-
headers={"Authorization": f"Bearer {key}"}
|
| 3826 |
-
)
|
| 3827 |
-
if res.status_code == 200:
|
| 3828 |
-
return {"valid": True, "provider": "openai", "message": "OpenAI connected and verified successfully!"}
|
| 3829 |
-
else:
|
| 3830 |
-
return {"valid": False, "provider": "openai", "message": f"OpenAI auth error ({res.status_code}): {res.text[:120]}"}
|
| 3831 |
-
elif provider in ("anthropic", "claude"):
|
| 3832 |
-
res = await client.post(
|
| 3833 |
-
"https://api.anthropic.com/v1/messages",
|
| 3834 |
-
headers={
|
| 3835 |
-
"x-api-key": key,
|
| 3836 |
-
"anthropic-version": "2023-06-01",
|
| 3837 |
-
"content-type": "application/json"
|
| 3838 |
-
},
|
| 3839 |
-
json={
|
| 3840 |
-
"model": req.model or "claude-3-5-haiku-20241022",
|
| 3841 |
-
"max_tokens": 5,
|
| 3842 |
-
"messages": [{"role": "user", "content": "ping"}]
|
| 3843 |
-
}
|
| 3844 |
-
)
|
| 3845 |
-
if res.status_code == 200:
|
| 3846 |
-
return {"valid": True, "provider": "claude", "message": "Anthropic Claude connected and verified successfully!"}
|
| 3847 |
-
err_text = res.text
|
| 3848 |
-
if "authentication_error" in err_text or res.status_code == 401:
|
| 3849 |
-
return {"valid": False, "provider": "claude", "message": "Invalid Anthropic API Key."}
|
| 3850 |
-
elif res.status_code in (200, 400) and "invalid_request_error" in err_text and "model" in err_text:
|
| 3851 |
-
return {"valid": True, "provider": "claude", "message": "Anthropic Claude key verified!"}
|
| 3852 |
-
else:
|
| 3853 |
-
return {"valid": False, "provider": "claude", "message": f"Claude validation notice ({res.status_code}): {err_text[:120]}"}
|
| 3854 |
-
elif provider == "openrouter":
|
| 3855 |
res = await client.get(
|
| 3856 |
"https://openrouter.ai/api/v1/auth/key",
|
| 3857 |
headers={"Authorization": f"Bearer {key}"}
|
| 3858 |
)
|
| 3859 |
if res.status_code == 200:
|
| 3860 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3861 |
else:
|
| 3862 |
return {"valid": False, "provider": "openrouter", "message": f"OpenRouter auth error ({res.status_code}): {res.text[:120]}"}
|
| 3863 |
-
elif provider == "groq":
|
| 3864 |
-
res = await client.get(
|
| 3865 |
-
"https://api.groq.com/openai/v1/models",
|
| 3866 |
-
headers={"Authorization": f"Bearer {key}"}
|
| 3867 |
-
)
|
| 3868 |
-
if res.status_code == 200:
|
| 3869 |
-
return {"valid": True, "provider": "groq", "message": "Groq Cloud connected and verified successfully!"}
|
| 3870 |
-
else:
|
| 3871 |
-
return {"valid": False, "provider": "groq", "message": f"Groq auth error ({res.status_code}): {res.text[:120]}"}
|
| 3872 |
-
else:
|
| 3873 |
-
raise HTTPException(status_code=400, detail=f"Unsupported provider: {provider}")
|
| 3874 |
except Exception as e:
|
| 3875 |
return {"valid": False, "provider": provider, "message": f"Connection error: {str(e)}"}
|
| 3876 |
|
| 3877 |
class AISuggestRequest(BaseModel):
|
| 3878 |
keyword: str
|
| 3879 |
-
provider: Optional[str] = "
|
| 3880 |
api_key: Optional[str] = None
|
| 3881 |
model: Optional[str] = None
|
| 3882 |
-
count: Optional[int] =
|
| 3883 |
|
| 3884 |
@app.post("/api/ai/suggest-keywords")
|
| 3885 |
async def suggest_ai_keywords(req: AISuggestRequest, user_id: str = Depends(get_current_user_id)):
|
|
@@ -3887,137 +3918,77 @@ async def suggest_ai_keywords(req: AISuggestRequest, user_id: str = Depends(get_
|
|
| 3887 |
if not seed:
|
| 3888 |
raise HTTPException(status_code=400, detail="Search keyword or niche is required.")
|
| 3889 |
|
| 3890 |
-
provider = (req.provider or "
|
| 3891 |
key = (req.api_key or "").strip()
|
| 3892 |
-
target_count = max(10, min(
|
|
|
|
| 3893 |
|
| 3894 |
if not key:
|
| 3895 |
raise HTTPException(
|
| 3896 |
status_code=400,
|
| 3897 |
-
detail="No
|
| 3898 |
)
|
| 3899 |
|
| 3900 |
ai_keywords: List[str] = []
|
| 3901 |
error_note = None
|
| 3902 |
|
| 3903 |
-
async with httpx.AsyncClient(timeout=
|
| 3904 |
prompt = (
|
| 3905 |
-
f"You are a Telegram search &
|
| 3906 |
-
f"Generate exactly {target_count} highly targeted, natural Telegram search keywords, "
|
| 3907 |
-
f"phrases, and
|
| 3908 |
-
f"
|
| 3909 |
-
f"
|
| 3910 |
-
f"
|
|
|
|
|
|
|
|
|
|
| 3911 |
)
|
| 3912 |
|
| 3913 |
try:
|
| 3914 |
-
|
| 3915 |
-
|
| 3916 |
-
|
| 3917 |
-
|
| 3918 |
-
json
|
| 3919 |
-
|
| 3920 |
-
|
| 3921 |
-
|
| 3922 |
-
|
| 3923 |
-
|
| 3924 |
-
|
| 3925 |
-
|
| 3926 |
-
|
| 3927 |
-
|
| 3928 |
-
|
| 3929 |
-
|
| 3930 |
-
|
| 3931 |
-
|
| 3932 |
-
|
| 3933 |
-
|
| 3934 |
-
|
| 3935 |
-
|
| 3936 |
-
|
| 3937 |
-
|
| 3938 |
-
"https://api.anthropic.com/v1/messages",
|
| 3939 |
-
headers={
|
| 3940 |
-
"x-api-key": key,
|
| 3941 |
-
"anthropic-version": "2023-06-01",
|
| 3942 |
-
"content-type": "application/json"
|
| 3943 |
-
},
|
| 3944 |
-
json={
|
| 3945 |
-
"model": req.model or "claude-3-5-haiku-20241022",
|
| 3946 |
-
"max_tokens": 1200,
|
| 3947 |
-
"messages": [{"role": "user", "content": prompt}]
|
| 3948 |
-
}
|
| 3949 |
-
)
|
| 3950 |
-
if res.status_code == 200:
|
| 3951 |
-
data = res.json()
|
| 3952 |
-
raw_content = data["content"][0]["text"]
|
| 3953 |
-
match = re.search(r'\[.*\]', raw_content, re.DOTALL)
|
| 3954 |
-
if match:
|
| 3955 |
-
ai_keywords = json.loads(match.group(0))
|
| 3956 |
-
else:
|
| 3957 |
-
error_note = f"Claude error ({res.status_code}): {res.text[:120]}"
|
| 3958 |
-
|
| 3959 |
-
elif provider == "openrouter":
|
| 3960 |
-
res = await client.post(
|
| 3961 |
-
"https://openrouter.ai/api/v1/chat/completions",
|
| 3962 |
-
headers={
|
| 3963 |
-
"Authorization": f"Bearer {key}",
|
| 3964 |
-
"Content-Type": "application/json",
|
| 3965 |
-
"HTTP-Referer": "https://arkitel.com",
|
| 3966 |
-
"X-Title": "ArkiTel"
|
| 3967 |
-
},
|
| 3968 |
-
json={
|
| 3969 |
-
"model": req.model or "openai/gpt-4o-mini",
|
| 3970 |
-
"messages": [
|
| 3971 |
-
{"role": "system", "content": "You are a Telegram keyword intelligence engine. Output only JSON arrays."},
|
| 3972 |
-
{"role": "user", "content": prompt}
|
| 3973 |
-
],
|
| 3974 |
-
"temperature": 0.7
|
| 3975 |
-
}
|
| 3976 |
-
)
|
| 3977 |
-
if res.status_code == 200:
|
| 3978 |
-
data = res.json()
|
| 3979 |
-
raw_content = data["choices"][0]["message"]["content"]
|
| 3980 |
-
match = re.search(r'\[.*\]', raw_content, re.DOTALL)
|
| 3981 |
-
if match:
|
| 3982 |
-
ai_keywords = json.loads(match.group(0))
|
| 3983 |
-
else:
|
| 3984 |
-
error_note = f"OpenRouter error ({res.status_code}): {res.text[:120]}"
|
| 3985 |
-
|
| 3986 |
-
elif provider == "groq":
|
| 3987 |
-
res = await client.post(
|
| 3988 |
-
"https://api.groq.com/openai/v1/chat/completions",
|
| 3989 |
-
headers={"Authorization": f"Bearer {key}", "Content-Type": "application/json"},
|
| 3990 |
-
json={
|
| 3991 |
-
"model": req.model or "llama-3.3-70b-versatile",
|
| 3992 |
-
"messages": [
|
| 3993 |
-
{"role": "system", "content": "You are a Telegram keyword intelligence engine. Output only JSON arrays."},
|
| 3994 |
-
{"role": "user", "content": prompt}
|
| 3995 |
-
],
|
| 3996 |
-
"temperature": 0.7
|
| 3997 |
-
}
|
| 3998 |
-
)
|
| 3999 |
-
if res.status_code == 200:
|
| 4000 |
-
data = res.json()
|
| 4001 |
-
raw_content = data["choices"][0]["message"]["content"]
|
| 4002 |
-
match = re.search(r'\[.*\]', raw_content, re.DOTALL)
|
| 4003 |
-
if match:
|
| 4004 |
-
ai_keywords = json.loads(match.group(0))
|
| 4005 |
else:
|
| 4006 |
-
error_note =
|
|
|
|
|
|
|
| 4007 |
except Exception as e:
|
| 4008 |
error_note = f"AI request exception: {str(e)}"
|
| 4009 |
|
| 4010 |
if error_note and not ai_keywords:
|
| 4011 |
raise HTTPException(status_code=400, detail=f"AI Suggestion failed: {error_note}")
|
| 4012 |
|
| 4013 |
-
# Clean and deduplicate AI returned strings
|
| 4014 |
clean_keywords: List[str] = []
|
| 4015 |
seen = set()
|
| 4016 |
for k in (ai_keywords or []):
|
| 4017 |
if isinstance(k, str) and k.strip():
|
| 4018 |
ks = k.strip().strip('"\'')
|
| 4019 |
-
|
| 4020 |
-
|
|
|
|
| 4021 |
clean_keywords.append(ks)
|
| 4022 |
|
| 4023 |
if not clean_keywords:
|
|
@@ -4027,8 +3998,8 @@ async def suggest_ai_keywords(req: AISuggestRequest, user_id: str = Depends(get_
|
|
| 4027 |
"seed": seed,
|
| 4028 |
"keywords": clean_keywords,
|
| 4029 |
"total": len(clean_keywords),
|
| 4030 |
-
"source":
|
| 4031 |
-
"
|
| 4032 |
}
|
| 4033 |
|
| 4034 |
@app.delete("/api/admin/users/{user_id}")
|
|
|
|
| 1987 |
country: Optional[str] = None,
|
| 1988 |
queue: Optional[asyncio.Queue] = None,
|
| 1989 |
user_id: str = "",
|
| 1990 |
+
provider: str = "openrouter",
|
| 1991 |
api_key: str = "",
|
| 1992 |
model: Optional[str] = None,
|
| 1993 |
rows: list = []
|
| 1994 |
) -> List[Dict[str, Any]]:
|
| 1995 |
"""
|
| 1996 |
AI Scraper: Scours the web and Telegram directories for active, public Telegram groups and channels
|
| 1997 |
+
matching base_query and country using OpenRouter with the user's selected model.
|
| 1998 |
Discovered channels are verified in real-time via Telegram t.me metadata and streamed.
|
| 1999 |
"""
|
| 2000 |
results = []
|
| 2001 |
seen = set()
|
| 2002 |
+
prov = (provider or "openrouter").lower().strip()
|
| 2003 |
key = (api_key or "").strip()
|
| 2004 |
|
| 2005 |
if not key:
|
| 2006 |
if queue:
|
| 2007 |
await queue.put({
|
| 2008 |
"type": "error",
|
| 2009 |
+
"msg": "AI Scraper requires an API key. Please configure your OpenRouter API key in settings."
|
| 2010 |
})
|
| 2011 |
return []
|
| 2012 |
|
|
|
|
| 2014 |
if queue:
|
| 2015 |
await queue.put({
|
| 2016 |
"type": "progress",
|
| 2017 |
+
"msg": f"AI Web Discovery: Asking OpenRouter ({model or 'default model'}) to uncover active Telegram communities for '{base_query}'{c_info}..."
|
| 2018 |
})
|
| 2019 |
|
| 2020 |
prompt = (
|
|
|
|
| 2320 |
country=country,
|
| 2321 |
queue=queue,
|
| 2322 |
user_id=user_id,
|
| 2323 |
+
provider=ai_provider or "openrouter",
|
| 2324 |
api_key=ai_key or "",
|
| 2325 |
model=ai_model,
|
| 2326 |
rows=rows
|
|
|
|
| 3805 |
|
| 3806 |
# --- AI Suggested Keywords & Model Connect Endpoints ---
|
| 3807 |
|
| 3808 |
+
_openrouter_models_cache = {"models": [], "timestamp": 0}
|
| 3809 |
+
|
| 3810 |
+
@app.get("/api/ai/openrouter-models")
|
| 3811 |
+
async def get_openrouter_models(user_id: str = Depends(get_current_user_id)):
|
| 3812 |
+
"""Fetches and caches the available OpenRouter models."""
|
| 3813 |
+
now = time.time()
|
| 3814 |
+
if _openrouter_models_cache["models"] and (now - _openrouter_models_cache["timestamp"] < 3600):
|
| 3815 |
+
return {"models": _openrouter_models_cache["models"]}
|
| 3816 |
+
|
| 3817 |
+
try:
|
| 3818 |
+
async with httpx.AsyncClient(timeout=15.0) as client:
|
| 3819 |
+
res = await client.get("https://openrouter.ai/api/v1/models")
|
| 3820 |
+
if res.status_code == 200:
|
| 3821 |
+
raw_models = res.json().get("data", [])
|
| 3822 |
+
models = []
|
| 3823 |
+
# Priority highlights
|
| 3824 |
+
priority_prefixes = [
|
| 3825 |
+
"meta-llama/llama-3.3-70b-instruct",
|
| 3826 |
+
"anthropic/claude-3.5-sonnet",
|
| 3827 |
+
"openai/gpt-4o-mini",
|
| 3828 |
+
"deepseek/deepseek-chat",
|
| 3829 |
+
"openai/gpt-4o",
|
| 3830 |
+
"google/gemini-2.0-flash-exp:free",
|
| 3831 |
+
"google/gemini-flash-1.5",
|
| 3832 |
+
"mistralai/mistral-nemo"
|
| 3833 |
+
]
|
| 3834 |
+
|
| 3835 |
+
for m in raw_models:
|
| 3836 |
+
m_id = m.get("id")
|
| 3837 |
+
if m_id:
|
| 3838 |
+
models.append({
|
| 3839 |
+
"id": m_id,
|
| 3840 |
+
"name": m.get("name") or m_id,
|
| 3841 |
+
"context_length": m.get("context_length", 0),
|
| 3842 |
+
"description": (m.get("description") or "")[:140]
|
| 3843 |
+
})
|
| 3844 |
+
|
| 3845 |
+
# Sort: priority models first, then alphabetical
|
| 3846 |
+
def sort_key(item):
|
| 3847 |
+
item_id = item["id"]
|
| 3848 |
+
for idx, prefix in enumerate(priority_prefixes):
|
| 3849 |
+
if item_id == prefix or item_id.startswith(prefix):
|
| 3850 |
+
return (0, idx)
|
| 3851 |
+
return (1, item["name"].lower())
|
| 3852 |
+
|
| 3853 |
+
models.sort(key=sort_key)
|
| 3854 |
+
_openrouter_models_cache["models"] = models
|
| 3855 |
+
_openrouter_models_cache["timestamp"] = now
|
| 3856 |
+
return {"models": models}
|
| 3857 |
+
except Exception as e:
|
| 3858 |
+
log_debug(f"Failed to fetch OpenRouter models: {e}")
|
| 3859 |
+
|
| 3860 |
+
# Fallback default models if network error
|
| 3861 |
+
fallback = [
|
| 3862 |
+
{"id": "meta-llama/llama-3.3-70b-instruct", "name": "Meta: Llama 3.3 70B Instruct", "context_length": 131072},
|
| 3863 |
+
{"id": "anthropic/claude-3.5-sonnet", "name": "Anthropic: Claude 3.5 Sonnet", "context_length": 200000},
|
| 3864 |
+
{"id": "openai/gpt-4o-mini", "name": "OpenAI: GPT-4o Mini", "context_length": 128000},
|
| 3865 |
+
{"id": "deepseek/deepseek-chat", "name": "DeepSeek: DeepSeek V3", "context_length": 64000},
|
| 3866 |
+
{"id": "openai/gpt-4o", "name": "OpenAI: GPT-4o", "context_length": 128000},
|
| 3867 |
+
{"id": "google/gemini-flash-1.5", "name": "Google: Gemini Flash 1.5", "context_length": 1000000},
|
| 3868 |
+
{"id": "mistralai/mistral-nemo", "name": "Mistral: Mistral Nemo", "context_length": 128000}
|
| 3869 |
+
]
|
| 3870 |
+
return {"models": _openrouter_models_cache["models"] or fallback}
|
| 3871 |
+
|
| 3872 |
class AIValidateRequest(BaseModel):
|
| 3873 |
+
provider: Optional[str] = "openrouter"
|
| 3874 |
api_key: str
|
| 3875 |
model: Optional[str] = None
|
| 3876 |
|
| 3877 |
@app.post("/api/ai/validate")
|
| 3878 |
async def validate_ai_credentials(req: AIValidateRequest, user_id: str = Depends(get_current_user_id)):
|
| 3879 |
+
provider = (req.provider or "openrouter").lower().strip()
|
| 3880 |
key = req.api_key.strip()
|
| 3881 |
if not key:
|
| 3882 |
+
raise HTTPException(status_code=400, detail="OpenRouter API key is required.")
|
| 3883 |
|
| 3884 |
async with httpx.AsyncClient(timeout=15.0) as client:
|
| 3885 |
try:
|
| 3886 |
+
if provider == "openrouter" or True: # Standardized strictly on OpenRouter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3887 |
res = await client.get(
|
| 3888 |
"https://openrouter.ai/api/v1/auth/key",
|
| 3889 |
headers={"Authorization": f"Bearer {key}"}
|
| 3890 |
)
|
| 3891 |
if res.status_code == 200:
|
| 3892 |
+
data = res.json().get("data", {})
|
| 3893 |
+
label = data.get("label") or "OpenRouter Key"
|
| 3894 |
+
limit = data.get("limit")
|
| 3895 |
+
usage = data.get("usage", 0)
|
| 3896 |
+
extra = f" (Usage: ${usage:.2f})" if usage else ""
|
| 3897 |
+
return {
|
| 3898 |
+
"valid": True,
|
| 3899 |
+
"provider": "openrouter",
|
| 3900 |
+
"message": f"OpenRouter connected & verified! {label}{extra}",
|
| 3901 |
+
"key_info": data
|
| 3902 |
+
}
|
| 3903 |
else:
|
| 3904 |
return {"valid": False, "provider": "openrouter", "message": f"OpenRouter auth error ({res.status_code}): {res.text[:120]}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3905 |
except Exception as e:
|
| 3906 |
return {"valid": False, "provider": provider, "message": f"Connection error: {str(e)}"}
|
| 3907 |
|
| 3908 |
class AISuggestRequest(BaseModel):
|
| 3909 |
keyword: str
|
| 3910 |
+
provider: Optional[str] = "openrouter"
|
| 3911 |
api_key: Optional[str] = None
|
| 3912 |
model: Optional[str] = None
|
| 3913 |
+
count: Optional[int] = 100
|
| 3914 |
|
| 3915 |
@app.post("/api/ai/suggest-keywords")
|
| 3916 |
async def suggest_ai_keywords(req: AISuggestRequest, user_id: str = Depends(get_current_user_id)):
|
|
|
|
| 3918 |
if not seed:
|
| 3919 |
raise HTTPException(status_code=400, detail="Search keyword or niche is required.")
|
| 3920 |
|
| 3921 |
+
provider = (req.provider or "openrouter").lower().strip()
|
| 3922 |
key = (req.api_key or "").strip()
|
| 3923 |
+
target_count = max(10, min(120, req.count or 100))
|
| 3924 |
+
selected_model = req.model or "openai/gpt-4o-mini"
|
| 3925 |
|
| 3926 |
if not key:
|
| 3927 |
raise HTTPException(
|
| 3928 |
status_code=400,
|
| 3929 |
+
detail="No OpenRouter API key configured. Please click 'AI Provider' to connect your OpenRouter key first."
|
| 3930 |
)
|
| 3931 |
|
| 3932 |
ai_keywords: List[str] = []
|
| 3933 |
error_note = None
|
| 3934 |
|
| 3935 |
+
async with httpx.AsyncClient(timeout=55.0) as client:
|
| 3936 |
prompt = (
|
| 3937 |
+
f"You are a Telegram search & community intelligence specialist. "
|
| 3938 |
+
f"Generate exactly {target_count} unique, highly targeted, natural Telegram search keywords, "
|
| 3939 |
+
f"community titles, phrases, and buyer/discussion terms for the niche: '{seed}'.\n"
|
| 3940 |
+
f"Rules:\n"
|
| 3941 |
+
f"1. Every single item MUST be completely unique and distinct. Absolute zero duplication or repetition.\n"
|
| 3942 |
+
f"2. Every item must be a realistic term active Telegram groups, channels, and communities actually use in titles/usernames.\n"
|
| 3943 |
+
f"3. Cover varied perspectives: popular discussion queries, group names, channels, locations/sub-niches, and services.\n"
|
| 3944 |
+
f"4. Return ONLY a valid JSON array of strings, for example: [\"keyword 1\", \"keyword 2\", ...].\n"
|
| 3945 |
+
f"5. Do NOT include any markdown code blocks, explanation, or preamble. Return raw JSON array only."
|
| 3946 |
)
|
| 3947 |
|
| 3948 |
try:
|
| 3949 |
+
res = await client.post(
|
| 3950 |
+
"https://openrouter.ai/api/v1/chat/completions",
|
| 3951 |
+
headers={
|
| 3952 |
+
"Authorization": f"Bearer {key}",
|
| 3953 |
+
"Content-Type": "application/json",
|
| 3954 |
+
"HTTP-Referer": "https://arkitel.com",
|
| 3955 |
+
"X-Title": "ArkiTel"
|
| 3956 |
+
},
|
| 3957 |
+
json={
|
| 3958 |
+
"model": selected_model,
|
| 3959 |
+
"messages": [
|
| 3960 |
+
{"role": "system", "content": "You are a Telegram search intelligence engine. Output only valid JSON arrays of unique strings."},
|
| 3961 |
+
{"role": "user", "content": prompt}
|
| 3962 |
+
],
|
| 3963 |
+
"temperature": 0.7,
|
| 3964 |
+
"max_tokens": 3500
|
| 3965 |
+
}
|
| 3966 |
+
)
|
| 3967 |
+
if res.status_code == 200:
|
| 3968 |
+
data = res.json()
|
| 3969 |
+
raw_content = data["choices"][0]["message"]["content"]
|
| 3970 |
+
match = re.search(r'\[.*\]', raw_content, re.DOTALL)
|
| 3971 |
+
if match:
|
| 3972 |
+
ai_keywords = json.loads(match.group(0))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3973 |
else:
|
| 3974 |
+
error_note = "Could not parse JSON array from AI response."
|
| 3975 |
+
else:
|
| 3976 |
+
error_note = f"OpenRouter error ({res.status_code}): {res.text[:120]}"
|
| 3977 |
except Exception as e:
|
| 3978 |
error_note = f"AI request exception: {str(e)}"
|
| 3979 |
|
| 3980 |
if error_note and not ai_keywords:
|
| 3981 |
raise HTTPException(status_code=400, detail=f"AI Suggestion failed: {error_note}")
|
| 3982 |
|
| 3983 |
+
# Clean and strictly deduplicate AI returned strings
|
| 3984 |
clean_keywords: List[str] = []
|
| 3985 |
seen = set()
|
| 3986 |
for k in (ai_keywords or []):
|
| 3987 |
if isinstance(k, str) and k.strip():
|
| 3988 |
ks = k.strip().strip('"\'')
|
| 3989 |
+
norm = ks.lower()
|
| 3990 |
+
if norm not in seen and len(ks) >= 2:
|
| 3991 |
+
seen.add(norm)
|
| 3992 |
clean_keywords.append(ks)
|
| 3993 |
|
| 3994 |
if not clean_keywords:
|
|
|
|
| 3998 |
"seed": seed,
|
| 3999 |
"keywords": clean_keywords,
|
| 4000 |
"total": len(clean_keywords),
|
| 4001 |
+
"source": "openrouter",
|
| 4002 |
+
"model": selected_model
|
| 4003 |
}
|
| 4004 |
|
| 4005 |
@app.delete("/api/admin/users/{user_id}")
|
frontend/src/app/scraper/page.tsx
CHANGED
|
@@ -3,12 +3,30 @@
|
|
| 3 |
import React, { useState, useRef, useEffect } from 'react';
|
| 4 |
import {
|
| 5 |
Search, Loader2, Users, UsersRound, Megaphone, CheckCircle2, X, ExternalLink, Plus, AlertCircle,
|
| 6 |
-
Sparkles, Settings2, Download, Copy, ChevronDown, ChevronUp, Minimize2, Maximize2, Zap, Sliders, Check, FileText
|
|
|
|
| 7 |
} from 'lucide-react';
|
| 8 |
import { apiFetch, getApiBase } from '@/lib/auth';
|
| 9 |
import { Preloader } from '@/components/Preloader';
|
| 10 |
import { MonetizationOverlay } from '@/components/MonetizationOverlay';
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
interface ScrapeResult {
|
| 13 |
id: string;
|
| 14 |
title: string;
|
|
@@ -71,14 +89,21 @@ export default function ScraperPage() {
|
|
| 71 |
const [bulkJoinProgress, setBulkJoinProgress] = useState<{ current: number, total: number, msg: string, logs: string[] }>({ current: 0, total: 0, msg: '', logs: [] });
|
| 72 |
const [isBulkJoining, setIsBulkJoining] = useState(false);
|
| 73 |
|
| 74 |
-
// AI Suggestions & Connect Model State
|
| 75 |
const [isAiModalOpen, setIsAiModalOpen] = useState(false);
|
| 76 |
-
const [aiProvider, setAiProvider] = useState<'openai' | 'claude' | 'openrouter' | 'groq'>('openai');
|
| 77 |
const [aiApiKey, setAiApiKey] = useState('');
|
| 78 |
-
const [aiModel, setAiModel] = useState('');
|
|
|
|
|
|
|
|
|
|
| 79 |
const [isValidatingAi, setIsValidatingAi] = useState(false);
|
| 80 |
const [aiValidationResult, setAiValidationResult] = useState<{ valid: boolean; message: string } | null>(null);
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
const [isSuggesting, setIsSuggesting] = useState(false);
|
| 83 |
const [suggestions, setSuggestions] = useState<string[]>([]);
|
| 84 |
const [suggestionSeed, setSuggestionSeed] = useState('');
|
|
@@ -93,20 +118,49 @@ export default function ScraperPage() {
|
|
| 93 |
const [memberLoading, setMemberLoading] = useState(false);
|
| 94 |
const [bulkSaving, setBulkSaving] = useState(false);
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
useEffect(() => {
|
| 97 |
try {
|
| 98 |
-
const p = localStorage.getItem('arkitel_ai_provider');
|
| 99 |
-
if (p === 'openai' || p === 'claude' || p === 'openrouter' || p === 'groq') setAiProvider(p);
|
| 100 |
const k = localStorage.getItem('arkitel_ai_key');
|
| 101 |
-
if (k) setAiApiKey(k);
|
| 102 |
const m = localStorage.getItem('arkitel_ai_model');
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
} catch {}
|
| 105 |
}, []);
|
| 106 |
|
| 107 |
const handleValidateAi = async () => {
|
| 108 |
if (!aiApiKey.trim()) {
|
| 109 |
-
setAiValidationResult({ valid: false, message: 'Please enter
|
| 110 |
return;
|
| 111 |
}
|
| 112 |
setIsValidatingAi(true);
|
|
@@ -115,17 +169,27 @@ export default function ScraperPage() {
|
|
| 115 |
const res = await apiFetch('/api/ai/validate', {
|
| 116 |
method: 'POST',
|
| 117 |
body: JSON.stringify({
|
| 118 |
-
provider:
|
| 119 |
api_key: aiApiKey.trim(),
|
| 120 |
model: aiModel.trim() || undefined
|
| 121 |
})
|
| 122 |
});
|
| 123 |
const data = await res.json();
|
| 124 |
if (res.ok && data.valid) {
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
localStorage.setItem('arkitel_ai_key', aiApiKey.trim());
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
} else {
|
| 130 |
setAiValidationResult({ valid: false, message: data.message || data.detail || 'Validation failed.' });
|
| 131 |
}
|
|
@@ -137,18 +201,33 @@ export default function ScraperPage() {
|
|
| 137 |
};
|
| 138 |
|
| 139 |
const handleSaveAiConfig = () => {
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
localStorage.setItem('arkitel_ai_model', aiModel.trim());
|
| 144 |
-
} else {
|
| 145 |
-
localStorage.removeItem('arkitel_ai_model');
|
| 146 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
setIsAiModalOpen(false);
|
| 148 |
-
setSuccessMsg(
|
| 149 |
setTimeout(() => setSuccessMsg(''), 4000);
|
| 150 |
};
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
const handleSuggestKeywords = async () => {
|
| 153 |
const seed = keyword.trim();
|
| 154 |
if (!seed) {
|
|
@@ -158,7 +237,7 @@ export default function ScraperPage() {
|
|
| 158 |
|
| 159 |
const storedKey = aiApiKey || localStorage.getItem('arkitel_ai_key') || '';
|
| 160 |
if (!storedKey) {
|
| 161 |
-
setError('No AI Model connected. Please
|
| 162 |
setIsAiModalOpen(true);
|
| 163 |
return;
|
| 164 |
}
|
|
@@ -166,17 +245,16 @@ export default function ScraperPage() {
|
|
| 166 |
setIsSuggesting(true);
|
| 167 |
setError('');
|
| 168 |
try {
|
| 169 |
-
const
|
| 170 |
-
const storedModel = aiModel || localStorage.getItem('arkitel_ai_model') || '';
|
| 171 |
|
| 172 |
const res = await apiFetch('/api/ai/suggest-keywords', {
|
| 173 |
method: 'POST',
|
| 174 |
body: JSON.stringify({
|
| 175 |
keyword: seed,
|
| 176 |
-
provider:
|
| 177 |
api_key: storedKey,
|
| 178 |
-
model: storedModel
|
| 179 |
-
count:
|
| 180 |
})
|
| 181 |
});
|
| 182 |
const data = await res.json();
|
|
@@ -185,7 +263,7 @@ export default function ScraperPage() {
|
|
| 185 |
setSuggestionSeed(seed);
|
| 186 |
setIsSuggestionsPanelOpen(true);
|
| 187 |
setIsSuggestionsMinimized(false);
|
| 188 |
-
setSuccessMsg(`Generated ${data.keywords.length} AI keyword variations!`);
|
| 189 |
setTimeout(() => setSuccessMsg(''), 4000);
|
| 190 |
} else {
|
| 191 |
setError(data.detail || data.note || 'Failed to suggest keywords.');
|
|
@@ -222,8 +300,7 @@ export default function ScraperPage() {
|
|
| 222 |
|
| 223 |
const handleUseKeyword = (kw: string) => {
|
| 224 |
setKeyword(kw);
|
| 225 |
-
|
| 226 |
-
// Submit search on next tick
|
| 227 |
setTimeout(() => {
|
| 228 |
const form = document.getElementById('scraper-form') as HTMLFormElement;
|
| 229 |
if (form) form.requestSubmit();
|
|
@@ -435,7 +512,7 @@ export default function ScraperPage() {
|
|
| 435 |
if (searchMode === 'ai') {
|
| 436 |
const storedKey = aiApiKey || localStorage.getItem('arkitel_ai_key') || '';
|
| 437 |
if (!storedKey) {
|
| 438 |
-
setError('No AI Model connected. Please
|
| 439 |
setIsAiModalOpen(true);
|
| 440 |
return;
|
| 441 |
}
|
|
@@ -459,10 +536,9 @@ export default function ScraperPage() {
|
|
| 459 |
const apiBase = getApiBase();
|
| 460 |
const cStr = country.trim() ? `&country=${encodeURIComponent(' ' + country.trim())}` : '';
|
| 461 |
const storedKey = aiApiKey || localStorage.getItem('arkitel_ai_key') || '';
|
| 462 |
-
const
|
| 463 |
-
const storedModel = aiModel || localStorage.getItem('arkitel_ai_model') || '';
|
| 464 |
const aiParams = searchMode === 'ai'
|
| 465 |
-
? `&ai_provider=
|
| 466 |
: '';
|
| 467 |
const res = await fetch(`${apiBase}/api/telegram/scrape_stream?query=${encodeURIComponent(keyword)}${cStr}&mode=${searchMode}&limit=1500${aiParams}`, {
|
| 468 |
headers: { 'Authorization': `Bearer ${token}` },
|
|
@@ -790,18 +866,75 @@ export default function ScraperPage() {
|
|
| 790 |
</button>
|
| 791 |
</div>
|
| 792 |
|
| 793 |
-
<div className="flex items-center gap-2 self-end sm:self-auto">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 794 |
<button
|
| 795 |
type="button"
|
| 796 |
-
onClick={() =>
|
| 797 |
-
|
| 798 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 799 |
>
|
| 800 |
-
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
|
| 804 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 805 |
</button>
|
| 806 |
</div>
|
| 807 |
</div>
|
|
@@ -903,7 +1036,7 @@ export default function ScraperPage() {
|
|
| 903 |
{suggestions.length} keywords
|
| 904 |
</span>
|
| 905 |
</div>
|
| 906 |
-
<p className="text-[11px] text-foreground/45 font-medium">Click any keyword to
|
| 907 |
</div>
|
| 908 |
</div>
|
| 909 |
|
|
@@ -963,7 +1096,7 @@ export default function ScraperPage() {
|
|
| 963 |
</div>
|
| 964 |
|
| 965 |
{!isSuggestionsMinimized && (
|
| 966 |
-
<div className="flex flex-wrap gap-1.5 max-h-
|
| 967 |
{suggestions.map((kw, i) => (
|
| 968 |
<button
|
| 969 |
key={i}
|
|
@@ -1238,153 +1371,289 @@ export default function ScraperPage() {
|
|
| 1238 |
</div>
|
| 1239 |
)}
|
| 1240 |
|
| 1241 |
-
{/* AI Configuration Modal */}
|
| 1242 |
{isAiModalOpen && (
|
| 1243 |
-
<div className="fixed inset-0 bg-black/80 backdrop-blur-sm z-[110] flex items-center justify-center p-4">
|
| 1244 |
-
<div className="bg-card border border-border rounded-2xl w-full max-w-
|
| 1245 |
-
|
| 1246 |
-
|
| 1247 |
-
|
| 1248 |
-
|
|
|
|
| 1249 |
</div>
|
| 1250 |
<div>
|
| 1251 |
-
<
|
| 1252 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1253 |
</div>
|
| 1254 |
</div>
|
| 1255 |
<button
|
| 1256 |
onClick={() => { setIsAiModalOpen(false); setAiValidationResult(null); }}
|
| 1257 |
-
className="text-foreground/40 hover:text-foreground p-1 rounded-lg hover:bg-foreground/5 transition-colors"
|
| 1258 |
>
|
| 1259 |
<X size={16} />
|
| 1260 |
</button>
|
| 1261 |
</div>
|
| 1262 |
|
| 1263 |
-
|
| 1264 |
-
|
| 1265 |
-
|
| 1266 |
-
|
| 1267 |
-
|
| 1268 |
-
|
| 1269 |
-
|
| 1270 |
-
|
| 1271 |
-
|
| 1272 |
-
|
| 1273 |
-
|
| 1274 |
-
|
| 1275 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1276 |
<button
|
| 1277 |
-
key={p.id}
|
| 1278 |
type="button"
|
| 1279 |
-
onClick={
|
| 1280 |
-
|
| 1281 |
-
|
| 1282 |
-
}}
|
| 1283 |
-
className={`py-2 px-2 rounded-xl text-xs font-semibold border transition-all cursor-pointer ${
|
| 1284 |
-
aiProvider === p.id
|
| 1285 |
-
? 'bg-indigo-600 text-white border-indigo-500 shadow-sm'
|
| 1286 |
-
: 'bg-foreground/[0.02] border-border text-foreground/60 hover:text-foreground hover:bg-foreground/5'
|
| 1287 |
-
}`}
|
| 1288 |
>
|
| 1289 |
-
|
| 1290 |
</button>
|
| 1291 |
-
)
|
| 1292 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1293 |
</div>
|
| 1294 |
|
| 1295 |
-
{/*
|
| 1296 |
-
<div>
|
| 1297 |
-
<
|
| 1298 |
-
|
| 1299 |
-
|
| 1300 |
-
|
| 1301 |
-
|
| 1302 |
-
|
| 1303 |
-
|
| 1304 |
-
|
| 1305 |
-
|
| 1306 |
-
|
| 1307 |
-
|
| 1308 |
-
|
| 1309 |
-
/>
|
| 1310 |
</div>
|
| 1311 |
|
| 1312 |
-
{/*
|
| 1313 |
<div>
|
| 1314 |
-
<label className="block text-[11px] font-bold text-foreground/60 uppercase tracking-wider mb-
|
| 1315 |
-
|
| 1316 |
</label>
|
| 1317 |
-
<
|
| 1318 |
-
|
| 1319 |
-
|
| 1320 |
-
|
| 1321 |
-
|
| 1322 |
-
|
| 1323 |
-
|
| 1324 |
-
|
| 1325 |
-
|
| 1326 |
-
|
| 1327 |
-
|
| 1328 |
-
|
| 1329 |
-
|
| 1330 |
-
|
| 1331 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1332 |
</div>
|
| 1333 |
|
| 1334 |
-
{/*
|
| 1335 |
-
<div>
|
| 1336 |
-
<
|
| 1337 |
-
|
| 1338 |
-
|
| 1339 |
-
|
| 1340 |
-
|
| 1341 |
-
|
| 1342 |
-
|
| 1343 |
-
|
| 1344 |
-
|
| 1345 |
-
|
| 1346 |
-
|
| 1347 |
-
|
| 1348 |
-
|
| 1349 |
-
|
| 1350 |
-
|
| 1351 |
-
|
| 1352 |
-
|
| 1353 |
-
|
| 1354 |
-
|
|
|
|
| 1355 |
|
| 1356 |
-
|
| 1357 |
-
|
| 1358 |
-
|
| 1359 |
-
|
| 1360 |
-
|
| 1361 |
-
|
| 1362 |
-
|
| 1363 |
-
|
| 1364 |
-
|
|
|
|
|
|
|
|
|
|
| 1365 |
</div>
|
| 1366 |
-
)}
|
| 1367 |
|
| 1368 |
-
|
| 1369 |
-
|
| 1370 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1371 |
</div>
|
| 1372 |
|
| 1373 |
-
|
| 1374 |
-
|
| 1375 |
-
|
| 1376 |
-
|
| 1377 |
-
|
| 1378 |
-
>
|
| 1379 |
-
|
| 1380 |
-
|
| 1381 |
-
|
| 1382 |
-
|
| 1383 |
-
|
| 1384 |
-
|
| 1385 |
-
|
| 1386 |
-
|
| 1387 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1388 |
</div>
|
| 1389 |
</div>
|
| 1390 |
</div>
|
|
|
|
| 3 |
import React, { useState, useRef, useEffect } from 'react';
|
| 4 |
import {
|
| 5 |
Search, Loader2, Users, UsersRound, Megaphone, CheckCircle2, X, ExternalLink, Plus, AlertCircle,
|
| 6 |
+
Sparkles, Settings2, Download, Copy, ChevronDown, ChevronUp, Minimize2, Maximize2, Zap, Sliders, Check, FileText,
|
| 7 |
+
Cpu, Eye, EyeOff, RefreshCw, Key
|
| 8 |
} from 'lucide-react';
|
| 9 |
import { apiFetch, getApiBase } from '@/lib/auth';
|
| 10 |
import { Preloader } from '@/components/Preloader';
|
| 11 |
import { MonetizationOverlay } from '@/components/MonetizationOverlay';
|
| 12 |
|
| 13 |
+
const TOP_MODELS = [
|
| 14 |
+
{ id: 'meta-llama/llama-3.3-70b-instruct', name: 'Llama 3.3 70B', tag: 'Recommended', provider: 'Meta' },
|
| 15 |
+
{ id: 'anthropic/claude-3.5-sonnet', name: 'Claude 3.5 Sonnet', tag: 'Deep Intel', provider: 'Anthropic' },
|
| 16 |
+
{ id: 'openai/gpt-4o-mini', name: 'GPT-4o Mini', tag: 'Fast & Cheap', provider: 'OpenAI' },
|
| 17 |
+
{ id: 'deepseek/deepseek-chat', name: 'DeepSeek V3', tag: 'High Performance', provider: 'DeepSeek' },
|
| 18 |
+
{ id: 'openai/gpt-4o', name: 'GPT-4o', tag: 'Flagship Intelligence', provider: 'OpenAI' },
|
| 19 |
+
{ id: 'google/gemini-flash-1.5', name: 'Gemini Flash 1.5', tag: 'High Speed', provider: 'Google' },
|
| 20 |
+
{ id: 'mistralai/mistral-nemo', name: 'Mistral Nemo', tag: 'Balanced', provider: 'Mistral' }
|
| 21 |
+
];
|
| 22 |
+
|
| 23 |
+
const getShortModelName = (id: string) => {
|
| 24 |
+
if (!id) return 'Llama 3.3 70B';
|
| 25 |
+
const parts = id.split('/');
|
| 26 |
+
const name = parts.length > 1 ? parts[1] : parts[0];
|
| 27 |
+
return name.replace(/-instruct|-chat|-versatile/g, '');
|
| 28 |
+
};
|
| 29 |
+
|
| 30 |
interface ScrapeResult {
|
| 31 |
id: string;
|
| 32 |
title: string;
|
|
|
|
| 89 |
const [bulkJoinProgress, setBulkJoinProgress] = useState<{ current: number, total: number, msg: string, logs: string[] }>({ current: 0, total: 0, msg: '', logs: [] });
|
| 90 |
const [isBulkJoining, setIsBulkJoining] = useState(false);
|
| 91 |
|
| 92 |
+
// AI Suggestions & OpenRouter Connect Model State
|
| 93 |
const [isAiModalOpen, setIsAiModalOpen] = useState(false);
|
|
|
|
| 94 |
const [aiApiKey, setAiApiKey] = useState('');
|
| 95 |
+
const [aiModel, setAiModel] = useState('meta-llama/llama-3.3-70b-instruct');
|
| 96 |
+
const [isAiConnected, setIsAiConnected] = useState(false);
|
| 97 |
+
const [aiKeyLabel, setAiKeyLabel] = useState('');
|
| 98 |
+
const [showAiKey, setShowAiKey] = useState(false);
|
| 99 |
const [isValidatingAi, setIsValidatingAi] = useState(false);
|
| 100 |
const [aiValidationResult, setAiValidationResult] = useState<{ valid: boolean; message: string } | null>(null);
|
| 101 |
|
| 102 |
+
// Available OpenRouter Models State
|
| 103 |
+
const [availableModels, setAvailableModels] = useState<{ id: string; name: string; context_length?: number; description?: string }[]>([]);
|
| 104 |
+
const [isLoadingModels, setIsLoadingModels] = useState(false);
|
| 105 |
+
const [modelSearch, setModelSearch] = useState('');
|
| 106 |
+
|
| 107 |
const [isSuggesting, setIsSuggesting] = useState(false);
|
| 108 |
const [suggestions, setSuggestions] = useState<string[]>([]);
|
| 109 |
const [suggestionSeed, setSuggestionSeed] = useState('');
|
|
|
|
| 118 |
const [memberLoading, setMemberLoading] = useState(false);
|
| 119 |
const [bulkSaving, setBulkSaving] = useState(false);
|
| 120 |
|
| 121 |
+
const fetchOpenRouterModels = async () => {
|
| 122 |
+
setIsLoadingModels(true);
|
| 123 |
+
try {
|
| 124 |
+
const res = await apiFetch('/api/ai/openrouter-models');
|
| 125 |
+
const data = await res.json();
|
| 126 |
+
if (res.ok && Array.isArray(data.models) && data.models.length > 0) {
|
| 127 |
+
setAvailableModels(data.models);
|
| 128 |
+
}
|
| 129 |
+
} catch (e) {
|
| 130 |
+
console.error('Failed to fetch OpenRouter models:', e);
|
| 131 |
+
} finally {
|
| 132 |
+
setIsLoadingModels(false);
|
| 133 |
+
}
|
| 134 |
+
};
|
| 135 |
+
|
| 136 |
useEffect(() => {
|
| 137 |
try {
|
|
|
|
|
|
|
| 138 |
const k = localStorage.getItem('arkitel_ai_key');
|
|
|
|
| 139 |
const m = localStorage.getItem('arkitel_ai_model');
|
| 140 |
+
const c = localStorage.getItem('arkitel_ai_connected');
|
| 141 |
+
const l = localStorage.getItem('arkitel_ai_key_label');
|
| 142 |
+
if (k) {
|
| 143 |
+
setAiApiKey(k);
|
| 144 |
+
if (c === 'true' || k.trim().length > 5) {
|
| 145 |
+
setIsAiConnected(true);
|
| 146 |
+
}
|
| 147 |
+
}
|
| 148 |
+
if (m) {
|
| 149 |
+
setAiModel(m);
|
| 150 |
+
}
|
| 151 |
+
if (l) {
|
| 152 |
+
setAiKeyLabel(l);
|
| 153 |
+
}
|
| 154 |
+
// Preload models list if user already has key
|
| 155 |
+
if (k) {
|
| 156 |
+
fetchOpenRouterModels();
|
| 157 |
+
}
|
| 158 |
} catch {}
|
| 159 |
}, []);
|
| 160 |
|
| 161 |
const handleValidateAi = async () => {
|
| 162 |
if (!aiApiKey.trim()) {
|
| 163 |
+
setAiValidationResult({ valid: false, message: 'Please enter your OpenRouter API Key (sk-or-...).' });
|
| 164 |
return;
|
| 165 |
}
|
| 166 |
setIsValidatingAi(true);
|
|
|
|
| 169 |
const res = await apiFetch('/api/ai/validate', {
|
| 170 |
method: 'POST',
|
| 171 |
body: JSON.stringify({
|
| 172 |
+
provider: 'openrouter',
|
| 173 |
api_key: aiApiKey.trim(),
|
| 174 |
model: aiModel.trim() || undefined
|
| 175 |
})
|
| 176 |
});
|
| 177 |
const data = await res.json();
|
| 178 |
if (res.ok && data.valid) {
|
| 179 |
+
const label = data.key_info?.label || 'OpenRouter Verified';
|
| 180 |
+
setAiKeyLabel(label);
|
| 181 |
+
setIsAiConnected(true);
|
| 182 |
+
setAiValidationResult({ valid: true, message: data.message || 'OpenRouter connected & verified!' });
|
| 183 |
+
|
| 184 |
+
// Persist immediately so user never has to reconnect
|
| 185 |
+
localStorage.setItem('arkitel_ai_provider', 'openrouter');
|
| 186 |
localStorage.setItem('arkitel_ai_key', aiApiKey.trim());
|
| 187 |
+
localStorage.setItem('arkitel_ai_model', aiModel.trim() || 'meta-llama/llama-3.3-70b-instruct');
|
| 188 |
+
localStorage.setItem('arkitel_ai_connected', 'true');
|
| 189 |
+
localStorage.setItem('arkitel_ai_key_label', label);
|
| 190 |
+
|
| 191 |
+
// Fetch available model catalogue
|
| 192 |
+
fetchOpenRouterModels();
|
| 193 |
} else {
|
| 194 |
setAiValidationResult({ valid: false, message: data.message || data.detail || 'Validation failed.' });
|
| 195 |
}
|
|
|
|
| 201 |
};
|
| 202 |
|
| 203 |
const handleSaveAiConfig = () => {
|
| 204 |
+
if (!aiApiKey.trim()) {
|
| 205 |
+
setError('Please enter an OpenRouter API key first.');
|
| 206 |
+
return;
|
|
|
|
|
|
|
|
|
|
| 207 |
}
|
| 208 |
+
const chosenModel = aiModel.trim() || 'meta-llama/llama-3.3-70b-instruct';
|
| 209 |
+
localStorage.setItem('arkitel_ai_provider', 'openrouter');
|
| 210 |
+
localStorage.setItem('arkitel_ai_key', aiApiKey.trim());
|
| 211 |
+
localStorage.setItem('arkitel_ai_model', chosenModel);
|
| 212 |
+
localStorage.setItem('arkitel_ai_connected', 'true');
|
| 213 |
+
setIsAiConnected(true);
|
| 214 |
setIsAiModalOpen(false);
|
| 215 |
+
setSuccessMsg(`Saved! Active model: ${getShortModelName(chosenModel)}`);
|
| 216 |
setTimeout(() => setSuccessMsg(''), 4000);
|
| 217 |
};
|
| 218 |
|
| 219 |
+
const handleDisconnectAi = () => {
|
| 220 |
+
localStorage.removeItem('arkitel_ai_key');
|
| 221 |
+
localStorage.removeItem('arkitel_ai_connected');
|
| 222 |
+
localStorage.removeItem('arkitel_ai_key_label');
|
| 223 |
+
setAiApiKey('');
|
| 224 |
+
setIsAiConnected(false);
|
| 225 |
+
setAiKeyLabel('');
|
| 226 |
+
setAiValidationResult(null);
|
| 227 |
+
setSuccessMsg('Disconnected OpenRouter.');
|
| 228 |
+
setTimeout(() => setSuccessMsg(''), 3000);
|
| 229 |
+
};
|
| 230 |
+
|
| 231 |
const handleSuggestKeywords = async () => {
|
| 232 |
const seed = keyword.trim();
|
| 233 |
if (!seed) {
|
|
|
|
| 237 |
|
| 238 |
const storedKey = aiApiKey || localStorage.getItem('arkitel_ai_key') || '';
|
| 239 |
if (!storedKey) {
|
| 240 |
+
setError('No AI Model connected. Please connect your OpenRouter API key first to use AI Keyword Suggestions.');
|
| 241 |
setIsAiModalOpen(true);
|
| 242 |
return;
|
| 243 |
}
|
|
|
|
| 245 |
setIsSuggesting(true);
|
| 246 |
setError('');
|
| 247 |
try {
|
| 248 |
+
const storedModel = aiModel || localStorage.getItem('arkitel_ai_model') || 'meta-llama/llama-3.3-70b-instruct';
|
|
|
|
| 249 |
|
| 250 |
const res = await apiFetch('/api/ai/suggest-keywords', {
|
| 251 |
method: 'POST',
|
| 252 |
body: JSON.stringify({
|
| 253 |
keyword: seed,
|
| 254 |
+
provider: 'openrouter',
|
| 255 |
api_key: storedKey,
|
| 256 |
+
model: storedModel,
|
| 257 |
+
count: 100
|
| 258 |
})
|
| 259 |
});
|
| 260 |
const data = await res.json();
|
|
|
|
| 263 |
setSuggestionSeed(seed);
|
| 264 |
setIsSuggestionsPanelOpen(true);
|
| 265 |
setIsSuggestionsMinimized(false);
|
| 266 |
+
setSuccessMsg(`Generated ${data.keywords.length} clean, unique AI keyword variations!`);
|
| 267 |
setTimeout(() => setSuccessMsg(''), 4000);
|
| 268 |
} else {
|
| 269 |
setError(data.detail || data.note || 'Failed to suggest keywords.');
|
|
|
|
| 300 |
|
| 301 |
const handleUseKeyword = (kw: string) => {
|
| 302 |
setKeyword(kw);
|
| 303 |
+
// User stays on whichever search tab they are currently on ('free', 'fixed', or 'ai')
|
|
|
|
| 304 |
setTimeout(() => {
|
| 305 |
const form = document.getElementById('scraper-form') as HTMLFormElement;
|
| 306 |
if (form) form.requestSubmit();
|
|
|
|
| 512 |
if (searchMode === 'ai') {
|
| 513 |
const storedKey = aiApiKey || localStorage.getItem('arkitel_ai_key') || '';
|
| 514 |
if (!storedKey) {
|
| 515 |
+
setError('No AI Model connected. Please connect your OpenRouter API key first to use the AI Scraper.');
|
| 516 |
setIsAiModalOpen(true);
|
| 517 |
return;
|
| 518 |
}
|
|
|
|
| 536 |
const apiBase = getApiBase();
|
| 537 |
const cStr = country.trim() ? `&country=${encodeURIComponent(' ' + country.trim())}` : '';
|
| 538 |
const storedKey = aiApiKey || localStorage.getItem('arkitel_ai_key') || '';
|
| 539 |
+
const storedModel = aiModel || localStorage.getItem('arkitel_ai_model') || 'meta-llama/llama-3.3-70b-instruct';
|
|
|
|
| 540 |
const aiParams = searchMode === 'ai'
|
| 541 |
+
? `&ai_provider=openrouter&ai_key=${encodeURIComponent(storedKey)}&ai_model=${encodeURIComponent(storedModel)}`
|
| 542 |
: '';
|
| 543 |
const res = await fetch(`${apiBase}/api/telegram/scrape_stream?query=${encodeURIComponent(keyword)}${cStr}&mode=${searchMode}&limit=1500${aiParams}`, {
|
| 544 |
headers: { 'Authorization': `Bearer ${token}` },
|
|
|
|
| 866 |
</button>
|
| 867 |
</div>
|
| 868 |
|
| 869 |
+
<div className="flex items-center gap-2 self-end sm:self-auto flex-wrap">
|
| 870 |
+
{isAiConnected && (
|
| 871 |
+
<div className="flex items-center gap-1.5 bg-card/90 border border-emerald-500/30 px-2.5 py-1.5 rounded-xl text-xs shadow-xs hover:border-emerald-500/50 transition-all">
|
| 872 |
+
<Cpu size={13} className="text-emerald-400 shrink-0" />
|
| 873 |
+
<span className="text-[11px] text-foreground/50 font-medium hidden sm:inline">Model:</span>
|
| 874 |
+
<select
|
| 875 |
+
value={aiModel}
|
| 876 |
+
onChange={(e) => {
|
| 877 |
+
const newM = e.target.value;
|
| 878 |
+
setAiModel(newM);
|
| 879 |
+
localStorage.setItem('arkitel_ai_model', newM);
|
| 880 |
+
setSuccessMsg(`Switched AI model to ${getShortModelName(newM)}`);
|
| 881 |
+
setTimeout(() => setSuccessMsg(''), 3000);
|
| 882 |
+
}}
|
| 883 |
+
className="bg-transparent text-xs text-foreground font-semibold focus:outline-none cursor-pointer max-w-[130px] sm:max-w-[200px] truncate"
|
| 884 |
+
title="Active Model for AI Scraping and Keyword Suggestions"
|
| 885 |
+
>
|
| 886 |
+
{aiModel && !availableModels.some(m => m.id === aiModel) && (
|
| 887 |
+
<option value={aiModel} className="bg-card text-foreground">{aiModel}</option>
|
| 888 |
+
)}
|
| 889 |
+
{availableModels.length > 0 ? (
|
| 890 |
+
availableModels.map(m => (
|
| 891 |
+
<option key={m.id} value={m.id} className="bg-card text-foreground">
|
| 892 |
+
{m.name || m.id}
|
| 893 |
+
</option>
|
| 894 |
+
))
|
| 895 |
+
) : (
|
| 896 |
+
TOP_MODELS.map(m => (
|
| 897 |
+
<option key={m.id} value={m.id} className="bg-card text-foreground">
|
| 898 |
+
{m.name}
|
| 899 |
+
</option>
|
| 900 |
+
))
|
| 901 |
+
)}
|
| 902 |
+
</select>
|
| 903 |
+
</div>
|
| 904 |
+
)}
|
| 905 |
+
|
| 906 |
<button
|
| 907 |
type="button"
|
| 908 |
+
onClick={() => {
|
| 909 |
+
setIsAiModalOpen(true);
|
| 910 |
+
if (availableModels.length === 0) fetchOpenRouterModels();
|
| 911 |
+
}}
|
| 912 |
+
className={`px-3 py-1.5 rounded-xl text-xs font-semibold flex items-center gap-2 transition-all cursor-pointer ${
|
| 913 |
+
isAiConnected
|
| 914 |
+
? 'border border-emerald-500/50 bg-gradient-to-r from-emerald-500/15 via-teal-500/10 to-indigo-500/15 text-emerald-400 shadow-[0_0_18px_rgba(16,185,129,0.35)] ring-1 ring-emerald-500/40 hover:border-emerald-400 hover:shadow-[0_0_24px_rgba(16,185,129,0.55)]'
|
| 915 |
+
: 'border border-border/80 bg-card hover:bg-foreground/5 text-foreground/70 hover:text-foreground shadow-xs'
|
| 916 |
+
}`}
|
| 917 |
+
title={isAiConnected ? "OpenRouter AI Connected & Active. Click to configure or view 400+ models." : "Connect OpenRouter AI Key"}
|
| 918 |
>
|
| 919 |
+
{isAiConnected ? (
|
| 920 |
+
<>
|
| 921 |
+
<span className="relative flex h-2 w-2">
|
| 922 |
+
<span className="animate-ping absolute inline-flex h-full w-full rounded-full bg-emerald-400 opacity-75"></span>
|
| 923 |
+
<span className="relative inline-flex rounded-full h-2 w-2 bg-emerald-500 shadow-[0_0_8px_rgba(52,211,153,0.9)]"></span>
|
| 924 |
+
</span>
|
| 925 |
+
<span className="text-emerald-300 font-bold tracking-tight">OpenRouter</span>
|
| 926 |
+
<span className="text-[10px] px-1.5 py-0.5 rounded-md bg-emerald-500/20 text-emerald-200 border border-emerald-500/30 font-mono max-w-[120px] truncate hidden xs:inline-block">
|
| 927 |
+
{getShortModelName(aiModel)}
|
| 928 |
+
</span>
|
| 929 |
+
<Settings2 size={12} className="text-emerald-400/80 hover:text-emerald-300 transition-colors" />
|
| 930 |
+
</>
|
| 931 |
+
) : (
|
| 932 |
+
<>
|
| 933 |
+
<Settings2 size={13} className="text-indigo-400" />
|
| 934 |
+
<span>Connect OpenRouter</span>
|
| 935 |
+
<span className="text-[10px] px-1.5 py-0.5 rounded-md bg-indigo-500/10 text-indigo-400 font-mono">AI</span>
|
| 936 |
+
</>
|
| 937 |
+
)}
|
| 938 |
</button>
|
| 939 |
</div>
|
| 940 |
</div>
|
|
|
|
| 1036 |
{suggestions.length} keywords
|
| 1037 |
</span>
|
| 1038 |
</div>
|
| 1039 |
+
<p className="text-[11px] text-foreground/45 font-medium">Click any keyword to search in active {searchMode === 'ai' ? 'AI Scraper' : searchMode === 'fixed' ? 'Fixed (60+ Suffixes)' : 'Free'} mode.</p>
|
| 1040 |
</div>
|
| 1041 |
</div>
|
| 1042 |
|
|
|
|
| 1096 |
</div>
|
| 1097 |
|
| 1098 |
{!isSuggestionsMinimized && (
|
| 1099 |
+
<div className="flex flex-wrap gap-1.5 max-h-64 overflow-y-auto custom-scrollbar pr-1 pt-1">
|
| 1100 |
{suggestions.map((kw, i) => (
|
| 1101 |
<button
|
| 1102 |
key={i}
|
|
|
|
| 1371 |
</div>
|
| 1372 |
)}
|
| 1373 |
|
| 1374 |
+
{/* OpenRouter AI Configuration & Model Selection Modal */}
|
| 1375 |
{isAiModalOpen && (
|
| 1376 |
+
<div className="fixed inset-0 bg-black/80 backdrop-blur-sm z-[110] flex items-center justify-center p-3 sm:p-4 animate-in fade-in duration-200">
|
| 1377 |
+
<div className="bg-card border border-border rounded-2xl w-full max-w-xl shadow-2xl overflow-hidden flex flex-col max-h-[92vh] animate-in zoom-in-95 duration-200">
|
| 1378 |
+
{/* Modal Header */}
|
| 1379 |
+
<div className="flex justify-between items-center p-4 sm:p-5 border-b border-border/70 bg-foreground/[0.01]">
|
| 1380 |
+
<div className="flex items-center gap-3">
|
| 1381 |
+
<div className="w-9 h-9 rounded-xl bg-gradient-to-br from-indigo-500/20 to-purple-500/20 text-indigo-400 flex items-center justify-center border border-indigo-500/30 shadow-xs">
|
| 1382 |
+
<Sparkles size={18} />
|
| 1383 |
</div>
|
| 1384 |
<div>
|
| 1385 |
+
<div className="flex items-center gap-2">
|
| 1386 |
+
<h3 className="font-bold text-sm text-foreground">OpenRouter AI Engine</h3>
|
| 1387 |
+
{isAiConnected && (
|
| 1388 |
+
<span className="px-2 py-0.5 rounded-full bg-emerald-500/15 border border-emerald-500/30 text-emerald-400 font-bold text-[10px] flex items-center gap-1 shadow-[0_0_10px_rgba(16,185,129,0.3)]">
|
| 1389 |
+
<span className="w-1.5 h-1.5 rounded-full bg-emerald-400 animate-pulse" />
|
| 1390 |
+
Connected
|
| 1391 |
+
</span>
|
| 1392 |
+
)}
|
| 1393 |
+
</div>
|
| 1394 |
+
<p className="text-[11px] text-foreground/45 font-medium">One connection for 400+ AI models (Llama 3.3, Claude 3.5, GPT-4o, DeepSeek)</p>
|
| 1395 |
</div>
|
| 1396 |
</div>
|
| 1397 |
<button
|
| 1398 |
onClick={() => { setIsAiModalOpen(false); setAiValidationResult(null); }}
|
| 1399 |
+
className="text-foreground/40 hover:text-foreground p-1.5 rounded-lg hover:bg-foreground/5 transition-colors"
|
| 1400 |
>
|
| 1401 |
<X size={16} />
|
| 1402 |
</button>
|
| 1403 |
</div>
|
| 1404 |
|
| 1405 |
+
{/* Modal Body */}
|
| 1406 |
+
<div className="p-4 sm:p-5 space-y-4 overflow-y-auto custom-scrollbar flex-1">
|
| 1407 |
+
{/* API Key Section */}
|
| 1408 |
+
<div className="p-3.5 rounded-xl bg-foreground/[0.02] border border-border/70 space-y-2.5">
|
| 1409 |
+
<div className="flex items-center justify-between">
|
| 1410 |
+
<label className="text-[11px] font-bold text-foreground/70 uppercase tracking-wider flex items-center gap-1.5">
|
| 1411 |
+
<Key size={12} className="text-indigo-400" />
|
| 1412 |
+
OpenRouter API Key
|
| 1413 |
+
</label>
|
| 1414 |
+
<a
|
| 1415 |
+
href="https://openrouter.ai/keys"
|
| 1416 |
+
target="_blank"
|
| 1417 |
+
rel="noreferrer"
|
| 1418 |
+
className="text-[11px] text-indigo-400 hover:text-indigo-300 font-medium flex items-center gap-1 hover:underline"
|
| 1419 |
+
>
|
| 1420 |
+
<span>Get OpenRouter Key</span>
|
| 1421 |
+
<ExternalLink size={10} />
|
| 1422 |
+
</a>
|
| 1423 |
+
</div>
|
| 1424 |
+
|
| 1425 |
+
<div className="relative">
|
| 1426 |
+
<input
|
| 1427 |
+
type={showAiKey ? "text" : "password"}
|
| 1428 |
+
value={aiApiKey}
|
| 1429 |
+
onChange={(e) => setAiApiKey(e.target.value)}
|
| 1430 |
+
placeholder="sk-or-v1-..."
|
| 1431 |
+
className="w-full bg-input border border-border/80 text-foreground rounded-xl py-2 pl-3 pr-10 text-xs focus:ring-2 focus:ring-indigo-500/20 outline-none transition-all placeholder:text-foreground/30 font-mono"
|
| 1432 |
+
/>
|
| 1433 |
+
<button
|
| 1434 |
+
type="button"
|
| 1435 |
+
onClick={() => setShowAiKey(!showAiKey)}
|
| 1436 |
+
className="absolute right-2.5 top-1/2 -translate-y-1/2 text-foreground/40 hover:text-foreground p-1 rounded-md transition-colors"
|
| 1437 |
+
title={showAiKey ? "Hide key" : "Show key"}
|
| 1438 |
+
>
|
| 1439 |
+
{showAiKey ? <EyeOff size={14} /> : <Eye size={14} />}
|
| 1440 |
+
</button>
|
| 1441 |
+
</div>
|
| 1442 |
+
|
| 1443 |
+
<div className="flex items-center gap-2 pt-1">
|
| 1444 |
+
<button
|
| 1445 |
+
type="button"
|
| 1446 |
+
onClick={handleValidateAi}
|
| 1447 |
+
disabled={isValidatingAi || !aiApiKey.trim()}
|
| 1448 |
+
className={`flex-1 py-2 px-3 rounded-xl text-xs font-semibold flex items-center justify-center gap-2 transition-all cursor-pointer ${
|
| 1449 |
+
isAiConnected
|
| 1450 |
+
? 'border border-emerald-500/40 bg-emerald-500/10 text-emerald-400 hover:bg-emerald-500/20 shadow-[0_0_12px_rgba(16,185,129,0.2)]'
|
| 1451 |
+
: 'border border-indigo-500/30 bg-indigo-500/10 hover:bg-indigo-500/20 text-indigo-400'
|
| 1452 |
+
} disabled:opacity-40`}
|
| 1453 |
+
>
|
| 1454 |
+
{isValidatingAi ? (
|
| 1455 |
+
<>
|
| 1456 |
+
<Loader2 size={13} className="animate-spin" />
|
| 1457 |
+
<span>Connecting & Verifying...</span>
|
| 1458 |
+
</>
|
| 1459 |
+
) : isAiConnected ? (
|
| 1460 |
+
<>
|
| 1461 |
+
<CheckCircle2 size={13} className="text-emerald-400" />
|
| 1462 |
+
<span>Verified & Active (Click to Re-test)</span>
|
| 1463 |
+
</>
|
| 1464 |
+
) : (
|
| 1465 |
+
<>
|
| 1466 |
+
<Sparkles size={13} />
|
| 1467 |
+
<span>Connect & Verify OpenRouter</span>
|
| 1468 |
+
</>
|
| 1469 |
+
)}
|
| 1470 |
+
</button>
|
| 1471 |
+
|
| 1472 |
+
{isAiConnected && (
|
| 1473 |
<button
|
|
|
|
| 1474 |
type="button"
|
| 1475 |
+
onClick={handleDisconnectAi}
|
| 1476 |
+
className="py-2 px-3 rounded-xl border border-rose-500/30 bg-rose-500/10 hover:bg-rose-500/20 text-rose-400 text-xs font-semibold transition-all cursor-pointer"
|
| 1477 |
+
title="Disconnect and remove saved API key"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1478 |
>
|
| 1479 |
+
Disconnect
|
| 1480 |
</button>
|
| 1481 |
+
)}
|
| 1482 |
</div>
|
| 1483 |
+
|
| 1484 |
+
{/* Validation Feedback Banner */}
|
| 1485 |
+
{aiValidationResult && (
|
| 1486 |
+
<div className={`p-2.5 rounded-xl text-xs flex items-start gap-2 border animate-in fade-in duration-200 ${
|
| 1487 |
+
aiValidationResult.valid
|
| 1488 |
+
? 'bg-emerald-500/10 border-emerald-500/30 text-emerald-400 shadow-[0_0_10px_rgba(16,185,129,0.2)]'
|
| 1489 |
+
: 'bg-rose-500/10 border-rose-500/30 text-rose-400'
|
| 1490 |
+
}`}>
|
| 1491 |
+
{aiValidationResult.valid ? <CheckCircle2 size={14} className="shrink-0 mt-0.5" /> : <AlertCircle size={14} className="shrink-0 mt-0.5" />}
|
| 1492 |
+
<div className="flex-1 font-medium">{aiValidationResult.message}</div>
|
| 1493 |
+
</div>
|
| 1494 |
+
)}
|
| 1495 |
</div>
|
| 1496 |
|
| 1497 |
+
{/* Active Model Card */}
|
| 1498 |
+
<div className="p-3 rounded-xl bg-gradient-to-r from-emerald-500/10 via-teal-500/5 to-indigo-500/10 border border-emerald-500/30 flex items-center justify-between shadow-[0_0_12px_rgba(16,185,129,0.15)]">
|
| 1499 |
+
<div className="flex items-center gap-2.5 min-w-0">
|
| 1500 |
+
<div className="w-7 h-7 rounded-lg bg-emerald-500/20 text-emerald-400 flex items-center justify-center shrink-0 border border-emerald-500/30">
|
| 1501 |
+
<Cpu size={14} />
|
| 1502 |
+
</div>
|
| 1503 |
+
<div className="min-w-0">
|
| 1504 |
+
<div className="text-[10px] text-foreground/45 font-bold uppercase tracking-wider">Active Selected Model</div>
|
| 1505 |
+
<div className="text-xs font-bold text-foreground truncate">{aiModel || 'meta-llama/llama-3.3-70b-instruct'}</div>
|
| 1506 |
+
</div>
|
| 1507 |
+
</div>
|
| 1508 |
+
<span className="text-[10px] uppercase font-mono font-bold text-emerald-400 bg-emerald-500/20 border border-emerald-500/30 px-2 py-0.5 rounded-md shrink-0">
|
| 1509 |
+
Selected
|
| 1510 |
+
</span>
|
|
|
|
| 1511 |
</div>
|
| 1512 |
|
| 1513 |
+
{/* Recommended Quick Picks */}
|
| 1514 |
<div>
|
| 1515 |
+
<label className="block text-[11px] font-bold text-foreground/60 uppercase tracking-wider mb-2">
|
| 1516 |
+
Top Recommended Models
|
| 1517 |
</label>
|
| 1518 |
+
<div className="grid grid-cols-2 sm:grid-cols-3 gap-2">
|
| 1519 |
+
{TOP_MODELS.map((m) => {
|
| 1520 |
+
const isSelected = aiModel === m.id;
|
| 1521 |
+
return (
|
| 1522 |
+
<button
|
| 1523 |
+
key={m.id}
|
| 1524 |
+
type="button"
|
| 1525 |
+
onClick={() => setAiModel(m.id)}
|
| 1526 |
+
className={`p-2.5 rounded-xl border text-left transition-all cursor-pointer relative ${
|
| 1527 |
+
isSelected
|
| 1528 |
+
? 'bg-emerald-500/15 border-emerald-500/50 text-foreground ring-1 ring-emerald-500/40 shadow-[0_0_12px_rgba(16,185,129,0.25)]'
|
| 1529 |
+
: 'bg-foreground/[0.02] border-border/70 text-foreground/70 hover:text-foreground hover:bg-foreground/5 hover:border-border'
|
| 1530 |
+
}`}
|
| 1531 |
+
>
|
| 1532 |
+
<div className="flex items-center justify-between mb-1">
|
| 1533 |
+
<span className="text-[9px] font-mono uppercase tracking-wider text-indigo-400 font-bold">{m.provider}</span>
|
| 1534 |
+
{isSelected && <Check size={12} className="text-emerald-400 font-bold" />}
|
| 1535 |
+
</div>
|
| 1536 |
+
<div className="text-xs font-bold text-foreground truncate">{m.name}</div>
|
| 1537 |
+
<div className="text-[10px] text-foreground/40 mt-0.5 truncate">{m.tag}</div>
|
| 1538 |
+
</button>
|
| 1539 |
+
);
|
| 1540 |
+
})}
|
| 1541 |
+
</div>
|
| 1542 |
</div>
|
| 1543 |
|
| 1544 |
+
{/* Dynamic Model Catalogue (Search & Select from 400+ OpenRouter models) */}
|
| 1545 |
+
<div className="space-y-2 pt-1">
|
| 1546 |
+
<div className="flex items-center justify-between">
|
| 1547 |
+
<label className="text-[11px] font-bold text-foreground/60 uppercase tracking-wider flex items-center gap-1.5">
|
| 1548 |
+
<span>All Available Models</span>
|
| 1549 |
+
{availableModels.length > 0 && (
|
| 1550 |
+
<span className="text-[10px] px-1.5 py-0.2 rounded-full bg-indigo-500/15 text-indigo-400 font-mono font-bold">
|
| 1551 |
+
{availableModels.length}
|
| 1552 |
+
</span>
|
| 1553 |
+
)}
|
| 1554 |
+
</label>
|
| 1555 |
+
<button
|
| 1556 |
+
type="button"
|
| 1557 |
+
onClick={fetchOpenRouterModels}
|
| 1558 |
+
disabled={isLoadingModels}
|
| 1559 |
+
className="text-[11px] text-foreground/50 hover:text-indigo-400 flex items-center gap-1 transition-colors"
|
| 1560 |
+
title="Reload model catalogue from OpenRouter"
|
| 1561 |
+
>
|
| 1562 |
+
<RefreshCw size={11} className={isLoadingModels ? "animate-spin" : ""} />
|
| 1563 |
+
<span>{isLoadingModels ? "Fetching..." : "Refresh"}</span>
|
| 1564 |
+
</button>
|
| 1565 |
+
</div>
|
| 1566 |
|
| 1567 |
+
{/* Search Input */}
|
| 1568 |
+
<div className="relative">
|
| 1569 |
+
<div className="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none text-foreground/40">
|
| 1570 |
+
<Search size={13} />
|
| 1571 |
+
</div>
|
| 1572 |
+
<input
|
| 1573 |
+
type="text"
|
| 1574 |
+
value={modelSearch}
|
| 1575 |
+
onChange={(e) => setModelSearch(e.target.value)}
|
| 1576 |
+
placeholder="Search 400+ models by name, provider, or ID (e.g. llama, claude, deepseek)..."
|
| 1577 |
+
className="w-full bg-input border border-border/80 text-foreground rounded-xl py-2 pl-8 pr-3 text-xs focus:ring-2 focus:ring-indigo-500/20 outline-none transition-all placeholder:text-foreground/30 font-medium"
|
| 1578 |
+
/>
|
| 1579 |
</div>
|
|
|
|
| 1580 |
|
| 1581 |
+
{/* Scrollable Model List */}
|
| 1582 |
+
<div className="border border-border/70 rounded-xl overflow-hidden max-h-44 overflow-y-auto custom-scrollbar divide-y divide-border/40 bg-foreground/[0.01]">
|
| 1583 |
+
{isLoadingModels ? (
|
| 1584 |
+
<div className="p-6 flex flex-col items-center justify-center text-foreground/40 gap-2">
|
| 1585 |
+
<Loader2 size={16} className="animate-spin text-indigo-400" />
|
| 1586 |
+
<span className="text-xs">Fetching OpenRouter models...</span>
|
| 1587 |
+
</div>
|
| 1588 |
+
) : availableModels.length === 0 ? (
|
| 1589 |
+
<div className="p-4 text-center text-xs text-foreground/40">
|
| 1590 |
+
No models loaded yet. Click "Refresh" to fetch the latest catalogue.
|
| 1591 |
+
</div>
|
| 1592 |
+
) : (
|
| 1593 |
+
availableModels
|
| 1594 |
+
.filter(m =>
|
| 1595 |
+
!modelSearch.trim() ||
|
| 1596 |
+
m.name.toLowerCase().includes(modelSearch.toLowerCase()) ||
|
| 1597 |
+
m.id.toLowerCase().includes(modelSearch.toLowerCase())
|
| 1598 |
+
)
|
| 1599 |
+
.slice(0, 150)
|
| 1600 |
+
.map((m) => {
|
| 1601 |
+
const isSelected = aiModel === m.id;
|
| 1602 |
+
return (
|
| 1603 |
+
<button
|
| 1604 |
+
key={m.id}
|
| 1605 |
+
type="button"
|
| 1606 |
+
onClick={() => setAiModel(m.id)}
|
| 1607 |
+
className={`w-full p-2.5 px-3 flex items-center justify-between text-left transition-colors cursor-pointer ${
|
| 1608 |
+
isSelected
|
| 1609 |
+
? 'bg-emerald-500/10 text-emerald-400 font-semibold'
|
| 1610 |
+
: 'hover:bg-foreground/5 text-foreground/80'
|
| 1611 |
+
}`}
|
| 1612 |
+
>
|
| 1613 |
+
<div className="min-w-0 pr-2">
|
| 1614 |
+
<div className="text-xs font-semibold truncate flex items-center gap-1.5">
|
| 1615 |
+
{m.name || m.id}
|
| 1616 |
+
</div>
|
| 1617 |
+
<div className="text-[10px] text-foreground/40 font-mono truncate">{m.id}</div>
|
| 1618 |
+
</div>
|
| 1619 |
+
<div className="flex items-center gap-2 shrink-0">
|
| 1620 |
+
{m.context_length ? (
|
| 1621 |
+
<span className="text-[9px] font-mono text-foreground/40 px-1.5 py-0.5 rounded bg-card border border-border/60">
|
| 1622 |
+
{Math.round(m.context_length / 1000)}k ctx
|
| 1623 |
+
</span>
|
| 1624 |
+
) : null}
|
| 1625 |
+
{isSelected && <CheckCircle2 size={14} className="text-emerald-400" />}
|
| 1626 |
+
</div>
|
| 1627 |
+
</button>
|
| 1628 |
+
);
|
| 1629 |
+
})
|
| 1630 |
+
)}
|
| 1631 |
+
</div>
|
| 1632 |
+
</div>
|
| 1633 |
</div>
|
| 1634 |
|
| 1635 |
+
{/* Modal Footer */}
|
| 1636 |
+
<div className="p-4 px-5 border-t border-border/70 flex justify-between items-center bg-foreground/[0.01]">
|
| 1637 |
+
<span className="text-[11px] text-foreground/40 font-mono">
|
| 1638 |
+
{isAiConnected ? "Connected & saved locally" : "Unconnected"}
|
| 1639 |
+
</span>
|
| 1640 |
+
<div className="flex items-center gap-2">
|
| 1641 |
+
<button
|
| 1642 |
+
type="button"
|
| 1643 |
+
onClick={() => { setIsAiModalOpen(false); setAiValidationResult(null); }}
|
| 1644 |
+
className="px-3.5 py-1.5 text-xs text-foreground/60 hover:text-foreground font-semibold rounded-lg hover:bg-foreground/5 transition-all"
|
| 1645 |
+
>
|
| 1646 |
+
Close
|
| 1647 |
+
</button>
|
| 1648 |
+
<button
|
| 1649 |
+
type="button"
|
| 1650 |
+
onClick={handleSaveAiConfig}
|
| 1651 |
+
className="px-4 py-1.5 text-xs text-white bg-indigo-600 hover:bg-indigo-500 font-bold rounded-xl transition-all shadow-md active:scale-95 flex items-center gap-1.5 cursor-pointer"
|
| 1652 |
+
>
|
| 1653 |
+
<Check size={13} />
|
| 1654 |
+
<span>Save & Use Model</span>
|
| 1655 |
+
</button>
|
| 1656 |
+
</div>
|
| 1657 |
</div>
|
| 1658 |
</div>
|
| 1659 |
</div>
|