-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Introduce ManagedAgent and AgentRunner implementations #110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jsonbailey
wants to merge
6
commits into
main
Choose a base branch
from
jb/aic-1664/managed-agent
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0e0c30f
feat: Introduce ManagedAgent and AgentRunner implementations
jsonbailey c1b87a6
feat: update managed-agent to use track_metrics_of_async and add prov…
jsonbailey 90b548f
fix: resolve lint errors from rebase onto main
jsonbailey ab9c4bf
simplifying tool configuration
jsonbailey 70097a0
Merge branch 'main' into jb/aic-1664/managed-agent
jsonbailey e4b3830
simplify agent loop to use built-ins
jsonbailey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/ai-providers/server-ai-langchain/src/ldai_langchain/langchain_agent_runner.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| """LangChain agent runner for LaunchDarkly AI SDK.""" | ||
|
|
||
| from typing import Any | ||
|
|
||
| from ldai import log | ||
| from ldai.providers import AgentResult, AgentRunner | ||
| from ldai.providers.types import LDAIMetrics | ||
|
|
||
| from ldai_langchain.langchain_helper import sum_token_usage_from_messages | ||
|
|
||
|
|
||
| class LangChainAgentRunner(AgentRunner): | ||
| """ | ||
| AgentRunner implementation for LangChain. | ||
|
|
||
| Wraps a compiled LangChain agent graph (from ``langchain.agents.create_agent``) | ||
| and delegates execution to it. Tool calling and loop management are handled | ||
| internally by the graph. | ||
| Returned by LangChainRunnerFactory.create_agent(config, tools). | ||
| """ | ||
|
|
||
| def __init__(self, agent: Any): | ||
| self._agent = agent | ||
|
|
||
| async def run(self, input: Any) -> AgentResult: | ||
| """ | ||
| Run the agent with the given input string. | ||
|
|
||
| Delegates to the compiled LangChain agent, which handles | ||
| the tool-calling loop internally. | ||
|
|
||
| :param input: The user prompt or input to the agent | ||
| :return: AgentResult with output, raw response, and aggregated metrics | ||
| """ | ||
| try: | ||
| result = await self._agent.ainvoke({ | ||
| "messages": [{"role": "user", "content": str(input)}] | ||
| }) | ||
| messages = result.get("messages", []) | ||
| output = "" | ||
| if messages: | ||
| last = messages[-1] | ||
| if hasattr(last, 'content') and isinstance(last.content, str): | ||
| output = last.content | ||
| return AgentResult( | ||
| output=output, | ||
| raw=result, | ||
| metrics=LDAIMetrics( | ||
| success=True, | ||
| usage=sum_token_usage_from_messages(messages), | ||
| ), | ||
| ) | ||
| except Exception as error: | ||
| log.warning(f"LangChain agent run failed: {error}") | ||
| return AgentResult( | ||
| output="", | ||
| raw=None, | ||
| metrics=LDAIMetrics(success=False, usage=None), | ||
| ) | ||
|
|
||
| def get_agent(self) -> Any: | ||
| """Return the underlying compiled LangChain agent.""" | ||
| return self._agent |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicated tool-filtering logic in two functions
Low Severity
build_structured_toolsand_resolve_tools_for_langchaincontain nearly identical tool-filtering logic: checkingisinstance(td, dict), checking thetypefield, extractingname, verifying registry membership, and logging the same warnings. The only difference is the output format (dicts vsStructuredTool). Extracting the shared filtering into a common helper would reduce duplication and the risk of inconsistent updates.Additional Locations (1)
packages/ai-providers/server-ai-langchain/src/ldai_langchain/langchain_helper.py#L97-L138