import json, subprocess, sys
from urllib.parse import quote
home, thread_file, session = sys.argv[1:]
# The public environment identity is read from the real server.
from urllib.request import urlopen
origin = json.load(open(home + '/userdata/server-runtime.json'))['origin']
environment = json.load(urlopen(origin + '/.well-known/t3/environment'))['environmentId']
thread = json.load(open(thread_file))['threadId']
link = 't3code-dev://threads/' + quote(environment, safe='') + '/' + quote(thread, safe='')
subprocess.run(['agent-device', 'open', 'com.t3tools.t3code.dev', link, '--session', session], check=True)
