/btw --explore (#158593) and its Desktop side chat (#158643) belong in a third-party plugin, but the plugin API could not host them: a fork lease came only from post_llm_call, no core tool could ever be named for a plugin fork, and a running fork reported nothing until it settled. Three generic extension points close that, reusing the existing ctx.fork_session machinery (cache-parity fork, dispatch whitelist, auto-denied approvals, next-turn cancellation): - Slash-command handlers that name a `side_question_token` keyword get a lease on the calling session's live agent (classic CLI, gateway-owned local sessions and messaging, TUI sidecar). Same lease rules as the turn token; it does not replace that token. Only a handler that names it pays for the capture. - `plugins.entries.<id>.fork_tools` lets the user grant ONE plugin's forks named core tools (read_file, search_files, terminal, ...). Pre-registry names (delegate_task, memory, connectors, Tool Search bridges) stay refused; FORK_SAFE_CORE_TOOLS stays empty. Forks now bind the non-interactive secret capture so a granted skill tool can never pop the turn's secret prompt. - `ctx.fork_session(..., on_tool=cb)` hears each allowed tool call as it starts (display-redacted args), for live progress via post_message / edit or emit_to_session. Desktop needs no SDK change: a plugin docks its side chat as a `panes` contribution with placement right and sends its slash command for follow-ups.
42 lines
2.2 KiB
Python
42 lines
2.2 KiB
Python
"""Compatibility-aware call boundary for plugin slash-command handlers."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import inspect
|
|
from typing import Any, Callable
|
|
|
|
_KEYWORD_KINDS = (inspect.Parameter.POSITIONAL_OR_KEYWORD, inspect.Parameter.KEYWORD_ONLY)
|
|
# Named explicitly, never through ``**kwargs``: issuing it captures the whole conversation.
|
|
SIDE_QUESTION_TOKEN = "side_question_token"
|
|
|
|
|
|
def invoke_plugin_command(handler: Callable, raw_args: str, *, session_agent: Any = None, **context: Any) -> Any:
|
|
"""Call *handler* once with *raw_args* plus the *context* keywords it declares.
|
|
|
|
Every surface passes the same context (``source``, ``session_id``, ``session_key``,
|
|
``platform``); a handler opts in by naming any of them as keyword-capable parameters, or
|
|
takes them all with ``**kwargs``. Legacy ``fn(raw_args)`` handlers and opaque signatures keep
|
|
the one-argument call. Handler exceptions propagate; they are never retried as a legacy call.
|
|
|
|
A handler that names ``side_question_token`` gets a lease on *session_agent* (the session's
|
|
live agent, or None) for ``ctx.side_question`` / ``ctx.fork_session``
|
|
(:func:`agent.side_question_lease.issue_command_token`), or None when there is nothing to lease.
|
|
"""
|
|
try:
|
|
parameters = list(inspect.signature(handler).parameters.values())
|
|
except (TypeError, ValueError):
|
|
return handler(raw_args)
|
|
# The first positional parameter receives raw_args whatever its name (``def h(source, **kw)``
|
|
# must not get ``source`` twice).
|
|
if parameters and parameters[0].kind is not inspect.Parameter.KEYWORD_ONLY:
|
|
if parameters[0].kind is inspect.Parameter.POSITIONAL_OR_KEYWORD:
|
|
context = {k: v for k, v in context.items() if k != parameters[0].name}
|
|
parameters = parameters[1:]
|
|
accepted = {p.name for p in parameters if p.kind in _KEYWORD_KINDS}
|
|
if SIDE_QUESTION_TOKEN in accepted:
|
|
from agent.side_question_lease import issue_command_token
|
|
context[SIDE_QUESTION_TOKEN] = None if session_agent is None else issue_command_token(session_agent)
|
|
if any(p.kind is inspect.Parameter.VAR_KEYWORD for p in parameters):
|
|
return handler(raw_args, **context)
|
|
return handler(raw_args, **{k: v for k, v in context.items() if k in accepted})
|