Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Collections.Generic;
using System.Linq;
using Azure.AI.Agents.Persistent;
using Microsoft.Shared.Diagnostics;

namespace Microsoft.Bot.ObjectModel;

/// <summary>
/// Extension methods for <see cref="CodeInterpreterTool"/>.
/// </summary>
internal static class CodeInterpreterToolExtensions
{
/// <summary>
/// Creates a <see cref="CodeInterpreterToolDefinition"/> from a <see cref="CodeInterpreterTool"/>.
/// </summary>
/// <param name="tool">Instance of <see cref="CodeInterpreterTool"/></param>
internal static CodeInterpreterToolDefinition CreateCodeInterpreterToolDefinition(this CodeInterpreterTool tool)
{
Throw.IfNull(tool);

return new CodeInterpreterToolDefinition();
}

/// <summary>
/// Converts a <see cref="CodeInterpreterTool"/> to an <see cref="OpenAI.Responses.CodeInterpreterTool"/>.
/// </summary>
/// <param name="tool">Instance of <see cref="CodeInterpreterTool"/></param>
/// <returns>A new <see cref="OpenAI.Responses.CodeInterpreterTool"/> instance configured with the container ID from the tool's extension data.</returns>
internal static OpenAI.Responses.CodeInterpreterTool CreateCodeInterpreterTool(this CodeInterpreterTool tool)
{
Throw.IfNull(tool);

var containerId = tool.ExtensionData?.GetPropertyOrNull<StringDataValue>(InitializablePropertyPath.Create("containerId"))?.Value;
Throw.IfNull(containerId, "The 'containerId' property must be specified in the CodeInterpreterTool's extension data to create a code interpreter tool.");

return new OpenAI.Responses.CodeInterpreterTool(new OpenAI.Responses.CodeInterpreterToolContainer(containerId));
}

/// <summary>
/// Collects the file IDs from the extension data of a <see cref="CodeInterpreterTool"/>.
/// </summary>
/// <param name="tool">Instance of <see cref="CodeInterpreterTool"/></param>
internal static List<string>? GetFileIds(this CodeInterpreterTool tool)
{
var fileIds = tool.ExtensionData?.GetPropertyOrNull<TableDataValue>(InitializablePropertyPath.Create("fileIds"));
return fileIds is not null
? [.. fileIds.Values.Select(fileId => fileId.GetPropertyOrNull<StringDataValue>(InitializablePropertyPath.Create("value"))?.Value)]
: null;
}

/// <summary>
/// Collects the data sources from the extension data of a <see cref="CodeInterpreterTool"/>.
/// </summary>
/// <param name="tool">Instance of <see cref="CodeInterpreterTool"/></param>
internal static List<VectorStoreDataSource>? GetDataSources(this CodeInterpreterTool tool)
{
var dataSources = tool.ExtensionData?.GetPropertyOrNull<TableDataValue>(InitializablePropertyPath.Create("dataSources"));
return dataSources is not null
? dataSources.Values.Select(dataSource => dataSource.CreateDataSource()).ToList()
: null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Collections.Generic;
using System.Linq;
using Azure.AI.Agents.Persistent;
using Microsoft.Shared.Diagnostics;

namespace Microsoft.Bot.ObjectModel;

/// <summary>
/// Extension methods for <see cref="FileSearchTool"/>.
/// </summary>
internal static class FileSearchToolExtensions
{
/// <summary>
/// Creates a <see cref="FileSearchToolDefinition"/> from a <see cref="FileSearchTool"/>.
/// </summary>
/// <param name="tool">Instance of <see cref="FileSearchTool"/></param>
internal static FileSearchToolDefinition CreateFileSearchToolDefinition(this FileSearchTool tool)
{
Throw.IfNull(tool);

// TODO: Add support for FileSearchToolDefinitionDetails.

return new FileSearchToolDefinition();
}

/// <summary>
/// Creates an <see cref="OpenAI.Responses.FileSearchTool"/> from a <see cref="FileSearchTool"/>.
/// </summary>
/// <param name="tool">Instance of <see cref="FileSearchTool"/></param>
/// <returns>A new <see cref="OpenAI.Responses.FileSearchTool"/> instance configured with the vector store IDs.</returns>
internal static OpenAI.Responses.FileSearchTool CreateFileSearchTool(this FileSearchTool tool)
{
Throw.IfNull(tool);

return new OpenAI.Responses.FileSearchTool(tool.GetVectorStoreIds());
}

/// <summary>
/// Get the vector store IDs for the specified <see cref="FileSearchTool"/>.
/// </summary>
/// <param name="tool">Instance of <see cref="FileSearchTool"/></param>
internal static List<string>? GetVectorStoreIds(this FileSearchTool tool)
{
return tool.VectorStoreIds?.LiteralValue.ToList();
}

internal static IList<VectorStoreConfigurations>? GetVectorStoreConfigurations(this FileSearchTool tool)
{
var dataSources = tool.ExtensionData?.GetPropertyOrNull<TableDataValue>(InitializablePropertyPath.Create("options.configurations"));
return dataSources?.Values.Select(value => value.CreateVectorStoreConfiguration()).ToList();
}

internal static VectorStoreConfigurations CreateVectorStoreConfiguration(this RecordDataValue value)
{
Throw.IfNull(value);

var storeName = value.GetPropertyOrNull<StringDataValue>(InitializablePropertyPath.Create("storeName"))?.Value;
Throw.IfNullOrEmpty(storeName);

var dataSources = value.GetDataSources();
Throw.IfNull(dataSources);

return new VectorStoreConfigurations(storeName, new VectorStoreConfiguration(dataSources));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using Azure.AI.Agents.Persistent;
using Microsoft.Shared.Diagnostics;
using OpenAI.Responses;

namespace Microsoft.Bot.ObjectModel;

/// <summary>
/// Extension methods for <see cref="InvokeClientTaskAction"/>.
/// </summary>
public static class FunctionToolExtensions
{
/// <summary>
/// Creates a <see cref="FunctionToolDefinition"/> from a <see cref="InvokeClientTaskAction"/>.
/// </summary>
/// <param name="tool">Instance of <see cref="InvokeClientTaskAction"/></param>
internal static FunctionToolDefinition CreateFunctionToolDefinition(this InvokeClientTaskAction tool)
{
Throw.IfNull(tool);
Throw.IfNull(tool.Name);

BinaryData parameters = tool.GetParameters();

return new FunctionToolDefinition(
name: tool.Name,
description: tool.Description,
parameters: parameters);
}

/// <summary>
/// Creates a <see cref="FunctionTool"/> from a <see cref="InvokeClientTaskAction"/>.
/// </summary>
/// <param name="tool">Instance of <see cref="InvokeClientTaskAction"/></param>
/// <returns>A new <see cref="FunctionTool"/> instance configured with the function name, parameters, and description.</returns>
internal static FunctionTool CreateFunctionTool(this InvokeClientTaskAction tool)
{
Throw.IfNull(tool);
Throw.IfNull(tool.Name);

BinaryData parameters = tool.GetParameters();

return new FunctionTool(
functionName: tool.Name,
functionParameters: parameters,
strictModeEnabled: null)
{
FunctionDescription = tool.Description
};
}

/// <summary>
/// Creates the parameters schema for a <see cref="InvokeClientTaskAction"/>.
/// </summary>
/// <param name="tool">Instance of <see cref="InvokeClientTaskAction"/></param>
internal static BinaryData GetParameters(this InvokeClientTaskAction tool)
{
Throw.IfNull(tool);

var parameters = tool.ClientActionInputSchema?.GetSchema().ToString() ?? DefaultSchema;

return new BinaryData(parameters);
}

private const string DefaultSchema = "{\"type\":\"object\",\"properties\":{},\"additionalProperties\":false}";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft. All rights reserved.

using Azure.AI.Agents.Persistent;
using Microsoft.Bot.ObjectModel;
using Microsoft.Shared.Diagnostics;

namespace Microsoft.Extensions.AI;

/// <summary>
/// Extension methods for <see cref="HostedCodeInterpreterTool"/>.
/// </summary>
internal static class HostedCodeInterpreterToolExtensions
{
/// <summary>
/// Creates a <see cref="CodeInterpreterToolDefinition"/> from a <see cref="HostedCodeInterpreterTool"/>.
/// </summary>
/// <param name="tool">Instance of <see cref="CodeInterpreterTool"/></param>
internal static CodeInterpreterToolDefinition CreateHostedCodeInterpreterToolDefinition(this HostedCodeInterpreterTool tool)
{
Throw.IfNull(tool);

return new CodeInterpreterToolDefinition();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Microsoft. All rights reserved.

using Azure.AI.Agents.Persistent;
using Microsoft.Shared.Diagnostics;

namespace Microsoft.Extensions.AI;

/// <summary>
/// Extension methods for <see cref="HostedFileSearchTool"/>.
/// </summary>
internal static class HostedFileSearchToolExtensions
{
/// <summary>
/// Creates a <see cref="FileSearchToolDefinition"/> from a <see cref="HostedFileSearchTool"/>.
/// </summary>
/// <param name="tool">Instance of <see cref="HostedFileSearchTool"/></param>
internal static FileSearchToolDefinition CreateFileSearchToolDefinition(this HostedFileSearchTool tool)
{
Throw.IfNull(tool);

// TODO: Add support for FileSearchToolDefinitionDetails.

return new FileSearchToolDefinition();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Linq;
using Azure.AI.Agents.Persistent;
using Microsoft.Shared.Diagnostics;

namespace Microsoft.Extensions.AI;

/// <summary>
/// Extension methods for <see cref="HostedMcpServerTool"/>.
/// </summary>
internal static class HostedMcpServerToolExtensions
{
/// <summary>
/// Creates a <see cref="MCPToolDefinition"/> from a <see cref="HostedMcpServerTool"/>.
/// </summary>
/// <param name="tool">Instance of <see cref="HostedMcpServerTool"/></param>
internal static MCPToolDefinition CreateMcpToolDefinition(this HostedMcpServerTool tool)
{
Throw.IfNull(tool);
Throw.IfNull(tool.ServerName);
Throw.IfNull(tool.ServerAddress);

var definition = new MCPToolDefinition(tool.ServerName, tool.ServerAddress);
tool.AllowedTools?.ToList().ForEach(definition.AllowedTools.Add);
return definition;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft. All rights reserved.

using Azure.AI.Agents.Persistent;
using Microsoft.Shared.Diagnostics;

namespace Microsoft.Extensions.AI;

/// <summary>
/// Extension methods for <see cref="HostedWebSearchTool"/>.
/// </summary>
internal static class HostedWebSearchToolExtensions
{
/// <summary>
/// Creates a <see cref="BingGroundingToolDefinition"/> from a <see cref="HostedWebSearchTool"/>.
/// </summary>
/// <param name="tool">Instance of <see cref="HostedWebSearchTool"/></param>
internal static BingGroundingToolDefinition CreateBingGroundingToolDefinition(this HostedWebSearchTool tool)
{
Throw.IfNull(tool);

// TODO: Add support for BingGroundingSearchToolParameters.
var parameters = new BingGroundingSearchToolParameters([]);

return new BingGroundingToolDefinition(parameters);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using Azure.AI.Agents.Persistent;
using Microsoft.Shared.Diagnostics;
using OpenAI.Responses;

namespace Microsoft.Bot.ObjectModel;

/// <summary>
/// Extension methods for <see cref="McpServerTool"/>.
/// </summary>
internal static class McpServerToolExtensions
{
/// <summary>
/// Creates a <see cref="MCPToolDefinition"/> from a <see cref="McpServerTool"/>.
/// </summary>
/// <param name="tool">Instance of <see cref="McpServerTool"/></param>
internal static MCPToolDefinition CreateMcpToolDefinition(this McpServerTool tool)
{
Throw.IfNull(tool);
Throw.IfNull(tool.ServerName?.LiteralValue);
Throw.IfNull(tool.Connection);

// TODO: Add support for additional properties

var connection = tool.Connection as AnonymousConnection ?? throw new ArgumentException($"Only AnonymousConnection is supported for MCP Server Tool connections. Actual connection type: {tool.Connection.GetType().Name}", nameof(tool));
var serverUrl = connection.Endpoint?.LiteralValue;
Throw.IfNullOrEmpty(serverUrl, nameof(connection.Endpoint));

return new MCPToolDefinition(tool.ServerName?.LiteralValue, serverUrl);
}

/// <summary>
/// Creates a <see cref="McpTool"/> from a <see cref="McpServerTool"/>.
/// </summary>
/// <param name="tool">Instance of <see cref="McpServerTool"/></param>
/// <returns>A new <see cref="McpTool"/> instance configured with the server name and URL.</returns>
internal static McpTool CreateMcpTool(this McpServerTool tool)
{
Throw.IfNull(tool);
Throw.IfNull(tool.ServerName?.LiteralValue);
Throw.IfNull(tool.Connection);

// TODO: Add support for headers

var connection = tool.Connection as AnonymousConnection ?? throw new ArgumentException("Only AnonymousConnection is supported for MCP Server Tool connections.", nameof(tool));
var serverUrl = connection.Endpoint?.LiteralValue;
Throw.IfNullOrEmpty(serverUrl, nameof(connection.Endpoint));

return new McpTool(tool.ServerName?.LiteralValue, serverUrl);
}
}
Loading
Loading