Skip to content

feat: expose current session to model plugins#313

Merged
WH-2099 merged 1 commit into
langgenius:mainfrom
ryuta-kobayashi-ug:feat/pass-session-to-model-plugins
Jul 6, 2026
Merged

feat: expose current session to model plugins#313
WH-2099 merged 1 commit into
langgenius:mainfrom
ryuta-kobayashi-ug:feat/pass-session-to-model-plugins

Conversation

@ryuta-kobayashi-ug

@ryuta-kobayashi-ug ryuta-kobayashi-ug commented May 6, 2026

Copy link
Copy Markdown
Contributor

Fixes #311

Follow-up to langgenius/dify#35772.

Summary

Expose the current SDK Session to model plugins through get_current_session().
This lets provider plugins read session.app_id for provider-side request metadata and cost attribution.

Current host-side design

The earlier plan changed substantially.
The host side now passes app identity explicitly before the request reaches this SDK:

That leaves one SDK problem: model plugin implementations do not receive Session.
Tool plugins receive it in their constructor, but model plugin _invoke() signatures are implemented by existing third-party plugins.
Adding a new _invoke() parameter would break those plugins.
AIModel.__init__() is final, so constructor injection is not the right extension point either.

Changes

  • Add dify_plugin.core.session_context.get_current_session().
  • Export get_current_session from the top-level dify_plugin package.
  • Bind the request Session around model invocation paths in PluginExecutor.
  • Wrap LLM streaming generators so the session remains available until the stream is consumed.
  • Add focused tests for the public API, synchronous model invocation, and streaming LLM invocation.

Usage

from dify_plugin import get_current_session

session = get_current_session()
if session and session.app_id:
    # attach session.app_id to provider request metadata
    ...

session.app_id is None for model calls outside app execution, such as routing, title generation, suggested questions, or other system use.
Provider plugins should skip tagging or bucket those calls as internal/system traffic.

Validation

  • just check
  • just test

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements request-scoped session context propagation for model plugins using Python's contextvars. It introduces a get_current_session utility and updates the PluginExecutor to manage session state across various model invocation types. A critical issue was identified in the invoke_llm implementation where managing the ContextVar outside of the generator's execution flow could lead to context leaks or ValueError exceptions. The reviewer suggests moving the context management logic entirely inside the generator wrapper to ensure proper cleanup.

Comment thread src/dify_plugin/core/plugin_executor.py Outdated
@WH-2099 WH-2099 force-pushed the feat/pass-session-to-model-plugins branch from 64407d2 to d7cd56d Compare July 6, 2026 19:17
@WH-2099 WH-2099 merged commit 1ef5b02 into langgenius:main Jul 6, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expose current session to model plugins for app_id attribution

2 participants