Skip to content

Commit

Permalink
Merge pull request #4 from kurokobo/plugin
Browse files Browse the repository at this point in the history
feat: add plugin support
  • Loading branch information
kurokobo authored Jan 9, 2025
2 parents 44b6ae5 + 12c5fce commit de69f38
Show file tree
Hide file tree
Showing 35 changed files with 1,223 additions and 254 deletions.
75 changes: 46 additions & 29 deletions PSDify.psd1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@{
RootModule = 'PSDify.psm1'
ModuleVersion = '0.2.0'
ModuleVersion = '0.3.0'
CompatiblePSEditions = @('Core', 'Desktop')
GUID = 'b791c4c0-ed46-4561-8713-5d4242e6bac7'
Author = 'kurokobo'
Expand All @@ -11,44 +11,61 @@
PowerShellVersion = '5.1'

FunctionsToExport = @(
'Add-DifyDocument',
'Add-DifyFile',
'Wait-Dify'
'Initialize-Dify',

'Connect-Dify',
'Disconnect-Dify',
'Export-DifyApp',
'Get-DifyApp',
'Get-DifyAppAPIKey',
'Get-DifyAppTag',
'Get-DifyDocument',
'Get-DifyDocumentIndexingStatus',
'Get-DifyDSLContent',
'Get-DifyKnowledge',
'Get-DifyKnowledgeTag',

'Get-DifyMember',
'New-DifyMember',
'Set-DifyMemberRole',
'Remove-DifyMember',

'Find-DifyPlugin',
'Get-DifyPlugin',
'Install-DifyPlugin',
'Uninstall-DifyPlugin',
'Get-DifyPluginInstallationStatus',

'Get-DifyModel',
'Get-DifyProfile',
'New-DifyModel',
'Remove-DifyModel',

'Get-DifySystemModel',
'Get-DifyTag',
'Get-DifyVersion',
'Import-DifyApp',
'Initialize-Dify',
'Invoke-DifyRestMethod',
'New-DifyAppAPIKey',
'Set-DifySystemModel',

'Get-DifyKnowledge',
'New-DifyKnowledge',
'New-DifyMember',
'New-DifyModel',
'Remove-DifyKnowledge',

'Get-DifyDocument',
'Add-DifyDocument',
'Remove-DifyDocument',
'Get-DifyDocumentIndexingStatus',

'Get-DifyApp',
'Remove-DifyApp',
'Import-DifyApp',
'Export-DifyApp',
'Get-DifyDSLContent',
'Set-DifyDSLContent',
'Get-DifyAppAPIKey',
'New-DifyAppAPIKey',
'Remove-DifyAppAPIKey',
'Remove-DifyDocument',
'Remove-DifyKnowledge',
'Remove-DifyMember',
'Remove-DifyModel',

'Send-DifyChatMessage',
'Set-DifyDSLContent',
'Set-DifyMemberRole',
'Set-DifySystemModel',

'Get-DifyTag',
'Get-DifyAppTag',
'Get-DifyKnowledgeTag',

'Get-DifyVersion',
'Get-DifyProfile',

'Set-PSDifyConfiguration',
'Wait-Dify'
'Add-DifyFile',
'Invoke-DifyRestMethod'
)
CmdletsToExport = @()
AliasesToExport = @()
Expand Down
9 changes: 8 additions & 1 deletion Public/Connect-Dify.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function Connect-Dify {
)

# Validate existing tokens
if (-not $Force) {
if (-not $Force -and $env:PSDIFY_URL -and $env:PSDIFY_CONSOLE_TOKEN -and (-not $Server -or $Server -eq $env:PSDIFY_URL)) {
try {
$DifyProfile = Get-DifyProfile
$DifyVersion = Get-DifyVersion
Expand Down Expand Up @@ -149,6 +149,13 @@ function Connect-Dify {
$DifyVersion = Get-DifyVersion

$env:PSDIFY_VERSION = $DifyVersion.Version
if ($DifyVersion.PluginSupport) {
$env:PSDIFY_PLUGIN_SUPPORT = "true"
}
else {
$env:PSDIFY_PLUGIN_SUPPORT = $null
}
$env:PSDIFY_MARKETPLACE_API_PREFIX = $null

return [PSCustomObject]@{
"Server" = $env:PSDIFY_URL
Expand Down
19 changes: 11 additions & 8 deletions Public/Disconnect-Dify.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ function Disconnect-Dify {
throw "Failed to logout"
}

Remove-Item Env:\PSDIFY_URL -ErrorAction SilentlyContinue
Remove-Item Env:\PSDIFY_AUTH_METHOD -ErrorAction SilentlyContinue
Remove-Item Env:\PSDIFY_EMAIL -ErrorAction SilentlyContinue
Remove-Item Env:\PSDIFY_PASSWORD -ErrorAction SilentlyContinue
Remove-Item Env:\PSDIFY_CONSOLE_TOKEN -ErrorAction SilentlyContinue
Remove-Item Env:\PSDIFY_CONSOLE_REFRESH_TOKEN -ErrorAction SilentlyContinue
Remove-Item Env:\PSDIFY_VERSION -ErrorAction SilentlyContinue
Remove-Item Env:\PSDIFY_DISABLE_SSL_VERIFICATION -ErrorAction SilentlyContinue
$env:PSDIFY_URL = $null
$env:PSDIFY_AUTH_METHOD = $null
$env:PSDIFY_EMAIL = $null
$env:PSDIFY_PASSWORD = $null
$env:PSDIFY_CONSOLE_TOKEN = $null
$env:PSDIFY_CONSOLE_REFRESH_TOKEN = $null
$env:PSDIFY_VERSION = $null
$env:PSDIFY_DISABLE_SSL_VERIFICATION = $null

$env:PSDIFY_PLUGIN_SUPPORT = $null
$env:PSDIFY_MARKETPLACE_API_PREFIX = $null
}
131 changes: 131 additions & 0 deletions Public/Find-DifyPlugin.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
function Find-DifyPlugin {
[CmdletBinding()]
param(
[String] $Category = "",
[String] $Id = "",
[String] $Name = "",
[String] $Search = ""
)

if (-not $env:PSDIFY_PLUGIN_SUPPORT) {
throw "The Dify server currently logged in does not support plugins."
}

$ValidCategories = @("model", "tool", "agent", "extension", "bundle")
if ($Category -and $Category -notin $ValidCategories) {
throw "Invalid value for Category. Must be one of: $($ValidCategories -join ', ')"
}
if ($Category -eq "agent") {
$Category = "agent_strategy"
}

# find the marketplace api prefix
if (-not $env:PSDIFY_MARKETPLACE_API_PREFIX) {
$Endpoint = Join-Url -Segments @($env:PSDIFY_URL, "/signin")
$Response = Invoke-WebRequest -Uri $Endpoint -UseBasicParsing
$MarketPlaceApiPrefix = $Response.Content -match 'data-marketplace-api-prefix="([^"]+)"'
if ($MarketPlaceApiPrefix) {
$MarketPlaceApiPrefix = $Matches[1].Trim()
}
else {
throw "Could not find the marketplace api prefix."
}
$env:PSDIFY_MARKETPLACE_API_PREFIX = $MarketPlaceApiPrefix
}

# get available plugins
$Plugins = @()
if ($Id) {
$Endpoint = Join-Url -Segments @($env:PSDIFY_MARKETPLACE_API_PREFIX, "/plugins/batch")
$Method = "POST"
$Body = @{
"plugin_ids" = @($Id)
} | ConvertTo-Json
try {
$Response = Invoke-DifyRestMethod -Uri $Endpoint -Method $Method -Body $Body
}
catch {
throw "Failed to obtain plugins: $_"
}
$PluginsInPage = $Response.data.plugins
foreach ($Plugin in $PluginsInPage) {
$Plugins += [PSCustomObject]@{
Category = $Plugin.category
Name = $Plugin.name
Type = $Plugin.type
DisplayName = $Plugin.label.en_US
Id = $Plugin.plugin_id
Brief = $Plugin.brief.en_US
InstallCount = $Plugin.install_count
UpdatedAt = $Plugin.version_updated_at
LatestVersion = $Plugin.latest_version
LatestPackageIdentifier = $Plugin.latest_package_identifier
}
}
}
else {
$Endpoint = Join-Url -Segments @($env:PSDIFY_MARKETPLACE_API_PREFIX, "/plugins/search/basic")
$Method = "POST"
$Page = 1
$PageSize = 100
$HasMore = $true

while ($HasMore) {
$Body = @{
"page" = $Page
"page_size" = $PageSize
}
if ($Search) {
$Body.query = $Search
}
if ($Category) {
$Body.category = $Category
}
$Body = $Body | ConvertTo-Json

try {
$Response = Invoke-DifyRestMethod -Uri $Endpoint -Method $Method -Body $Body
}
catch {
throw "Failed to obtain plugins: $_"
}
if ($Response.code -ne 0) {
throw "Failed to obtain plugins: $($Response.msg)"
}

$PluginsInPage = $Response.data.plugins
if (-not $PluginsInPage) {
$HasMore = $false
}
foreach ($Plugin in $PluginsInPage) {
$Plugins += [PSCustomObject]@{
Category = $Plugin.category
Name = $Plugin.name
Type = $Plugin.type
DisplayName = $Plugin.label.en_US
Id = $Plugin.plugin_id
Brief = $Plugin.brief.en_US
InstallCount = $Plugin.install_count
UpdatedAt = $Plugin.version_updated_at
LatestVersion = $Plugin.latest_version
LatestPackageIdentifier = $Plugin.latest_package_identifier
}
}

if ($Response.data.total -lt $PageSize) {
$HasMore = $false
}
else {
$Page++
}
}
if ($Name) {
$Plugins = $Plugins | Where-Object { $_.Name -in $Name }
}
if ($Search) {
$Plugins = $Plugins | Where-Object { $_.Id -like "*$($Search)*" -or $_.Name -like "*$($Search)*" -or $_.DisplayName -like "*$($Search)*" }
}
}

return $Plugins
}
64 changes: 44 additions & 20 deletions Public/Get-DifyModel.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ function Get-DifyModel {
[String[]] $Provider = @(),
[String[]] $From = @(),
[String[]] $Name = @(),
[String[]] $Type = @()
[String[]] $Type = @(),
[Switch] $Active,
[Switch] $NoConfigure
)

$ValidFroms = @("predefined", "customizable")
Expand All @@ -24,25 +26,40 @@ function Get-DifyModel {
}
}

$Models = @()
if ($Provider) {
$Endpoint = Join-Url -Segments @($env:PSDIFY_URL, "/console/api/workspaces/current/model-providers", $Provider, "/models")
$Method = "GET"
try {
$Response = Invoke-DifyRestMethod -Uri $Endpoint -Method $Method -Token $env:PSDIFY_CONSOLE_TOKEN
}
catch {
throw "Failed to get models from provider: $_"
$FormattedProviders = @()
if ($env:PSDIFY_PLUGIN_SUPPORT) {
foreach ($ProviderObj in $Provider) {
if ($ProviderObj -notmatch "/") {
$ProviderObj = "langgenius/$($ProviderObj)/$($ProviderObj)"
}
$FormattedProviders += $ProviderObj
}
}
else {
$FormattedProviders = $Provider
}

$Models = @()
if ($FormattedProviders) {
foreach ($FormattedProvider in $FormattedProviders) {
$Endpoint = Join-Url -Segments @($env:PSDIFY_URL, "/console/api/workspaces/current/model-providers", $FormattedProvider, "/models")
$Method = "GET"
try {
$Response = Invoke-DifyRestMethod -Uri $Endpoint -Method $Method -Token $env:PSDIFY_CONSOLE_TOKEN
}
catch {
throw "Failed to get models from provider: $_"
}

foreach ($Model in $Response.data) {
$Models += [PSCustomObject]@{
"Provider" = $Model.provider.provider
"Model" = $Model.model
"Type" = $Model.model_type
"FetchFrom" = $Model.fetch_from
"Deprecated" = $Model.deprecated
"Status" = $Model.status
foreach ($Model in $Response.data) {
$Models += [PSCustomObject]@{
"Provider" = $Model.provider.provider
"Model" = $Model.model
"Type" = $Model.model_type
"FetchFrom" = $Model.fetch_from
"Deprecated" = $Model.deprecated
"Status" = $Model.status
}
}
}
}
Expand Down Expand Up @@ -75,8 +92,8 @@ function Get-DifyModel {
}
}

if ($Provider) {
$Models = $Models | Where-Object { $_.Provider -in $Provider }
if ($FormattedProviders) {
$Models = $Models | Where-Object { $_.Provider -in $FormattedProviders }
}
if ($From) {
$From = $From | ForEach-Object { "$($_)-model" }
Expand All @@ -89,5 +106,12 @@ function Get-DifyModel {
$Models = $Models | Where-Object { $_.Type -in $Type }
}

if ($Active) {
$Models = $Models | Where-Object { $_.Status -eq "active" }
}
if ($NoConfigure) {
$Models = $Models | Where-Object { $_.Status -eq "no-configure" }
}

return $Models
}
Loading

0 comments on commit de69f38

Please sign in to comment.