Skip to content

Commit fb36f2e

Browse files
author
Jicheng Lu
committed
return agent code script time stamp
1 parent d4df317 commit fb36f2e

File tree

11 files changed

+35
-19
lines changed

11 files changed

+35
-19
lines changed

src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public interface IAgentService
7272
Task<List<AgentCodeScript>> GetAgentCodeScripts(string agentId, AgentCodeScriptFilter? filter = null)
7373
=> Task.FromResult(new List<AgentCodeScript>());
7474

75-
Task<string?> GetAgentCodeScript(string agentId, string scriptName, string scriptType = AgentCodeScriptType.Src)
76-
=> Task.FromResult(string.Empty);
75+
Task<AgentCodeScript?> GetAgentCodeScript(string agentId, string scriptName, string scriptType = AgentCodeScriptType.Src)
76+
=> Task.FromResult((AgentCodeScript?)null);
7777

7878
Task<bool> UpdateAgentCodeScripts(string agentId, List<AgentCodeScript> codeScripts, AgentCodeScriptUpdateOptions? options = null)
7979
=> Task.FromResult(false);

src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentCodeScript.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ public class AgentCodeScript : AgentCodeScriptBase
55
public string Id { get; set; }
66
public string AgentId { get; set; } = null!;
77

8+
public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
9+
public DateTime UpdatedTime { get; set; } = DateTime.UtcNow;
10+
811
public AgentCodeScript() : base()
912
{
1013
}

src/Infrastructure/BotSharp.Abstraction/Instructs/Contexts/CodeInstructContext.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ namespace BotSharp.Abstraction.Instructs.Contexts;
22

33
public class CodeInstructContext
44
{
5-
public string CodeScript { get; set; }
5+
public string ScriptName { get; set; }
6+
public string ScriptContent { get; set; }
67
public string ScriptType { get; set; }
78
public List<KeyValue> Arguments { get; set; } = [];
89
}

src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ bool DeleteAgentTasks(string agentId, List<string>? taskIds = null)
114114
#region Agent Code
115115
List<AgentCodeScript> GetAgentCodeScripts(string agentId, AgentCodeScriptFilter? filter = null)
116116
=> throw new NotImplementedException();
117-
string? GetAgentCodeScript(string agentId, string scriptName, string scriptType = AgentCodeScriptType.Src)
117+
AgentCodeScript? GetAgentCodeScript(string agentId, string scriptName, string scriptType = AgentCodeScriptType.Src)
118118
=> throw new NotImplementedException();
119119
bool UpdateAgentCodeScripts(string agentId, List<AgentCodeScript> scripts, AgentCodeScriptDbUpdateOptions? options = null)
120120
=> throw new NotImplementedException();

src/Infrastructure/BotSharp.Core.Rules/Engines/RuleEngine.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,15 @@ private async Task<bool> TriggerCodeScript(string agentId, string triggerName, R
111111

112112
var msg = $"rule trigger ({triggerName}) code script ({scriptName}) in agent ({agentId}) => args: {options.ArgumentContent?.RootElement.GetRawText()}.";
113113

114-
if (string.IsNullOrWhiteSpace(codeScript))
114+
if (string.IsNullOrWhiteSpace(codeScript?.Content))
115115
{
116116
_logger.LogWarning($"Unable to find {msg}.");
117117
return false;
118118
}
119119

120120
try
121121
{
122-
var response = await processor.RunAsync(codeScript, options: new()
122+
var response = await processor.RunAsync(codeScript.Content, options: new()
123123
{
124124
ScriptName = scriptName,
125125
Arguments = BuildArguments(options.ArgumentName, options.ArgumentContent)

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.Coding.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public async Task<List<AgentCodeScript>> GetAgentCodeScripts(string agentId, Age
1414
return await Task.FromResult(scripts);
1515
}
1616

17-
public async Task<string?> GetAgentCodeScript(string agentId, string scriptName, string scriptType = AgentCodeScriptType.Src)
17+
public async Task<AgentCodeScript?> GetAgentCodeScript(string agentId, string scriptName, string scriptType = AgentCodeScriptType.Src)
1818
{
1919
var db = _services.GetRequiredService<IBotSharpRepository>();
2020
var script = db.GetAgentCodeScript(agentId, scriptName, scriptType);

src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private string BuildFileName(string? name, string? extension, string defaultName
101101

102102
private IImageConverter? GetImageConverter(string? provider)
103103
{
104-
var converter = _services.GetServices<IImageConverter>().FirstOrDefault(x => x.Provider == (provider ?? "file-handler"));
104+
var converter = _services.GetServices<IImageConverter>().FirstOrDefault(x => x.Provider == (provider ?? "image-handler"));
105105
return converter;
106106
}
107107
#endregion

src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ await hook.OnResponseGenerated(new InstructResponseModel
214214
// Get code script
215215
var scriptType = codeOptions?.ScriptType ?? AgentCodeScriptType.Src;
216216
var codeScript = await agentService.GetAgentCodeScript(agent.Id, scriptName, scriptType);
217-
if (string.IsNullOrWhiteSpace(codeScript))
217+
if (string.IsNullOrWhiteSpace(codeScript?.Content))
218218
{
219219
#if DEBUG
220220
_logger.LogWarning($"Empty code script. (Agent: {agent.Id}, {scriptName})");
@@ -231,7 +231,8 @@ await hook.OnResponseGenerated(new InstructResponseModel
231231

232232
var context = new CodeInstructContext
233233
{
234-
CodeScript = codeScript,
234+
ScriptName = codeScript.Name,
235+
ScriptContent = codeScript.Content,
235236
ScriptType = scriptType,
236237
Arguments = arguments
237238
};
@@ -254,7 +255,7 @@ await hook.OnResponseGenerated(new InstructResponseModel
254255
}
255256

256257
// Run code script
257-
var codeResponse = await codeProcessor.RunAsync(context.CodeScript, options: new()
258+
var codeResponse = await codeProcessor.RunAsync(context.ScriptContent, options: new()
258259
{
259260
ScriptName = scriptName,
260261
Arguments = context.Arguments
@@ -284,7 +285,7 @@ await hook.OnResponseGenerated(new InstructResponseModel
284285
Model = string.Empty,
285286
TemplateName = scriptName,
286287
UserMessage = message.Content,
287-
SystemInstruction = context?.CodeScript,
288+
SystemInstruction = $"Code script name: {codeScript.Name}, Version: {codeScript.UpdatedTime.ToString("o")}",
288289
CompletionText = response.Text
289290
});
290291
}

src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentCodeScript.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public List<AgentCodeScript> GetAgentCodeScripts(string agentId, AgentCodeScript
5050
return results;
5151
}
5252

53-
public string? GetAgentCodeScript(string agentId, string scriptName, string scriptType = AgentCodeScriptType.Src)
53+
public AgentCodeScript? GetAgentCodeScript(string agentId, string scriptName, string scriptType = AgentCodeScriptType.Src)
5454
{
5555
if (string.IsNullOrWhiteSpace(agentId)
5656
|| string.IsNullOrWhiteSpace(scriptName)
@@ -66,11 +66,20 @@ public List<AgentCodeScript> GetAgentCodeScripts(string agentId, AgentCodeScript
6666
}
6767

6868
var foundFile = Directory.EnumerateFiles(dir).FirstOrDefault(file => scriptName.IsEqualTo(Path.GetFileName(file)));
69-
if (!string.IsNullOrEmpty(foundFile))
69+
if (!File.Exists(foundFile))
7070
{
71-
return File.ReadAllText(foundFile);
71+
return null;
7272
}
73-
return string.Empty;
73+
74+
return new AgentCodeScript
75+
{
76+
AgentId = agentId,
77+
Name = scriptName,
78+
ScriptType = scriptType,
79+
Content = File.ReadAllText(foundFile),
80+
CreatedTime = File.GetCreationTimeUtc(foundFile),
81+
UpdatedTime = File.GetLastWriteTimeUtc(foundFile)
82+
};
7483
}
7584

7685
public bool UpdateAgentCodeScripts(string agentId, List<AgentCodeScript> scripts, AgentCodeScriptDbUpdateOptions? options = null)

src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentCodeScriptDocument.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ public static AgentCodeScript ToDomainModel(AgentCodeScriptDocument script)
3131
AgentId = script.AgentId,
3232
Name = script.Name,
3333
Content = script.Content,
34-
ScriptType = script.ScriptType
34+
ScriptType = script.ScriptType,
35+
CreatedTime = script.CreatedTime,
36+
UpdatedTime = script.UpdatedTime
3537
};
3638
}
3739
}

0 commit comments

Comments
 (0)