File size: 7,044 Bytes
caf8059
 
 
 
7ebebb0
 
 
caf8059
 
 
 
 
 
 
 
5fd201e
caf8059
 
 
 
 
 
 
115b94d
caf8059
 
 
 
 
115b94d
caf8059
 
 
 
 
 
 
 
 
 
 
 
 
7ebebb0
 
 
 
 
 
 
 
 
 
 
 
 
115b94d
7ebebb0
115b94d
7ebebb0
115b94d
7ebebb0
 
115b94d
7ebebb0
 
 
 
 
 
 
 
 
 
 
caf8059
 
 
115b94d
 
 
 
 
 
 
 
 
caf8059
 
 
 
 
 
5ca3c16
caf8059
 
5ca3c16
 
caf8059
 
 
 
 
 
7ebebb0
52107a5
 
 
 
 
7ebebb0
caf8059
7ebebb0
52107a5
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""HF-owned Docker Space bootstrap; secrets stay in process memory, never in the image."""
import os
import sqlite3
import time
import json
import hashlib
import threading
from pathlib import Path
from urllib.parse import urlsplit
from src.adapters.llm.chat_completion import ChatCompletionProvider
from src.modules.llm_gateway.service import ProviderFailure
from src.modules.llm_gateway.routing import PROFILES,VERSION
from src.apps.workbench.engine import LocalWorkbench
from src.apps.workbench.web import create_web
from deploy.hf.adapter import SpaceIngress
from deploy.hf.preview_bridge import PreviewBridge

ROOT=Path(__file__).resolve().parents[2]
ORIGIN='https://dellweb3-vibecoding.hf.space'


class DemoCallBudget:
    def __init__(self,path,limit=20):
        if not 0<=limit<=100:raise ValueError('INVALID_DEMO_CALL_LIMIT')
        self.path,self.limit=Path(path),limit;self.path.parent.mkdir(parents=True,exist_ok=True)
        with sqlite3.connect(self.path) as db:db.execute('CREATE TABLE IF NOT EXISTS physical_calls(id INTEGER PRIMARY KEY,created REAL,model TEXT)')
    def claim(self,model):
        with sqlite3.connect(self.path,timeout=30) as db:
            db.execute('BEGIN IMMEDIATE')
            if self.limit and db.execute('SELECT count(*) FROM physical_calls').fetchone()[0]>=self.limit:
                raise ProviderFailure('MODEL_DEMO_CALL_LIMIT',rejected=True,retryable=False)
            db.execute('INSERT INTO physical_calls(created,model) VALUES(?,?)',(time.time(),model))


class LimitedProvider:
    def __init__(self,provider,budget):self.provider,self.budget=provider,budget
    def __getattr__(self,key):return getattr(self.provider,key)
    def complete(self,messages,max_tokens,cancelled):
        if cancelled():raise ProviderFailure('RUN_CANCELLED',rejected=True)
        self.budget.claim(self.model)
        return self.provider.complete(messages,max_tokens,cancelled)


def initialize_model_health(providers,path):
    """Real, budgeted probes inside the Space; uncertain attempts are never replayed."""
    path=Path(path);path.parent.mkdir(parents=True,exist_ok=True)
    selected=[p for p in providers if p.selectable]
    if not selected:return
    binding=hashlib.sha256((VERSION+'\0'+selected[0].endpoint+'\0'+selected[0]._token).encode()).hexdigest()
    report={'binding':binding,'models':{}}
    if path.is_file():
        previous=json.loads(path.read_text(encoding='utf-8'))
        if previous.get('binding')==binding:report=previous
    def save():
        pending=path.with_suffix('.tmp');pending.write_text(json.dumps(report),encoding='utf-8');pending.replace(path)
    for provider in selected:
        provider_binding=hashlib.sha256((VERSION+'\0'+provider.endpoint+'\0'+provider._token).encode()).hexdigest()
        old=report['models'].get(provider.model)
        if old and old.get('binding',report['binding'])==provider_binding:
            provider.health={**old,'status':'unknown' if old['status']=='reserved' else old['status']};continue
        row={'status':'reserved','checked_at':time.time(),'binding':provider_binding};report['models'][provider.model]=row;save()
        try:
            reply=provider.complete([{'role':'system','content':'Return JSON only.'},
                                     {'role':'user','content':'Return exactly {"summary":"ok"}.'}],512 if getattr(provider,'provider_family',None)=='openai' else 64,lambda:False)
            row.update(status='healthy' if reply.content=={'summary':'ok'} else 'rejected',
                       request_id=reply.request_id,input_tokens=reply.input_tokens,output_tokens=reply.output_tokens)
            if row['status']!='healthy':row['code']='PROBE_CONTRACT_INVALID'
        except ProviderFailure as exc:
            row.update(status='rejected' if exc.rejected else 'unknown',code=exc.code,
                       input_tokens=exc.input_tokens,output_tokens=exc.output_tokens)
        except Exception:
            row.update(status='unknown',code='PROBE_RESULT_UNKNOWN')
        provider.health=dict(row);save()


def providers_from_environment(root):
    # Remove the provider secret before spawning trusted tools/VM emulators.
    token=os.environ.pop('DASHSCOPE_API_KEY',None)
    openai_token=os.environ.pop('OPENAI_API_KEY',None)
    if not token and not openai_token:return []
    budget=DemoCallBudget(Path(root)/'model-demo-budget.sqlite',int(os.environ.get('ZKCODING_MODEL_CALL_LIMIT','20')))
    result=[]
    if openai_token:
        if not 8<=len(openai_token)<=8192 or any(not 33<=ord(c)<=126 for c in openai_token):raise ValueError('INVALID_MODEL_SECRET')
        from src.adapters.llm.openai_code import OpenAICodeProvider,MODELS
        result.extend(LimitedProvider(OpenAICodeProvider(model=model,token=openai_token),budget) for model in MODELS)
    if not token:return result
    if not 8<=len(token)<=8192 or any(not 33<=ord(c)<=126 for c in token):raise ValueError('INVALID_MODEL_SECRET')
    base=os.environ.get('DASHSCOPE_BASE_URL','https://dashscope.aliyuncs.com/compatible-mode/v1').rstrip('/')
    url=urlsplit(base)
    if (url.scheme!='https' or url.hostname not in {'dashscope.aliyuncs.com','dashscope-intl.aliyuncs.com','dashscope-us.aliyuncs.com','cn-hongkong.dashscope.aliyuncs.com'}
            or url.path!='/compatible-mode/v1' or url.port not in {None,443} or url.username or url.password or url.query or url.fragment):
        raise ValueError('INVALID_MODEL_ENDPOINT')
    for model,profile in PROFILES.items():
        provider=ChatCompletionProvider(name='bailian-'+model,endpoint=base+'/chat/completions',model=model,token=token,timeout=120,
                                        enable_thinking=profile['thinking'],json_mode=profile['json_mode'])
        provider.routing_version=VERSION;provider.task_roles=profile['roles']
        provider.selectable=profile.get('selectable',False);provider.health={'status':'not_probed'}
        result.append(LimitedProvider(provider,budget))
    return result


def create_app():
    data=ROOT/'.local/hf-runtime';data.mkdir(parents=True,exist_ok=True)
    providers=providers_from_environment(data)
    preferred=os.environ.get('ZKCODING_DEFAULT_MODEL','')
    if preferred:
        if not any(p.model==preferred and p.selectable for p in providers):
            raise ValueError('INVALID_DEFAULT_MODEL')
        for provider in providers:provider.automatic_preference=preferred
    engine=LocalWorkbench(data,providers,sandbox_capacity=int(os.environ.get('ZKCODING_SANDBOX_CAPACITY','3')))
    engine.identity.seed_demo_users();engine.preview_origin=ORIGIN
    threading.Thread(target=initialize_model_health,args=(providers,data/'model-health.json'),daemon=True,name='space-model-health').start()
    web=create_web(engine,7860)
    release_path=ROOT/'deploy/hf/release.json'
    if release_path.is_file():
        release=json.loads(release_path.read_text(encoding='utf-8'))
        @web.get('/api/release')
        def release_info():return release
    return SpaceIngress(web,ORIGIN,bridge=PreviewBridge(engine))