-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add SharePoint Knowledge Agent - AI Agent for Document-Based Knowledge Management #30
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
Open
devin-ai-integration
wants to merge
1
commit into
main
Choose a base branch
from
devin/1777564158-sharepoint-knowledge-agent
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
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 @@ | ||
| FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base | ||
| WORKDIR /app | ||
| EXPOSE 8080 | ||
|
|
||
| FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build | ||
| WORKDIR /src | ||
| COPY ["KnowledgeAgent.API/KnowledgeAgent.API.csproj", "KnowledgeAgent.API/"] | ||
| COPY ["KnowledgeAgent.Core/KnowledgeAgent.Core.csproj", "KnowledgeAgent.Core/"] | ||
| COPY ["KnowledgeAgent.Infrastructure/KnowledgeAgent.Infrastructure.csproj", "KnowledgeAgent.Infrastructure/"] | ||
| RUN dotnet restore "KnowledgeAgent.API/KnowledgeAgent.API.csproj" | ||
| COPY . . | ||
| WORKDIR "/src/KnowledgeAgent.API" | ||
| RUN dotnet build "KnowledgeAgent.API.csproj" -c Release -o /app/build | ||
|
|
||
| FROM build AS publish | ||
| RUN dotnet publish "KnowledgeAgent.API.csproj" -c Release -o /app/publish /p:UseAppHost=false | ||
|
|
||
| FROM base AS final | ||
| WORKDIR /app | ||
| COPY --from=publish /app/publish . | ||
| ENTRYPOINT ["dotnet", "KnowledgeAgent.API.dll"] |
96 changes: 96 additions & 0 deletions
96
src/Services/KnowledgeAgent/KnowledgeAgent.API/Controllers/AgentController.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,96 @@ | ||
| using KnowledgeAgent.API.DTOs; | ||
| using KnowledgeAgent.Core.Interfaces; | ||
| using KnowledgeAgent.Core.Models; | ||
| using Microsoft.AspNetCore.Mvc; | ||
|
|
||
| namespace KnowledgeAgent.API.Controllers; | ||
|
|
||
| [ApiController] | ||
| [Route("api/[controller]")] | ||
| public class AgentController : ControllerBase | ||
| { | ||
| private readonly IKnowledgeAgentOrchestrator _orchestrator; | ||
| private readonly ILogger<AgentController> _logger; | ||
|
|
||
| public AgentController( | ||
| IKnowledgeAgentOrchestrator orchestrator, | ||
| ILogger<AgentController> logger) | ||
| { | ||
| _orchestrator = orchestrator; | ||
| _logger = logger; | ||
| } | ||
|
|
||
| [HttpPost("sync")] | ||
| [ProducesResponseType(StatusCodes.Status202Accepted)] | ||
| public Task<IActionResult> TriggerSync(CancellationToken cancellationToken) | ||
| { | ||
| _logger.LogInformation("Manual SharePoint sync triggered"); | ||
|
|
||
| // Run sync in background, return immediately | ||
| _ = Task.Run(async () => | ||
| { | ||
| try | ||
| { | ||
| await _orchestrator.SyncSharePointDocumentsAsync(cancellationToken); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| _logger.LogError(ex, "Error during manual sync"); | ||
| } | ||
| }, cancellationToken); | ||
|
|
||
| return Task.FromResult<IActionResult>(Accepted(new { message = "SharePoint document sync initiated" })); | ||
| } | ||
|
|
||
| [HttpPost("monitor")] | ||
| [ProducesResponseType(StatusCodes.Status202Accepted)] | ||
| public Task<IActionResult> TriggerMonitoring(CancellationToken cancellationToken) | ||
| { | ||
| _logger.LogInformation("Manual monitoring cycle triggered"); | ||
|
|
||
| _ = Task.Run(async () => | ||
| { | ||
| try | ||
| { | ||
| await _orchestrator.RunMonitoringCycleAsync(cancellationToken); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| _logger.LogError(ex, "Error during manual monitoring cycle"); | ||
| } | ||
| }, cancellationToken); | ||
|
|
||
| return Task.FromResult<IActionResult>(Accepted(new { message = "Monitoring cycle initiated" })); | ||
| } | ||
|
|
||
| [HttpPost("alerts/webhook")] | ||
| [ProducesResponseType(StatusCodes.Status200OK)] | ||
| [ProducesResponseType(StatusCodes.Status400BadRequest)] | ||
| public async Task<IActionResult> HandleAlertWebhook( | ||
| [FromBody] AlertWebhookRequest request, CancellationToken cancellationToken) | ||
| { | ||
| _logger.LogInformation("Received alert webhook: {AlertId} - {AlertName}", | ||
| request.AlertId, request.AlertName); | ||
|
|
||
| if (string.IsNullOrWhiteSpace(request.AlertId)) | ||
| return BadRequest("AlertId is required"); | ||
|
|
||
| var alert = new AppInsightAlert | ||
| { | ||
| AlertId = request.AlertId, | ||
| AlertName = request.AlertName, | ||
| Severity = request.Severity, | ||
| Description = request.Description, | ||
| AffectedResource = request.AffectedResource, | ||
| ExceptionType = request.ExceptionType, | ||
| ExceptionMessage = request.ExceptionMessage, | ||
| StackTrace = request.StackTrace, | ||
| FiredAt = DateTime.UtcNow, | ||
| CustomProperties = request.CustomProperties ?? new Dictionary<string, string>() | ||
| }; | ||
|
|
||
| await _orchestrator.ProcessAlertAsync(alert, cancellationToken); | ||
|
|
||
| return Ok(new { message = "Alert processed successfully", alertId = alert.AlertId }); | ||
| } | ||
| } | ||
21 changes: 21 additions & 0 deletions
21
src/Services/KnowledgeAgent/KnowledgeAgent.API/Controllers/HealthController.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 @@ | ||
| using Microsoft.AspNetCore.Mvc; | ||
|
|
||
| namespace KnowledgeAgent.API.Controllers; | ||
|
|
||
| [ApiController] | ||
| [Route("api/[controller]")] | ||
| public class HealthController : ControllerBase | ||
| { | ||
| [HttpGet] | ||
| [ProducesResponseType(StatusCodes.Status200OK)] | ||
| public IActionResult GetHealth() | ||
| { | ||
| return Ok(new | ||
| { | ||
| status = "Healthy", | ||
| service = "SharePoint Knowledge Agent", | ||
| version = "1.0.0", | ||
| timestamp = DateTime.UtcNow | ||
| }); | ||
| } | ||
| } |
59 changes: 59 additions & 0 deletions
59
src/Services/KnowledgeAgent/KnowledgeAgent.API/Controllers/KnowledgeController.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,59 @@ | ||
| using KnowledgeAgent.API.DTOs; | ||
| using KnowledgeAgent.Core.Interfaces; | ||
| using Microsoft.AspNetCore.Mvc; | ||
|
|
||
| namespace KnowledgeAgent.API.Controllers; | ||
|
|
||
| [ApiController] | ||
| [Route("api/[controller]")] | ||
| public class KnowledgeController : ControllerBase | ||
| { | ||
| private readonly IKnowledgeArticleService _knowledgeArticleService; | ||
| private readonly ILogger<KnowledgeController> _logger; | ||
|
|
||
| public KnowledgeController( | ||
| IKnowledgeArticleService knowledgeArticleService, | ||
| ILogger<KnowledgeController> logger) | ||
| { | ||
| _knowledgeArticleService = knowledgeArticleService; | ||
| _logger = logger; | ||
| } | ||
|
|
||
| [HttpPost("search")] | ||
| [ProducesResponseType(StatusCodes.Status200OK)] | ||
| [ProducesResponseType(StatusCodes.Status400BadRequest)] | ||
| public async Task<IActionResult> SearchKnowledge( | ||
| [FromBody] SearchRequest request, CancellationToken cancellationToken) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(request.Query)) | ||
| return BadRequest("Query cannot be empty"); | ||
|
|
||
| _logger.LogInformation("Knowledge search request: {Query}", request.Query); | ||
|
|
||
| var results = await _knowledgeArticleService.SearchKnowledgeAsync( | ||
| request.Query, request.TopK, cancellationToken); | ||
|
|
||
| return Ok(results); | ||
| } | ||
|
|
||
| [HttpGet("articles")] | ||
| [ProducesResponseType(StatusCodes.Status200OK)] | ||
| public async Task<IActionResult> GetAllArticles(CancellationToken cancellationToken) | ||
| { | ||
| var articles = await _knowledgeArticleService.GetAllAsync(cancellationToken); | ||
| return Ok(articles); | ||
| } | ||
|
|
||
| [HttpGet("articles/{id:guid}")] | ||
| [ProducesResponseType(StatusCodes.Status200OK)] | ||
| [ProducesResponseType(StatusCodes.Status404NotFound)] | ||
| public async Task<IActionResult> GetArticle(Guid id, CancellationToken cancellationToken) | ||
| { | ||
| var article = await _knowledgeArticleService.GetByIdAsync(id, cancellationToken); | ||
|
|
||
| if (article == null) | ||
| return NotFound(); | ||
|
|
||
| return Ok(article); | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/Services/KnowledgeAgent/KnowledgeAgent.API/DTOs/AlertWebhookRequest.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,14 @@ | ||
| namespace KnowledgeAgent.API.DTOs; | ||
|
|
||
| public class AlertWebhookRequest | ||
| { | ||
| public string AlertId { get; set; } = string.Empty; | ||
| public string AlertName { get; set; } = string.Empty; | ||
| public string Severity { get; set; } = string.Empty; | ||
| public string Description { get; set; } = string.Empty; | ||
| public string AffectedResource { get; set; } = string.Empty; | ||
| public string ExceptionType { get; set; } = string.Empty; | ||
| public string ExceptionMessage { get; set; } = string.Empty; | ||
| public string StackTrace { get; set; } = string.Empty; | ||
| public Dictionary<string, string>? CustomProperties { get; set; } | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/Services/KnowledgeAgent/KnowledgeAgent.API/DTOs/SearchRequest.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,7 @@ | ||
| namespace KnowledgeAgent.API.DTOs; | ||
|
|
||
| public class SearchRequest | ||
| { | ||
| public string Query { get; set; } = string.Empty; | ||
| public int TopK { get; set; } = 5; | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/Services/KnowledgeAgent/KnowledgeAgent.API/DTOs/SyncRequest.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,7 @@ | ||
| namespace KnowledgeAgent.API.DTOs; | ||
|
|
||
| public class SyncRequest | ||
| { | ||
| public string? SiteId { get; set; } | ||
| public string? DriveId { get; set; } | ||
| } |
16 changes: 16 additions & 0 deletions
16
src/Services/KnowledgeAgent/KnowledgeAgent.API/KnowledgeAgent.API.csproj
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,16 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <ProjectReference Include="..\KnowledgeAgent.Core\KnowledgeAgent.Core.csproj" /> | ||
| <ProjectReference Include="..\KnowledgeAgent.Infrastructure\KnowledgeAgent.Infrastructure.csproj" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" /> | ||
| <PackageReference Include="Swashbuckle.AspNetCore" Version="6.9.0" /> | ||
| <PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" /> | ||
| </ItemGroup> | ||
| </Project> |
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,58 @@ | ||
| using KnowledgeAgent.Core.Interfaces; | ||
| using KnowledgeAgent.Infrastructure; | ||
|
|
||
| var builder = WebApplication.CreateBuilder(args); | ||
|
|
||
| // Add Application Insights telemetry | ||
| builder.Services.AddApplicationInsightsTelemetry(options => | ||
| { | ||
| options.ConnectionString = builder.Configuration["AppInsights:ConnectionString"]; | ||
| }); | ||
|
|
||
| // Add Knowledge Agent infrastructure services | ||
| builder.Services.AddKnowledgeAgentInfrastructure(builder.Configuration); | ||
|
|
||
| // Add controllers and Swagger | ||
| builder.Services.AddControllers(); | ||
| builder.Services.AddEndpointsApiExplorer(); | ||
| builder.Services.AddSwaggerGen(options => | ||
| { | ||
| options.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo | ||
| { | ||
| Title = "SharePoint Knowledge Agent API", | ||
| Version = "v1", | ||
| Description = "AI Agent that reads SharePoint documents, creates Knowledge Articles, " + | ||
| "stores them in a vector database, and integrates with Application Insights " + | ||
| "to provide relevant knowledge when issues are detected." | ||
| }); | ||
| }); | ||
|
|
||
| // Add health checks | ||
| builder.Services.AddHealthChecks(); | ||
|
|
||
| var app = builder.Build(); | ||
|
|
||
| // Initialize vector store on startup | ||
| using (var scope = app.Services.CreateScope()) | ||
| { | ||
| var vectorStore = scope.ServiceProvider.GetRequiredService<IVectorStoreService>(); | ||
| await vectorStore.InitializeAsync(); | ||
| } | ||
|
|
||
| // Configure middleware | ||
| if (app.Environment.IsDevelopment()) | ||
| { | ||
| app.UseSwagger(); | ||
| app.UseSwaggerUI(c => | ||
| { | ||
| c.SwaggerEndpoint("/swagger/v1/swagger.json", "Knowledge Agent API v1"); | ||
| c.RoutePrefix = string.Empty; | ||
| }); | ||
| } | ||
|
|
||
| app.UseHttpsRedirection(); | ||
| app.UseAuthorization(); | ||
| app.MapControllers(); | ||
| app.MapHealthChecks("/health"); | ||
|
|
||
| app.Run(); |
16 changes: 16 additions & 0 deletions
16
src/Services/KnowledgeAgent/KnowledgeAgent.API/appsettings.Development.json
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,16 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Debug", | ||
| "Microsoft.AspNetCore": "Information", | ||
| "KnowledgeAgent": "Trace" | ||
| } | ||
| }, | ||
| "SharePoint": { | ||
| "SyncIntervalMinutes": 5 | ||
| }, | ||
| "AppInsights": { | ||
| "MonitoringIntervalMinutes": 2, | ||
| "AlertLookbackMinutes": 30 | ||
| } | ||
| } |
54 changes: 54 additions & 0 deletions
54
src/Services/KnowledgeAgent/KnowledgeAgent.API/appsettings.json
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,54 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning", | ||
| "KnowledgeAgent": "Debug" | ||
| } | ||
| }, | ||
| "AllowedHosts": "*", | ||
| "SharePoint": { | ||
| "TenantId": "<your-tenant-id>", | ||
| "ClientId": "<your-client-id>", | ||
| "ClientSecret": "<your-client-secret>", | ||
| "SiteId": "<your-sharepoint-site-id>", | ||
| "DriveId": "<your-sharepoint-drive-id>", | ||
| "FolderPath": "/Documents/KnowledgeBase", | ||
| "SyncIntervalMinutes": 60 | ||
| }, | ||
| "VectorStore": { | ||
| "Host": "localhost", | ||
| "Port": 6334, | ||
| "CollectionName": "knowledge_articles", | ||
| "VectorSize": 1536, | ||
| "ApiKey": "" | ||
| }, | ||
| "OpenAI": { | ||
| "ApiKey": "<your-openai-api-key>", | ||
| "Endpoint": "https://<your-resource>.openai.azure.com/", | ||
| "EmbeddingModel": "text-embedding-ada-002", | ||
| "CompletionModel": "gpt-4", | ||
| "UseAzureOpenAI": true, | ||
| "AzureDeploymentName": "<your-deployment-name>" | ||
| }, | ||
| "AppInsights": { | ||
| "ConnectionString": "<your-app-insights-connection-string>", | ||
| "InstrumentationKey": "<your-instrumentation-key>", | ||
| "ApplicationId": "<your-application-id>", | ||
| "ApiKey": "<your-api-key>", | ||
| "MonitoringIntervalMinutes": 5, | ||
| "AlertLookbackMinutes": 15 | ||
| }, | ||
| "Email": { | ||
| "SmtpHost": "smtp.office365.com", | ||
| "SmtpPort": 587, | ||
| "SmtpUsername": "<your-smtp-username>", | ||
| "SmtpPassword": "<your-smtp-password>", | ||
| "UseSsl": true, | ||
| "FromAddress": "knowledge-agent@yourdomain.com", | ||
| "FromName": "Knowledge Agent", | ||
| "DefaultRecipients": [ | ||
| "team@yourdomain.com" | ||
| ] | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/Services/KnowledgeAgent/KnowledgeAgent.Core/Enums/ArticleStatus.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,10 @@ | ||
| namespace KnowledgeAgent.Core.Models; | ||
|
|
||
| public enum ArticleStatus | ||
| { | ||
| Draft, | ||
| Processing, | ||
| Indexed, | ||
| Failed, | ||
| Archived | ||
| } |
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.
🔴 Request-scoped CancellationToken passed to fire-and-forget Task.Run cancels background work
In
TriggerSyncandTriggerMonitoring, thecancellationTokenparameter is bound to the HTTP request'sHttpContext.RequestAborted. Since the controller returnsAccepted()immediately, the HTTP connection closes shortly after, and ASP.NET Core signals the cancellation token. The backgroundTask.Runreceives this token both as its scheduler token (line 40/61) and passes it into the orchestrator methods (line 34/55), causing the sync/monitoring work to be cancelled almost immediately viaOperationCanceledException.Prompt for agents
Was this helpful? React with 👍 or 👎 to provide feedback.