-
Notifications
You must be signed in to change notification settings - Fork 4.1k
[17.14] Move Copilot context provider to EA.Copilot and handler to LanguageServer #78322
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
base: release/dev17.14
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. primary constructor? |
||
{ | ||
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; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can just be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. consider doc'ing these. |
||
} |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's teh |
||
} |
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))] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ni: call these CodeSnippetContextItem and TraitContextItem to make it clearer what they are. |
||
internal interface IContextItem | ||
{ | ||
} |
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why pass an array, instead of just an itemt at a time, if it's a callback? |
||
CancellationToken cancellationToken); | ||
} |
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; | ||
} |
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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what does 'trait' mean here? |
||||||
{ | ||||||
public TraitItem(string name, string value, int importance = Completion.Importance.Default) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. primary constructor, with attribute on params. |
||||||
{ | ||||||
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; } | ||||||
} |
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))] | ||||||||
Comment on lines
+21
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
nit. |
||||||||
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(); | ||||||||
} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. primary constructor. |
||||||||
|
||||||||
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)); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we have a better api for this. ProducerConsumer. You can write this as:
|
||||||||
|
||||||||
// 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; | ||||||||
} | ||||||||
} | ||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
<!-- | ||
⚠ ONLY COPILOT ASSEMBLIES MAY BE ADDED HERE ⚠ | ||
--> | ||
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.LanguageServer" /> | ||
<InternalsVisibleTo Include="Microsoft.VisualStudio.Copilot.CodeMappers.CSharp" Key="$(CopilotKey)" /> | ||
<InternalsVisibleTo Include="Microsoft.VisualStudio.Copilot.Roslyn" Key="$(CopilotKey)" /> | ||
<InternalsVisibleTo Include="Microsoft.VisualStudio.Copilot.Roslyn.LanguageServer" Key="$(CopilotKey)" /> | ||
|
@@ -29,6 +30,10 @@ | |
<ProjectReference Include="..\..\Core\Portable\Microsoft.CodeAnalysis.Features.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.VisualStudio.Threading" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this seems undesirable. this will also make it so we can't use this code in vscode. i dont' see anything that would make this necessary. |
||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PublicAPI Include="PublicAPI.Shipped.txt" /> | ||
<PublicAPI Include="PublicAPI.Unshipped.txt" /> | ||
|
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.