diff --git a/open_terminal/agents/acp.py b/open_terminal/agents/acp.py index 6157bb5..5b2bc00 100644 --- a/open_terminal/agents/acp.py +++ b/open_terminal/agents/acp.py @@ -3,17 +3,27 @@ async def discover(send, response, cwd, auth_method=None): sequence = 0 + config_options = [] + + def notification(message): + nonlocal config_options + if message.get("method") == "session/update": + update = message.get("params", {}).get("update", {}) + if update.get("sessionUpdate") == "config_option_update": + config_options = update.get("configOptions", []) async def request(method, params): - nonlocal sequence + nonlocal sequence, config_options sequence += 1 await send( {"jsonrpc": "2.0", "id": sequence, "method": method, "params": params} ) - reply = await response("id", sequence) + reply = await response("id", sequence, on_notification=notification) if "error" in reply: raise RuntimeError(reply["error"].get("message", "Agent request failed")) - return reply.get("result", {}) + result = reply.get("result", {}) + config_options = result.get("configOptions", config_options) + return result initialized = await request( "initialize", @@ -35,7 +45,7 @@ async def discover(send, response, cwd, auth_method=None): option = next( ( c - for c in session.get("configOptions", []) + for c in config_options if c.get("category") == "model" or c.get("id") == "model" ), None, @@ -47,14 +57,62 @@ async def discover(send, response, cwd, auth_method=None): for group in values for v in (group["options"] if "group" in group else [group]) ] - return [ + models = [ {"id": v["value"], "name": v.get("name") or v["value"]} for v in values ] - # Some current agents still advertise the native ACP models field. - return [ - {"id": m["modelId"], "name": m.get("name") or m["modelId"]} - for m in session.get("models", {}).get("availableModels", []) - ] + else: + # Some current agents still advertise the native ACP models field. + models = [ + {"id": m["modelId"], "name": m.get("name") or m["modelId"]} + for m in session.get("models", {}).get("availableModels", []) + ] + current_model = (option or {}).get("currentValue") or session.get( + "models", {} + ).get("currentModelId") + for model in models: + model["supportedReasoningEfforts"] = [] + # Thinking choices can change when the model changes. + if model["id"] != current_model: + try: + if option: + await request( + "session/set_config_option", + { + "sessionId": session["sessionId"], + "configId": option["id"], + "value": model["id"], + }, + ) + else: + await request( + "session/set_model", + { + "sessionId": session["sessionId"], + "modelId": model["id"], + }, + ) + except RuntimeError: + # An unavailable model's optional controls must not hide the catalog. + continue + current_model = model["id"] + thought = next( + ( + c + for c in config_options + if c.get("category") == "thought_level" + and c.get("type") == "select" + ), + {}, + ) + model["supportedReasoningEfforts"] = [ + { + "reasoningEffort": value["value"], + "label": value.get("name") or value["value"], + } + for group in thought.get("options", []) + for value in (group["options"] if "group" in group else [group]) + ] + return models finally: capabilities = initialized.get("agentCapabilities", {}).get( "sessionCapabilities", {} diff --git a/open_terminal/agents/claude_code.py b/open_terminal/agents/claude_code.py index 508c204..d0149f4 100644 --- a/open_terminal/agents/claude_code.py +++ b/open_terminal/agents/claude_code.py @@ -39,6 +39,10 @@ async def discover(send, response, cwd=None): { "id": m.get("resolvedModel") or m["value"], "name": m.get("displayName") or m["value"], + "supportedReasoningEfforts": [ + {"reasoningEffort": level} + for level in (m.get("supportedEffortLevels") or []) + ], } for m in result.get("response", {}).get("models", []) ] diff --git a/open_terminal/agents/discovery.py b/open_terminal/agents/discovery.py index 6b107c7..4007b3b 100644 --- a/open_terminal/agents/discovery.py +++ b/open_terminal/agents/discovery.py @@ -25,7 +25,7 @@ async def probe(provider, fs, cwd=None): process.stdin.write((json.dumps(message) + "\n").encode()) await process.stdin.drain() - async def response(key, value): + async def response(key, value, *, on_notification=None): while line := await process.stdout.readline(): message = json.loads(line) if "method" in message and "id" in message: @@ -42,6 +42,8 @@ async def probe(provider, fs, cwd=None): continue if message.get(key) == value: return message + if on_notification: + on_notification(message) raise RuntimeError("Agent exited during discovery") async with asyncio.timeout(20): diff --git a/open_terminal/agents/pi.py b/open_terminal/agents/pi.py index 3050660..576a00d 100644 --- a/open_terminal/agents/pi.py +++ b/open_terminal/agents/pi.py @@ -22,6 +22,25 @@ async def discover(send, response, cwd): if not reply.get("success"): raise RuntimeError(reply.get("error", "Pi model discovery failed")) return [ - {"id": f"{m['provider']}/{m['id']}", "name": m.get("name") or m["id"]} + { + "id": f"{m['provider']}/{m['id']}", + "name": m.get("name") or m["id"], + "supportedReasoningEfforts": [ + {"reasoningEffort": level} for level in thinking_levels(m) + ], + } for m in reply.get("data", {}).get("models", []) ] + + +def thinking_levels(model): + if not model.get("reasoning"): + return [] + # Match Pi's getSupportedThinkingLevels without changing the user's model/settings. + mapping = model.get("thinkingLevelMap") or {} + return [ + level + for level in ("off", "minimal", "low", "medium", "high", "xhigh", "max") + if mapping.get(level, True) is not None + and (level not in {"xhigh", "max"} or level in mapping) + ]