witbitz-code 1.15.7__tar.gz → 1.15.8__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {witbitz_code-1.15.7/src/witbitz_code.egg-info → witbitz_code-1.15.8}/PKG-INFO +1 -1
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/pyproject.toml +2 -2
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/__init__.py +1 -1
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/cli.py +24 -2
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/codekeys.py +10 -6
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/confidential.py +150 -2
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/models.py +10 -2
- witbitz_code-1.15.8/src/witbitz_code/venice.py +204 -0
- witbitz_code-1.15.8/src/witbitz_code/venice_models.json +147 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8/src/witbitz_code.egg-info}/PKG-INFO +1 -1
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code.egg-info/SOURCES.txt +3 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_codekeys.py +1 -1
- witbitz_code-1.15.8/tests/test_venice.py +155 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/LICENSE +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/README.md +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/setup.cfg +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/__main__.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/_js.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/account.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/app.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/asks.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/attachments.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/auto.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/auto_runner.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/browse.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/browsetree.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/codetools.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/compress.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/computers.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/confidential_models.json +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/connector.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/device.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/devicelink.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/dirs.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/files.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/folders.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/gateway.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/icons/witbitz-code-wsl.ico +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/icons/witbitz-code.ico +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/images.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/keys.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/link.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/mkdir.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/net.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/offered_models.json +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/opencode_plugins/notes-init.md +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/opencode_plugins/witbitz-browse-cdp.mjs +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/opencode_plugins/witbitz-browse-local.mjs +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/opencode_plugins/witbitz-browse-publisher.mjs +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/opencode_plugins/witbitz-browse-tree.mjs +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/opencode_plugins/witbitz-browser.js +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/opencode_plugins/witbitz-media-server.mjs +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/opencode_plugins/witbitz-notes.js +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/opencode_plugins/witbitz-progress.js +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/outputs.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/pair.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/pairings.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/payers.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/plugins.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/policy.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/ports.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/qr.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/receipt.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/relay.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/remind.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/seccheck.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/seen.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/selfupdate.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/tinfoil_read.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/tools_catalog.json +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/tools_probe.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/toolsetup.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/ui.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/ui_static/app.css +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/ui_static/app.js +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/ui_static/index.html +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/uitools.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/uninstall.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/winagent.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/winbg.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/winconsole.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/winrun.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/wizard.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code/wslstart.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code.egg-info/dependency_links.txt +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code.egg-info/entry_points.txt +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code.egg-info/requires.txt +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/src/witbitz_code.egg-info/top_level.txt +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_asks.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_attachments.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_auto.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_browse.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_browsetree.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_cli.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_codetools.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_confidential.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_connector_e2e.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_device.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_devicelink.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_dirs.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_files.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_files_and_compress.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_folders.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_images.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_outputs.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_pair_flows.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_plugins.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_policy.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_ports.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_ports_win.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_registry.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_relay_codec.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_remind.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_seccheck.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_selfupdate.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_setup.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_tools_probe.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_toolsetup.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_ui.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_ui_page.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_uitools.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_uninstall.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_winagent.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_winbg.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_winconsole.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_windows_files.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_winrun.py +0 -0
- {witbitz_code-1.15.7 → witbitz_code-1.15.8}/tests/test_wslstart.py +0 -0
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "witbitz-code"
|
|
7
|
-
version = "1.15.
|
|
7
|
+
version = "1.15.8"
|
|
8
8
|
description = "Reach OpenCode on this computer from the Witbitz Spaces Code section, through an end-to-end sealed relay."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.10"
|
|
@@ -45,7 +45,7 @@ where = ["src"]
|
|
|
45
45
|
|
|
46
46
|
# Our OpenCode plugins, installed by `serve` (plugins.py) — copies of tools/opencode-plugins, kept current by embed.mjs
|
|
47
47
|
[tool.setuptools.package-data]
|
|
48
|
-
witbitz_code = ["opencode_plugins/*", "confidential_models.json", "offered_models.json", "tools_catalog.json", "ui_static/*", "icons/*"]
|
|
48
|
+
witbitz_code = ["opencode_plugins/*", "confidential_models.json", "offered_models.json", "tools_catalog.json", "venice_models.json", "ui_static/*", "icons/*"]
|
|
49
49
|
|
|
50
50
|
[tool.pytest.ini_options]
|
|
51
51
|
testpaths = ["tests"]
|
|
@@ -27,6 +27,7 @@ from . import __version__, app
|
|
|
27
27
|
from . import pair as pair_cmd
|
|
28
28
|
from . import pairings as pp
|
|
29
29
|
from . import browse as browse_mod, codekeys, codetools, confidential, keys, models, plugins, policy, ports, toolsetup, uninstall as uninstall_mod, winagent, winbg, winconsole, wizard, wslstart
|
|
30
|
+
from . import venice as vn # the zero-retention models (docs/code-venice-fallback.md)
|
|
30
31
|
from ._js import to_number, truthy
|
|
31
32
|
from .connector import pairings_for_port, start_connector
|
|
32
33
|
from .payers import SessionPayers
|
|
@@ -44,6 +45,8 @@ HELP = f"""witbitz-code {__version__} — reach OpenCode on this computer from t
|
|
|
44
45
|
serve [--port <n>] [--no-opencode] [--no-browser] [--private-browser]
|
|
45
46
|
start OpenCode on 127.0.0.1 with your account's keys, and the connector
|
|
46
47
|
status list this computer's pairings and their coding keys
|
|
48
|
+
image-reader [<model>|off] who reads pictures for a text-only model that is NOT confidential: a zero-retention
|
|
49
|
+
model (Venice) — not confidential. Off by default. With no argument: what is set.
|
|
47
50
|
rotate [--account <email>] replace the pairing secret(s) without a scan, then restart serve
|
|
48
51
|
unpair [--account <email>] remove this computer from an account
|
|
49
52
|
uninstall [--yes] [--remove-keys] [--remove-notes] [--remove-sessions] [--remove-opencode]
|
|
@@ -54,6 +57,10 @@ Model calls go from this computer straight to TrustedRouter and Tinfoil, each bi
|
|
|
54
57
|
"""
|
|
55
58
|
|
|
56
59
|
|
|
60
|
+
def _image_reader_path() -> str:
|
|
61
|
+
return str(codekeys.keys_path().parent / "image-reader.json")
|
|
62
|
+
|
|
63
|
+
|
|
57
64
|
def _err(msg: str) -> None:
|
|
58
65
|
print(msg, file=sys.stderr, flush=True)
|
|
59
66
|
|
|
@@ -255,14 +262,14 @@ async def serve(args: list[str], *, stop: Any = None, live_out: dict | None = No
|
|
|
255
262
|
try:
|
|
256
263
|
proxy = await confidential.start_confidential_proxy(port=pport, log=_err, on_progress=lambda ev: live["connector"] and live["connector"].progress(ev),
|
|
257
264
|
credential_for=credential_for, offered=lambda m: m in offered,
|
|
258
|
-
on_key_refused=key_refused)
|
|
265
|
+
on_key_refused=key_refused, image_reader=vn.reader_setting(_image_reader_path()))
|
|
259
266
|
except OSError as e:
|
|
260
267
|
held = "; ".join(f"{(o.get('cmd') or 'a program')[:70]} (process {o['pid']})" for o in ports.port_owners(pport))
|
|
261
268
|
_err(f"witbitz-code: could not start the model proxy on 127.0.0.1:{pport} ({e})"
|
|
262
269
|
+ (f" — held by {held}: an earlier witbitz-code still running? witbitz-code setup offers to stop it" if held
|
|
263
270
|
else " — free that port and run serve again"))
|
|
264
271
|
return 1
|
|
265
|
-
route = {**models.proxy_config(proxy.port), **models.default_model_config()}
|
|
272
|
+
route = {**models.proxy_config(proxy.port, image_reader=vn.reader_setting(_image_reader_path())()), **models.default_model_config()}
|
|
266
273
|
if await is_listening(port) and "--no-opencode" not in args:
|
|
267
274
|
_err(f"witbitz-code: OpenCode is already running on 127.0.0.1:{port}, started some other way — it cannot use your account's keys.")
|
|
268
275
|
_err(" Close it, then run witbitz-code serve again (it starts OpenCode itself).")
|
|
@@ -491,6 +498,21 @@ async def _main(argv: list[str]) -> int:
|
|
|
491
498
|
return await ui_command(rest)
|
|
492
499
|
if cmd == "status":
|
|
493
500
|
return await pair_cmd.main(["--status"])
|
|
501
|
+
if cmd == "image-reader":
|
|
502
|
+
want = rest[0] if rest else ""
|
|
503
|
+
if not want:
|
|
504
|
+
m = vn.reader_setting(_image_reader_path())()
|
|
505
|
+
print(f"{vn.entry(m)['label']} ({m}) reads images for text-only models that are not confidential — not confidentially" if m
|
|
506
|
+
else f"off — text-only models get no images. To set one: witbitz-code image-reader <model>, one of {', '.join(vn.VENICE_READERS)}")
|
|
507
|
+
return 0
|
|
508
|
+
try:
|
|
509
|
+
m = vn.set_reader(_image_reader_path(), want)
|
|
510
|
+
except ValueError as e:
|
|
511
|
+
_err(f"witbitz-code: {e}")
|
|
512
|
+
return 2
|
|
513
|
+
print((f"set: {vn.entry(m)['label']} reads images for text-only models that are not confidential. A confidential model's images are still read only inside Tinfoil's enclave." if m
|
|
514
|
+
else "off: text-only models get no images") + "\nRestart serve for OpenCode to take it.")
|
|
515
|
+
return 0
|
|
494
516
|
if cmd == "rotate":
|
|
495
517
|
return await pair_cmd.main(["--rotate", *rest])
|
|
496
518
|
if cmd == "unpair":
|
|
@@ -30,7 +30,7 @@ from . import app as app_mod
|
|
|
30
30
|
from . import pairings as pp
|
|
31
31
|
|
|
32
32
|
API = "https://api.witbitz.chat/v1/spaces/code-keys"
|
|
33
|
-
PROVIDERS = ("trustedrouter", "tinfoil")
|
|
33
|
+
PROVIDERS = ("trustedrouter", "tinfoil", "venice") # venice: the zero-retention models (docs/code-venice-fallback.md)
|
|
34
34
|
STATUS_EVERY_S = 6 * 3600
|
|
35
35
|
SIGNIN_MIN_LEFT_S = 86_400 # = codeKeys.mjs SIGNIN_MIN_LEFT_SEC
|
|
36
36
|
|
|
@@ -221,6 +221,7 @@ def _take(entry: dict, d: dict) -> dict:
|
|
|
221
221
|
computer's own identity. Raises CodeKeyRefusal when they cannot be opened."""
|
|
222
222
|
entry.update({"keyId": d.get("keyId"), "tenantId": d.get("tenantId"), "userId": d.get("userId"), "kid": d.get("kid"), "caps": d.get("caps") or {}})
|
|
223
223
|
entry.pop("tinfoil", None)
|
|
224
|
+
entry.pop("venice", None)
|
|
224
225
|
sealed = d.get("sealed") or {}
|
|
225
226
|
for provider in PROVIDERS:
|
|
226
227
|
if sealed.get(provider):
|
|
@@ -313,11 +314,14 @@ async def status(client: httpx.AsyncClient, entry: dict) -> dict:
|
|
|
313
314
|
except CodeKeyRefusal as e:
|
|
314
315
|
return {"live": None, "why": str(e)}
|
|
315
316
|
return d
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
317
|
+
# a key minted or renewed server-side after the pairing (Tinfoil's, Venice's) arrives sealed in a status answer
|
|
318
|
+
for p in ("tinfoil", "venice"):
|
|
319
|
+
blob = (d.get("sealed") or {}).get(p)
|
|
320
|
+
if not blob:
|
|
321
|
+
continue
|
|
322
|
+
secret = open_sealed(blob, entry, aad(entry["tenantId"], entry["userId"], entry["kid"], p))
|
|
319
323
|
if secret:
|
|
320
|
-
entry[
|
|
324
|
+
entry[p] = secret
|
|
321
325
|
d["changed"] = True
|
|
322
326
|
return d
|
|
323
327
|
|
|
@@ -378,7 +382,7 @@ class LiveKeys:
|
|
|
378
382
|
e = self.entries.get(computer_id)
|
|
379
383
|
if not e:
|
|
380
384
|
return {"refuse": REVOKED_TEXT}
|
|
381
|
-
return {"trustedrouter": e.get("trustedrouter", ""), "tinfoil": e.get("tinfoil", "")}
|
|
385
|
+
return {"trustedrouter": e.get("trustedrouter", ""), "tinfoil": e.get("tinfoil", ""), "venice": e.get("venice", "")}
|
|
382
386
|
|
|
383
387
|
async def check(self, client: httpx.AsyncClient, say: Any = lambda _m: None) -> None:
|
|
384
388
|
self._fresh()
|
|
@@ -20,6 +20,7 @@ from __future__ import annotations
|
|
|
20
20
|
|
|
21
21
|
import asyncio
|
|
22
22
|
import json
|
|
23
|
+
import os
|
|
23
24
|
import re
|
|
24
25
|
import time
|
|
25
26
|
from typing import Any, Callable
|
|
@@ -30,6 +31,7 @@ from . import images, keys, models as mdl, receipt as rcp, tinfoil_read
|
|
|
30
31
|
from .gateway import verify_gateway_attestation
|
|
31
32
|
|
|
32
33
|
TR_UPSTREAM = "https://api.trustedrouter.com"
|
|
34
|
+
from . import venice as vn # the zero-retention models the person may pick (docs/code-venice-fallback.md)
|
|
33
35
|
GATE_TTL_MS = 600_000
|
|
34
36
|
MAX_BODY = 40 * 1024 * 1024
|
|
35
37
|
HOLD_BEFORE_LAPSE_SEC = 90
|
|
@@ -138,6 +140,30 @@ async def convert_unreadable(body: dict, label: str, to_text: Callable | None) -
|
|
|
138
140
|
CONFIDENTIAL_PROVIDERS = ("tinfoil",)
|
|
139
141
|
|
|
140
142
|
|
|
143
|
+
async def describe_images(body: dict, label: str, to_text: Callable) -> dict:
|
|
144
|
+
"""Replace every IMAGE part with the selected reader's description (= describeImages) — not confidentially, and said so."""
|
|
145
|
+
parts = [p for p in unreadable_parts(body) if p["kind"] == "image"]
|
|
146
|
+
if not parts:
|
|
147
|
+
return body
|
|
148
|
+
messages = [({**m, "content": list(m["content"])} if isinstance(m.get("content"), list) else m) for m in body["messages"]]
|
|
149
|
+
who = getattr(to_text, "label", "another model")
|
|
150
|
+
for p in parts:
|
|
151
|
+
mi, pi = p["at"]
|
|
152
|
+
if p.get("remote") or not p.get("b64"):
|
|
153
|
+
raise Refusal(400, f"{label} cannot read an image that is not attached inline.")
|
|
154
|
+
question = ("Describe this image for a coding assistant that cannot see it: its layout and every visible piece of text, code, numbers and UI "
|
|
155
|
+
f"elements, quoted verbatim. The person's message with it: \"{_text_of(body['messages'][mi])[:600]}\"")
|
|
156
|
+
try:
|
|
157
|
+
text = await to_text(p, question)
|
|
158
|
+
except Exception as e: # noqa: BLE001
|
|
159
|
+
said = getattr(e, "said", None)
|
|
160
|
+
if said:
|
|
161
|
+
raise Refusal(said[0], said[1], said[2]) from None
|
|
162
|
+
raise Refusal(502, f"{label}: {who} could not read an image ({str(e)[:140]}).", "image_reader_failed") from None
|
|
163
|
+
messages[mi]["content"][pi] = {"type": "text", "text": f"[An image was attached. This model cannot see images, so {who} read it — not confidentially; this is what it shows:]\n{text}"}
|
|
164
|
+
return {**body, "messages": messages}
|
|
165
|
+
|
|
166
|
+
|
|
141
167
|
def stamp_floor(req: dict) -> dict:
|
|
142
168
|
"""The confidential floor, as a confidential room stamps it. Key ORDER matters — the receipt signs these exact bytes."""
|
|
143
169
|
return {**req, "stream": True, "stream_options": {"include_usage": True}, "provider": {**(req.get("provider") or {}), "min_privacy": "confidential", "only": list(CONFIDENTIAL_PROVIDERS)}}
|
|
@@ -500,7 +526,9 @@ class ConfidentialProxy:
|
|
|
500
526
|
reader: Any = "default", log: Callable[[str], Any] = print, policies: set | None = None,
|
|
501
527
|
verify: Callable | None = None, sleep: Callable = asyncio.sleep, on_progress: Callable[[dict], Any] = lambda ev: None,
|
|
502
528
|
cache_marks: bool = True, credential_for: Callable[["_Request"], dict | None] | None = None,
|
|
503
|
-
offered: Callable[[str], bool] | None = None, on_key_refused: Callable[[], Any] = lambda: None
|
|
529
|
+
offered: Callable[[str], bool] | None = None, on_key_refused: Callable[[], Any] = lambda: None,
|
|
530
|
+
venice_key: Callable[[], str] = lambda: os.environ.get("VENICE_API_KEY", ""), image_reader: Callable[[], str] = lambda: "",
|
|
531
|
+
venice_client: Any = None, on_call: Callable[[dict], Any] = lambda c: None) -> None:
|
|
504
532
|
self.port, self.host, self.upstream = port, host, upstream
|
|
505
533
|
self.client = client or httpx.AsyncClient(trust_env=False, timeout=httpx.Timeout(None, connect=15.0))
|
|
506
534
|
self.models = models if models is not None else mdl.confidential_models()
|
|
@@ -519,6 +547,11 @@ class ConfidentialProxy:
|
|
|
519
547
|
# TrustedRouter refused an account's key (401): it was taken back — a removal in the app, a re-pair. serve asks the
|
|
520
548
|
# account at once instead of at its next check, hours later (the owner, 2026-10-08: "Invalid API key").
|
|
521
549
|
self._on_key_refused = on_key_refused
|
|
550
|
+
# VENICE (venice.py): the key when there is no credential_for (else the credential's `venice`); the selected image reader
|
|
551
|
+
# for text-only models that are NOT confidential ('' = off); each Venice call's record (its stated price).
|
|
552
|
+
self._venice_key, self._image_reader, self._on_call = venice_key, image_reader, on_call
|
|
553
|
+
self._venice_client = venice_client
|
|
554
|
+
self._readers: dict = {}
|
|
522
555
|
|
|
523
556
|
def _key_refused(self, status: int) -> None:
|
|
524
557
|
if status == 401 and self._credential_for is not None:
|
|
@@ -609,11 +642,21 @@ class ConfidentialProxy:
|
|
|
609
642
|
except ValueError:
|
|
610
643
|
body = None
|
|
611
644
|
tinfoil = None
|
|
645
|
+
vkey = self._venice_key() if self._credential_for is None else ""
|
|
612
646
|
if self._credential_for is not None:
|
|
613
647
|
cred = self._credential_for(req)
|
|
614
648
|
if not cred or not cred.get("trustedrouter"):
|
|
615
649
|
await self._fail(res, Refusal(403, (cred or {}).get("refuse") or OUTSIDE_APP, "outside_app"), False)
|
|
616
650
|
return
|
|
651
|
+
vkey = cred.get("venice") or ""
|
|
652
|
+
# ── a zero-retention model (Venice): its own upstream, its own key; never the confidential path ──
|
|
653
|
+
if req.target.startswith(vn.VENICE_PREFIX + "/"):
|
|
654
|
+
await self._venice(req, res, vkey)
|
|
655
|
+
return
|
|
656
|
+
# TrustedRouter not answering: the turn fails and names the way out — the person picks ("I mean the user selects the model")
|
|
657
|
+
self._way_out = f" You can switch to a {vn.ZDR} model (Venice) in the model menu." if vkey else ""
|
|
658
|
+
self._vkey = vkey
|
|
659
|
+
if self._credential_for is not None:
|
|
617
660
|
model = body.get("model") if isinstance(body, dict) else None
|
|
618
661
|
if isinstance(model, str) and model not in self.models and self._offered and not self._offered(model):
|
|
619
662
|
await self._fail(res, Refusal(403, f"{model} is not offered on Witbitz — pick one of the models in the Code section", "model_not_offered"), False)
|
|
@@ -628,6 +671,10 @@ class ConfidentialProxy:
|
|
|
628
671
|
self.log(f"code-confidential: the request was {rep['before'] // 1024} KB; {rep['dropped']} older screenshot(s) left out to send it")
|
|
629
672
|
conf = self.models.get(body.get("model")) if isinstance(body, dict) and isinstance(body.get("model"), str) else None
|
|
630
673
|
if not conf:
|
|
674
|
+
if isinstance(body, dict) and self._vkey:
|
|
675
|
+
body = await self._read_images_for_blind(req, res, body)
|
|
676
|
+
if body is None:
|
|
677
|
+
return
|
|
631
678
|
await self._pass_through(req, res, body, target)
|
|
632
679
|
return
|
|
633
680
|
await self._confidential(req, res, body, conf, target, tinfoil)
|
|
@@ -635,10 +682,19 @@ class ConfidentialProxy:
|
|
|
635
682
|
async def _pass_through(self, req: _Request, res: _Response, body: Any, target: str) -> None:
|
|
636
683
|
marked = with_cache_marks(body) if self.cache_marks else None
|
|
637
684
|
out = dumps(marked).encode() if marked else req.body
|
|
685
|
+
way_out = getattr(self, "_way_out", "")
|
|
638
686
|
try:
|
|
639
687
|
async with self.client.stream(req.method, target, headers=_forward_headers(req.headers), content=None if req.method in ("GET", "HEAD") else out) as up:
|
|
640
688
|
headers = {k: v for k, v in up.headers.items() if k.lower() not in ("content-encoding", "content-length", "transfer-encoding", "connection")}
|
|
641
689
|
self._key_refused(up.status_code)
|
|
690
|
+
if up.status_code >= 500 and way_out and isinstance(body, dict): # an outage, said with the way out — the person picks
|
|
691
|
+
text = (await up.aread()).decode("utf-8", "replace")
|
|
692
|
+
try:
|
|
693
|
+
why = str(json.loads(text)["error"]["message"])[:120]
|
|
694
|
+
except (ValueError, KeyError, TypeError):
|
|
695
|
+
why = ""
|
|
696
|
+
await self._fail(res, Refusal(up.status_code, f"TrustedRouter isn't answering right now ({up.status_code}{': ' + why if why else ''}).{way_out}", "upstream_unavailable"), False)
|
|
697
|
+
return
|
|
642
698
|
if up.status_code == 429: # the month's allowance, said plainly — never a "rate limit" OpenCode would retry
|
|
643
699
|
text = (await up.aread()).decode("utf-8", "replace")
|
|
644
700
|
used = allowance_used(up.status_code, text)
|
|
@@ -654,10 +710,102 @@ class ConfidentialProxy:
|
|
|
654
710
|
except ConnectionError:
|
|
655
711
|
return # OpenCode went away; the upstream call is dropped with the context
|
|
656
712
|
except httpx.HTTPError as e:
|
|
657
|
-
await self._fail(res, Refusal(502, f"could not reach TrustedRouter ({str(e)[:120]})"), False)
|
|
713
|
+
await self._fail(res, Refusal(502, f"could not reach TrustedRouter ({str(e)[:120]}).{way_out}", "upstream_unavailable"), False)
|
|
658
714
|
return
|
|
659
715
|
await res.end()
|
|
660
716
|
|
|
717
|
+
async def _read_images_for_blind(self, req: _Request, res: _Response, body: dict) -> dict | None:
|
|
718
|
+
"""A text-only model that is NOT confidential, and a Venice reader selected: each image becomes the reader's words. Decided by
|
|
719
|
+
the model's tier — a confidential model never comes here, so no picture of a confidential turn reaches Venice."""
|
|
720
|
+
rm = str(self._image_reader() or "")
|
|
721
|
+
blind = next((m for m in vn._DATA.get("blindRegular", []) if m["model"] == body.get("model")), None)
|
|
722
|
+
if not rm or not blind or not any(p["kind"] == "image" for p in unreadable_parts(body)):
|
|
723
|
+
return body
|
|
724
|
+
key = (self._vkey, rm)
|
|
725
|
+
if key not in self._readers:
|
|
726
|
+
self._readers[key] = vn.make_reader(self._venice_client or self.client, self._vkey, rm, on_cost=lambda usd: self._called({"model": "venice/" + rm, "venice": True, "reader": True, "confidential": False, "status": 200, "costUsd": usd}))
|
|
727
|
+
to_text = self._readers[key]
|
|
728
|
+
try:
|
|
729
|
+
out = await describe_images(body, blind["label"], to_text)
|
|
730
|
+
except Refusal as e:
|
|
731
|
+
await self._fail(res, e, False)
|
|
732
|
+
return None
|
|
733
|
+
req.body = dumps(out).encode("utf-8")
|
|
734
|
+
return out
|
|
735
|
+
|
|
736
|
+
def _called(self, fields: dict) -> None:
|
|
737
|
+
try:
|
|
738
|
+
self._on_call(fields)
|
|
739
|
+
except Exception: # noqa: BLE001 — the record must never fail a call
|
|
740
|
+
pass
|
|
741
|
+
|
|
742
|
+
async def _venice(self, req: _Request, res: _Response, key: str) -> None:
|
|
743
|
+
"""ONE call to Venice (= handleVenice)."""
|
|
744
|
+
path = req.target[len(vn.VENICE_PREFIX):]
|
|
745
|
+
if req.method == "GET" and re.match(r"^/v1/models(\?|$)", path):
|
|
746
|
+
await res.head(200, {"content-type": "application/json"})
|
|
747
|
+
await res.end(dumps({"object": "list", "data": [{"id": m["id"], "object": "model", "owned_by": "venice.ai"} for m in vn.VENICE_MODELS]}))
|
|
748
|
+
return
|
|
749
|
+
if req.method != "POST" or not re.match(r"^/v1/chat/completions(\?|$)", path):
|
|
750
|
+
await self._fail(res, Refusal(404, "only chat completions are offered on the Venice route", "not_offered"), False)
|
|
751
|
+
return
|
|
752
|
+
try:
|
|
753
|
+
body = json.loads(req.body.decode("utf-8"))
|
|
754
|
+
except ValueError:
|
|
755
|
+
await self._fail(res, Refusal(400, "the request is not JSON", "bad_request"), False)
|
|
756
|
+
return
|
|
757
|
+
m = vn.entry(body.get("model") if isinstance(body, dict) else None)
|
|
758
|
+
if not m:
|
|
759
|
+
await self._fail(res, Refusal(403, f"{str((body or {}).get('model'))[:80]} is not offered on Witbitz — pick one of the {vn.ZDR} models in the Code section", "model_not_offered"), False)
|
|
760
|
+
return
|
|
761
|
+
if not key:
|
|
762
|
+
await self._fail(res, Refusal(403, f"This computer has no key for the {vn.ZDR} models (Venice) yet. It gets one from your Spaces account at its next check — try again in a minute, or pair again: witbitz-code pair.", "no_credential"), False)
|
|
763
|
+
return
|
|
764
|
+
lbl = vn.label(m)
|
|
765
|
+
rec = {"model": "venice/" + m["id"], "venice": True, "confidential": False}
|
|
766
|
+
stream = body.get("stream") is True
|
|
767
|
+
client = self._venice_client or self.client
|
|
768
|
+
try:
|
|
769
|
+
async with client.stream("POST", vn.VENICE_BASE + "/chat/completions", headers={"authorization": "Bearer " + key, "content-type": "application/json", "accept": "text/event-stream" if stream else "application/json"}, content=dumps(vn.request(body, m)).encode("utf-8")) as up:
|
|
770
|
+
if up.status_code != 200:
|
|
771
|
+
text = (await up.aread()).decode("utf-8", "replace")
|
|
772
|
+
if up.status_code == 401:
|
|
773
|
+
self._key_refused(401)
|
|
774
|
+
said = vn.refusal_of(up.status_code, text, lbl)
|
|
775
|
+
self._called({**rec, "status": said[0] if said else up.status_code, **({"refused": said[2]} if said else {})})
|
|
776
|
+
if said:
|
|
777
|
+
await self._fail(res, Refusal(said[0], said[1], said[2]), False)
|
|
778
|
+
return
|
|
779
|
+
await res.head(up.status_code, {"content-type": up.headers.get("content-type", "application/json")})
|
|
780
|
+
await res.end(text)
|
|
781
|
+
return
|
|
782
|
+
if stream:
|
|
783
|
+
await res.head(200, {"content-type": "text/event-stream", "cache-control": "no-cache"})
|
|
784
|
+
clean = vn.SseCleaner()
|
|
785
|
+
async for c in up.aiter_bytes():
|
|
786
|
+
out = clean.feed(c)
|
|
787
|
+
if out:
|
|
788
|
+
await res.write(out)
|
|
789
|
+
rest = clean.done()
|
|
790
|
+
await res.end(rest)
|
|
791
|
+
self._called({**rec, "status": 200, "tokens": vn.usage_tokens(clean.usage), "costUsd": clean.cost_usd})
|
|
792
|
+
return
|
|
793
|
+
text = (await up.aread()).decode("utf-8", "replace")
|
|
794
|
+
except ConnectionError:
|
|
795
|
+
return
|
|
796
|
+
except httpx.HTTPError as e:
|
|
797
|
+
self._called({**rec, "status": 502})
|
|
798
|
+
await self._fail(res, Refusal(502, f"could not reach Venice ({str(e)[:120]})"), False)
|
|
799
|
+
return
|
|
800
|
+
try:
|
|
801
|
+
j = json.loads(text)
|
|
802
|
+
except ValueError:
|
|
803
|
+
await self._fail(res, Refusal(502, "Venice answered something that is not JSON"), False)
|
|
804
|
+
return
|
|
805
|
+
await res.head(200, {"content-type": "application/json"})
|
|
806
|
+
await res.end(dumps(vn.clean_event(j)))
|
|
807
|
+
self._called({**rec, "status": 200, "tokens": vn.usage_tokens(j.get("usage")), "costUsd": vn.cost_of(j)})
|
|
808
|
+
|
|
661
809
|
async def _confidential(self, req: _Request, res: _Response, body: dict, conf: dict, target: str, tinfoil: str | None = None) -> None:
|
|
662
810
|
label = f"{conf['label']} (confidential)"
|
|
663
811
|
wants_stream = body.get("stream") is True
|
|
@@ -78,7 +78,7 @@ def default_model_config() -> dict:
|
|
|
78
78
|
return {"model": f"trustedrouter/{DEFAULT_MODEL}"}
|
|
79
79
|
|
|
80
80
|
|
|
81
|
-
def proxy_config(proxy_port: int, cat: list[dict] | None = None) -> dict:
|
|
81
|
+
def proxy_config(proxy_port: int, cat: list[dict] | None = None, image_reader: str = "") -> dict:
|
|
82
82
|
"""OpenCode config that sends TrustedRouter through the proxy and declares the confidential models — a text-only one
|
|
83
83
|
still receives images (the proxy reads them through Tinfoil). Merged by OpenCode over the user's own config."""
|
|
84
84
|
models = {}
|
|
@@ -88,4 +88,12 @@ def proxy_config(proxy_port: int, cat: list[dict] | None = None) -> dict:
|
|
|
88
88
|
"modalities": {"input": ["text", "image"], "output": ["text"]},
|
|
89
89
|
"limit": {"context": 200_000, "output": m.get("maxTokens") or 16_000},
|
|
90
90
|
**({"reasoning": True, "variants": v} if v else {})}
|
|
91
|
-
|
|
91
|
+
# A selected image reader (venice.py): the text-only models that are NOT confidential take images — the proxy has it describe them
|
|
92
|
+
if image_reader:
|
|
93
|
+
from . import venice as vn
|
|
94
|
+
for m in vn._DATA.get("blindRegular", []):
|
|
95
|
+
v = variants_for(m)
|
|
96
|
+
models.setdefault(m["model"], {"name": m["label"], "attachment": True, "tool_call": True, "modalities": {"input": ["text", "image"], "output": ["text"]},
|
|
97
|
+
"limit": {"context": 200_000, "output": m.get("maxTokens") or 16_000}, **({"reasoning": True, "variants": v} if v else {})})
|
|
98
|
+
from . import venice as vn # + the zero-retention models (Venice), through the same proxy
|
|
99
|
+
return {"provider": {"trustedrouter": {"options": {"baseURL": f"http://127.0.0.1:{proxy_port}/v1"}, "models": models}, **vn.provider_config(proxy_port)}}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"""VENICE, CODE'S SECOND MODEL ROUTE — the Python twin of tools/code-venice.mjs (docs/code-venice-fallback.md).
|
|
2
|
+
|
|
3
|
+
OpenCode ──http──▶ this computer's proxy (confidential.py) ──/venice/…──▶ here ──https──▶ api.venice.ai
|
|
4
|
+
|
|
5
|
+
Venice's "private" tier is a POLICY — open models Venice hosts, zero data retention — not a proof: nothing here is ever labelled
|
|
6
|
+
confidential. Only the models in venice_models.json (written by tools/code-py-models.mjs from code-venice.mjs) are offered, and the
|
|
7
|
+
computer's key is minted "private models only" at Venice too. The person SELECTS a Venice model; nothing falls back to it by itself.
|
|
8
|
+
|
|
9
|
+
Four things a Venice call needs (measured 2026-11-20): Venice's own ~1,650-token system prompt switched off on every call; answers
|
|
10
|
+
passed on with OpenAI's fields only (Venice echoes the whole prompt back); each call recorded at the price the answer states; the
|
|
11
|
+
402 at a key's cap said as the allowance sentence."""
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import hashlib
|
|
15
|
+
import json
|
|
16
|
+
import os
|
|
17
|
+
import re
|
|
18
|
+
from importlib import resources
|
|
19
|
+
from typing import Any, Callable
|
|
20
|
+
|
|
21
|
+
VENICE_BASE = "https://api.venice.ai/api/v1"
|
|
22
|
+
VENICE_PREFIX = "/venice"
|
|
23
|
+
ZDR = "zero retention"
|
|
24
|
+
PLAN_URL = "https://getspaces.witbitz.chat/account"
|
|
25
|
+
_DATA = json.loads(resources.files(__package__).joinpath("venice_models.json").read_text("utf-8"))
|
|
26
|
+
VENICE_MODELS: list[dict] = _DATA["models"]
|
|
27
|
+
VENICE_READERS: list[str] = _DATA["readers"]
|
|
28
|
+
KEEP = {"id", "object", "created", "model", "choices", "usage", "system_fingerprint", "service_tier", "error"}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def entry(model: Any) -> dict | None:
|
|
32
|
+
return next((m for m in VENICE_MODELS if m["id"] == str(model or "")), None)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def label(m: dict) -> str:
|
|
36
|
+
return f"{m['label']} · {'chat only · ' if m.get('chatOnly') else ''}{ZDR}"
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def request(body: dict, m: dict) -> dict:
|
|
40
|
+
"""The request as Venice gets it — a NEW dict (= veniceRequest)."""
|
|
41
|
+
vp = body.get("venice_parameters") if isinstance(body.get("venice_parameters"), dict) else {}
|
|
42
|
+
out = {**body, "model": m["id"], "venice_parameters": {**vp, "include_venice_system_prompt": False}}
|
|
43
|
+
if out.get("stream") is True:
|
|
44
|
+
out["stream_options"] = {**(out.get("stream_options") or {}), "include_usage": True}
|
|
45
|
+
if m.get("chatOnly"):
|
|
46
|
+
for k in ("tools", "tool_choice", "parallel_tool_calls"):
|
|
47
|
+
out.pop(k, None)
|
|
48
|
+
return out
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def clean_event(ev: Any) -> Any:
|
|
52
|
+
return {k: v for k, v in ev.items() if k in KEEP} if isinstance(ev, dict) else ev
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def cost_of(ev: Any) -> float:
|
|
56
|
+
try:
|
|
57
|
+
c = float(((ev or {}).get("cost") or {}).get("usd"))
|
|
58
|
+
except (TypeError, ValueError, AttributeError):
|
|
59
|
+
return 0.0
|
|
60
|
+
return c if c > 0 else 0.0
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class SseCleaner:
|
|
64
|
+
"""A streamed answer, cleaned event by event, whatever the chunk boundaries (= sseCleaner)."""
|
|
65
|
+
|
|
66
|
+
def __init__(self) -> None:
|
|
67
|
+
self._buf = ""
|
|
68
|
+
self.cost_usd = 0.0
|
|
69
|
+
self.usage: dict | None = None
|
|
70
|
+
|
|
71
|
+
def _line(self, line: str) -> str:
|
|
72
|
+
if not line.startswith("data:"):
|
|
73
|
+
return line
|
|
74
|
+
d = line[5:].strip()
|
|
75
|
+
if not d or d == "[DONE]":
|
|
76
|
+
return line
|
|
77
|
+
try:
|
|
78
|
+
ev = json.loads(d)
|
|
79
|
+
except ValueError:
|
|
80
|
+
return line
|
|
81
|
+
self.cost_usd += cost_of(ev)
|
|
82
|
+
if isinstance(ev, dict) and ev.get("usage"):
|
|
83
|
+
self.usage = ev["usage"]
|
|
84
|
+
return "data: " + json.dumps(clean_event(ev), separators=(",", ":"), ensure_ascii=False)
|
|
85
|
+
|
|
86
|
+
def feed(self, chunk: bytes | str) -> str:
|
|
87
|
+
self._buf += chunk.decode("utf-8", "replace") if isinstance(chunk, bytes) else chunk
|
|
88
|
+
at = self._buf.rfind("\n")
|
|
89
|
+
if at < 0:
|
|
90
|
+
return ""
|
|
91
|
+
whole, self._buf = self._buf[: at + 1], self._buf[at + 1:]
|
|
92
|
+
return "\n".join(self._line(x) for x in whole.split("\n"))
|
|
93
|
+
|
|
94
|
+
def done(self) -> str:
|
|
95
|
+
rest, self._buf = self._buf, ""
|
|
96
|
+
return self._line(rest) if rest else ""
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def refusal_of(status: int, text: str, lbl: str = "This model") -> tuple[int, str, str] | None:
|
|
100
|
+
"""Venice's refusals as sentences (= veniceRefusal): (status, message, kind) or None."""
|
|
101
|
+
t = str(text or "")
|
|
102
|
+
if status == 402:
|
|
103
|
+
if re.search(r"spend limit", t, re.I):
|
|
104
|
+
return 402, f"This computer's Code allowance for {ZDR} models (Venice) is used up for the month — it comes back on the 1st. Your plan and use: {PLAN_URL}", "allowance_used"
|
|
105
|
+
return 402, f"Venice refused the call for lack of credit ({t[:120]}).", "allowance_used"
|
|
106
|
+
if status == 401:
|
|
107
|
+
return 401, "Venice did not accept this computer's key. If the computer was removed from your account, pair it again: witbitz-code pair.", "key_refused"
|
|
108
|
+
if status == 403 and re.search(r"Private models only", t, re.I):
|
|
109
|
+
return 403, f"{lbl} is not one of the {ZDR} models offered on Witbitz.", "model_not_offered"
|
|
110
|
+
return None
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def usage_tokens(u: Any) -> dict | None:
|
|
114
|
+
if not isinstance(u, dict):
|
|
115
|
+
return None
|
|
116
|
+
det = u.get("prompt_tokens_details") or {}
|
|
117
|
+
return {"in": int(u.get("prompt_tokens") or 0), "out": int(u.get("completion_tokens") or 0), "cached": int(det.get("cached_tokens") or 0)}
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
# ── the image reader: a Venice model that sees, for a text-only model that is NOT confidential ─────────────────────────────────
|
|
121
|
+
def reader_setting(path: str) -> Callable[[], str]:
|
|
122
|
+
"""`<dir>/image-reader.json` → the selected reader ('' = off), read again when the file changes (= imageReaderSetting)."""
|
|
123
|
+
seen: dict = {"k": None, "v": ""}
|
|
124
|
+
|
|
125
|
+
def read() -> str:
|
|
126
|
+
try:
|
|
127
|
+
st = os.stat(path)
|
|
128
|
+
except OSError:
|
|
129
|
+
seen.update(k=None, v="")
|
|
130
|
+
return ""
|
|
131
|
+
k = f"{st.st_mtime_ns}:{st.st_size}"
|
|
132
|
+
if k != seen["k"]:
|
|
133
|
+
seen["k"] = k
|
|
134
|
+
try:
|
|
135
|
+
m = str(json.loads(open(path, encoding="utf-8").read()).get("model") or "")
|
|
136
|
+
seen["v"] = m if m in VENICE_READERS else ""
|
|
137
|
+
except (OSError, ValueError, AttributeError):
|
|
138
|
+
seen["v"] = ""
|
|
139
|
+
return seen["v"]
|
|
140
|
+
return read
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def set_reader(path: str, model: str) -> str:
|
|
144
|
+
if not model or model == "off":
|
|
145
|
+
try:
|
|
146
|
+
os.unlink(path)
|
|
147
|
+
except FileNotFoundError:
|
|
148
|
+
pass
|
|
149
|
+
return ""
|
|
150
|
+
if model not in VENICE_READERS:
|
|
151
|
+
raise ValueError(f"{model} cannot read images for other models — one of: {', '.join(VENICE_READERS)}")
|
|
152
|
+
os.makedirs(os.path.dirname(path), mode=0o700, exist_ok=True)
|
|
153
|
+
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
|
|
154
|
+
with os.fdopen(fd, "w", encoding="utf-8") as f:
|
|
155
|
+
f.write(json.dumps({"model": model}) + "\n")
|
|
156
|
+
return model
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def make_reader(client: Any, api_key: str, model: str, on_cost: Callable[[float], Any] = lambda usd: None, cache_max: int = 64) -> Callable:
|
|
160
|
+
"""An async to_text(item, question) reading an IMAGE with a Venice model (= makeVeniceReader), cached by content."""
|
|
161
|
+
m = entry(model)
|
|
162
|
+
if not m or not m.get("vision") or m["id"] not in VENICE_READERS:
|
|
163
|
+
raise ValueError(f"{model} cannot be the image reader")
|
|
164
|
+
cache: dict[str, str] = {}
|
|
165
|
+
|
|
166
|
+
async def to_text(item: dict, question: str) -> str:
|
|
167
|
+
if item.get("kind") != "image":
|
|
168
|
+
raise ValueError("only images can be read this way")
|
|
169
|
+
k = hashlib.sha256((m["id"] + "\0" + item.get("b64", "") + "\0" + question).encode()).hexdigest()
|
|
170
|
+
if k in cache:
|
|
171
|
+
return cache[k]
|
|
172
|
+
body = {"model": m["id"], "max_tokens": 1500, "venice_parameters": {"include_venice_system_prompt": False},
|
|
173
|
+
"messages": [{"role": "user", "content": [{"type": "text", "text": question}, {"type": "image_url", "image_url": {"url": f"data:{item.get('mime') or 'image/png'};base64,{item.get('b64', '')}"}}]}]}
|
|
174
|
+
r = await client.post(VENICE_BASE + "/chat/completions", headers={"authorization": "Bearer " + api_key, "content-type": "application/json"}, content=json.dumps(body).encode())
|
|
175
|
+
text = r.text
|
|
176
|
+
if r.status_code != 200:
|
|
177
|
+
e = RuntimeError(f"Venice answered {r.status_code}: {text[:160]}")
|
|
178
|
+
e.said = refusal_of(r.status_code, text, m["label"]) # type: ignore[attr-defined]
|
|
179
|
+
raise e
|
|
180
|
+
j = json.loads(text)
|
|
181
|
+
said = str((((j.get("choices") or [{}])[0].get("message") or {}).get("content")) or "").strip()
|
|
182
|
+
if not said:
|
|
183
|
+
raise RuntimeError("the reader answered nothing")
|
|
184
|
+
try:
|
|
185
|
+
on_cost(cost_of(j))
|
|
186
|
+
except Exception: # noqa: BLE001
|
|
187
|
+
pass
|
|
188
|
+
cache[k] = said
|
|
189
|
+
if len(cache) > cache_max:
|
|
190
|
+
cache.pop(next(iter(cache)))
|
|
191
|
+
return said
|
|
192
|
+
to_text.label = f"{m['label']} ({ZDR}, Venice)" # type: ignore[attr-defined]
|
|
193
|
+
to_text.model = m["id"] # type: ignore[attr-defined]
|
|
194
|
+
return to_text
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def provider_config(proxy_port: int) -> dict:
|
|
198
|
+
"""OpenCode's `venice` provider through the proxy (= veniceProviderConfig)."""
|
|
199
|
+
models = {}
|
|
200
|
+
for m in VENICE_MODELS:
|
|
201
|
+
inputs = ["text", "image"] if m.get("vision") else ["text"]
|
|
202
|
+
models[m["id"]] = {"name": label(m), "attachment": bool(m.get("vision")), "tool_call": not m.get("chatOnly"), "reasoning": False,
|
|
203
|
+
"modalities": {"input": inputs, "output": ["text"]}, "limit": {"context": 128_000, "output": 16_000}}
|
|
204
|
+
return {"venice": {"name": f"Venice · {ZDR}", "npm": "@ai-sdk/openai-compatible", "options": {"baseURL": f"http://127.0.0.1:{proxy_port}{VENICE_PREFIX}/v1", "apiKey": "held-by-the-proxy"}, "models": models}}
|