Skip to content

Commit ddf39ee

Browse files
committed
Adding completion handler
1 parent 6889563 commit ddf39ee

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

samples/EverythingServer/Program.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,57 @@ await ctx.Server.RequestSamplingAsync([
133133
}
134134
return Task.FromResult(new EmptyResult());
135135
})
136+
.WithGetCompletionHandler((ctx, ct) =>
137+
{
138+
var exampleCompletions = new Dictionary<string, IEnumerable<string>>
139+
{
140+
{ "style", ["casual", "formal", "technical", "friendly"] },
141+
{ "temperature", ["0", "0.5", "0.7", "1.0"] },
142+
{ "resourceId", ["1", "2", "3", "4", "5"] }
143+
};
144+
145+
var @ref = ctx.Params?.Ref;
146+
147+
if (@ref is null)
148+
{
149+
throw new NotSupportedException($"Reference is required.");
150+
}
151+
152+
var argument = ctx.Params!.Argument;
153+
154+
if (@ref.Type == "ref/resource")
155+
{
156+
var resourceId = @ref.Uri?.Split("/").Last();
157+
158+
if (resourceId is null)
159+
{
160+
return Task.FromResult(new CompleteResult());
161+
}
162+
163+
var values = exampleCompletions["resourceId"].Where(id => id.StartsWith(argument.Value));
164+
165+
return Task.FromResult(new CompleteResult
166+
{
167+
Completion = new Completion { Values = [..values], HasMore = false, Total = values.Count() }
168+
});
169+
}
170+
171+
if (@ref.Type == "ref/prompt")
172+
{
173+
if (!exampleCompletions.TryGetValue(argument.Name, out IEnumerable<string>? value))
174+
{
175+
throw new NotSupportedException($"Unknown argument name: {argument.Name}");
176+
}
177+
178+
var values = value.Where(value => value.StartsWith(argument.Value));
179+
return Task.FromResult(new CompleteResult
180+
{
181+
Completion = new Completion { Values = [..values], HasMore = false, Total = values.Count() }
182+
});
183+
}
184+
185+
throw new NotSupportedException($"Unknown reference type: {@ref.Type}");
186+
})
136187
;
137188

138189
builder.Services.AddHostedService(sp =>

0 commit comments

Comments
 (0)