-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from kurokobo/plugin
feat: add plugin support
- Loading branch information
Showing
35 changed files
with
1,223 additions
and
254 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,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 | ||
} |
This file contains 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.