Skip to content

.NET: [Feature]: tools support NativeAot #5969

Description

@williamlzw

Description

dotnet:
tools support NativeAot

Code Sample

var skillsProvider = new AgentSkillsProvider(
    Path.GetFullPath(Config.SKILLS_DIR),
    SubprocessScriptRunner.RunAsync);
var config = new OllamaApiClient.Configuration
{
    Uri = new Uri(Config.OLLAMA_API_URL),
    Model = Config.OLLAMA_MODEL,
    JsonSerializerContext = OllamaJsonContext.Default
};
// 创建主客户端
OllamaApiClient client = new OllamaApiClient(config);

// 创建主Agent
return client.AsAIAgent(options: new ChatClientAgentOptions
{
    Name = "LeadAgent",
    ChatOptions = new ChatOptions
    {
        Instructions = Config.AGENT_INSTRUCTIONS,
        Reasoning = new() { Effort = ReasoningEffort.High },
        Tools = NativeAotTools.GetAllTools()//not support nativeaot
    },
    AIContextProviders = [skillsProvider]
});

internal static class NativeAotTools
{

    // ==================== 3. ReadFile 工具 ====================
    public static readonly AIFunctionDeclaration ReadFileDecl = AIFunctionFactory.CreateDeclaration(
        name: "ReadFile",
        description: "【沙箱文件读取】仅可读取Agent沙箱workspace目录内的文件,路径必须以/开头,禁止传入本地绝对路径",
        jsonSchema: JsonDocument.Parse("""
        {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "file_path": { "type": "string", "description": "文件路径,必须以/开头" },
                "encoding": { "type": ["string", "null"], "description": "编码,默认utf-8" },
                "create_if_missing": { "type": "boolean", "default": false, "description": "文件不存在时是否创建空文件" }
            },
            "required": ["file_path"]
        }
        """).RootElement);
    public static ValueTask<object?> ExecReadFile(AIFunctionArguments args, CancellationToken ct)
    {
        string path = args["file_path"]!.ToString();
        string? encoding = args.TryGetValue("encoding", out var e) ? e.ToString() : null;
        bool create = args.TryGetValue("create_if_missing", out var c) && bool.Parse(c.ToString());
        return ValueTask.FromResult<object?>(FileTools.ReadFile(path, encoding, create));
    }
    /// <summary>
    /// 注册所有 NativeAOT 兼容工具(Declaration + Executor)
    /// </summary>
    public static List<AITool> GetAllTools()
    {
        var ret = new List<AITool>();
        ret.Add(ReadFileDecl);
        return ret;
    }
}

Language/SDK

.NET

Metadata

Metadata

Labels

.NETUsage: [Issues, PRs], Target: .Net

Type

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions