Compare commits

..

1 Commits

Author SHA1 Message Date
TyperBody 6c05f3dfcb feat(plugins): show installed state in marketplace and stream install tasks
- report download progress (bytes, speed) and human-readable install stages
  from the plugin runtime connector via the install task context
- add a marketplace installed-state helper so market cards reflect whether a
  plugin is already installed
- surface install progress / queue state in the plugin install-task UI
- extend BackendClient and API entities with install-task status
- i18n for the new marketplace / install strings
2026-09-13 00:02:08 +08:00
22 changed files with 580 additions and 893 deletions
-111
View File
@@ -1,111 +0,0 @@
# Discord release announcements
This independent workflow announces new stable LangBot releases in the channel
selected by a dedicated Discord incoming webhook. It does not change the existing
release/build workflows, edit releases, run a persistent service, poll, or backfill.
Announcements run on publication, independently of artifact builds finishing.
## Setup and read-only validation
1. In the intended community **announcement channel**, create a dedicated incoming
webhook (Channel Settings → Integrations → Webhooks). Copy its URL; do not reuse
a webhook belonging to another automation.
2. In `langbot-app/LangBot` → Settings → Secrets and variables → Actions, create the
**repository secret** `DISCORD_RELEASE_WEBHOOK_URL`. Its value must be exactly
`https://discord.com/api/webhooks/<id>/<token>` — no query, trailing slash,
API-version segment, or alternate domain. Treat the entire URL as a password.
3. Once this workflow is on `master`, open Actions → **Discord Release Announcement**
→ Run workflow, choosing `master`. Alternatively:
```sh
gh workflow run discord-release.yml --repo langbot-app/LangBot --ref master
```
4. Inspect **Validate webhook (GET only, no message)**. It checks webhook type `1`
and reports `guild_id` and `channel_id`; compare both with the intended server
and channel using Discord Developer Mode → Copy ID. The secret determines the
destination; no channel ID is guessed or overridden. The URL/token is never
logged. Dispatch cannot send a test message or announce an old release, even
when run again. Missing/invalid secrets fail validation clearly; offline tests
do not need secrets.
GET validation confirms the webhook's identity, not delivery or notification
permissions. Verify those on the first genuine release. `mention_everyone=true`
confirms Discord parsed the mention; it cannot prove every member received a push
notification (member/server notification settings still apply).
## Activation and message
The workflow and `.github/discord-release/` helper **must be in the commit targeted
by each new release tag**. Merging to `master` does not enable announcements for
old tags whose commits lack these files. Manual dispatch becomes available when
the workflow is on the default branch. Only publish release tags from trusted,
reviewed commits: release workflows execute that tag's code with the secret.
Only `release` events with action `published`, `draft=false`, and
`prerelease=false` can send. Drafts and prereleases are skipped; release edits do
not trigger announcements. The helper requires the repository to be exactly
`langbot-app/LangBot`, a stable `vX.Y.Z` tag (ASCII digits, at most 64 characters),
and its exact canonical GitHub release URL. Other naming schemes fail closed.
Example message (the version and URL come from the validated event file):
```text
@everyone LangBot v4.10.11 is now available!
Release notes: https://github.com/langbot-app/LangBot/releases/tag/v4.10.11
```
The release title/body is never copied. There is one literal `@everyone`, explicit
`allowed_mentions.parse=["everyone"]`, empty user/role allowlists, and no reply
mention. TTS and notification-suppressing flags are disabled. Requests use HTTPS
only to `discord.com`, an explicit User-Agent, and no redirects or automatic
retries. After a webhook identity GET, one `POST ?wait=true` obtains a message ID;
an exact `/messages/<id>` GET verifies its ID, webhook/channel, content,
`mention_everyone=true`, and empty user/role mention arrays before success.
## Repeat guard and manual recovery
Production sending requires **`GITHUB_RUN_ATTEMPT == "1"`**. Any Actions rerun
(including “Re-run failed jobs”) refuses to POST and requires manual reconciliation,
even if the first attempt failed before sending. Read-only dispatch may be rerun.
This is a practical repeat guard, **not durable exactly-once delivery**. It cannot
prevent duplicates from a separate new run/event (for example deleting/recreating
a release), separate automation, or manual posting. It stores no durable dedupe
state and never modifies the release to mark delivery.
If a POST times out, returns an error, or readback fails, the message may already
exist. The workflow fails rather than blindly sending again. A returned message ID
is included in the safe error when available. A runner termination can also leave
an ambiguous send without that log line.
1. Inspect the announcement channel and the failed run logs. Locate the canonical
release link and, if available, the returned message ID. A failed verification
does **not** mean the message was absent.
2. If present, reconcile the existing message/mention problem manually; do not
rerun, create another release event, or send a duplicate ping.
3. If an operator has positively confirmed no message exists, fix the secret or
permission issue and use read-only dispatch to validate configuration. A
maintainer may then post the announcement manually once in Discord and record
the message link in the incident/run notes. Do not override the attempt guard
or delete/recreate a release to force recovery.
4. If absence cannot be established, pause and reconcile rather than resending.
To stop future sends, disable **Discord Release Announcement** in Actions. Rotate
or delete the dedicated Discord webhook if the URL is exposed, and update the
secret before validation. No rollback of release artifacts is involved.
## Local checks
Requires Python 3.11+ and the standard library only:
```sh
python3 -m unittest discover -s .github/discord-release -p 'test_*.py' -v
python3 -m py_compile .github/discord-release/announce.py .github/discord-release/test_announce.py
```
Tests exercise policy, CLI/event-file handling, mention payloads, hostile inputs,
HTTP failures, exact message readback, and refusal to retry. Only the HTTPS
transport is mocked for Discord tests; no live Discord requests or messages are
made. Changes to this directory or its workflow run the offline tests on push and
pull request; tests also gate release sending and read-only dispatch validation.
-162
View File
@@ -1,162 +0,0 @@
"""Announce only first-attempt stable releases; dispatch is read-only validation."""
import http.client
import json
import os
from pathlib import Path
import re
import sys
REPOSITORY = 'langbot-app/LangBot'
RELEASE_PREFIX = f'https://github.com/{REPOSITORY}/releases/tag/'
RECONCILE = (
'Do not resend or bypass the run-attempt guard; manual reconciliation is required. '
'Inspect the announcement channel and workflow logs before any manual recovery '
'(see .github/discord-release/README.md).'
)
class AnnouncementError(Exception):
"""A safe, operator-facing error containing no webhook URL or response body."""
def release_payload(event, attempt):
"""Return a bounded, mention-safe payload, or None for draft/preview releases."""
if not isinstance(event, dict) or event.get('action') != 'published':
raise AnnouncementError('Only release.published events are accepted.')
repository = event.get('repository')
if not isinstance(repository, dict) or repository.get('full_name') != REPOSITORY:
raise AnnouncementError('Unexpected release repository.')
release = event.get('release')
if not isinstance(release, dict) or any(type(release.get(key)) is not bool for key in ('draft', 'prerelease')):
raise AnnouncementError('Invalid release flags.')
if release['draft'] or release['prerelease']:
return None
if attempt != '1':
raise AnnouncementError(f'Release reruns or missing run attempts are refused. {RECONCILE}')
tag = release.get('tag_name')
if not isinstance(tag, str) or len(tag) > 64 or not re.fullmatch(r'v[0-9]+\.[0-9]+\.[0-9]+', tag):
raise AnnouncementError('Expected a stable release tag in vX.Y.Z format (at most 64 characters).')
url = RELEASE_PREFIX + tag
if release.get('html_url') != url:
raise AnnouncementError('Release URL must be the canonical LangBot release URL matching its tag.')
return {
'content': f'@everyone LangBot {tag} is now available!\nRelease notes: {url}',
'allowed_mentions': {'parse': ['everyone'], 'users': [], 'roles': [], 'replied_user': False},
'tts': False,
'flags': 0,
}
def is_snowflake(value):
return isinstance(value, str) and re.fullmatch(r'[0-9]{1,20}', value) is not None
class DiscordWebhook:
def __init__(self, url):
if not url:
raise AnnouncementError('DISCORD_RELEASE_WEBHOOK_URL is missing. Set the repository Actions secret.')
match = re.fullmatch(r'https://discord\.com(/api/webhooks/([0-9]{1,20})/[A-Za-z0-9_-]+)', url)
if not match:
raise AnnouncementError('Invalid webhook URL; expected https://discord.com/api/webhooks/<id>/<token>.')
self.path, self.id = match.groups()
def _request(self, method, suffix='', payload=None):
# Direct HTTPS, default certificate verification, no proxies or redirect/retry machinery.
connection = http.client.HTTPSConnection('discord.com', timeout=20)
try:
body = json.dumps(payload).encode('utf-8') if payload is not None else None
connection.request(
method,
self.path + suffix,
body=body,
headers={'Content-Type': 'application/json', 'User-Agent': 'LangBot-Release-Announcements/1.0'},
)
response = connection.getresponse()
if response.status != 200:
raise AnnouncementError(f'Discord {method} returned HTTP {response.status}; no retry was attempted.')
raw = response.read(1_048_577)
if len(raw) > 1_048_576:
raise AnnouncementError('Discord response exceeded the size limit.')
return json.loads(raw)
except (OSError, http.client.HTTPException, ValueError, UnicodeError):
# Exceptions and bodies can contain the token; never print them or chain them.
raise AnnouncementError(
f'Discord {method} failed or returned invalid JSON; no retry was attempted.'
) from None
finally:
connection.close()
def validate(self):
"""GET only: verify an incoming webhook and return safe identifying fields."""
webhook = self._request('GET')
if (
not isinstance(webhook, dict)
or type(webhook.get('type')) is not int
or webhook['type'] != 1
or webhook.get('id') != self.id
or not is_snowflake(webhook.get('guild_id'))
or not is_snowflake(webhook.get('channel_id'))
):
raise AnnouncementError('Expected an incoming (type 1) webhook with matching ID and guild/channel IDs.')
return {key: webhook[key] for key in ('id', 'type', 'guild_id', 'channel_id')}
def send(self, payload):
"""One POST, followed by exact message GET; never automatically retry a send."""
webhook = self.validate()
message_id = None
try:
sent = self._request('POST', '?wait=true', payload)
if not isinstance(sent, dict) or not is_snowflake(sent.get('id')):
raise AnnouncementError('Discord did not return a valid message ID.')
message_id = sent['id']
saved = self._request('GET', f'/messages/{message_id}')
if (
not isinstance(saved, dict)
or saved.get('id') != message_id
or saved.get('webhook_id') != self.id
or saved.get('channel_id') != webhook['channel_id']
or saved.get('content') != payload['content']
or saved.get('mention_everyone') is not True
or saved.get('mentions') != []
or saved.get('mention_roles') != []
):
raise AnnouncementError('Discord message readback did not match content, identity, or mentions.')
except AnnouncementError as error:
reference = f' Returned message ID: {message_id}.' if message_id else ''
raise AnnouncementError(f'Delivery not confirmed. {error}{reference} {RECONCILE}') from None
return message_id
def main(env=None):
env = os.environ if env is None else env
try:
if env.get('GITHUB_REPOSITORY') != REPOSITORY:
raise AnnouncementError('This workflow is restricted to langbot-app/LangBot.')
name = env.get('GITHUB_EVENT_NAME')
if name == 'workflow_dispatch':
webhook = DiscordWebhook(env.get('DISCORD_RELEASE_WEBHOOK_URL')).validate()
print(
f'Validated incoming webhook: guild_id={webhook["guild_id"]} channel_id={webhook["channel_id"]}. No message sent.'
)
return 0
if name != 'release':
raise AnnouncementError('Only release and workflow_dispatch events are accepted by this helper.')
try:
event = json.loads(Path(env.get('GITHUB_EVENT_PATH', '')).read_text(encoding='utf-8'))
except (OSError, ValueError, UnicodeError):
raise AnnouncementError('Cannot read a valid JSON release event from GITHUB_EVENT_PATH.') from None
payload = release_payload(event, env.get('GITHUB_RUN_ATTEMPT'))
if payload is None:
print('Skipped draft or prerelease; no message sent.')
return 0
message_id = DiscordWebhook(env.get('DISCORD_RELEASE_WEBHOOK_URL')).send(payload)
print(f'Announcement verified by exact message readback: message_id={message_id}.')
return 0
except AnnouncementError as error:
print(f'Error: {error}', file=sys.stderr)
return 1
if __name__ == '__main__':
sys.exit(main())
-427
View File
@@ -1,427 +0,0 @@
"""Offline contract tests; no Discord credentials or network required."""
import contextlib
import io
import json
import os
from pathlib import Path
import subprocess
import sys
import tempfile
import unittest
from unittest.mock import MagicMock, patch
try:
import announce
except ModuleNotFoundError:
announce = None
WEBHOOK = 'https://discord.com/api/webhooks/123456789012345678/fixture_token-ONLY'
WEBHOOK_ID = '123456789012345678'
GUILD_ID = '234567890123456789'
CHANNEL_ID = '345678901234567890'
MESSAGE_ID = '456789012345678901'
REPO = 'langbot-app/LangBot'
URL = f'https://github.com/{REPO}/releases/tag/v4.10.11'
CONTENT = f'@everyone LangBot v4.10.11 is now available!\nRelease notes: {URL}'
def event():
return {
'action': 'published',
'repository': {'full_name': REPO},
'release': {
'draft': False,
'prerelease': False,
'tag_name': 'v4.10.11',
'html_url': URL,
'name': 'Hostile @everyone <@123> $(touch /tmp/unsafe)',
'body': '@everyone @here <@123> <@&456> `hostile`',
},
}
def metadata():
return {'id': WEBHOOK_ID, 'type': 1, 'guild_id': GUILD_ID, 'channel_id': CHANNEL_ID}
def message():
return {
'id': MESSAGE_ID,
'webhook_id': WEBHOOK_ID,
'channel_id': CHANNEL_ID,
'content': CONTENT,
'mention_everyone': True,
'mentions': [],
'mention_roles': [],
}
class BaseTest(unittest.TestCase):
def setUp(self):
self.assertIsNotNone(announce, 'The release announcement helper must exist')
class PolicyTests(BaseTest):
def test_payload_has_one_literal_everyone_and_no_untrusted_body(self):
payload = announce.release_payload(event(), '1')
self.assertEqual(payload['content'], CONTENT)
self.assertEqual(json.dumps(payload).count('@everyone'), 1)
self.assertEqual(
payload['allowed_mentions'],
{
'parse': ['everyone'],
'users': [],
'roles': [],
'replied_user': False,
},
)
self.assertIs(payload['tts'], False)
self.assertEqual(payload['flags'], 0)
def test_drafts_and_prereleases_are_skipped(self):
for flag in ('draft', 'prerelease'):
with self.subTest(flag=flag):
value = event()
value['release'][flag] = True
self.assertIsNone(announce.release_payload(value, '1'))
def test_only_published_action_is_accepted(self):
for action in ('edited', 'created', 'released', 'deleted', '', None):
with self.subTest(action=action):
value = event()
value['action'] = action
with self.assertRaises(announce.AnnouncementError):
announce.release_payload(value, '1')
def test_reruns_and_missing_attempt_refuse_manual_reconciliation(self):
for attempt in ('2', '3', '', None, '01', '0', '1\n'):
with self.subTest(attempt=attempt):
with self.assertRaisesRegex(announce.AnnouncementError, 'manual reconciliation'):
announce.release_payload(event(), attempt)
def test_repository_must_match_exactly(self):
for repo in ('evil/LangBot', 'langbot-app/langbot', None):
value = event()
value['repository']['full_name'] = repo
with self.assertRaises(announce.AnnouncementError):
announce.release_payload(value, '1')
def test_hostile_and_noncanonical_tags_are_rejected(self):
for tag in (
'v1.2.3 @everyone',
'v1.2.3\n',
'v1.2.3/../../x',
'v1.2.3?x=y',
'$(id)',
'v1.2.3-rc.1',
'v.2.3',
'v1.2.3%0a',
'<@123>',
'v1.2.' + '3' * 100,
'',
None,
123,
):
with self.subTest(tag=tag):
value = event()
value['release']['tag_name'] = tag
value['release']['html_url'] = f'https://github.com/{REPO}/releases/tag/{tag}'
with self.assertRaises(announce.AnnouncementError):
announce.release_payload(value, '1')
def test_release_url_must_be_canonical_and_match_tag(self):
for url in (
'https://evil.example/tag/v4.10.11',
URL + '?x=y',
URL + '#anchor',
URL + '/',
URL.replace('v4.10.11', 'v4.10.12'),
URL.replace('github.com', 'github.com@evil.example'),
URL.replace('https:', 'http:'),
URL + '\n',
None,
):
with self.subTest(url=url):
value = event()
value['release']['html_url'] = url
with self.assertRaises(announce.AnnouncementError):
announce.release_payload(value, '1')
def test_malformed_events_fail_closed(self):
for value in (None, [], {}, {'release': []}, {'repository': None}):
with self.subTest(value=value):
with self.assertRaises(announce.AnnouncementError):
announce.release_payload(value, '1')
for flag in ('draft', 'prerelease'):
for bad in (None, 'false', 0, 1):
value = event()
value['release'][flag] = bad
with self.assertRaises(announce.AnnouncementError):
announce.release_payload(value, '1')
class DiscordTests(BaseTest):
def setUp(self):
super().setUp()
self.patch = patch('announce.http.client.HTTPSConnection')
self.connection_class = self.patch.start()
self.addCleanup(self.patch.stop)
self.connection = self.connection_class.return_value
def respond(self, *values):
responses = []
for value in values:
response = MagicMock()
response.status = 200
response.read.return_value = json.dumps(value).encode()
responses.append(response)
self.connection.getresponse.side_effect = responses
def methods(self):
return [call.args[0] for call in self.connection.request.call_args_list]
def test_webhook_validation_is_get_only_and_reports_ids(self):
self.respond(metadata())
result = announce.DiscordWebhook(WEBHOOK).validate()
self.assertEqual(result, metadata())
self.assertEqual(self.methods(), ['GET'])
self.assertEqual(
self.connection.request.call_args.args[:2], ('GET', f'/api/webhooks/{WEBHOOK_ID}/fixture_token-ONLY')
)
self.connection_class.assert_called_with('discord.com', timeout=20)
self.connection.close.assert_called_once()
def test_invalid_webhook_urls_are_rejected_before_network(self):
for url in (
'',
None,
WEBHOOK + '/',
WEBHOOK + '?wait=true',
WEBHOOK + '#x',
WEBHOOK + '\n',
' ' + WEBHOOK,
WEBHOOK.replace('https:', 'http:'),
WEBHOOK.replace('discord.com', 'discord.com.evil.example'),
WEBHOOK.replace('discord.com', 'discord.com@evil.example'),
WEBHOOK.replace('discord.com', 'discord.com:443'),
WEBHOOK.replace('/api/', '/api/v10/'),
WEBHOOK.replace(WEBHOOK_ID, 'abc'),
WEBHOOK + '/../../x',
WEBHOOK.replace('fixture_token-ONLY', 'a%2Fb'),
):
with self.subTest(url=url):
with self.assertRaises(announce.AnnouncementError):
announce.DiscordWebhook(url)
self.connection_class.assert_not_called()
def test_webhook_metadata_requires_incoming_type_and_ids(self):
invalid = [
None,
[],
{},
dict(metadata(), type=2),
dict(metadata(), type=True),
dict(metadata(), id='999'),
dict(metadata(), channel_id=None),
dict(metadata(), guild_id='::error::hostile'),
]
for value in invalid:
with self.subTest(value=value):
self.respond(value)
with self.assertRaises(announce.AnnouncementError):
announce.DiscordWebhook(WEBHOOK).validate()
self.assertNotIn('POST', self.methods())
def test_send_waits_and_reads_back_exact_returned_message(self):
self.respond(metadata(), message(), message())
result = announce.DiscordWebhook(WEBHOOK).send(announce.release_payload(event(), '1'))
self.assertEqual(result, MESSAGE_ID)
self.assertEqual(self.methods(), ['GET', 'POST', 'GET'])
calls = self.connection.request.call_args_list
self.assertEqual(calls[1].args[:2], ('POST', f'/api/webhooks/{WEBHOOK_ID}/fixture_token-ONLY?wait=true'))
self.assertEqual(json.loads(calls[1].kwargs['body']), announce.release_payload(event(), '1'))
self.assertEqual(
calls[2].args[:2], ('GET', f'/api/webhooks/{WEBHOOK_ID}/fixture_token-ONLY/messages/{MESSAGE_ID}')
)
def test_readback_must_match_content_mentions_and_identity(self):
for field, bad in (
('content', 'wrong'),
('mention_everyone', False),
('mention_everyone', 1),
('mentions', [{'id': '123'}]),
('mention_roles', ['123']),
('id', '999'),
('channel_id', '999'),
('webhook_id', '999'),
):
with self.subTest(field=field, bad=bad):
self.connection.reset_mock()
self.respond(metadata(), message(), dict(message(), **{field: bad}))
with self.assertRaisesRegex(announce.AnnouncementError, 'manual reconciliation'):
announce.DiscordWebhook(WEBHOOK).send(announce.release_payload(event(), '1'))
self.assertEqual(self.methods().count('POST'), 1)
def test_missing_readback_fields_fail_closed(self):
for field in message():
value = message()
del value[field]
self.respond(metadata(), message(), value)
with self.assertRaises(announce.AnnouncementError):
announce.DiscordWebhook(WEBHOOK).send(announce.release_payload(event(), '1'))
def test_unsafe_post_message_id_never_becomes_get_path(self):
for value in (None, {}, dict(message(), id='../evil'), dict(message(), id='123?x=y')):
self.connection.reset_mock()
self.respond(metadata(), value)
with self.assertRaisesRegex(announce.AnnouncementError, 'manual reconciliation'):
announce.DiscordWebhook(WEBHOOK).send(announce.release_payload(event(), '1'))
self.assertEqual(self.methods(), ['GET', 'POST'])
def test_post_failure_never_retries_and_never_logs_secret(self):
for status in (301, 302, 307, 308, 400, 401, 403, 429, 500, 204):
with self.subTest(status=status):
self.connection.reset_mock()
self.respond(metadata(), message())
responses = list(self.connection.getresponse.side_effect)
responses[1].status = status
self.connection.getresponse.side_effect = responses
with self.assertRaisesRegex(announce.AnnouncementError, 'manual reconciliation') as caught:
announce.DiscordWebhook(WEBHOOK).send(announce.release_payload(event(), '1'))
self.assertNotIn('fixture_token', str(caught.exception))
self.assertEqual(self.methods(), ['GET', 'POST'])
def test_ambiguous_timeout_never_retries_or_echoes_exception(self):
self.respond(metadata())
first = next(self.connection.getresponse.side_effect)
self.connection.getresponse.side_effect = [first, TimeoutError(WEBHOOK)]
with self.assertRaisesRegex(announce.AnnouncementError, 'manual reconciliation') as caught:
announce.DiscordWebhook(WEBHOOK).send(announce.release_payload(event(), '1'))
self.assertNotIn('fixture_token', str(caught.exception))
self.assertEqual(self.methods(), ['GET', 'POST'])
def test_malformed_json_response_is_sanitized(self):
self.respond(metadata())
response = next(self.connection.getresponse.side_effect)
response.read.return_value = WEBHOOK.encode()
self.connection.getresponse.side_effect = [response]
with self.assertRaises(announce.AnnouncementError) as caught:
announce.DiscordWebhook(WEBHOOK).validate()
self.assertNotIn('fixture_token', str(caught.exception))
def test_get_redirect_is_not_followed(self):
self.respond(metadata())
response = next(self.connection.getresponse.side_effect)
response.status = 302
response.getheader.return_value = 'https://evil.example/'
self.connection.getresponse.side_effect = [response]
with self.assertRaises(announce.AnnouncementError):
announce.DiscordWebhook(WEBHOOK).validate()
self.assertEqual(self.methods(), ['GET'])
class EntrypointTests(BaseTest):
def run_main(self, data=None, **overrides):
with tempfile.TemporaryDirectory() as directory:
path = Path(directory) / 'event.json'
path.write_text(json.dumps(event() if data is None else data))
env = {
'GITHUB_EVENT_NAME': 'release',
'GITHUB_EVENT_PATH': str(path),
'GITHUB_REPOSITORY': REPO,
'GITHUB_RUN_ATTEMPT': '1',
'DISCORD_RELEASE_WEBHOOK_URL': WEBHOOK,
}
env.update(overrides)
output = io.StringIO()
with contextlib.redirect_stdout(output), contextlib.redirect_stderr(output):
result = announce.main(env)
return result, output.getvalue()
def test_dispatch_only_validates_even_if_event_contains_release(self):
with patch('announce.DiscordWebhook') as client:
client.return_value.validate.return_value = metadata()
result, output = self.run_main(GITHUB_EVENT_NAME='workflow_dispatch')
self.assertEqual(result, 0)
client.return_value.validate.assert_called_once()
client.return_value.send.assert_not_called()
self.assertIn(GUILD_ID, output)
self.assertIn(CHANNEL_ID, output)
self.assertNotIn('fixture_token', output)
def test_production_release_sends_once(self):
with patch('announce.DiscordWebhook') as client:
client.return_value.send.return_value = MESSAGE_ID
result, output = self.run_main()
self.assertEqual(result, 0)
client.return_value.send.assert_called_once_with(announce.release_payload(event(), '1'))
self.assertIn(MESSAGE_ID, output)
def test_skipped_releases_need_no_secret_or_network(self):
for flag in ('draft', 'prerelease'):
value = event()
value['release'][flag] = True
with patch('announce.DiscordWebhook') as client:
result, _ = self.run_main(value, DISCORD_RELEASE_WEBHOOK_URL='')
self.assertEqual(result, 0)
client.assert_not_called()
def test_rerun_never_constructs_client(self):
with patch('announce.DiscordWebhook') as client:
result, output = self.run_main(GITHUB_RUN_ATTEMPT='2')
self.assertEqual(result, 1)
self.assertIn('manual reconciliation', output)
client.assert_not_called()
def test_unexpected_event_or_repository_cannot_send(self):
for overrides in (
{'GITHUB_EVENT_NAME': 'push'},
{'GITHUB_EVENT_NAME': 'pull_request'},
{'GITHUB_REPOSITORY': 'evil/LangBot'},
):
with patch('announce.DiscordWebhook') as client:
result, _ = self.run_main(**overrides)
self.assertEqual(result, 1)
client.assert_not_called()
def test_missing_secret_fails_clearly_for_send_and_validation(self):
for name in ('release', 'workflow_dispatch'):
result, output = self.run_main(GITHUB_EVENT_NAME=name, DISCORD_RELEASE_WEBHOOK_URL='')
self.assertEqual(result, 1)
self.assertIn('DISCORD_RELEASE_WEBHOOK_URL is missing', output)
def test_cli_reads_event_file_and_redacts_invalid_input(self):
with tempfile.TemporaryDirectory() as directory:
path = Path(directory) / 'event.json'
value = event()
value['release']['tag_name'] = '::error::hostile @everyone'
path.write_text(json.dumps(value))
env = dict(
os.environ,
GITHUB_EVENT_NAME='release',
GITHUB_EVENT_PATH=str(path),
GITHUB_REPOSITORY=REPO,
GITHUB_RUN_ATTEMPT='1',
DISCORD_RELEASE_WEBHOOK_URL=WEBHOOK,
)
result = subprocess.run(
[sys.executable, str(Path(__file__).with_name('announce.py'))],
env=env,
text=True,
capture_output=True,
check=False,
)
self.assertEqual(result.returncode, 1)
self.assertNotIn('hostile', result.stderr)
self.assertNotIn('fixture_token', result.stderr)
self.assertNotIn('Traceback', result.stderr)
def test_unreadable_event_fails_safely(self):
result, output = self.run_main(GITHUB_EVENT_PATH='/nonexistent/event.json')
self.assertEqual(result, 1)
self.assertNotIn('Traceback', output)
if __name__ == '__main__':
unittest.main()
-64
View File
@@ -1,64 +0,0 @@
name: Discord Release Announcement
on:
release:
types: [published]
workflow_dispatch:
push:
paths:
- '.github/workflows/discord-release.yml'
- '.github/discord-release/**'
pull_request:
paths:
- '.github/workflows/discord-release.yml'
- '.github/discord-release/**'
permissions:
contents: read
jobs:
tests:
name: Offline announcement tests
runs-on: ubuntu-24.04
timeout-minutes: 5
steps:
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
with:
persist-credentials: false
- name: Test helper without secrets or network
run: python3 -m unittest discover -s .github/discord-release -p 'test_*.py' -v
validate:
name: Validate webhook (GET only, no message)
if: github.repository == 'langbot-app/LangBot' && github.event_name == 'workflow_dispatch'
needs: tests
runs-on: ubuntu-24.04
timeout-minutes: 5
steps:
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
with:
persist-credentials: false
- name: Validate incoming webhook and report guild/channel IDs
env:
DISCORD_RELEASE_WEBHOOK_URL: ${{ secrets.DISCORD_RELEASE_WEBHOOK_URL }}
run: python3 .github/discord-release/announce.py
announce:
name: Announce published stable release
if: >-
github.repository == 'langbot-app/LangBot' &&
github.event_name == 'release' && github.event.action == 'published' &&
github.event.release.draft == false && github.event.release.prerelease == false
needs: tests
runs-on: ubuntu-24.04
timeout-minutes: 5
steps:
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
with:
persist-credentials: false
# The helper refuses GITHUB_RUN_ATTEMPT != 1 with recovery guidance.
# Never interpolate release data into a shell command.
- name: Send once and verify the exact Discord message
env:
DISCORD_RELEASE_WEBHOOK_URL: ${{ secrets.DISCORD_RELEASE_WEBHOOK_URL }}
run: python3 .github/discord-release/announce.py
+41
View File
@@ -87,8 +87,10 @@ async def _read_httpx_response_limited(
response: httpx.Response,
*,
max_bytes: int,
task_context: taskmgr.TaskContext | None = None,
) -> bytes:
content_length = response.headers.get('content-length')
declared_size: int | None = None
if content_length is not None:
try:
declared_size = int(content_length)
@@ -97,11 +99,23 @@ async def _read_httpx_response_limited(
if declared_size is not None and declared_size > max_bytes:
raise ValueError(f'Remote response exceeds the {max_bytes}-byte limit')
if task_context is not None and declared_size is not None:
task_context.metadata['download_total'] = declared_size
start_time = time.time()
body = bytearray()
async for chunk in response.aiter_bytes(chunk_size=64 * 1024):
body.extend(chunk)
if len(body) > max_bytes:
raise ValueError(f'Remote response exceeds the {max_bytes}-byte limit')
if task_context is not None:
elapsed = time.time() - start_time
task_context.metadata.update(
{
'download_current': len(body),
'download_speed': len(body) / elapsed if elapsed > 0 else 0,
}
)
return bytes(body)
@@ -111,6 +125,7 @@ async def _marketplace_get(
*,
max_bytes: int,
allow_not_found: bool = False,
task_context: taskmgr.TaskContext | None = None,
) -> tuple[int, bytes]:
async with client.stream('GET', url) as response:
if allow_not_found and response.status_code == 404:
@@ -119,6 +134,7 @@ async def _marketplace_get(
return response.status_code, await _read_httpx_response_limited(
response,
max_bytes=max_bytes,
task_context=task_context,
)
@@ -1680,6 +1696,7 @@ class PluginRuntimeConnector(ManagedRuntimeConnector):
client,
f'{space_url}/api/v1/marketplace/plugins/download/{plugin_author}/{plugin_name}/{latest_version}',
max_bytes=_MARKETPLACE_PLUGIN_DOWNLOAD_MAX_BYTES,
task_context=task_context,
)
return plugin_package, latest_version
@@ -1695,7 +1712,21 @@ class PluginRuntimeConnector(ManagedRuntimeConnector):
plugin_name = str(install_info.get('plugin_name') or '')
file_bytes: bytes | None
if task_context is not None:
# Reset per-install progress counters so a re-install of the same
# plugin does not inherit stale metadata from a previous task.
task_context.set_current_action('preparing plugin install')
task_context.metadata.update(
{
'download_total': 0,
'download_current': 0,
'download_speed': 0,
}
)
if install_source == PluginInstallSource.MARKETPLACE:
if task_context is not None:
task_context.set_current_action('downloading plugin package')
file_bytes, version = await self._download_marketplace_package(
execution_context,
plugin_author,
@@ -1719,6 +1750,8 @@ class PluginRuntimeConnector(ManagedRuntimeConnector):
else:
raise ValueError(f'Unsupported plugin install source: {install_source.value}')
if task_context is not None:
task_context.set_current_action('inspecting plugin package')
manifest_author, manifest_name = self._inspect_plugin_package(file_bytes, task_context)
if not manifest_author or not manifest_name:
raise ValueError('Plugin package manifest identity is missing')
@@ -1730,8 +1763,12 @@ class PluginRuntimeConnector(ManagedRuntimeConnector):
if task_context is not None:
task_context.metadata['plugin_name'] = f'{plugin_author}/{plugin_name}'
if task_context is not None:
task_context.set_current_action('storing plugin package')
artifact_digest = hashlib.sha256(file_bytes).hexdigest()
await self._store_artifact_package(execution_context, artifact_digest, file_bytes)
if task_context is not None:
task_context.set_current_action('installing plugin dependencies')
try:
binding, previous_digest, previous_was_durable = await self._persist_installation_package(
execution_context,
@@ -1749,6 +1786,8 @@ class PluginRuntimeConnector(ManagedRuntimeConnector):
plugin_author=plugin_author,
plugin_name=plugin_name,
)
if task_context is not None:
task_context.set_current_action('launching plugin')
await self._apply_desired_state(
PluginInstallationDesiredState(binding=binding, enabled=True),
artifact_package=file_bytes,
@@ -1766,6 +1805,8 @@ class PluginRuntimeConnector(ManagedRuntimeConnector):
pass
except Exception as exc:
self.ap.logger.debug(f'Legacy OSS plugin cleanup skipped: {exc}')
if task_context is not None:
task_context.set_current_action('waiting for plugin to become ready')
await self._wait_for_installed_plugin_ready(plugin_author, plugin_name, task_context)
async def upgrade_plugin(
@@ -10,6 +10,7 @@ import { Button } from '@/components/ui/button';
import {
Download,
Package,
Rocket,
Server,
Sparkles,
CheckCircle2,
@@ -39,11 +40,27 @@ const STAGES: {
icon: Package,
i18nKey: 'plugins.installProgress.installingDeps',
},
{
key: InstallStage.LAUNCHING,
icon: Rocket,
i18nKey: 'plugins.installProgress.launching',
},
];
/**
* Find the row that should be highlighted for a given stage.
* LAUNCHING/INITIALIZING/DONE collapse onto the launching row.
*/
function getStageIndex(stage: InstallStage): number {
if (
stage === InstallStage.LAUNCHING ||
stage === InstallStage.INITIALIZING ||
stage === InstallStage.DONE
) {
return STAGES.length - 1;
}
const idx = STAGES.findIndex((s) => s.key === stage);
return idx >= 0 ? idx : -1;
return idx >= 0 ? idx : 0;
}
function formatFileSize(bytes: number): string {
@@ -169,9 +186,12 @@ function formatSpeed(bytesPerSec: number): string {
function TaskProgressContent({ task }: { task: PluginInstallTask }) {
const { t } = useTranslation();
const currentStageIndex = getStageIndex(task.stage);
const isDone = task.stage === InstallStage.DONE;
const isError = task.stage === InstallStage.ERROR;
// When a task fails, `stage` becomes ERROR — fall back to the furthest
// stage it actually reached so the failed phase is still displayed.
const displayStage = isError && task.lastStage ? task.lastStage : task.stage;
const currentStageIndex = getStageIndex(displayStage);
// MCP / Skill don't have the plugin's download + dependency-install stages;
// show a single "installing → done/failed" row instead of plugin steps.
@@ -27,6 +27,9 @@ export interface PluginInstallTask {
pluginName: string; // display name
source: 'github' | 'marketplace' | 'local';
stage: InstallStage;
/** Furthest non-terminal stage reached — kept when the task fails so the
* UI can still show which phase failed. */
lastStage?: InstallStage;
overallProgress: number; // 0-100
extensionType: 'plugin' | 'mcp' | 'skill'; // type of extension being installed
fileSize?: number; // bytes, if known
@@ -43,6 +46,8 @@ export interface PluginInstallTask {
depsSpeed?: number; // deps download speed bytes/s
error?: string;
startedAt: number; // timestamp
/** Timestamp when the current stage began; used for smooth creeping. */
stageStartedAt?: number;
currentAction: string; // raw backend action string
}
@@ -84,42 +89,158 @@ export function usePluginInstallTasks() {
}
/**
* Map backend `current_action` to our InstallStage.
* Ordered lifecycle stages. Used to enforce forward-only transitions so the
* progress bar never moves backwards while a task is running.
*/
function mapActionToStage(action: string): InstallStage {
if (!action) return InstallStage.DOWNLOADING;
const lower = action.toLowerCase();
if (lower.includes('download')) return InstallStage.DOWNLOADING;
if (lower.includes('dependencies') || lower.includes('requirements'))
return InstallStage.INSTALLING_DEPS;
if (lower.includes('initializ') || lower.includes('setting'))
return InstallStage.INSTALLING_DEPS;
if (lower.includes('launch')) return InstallStage.INSTALLING_DEPS;
if (lower.includes('installed') || lower.includes('complete'))
return InstallStage.DONE;
return InstallStage.DOWNLOADING;
const STAGE_ORDER: InstallStage[] = [
InstallStage.DOWNLOADING,
InstallStage.INSTALLING_DEPS,
InstallStage.INITIALIZING,
InstallStage.LAUNCHING,
InstallStage.DONE,
];
/**
* Lower bound (%) for each stage. A task's progress is never allowed to drop
* below the floor of the furthest stage it has already reached.
*/
const STAGE_FLOOR: Record<InstallStage, number> = {
[InstallStage.DOWNLOADING]: 2,
[InstallStage.INSTALLING_DEPS]: 55,
[InstallStage.INITIALIZING]: 85,
[InstallStage.LAUNCHING]: 94,
[InstallStage.DONE]: 100,
[InstallStage.ERROR]: 0,
};
/** Get the lower-bound percentage for a stage. */
function stageFloor(stage: InstallStage): number {
return STAGE_FLOOR[stage] ?? 0;
}
/** Get the lower bound of the stage that follows the given one. */
function nextStageFloor(stage: InstallStage): number {
const idx = STAGE_ORDER.indexOf(stage);
const next = idx >= 0 ? STAGE_ORDER[idx + 1] : undefined;
return next ? stageFloor(next) : 100;
}
/** Return whichever stage is further along in the lifecycle. */
function maxStage(current: InstallStage, incoming: InstallStage): InstallStage {
const currentIdx = STAGE_ORDER.indexOf(current);
const incomingIdx = STAGE_ORDER.indexOf(incoming);
if (currentIdx === -1) return incoming;
if (incomingIdx === -1) return current;
return incomingIdx >= currentIdx ? incoming : current;
}
/**
* Get overall progress percentage from a stage.
* Map backend `current_action` to our InstallStage.
*
* Unknown / transitional actions must NOT map back to an earlier stage,
* otherwise the bar would jump backwards mid-install.
*/
function stageToProgress(stage: InstallStage): number {
switch (stage) {
case InstallStage.DOWNLOADING:
return 10;
case InstallStage.INSTALLING_DEPS:
return 70;
case InstallStage.INITIALIZING:
return 70;
case InstallStage.LAUNCHING:
return 85;
case InstallStage.DONE:
return 100;
case InstallStage.ERROR:
return 0;
default:
return 0;
function mapActionToStage(action: string): InstallStage {
const lower = (action || '').toLowerCase();
if (!lower) return InstallStage.DOWNLOADING;
// "preparing"/"resolving" happen before any bytes land on disk.
if (lower.includes('prepar') || lower.includes('resolv'))
return InstallStage.DOWNLOADING;
if (lower.includes('download') && !lower.includes('dependenc'))
return InstallStage.DOWNLOADING;
// Activation / readiness tail phase — its own slice of the bar.
if (
lower.includes('launch') ||
lower.includes('start') ||
lower.includes('wait') ||
lower.includes('ready') ||
lower.includes('initializ')
) {
return InstallStage.LAUNCHING;
}
// Dependency installation and package finalization.
if (
lower.includes('dependenc') ||
lower.includes('requirements') ||
lower.includes('parsing') ||
lower.includes('extract') ||
lower.includes('inspect') ||
lower.includes('persist') ||
lower.includes('stor') ||
lower.includes('install') ||
lower.includes('setting')
) {
return InstallStage.INSTALLING_DEPS;
}
// Unknown transitional actions belong to the busy middle of the install.
return InstallStage.INSTALLING_DEPS;
}
/**
* Time-based creep so the bar keeps moving when no counters exist.
*
* Uses an asymptote so the increment decelerates as it approaches the stage
* ceiling — the bar always feels alive but never overshoots into the next
* stage's range.
*/
function creep(stageStartedAt: number, span: number): number {
if (span <= 0) return 0;
const elapsed = (Date.now() - stageStartedAt) / 1000;
// Approaching `span` asymptotically: after ~60s we are ~86% of the span.
const ratio = 1 - Math.exp(-elapsed / 30);
return span * ratio;
}
/**
* Compute a progress value for the current stage.
*
* Real byte / dependency counters drive the value when available; otherwise
* the value creeps forward slowly based on elapsed time. Callers are expected
* to combine the result with the previous value via `Math.max` so it is
* monotonic.
*/
function computeStageProgress(
task: PluginInstallTask,
stage: InstallStage,
): number {
const floor = stageFloor(stage);
const ceiling = Math.max(floor, nextStageFloor(stage) - 1);
// Creep from when this stage began so a stage change restarts the ramp
// instead of inheriting the previous stage's elapsed time.
const stageStartedAt = task.stageStartedAt ?? task.startedAt;
const creepValue = Math.min(
ceiling,
floor + creep(stageStartedAt, ceiling - floor),
);
if (stage === InstallStage.DOWNLOADING) {
const total = task.downloadTotal ?? task.fileSize;
const current = task.downloadCurrent;
if (total && total > 0 && current != null && current > 0) {
const ratio = Math.min(1, current / total);
// Never let a stale counter pull the value below the creep baseline.
return Math.max(creepValue, floor + (ceiling - floor) * ratio);
}
return creepValue;
}
if (stage === InstallStage.INSTALLING_DEPS) {
const total = task.depsTotal;
const installed = task.depsInstalled;
if (total && total > 0 && installed != null && installed > 0) {
const ratio = Math.min(1, installed / total);
// Leave headroom for the finalize/launch phase that has no counters.
return Math.max(creepValue, floor + (ceiling - floor) * ratio * 0.9);
}
return creepValue;
}
return creepValue;
}
/**
@@ -146,8 +267,14 @@ function isPluginInstallTask(name: string): boolean {
/**
* Convert a backend AsyncTask to our PluginInstallTask.
*
* `previous` (when provided) carries monotonic state forward so re-syncing
* after a refresh or a poll cannot make the progress bar move backwards.
*/
function asyncTaskToPluginInstallTask(task: AsyncTask): PluginInstallTask {
function asyncTaskToPluginInstallTask(
task: AsyncTask,
previous?: PluginInstallTask,
): PluginInstallTask {
const source = extractSourceFromName(task.name);
const md = (task.task_context?.metadata ?? {}) as Record<string, unknown>;
const action = task.task_context?.current_action || '';
@@ -157,24 +284,6 @@ function asyncTaskToPluginInstallTask(task: AsyncTask): PluginInstallTask {
const num = (v: unknown) => (typeof v === 'number' ? v : undefined);
const str = (v: unknown) => (typeof v === 'string' ? v : undefined);
let stage: InstallStage;
let overallProgress: number;
let error: string | undefined;
if (done) {
if (exception) {
stage = InstallStage.ERROR;
overallProgress = 0;
error = exception;
} else {
stage = InstallStage.DONE;
overallProgress = 100;
}
} else {
stage = mapActionToStage(action);
overallProgress = Math.min(95, stageToProgress(stage));
}
const pluginName = str(md.plugin_name) || task.label || `${source} extension`;
let extensionType: 'plugin' | 'mcp' | 'skill' = 'plugin';
@@ -184,6 +293,75 @@ function asyncTaskToPluginInstallTask(task: AsyncTask): PluginInstallTask {
extensionType = 'skill';
}
// Prefer the task's real creation time so a refresh (or first sync) restores
// the correct elapsed baseline instead of restarting the ramp from zero.
const backendStartedAt =
typeof task.created_at === 'number' && task.created_at > 0
? task.created_at * 1000
: undefined;
const startedAt = previous?.startedAt ?? backendStartedAt ?? Date.now();
let stageStartedAt =
previous?.stageStartedAt ??
previous?.startedAt ??
backendStartedAt ??
startedAt;
let stage: InstallStage;
let overallProgress: number;
let error: string | undefined;
// Furthest non-terminal stage reached, kept across failures.
let lastStage = previous?.lastStage ?? previous?.stage;
if (done) {
if (exception) {
// Preserve how far the task got before failing, so the bar shows the
// failure point instead of jumping back to zero.
stage = InstallStage.ERROR;
overallProgress = previous?.overallProgress ?? 0;
error = exception;
} else {
stage = InstallStage.DONE;
overallProgress = 100;
}
} else {
const incoming = mapActionToStage(action);
// Forward-only: never move back to an earlier stage than we already reached.
stage = previous ? maxStage(previous.stage, incoming) : incoming;
if (!previous || previous.stage !== stage) {
stageStartedAt = Date.now();
}
lastStage = stage;
const counters: PluginInstallTask = {
id: `${source}-${task.id}`,
taskId: task.id,
pluginName,
source,
extensionType,
stage,
overallProgress: 0,
downloadCurrent: num(md.download_current) ?? previous?.downloadCurrent,
downloadTotal: num(md.download_total) ?? previous?.downloadTotal,
downloadSpeed: num(md.download_speed) ?? previous?.downloadSpeed,
depsTotal: num(md.deps_total) ?? previous?.depsTotal,
depsInstalled: num(md.deps_installed) ?? previous?.depsInstalled,
depsRemaining: num(md.deps_remaining) ?? previous?.depsRemaining,
currentDep: str(md.current_dep) ?? previous?.currentDep,
depsDownloadedSize:
num(md.deps_downloaded_size) ?? previous?.depsDownloadedSize,
depsSpeed: num(md.deps_speed) ?? previous?.depsSpeed,
startedAt,
stageStartedAt,
currentAction: action,
};
const computed = computeStageProgress(counters, stage);
overallProgress = Math.max(previous?.overallProgress ?? 0, computed);
// Keep the bar strictly below 100 until the backend confirms completion.
overallProgress = Math.round(Math.min(99, overallProgress));
}
return {
id: `${source}-${task.id}`,
taskId: task.id,
@@ -191,18 +369,21 @@ function asyncTaskToPluginInstallTask(task: AsyncTask): PluginInstallTask {
source,
extensionType,
stage,
lastStage,
overallProgress,
downloadCurrent: num(md.download_current),
downloadTotal: num(md.download_total),
downloadSpeed: num(md.download_speed),
depsTotal: num(md.deps_total),
depsInstalled: num(md.deps_installed),
depsRemaining: num(md.deps_remaining),
currentDep: str(md.current_dep),
depsDownloadedSize: num(md.deps_downloaded_size),
depsSpeed: num(md.deps_speed),
downloadCurrent: num(md.download_current) ?? previous?.downloadCurrent,
downloadTotal: num(md.download_total) ?? previous?.downloadTotal,
downloadSpeed: num(md.download_speed) ?? previous?.downloadSpeed,
depsTotal: num(md.deps_total) ?? previous?.depsTotal,
depsInstalled: num(md.deps_installed) ?? previous?.depsInstalled,
depsRemaining: num(md.deps_remaining) ?? previous?.depsRemaining,
currentDep: str(md.current_dep) ?? previous?.currentDep,
depsDownloadedSize:
num(md.deps_downloaded_size) ?? previous?.depsDownloadedSize,
depsSpeed: num(md.deps_speed) ?? previous?.depsSpeed,
error,
startedAt: Date.now(),
startedAt,
stageStartedAt,
currentAction: action,
};
}
@@ -315,8 +496,11 @@ export function PluginInstallTaskProvider({
return {
...t,
stage: InstallStage.ERROR,
// Keep the phase that failed for the UI to display.
lastStage: t.lastStage ?? t.stage,
error: exception,
overallProgress: 0,
// Show where it failed instead of resetting to 0.
overallProgress: t.overallProgress,
currentAction: action,
...progressFields,
};
@@ -332,26 +516,28 @@ export function PluginInstallTaskProvider({
};
}
const stage = mapActionToStage(action);
const baseProgress = stageToProgress(stage);
// Add small time-based increment within stage
const elapsed = (Date.now() - t.startedAt) / 1000;
const withinStageIncrement = Math.min(
15,
Math.floor(elapsed / 2),
);
const progress = Math.min(
95,
baseProgress + withinStageIncrement,
);
// Forward-only stage transition.
const incoming = mapActionToStage(action);
const stage = maxStage(t.stage, incoming);
// Reset the per-stage ramp whenever we enter a new stage.
const stageAdvanced = stage !== t.stage;
return {
const next: PluginInstallTask = {
...t,
stage,
overallProgress: progress,
lastStage: stage,
stageStartedAt: stageAdvanced
? Date.now()
: (t.stageStartedAt ?? t.startedAt),
currentAction: action,
...progressFields,
};
const computed = computeStageProgress(next, stage);
// Progress must never move backwards while the task runs.
const overallProgress = Math.round(
Math.min(99, Math.max(t.overallProgress, computed)),
);
return { ...next, overallProgress };
}),
);
})
@@ -377,46 +563,61 @@ export function PluginInstallTaskProvider({
);
setTasks((prevTasks) => {
const existingTaskIds = new Set(prevTasks.map((t) => t.taskId));
const updatedTasks = [...prevTasks];
// Collect tasks that need polling started after state is committed.
const toPoll: Array<{ key: string; taskId: number }> = [];
for (const bt of backendTasks) {
// Skip tasks that the user has dismissed
if (dismissedTaskIds.current.has(bt.id)) continue;
if (!existingTaskIds.has(bt.id)) {
const idx = updatedTasks.findIndex((t) => t.taskId === bt.id);
if (idx === -1) {
// New task from backend (e.g. after page refresh) — add it
const newTask = asyncTaskToPluginInstallTask(bt);
updatedTasks.push(newTask);
// If not done, start polling for progress
if (!bt.runtime.done) {
pollTask(newTask.id, bt.id);
toPoll.push({ key: newTask.id, taskId: bt.id });
} else {
// Mark as already notified so we don't re-trigger toasts for old completed tasks
notifiedTaskIds.current.add(bt.id);
}
} else {
// Already tracking — if it's done in backend but still active locally, update it
const idx = updatedTasks.findIndex((t) => t.taskId === bt.id);
if (idx !== -1) {
const existing = updatedTasks[idx];
if (
bt.runtime.done &&
existing.stage !== InstallStage.DONE &&
existing.stage !== InstallStage.ERROR
) {
const converted = asyncTaskToPluginInstallTask(bt);
converted.startedAt = existing.startedAt;
converted.pluginName = existing.pluginName;
converted.fileSize = existing.fileSize;
converted.extensionType = existing.extensionType;
updatedTasks[idx] = converted;
}
}
continue;
}
// Already tracking — merge the backend snapshot into the existing
// task. Passing `existing` keeps `startedAt`, `pluginName` and
// progress monotonic so re-syncing never rewinds the bar.
const existing = updatedTasks[idx];
const converted = asyncTaskToPluginInstallTask(bt, existing);
converted.pluginName = existing.pluginName;
converted.fileSize = existing.fileSize;
converted.extensionType = existing.extensionType;
// Never downgrade a terminal task that is already done/failed locally,
// unless the backend reports it finished as well.
if (
(existing.stage === InstallStage.DONE ||
existing.stage === InstallStage.ERROR) &&
!bt.runtime.done
) {
continue;
}
updatedTasks[idx] = converted;
if (!bt.runtime.done) {
toPoll.push({ key: converted.id, taskId: bt.id });
}
}
// Schedule polling outside the state updater.
queueMicrotask(() => {
toPoll.forEach(({ key, taskId }) => pollTask(key, taskId));
});
return updatedTasks;
});
} catch {
@@ -464,6 +665,7 @@ export function PluginInstallTaskProvider({
// Remove from dismissed set if re-added
dismissedTaskIds.current.delete(params.taskId);
const startedAt = Date.now();
const newTask: PluginInstallTask = {
id: taskKey,
taskId: params.taskId,
@@ -471,9 +673,11 @@ export function PluginInstallTaskProvider({
source: params.source,
extensionType: params.extensionType,
stage: InstallStage.DOWNLOADING,
overallProgress: 5,
// Start at the downloading floor and creep up from real counters.
overallProgress: stageFloor(InstallStage.DOWNLOADING),
fileSize: params.fileSize,
startedAt: Date.now(),
downloadTotal: params.fileSize,
startedAt,
currentAction: '',
};
@@ -7,6 +7,7 @@ import {
CheckCircle2,
XCircle,
Loader2,
Rocket,
X,
ListTodo,
Puzzle,
@@ -30,6 +31,7 @@ import { cn } from '@/lib/utils';
const STAGE_ICONS: Record<string, React.ElementType> = {
[InstallStage.DOWNLOADING]: Download,
[InstallStage.INSTALLING_DEPS]: Package,
[InstallStage.LAUNCHING]: Rocket,
[InstallStage.DONE]: CheckCircle2,
[InstallStage.ERROR]: XCircle,
};
@@ -95,6 +97,8 @@ function TaskQueueItem({
return t('plugins.installProgress.downloading');
case InstallStage.INSTALLING_DEPS:
return t('plugins.installProgress.installingDeps');
case InstallStage.LAUNCHING:
return t('plugins.installProgress.launching');
case InstallStage.DONE:
return isDone
? getInstallCompleteMessage()
@@ -1,4 +1,11 @@
import { useState, useEffect, useCallback, useRef, Suspense } from 'react';
import {
useState,
useEffect,
useCallback,
useMemo,
useRef,
Suspense,
} from 'react';
import { useSearchParams } from 'react-router-dom';
import { Input } from '@/components/ui/input';
import {
@@ -51,6 +58,10 @@ import { ApiRespMarketplacePlugins } from '@/app/infra/entities/api';
import { LoadingSpinner } from '@/components/ui/loading-spinner';
import { Button } from '@/components/ui/button';
import { PluginTag } from '@/app/infra/http/CloudServiceClient';
import {
resolveInstalledState,
useMarketplaceInstalledIndex,
} from './marketplace-installed';
interface SortOption {
value: string;
@@ -91,6 +102,20 @@ function MarketPageContent({
const { t } = useTranslation();
const [searchParams] = useSearchParams();
// Installed-extension lookup, recomputed whenever the sidebar lists change
// (e.g. right after an install completes).
const installedIndex = useMarketplaceInstalledIndex();
const decorateInstalled = useCallback(
(vo: PluginMarketCardVO): PluginMarketCardVO => {
const state = resolveInstalledState(installedIndex, vo);
vo.installed = state.installed;
vo.hasUpdate = state.hasUpdate;
return vo;
},
[installedIndex],
);
const validTypes = ['plugin', 'mcp', 'skill'];
const extensionTypeOptions = [
@@ -571,7 +596,12 @@ function MarketPageContent({
};
}, []);
const visiblePlugins = plugins;
// Decorate with installed state at render time so the badge updates the
// moment the sidebar lists refresh (e.g. after an install completes).
const visiblePlugins = useMemo(
() => plugins.map((plugin) => decorateInstalled(plugin)),
[plugins, decorateInstalled],
);
// 加载更多
const loadMore = useCallback(() => {
@@ -8,6 +8,10 @@ import { I18nObject } from '@/app/infra/entities/common';
import { extractI18nObject } from '@/i18n/I18nProvider';
import { getCloudServiceClientSync } from '@/app/infra/http';
import { useTranslation } from 'react-i18next';
import {
resolveInstalledState,
useMarketplaceInstalledIndex,
} from './marketplace-installed';
export interface RecommendationList {
uuid: string;
@@ -66,6 +70,7 @@ function RecommendationListRow({
isLast: boolean;
}) {
const { t } = useTranslation();
const installedIndex = useMarketplaceInstalledIndex();
const [page, setPage] = useState(0);
const [perPage, setPerPage] = useState(4);
// Countdown progress to the next auto-advance, 0 → 1 over AUTO_ADVANCE_MS.
@@ -261,16 +266,22 @@ function RecommendationListRow({
ref={gridRef}
className="grid gap-6 [grid-template-columns:repeat(auto-fill,minmax(min(100%,24rem),1fr))]"
>
{visiblePlugins.map((plugin) => (
<PluginMarketCardComponent
key={plugin.author + ' / ' + plugin.name}
cardVO={pluginToVO(plugin, t)}
tagNames={tagNames}
onInstall={onInstall}
installDisabled={installDisabled}
installDisabledTooltip={installDisabledTooltip}
/>
))}
{visiblePlugins.map((plugin) => {
const cardVO = pluginToVO(plugin, t);
const state = resolveInstalledState(installedIndex, cardVO);
cardVO.installed = state.installed;
cardVO.hasUpdate = state.hasUpdate;
return (
<PluginMarketCardComponent
key={plugin.author + ' / ' + plugin.name}
cardVO={cardVO}
tagNames={tagNames}
onInstall={onInstall}
installDisabled={installDisabled}
installDisabledTooltip={installDisabledTooltip}
/>
);
})}
</div>
{totalPages > 1 && !isLast && (
<div className="border-b border-border mt-6" />
@@ -0,0 +1,85 @@
import { useMemo } from 'react';
import { useSidebarData } from '@/app/home/components/home-sidebar/SidebarDataContext';
export interface MarketplaceInstalledState {
installed: boolean;
hasUpdate: boolean;
}
export interface InstalledIndexEntry {
hasUpdate: boolean;
}
/** Composite key used to look up installed extensions: `type:author/name`. */
export function installedExtensionKey(
type: string | undefined,
author: string,
name: string,
): string {
return `${type || 'plugin'}:${author}/${name}`;
}
/**
* Build a lookup of already-installed extensions.
*
* The sidebar identifies each kind differently:
* - plugins: `author/name`
* - MCP servers: `author__name` (double underscore)
* - skills: the bare skill name
*/
export function buildInstalledIndex(
plugins: { id: string; hasUpdate?: boolean }[],
mcpServers: { id: string }[],
skills: { id: string }[],
): Map<string, InstalledIndexEntry> {
const index = new Map<string, InstalledIndexEntry>();
for (const plugin of plugins) {
index.set(`plugin:${plugin.id}`, { hasUpdate: plugin.hasUpdate ?? false });
}
for (const server of mcpServers) {
index.set(`mcp:${server.id.replace(/__/g, '/')}`, { hasUpdate: false });
}
for (const skill of skills) {
index.set(`skill:${skill.id}`, { hasUpdate: false });
}
return index;
}
/**
* Resolve whether a marketplace extension is installed.
*
* Marketplace entries always use `author/name`; skills may be stored under
* their bare name, so both keys are checked for that case.
*/
export function resolveInstalledState(
index: Map<string, InstalledIndexEntry>,
extension: { type?: string; author: string; pluginName: string },
): MarketplaceInstalledState {
const type = extension.type || 'plugin';
const keys = [
`${type}:${extension.author}/${extension.pluginName}`,
`${type}:${extension.pluginName}`,
];
for (const key of keys) {
const entry = index.get(key);
if (entry) {
return { installed: true, hasUpdate: entry.hasUpdate };
}
}
return { installed: false, hasUpdate: false };
}
/**
* Reactive installed-extension index derived from the sidebar data context.
* Recomputes automatically after an install finishes and the sidebar refreshes.
*/
export function useMarketplaceInstalledIndex(): Map<
string,
InstalledIndexEntry
> {
const { plugins, mcpServers, skills } = useSidebarData();
return useMemo(
() => buildInstalledIndex(plugins, mcpServers, skills),
[plugins, mcpServers, skills],
);
}
@@ -3,7 +3,14 @@ import { useRef, useState, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import PluginComponentList from '../PluginComponentList';
import { Badge } from '@/components/ui/badge';
import { Info, Package, ExternalLink, Heart, Loader2 } from 'lucide-react';
import {
CheckCircle2,
Info,
Package,
ExternalLink,
Heart,
Loader2,
} from 'lucide-react';
import {
Tooltip,
TooltipContent,
@@ -48,6 +55,10 @@ export default function PluginMarketCardComponent({
return keys.length > 0 && keys.every((k) => k === 'KnowledgeRetriever');
})();
// Already installed → swap the download count for an "installed" marker.
// Click behaviour stays identical to a normal card.
const isInstalled = cardVO.installed === true;
const showTypeBadge = cardVO.type;
const typeLabel =
cardVO.type === 'mcp'
@@ -320,23 +331,34 @@ export default function PluginMarketCardComponent({
className="w-full flex flex-row items-center justify-between gap-2 px-0 sm:px-[0.4rem] flex-shrink-0 overflow-hidden"
>
<div className="flex flex-row items-center justify-start gap-2 min-w-0 overflow-hidden">
<div className="flex flex-row items-center gap-[0.3rem] sm:gap-[0.4rem] flex-shrink-0">
<svg
className="w-4 h-4 sm:w-[1.2rem] sm:h-[1.2rem] text-[#2563eb] dark:text-[#5b8def] flex-shrink-0"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
>
<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" />
<polyline points="7,10 12,15 17,10" />
<line x1="12" y1="15" x2="12" y2="3" />
</svg>
<div className="text-xs sm:text-sm text-[#2563eb] dark:text-[#5b8def] font-medium whitespace-nowrap">
{cardVO.installCount?.toLocaleString() ?? '0'}
{/* Installed extensions replace the download count with an
"installed" marker so the card reflects local state. */}
{isInstalled ? (
<div className="flex flex-row items-center gap-[0.3rem] sm:gap-[0.4rem] flex-shrink-0">
<CheckCircle2 className="w-4 h-4 sm:w-[1.2rem] sm:h-[1.2rem] text-green-600 dark:text-green-400 flex-shrink-0" />
<div className="text-xs sm:text-sm text-green-600 dark:text-green-400 font-medium whitespace-nowrap">
{t('market.installed')}
</div>
</div>
</div>
) : (
<div className="flex flex-row items-center gap-[0.3rem] sm:gap-[0.4rem] flex-shrink-0">
<svg
className="w-4 h-4 sm:w-[1.2rem] sm:h-[1.2rem] text-[#2563eb] dark:text-[#5b8def] flex-shrink-0"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
>
<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" />
<polyline points="7,10 12,15 17,10" />
<line x1="12" y1="15" x2="12" y2="3" />
</svg>
<div className="text-xs sm:text-sm text-[#2563eb] dark:text-[#5b8def] font-medium whitespace-nowrap">
{cardVO.installCount?.toLocaleString() ?? '0'}
</div>
</div>
)}
{cardVO.tags && cardVO.tags.length > 0 && visibleTags > 0 && (
<div className="flex flex-row items-center gap-1.5 overflow-hidden flex-shrink min-w-0">
@@ -12,6 +12,10 @@ export interface IPluginMarketCardVO {
components?: Record<string, number>;
tags?: string[];
type?: 'plugin' | 'mcp' | 'skill';
/** Whether this extension is already installed in the current workspace. */
installed?: boolean;
/** Whether an installed extension has a newer marketplace version. */
hasUpdate?: boolean;
}
export class PluginMarketCardVO implements IPluginMarketCardVO {
@@ -28,6 +32,8 @@ export class PluginMarketCardVO implements IPluginMarketCardVO {
components?: Record<string, number>;
tags?: string[];
type?: 'plugin' | 'mcp' | 'skill';
installed?: boolean;
hasUpdate?: boolean;
constructor(prop: IPluginMarketCardVO) {
this.description = prop.description;
@@ -43,5 +49,7 @@ export class PluginMarketCardVO implements IPluginMarketCardVO {
this.components = prop.components;
this.tags = prop.tags;
this.type = prop.type;
this.installed = prop.installed ?? false;
this.hasUpdate = prop.hasUpdate ?? false;
}
}
+2
View File
@@ -460,6 +460,8 @@ export interface AsyncTask {
name: string;
label: string;
task_type: string; // system or user
/** Unix epoch seconds (float) when the task was created. */
created_at?: number;
runtime: AsyncTaskRuntimeInfo;
task_context: AsyncTaskTaskContext;
}
+3
View File
@@ -748,6 +748,9 @@ const enUS = {
'Are you sure you want to install plugin "{{name}}" ({{version}})?',
downloadComplete: 'Plugin "{{name}}" download completed',
installFailed: 'Installation failed, please try again later',
installed: 'Installed',
updateAvailable: 'Update available',
alreadyInstalled: '{{name}} is already installed',
loadFailed: 'Failed to get plugin list, please try again later',
noDescription: 'No description available',
recommendation: {
+3
View File
@@ -769,6 +769,9 @@ const esES = {
installFailed: 'Error en la instalación, por favor inténtalo más tarde',
loadFailed:
'Error al obtener la lista de plugins, por favor inténtalo más tarde',
installed: 'Instalado',
updateAvailable: 'Actualización disponible',
alreadyInstalled: '{{name}} ya está instalado',
noDescription: 'No hay descripción disponible',
recommendation: {
pause: 'Pausar rotación automática',
+3
View File
@@ -758,6 +758,9 @@ const jaJP = {
installFailed: 'インストールに失敗しました。後でもう一度お試しください',
loadFailed:
'プラグインリストの取得に失敗しました。後でもう一度お試しください',
installed: 'インストール済み',
updateAvailable: '更新あり',
alreadyInstalled: '{{name}} はインストール済みです',
noDescription: '説明がありません',
recommendation: {
pause: '自動ローテーションを一時停止',
+3
View File
@@ -763,6 +763,9 @@ const ruRU = {
downloadComplete: 'Плагин "{{name}}" загружен',
installFailed: 'Ошибка установки, попробуйте позже',
loadFailed: 'Не удалось получить список плагинов, попробуйте позже',
installed: 'Установлено',
updateAvailable: 'Доступно обновление',
alreadyInstalled: '{{name}} уже установлен',
noDescription: 'Описание отсутствует',
recommendation: {
pause: 'Приостановить авто-прокрутку',
+3
View File
@@ -741,6 +741,9 @@ const thTH = {
downloadComplete: 'ดาวน์โหลดปลั๊กอิน "{{name}}" เสร็จสมบูรณ์',
installFailed: 'ติดตั้งล้มเหลว กรุณาลองใหม่ภายหลัง',
loadFailed: 'ไม่สามารถดึงรายการปลั๊กอินได้ กรุณาลองใหม่ภายหลัง',
installed: 'ติดตั้งแล้ว',
updateAvailable: 'มีอัปเดต',
alreadyInstalled: '{{name}} ติดตั้งแล้ว',
noDescription: 'ไม่มีคำอธิบาย',
recommendation: {
pause: 'หยุดการหมุนอัตโนมัติชั่วคราว',
+3
View File
@@ -756,6 +756,9 @@ const viVN = {
downloadComplete: 'Tải plugin "{{name}}" hoàn tất',
installFailed: 'Cài đặt thất bại, vui lòng thử lại sau',
loadFailed: 'Lấy danh sách plugin thất bại, vui lòng thử lại sau',
installed: 'Đã cài đặt',
updateAvailable: 'Có bản cập nhật',
alreadyInstalled: '{{name}} đã được cài đặt',
noDescription: 'Không có mô tả',
recommendation: {
pause: 'Tạm dừng tự động xoay',
+3
View File
@@ -715,6 +715,9 @@ const zhHans = {
installConfirm: '确定要安装插件 "{{name}}" ({{version}}) 吗?',
downloadComplete: '插件 "{{name}}" 下载完成',
installFailed: '安装失败,请稍后重试',
installed: '已安装',
updateAvailable: '有更新',
alreadyInstalled: '{{name}} 已安装',
loadFailed: '获取插件列表失败,请稍后重试',
noDescription: '暂无描述',
recommendation: {
+3
View File
@@ -719,6 +719,9 @@ const zhHant = {
downloadComplete: '插件 "{{name}}" 下載完成',
installFailed: '安裝失敗,請稍後重試',
loadFailed: '取得插件列表失敗,請稍後重試',
installed: '已安裝',
updateAvailable: '有更新',
alreadyInstalled: '{{name}} 已安裝',
noDescription: '暫無描述',
recommendation: {
pause: '暫停自動輪播',