This commit is contained in:
1 parent
bb5ff39f03
commit
9b312cd840
8 files changed
+90
-111
No files matched your search
@@ -8,30 +8,14 @@ import subprocess
|
||||
import sys
|
||||
|
||||
|
||||
NAME = 'my-ai-qwen3-retrieval'
|
||||
FILES = ('server.py', 'requirements.txt')
|
||||
CUDA_INDEX = 'https://download.pytorch.org/whl/cu126'
|
||||
TOOL_TIMEOUT_SECONDS = 1800
|
||||
|
||||
|
||||
def digest(path):
|
||||
return hashlib.sha256(path.read_bytes()).hexdigest()
|
||||
|
||||
|
||||
def command(client, arguments, home, dry_run):
|
||||
if dry_run:
|
||||
print('DRYRUN ' + ' '.join(arguments))
|
||||
return True
|
||||
executable = shutil.which(client)
|
||||
if executable is None:
|
||||
raise ValueError(f'{client} CLI is required for semantic retrieval.')
|
||||
result = subprocess.run([executable, *arguments[1:]], stdin=subprocess.DEVNULL, capture_output=True, text=True,
|
||||
timeout=180, env=dict(os.environ, HOME=str(home), USERPROFILE=str(home), CODEX_HOME=str(home / '.codex'), CLAUDE_CONFIG_DIR=str(home / '.claude')))
|
||||
if result.returncode:
|
||||
raise ValueError(f'Semantic retrieval command failed: {client} {" ".join(arguments[1:])}\n{result.stdout}{result.stderr}')
|
||||
return True
|
||||
|
||||
|
||||
def python_path(target):
|
||||
return target / ('Scripts/python.exe' if os.name == 'nt' else 'bin/python')
|
||||
|
||||
@@ -57,26 +41,6 @@ def ensure_runtime(target, dry_run):
|
||||
return python
|
||||
|
||||
|
||||
def set_codex_tool_timeout(path, seconds):
|
||||
lines = path.read_text(encoding='utf-8').splitlines(keepends=True)
|
||||
section = f'[mcp_servers.{NAME}]'
|
||||
start = next((number for number, line in enumerate(lines) if line.strip() == section), None)
|
||||
if start is None:
|
||||
raise ValueError('Codex semantic retrieval configuration is missing.')
|
||||
end = next((number for number in range(start + 1, len(lines)) if lines[number].lstrip().startswith('[')), len(lines))
|
||||
for number in range(start + 1, end):
|
||||
if lines[number].lstrip().startswith('tool_timeout_sec'):
|
||||
newline = '\r\n' if lines[number].endswith('\r\n') else '\n'
|
||||
lines[number] = f'tool_timeout_sec = {seconds}{newline}'
|
||||
break
|
||||
else:
|
||||
while end > start + 1 and not lines[end - 1].strip():
|
||||
end -= 1
|
||||
newline = '\r\n' if lines and lines[0].endswith('\r\n') else '\n'
|
||||
lines.insert(end, f'tool_timeout_sec = {seconds}{newline}')
|
||||
path.write_text(''.join(lines), encoding='utf-8')
|
||||
|
||||
|
||||
def load_state(path):
|
||||
if not path.exists():
|
||||
return {'version': 1, 'clients': [], 'files': {}}
|
||||
@@ -110,8 +74,6 @@ def sync(root, home, clients, enabled, dry_run, update, summary=False):
|
||||
if not summary:
|
||||
print('DRYRUN disable semantic retrieval: ' + (', '.join(owned) if owned else 'nothing owned'))
|
||||
return
|
||||
for client in owned:
|
||||
command(client, [client, 'mcp', 'remove', NAME] + (['--scope', 'user'] if client == 'claude' else []), home, False)
|
||||
state['clients'] = [client for client in state['clients'] if client not in clients]
|
||||
if state['clients']:
|
||||
save_state(state_path, state, False)
|
||||
@@ -135,23 +97,9 @@ def sync(root, home, clients, enabled, dry_run, update, summary=False):
|
||||
shutil.copy2(source / name, target / name)
|
||||
state['files'][name] = digest(target / name)
|
||||
python = ensure_runtime(target, False)
|
||||
existing_clients = set(state['clients'])
|
||||
state['clients'] = list(dict.fromkeys(state['clients']))
|
||||
save_state(state_path, state, False)
|
||||
arguments = ['--', str(python), str(target / 'server.py'), '--root', '.', '--data-dir', str(target / 'data'), '--model-cache', str(target / 'model-cache')]
|
||||
for client in clients:
|
||||
command_args = [client, 'mcp', 'add']
|
||||
if client == 'claude':
|
||||
command_args += [NAME, '--scope', 'user', '-e', f'HF_HOME={target / "model-cache"}']
|
||||
else:
|
||||
command_args += ['--env', f'HF_HOME={target / "model-cache"}']
|
||||
command_args += [NAME]
|
||||
command_args += arguments
|
||||
if update and client in existing_clients:
|
||||
command(client, [client, 'mcp', 'remove', NAME] + (['--scope', 'user'] if client == 'claude' else []), home, False)
|
||||
command(client, command_args, home, False)
|
||||
if client == 'codex':
|
||||
set_codex_tool_timeout(home / '.codex' / 'config.toml', TOOL_TIMEOUT_SECONDS)
|
||||
if client not in state['clients']:
|
||||
state['clients'].append(client)
|
||||
save_state(state_path, state, False)
|
||||
|
||||
Reference in new issue
Block a user