-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use separate
AssemblyLoadContext
for every agent plugin (#56)
- Loading branch information
1 parent
2efc3d3
commit 249faf0
Showing
4 changed files
with
46 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,36 @@ | ||
using System.Reflection; | ||
using System.Runtime.Loader; | ||
|
||
namespace ShellCopilot.Kernel; | ||
|
||
internal class AgentAssemblyLoadContext : AssemblyLoadContext | ||
{ | ||
private readonly string _dependencyDir; | ||
|
||
internal AgentAssemblyLoadContext(string name, string dependencyDir) | ||
: base($"{name.Replace(' ', '.')}-ALC", isCollectible: false) | ||
{ | ||
if (!Directory.Exists(dependencyDir)) | ||
{ | ||
throw new ArgumentException($"The agent home directory '{dependencyDir}' doesn't exist.", nameof(dependencyDir)); | ||
} | ||
|
||
// Save the full path to the dependencies directory when creating the context. | ||
_dependencyDir = dependencyDir; | ||
} | ||
|
||
protected override Assembly Load(AssemblyName assemblyName) | ||
{ | ||
// Create a path to the assembly in the dependencies directory. | ||
string path = Path.Combine(_dependencyDir, $"{assemblyName.Name}.dll"); | ||
|
||
if (File.Exists(path)) | ||
{ | ||
// If the assembly exists in our dependency directory, then load it into this load context. | ||
return LoadFromAssemblyPath(path); | ||
} | ||
|
||
// Otherwise we will depend on the default load context to resolve the request. | ||
return null; | ||
} | ||
} |