Skip to content

Make KnowledgeInteractionContext.dispatch() async with dual handler support #30

@DaviddeBest-TNO

Description

@DaviddeBest-TNO

Parent

#27

What to build

Make the handler dispatch path async and support both sync and async handler functions.

Dispatch: KnowledgeInteractionContext.dispatch() becomes async def. It awaits the now-async resolve_dependencies() call. For the handler invocation itself, detect whether the handler is a coroutine function via asyncio.iscoroutinefunction(): async handlers are awaited directly; sync handlers are run via asyncio.to_thread() so they do not block the event loop.

Handler type: Update the Handler type alias to accept both sync and async callables. Both should pass type checking.

Decorator wrapper: The wrapper function created inside _register_ki_decorator must detect and preserve the async-ness of the original handler. If the user decorates an async def, the wrapper should also be async def (or at minimum, the dispatch path must correctly detect the original function as async).

No changes to decorators themselves: @kb.answer_ki() and @kb.react_ki() remain synchronous — they only store the handler in the registry.

Acceptance criteria

  • KnowledgeInteractionContext.dispatch() is async def
  • Async handlers (async def) are awaited directly during dispatch
  • Sync handlers (def) are run via asyncio.to_thread() during dispatch
  • Handler type alias accepts both sync and async callables
  • Decorator wrapper preserves async-ness of the original handler function
  • Decorators (answer_ki, react_ki) remain synchronous (registration only)
  • All handler and dispatch tests updated to async and passing
  • uv run ruff check . passes

Blocked by

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestready-for-agentIssue is ready for agent implementation

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions