yyqdbngt opened a new pull request, #5333:
URL: https://github.com/apache/rocketmq-dashboard/pull/5333
### Which Issue(s) This PR Fixes
- Fixes #5163
### Brief Description
`web/src/pages/ai/promptTemplates.ts` kept every custom template under one
fixed key,
`PROMPT_TEMPLATE_STORAGE_KEY = 'rocketmq-studio-ai-prompt-templates'`
(`readCustomPromptTemplates`,
`writeCustomPromptTemplates`, and the empty-list branch of
`deleteCustomPromptTemplate` all used it
verbatim). A `localStorage` key is per browser profile, not per account, so
on a shared workstation
the templates of the operator who signed in last were listed for the next
one: readable, applicable
(the wording is copied into the composer in full) and deletable, and
deleting silently removed the
other operator's saved prompt.
The storage layer now derives the owner itself, so no caller can forget it:
`promptTemplateStorageKey(owner)` yields
`…-ai-prompt-templates:v2:user-id:<userId>` for a signed-in
account and `…:v2:single-user` for the unauthenticated single-user mode the
application falls back to
when no login is required (`AuthGate` clears the session in that case). The
owner is
`readAuthSession().userId` from `stores/authStorage` — the same value
`useAuthStore` exposes and the
component layer already renders — so `PromptTemplateModal.tsx` needs no
signature change and cannot
drift from the account it is showing.
The legacy ownerless key is handled on the first read of a scoped key that
is still empty:
- **no account signed in** — it is this browser's own data, so it moves to
the single-user key and is
kept, and the legacy key is removed behind the move;
- **an account signed in** — it belongs to whoever used this browser last,
so it is not adopted (that
would reproduce the disclosure) and the legacy key is cleared instead.
Built-in templates stay shared and untouched, the corrupt-JSON repair from
#4575 still applies to the
scoped keys, and a `localStorage` that throws still reads as "storage
unavailable" rather than as an
error: `readAuthSession` and the legacy takeover both happen inside the
existing guard.
`promptTemplates.test.ts` gains the four cases the issue asks for and moves
its two raw-key
assertions from the legacy key onto the scoped one.
### How Did You Test This Change?
Red — the new and updated cases, written and run first, with
`promptTemplates.ts` still unmodified:
```
$ cd web && npx vitest run src/pages/ai/promptTemplates.test.ts
❯ src/pages/ai/promptTemplates.test.ts (16 tests | 5 failed) 61ms
× repairs corrupt stored JSON instead of treating storage as
unavailable 10ms
× keeps the custom templates of one account away from another 6ms
× scopes a delete to the account that owns the template 2ms
× does not adopt the ownerless legacy catalog for a signed-in account
1ms
× migrates the ownerless legacy catalog in single-user mode 1ms
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
FAIL src/pages/ai/promptTemplates.test.ts > AI prompt templates > keeps
the custom templates of one account away from another
AssertionError: expected 1 to be +0 // Object.is equality
- Expected
+ Received
- 0
+ 1
❯ src/pages/ai/promptTemplates.test.ts:200:53
198| // The same browser profile, a different account: none of
alice's …
199| localStorage.setItem(USER_ID_STORAGE_KEY, '22');
200| expect(loadPromptTemplateCatalog().customCount).toBe(0);
FAIL src/pages/ai/promptTemplates.test.ts > AI prompt templates > scopes a
delete to the account that owns the template
AssertionError: expected +0 to be 1 // Object.is equality
❯ src/pages/ai/promptTemplates.test.ts:222:53
FAIL src/pages/ai/promptTemplates.test.ts > AI prompt templates > does not
adopt the ownerless legacy catalog for a signed-in account
AssertionError: expected 1 to be +0 // Object.is equality
❯ src/pages/ai/promptTemplates.test.ts:236:33
Test Files 1 failed (1)
Tests 5 failed | 11 passed (16)
```
Green — the same file after the fix, and the whole AI page suite (it
contains the template modal
tests, which exercise save / apply / delete end to end):
```
$ cd web && npx vitest run src/pages/ai/promptTemplates.test.ts
Test Files 1 passed (1)
Tests 16 passed (16)
$ cd web && npx vitest run src/pages/ai
Test Files 17 passed (17)
Tests 198 passed (198)
```
Also run for the two touched files: `npx tsc -b` (exit 0) and
`npx eslint src/pages/ai/promptTemplates.ts
src/pages/ai/promptTemplates.test.ts` (exit 0).
### Checklist
- [x] One coherent change; unrelated modifications are not bundled in
- [x] Commit subject follows Conventional Commits
- [x] Tests added or updated for non-trivial changes
- [x] New UI text has both Chinese and English entries under `web/src/i18n/`
— not applicable: no
user-facing string is added, the modal keeps the strings it already
had.
- [x] Architecture constraints stay green (`mvn test` runs the ArchUnit
checks) — not applicable:
the change is web-only and touches no Java module.
- [x] New source files carry the ASF license header — no new file; both
touched files keep theirs.
- [x] Documentation touched where behaviour changed (README / `docs/` /
in-app help) — not
applicable: the storage layout is internal and is documented at the
key helper in
`promptTemplates.ts`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]