-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Move Copilot context provider to EA.Copilot and handler to LanguageServer #77973
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/Features/ExternalAccess/Copilot/Completion/CodeSnippetItem.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Text.Json.Serialization; | ||
|
||
namespace Microsoft.CodeAnalysis.ExternalAccess.Copilot.Completion; | ||
|
||
internal record CodeSnippetItem : IContextItem | ||
{ | ||
public CodeSnippetItem(string uri, string value, string[]? additionalUris = null, int importance = Completion.Importance.Default) | ||
{ | ||
this.Uri = uri; | ||
this.Value = value; | ||
this.AdditionalUris = additionalUris; | ||
this.Importance = importance; | ||
} | ||
|
||
[JsonPropertyName("uri")] | ||
public string Uri { get; init; } | ||
|
||
[JsonPropertyName("value")] | ||
public string Value { get; init; } | ||
|
||
[JsonPropertyName("additionalUris")] | ||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] | ||
public string[]? AdditionalUris { get; init; } | ||
|
||
[JsonPropertyName("importance")] | ||
public int Importance { get; init; } | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Features/ExternalAccess/Copilot/Completion/ICSharpCopilotContextProviderService.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Collections.Generic; | ||
using System.Threading; | ||
|
||
namespace Microsoft.CodeAnalysis.ExternalAccess.Copilot.Completion; | ||
|
||
internal interface ICSharpCopilotContextProviderService | ||
{ | ||
IAsyncEnumerable<IContextItem> GetContextItemsAsync(Document document, int position, IReadOnlyDictionary<string, object> activeExperiments, CancellationToken cancellationToken); | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Features/ExternalAccess/Copilot/Completion/IContextItem.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Text.Json.Serialization; | ||
|
||
namespace Microsoft.CodeAnalysis.ExternalAccess.Copilot.Completion; | ||
|
||
[JsonDerivedType(typeof(CodeSnippetItem))] | ||
[JsonDerivedType(typeof(TraitItem))] | ||
internal interface IContextItem | ||
{ | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Features/ExternalAccess/Copilot/Completion/IContextProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.CodeAnalysis.ExternalAccess.Copilot.Completion; | ||
|
||
internal interface IContextProvider | ||
{ | ||
ValueTask ProvideContextItemsAsync( | ||
Document document, | ||
int position, | ||
IReadOnlyDictionary<string, object> activeExperiments, | ||
Func<ImmutableArray<IContextItem>, CancellationToken, ValueTask> callback, | ||
CancellationToken cancellationToken); | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Features/ExternalAccess/Copilot/Completion/Importance.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
namespace Microsoft.CodeAnalysis.ExternalAccess.Copilot.Completion; | ||
|
||
internal static class Importance | ||
{ | ||
public const int Lowest = 0; | ||
public const int Highest = 100; | ||
|
||
public const int Default = Lowest; | ||
} |
26 changes: 26 additions & 0 deletions
26
src/Features/ExternalAccess/Copilot/Completion/TraitItem.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Text.Json.Serialization; | ||
|
||
namespace Microsoft.CodeAnalysis.ExternalAccess.Copilot.Completion; | ||
|
||
internal record TraitItem : IContextItem | ||
{ | ||
public TraitItem(string name, string value, int importance = Completion.Importance.Default) | ||
{ | ||
this.Name = name; | ||
this.Value = value; | ||
this.Importance = importance; | ||
} | ||
|
||
[JsonPropertyName("name")] | ||
public string Name { get; init; } | ||
|
||
[JsonPropertyName("value")] | ||
public string Value { get; init; } | ||
|
||
[JsonPropertyName("importance")] | ||
public int Importance { get; init; } | ||
} |
85 changes: 85 additions & 0 deletions
85
src/Features/ExternalAccess/Copilot/Internal/Completion/CSharpContextProviderService.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Composition; | ||
using System.Linq; | ||
using System.Runtime.CompilerServices; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.ErrorReporting; | ||
using Microsoft.CodeAnalysis.ExternalAccess.Copilot.Completion; | ||
using Microsoft.CodeAnalysis.Host.Mef; | ||
using Microsoft.VisualStudio.Threading; | ||
|
||
namespace Microsoft.CodeAnalysis.ExternalAccess.Copilot.Internal.Completion; | ||
|
||
[Shared] | ||
[Export(typeof(ICSharpCopilotContextProviderService))] | ||
internal sealed class CSharpContextProviderService : ICSharpCopilotContextProviderService | ||
{ | ||
// Exposed for testing | ||
public ImmutableArray<IContextProvider> Providers { get; } | ||
|
||
[ImportingConstructor] | ||
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] | ||
public CSharpContextProviderService([ImportMany] IEnumerable<IContextProvider> providers) | ||
{ | ||
Providers = providers.ToImmutableArray(); | ||
} | ||
|
||
public async IAsyncEnumerable<IContextItem> GetContextItemsAsync(Document document, int position, IReadOnlyDictionary<string, object> activeExperiments, [EnumeratorCancellation] CancellationToken cancellationToken) | ||
{ | ||
var queue = new AsyncQueue<IContextItem>(); | ||
var tasks = this.Providers.Select(provider => Task.Run(async () => | ||
{ | ||
try | ||
{ | ||
await provider.ProvideContextItemsAsync(document, position, activeExperiments, ProvideItemsAsync, cancellationToken).ConfigureAwait(false); | ||
} | ||
catch (Exception exception) when (FatalError.ReportAndCatchUnlessCanceled(exception, ErrorSeverity.General)) | ||
{ | ||
} | ||
}, | ||
cancellationToken)); | ||
|
||
// Let all providers run in parallel in the background, so we can steam results as they come in. | ||
// Complete the queue when all providers are done. | ||
_ = Task.WhenAll(tasks) | ||
.ContinueWith((_, __) => queue.Complete(), | ||
null, | ||
cancellationToken, | ||
TaskContinuationOptions.ExecuteSynchronously, | ||
TaskScheduler.Default); | ||
|
||
while (true) | ||
{ | ||
IContextItem item; | ||
try | ||
{ | ||
item = await queue.DequeueAsync(cancellationToken).ConfigureAwait(false); | ||
} | ||
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested) | ||
{ | ||
// Dequeue is cancelled because the queue is empty and completed, we can break out of the loop. | ||
break; | ||
} | ||
|
||
yield return item; | ||
} | ||
|
||
ValueTask ProvideItemsAsync(ImmutableArray<IContextItem> items, CancellationToken cancellationToken) | ||
{ | ||
foreach (var item in items) | ||
{ | ||
queue.Enqueue(item); | ||
} | ||
|
||
return default; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we remove the EA for the handler itself? though that may break the recent prerelease...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm inclined to keep it here a bit longer, at least until we have a release with the new APIs