Skip to content

Commit

Permalink
Added Get-PnPCopilotAgent cmdlet
Browse files Browse the repository at this point in the history
  • Loading branch information
erwinvanhunen committed Dec 6, 2024
1 parent c2d0288 commit af9eed3
Show file tree
Hide file tree
Showing 5 changed files with 300 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Added `-List` parameter to `Get-PnPFolderItem`, `Get-PnPFileInFolder` and `Get-PnPFolderInFolder` which allows them to work with a document library containing more than 5,000 items [#4611](https://github.com/pnp/powershell/pull/4611)
- Added `Start-PnPTraceLog`, `Stop-PnPTraceLog` and `Get-PnPTraceLog` cmdlets to handle tracelogging. Removed `Set-PnPTraceLog` cmdlet.
- Added `-ListPermissionScopes` parameter on `Get-PnPAccessToken` cmdlet to list the current permission scopes on the current access token.
- Added `Get-PnPCopilotAgent` cmdlet that returns the Microsoft Copilot Agents in a site collection

### Changed

Expand Down
105 changes: 105 additions & 0 deletions documentation/Get-PnPCopilotAgent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
---
document type: cmdlet
external help file: PnP.PowerShell.dll-Help.xml
HelpUri: ''
Locale: en-US
Module Name: PnP.PowerShell
ms.date: 12/06/2024
PlatyPS schema version: 2024-05-01
title: Get-PnPCopilotAgent
---

# Get-PnPCopilotAgent

## SYNOPSIS

Returns the Microsoft Copilot Agents (*.agent) in a site collection.

## SYNTAX

### __AllParameterSets

```powershell
Get-PnPCopilotAgent [-ServerRelativeUrl <string>] [-Connection <PnPConnection>] [<CommonParameters>]
```

## DESCRIPTION

This cmdlet iterates through the document libraries in a site and finds the copilot agents in that site.

## EXAMPLES

### Example 1

```powershell
Get-PnPCopilotAgent
```

This will return all the Microsoft Copilot agents in a site.


### Example 2

```powershell
Get-PnPCopilotAgent -ServerRelativeUrl /sites/demo/siteassets/copilots/approved/main.agent
```

This will return the specific Microsoft Copilot agent if it exists.


## PARAMETERS

### -Connection

Optional connection to be used by the cmdlet. Retrieve the value for this parameter by either specifying -ReturnConnection on Connect-PnPOnline or by executing Get-PnPConnection.

```yaml
Type: PnP.PowerShell.Commands.Base.PnPConnection
DefaultValue: ''
SupportsWildcards: false
ParameterValue: []
Aliases: []
ParameterSets:
- Name: (All)
Position: Named
IsRequired: false
ValueFromPipeline: false
ValueFromPipelineByPropertyName: false
ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''
```
### -ServerRelativeUrl
The server relative URL to the .agent file.
```yaml
Type: System.String
DefaultValue: ''
SupportsWildcards: false
ParameterValue: []
Aliases: []
ParameterSets:
- Name: (All)
Position: Named
IsRequired: false
ValueFromPipeline: false
ValueFromPipelineByPropertyName: false
ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''
```
### CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable,
-InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable,
-ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see
[about_CommonParameters](https://go.microsoft.com/fwlink/?LinkID=113216).
## RELATED LINKS
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
32 changes: 31 additions & 1 deletion resources/PnP.PowerShell.Format.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -3090,6 +3090,36 @@
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</View>
<View>
<Name>CopilotAgent</Name>
<ViewSelectedBy>
<TypeName>PnP.PowerShell.Commands.Model.Copilot.CopilotAgent</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>Name</Label>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>FileLocation</Label>
<Alignment>left</Alignment>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<ScriptBlock>$_.CustomCopilotConfig.GPTDefinition.Name</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<PropertyName>ServerRelativeUrl</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
75 changes: 75 additions & 0 deletions src/Commands/Copilot/GetCopilotAgent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System.Management.Automation;
using System.Text.Json;
using Microsoft.SharePoint.Client;
using PnP.PowerShell.Commands.Model.Copilot;
using System.Linq;
using System;

namespace PnP.PowerShell.Commands.Copilot
{
[Cmdlet(VerbsCommon.Get, "PnPCopilotAgent")]
[OutputType("PnP.PowerShell.Commands.Model.Copilot.CopilotAgent")]
public class NewCopilotAgent : PnPWebCmdlet
{
[Parameter(Mandatory = false)]
public string ServerRelativeUrl;

protected override void ExecuteCmdlet()
{

if (ParameterSpecified(nameof(ServerRelativeUrl)))
{
try
{
var agentContents = CurrentWeb.GetFileAsString(ServerRelativeUrl);
var agentObject = JsonSerializer.Deserialize<CopilotAgent>(agentContents);
agentObject.ServerRelativeUrl = ServerRelativeUrl;
WriteObject(agentObject);
}
catch (JsonException)
{
throw new PSNotSupportedException("Cannot extract agent information from contents.");
}
catch (ServerException)
{
throw new PSArgumentException($"Agent with url {ServerRelativeUrl} not found.");
}
}
else
{

// find all doclibraries
var doclibs = ClientContext.LoadQuery(CurrentWeb.Lists.Where(l => l.BaseTemplate == 101));
ClientContext.ExecuteQuery();
var camlQuery = CamlQuery.CreateAllItemsQuery();

camlQuery.ViewXml = "<View Scope=\"RecursiveAll\"><Query><Where><Eq><FieldRef Name=\"File_x0020_Type\" /><Value Type=\"Text\">agent</Value></Eq></Where></Query></View>";
foreach (var doclib in doclibs)
{
camlQuery.ListItemCollectionPosition = null;
do
{
camlQuery.ListItemCollectionPosition = GetAgents(doclib, camlQuery);

} while (camlQuery.ListItemCollectionPosition != null);
}
}
}

private ListItemCollectionPosition GetAgents(List list, CamlQuery camlQuery)
{
var items = list.GetItems(camlQuery);
list.Context.Load(items, i => i.IncludeWithDefaultProperties(li => li.FieldValuesAsText), i => i.ListItemCollectionPosition);
ClientContext.ExecuteQueryRetry();
foreach (var item in items)
{
var agentContents = CurrentWeb.GetFileAsString(item.FieldValuesAsText["FileRef"]);
var agentObject = JsonSerializer.Deserialize<CopilotAgent>(agentContents);
agentObject.ServerRelativeUrl = item.FieldValuesAsText["FileRef"];
WriteObject(agentObject);
}

return items.ListItemCollectionPosition;
}
}
}
88 changes: 88 additions & 0 deletions src/Commands/Model/Copilot/CopilotAgent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace PnP.PowerShell.Commands.Model.Copilot
{
public class CopilotAgent
{
[JsonPropertyName("schemaVersion")]
public string SchemaVersion { get; set; } = "0.2.0";

[JsonPropertyName("customCopilotConfig")]
public CopilotAgentCustomCopilotConfig CustomCopilotConfig { get; set; }

public string ServerRelativeUrl {get;set;}
}

public class CopilotAgentCustomCopilotConfig
{
[JsonPropertyName("conversationStarters")]
public CopilotAgentConversationStarters ConversationStarters { get; set; }

[JsonPropertyName("gptDefinition")]
public CopilotAgentGPTDefinition GPTDefinition { get; set; }

[JsonPropertyName("icon")]
public string Icon { get; set; }
}

public class CopilotAgentConversationStarters
{
[JsonPropertyName("conversationStarterList")]
public List<CopilotAgentTextValue> conversationStarterList { get; set; }

[JsonPropertyName("welcomeMessage")]
public CopilotAgentTextValue WelcomeMessage { get; set; }
}

public class CopilotAgentTextValue
{
[JsonPropertyName("text")]
public string Text { get; set; }
}

public class CopilotAgentGPTDefinition
{
[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("description")]
public string Description { get; set; }

[JsonPropertyName("instructions")]
public string Instructions { get; set; }

[JsonPropertyName("capabilities")]
public List<CopilotAgentCapabilities> Capabilities { get; set; }
}

public class CopilotAgentCapabilities
{
[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("items_by_sharepoint_ids")]
public List<CopilotAgentSourceItem> ItemsBySharePointIds { get; set; }

[JsonPropertyName("items_by_url")]
public List<CopilotAgentSourceItem> ItemsByUrl { get; set; }
}

public class CopilotAgentSourceItem
{
[JsonPropertyName("url")]
public string Url { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("site_id")]
public string SiteId { get; set; }
[JsonPropertyName("web_id")]
public string WebId { get; set; }
[JsonPropertyName("list_id")]
public string ListId { get; set; }
[JsonPropertyName("unique_id")]
public string UniqueId { get; set; }
[JsonPropertyName("type")]
public string Type { get; set; }
}
}

0 comments on commit af9eed3

Please sign in to comment.