# openTARS — nucleo/elementos.py # Clicar e escrever pelo nome: acessibilidade e, nos apps que não a oferecem, a visão da tela (o Gargantua 2). # # Parte do núcleo: o tars.py executa este arquivo DENTRO do próprio # namespace (ver _carregar_nucleo), então tudo aqui é tars. e as # partes enxergam umas às outras sem import. Os testes que trocam # tars. por uma falsa continuam valendo pra todas as partes. # flake8: noqa # ============================================================ # ELEMENTOS DA JANELA (árvore de acessibilidade) # ============================================================ # # Clicar num botão pelo NOME ("7", "=", "Salvar") em vez de print + # coordenada: ver tars_acessibilidade.py. O print continua como plano B # pra app que não expõe a árvore. _acessibilidade_ligada = {"feito": False} AVISO_SEM_ACESSIBILIDADE = ( "The accessibility interface is not available ({motivo}). Use take_screenshot " "with window= and click by coordinates. (To enable it: sudo apt install " "gir1.2-atspi-2.0 python3-gi at-spi2-core)" ) def _dica_clique_por_nome(): if acess.indisponivel(): return "" return " To press its buttons, use click_element with the text shown on each button." def _janela_x_do_pid(pid): if not pid: return None return next((j for j in listar_janelas_x() or [] if j["pid"] == pid), None) TIMEOUT_JANELA_ACESSIVEL_SEG = 5 _abertos_sozinhos = {} # app normalizado -> quando o openTARS tentou abrir def _pode_abrir_sozinho(nome): """Abre no máximo uma vez a cada 30 s por app (uma fila de cliques não abre cinco calculadoras), nunca o próprio openTARS, e só se o nome for de um app instalado.""" chave = normalizar(nome) if not chave or "tars" in chave: return False agora = time.time() if agora - _abertos_sozinhos.get(chave, 0) < 30: return False _abertos_sozinhos[chave] = agora return bool(encontrar_app(nome)) def _nota_aberto(alvo): return " (it was not open, so openTARS opened it first)" if alvo.get("aberto_agora") else "" # Apps abertos que não expõem os botões (Electron/Chromium: Claude, # Discord, VS Code, Slack...). Depois da primeira falha, click_element e # list_elements respondem na hora com o caminho certo, em vez de a IA # tentar nome por nome (log real: 14 cliques falhos seguidos no Claude). _SEM_ARVORE = {} # nome normalizado -> quando descobrimos VALIDADE_SEM_ARVORE_SEG = 600 def _capturar_janela(nome): """(imagem, ox, oy, janela_x) da janela pedida, já na frente, ou None.""" janela_x = encontrar_janela_por_nome(nome) if nome else None if not janela_x: return None try: if janela_x["id"] != janela_ativa_x(): focar_janela_x(janela_x["id"]) time.sleep(0.3) janela_x = next((j for j in listar_janelas_x() or [] if j["id"] == janela_x["id"]), janela_x) except Exception: pass captura = _capturar_tela() x0, y0 = max(0, janela_x["x"]), max(0, janela_x["y"]) x1 = min(captura.width, janela_x["x"] + janela_x["largura"]) y1 = min(captura.height, janela_x["y"] + janela_x["altura"]) if x1 - x0 < 10 or y1 - y0 < 10: return None return captura.crop((x0, y0, x1, y1)), x0, y0, janela_x # Clique pela visão (ícone sem texto): TARS_CLIQUE_POR_VISAO=0 desliga a visão nos cliques. CLIQUE_POR_VISAO = os.environ.get("TARS_CLIQUE_POR_VISAO", "1") not in ("0", "off", "nao", "no") def clicar_pela_visao(nome, janela): """Plano B do click_element: o modelo de visão olha a janela e aponta onde está 'nome' (botão, campo ou ícone); o openTARS clica ali. Funciona em app que não expõe seus botões (Electron...). None se não deu pra olhar (sem modelo de visão, sem janela).""" if not CLIQUE_POR_VISAO or ERRO_PYAUTOGUI or not janela: return None capturada = _capturar_janela(janela) if not capturada: return None imagem, ox, oy, janela_x = capturada ponto, _tag = localizar_com_visao(nome, imagem) if ponto == "ausente": return {"sucesso": False, "janela": janela_x["titulo"], "mensagem": f"Looked at '{janela_x['titulo']}' and '{nome}' is not there."} if not isinstance(ponto, tuple): _avisar_falha_da_visao() return None try: pyautogui.click(x=ox + ponto[0], y=oy + ponto[1]) except Exception as e: return {"sucesso": False, "mensagem": f"Located '{nome}' but the click failed: {e}"} time.sleep(0.35) return {"sucesso": True, "janela": janela_x["titulo"], "elemento": nome, "mensagem": ( f"SUCCESS: clicked where '{nome}' was located in '{janela_x['titulo']}' (x={ponto[0]}, y={ponto[1]} in " "the window). It was found by LOOKING at the screen, so it can be a little off: check the result " "(take_screenshot) before going on.")} def _mensagem_sem_arvore(nome): return ( f"'{nome}' is open, but it does not expose its buttons to the accessibility interface " "(common in Electron/Chromium apps such as Claude, Discord, VS Code, Slack). You CAN " "still use it. To write and send a message (its text box usually already has the " f"keyboard focus): type_in_element(text=, window='{nome}', submit=true). " f"To click something else: take_screenshot(window='{nome}') and click_mouse with x/y of " "that image plus target=''. Do NOT use click_element or list_elements on it again, and don't ask the " "user to do it: do it yourself with those tools." ) def _digitar_na_janela(janela, texto, enviar=False): """Traz a janela pra frente e digita no campo que tem o foco (num app de chat, a caixa de mensagem). enviar=True aperta Enter no fim.""" focada = focar_janela(janela) if not focada.get("sucesso"): return focada time.sleep(0.25) # o app redesenha e devolve o foco à caixa de texto resultado = digitar_texto(texto or "") if not resultado.get("sucesso"): return resultado if enviar: tecla = pressionar_tecla("enter") if not tecla.get("sucesso"): return tecla titulo = (focada.get("janela") or {}).get("titulo") or janela return {"sucesso": True, "janela": titulo, "mensagem": ( f"SUCCESS: typed the text into the focused box of '{titulo}'" + (" and pressed Enter to send it." if enviar else ". Use press_key('enter') to send it.") + " If it may have gone to the wrong place, check with take_screenshot.")} def _marcar_sem_arvore(nome): if nome: _SEM_ARVORE[normalizar(nome)] = time.time() def _sem_arvore(nome): quando = _SEM_ARVORE.get(normalizar(nome or "")) return quando is not None and time.time() - quando < VALIDADE_SEM_ARVORE_SEG def _janela_acessivel(nome): """A janela (na árvore de acessibilidade) que a IA pediu, ou a ativa. Devolve (janela, erro).""" motivo = acess.indisponivel() if motivo: aviso = (plataforma.dica_instalar(AVISO_SEM_ACESSIBILIDADE) if plataforma.sistema() == "linux" else AVISO_SEM_ACESSIBILIDADE.split(" (To enable")[0]) return None, aviso.format(motivo=motivo) if nome and _sem_arvore(nome): return None, _mensagem_sem_arvore(nome) def procurar(): janela_x, termos = None, () if nome: termos = _termos_para_janela(nome) janela_x = encontrar_janela_por_nome(nome) # Só o título batendo (uma aba "Calculadora online" no # navegador) não basta pra dizer que é o app pedido. if janela_x and _pontuar_janela(janela_x, termos) < 2: janela_x = None else: ativa = janela_ativa_x() if ativa: janela_x = next((j for j in listar_janelas_x() or [] if j["id"] == ativa), None) return acess.achar_janela( pid=janela_x["pid"] if janela_x else None, titulo=janela_x["titulo"] if janela_x else None, termos=termos, ativa=not nome, ignorar_pids=_pids_protegidos(), ) alvo = procurar() # Apps Qt/KDE (e alguns outros) só expõem a árvore com a # acessibilidade da sessão ligada: liga uma vez e procura de novo. if alvo is None and not _acessibilidade_ligada["feito"]: _acessibilidade_ligada["feito"] = True if acess.ativar(): time.sleep(0.6) alvo = procurar() # A IA pulou o open_application ("clique no 1 da calculadora" com a # calculadora fechada): abre o app e espera a janela aparecer na # árvore, uma vez por app. Antes os cinco cliques falhavam em fila. aberta_no_x = bool(nome) and alvo is None and encontrar_janela_por_nome(nome) is not None if alvo is None and nome and not aberta_no_x and _pode_abrir_sozinho(nome): aberto = abrir_aplicativo(nome) if aberto.get("sucesso"): limite = time.time() + TIMEOUT_JANELA_ACESSIVEL_SEG while alvo is None and time.time() < limite: time.sleep(0.25) alvo = procurar() if alvo is not None: alvo = dict(alvo, aberto_agora=True) # 11/11 (F11): app Electron já aberto sem árvore: com a permissão da pessoa, reabre com a acessibilidade ligada e procura de novo if alvo is None and nome and i18n.ler_config().get("reabrir_com_acessibilidade") and not _sem_arvore(nome): janela_x = encontrar_janela_por_nome(nome) if janela_x and janela_x.get("pid"): ok, _ = reabrir_com_acessibilidade(janela_x["pid"]) limite = time.time() + (TIMEOUT_JANELA_ACESSIVEL_SEG if ok else 0) while ok and alvo is None and time.time() < limite: time.sleep(0.5) alvo = procurar() if alvo is None: if nome and (aberta_no_x or encontrar_janela_por_nome(nome) is not None): _marcar_sem_arvore(nome) return None, _mensagem_sem_arvore(nome) if nome: return None, ( f"'{nome}' is not open, or does not expose its elements to the accessibility " "interface. If it is open, use take_screenshot with window= and click by coordinates." ) return None, ( "No accessible window is in focus. Pass window=, or use " "take_screenshot and click by coordinates." ) return alvo, None def listar_elementos(janela=None, filtro=None): """Handler da ferramenta list_elements.""" alvo, erro = _janela_acessivel(janela) if erro: return {"sucesso": False, "mensagem": erro} r = acess.listar(alvo, filtro) if not r["total"] and not r["textos"] and not filtro and janela: _marcar_sem_arvore(janela) return {"sucesso": False, "janela": alvo["titulo"], "mensagem": _mensagem_sem_arvore(janela)} if not r["total"] and not r["textos"]: return { "sucesso": False, "janela": alvo["titulo"], "mensagem": ( f"'{alvo['titulo']}' exposes no clickable elements" + (f" matching '{filtro}'" if filtro else "") + ". Use take_screenshot with window= and click by coordinates." ), } nota_lista = "" if not alvo["titulo"] and list(r["elementos"]) == ["menu item"]: # lista suspensa aberta (TARS-World B4: o modelo listava de novo e desistia) nota_lista = " This is an open drop-down list: pick the option now with click_element(name=