Skip to content
Merged
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
66 changes: 61 additions & 5 deletions src/xAI.Tests/GrokConversionTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System;
using System.Collections.Generic;
using System.Text;
using Google.Protobuf.WellKnownTypes;
using Google.Protobuf.WellKnownTypes;
using Microsoft.Extensions.AI;
using Moq;
using OpenAI.Responses;
using xAI.Protocol;

namespace xAI;
namespace xAI.Tests;

public class GrokConversionTests
{
Expand Down Expand Up @@ -278,4 +276,62 @@ public void AsTool_WithHostedMcpTool()
Assert.Contains(KeyValuePair.Create("x-extra", "bar"), tool.Mcp.ExtraHeaders);
Assert.Contains(KeyValuePair.Create("foo", "baz"), tool.Mcp.ExtraHeaders);
}

static IGrokChatClient CreateClient()
{
var mock = new Mock<IGrokChatClient>();
mock.SetupGet(x => x.DefaultModelId).Returns("grok-4");
mock.SetupGet(x => x.EndUserId).Returns((string?)null);
return mock.Object;
}

[Fact]
public void AsCompletionsRequest_NullToolMode_SetsAutoToolChoice()
{
var request = CreateClient().AsCompletionsRequest([], new ChatOptions { ToolMode = null });

Assert.NotNull(request.ToolChoice);
Assert.True(request.ToolChoice.HasMode);
Assert.Equal(Protocol.ToolMode.Auto, request.ToolChoice.Mode);
}

[Fact]
public void AsCompletionsRequest_AutoToolMode_SetsAutoToolChoice()
{
var request = CreateClient().AsCompletionsRequest([], new ChatOptions { ToolMode = ChatToolMode.Auto });

Assert.NotNull(request.ToolChoice);
Assert.True(request.ToolChoice.HasMode);
Assert.Equal(Protocol.ToolMode.Auto, request.ToolChoice.Mode);
}

[Fact]
public void AsCompletionsRequest_NoneToolMode_SetsNoneToolChoice()
{
var request = CreateClient().AsCompletionsRequest([], new ChatOptions { ToolMode = ChatToolMode.None });

Assert.NotNull(request.ToolChoice);
Assert.True(request.ToolChoice.HasMode);
Assert.Equal(Protocol.ToolMode.None, request.ToolChoice.Mode);
}

[Fact]
public void AsCompletionsRequest_RequireAnyToolMode_SetsRequiredToolChoice()
{
var request = CreateClient().AsCompletionsRequest([], new ChatOptions { ToolMode = ChatToolMode.RequireAny });

Assert.NotNull(request.ToolChoice);
Assert.True(request.ToolChoice.HasMode);
Assert.Equal(Protocol.ToolMode.Required, request.ToolChoice.Mode);
}

[Fact]
public void AsCompletionsRequest_RequireSpecificToolMode_SetsFunctionNameToolChoice()
{
var request = CreateClient().AsCompletionsRequest([], new ChatOptions { ToolMode = ChatToolMode.RequireSpecific("get_weather") });

Assert.NotNull(request.ToolChoice);
Assert.True(request.ToolChoice.HasFunctionName);
Assert.Equal("get_weather", request.ToolChoice.FunctionName);
}
}
9 changes: 9 additions & 0 deletions src/xAI/GrokProtocolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,15 @@ internal static GetCompletionsRequest AsCompletionsRequest(this IGrokChatClient
});
}

request.ToolChoice = options?.ToolMode switch
{
null or AutoChatToolMode => new ToolChoice { Mode = ToolMode.Auto },
NoneChatToolMode => new ToolChoice { Mode = ToolMode.None },
RequiredChatToolMode { RequiredFunctionName: { } name } => new ToolChoice { FunctionName = name },
RequiredChatToolMode => new ToolChoice { Mode = ToolMode.Required },
_ => null
};

foreach (var message in messages)
{
if (message.RawRepresentation is Message input)
Expand Down
1 change: 1 addition & 0 deletions src/xAI/xAI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<ItemGroup>
<None Include="..\..\osmfeula.txt" Link="osmfeula.txt" PackagePath="OSMFEULA.txt" />
<InternalsVisibleTo Include="xAI.Tests" />
<InternalsVisibleTo Include="DynamicProxyGenAssembly2"/>
</ItemGroup>

<ItemGroup>
Expand Down
Loading