#72 #73 Plusieurs terminaux par agent, qui continuent de tourner quand on va ailleurs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
eb293b8 parent: 658ef02 modified
.tickets/issues/0072-garder-les-terminaux-ouverts-et-leur-contenu-qua.yaml +19 -2 | @@ -6,7 +6,7 @@ author: | ||
| 6 | 6 | name: k33g |
| 7 | 7 | email: ph.charriere@gmail.com |
| 8 | 8 | createdAt: 2026-11-09T04:42:57.000Z |
| 9 | -updatedAt: 2026-11-09T04:42:57.000Z | |
| 9 | +updatedAt: 2026-11-09T05:17:35.000Z | |
| 10 | 10 | labels: |
| 11 | 11 | - kind::feature |
| 12 | 12 | - area::workspace |
| @@ -37,4 +37,21 @@ body: | | ||
| 37 | 37 | - Un indicateur dans le panneau de gauche montre qu'un agent a des terminaux ouverts (à confirmer). |
| 38 | 38 | - Tests frontend ; docs EN + FR. |
| 39 | 39 | tasks: [] |
| 40 | -comments: [] | |
| 40 | +comments: | |
| 41 | + - id: 1 | |
| 42 | + author: | |
| 43 | + name: k33g | |
| 44 | + email: ph.charriere@gmail.com | |
| 45 | + createdAt: 2026-11-09T05:17:35.000Z | |
| 46 | + body: | | |
| 47 | + Fait (avec #73, même commit : le même code). | |
| 48 | + - [terminalSessions.svelte.ts](frontend/src/lib/terminalSessions.svelte.ts) : les terminaux vivent hors de la vue de | |
| 49 | + l'agent ; chaque écran xterm.js a son propre élément, que [TerminalView.svelte](frontend/src/lib/agent/TerminalView.svelte) | |
| 50 | + déplace dans la page et rend en partant. Ce que le shell affiche pendant ce temps est écrit dans l'écran. | |
| 51 | + - La vue de l'agent se souvient de l'onglet affiché : en revenant, on retombe sur les terminaux. | |
| 52 | + - Indicateurs : nombre de terminaux en cours sur l'onglet **Terminal** et **⌨ n** sur la ligne de l'agent. | |
| 53 | + - Un terminal ne s'arrête que s'il est fermé, si son shell se termine, à l'ouverture d'un autre fichier de | |
| 54 | + configuration ou à la fermeture de Sidekicks. | |
| 55 | + - Vérifié dans l'application (Linux, Xvfb) : une boucle `echo tick` continue pendant 6 s passées dans la | |
| 56 | + conversation, l'écran est complet au retour ; quitter Sidekicks ne laisse aucun shell. | |
| 57 | + - Docs EN + FR (guide et explication). | |
| @@ -6,7 +6,7 @@ author: | |||
| 6 | name: k33g | 6 | name: k33g |
| 7 | email: ph.charriere@gmail.com | 7 | email: ph.charriere@gmail.com |
| 8 | createdAt: 2026-11-09T04:42:57.000Z | 8 | createdAt: 2026-11-09T04:42:57.000Z |
| 9 | -updatedAt: 2026-11-09T04:42:57.000Z | 9 | +updatedAt: 2026-11-09T05:17:35.000Z |
| 10 | labels: | 10 | labels: |
| 11 | - kind::feature | 11 | - kind::feature |
| 12 | - area::workspace | 12 | - area::workspace |
| @@ -37,4 +37,21 @@ body: | | |||
| 37 | - Un indicateur dans le panneau de gauche montre qu'un agent a des terminaux ouverts (à confirmer). | 37 | - Un indicateur dans le panneau de gauche montre qu'un agent a des terminaux ouverts (à confirmer). |
| 38 | - Tests frontend ; docs EN + FR. | 38 | - Tests frontend ; docs EN + FR. |
| 39 | tasks: [] | 39 | tasks: [] |
| 40 | -comments: [] | 40 | +comments: |
| 41 | + - id: 1 | ||
| 42 | + author: | ||
| 43 | + name: k33g | ||
| 44 | + email: ph.charriere@gmail.com | ||
| 45 | + createdAt: 2026-11-09T05:17:35.000Z | ||
| 46 | + body: | | ||
| 47 | + Fait (avec #73, même commit : le même code). | ||
| 48 | + - [terminalSessions.svelte.ts](frontend/src/lib/terminalSessions.svelte.ts) : les terminaux vivent hors de la vue de | ||
| 49 | + l'agent ; chaque écran xterm.js a son propre élément, que [TerminalView.svelte](frontend/src/lib/agent/TerminalView.svelte) | ||
| 50 | + déplace dans la page et rend en partant. Ce que le shell affiche pendant ce temps est écrit dans l'écran. | ||
| 51 | + - La vue de l'agent se souvient de l'onglet affiché : en revenant, on retombe sur les terminaux. | ||
| 52 | + - Indicateurs : nombre de terminaux en cours sur l'onglet **Terminal** et **⌨ n** sur la ligne de l'agent. | ||
| 53 | + - Un terminal ne s'arrête que s'il est fermé, si son shell se termine, à l'ouverture d'un autre fichier de | ||
| 54 | + configuration ou à la fermeture de Sidekicks. | ||
| 55 | + - Vérifié dans l'application (Linux, Xvfb) : une boucle `echo tick` continue pendant 6 s passées dans la | ||
| 56 | + conversation, l'écran est complet au retour ; quitter Sidekicks ne laisse aucun shell. | ||
| 57 | + - Docs EN + FR (guide et explication). | ||
modified
.tickets/issues/0073-ouvrir-plusieurs-terminaux-pour-un-meme-agent.yaml +17 -2 | @@ -6,7 +6,7 @@ author: | ||
| 6 | 6 | name: k33g |
| 7 | 7 | email: ph.charriere@gmail.com |
| 8 | 8 | createdAt: 2026-11-09T04:42:57.000Z |
| 9 | -updatedAt: 2026-11-09T04:42:57.000Z | |
| 9 | +updatedAt: 2026-11-09T05:17:35.000Z | |
| 10 | 10 | labels: |
| 11 | 11 | - kind::feature |
| 12 | 12 | - area::workspace |
| @@ -31,4 +31,19 @@ body: | | ||
| 31 | 31 | - Limite raisonnable du nombre de terminaux par agent (à définir), avec message si atteinte. |
| 32 | 32 | - Tests Go + frontend ; textes EN + FR ; docs EN + FR. |
| 33 | 33 | tasks: [] |
| 34 | -comments: [] | |
| 34 | +comments: | |
| 35 | + - id: 1 | |
| 36 | + author: | |
| 37 | + name: k33g | |
| 38 | + email: ph.charriere@gmail.com | |
| 39 | + createdAt: 2026-11-09T05:17:35.000Z | |
| 40 | + body: | | |
| 41 | + Fait (avec #72, même commit). | |
| 42 | + - Sous-onglets *Terminal 1*, *Terminal 2*… (le plus petit numéro libre), **+** pour en ouvrir un autre | |
| 43 | + (8 au plus par agent, `maxTerminalsPerAgent` dans [terminals.ts](frontend/src/lib/terminals.ts)), **×** pour en fermer un | |
| 44 | + (son shell et ses descendants s'arrêtent, les autres continuent). Le premier passage sur l'onglet en ouvre un. | |
| 45 | + - Un terminal dont le shell est terminé est barré, avec **Open a new shell**. | |
| 46 | + - Rien à changer côté Go : chaque terminal avait déjà son id. | |
| 47 | + - Pas fait : renommer un terminal. | |
| 48 | + - Vérifié dans l'application : 3 terminaux, fermeture de l'un (3 → 2 shells), `exit` puis relance. | |
| 49 | + - Test vitest (numérotation) ; textes EN + FR ; docs EN + FR. | |
| @@ -6,7 +6,7 @@ author: | |||
| 6 | name: k33g | 6 | name: k33g |
| 7 | email: ph.charriere@gmail.com | 7 | email: ph.charriere@gmail.com |
| 8 | createdAt: 2026-11-09T04:42:57.000Z | 8 | createdAt: 2026-11-09T04:42:57.000Z |
| 9 | -updatedAt: 2026-11-09T04:42:57.000Z | 9 | +updatedAt: 2026-11-09T05:17:35.000Z |
| 10 | labels: | 10 | labels: |
| 11 | - kind::feature | 11 | - kind::feature |
| 12 | - area::workspace | 12 | - area::workspace |
| @@ -31,4 +31,19 @@ body: | | |||
| 31 | - Limite raisonnable du nombre de terminaux par agent (à définir), avec message si atteinte. | 31 | - Limite raisonnable du nombre de terminaux par agent (à définir), avec message si atteinte. |
| 32 | - Tests Go + frontend ; textes EN + FR ; docs EN + FR. | 32 | - Tests Go + frontend ; textes EN + FR ; docs EN + FR. |
| 33 | tasks: [] | 33 | tasks: [] |
| 34 | -comments: [] | 34 | +comments: |
| 35 | + - id: 1 | ||
| 36 | + author: | ||
| 37 | + name: k33g | ||
| 38 | + email: ph.charriere@gmail.com | ||
| 39 | + createdAt: 2026-11-09T05:17:35.000Z | ||
| 40 | + body: | | ||
| 41 | + Fait (avec #72, même commit). | ||
| 42 | + - Sous-onglets *Terminal 1*, *Terminal 2*… (le plus petit numéro libre), **+** pour en ouvrir un autre | ||
| 43 | + (8 au plus par agent, `maxTerminalsPerAgent` dans [terminals.ts](frontend/src/lib/terminals.ts)), **×** pour en fermer un | ||
| 44 | + (son shell et ses descendants s'arrêtent, les autres continuent). Le premier passage sur l'onglet en ouvre un. | ||
| 45 | + - Un terminal dont le shell est terminé est barré, avec **Open a new shell**. | ||
| 46 | + - Rien à changer côté Go : chaque terminal avait déjà son id. | ||
| 47 | + - Pas fait : renommer un terminal. | ||
| 48 | + - Vérifié dans l'application : 3 terminaux, fermeture de l'un (3 → 2 shells), `exit` puis relance. | ||
| 49 | + - Test vitest (numérotation) ; textes EN + FR ; docs EN + FR. | ||
modified
docs/en/explanation/workspaces-and-terminals.md +2 -0 | @@ -24,6 +24,8 @@ A deletion from a graphical tool is expected to be reversible. On this computer, | ||
| 24 | 24 | |
| 25 | 25 | A terminal is a real shell behind a pseudo-terminal, so full-screen programs work and the size follows the panel. Closing it must end what was started from it — including programs sent to the background, which a simple "kill the shell" misses. In a container there is a trap: stopping the `docker exec` client leaves the shell running inside the container. Sidekicks therefore ends the shell's whole session on the container's side too. Both cases were found by testing, not by reading. |
| 26 | 26 | |
| 27 | +A terminal belongs to its agent, not to the view that shows it: a long build or a server is often started in one, then the user goes back to the conversation. So the terminals, and their screens, live outside the agent's view: they go on running, and printing, while the user looks elsewhere, and an agent may have several (issues #72 and #73). Closing one is always the user's decision; only quitting Sidekicks or opening another configuration file closes them all. | |
| 28 | + | |
| 27 | 29 | ## How it relates to the rest |
| 28 | 30 | |
| 29 | 31 | - [Channels and what each agent knows](channels-and-context.md): the conversation is the main way to work with an agent; the workspace is where to look when something needs checking. |
| @@ -24,6 +24,8 @@ A deletion from a graphical tool is expected to be reversible. On this computer, | |||
| 24 | 24 | ||
| 25 | A terminal is a real shell behind a pseudo-terminal, so full-screen programs work and the size follows the panel. Closing it must end what was started from it — including programs sent to the background, which a simple "kill the shell" misses. In a container there is a trap: stopping the `docker exec` client leaves the shell running inside the container. Sidekicks therefore ends the shell's whole session on the container's side too. Both cases were found by testing, not by reading. | 25 | A terminal is a real shell behind a pseudo-terminal, so full-screen programs work and the size follows the panel. Closing it must end what was started from it — including programs sent to the background, which a simple "kill the shell" misses. In a container there is a trap: stopping the `docker exec` client leaves the shell running inside the container. Sidekicks therefore ends the shell's whole session on the container's side too. Both cases were found by testing, not by reading. |
| 26 | 26 | ||
| 27 | +A terminal belongs to its agent, not to the view that shows it: a long build or a server is often started in one, then the user goes back to the conversation. So the terminals, and their screens, live outside the agent's view: they go on running, and printing, while the user looks elsewhere, and an agent may have several (issues #72 and #73). Closing one is always the user's decision; only quitting Sidekicks or opening another configuration file closes them all. | ||
| 28 | + | ||
| 27 | ## How it relates to the rest | 29 | ## How it relates to the rest |
| 28 | 30 | ||
| 29 | - [Channels and what each agent knows](channels-and-context.md): the conversation is the main way to work with an agent; the workspace is where to look when something needs checking. | 31 | - [Channels and what each agent knows](channels-and-context.md): the conversation is the main way to work with an agent; the workspace is where to look when something needs checking. |
modified
docs/en/how-to/work-with-an-agents-files.md +5 -1 | @@ -61,7 +61,11 @@ A protected file shows 🔒 instead of ✏️ and 🗑 and opens read only; it c | ||
| 61 | 61 | |
| 62 | 62 | ## Run commands in the agent's folder |
| 63 | 63 | |
| 64 | -Click **Terminal**: a shell opens in the agent's folder — yours on this computer, bash or sh inside a container. Full-screen programs (`vim`, `htop`) work and follow the size of the panel. The shell keeps running while you look at **Files**; leaving the agent's view ends it, with everything it started. When the shell ends (`exit`), **Open a new shell** starts another one. | |
| 64 | +Click **Terminal**: a shell opens in the agent's folder — yours on this computer, bash or sh inside a container. Full-screen programs (`vim`, `htop`) work and follow the size of the panel. When the shell ends (`exit`), **Open a new shell** starts another one. | |
| 65 | + | |
| 66 | +- **Several terminals**: **+** next to the terminals opens another one (8 at most per agent), named *Terminal 1*, *Terminal 2*…; click a name to show it. **×** closes a terminal: its shell ends, with everything it started; the others go on. | |
| 67 | +- **They keep running**: go to **Files**, a conversation, a channel or another agent — the terminals go on, with what runs in them, and what they print meanwhile is kept. Coming back to the agent shows its terminals as they are. The **Terminal** tab shows how many are running, and the agent's row in the **Agents** panel shows **⌨ 2**. | |
| 68 | +- A terminal ends only when you close it, when its shell exits, when another configuration file is opened, or when Sidekicks quits. | |
| 65 | 69 | |
| 66 | 70 | ## Read the agent's log |
| 67 | 71 | |
| @@ -61,7 +61,11 @@ A protected file shows 🔒 instead of ✏️ and 🗑 and opens read only; it c | |||
| 61 | 61 | ||
| 62 | ## Run commands in the agent's folder | 62 | ## Run commands in the agent's folder |
| 63 | 63 | ||
| 64 | -Click **Terminal**: a shell opens in the agent's folder — yours on this computer, bash or sh inside a container. Full-screen programs (`vim`, `htop`) work and follow the size of the panel. The shell keeps running while you look at **Files**; leaving the agent's view ends it, with everything it started. When the shell ends (`exit`), **Open a new shell** starts another one. | 64 | +Click **Terminal**: a shell opens in the agent's folder — yours on this computer, bash or sh inside a container. Full-screen programs (`vim`, `htop`) work and follow the size of the panel. When the shell ends (`exit`), **Open a new shell** starts another one. |
| 65 | + | ||
| 66 | +- **Several terminals**: **+** next to the terminals opens another one (8 at most per agent), named *Terminal 1*, *Terminal 2*…; click a name to show it. **×** closes a terminal: its shell ends, with everything it started; the others go on. | ||
| 67 | +- **They keep running**: go to **Files**, a conversation, a channel or another agent — the terminals go on, with what runs in them, and what they print meanwhile is kept. Coming back to the agent shows its terminals as they are. The **Terminal** tab shows how many are running, and the agent's row in the **Agents** panel shows **⌨ 2**. | ||
| 68 | +- A terminal ends only when you close it, when its shell exits, when another configuration file is opened, or when Sidekicks quits. | ||
| 65 | 69 | ||
| 66 | ## Read the agent's log | 70 | ## Read the agent's log |
| 67 | 71 | ||
modified
docs/fr/explanation/workspaces-et-terminaux.md +2 -0 | @@ -24,6 +24,8 @@ Une suppression depuis un outil graphique est censée être réversible. Sur cet | ||
| 24 | 24 | |
| 25 | 25 | Un terminal est un vrai shell derrière un pseudo-terminal : les programmes plein écran fonctionnent et la taille suit le panneau. Le fermer doit terminer ce qui y a été lancé — y compris les programmes mis en arrière-plan, qu'un simple « tuer le shell » rate. Dans un conteneur, il y a un piège : arrêter le client `docker exec` laisse le shell tourner dans le conteneur. Sidekicks termine donc toute la session du shell, côté conteneur aussi. Les deux cas ont été trouvés en testant, pas en lisant. |
| 26 | 26 | |
| 27 | +Un terminal appartient à son agent, pas à la vue qui l'affiche : on y lance souvent un long build ou un serveur, puis on retourne à la conversation. Les terminaux, et leurs écrans, vivent donc en dehors de la vue de l'agent : ils continuent de tourner, et d'afficher, pendant que l'utilisateur regarde ailleurs, et un agent peut en avoir plusieurs (issues #72 et #73). En fermer un est toujours la décision de l'utilisateur ; seuls la fermeture de Sidekicks ou l'ouverture d'un autre fichier de configuration les ferment tous. | |
| 28 | + | |
| 27 | 29 | ## Les liens avec le reste |
| 28 | 30 | |
| 29 | 31 | - [Les channels et ce que sait chaque agent](channels-et-contexte.md) : la conversation est la façon principale de travailler avec un agent ; le workspace est l'endroit où regarder quand il faut vérifier quelque chose. |
| @@ -24,6 +24,8 @@ Une suppression depuis un outil graphique est censée être réversible. Sur cet | |||
| 24 | 24 | ||
| 25 | Un terminal est un vrai shell derrière un pseudo-terminal : les programmes plein écran fonctionnent et la taille suit le panneau. Le fermer doit terminer ce qui y a été lancé — y compris les programmes mis en arrière-plan, qu'un simple « tuer le shell » rate. Dans un conteneur, il y a un piège : arrêter le client `docker exec` laisse le shell tourner dans le conteneur. Sidekicks termine donc toute la session du shell, côté conteneur aussi. Les deux cas ont été trouvés en testant, pas en lisant. | 25 | Un terminal est un vrai shell derrière un pseudo-terminal : les programmes plein écran fonctionnent et la taille suit le panneau. Le fermer doit terminer ce qui y a été lancé — y compris les programmes mis en arrière-plan, qu'un simple « tuer le shell » rate. Dans un conteneur, il y a un piège : arrêter le client `docker exec` laisse le shell tourner dans le conteneur. Sidekicks termine donc toute la session du shell, côté conteneur aussi. Les deux cas ont été trouvés en testant, pas en lisant. |
| 26 | 26 | ||
| 27 | +Un terminal appartient à son agent, pas à la vue qui l'affiche : on y lance souvent un long build ou un serveur, puis on retourne à la conversation. Les terminaux, et leurs écrans, vivent donc en dehors de la vue de l'agent : ils continuent de tourner, et d'afficher, pendant que l'utilisateur regarde ailleurs, et un agent peut en avoir plusieurs (issues #72 et #73). En fermer un est toujours la décision de l'utilisateur ; seuls la fermeture de Sidekicks ou l'ouverture d'un autre fichier de configuration les ferment tous. | ||
| 28 | + | ||
| 27 | ## Les liens avec le reste | 29 | ## Les liens avec le reste |
| 28 | 30 | ||
| 29 | - [Les channels et ce que sait chaque agent](channels-et-contexte.md) : la conversation est la façon principale de travailler avec un agent ; le workspace est l'endroit où regarder quand il faut vérifier quelque chose. | 31 | - [Les channels et ce que sait chaque agent](channels-et-contexte.md) : la conversation est la façon principale de travailler avec un agent ; le workspace est l'endroit où regarder quand il faut vérifier quelque chose. |
modified
docs/fr/how-to/fichiers-et-terminal-d-un-agent.md +5 -1 | @@ -61,7 +61,11 @@ Un fichier protégé affiche 🔒 au lieu de ✏️ et 🗑 et s'ouvre en lectur | ||
| 61 | 61 | |
| 62 | 62 | ## Lancer des commandes dans le dossier de l'agent |
| 63 | 63 | |
| 64 | -Cliquez sur **Terminal** : un shell s'ouvre dans le dossier de l'agent — le vôtre sur cet ordinateur, bash ou sh dans un conteneur. Les programmes plein écran (`vim`, `htop`) fonctionnent et suivent la taille du panneau. Le shell continue de tourner pendant que vous regardez **Files** ; quitter la vue de l'agent le termine, avec tout ce qu'il a lancé. Quand le shell se termine (`exit`), **Open a new shell** en ouvre un autre. | |
| 64 | +Cliquez sur **Terminal** : un shell s'ouvre dans le dossier de l'agent — le vôtre sur cet ordinateur, bash ou sh dans un conteneur. Les programmes plein écran (`vim`, `htop`) fonctionnent et suivent la taille du panneau. Quand le shell se termine (`exit`), **Open a new shell** en ouvre un autre. | |
| 65 | + | |
| 66 | +- **Plusieurs terminaux** : **+** à côté des terminaux en ouvre un autre (8 au plus par agent), nommés *Terminal 1*, *Terminal 2*… ; cliquez sur un nom pour l'afficher. **×** ferme un terminal : son shell se termine, avec tout ce qu'il a lancé ; les autres continuent. | |
| 67 | +- **Ils continuent de tourner** : allez dans **Files**, une conversation, un channel ou un autre agent — les terminaux continuent, avec ce qui y tourne, et ce qu'ils affichent entre-temps est gardé. En revenant sur l'agent, ses terminaux sont tels quels. L'onglet **Terminal** indique combien tournent, et la ligne de l'agent dans le panneau **Agents** affiche **⌨ 2**. | |
| 68 | +- Un terminal ne s'arrête que si vous le fermez, si son shell se termine, si un autre fichier de configuration est ouvert, ou quand Sidekicks se ferme. | |
| 65 | 69 | |
| 66 | 70 | ## Lire le journal de l'agent |
| 67 | 71 | |
| @@ -61,7 +61,11 @@ Un fichier protégé affiche 🔒 au lieu de ✏️ et 🗑 et s'ouvre en lectur | |||
| 61 | 61 | ||
| 62 | ## Lancer des commandes dans le dossier de l'agent | 62 | ## Lancer des commandes dans le dossier de l'agent |
| 63 | 63 | ||
| 64 | -Cliquez sur **Terminal** : un shell s'ouvre dans le dossier de l'agent — le vôtre sur cet ordinateur, bash ou sh dans un conteneur. Les programmes plein écran (`vim`, `htop`) fonctionnent et suivent la taille du panneau. Le shell continue de tourner pendant que vous regardez **Files** ; quitter la vue de l'agent le termine, avec tout ce qu'il a lancé. Quand le shell se termine (`exit`), **Open a new shell** en ouvre un autre. | 64 | +Cliquez sur **Terminal** : un shell s'ouvre dans le dossier de l'agent — le vôtre sur cet ordinateur, bash ou sh dans un conteneur. Les programmes plein écran (`vim`, `htop`) fonctionnent et suivent la taille du panneau. Quand le shell se termine (`exit`), **Open a new shell** en ouvre un autre. |
| 65 | + | ||
| 66 | +- **Plusieurs terminaux** : **+** à côté des terminaux en ouvre un autre (8 au plus par agent), nommés *Terminal 1*, *Terminal 2*… ; cliquez sur un nom pour l'afficher. **×** ferme un terminal : son shell se termine, avec tout ce qu'il a lancé ; les autres continuent. | ||
| 67 | +- **Ils continuent de tourner** : allez dans **Files**, une conversation, un channel ou un autre agent — les terminaux continuent, avec ce qui y tourne, et ce qu'ils affichent entre-temps est gardé. En revenant sur l'agent, ses terminaux sont tels quels. L'onglet **Terminal** indique combien tournent, et la ligne de l'agent dans le panneau **Agents** affiche **⌨ 2**. | ||
| 68 | +- Un terminal ne s'arrête que si vous le fermez, si son shell se termine, si un autre fichier de configuration est ouvert, ou quand Sidekicks se ferme. | ||
| 65 | 69 | ||
| 66 | ## Lire le journal de l'agent | 70 | ## Lire le journal de l'agent |
| 67 | 71 | ||
modified
frontend/src/lib/agent/AgentView.svelte +53 -9 | @@ -1,4 +1,10 @@ | ||
| 1 | 1 | <!-- Copyright (c) 2026 Philippe Charrière — Sidekicks Source-Available License 1.0, see LICENSE --> |
| 2 | +<script lang="ts" module> | |
| 3 | + // The tab shown last for each agent: coming back to an agent shows its | |
| 4 | + // terminals again if they were in view (issue #72). | |
| 5 | + const lastTab: Record<string, 'files' | 'terminal' | 'log'> = {} | |
| 6 | +</script> | |
| 7 | + | |
| 2 | 8 | <script lang="ts"> |
| 3 | 9 | // An agent's workspace in the centre (wireframe 5, issue #27): its state |
| 4 | 10 | // and where it runs, then its files, a terminal and its log. |
| @@ -12,6 +18,8 @@ | ||
| 12 | 18 | import FilesTab from './FilesTab.svelte' |
| 13 | 19 | import TerminalView from './TerminalView.svelte' |
| 14 | 20 | import ErrorText from '../chat/ErrorText.svelte' |
| 21 | + import { activeTerminal, closeTerminal, newTerminal, runningTerminals, terminalsOf } from '../terminalSessions.svelte' | |
| 22 | + import { maxTerminalsPerAgent } from '../terminals' | |
| 15 | 23 | |
| 16 | 24 | let { agent: name }: { agent: string } = $props() |
| 17 | 25 | |
| @@ -20,10 +28,12 @@ | ||
| 20 | 28 | const stateKey = $derived(`agent.state.${agent?.state ?? ''}`) |
| 21 | 29 | |
| 22 | 30 | type Tab = 'files' | 'terminal' | 'log' |
| 23 | - let tab: Tab = $state('files') | |
| 24 | - // The terminal is kept once opened, hidden on the other tabs, so that its | |
| 25 | - // shell survives a look at the files. | |
| 26 | - let terminalOpened = $state(false) | |
| 31 | + // svelte-ignore state_referenced_locally | |
| 32 | + let tab: Tab = $state(lastTab[name] ?? 'files') | |
| 33 | + // The terminals of the agent (issues #72 and #73) live outside this view: | |
| 34 | + // they keep running when it goes away. | |
| 35 | + const terminals = $derived(terminalsOf(name)) | |
| 36 | + const current = $derived(terminals.find((session) => session.key === activeTerminal[name]) ?? terminals[0]) | |
| 27 | 37 | let location = $state('') |
| 28 | 38 | let revealable = $state(false) |
| 29 | 39 | let problem = $state('') |
| @@ -45,7 +55,9 @@ | ||
| 45 | 55 | |
| 46 | 56 | function show(next: Tab) { |
| 47 | 57 | tab = next |
| 48 | - if (next === 'terminal') terminalOpened = true | |
| 58 | + lastTab[name] = next | |
| 59 | + // The first look at the Terminal tab opens a shell. | |
| 60 | + if (next === 'terminal' && terminals.length === 0) newTerminal(name) | |
| 49 | 61 | } |
| 50 | 62 | |
| 51 | 63 | onMount(async () => { |
| @@ -93,7 +105,9 @@ | ||
| 93 | 105 | |
| 94 | 106 | <div class="tabs" role="tablist"> |
| 95 | 107 | {#each ['files', 'terminal', 'log'] as const as each (each)} |
| 96 | - <button role="tab" aria-selected={tab === each} class:active={tab === each} onclick={() => show(each)}>{t(`workspace.tab.${each}`)}</button> | |
| 108 | + <button role="tab" aria-selected={tab === each} class:active={tab === each} onclick={() => show(each)}> | |
| 109 | + {t(`workspace.tab.${each}`)}{#if each === 'terminal' && runningTerminals(name) > 0}<span class="count" title={t('terminal.running', { count: runningTerminals(name) })}>{runningTerminals(name)}</span>{/if} | |
| 110 | + </button> | |
| 97 | 111 | {/each} |
| 98 | 112 | </div> |
| 99 | 113 | |
| @@ -106,9 +120,26 @@ | ||
| 106 | 120 | <div class="pane" class:hidden={tab !== 'files'}> |
| 107 | 121 | <FilesTab agent={name} inContainer={!location.startsWith('local')} {revealable} /> |
| 108 | 122 | </div> |
| 109 | - {#if terminalOpened} | |
| 110 | - <div class="pane" class:hidden={tab !== 'terminal'}> | |
| 111 | - <TerminalView agent={name} visible={tab === 'terminal'} /> | |
| 123 | + {#if tab === 'terminal'} | |
| 124 | + <div class="pane terminals"> | |
| 125 | + <div class="terminal-tabs" role="tablist" aria-label={t('terminal.list')}> | |
| 126 | + {#each terminals as session (session.key)} | |
| 127 | + <span class="terminal-tab" class:active={session.key === current?.key} class:ended={session.ended}> | |
| 128 | + <button role="tab" aria-selected={session.key === current?.key} onclick={() => (activeTerminal[name] = session.key)}> | |
| 129 | + {t('terminal.name', { number: session.number })} | |
| 130 | + </button> | |
| 131 | + <button class="close" onclick={() => closeTerminal(session.key)} aria-label={t('terminal.close', { name: t('terminal.name', { number: session.number }) })} title={t('terminal.close', { name: t('terminal.name', { number: session.number }) })}>×</button> | |
| 132 | + </span> | |
| 133 | + {/each} | |
| 134 | + <button class="add" onclick={() => newTerminal(name)} disabled={terminals.length >= maxTerminalsPerAgent} title={terminals.length >= maxTerminalsPerAgent ? t('terminal.limit', { count: maxTerminalsPerAgent }) : t('terminal.new')} aria-label={t('terminal.new')}>+</button> | |
| 135 | + </div> | |
| 136 | + {#if current} | |
| 137 | + {#key current.key} | |
| 138 | + <div class="terminal-pane"><TerminalView session={current} visible={tab === 'terminal'} /></div> | |
| 139 | + {/key} | |
| 140 | + {:else} | |
| 141 | + <p class="no-terminal">{t('terminal.none')}</p> | |
| 142 | + {/if} | |
| 112 | 143 | </div> |
| 113 | 144 | {/if} |
| 114 | 145 | {/if} |
| @@ -139,5 +170,18 @@ | ||
| 139 | 170 | .body { flex: 1; min-height: 0; padding: 12px 18px 14px; display: flex; flex-direction: column; } |
| 140 | 171 | .pane { flex: 1; min-height: 0; } |
| 141 | 172 | .hidden { display: none; } |
| 173 | + .count { margin-left: 6px; padding: 0 6px; border-radius: 8px; background: var(--accent); color: #fff; font-size: 11px; font-weight: 600; } | |
| 174 | + .terminals { display: flex; flex-direction: column; gap: 6px; } | |
| 175 | + .terminal-tabs { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; } | |
| 176 | + .terminal-tab { display: inline-flex; align-items: center; border: 1px solid var(--border); border-radius: 6px; font-size: 12px; overflow: hidden; } | |
| 177 | + .terminal-tab button { border: none; border-radius: 0; background: none; padding: 3px 8px; font-size: 12px; } | |
| 178 | + .terminal-tab .close { padding: 3px 6px; color: var(--muted); } | |
| 179 | + .terminal-tab .close:hover { color: var(--danger); } | |
| 180 | + .terminal-tab.active { border-color: var(--accent); } | |
| 181 | + .terminal-tab.active button:first-child { color: var(--accent); font-weight: 600; } | |
| 182 | + .terminal-tab.ended button:first-child { text-decoration: line-through; color: var(--muted); } | |
| 183 | + .add { border: 1px dashed var(--border); background: none; padding: 2px 8px; font-size: 12px; } | |
| 184 | + .terminal-pane { flex: 1; min-height: 0; } | |
| 185 | + .no-terminal { color: var(--muted); font-size: 13px; } | |
| 142 | 186 | .error { color: var(--danger); font-size: 13px; margin: 6px 18px; } |
| 143 | 187 | </style> |
| @@ -1,4 +1,10 @@ | |||
| 1 | <!-- Copyright (c) 2026 Philippe Charrière — Sidekicks Source-Available License 1.0, see LICENSE --> | 1 | <!-- Copyright (c) 2026 Philippe Charrière — Sidekicks Source-Available License 1.0, see LICENSE --> |
| 2 | +<script lang="ts" module> | ||
| 3 | + // The tab shown last for each agent: coming back to an agent shows its | ||
| 4 | + // terminals again if they were in view (issue #72). | ||
| 5 | + const lastTab: Record<string, 'files' | 'terminal' | 'log'> = {} | ||
| 6 | +</script> | ||
| 7 | + | ||
| 2 | <script lang="ts"> | 8 | <script lang="ts"> |
| 3 | // An agent's workspace in the centre (wireframe 5, issue #27): its state | 9 | // An agent's workspace in the centre (wireframe 5, issue #27): its state |
| 4 | // and where it runs, then its files, a terminal and its log. | 10 | // and where it runs, then its files, a terminal and its log. |
| @@ -12,6 +18,8 @@ | |||
| 12 | import FilesTab from './FilesTab.svelte' | 18 | import FilesTab from './FilesTab.svelte' |
| 13 | import TerminalView from './TerminalView.svelte' | 19 | import TerminalView from './TerminalView.svelte' |
| 14 | import ErrorText from '../chat/ErrorText.svelte' | 20 | import ErrorText from '../chat/ErrorText.svelte' |
| 21 | + import { activeTerminal, closeTerminal, newTerminal, runningTerminals, terminalsOf } from '../terminalSessions.svelte' | ||
| 22 | + import { maxTerminalsPerAgent } from '../terminals' | ||
| 15 | 23 | ||
| 16 | let { agent: name }: { agent: string } = $props() | 24 | let { agent: name }: { agent: string } = $props() |
| 17 | 25 | ||
| @@ -20,10 +28,12 @@ | |||
| 20 | const stateKey = $derived(`agent.state.${agent?.state ?? ''}`) | 28 | const stateKey = $derived(`agent.state.${agent?.state ?? ''}`) |
| 21 | 29 | ||
| 22 | type Tab = 'files' | 'terminal' | 'log' | 30 | type Tab = 'files' | 'terminal' | 'log' |
| 23 | - let tab: Tab = $state('files') | 31 | + // svelte-ignore state_referenced_locally |
| 24 | - // The terminal is kept once opened, hidden on the other tabs, so that its | 32 | + let tab: Tab = $state(lastTab[name] ?? 'files') |
| 25 | - // shell survives a look at the files. | 33 | + // The terminals of the agent (issues #72 and #73) live outside this view: |
| 26 | - let terminalOpened = $state(false) | 34 | + // they keep running when it goes away. |
| 35 | + const terminals = $derived(terminalsOf(name)) | ||
| 36 | + const current = $derived(terminals.find((session) => session.key === activeTerminal[name]) ?? terminals[0]) | ||
| 27 | let location = $state('') | 37 | let location = $state('') |
| 28 | let revealable = $state(false) | 38 | let revealable = $state(false) |
| 29 | let problem = $state('') | 39 | let problem = $state('') |
| @@ -45,7 +55,9 @@ | |||
| 45 | 55 | ||
| 46 | function show(next: Tab) { | 56 | function show(next: Tab) { |
| 47 | tab = next | 57 | tab = next |
| 48 | - if (next === 'terminal') terminalOpened = true | 58 | + lastTab[name] = next |
| 59 | + // The first look at the Terminal tab opens a shell. | ||
| 60 | + if (next === 'terminal' && terminals.length === 0) newTerminal(name) | ||
| 49 | } | 61 | } |
| 50 | 62 | ||
| 51 | onMount(async () => { | 63 | onMount(async () => { |
| @@ -93,7 +105,9 @@ | |||
| 93 | 105 | ||
| 94 | <div class="tabs" role="tablist"> | 106 | <div class="tabs" role="tablist"> |
| 95 | {#each ['files', 'terminal', 'log'] as const as each (each)} | 107 | {#each ['files', 'terminal', 'log'] as const as each (each)} |
| 96 | - <button role="tab" aria-selected={tab === each} class:active={tab === each} onclick={() => show(each)}>{t(`workspace.tab.${each}`)}</button> | 108 | + <button role="tab" aria-selected={tab === each} class:active={tab === each} onclick={() => show(each)}> |
| 109 | + {t(`workspace.tab.${each}`)}{#if each === 'terminal' && runningTerminals(name) > 0}<span class="count" title={t('terminal.running', { count: runningTerminals(name) })}>{runningTerminals(name)}</span>{/if} | ||
| 110 | + </button> | ||
| 97 | {/each} | 111 | {/each} |
| 98 | </div> | 112 | </div> |
| 99 | 113 | ||
| @@ -106,9 +120,26 @@ | |||
| 106 | <div class="pane" class:hidden={tab !== 'files'}> | 120 | <div class="pane" class:hidden={tab !== 'files'}> |
| 107 | <FilesTab agent={name} inContainer={!location.startsWith('local')} {revealable} /> | 121 | <FilesTab agent={name} inContainer={!location.startsWith('local')} {revealable} /> |
| 108 | </div> | 122 | </div> |
| 109 | - {#if terminalOpened} | 123 | + {#if tab === 'terminal'} |
| 110 | - <div class="pane" class:hidden={tab !== 'terminal'}> | 124 | + <div class="pane terminals"> |
| 111 | - <TerminalView agent={name} visible={tab === 'terminal'} /> | 125 | + <div class="terminal-tabs" role="tablist" aria-label={t('terminal.list')}> |
| 126 | + {#each terminals as session (session.key)} | ||
| 127 | + <span class="terminal-tab" class:active={session.key === current?.key} class:ended={session.ended}> | ||
| 128 | + <button role="tab" aria-selected={session.key === current?.key} onclick={() => (activeTerminal[name] = session.key)}> | ||
| 129 | + {t('terminal.name', { number: session.number })} | ||
| 130 | + </button> | ||
| 131 | + <button class="close" onclick={() => closeTerminal(session.key)} aria-label={t('terminal.close', { name: t('terminal.name', { number: session.number }) })} title={t('terminal.close', { name: t('terminal.name', { number: session.number }) })}>×</button> | ||
| 132 | + </span> | ||
| 133 | + {/each} | ||
| 134 | + <button class="add" onclick={() => newTerminal(name)} disabled={terminals.length >= maxTerminalsPerAgent} title={terminals.length >= maxTerminalsPerAgent ? t('terminal.limit', { count: maxTerminalsPerAgent }) : t('terminal.new')} aria-label={t('terminal.new')}>+</button> | ||
| 135 | + </div> | ||
| 136 | + {#if current} | ||
| 137 | + {#key current.key} | ||
| 138 | + <div class="terminal-pane"><TerminalView session={current} visible={tab === 'terminal'} /></div> | ||
| 139 | + {/key} | ||
| 140 | + {:else} | ||
| 141 | + <p class="no-terminal">{t('terminal.none')}</p> | ||
| 142 | + {/if} | ||
| 112 | </div> | 143 | </div> |
| 113 | {/if} | 144 | {/if} |
| 114 | {/if} | 145 | {/if} |
| @@ -139,5 +170,18 @@ | |||
| 139 | .body { flex: 1; min-height: 0; padding: 12px 18px 14px; display: flex; flex-direction: column; } | 170 | .body { flex: 1; min-height: 0; padding: 12px 18px 14px; display: flex; flex-direction: column; } |
| 140 | .pane { flex: 1; min-height: 0; } | 171 | .pane { flex: 1; min-height: 0; } |
| 141 | .hidden { display: none; } | 172 | .hidden { display: none; } |
| 173 | + .count { margin-left: 6px; padding: 0 6px; border-radius: 8px; background: var(--accent); color: #fff; font-size: 11px; font-weight: 600; } | ||
| 174 | + .terminals { display: flex; flex-direction: column; gap: 6px; } | ||
| 175 | + .terminal-tabs { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; } | ||
| 176 | + .terminal-tab { display: inline-flex; align-items: center; border: 1px solid var(--border); border-radius: 6px; font-size: 12px; overflow: hidden; } | ||
| 177 | + .terminal-tab button { border: none; border-radius: 0; background: none; padding: 3px 8px; font-size: 12px; } | ||
| 178 | + .terminal-tab .close { padding: 3px 6px; color: var(--muted); } | ||
| 179 | + .terminal-tab .close:hover { color: var(--danger); } | ||
| 180 | + .terminal-tab.active { border-color: var(--accent); } | ||
| 181 | + .terminal-tab.active button:first-child { color: var(--accent); font-weight: 600; } | ||
| 182 | + .terminal-tab.ended button:first-child { text-decoration: line-through; color: var(--muted); } | ||
| 183 | + .add { border: 1px dashed var(--border); background: none; padding: 2px 8px; font-size: 12px; } | ||
| 184 | + .terminal-pane { flex: 1; min-height: 0; } | ||
| 185 | + .no-terminal { color: var(--muted); font-size: 13px; } | ||
| 142 | .error { color: var(--danger); font-size: 13px; margin: 6px 18px; } | 186 | .error { color: var(--danger); font-size: 13px; margin: 6px 18px; } |
| 143 | </style> | 187 | </style> |
modified
frontend/src/lib/agent/TerminalView.svelte +18 -65 | @@ -1,95 +1,48 @@ | ||
| 1 | 1 | <!-- Copyright (c) 2026 Philippe Charrière — Sidekicks Source-Available License 1.0, see LICENSE --> |
| 2 | 2 | <script lang="ts"> |
| 3 | - // A shell in the agent's folder (issue #31): xterm.js here, a | |
| 4 | - // pseudo-terminal on the Go side (local, or docker exec). The shell ends | |
| 5 | - // when this view goes away. | |
| 3 | + // Shows one terminal of an agent (issues #31, #72): xterm.js here, a | |
| 4 | + // pseudo-terminal on the Go side (local, docker exec or sbx exec). The | |
| 5 | + // terminal lives in terminalSessions: when this view goes away, its shell | |
| 6 | + // and its screen go on, and come back as they are. | |
| 6 | 7 | import { onMount } from 'svelte' |
| 7 | - import { Terminal } from '@xterm/xterm' | |
| 8 | - import { FitAddon } from '@xterm/addon-fit' | |
| 9 | 8 | import '@xterm/xterm/css/xterm.css' |
| 10 | - import { TerminalService } from '../../../bindings/git.rickub.com/bots-garden/sidekicks/internal/app' | |
| 11 | 9 | import { t } from '../i18n/index.svelte' |
| 12 | - import { listenTerminal } from '../terminals' | |
| 10 | + import { attachTerminal, fitTerminal, focusTerminal, restartTerminal, type TerminalSession } from '../terminalSessions.svelte' | |
| 13 | 11 | import ErrorText from '../chat/ErrorText.svelte' |
| 14 | 12 | |
| 15 | - let { agent, visible }: { agent: string; visible: boolean } = $props() | |
| 13 | + let { session, visible }: { session: TerminalSession; visible: boolean } = $props() | |
| 16 | 14 | |
| 17 | 15 | let host: HTMLElement |
| 18 | - let terminal: Terminal | undefined | |
| 19 | - let fit: FitAddon | undefined | |
| 20 | - let id = '' | |
| 21 | - let stopListening = () => {} | |
| 22 | - let ended = $state(false) | |
| 23 | - let error = $state('') | |
| 24 | - | |
| 25 | - async function openShell() { | |
| 26 | - if (!terminal || !fit) return | |
| 27 | - ended = false | |
| 28 | - error = '' | |
| 29 | - terminal.reset() | |
| 30 | - fit.fit() | |
| 31 | - try { | |
| 32 | - id = (await TerminalService.Open(agent, terminal.cols, terminal.rows)) ?? '' | |
| 33 | - } catch (problem) { | |
| 34 | - error = problem instanceof Error ? problem.message : String(problem) | |
| 35 | - return | |
| 36 | - } | |
| 37 | - stopListening = listenTerminal( | |
| 38 | - id, | |
| 39 | - (data) => terminal?.write(data), | |
| 40 | - () => { | |
| 41 | - ended = true | |
| 42 | - id = '' | |
| 43 | - }, | |
| 44 | - ) | |
| 45 | - terminal.focus() | |
| 46 | - } | |
| 47 | - | |
| 48 | - // Follows the size of the panel; the shell is told the new size. | |
| 49 | - function resize() { | |
| 50 | - if (!terminal || !fit || !visible || host.clientWidth === 0) return | |
| 51 | - fit.fit() | |
| 52 | - if (id) void TerminalService.Resize(id, terminal.cols, terminal.rows) | |
| 53 | - } | |
| 54 | 16 | |
| 55 | 17 | onMount(() => { |
| 56 | - terminal = new Terminal({ | |
| 57 | - fontFamily: getComputedStyle(document.documentElement).getPropertyValue('--mono') || 'monospace', | |
| 58 | - fontSize: 13, | |
| 59 | - cursorBlink: true, | |
| 60 | - theme: { background: '#16181d', foreground: '#e6e6e6' }, | |
| 61 | - }) | |
| 62 | - fit = new FitAddon() | |
| 63 | - terminal.loadAddon(fit) | |
| 64 | - terminal.open(host) | |
| 65 | - terminal.onData((data) => { | |
| 66 | - if (id) void TerminalService.Write(id, data) | |
| 18 | + const key = session.key | |
| 19 | + const detach = attachTerminal(key, host) | |
| 20 | + // Follows the size of the panel; the shell is told the new size. | |
| 21 | + const observer = new ResizeObserver(() => { | |
| 22 | + if (visible) fitTerminal(key) | |
| 67 | 23 | }) |
| 68 | - const observer = new ResizeObserver(() => resize()) | |
| 69 | 24 | observer.observe(host) |
| 70 | - void openShell() | |
| 71 | 25 | return () => { |
| 72 | 26 | observer.disconnect() |
| 73 | - stopListening() | |
| 74 | - if (id) void TerminalService.Close(id) | |
| 75 | - terminal?.dispose() | |
| 27 | + detach() | |
| 76 | 28 | } |
| 77 | 29 | }) |
| 78 | 30 | |
| 79 | 31 | $effect(() => { |
| 32 | + const key = session.key | |
| 80 | 33 | if (visible) queueMicrotask(() => { |
| 81 | - resize() | |
| 82 | - terminal?.focus() | |
| 34 | + fitTerminal(key) | |
| 35 | + focusTerminal(key) | |
| 83 | 36 | }) |
| 84 | 37 | }) |
| 85 | 38 | </script> |
| 86 | 39 | |
| 87 | 40 | <div class="terminal"> |
| 88 | 41 | <div class="screen" bind:this={host}></div> |
| 89 | - {#if ended || error} | |
| 42 | + {#if session.ended} | |
| 90 | 43 | <div class="ended"> |
| 91 | - <span>{#if error}{t('terminal.error')} <ErrorText raw={error} />{:else}{t('terminal.ended')}{/if}</span> | |
| 92 | - <button onclick={openShell}>{t('terminal.restart')}</button> | |
| 44 | + <span>{#if session.error}{t('terminal.error')} <ErrorText raw={session.error} />{:else}{t('terminal.ended')}{/if}</span> | |
| 45 | + <button onclick={() => restartTerminal(session.key)}>{t('terminal.restart')}</button> | |
| 93 | 46 | </div> |
| 94 | 47 | {/if} |
| 95 | 48 | </div> |
| @@ -1,95 +1,48 @@ | |||
| 1 | <!-- Copyright (c) 2026 Philippe Charrière — Sidekicks Source-Available License 1.0, see LICENSE --> | 1 | <!-- Copyright (c) 2026 Philippe Charrière — Sidekicks Source-Available License 1.0, see LICENSE --> |
| 2 | <script lang="ts"> | 2 | <script lang="ts"> |
| 3 | - // A shell in the agent's folder (issue #31): xterm.js here, a | 3 | + // Shows one terminal of an agent (issues #31, #72): xterm.js here, a |
| 4 | - // pseudo-terminal on the Go side (local, or docker exec). The shell ends | 4 | + // pseudo-terminal on the Go side (local, docker exec or sbx exec). The |
| 5 | - // when this view goes away. | 5 | + // terminal lives in terminalSessions: when this view goes away, its shell |
| 6 | + // and its screen go on, and come back as they are. | ||
| 6 | import { onMount } from 'svelte' | 7 | import { onMount } from 'svelte' |
| 7 | - import { Terminal } from '@xterm/xterm' | ||
| 8 | - import { FitAddon } from '@xterm/addon-fit' | ||
| 9 | import '@xterm/xterm/css/xterm.css' | 8 | import '@xterm/xterm/css/xterm.css' |
| 10 | - import { TerminalService } from '../../../bindings/git.rickub.com/bots-garden/sidekicks/internal/app' | ||
| 11 | import { t } from '../i18n/index.svelte' | 9 | import { t } from '../i18n/index.svelte' |
| 12 | - import { listenTerminal } from '../terminals' | 10 | + import { attachTerminal, fitTerminal, focusTerminal, restartTerminal, type TerminalSession } from '../terminalSessions.svelte' |
| 13 | import ErrorText from '../chat/ErrorText.svelte' | 11 | import ErrorText from '../chat/ErrorText.svelte' |
| 14 | 12 | ||
| 15 | - let { agent, visible }: { agent: string; visible: boolean } = $props() | 13 | + let { session, visible }: { session: TerminalSession; visible: boolean } = $props() |
| 16 | 14 | ||
| 17 | let host: HTMLElement | 15 | let host: HTMLElement |
| 18 | - let terminal: Terminal | undefined | ||
| 19 | - let fit: FitAddon | undefined | ||
| 20 | - let id = '' | ||
| 21 | - let stopListening = () => {} | ||
| 22 | - let ended = $state(false) | ||
| 23 | - let error = $state('') | ||
| 24 | - | ||
| 25 | - async function openShell() { | ||
| 26 | - if (!terminal || !fit) return | ||
| 27 | - ended = false | ||
| 28 | - error = '' | ||
| 29 | - terminal.reset() | ||
| 30 | - fit.fit() | ||
| 31 | - try { | ||
| 32 | - id = (await TerminalService.Open(agent, terminal.cols, terminal.rows)) ?? '' | ||
| 33 | - } catch (problem) { | ||
| 34 | - error = problem instanceof Error ? problem.message : String(problem) | ||
| 35 | - return | ||
| 36 | - } | ||
| 37 | - stopListening = listenTerminal( | ||
| 38 | - id, | ||
| 39 | - (data) => terminal?.write(data), | ||
| 40 | - () => { | ||
| 41 | - ended = true | ||
| 42 | - id = '' | ||
| 43 | - }, | ||
| 44 | - ) | ||
| 45 | - terminal.focus() | ||
| 46 | - } | ||
| 47 | - | ||
| 48 | - // Follows the size of the panel; the shell is told the new size. | ||
| 49 | - function resize() { | ||
| 50 | - if (!terminal || !fit || !visible || host.clientWidth === 0) return | ||
| 51 | - fit.fit() | ||
| 52 | - if (id) void TerminalService.Resize(id, terminal.cols, terminal.rows) | ||
| 53 | - } | ||
| 54 | 16 | ||
| 55 | onMount(() => { | 17 | onMount(() => { |
| 56 | - terminal = new Terminal({ | 18 | + const key = session.key |
| 57 | - fontFamily: getComputedStyle(document.documentElement).getPropertyValue('--mono') || 'monospace', | 19 | + const detach = attachTerminal(key, host) |
| 58 | - fontSize: 13, | 20 | + // Follows the size of the panel; the shell is told the new size. |
| 59 | - cursorBlink: true, | 21 | + const observer = new ResizeObserver(() => { |
| 60 | - theme: { background: '#16181d', foreground: '#e6e6e6' }, | 22 | + if (visible) fitTerminal(key) |
| 61 | - }) | ||
| 62 | - fit = new FitAddon() | ||
| 63 | - terminal.loadAddon(fit) | ||
| 64 | - terminal.open(host) | ||
| 65 | - terminal.onData((data) => { | ||
| 66 | - if (id) void TerminalService.Write(id, data) | ||
| 67 | }) | 23 | }) |
| 68 | - const observer = new ResizeObserver(() => resize()) | ||
| 69 | observer.observe(host) | 24 | observer.observe(host) |
| 70 | - void openShell() | ||
| 71 | return () => { | 25 | return () => { |
| 72 | observer.disconnect() | 26 | observer.disconnect() |
| 73 | - stopListening() | 27 | + detach() |
| 74 | - if (id) void TerminalService.Close(id) | ||
| 75 | - terminal?.dispose() | ||
| 76 | } | 28 | } |
| 77 | }) | 29 | }) |
| 78 | 30 | ||
| 79 | $effect(() => { | 31 | $effect(() => { |
| 32 | + const key = session.key | ||
| 80 | if (visible) queueMicrotask(() => { | 33 | if (visible) queueMicrotask(() => { |
| 81 | - resize() | 34 | + fitTerminal(key) |
| 82 | - terminal?.focus() | 35 | + focusTerminal(key) |
| 83 | }) | 36 | }) |
| 84 | }) | 37 | }) |
| 85 | </script> | 38 | </script> |
| 86 | 39 | ||
| 87 | <div class="terminal"> | 40 | <div class="terminal"> |
| 88 | <div class="screen" bind:this={host}></div> | 41 | <div class="screen" bind:this={host}></div> |
| 89 | - {#if ended || error} | 42 | + {#if session.ended} |
| 90 | <div class="ended"> | 43 | <div class="ended"> |
| 91 | - <span>{#if error}{t('terminal.error')} <ErrorText raw={error} />{:else}{t('terminal.ended')}{/if}</span> | 44 | + <span>{#if session.error}{t('terminal.error')} <ErrorText raw={session.error} />{:else}{t('terminal.ended')}{/if}</span> |
| 92 | - <button onclick={openShell}>{t('terminal.restart')}</button> | 45 | + <button onclick={() => restartTerminal(session.key)}>{t('terminal.restart')}</button> |
| 93 | </div> | 46 | </div> |
| 94 | {/if} | 47 | {/if} |
| 95 | </div> | 48 | </div> |
modified
frontend/src/lib/i18n/messages.ts +14 -0 | @@ -284,6 +284,13 @@ export const en = { | ||
| 284 | 284 | 'terminal.ended': 'The shell has ended.', |
| 285 | 285 | 'terminal.restart': 'Open a new shell', |
| 286 | 286 | 'terminal.error': 'Cannot open a terminal.', |
| 287 | + 'terminal.list': 'Terminals of the agent', | |
| 288 | + 'terminal.name': 'Terminal {number}', | |
| 289 | + 'terminal.new': 'Open another terminal', | |
| 290 | + 'terminal.close': 'Close {name}: ends its shell and what it runs', | |
| 291 | + 'terminal.limit': 'At most {count} terminals per agent', | |
| 292 | + 'terminal.none': 'No terminal open. Click + to open one.', | |
| 293 | + 'terminal.running': '{count} terminal(s) running, even while you look elsewhere', | |
| 287 | 294 | |
| 288 | 295 | // welcome and the "add an agent" assistant |
| 289 | 296 | 'welcome.title': 'Welcome to Sidekicks', |
| @@ -649,6 +656,13 @@ export const fr: Record<MessageKey, string> = { | ||
| 649 | 656 | 'terminal.ended': 'Le shell est terminé.', |
| 650 | 657 | 'terminal.restart': 'Ouvrir un nouveau shell', |
| 651 | 658 | 'terminal.error': "Impossible d'ouvrir un terminal.", |
| 659 | + 'terminal.list': "Terminaux de l'agent", | |
| 660 | + 'terminal.name': 'Terminal {number}', | |
| 661 | + 'terminal.new': 'Ouvrir un autre terminal', | |
| 662 | + 'terminal.close': 'Fermer {name} : arrête son shell et ce qui y tourne', | |
| 663 | + 'terminal.limit': 'Au plus {count} terminaux par agent', | |
| 664 | + 'terminal.none': 'Aucun terminal ouvert. Cliquez sur + pour en ouvrir un.', | |
| 665 | + 'terminal.running': '{count} terminal(aux) en cours, même quand vous regardez ailleurs', | |
| 652 | 666 | |
| 653 | 667 | 'welcome.title': 'Bienvenue dans Sidekicks', |
| 654 | 668 | 'welcome.tagline': "Discutez avec des agents d'IA dans des channels, comme avec des collègues.", |
| @@ -284,6 +284,13 @@ export const en = { | |||
| 284 | 'terminal.ended': 'The shell has ended.', | 284 | 'terminal.ended': 'The shell has ended.', |
| 285 | 'terminal.restart': 'Open a new shell', | 285 | 'terminal.restart': 'Open a new shell', |
| 286 | 'terminal.error': 'Cannot open a terminal.', | 286 | 'terminal.error': 'Cannot open a terminal.', |
| 287 | + 'terminal.list': 'Terminals of the agent', | ||
| 288 | + 'terminal.name': 'Terminal {number}', | ||
| 289 | + 'terminal.new': 'Open another terminal', | ||
| 290 | + 'terminal.close': 'Close {name}: ends its shell and what it runs', | ||
| 291 | + 'terminal.limit': 'At most {count} terminals per agent', | ||
| 292 | + 'terminal.none': 'No terminal open. Click + to open one.', | ||
| 293 | + 'terminal.running': '{count} terminal(s) running, even while you look elsewhere', | ||
| 287 | 294 | ||
| 288 | // welcome and the "add an agent" assistant | 295 | // welcome and the "add an agent" assistant |
| 289 | 'welcome.title': 'Welcome to Sidekicks', | 296 | 'welcome.title': 'Welcome to Sidekicks', |
| @@ -649,6 +656,13 @@ export const fr: Record<MessageKey, string> = { | |||
| 649 | 'terminal.ended': 'Le shell est terminé.', | 656 | 'terminal.ended': 'Le shell est terminé.', |
| 650 | 'terminal.restart': 'Ouvrir un nouveau shell', | 657 | 'terminal.restart': 'Ouvrir un nouveau shell', |
| 651 | 'terminal.error': "Impossible d'ouvrir un terminal.", | 658 | 'terminal.error': "Impossible d'ouvrir un terminal.", |
| 659 | + 'terminal.list': "Terminaux de l'agent", | ||
| 660 | + 'terminal.name': 'Terminal {number}', | ||
| 661 | + 'terminal.new': 'Ouvrir un autre terminal', | ||
| 662 | + 'terminal.close': 'Fermer {name} : arrête son shell et ce qui y tourne', | ||
| 663 | + 'terminal.limit': 'Au plus {count} terminaux par agent', | ||
| 664 | + 'terminal.none': 'Aucun terminal ouvert. Cliquez sur + pour en ouvrir un.', | ||
| 665 | + 'terminal.running': '{count} terminal(aux) en cours, même quand vous regardez ailleurs', | ||
| 652 | 666 | ||
| 653 | 'welcome.title': 'Bienvenue dans Sidekicks', | 667 | 'welcome.title': 'Bienvenue dans Sidekicks', |
| 654 | 'welcome.tagline': "Discutez avec des agents d'IA dans des channels, comme avec des collègues.", | 668 | 'welcome.tagline': "Discutez avec des agents d'IA dans des channels, comme avec des collègues.", |
modified
frontend/src/lib/panels/AgentsPanel.svelte +6 -1 | @@ -1,5 +1,6 @@ | ||
| 1 | 1 | <!-- Copyright (c) 2026 Philippe Charrière — Sidekicks Source-Available License 1.0, see LICENSE --> |
| 2 | 2 | <script lang="ts"> |
| 3 | + import { runningTerminals } from '../terminalSessions.svelte' | |
| 3 | 4 | import { hasMessage, t, type MessageKey } from '../i18n/index.svelte' |
| 4 | 5 | import ActionError from '../chat/ActionError.svelte' |
| 5 | 6 | import ErrorText from '../chat/ErrorText.svelte' |
| @@ -30,6 +31,9 @@ | ||
| 30 | 31 | {#if unreadOf(agent.name)} |
| 31 | 32 | <span class="badge" title={t('channels.unread', { count: unreadOf(agent.name) })}>{unreadOf(agent.name)}</span> |
| 32 | 33 | {/if} |
| 34 | + {#if runningTerminals(agent.name)} | |
| 35 | + <span class="terminals" title={t('terminal.running', { count: runningTerminals(agent.name) })} aria-label={t('terminal.running', { count: runningTerminals(agent.name) })}>⌨ {runningTerminals(agent.name)}</span> | |
| 36 | + {/if} | |
| 33 | 37 | <span class="state">{hasMessage(`agent.state.${agent.state}`) ? t(`agent.state.${agent.state}` as MessageKey) : agent.state}{agent.outdated ? t('agent.outdated') : ''}</span> |
| 34 | 38 | <span class="actions"> |
| 35 | 39 | <button onclick={() => openWorkspace(agent.name)} aria-label={t('workspace.open', { agent: agent.title })} title={t('workspace.open', { agent: agent.title })}>📂</button> |
| @@ -67,7 +71,8 @@ | ||
| 67 | 71 | .state { grid-column: 2; grid-row: 2; color: var(--muted); font-size: 12px; } |
| 68 | 72 | .actions { grid-column: 3; grid-row: 1 / span 2; display: flex; gap: 4px; opacity: 0; } |
| 69 | 73 | li:hover .actions, li:focus-within .actions { opacity: 1; } |
| 70 | - li:hover .badge, li:focus-within .badge { visibility: hidden; } | |
| 74 | + .terminals { grid-column: 3; grid-row: 2; justify-self: end; font-size: 11px; color: var(--muted); } | |
| 75 | + li:hover .badge, li:focus-within .badge, li:hover .terminals, li:focus-within .terminals { visibility: hidden; } | |
| 71 | 76 | .actions button { padding: 1px 7px; font-size: 11px; } |
| 72 | 77 | .failure { grid-column: 2 / span 2; grid-row: 3; margin: 2px 0 0; color: var(--danger); font-size: 12px; white-space: pre-wrap; } |
| 73 | 78 | .empty { color: var(--muted); padding: 0 8px; } |
| @@ -1,5 +1,6 @@ | |||
| 1 | <!-- Copyright (c) 2026 Philippe Charrière — Sidekicks Source-Available License 1.0, see LICENSE --> | 1 | <!-- Copyright (c) 2026 Philippe Charrière — Sidekicks Source-Available License 1.0, see LICENSE --> |
| 2 | <script lang="ts"> | 2 | <script lang="ts"> |
| 3 | + import { runningTerminals } from '../terminalSessions.svelte' | ||
| 3 | import { hasMessage, t, type MessageKey } from '../i18n/index.svelte' | 4 | import { hasMessage, t, type MessageKey } from '../i18n/index.svelte' |
| 4 | import ActionError from '../chat/ActionError.svelte' | 5 | import ActionError from '../chat/ActionError.svelte' |
| 5 | import ErrorText from '../chat/ErrorText.svelte' | 6 | import ErrorText from '../chat/ErrorText.svelte' |
| @@ -30,6 +31,9 @@ | |||
| 30 | {#if unreadOf(agent.name)} | 31 | {#if unreadOf(agent.name)} |
| 31 | <span class="badge" title={t('channels.unread', { count: unreadOf(agent.name) })}>{unreadOf(agent.name)}</span> | 32 | <span class="badge" title={t('channels.unread', { count: unreadOf(agent.name) })}>{unreadOf(agent.name)}</span> |
| 32 | {/if} | 33 | {/if} |
| 34 | + {#if runningTerminals(agent.name)} | ||
| 35 | + <span class="terminals" title={t('terminal.running', { count: runningTerminals(agent.name) })} aria-label={t('terminal.running', { count: runningTerminals(agent.name) })}>⌨ {runningTerminals(agent.name)}</span> | ||
| 36 | + {/if} | ||
| 33 | <span class="state">{hasMessage(`agent.state.${agent.state}`) ? t(`agent.state.${agent.state}` as MessageKey) : agent.state}{agent.outdated ? t('agent.outdated') : ''}</span> | 37 | <span class="state">{hasMessage(`agent.state.${agent.state}`) ? t(`agent.state.${agent.state}` as MessageKey) : agent.state}{agent.outdated ? t('agent.outdated') : ''}</span> |
| 34 | <span class="actions"> | 38 | <span class="actions"> |
| 35 | <button onclick={() => openWorkspace(agent.name)} aria-label={t('workspace.open', { agent: agent.title })} title={t('workspace.open', { agent: agent.title })}>📂</button> | 39 | <button onclick={() => openWorkspace(agent.name)} aria-label={t('workspace.open', { agent: agent.title })} title={t('workspace.open', { agent: agent.title })}>📂</button> |
| @@ -67,7 +71,8 @@ | |||
| 67 | .state { grid-column: 2; grid-row: 2; color: var(--muted); font-size: 12px; } | 71 | .state { grid-column: 2; grid-row: 2; color: var(--muted); font-size: 12px; } |
| 68 | .actions { grid-column: 3; grid-row: 1 / span 2; display: flex; gap: 4px; opacity: 0; } | 72 | .actions { grid-column: 3; grid-row: 1 / span 2; display: flex; gap: 4px; opacity: 0; } |
| 69 | li:hover .actions, li:focus-within .actions { opacity: 1; } | 73 | li:hover .actions, li:focus-within .actions { opacity: 1; } |
| 70 | - li:hover .badge, li:focus-within .badge { visibility: hidden; } | 74 | + .terminals { grid-column: 3; grid-row: 2; justify-self: end; font-size: 11px; color: var(--muted); } |
| 75 | + li:hover .badge, li:focus-within .badge, li:hover .terminals, li:focus-within .terminals { visibility: hidden; } | ||
| 71 | .actions button { padding: 1px 7px; font-size: 11px; } | 76 | .actions button { padding: 1px 7px; font-size: 11px; } |
| 72 | .failure { grid-column: 2 / span 2; grid-row: 3; margin: 2px 0 0; color: var(--danger); font-size: 12px; white-space: pre-wrap; } | 77 | .failure { grid-column: 2 / span 2; grid-row: 3; margin: 2px 0 0; color: var(--danger); font-size: 12px; white-space: pre-wrap; } |
| 73 | .empty { color: var(--muted); padding: 0 8px; } | 78 | .empty { color: var(--muted); padding: 0 8px; } |
added
frontend/src/lib/terminalSessions.svelte.ts +194 -0 | new file mode 100644 | ||
| @@ -0,0 +1,194 @@ | ||
| 1 | +// Copyright (c) 2026 Philippe Charrière — Sidekicks Source-Available License 1.0, see LICENSE | |
| 2 | + | |
| 3 | +// The terminals of the agents, kept outside the views (issues #72 and #73): | |
| 4 | +// an agent may have several, and each one goes on running — its shell, its | |
| 5 | +// screen, the output arriving meanwhile — when the user goes to a | |
| 6 | +// conversation, a channel or another agent. A terminal ends only when the | |
| 7 | +// user closes it, its shell exits, or the application closes its terminals | |
| 8 | +// (another configuration file opened, the application quits). | |
| 9 | +// | |
| 10 | +// The screen of a terminal is an xterm.js instance drawn in its own element; | |
| 11 | +// a TerminalView shows it by moving that element into the page, and lets it | |
| 12 | +// go when it disappears. What the shell prints while nobody looks is written | |
| 13 | +// into the instance all the same. | |
| 14 | + | |
| 15 | +import { Terminal } from '@xterm/xterm' | |
| 16 | +import { FitAddon } from '@xterm/addon-fit' | |
| 17 | +import { TerminalService } from '../../bindings/git.rickub.com/bots-garden/sidekicks/internal/app' | |
| 18 | +import { listenTerminal, maxTerminalsPerAgent, nextTerminalNumber } from './terminals' | |
| 19 | + | |
| 20 | +/** What the views show of a terminal. */ | |
| 21 | +export type TerminalSession = { | |
| 22 | + key: string | |
| 23 | + agent: string | |
| 24 | + number: number | |
| 25 | + /** The shell has exited (or could not start: error). */ | |
| 26 | + ended: boolean | |
| 27 | + error: string | |
| 28 | +} | |
| 29 | + | |
| 30 | +// The parts of a terminal that are not displayed state: xterm objects must | |
| 31 | +// never become Svelte proxies. | |
| 32 | +type Screen = { | |
| 33 | + xterm: Terminal | |
| 34 | + fit: FitAddon | |
| 35 | + element: HTMLDivElement | |
| 36 | + opened: boolean | |
| 37 | + id: string | |
| 38 | + stop: () => void | |
| 39 | +} | |
| 40 | + | |
| 41 | +/** The terminals of each agent, in the order they were opened. */ | |
| 42 | +export const terminalSessions: Record<string, TerminalSession[]> = $state({}) | |
| 43 | +/** The terminal shown for each agent. */ | |
| 44 | +export const activeTerminal: Record<string, string> = $state({}) | |
| 45 | + | |
| 46 | +const screens = new Map<string, Screen>() | |
| 47 | +let sequence = 0 | |
| 48 | + | |
| 49 | +/** The terminals of an agent. */ | |
| 50 | +export function terminalsOf(agent: string): TerminalSession[] { | |
| 51 | + return terminalSessions[agent] ?? [] | |
| 52 | +} | |
| 53 | + | |
| 54 | +/** How many terminals of an agent still run a shell. */ | |
| 55 | +export function runningTerminals(agent: string): number { | |
| 56 | + return terminalsOf(agent).filter((session) => !session.ended).length | |
| 57 | +} | |
| 58 | + | |
| 59 | +/** | |
| 60 | + * Adds a terminal to the agent and shows it; its shell starts once the view | |
| 61 | + * has measured it. Returns its key, or "" when the agent has as many | |
| 62 | + * terminals as allowed. | |
| 63 | + */ | |
| 64 | +export function newTerminal(agent: string): string { | |
| 65 | + const sessions = terminalsOf(agent) | |
| 66 | + if (sessions.length >= maxTerminalsPerAgent) return '' | |
| 67 | + const key = `term-${++sequence}` | |
| 68 | + const xterm = new Terminal({ | |
| 69 | + fontFamily: getComputedStyle(document.documentElement).getPropertyValue('--mono') || 'monospace', | |
| 70 | + fontSize: 13, | |
| 71 | + cursorBlink: true, | |
| 72 | + theme: { background: '#16181d', foreground: '#e6e6e6' }, | |
| 73 | + }) | |
| 74 | + const fit = new FitAddon() | |
| 75 | + xterm.loadAddon(fit) | |
| 76 | + const element = document.createElement('div') | |
| 77 | + element.className = 'terminal-screen' | |
| 78 | + element.style.height = '100%' | |
| 79 | + const screen: Screen = { xterm, fit, element, opened: false, id: '', stop: () => {} } | |
| 80 | + xterm.onData((data) => { | |
| 81 | + if (screen.id) void TerminalService.Write(screen.id, data) | |
| 82 | + }) | |
| 83 | + screens.set(key, screen) | |
| 84 | + terminalSessions[agent] = [ | |
| 85 | + ...sessions, | |
| 86 | + { key, agent, number: nextTerminalNumber(sessions.map((session) => session.number)), ended: false, error: '' }, | |
| 87 | + ] | |
| 88 | + activeTerminal[agent] = key | |
| 89 | + return key | |
| 90 | +} | |
| 91 | + | |
| 92 | +function sessionOf(key: string): TerminalSession | undefined { | |
| 93 | + for (const sessions of Object.values(terminalSessions)) { | |
| 94 | + const found = sessions.find((session) => session.key === key) | |
| 95 | + if (found) return found | |
| 96 | + } | |
| 97 | + return undefined | |
| 98 | +} | |
| 99 | + | |
| 100 | +// Starts the shell of a terminal, of the size of its screen. | |
| 101 | +async function start(key: string) { | |
| 102 | + const screen = screens.get(key) | |
| 103 | + const session = sessionOf(key) | |
| 104 | + if (!screen || !session) return | |
| 105 | + session.ended = false | |
| 106 | + session.error = '' | |
| 107 | + try { | |
| 108 | + screen.id = (await TerminalService.Open(session.agent, screen.xterm.cols, screen.xterm.rows)) ?? '' | |
| 109 | + } catch (problem) { | |
| 110 | + session.ended = true | |
| 111 | + session.error = problem instanceof Error ? problem.message : String(problem) | |
| 112 | + return | |
| 113 | + } | |
| 114 | + if (!screens.has(key)) { | |
| 115 | + // Closed while the shell was starting. | |
| 116 | + void TerminalService.Close(screen.id) | |
| 117 | + return | |
| 118 | + } | |
| 119 | + screen.stop = listenTerminal( | |
| 120 | + screen.id, | |
| 121 | + (data) => screen.xterm.write(data), | |
| 122 | + () => { | |
| 123 | + screen.id = '' | |
| 124 | + const ended = sessionOf(key) | |
| 125 | + if (ended) ended.ended = true | |
| 126 | + }, | |
| 127 | + ) | |
| 128 | +} | |
| 129 | + | |
| 130 | +/** | |
| 131 | + * Shows a terminal in host: its screen moves there, takes the size of host, | |
| 132 | + * and its shell starts the first time. Returns the function that lets it go | |
| 133 | + * (the shell keeps running). | |
| 134 | + */ | |
| 135 | +export function attachTerminal(key: string, host: HTMLElement): () => void { | |
| 136 | + const screen = screens.get(key) | |
| 137 | + if (!screen) return () => {} | |
| 138 | + host.appendChild(screen.element) | |
| 139 | + if (!screen.opened) { | |
| 140 | + screen.xterm.open(screen.element) | |
| 141 | + screen.opened = true | |
| 142 | + screen.fit.fit() | |
| 143 | + void start(key) | |
| 144 | + } else { | |
| 145 | + fitTerminal(key) | |
| 146 | + screen.xterm.refresh(0, screen.xterm.rows - 1) | |
| 147 | + } | |
| 148 | + return () => { | |
| 149 | + if (screen.element.parentElement === host) host.removeChild(screen.element) | |
| 150 | + } | |
| 151 | +} | |
| 152 | + | |
| 153 | +/** Gives a shown terminal the size of its place; the shell is told. */ | |
| 154 | +export function fitTerminal(key: string) { | |
| 155 | + const screen = screens.get(key) | |
| 156 | + if (!screen?.opened || !screen.element.isConnected || screen.element.clientWidth === 0) return | |
| 157 | + screen.fit.fit() | |
| 158 | + if (screen.id) void TerminalService.Resize(screen.id, screen.xterm.cols, screen.xterm.rows) | |
| 159 | +} | |
| 160 | + | |
| 161 | +/** Puts the keyboard in a terminal. */ | |
| 162 | +export function focusTerminal(key: string) { | |
| 163 | + screens.get(key)?.xterm.focus() | |
| 164 | +} | |
| 165 | + | |
| 166 | +/** Starts a new shell in a terminal whose shell has ended. */ | |
| 167 | +export function restartTerminal(key: string) { | |
| 168 | + const screen = screens.get(key) | |
| 169 | + if (!screen) return | |
| 170 | + screen.stop() | |
| 171 | + screen.xterm.reset() | |
| 172 | + void start(key) | |
| 173 | +} | |
| 174 | + | |
| 175 | +/** Ends a terminal: its shell and everything it started, then its screen. */ | |
| 176 | +export function closeTerminal(key: string) { | |
| 177 | + const session = sessionOf(key) | |
| 178 | + const screen = screens.get(key) | |
| 179 | + screens.delete(key) | |
| 180 | + if (screen) { | |
| 181 | + screen.stop() | |
| 182 | + if (screen.id) void TerminalService.Close(screen.id) | |
| 183 | + screen.element.remove() | |
| 184 | + screen.xterm.dispose() | |
| 185 | + } | |
| 186 | + if (!session) return | |
| 187 | + const sessions = terminalsOf(session.agent) | |
| 188 | + const index = sessions.findIndex((candidate) => candidate.key === key) | |
| 189 | + const rest = sessions.filter((candidate) => candidate.key !== key) | |
| 190 | + terminalSessions[session.agent] = rest | |
| 191 | + if (activeTerminal[session.agent] === key) { | |
| 192 | + activeTerminal[session.agent] = rest[Math.min(index, rest.length - 1)]?.key ?? '' | |
| 193 | + } | |
| 194 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,194 @@ | |||
| 1 | +// Copyright (c) 2026 Philippe Charrière — Sidekicks Source-Available License 1.0, see LICENSE | ||
| 2 | + | ||
| 3 | +// The terminals of the agents, kept outside the views (issues #72 and #73): | ||
| 4 | +// an agent may have several, and each one goes on running — its shell, its | ||
| 5 | +// screen, the output arriving meanwhile — when the user goes to a | ||
| 6 | +// conversation, a channel or another agent. A terminal ends only when the | ||
| 7 | +// user closes it, its shell exits, or the application closes its terminals | ||
| 8 | +// (another configuration file opened, the application quits). | ||
| 9 | +// | ||
| 10 | +// The screen of a terminal is an xterm.js instance drawn in its own element; | ||
| 11 | +// a TerminalView shows it by moving that element into the page, and lets it | ||
| 12 | +// go when it disappears. What the shell prints while nobody looks is written | ||
| 13 | +// into the instance all the same. | ||
| 14 | + | ||
| 15 | +import { Terminal } from '@xterm/xterm' | ||
| 16 | +import { FitAddon } from '@xterm/addon-fit' | ||
| 17 | +import { TerminalService } from '../../bindings/git.rickub.com/bots-garden/sidekicks/internal/app' | ||
| 18 | +import { listenTerminal, maxTerminalsPerAgent, nextTerminalNumber } from './terminals' | ||
| 19 | + | ||
| 20 | +/** What the views show of a terminal. */ | ||
| 21 | +export type TerminalSession = { | ||
| 22 | + key: string | ||
| 23 | + agent: string | ||
| 24 | + number: number | ||
| 25 | + /** The shell has exited (or could not start: error). */ | ||
| 26 | + ended: boolean | ||
| 27 | + error: string | ||
| 28 | +} | ||
| 29 | + | ||
| 30 | +// The parts of a terminal that are not displayed state: xterm objects must | ||
| 31 | +// never become Svelte proxies. | ||
| 32 | +type Screen = { | ||
| 33 | + xterm: Terminal | ||
| 34 | + fit: FitAddon | ||
| 35 | + element: HTMLDivElement | ||
| 36 | + opened: boolean | ||
| 37 | + id: string | ||
| 38 | + stop: () => void | ||
| 39 | +} | ||
| 40 | + | ||
| 41 | +/** The terminals of each agent, in the order they were opened. */ | ||
| 42 | +export const terminalSessions: Record<string, TerminalSession[]> = $state({}) | ||
| 43 | +/** The terminal shown for each agent. */ | ||
| 44 | +export const activeTerminal: Record<string, string> = $state({}) | ||
| 45 | + | ||
| 46 | +const screens = new Map<string, Screen>() | ||
| 47 | +let sequence = 0 | ||
| 48 | + | ||
| 49 | +/** The terminals of an agent. */ | ||
| 50 | +export function terminalsOf(agent: string): TerminalSession[] { | ||
| 51 | + return terminalSessions[agent] ?? [] | ||
| 52 | +} | ||
| 53 | + | ||
| 54 | +/** How many terminals of an agent still run a shell. */ | ||
| 55 | +export function runningTerminals(agent: string): number { | ||
| 56 | + return terminalsOf(agent).filter((session) => !session.ended).length | ||
| 57 | +} | ||
| 58 | + | ||
| 59 | +/** | ||
| 60 | + * Adds a terminal to the agent and shows it; its shell starts once the view | ||
| 61 | + * has measured it. Returns its key, or "" when the agent has as many | ||
| 62 | + * terminals as allowed. | ||
| 63 | + */ | ||
| 64 | +export function newTerminal(agent: string): string { | ||
| 65 | + const sessions = terminalsOf(agent) | ||
| 66 | + if (sessions.length >= maxTerminalsPerAgent) return '' | ||
| 67 | + const key = `term-${++sequence}` | ||
| 68 | + const xterm = new Terminal({ | ||
| 69 | + fontFamily: getComputedStyle(document.documentElement).getPropertyValue('--mono') || 'monospace', | ||
| 70 | + fontSize: 13, | ||
| 71 | + cursorBlink: true, | ||
| 72 | + theme: { background: '#16181d', foreground: '#e6e6e6' }, | ||
| 73 | + }) | ||
| 74 | + const fit = new FitAddon() | ||
| 75 | + xterm.loadAddon(fit) | ||
| 76 | + const element = document.createElement('div') | ||
| 77 | + element.className = 'terminal-screen' | ||
| 78 | + element.style.height = '100%' | ||
| 79 | + const screen: Screen = { xterm, fit, element, opened: false, id: '', stop: () => {} } | ||
| 80 | + xterm.onData((data) => { | ||
| 81 | + if (screen.id) void TerminalService.Write(screen.id, data) | ||
| 82 | + }) | ||
| 83 | + screens.set(key, screen) | ||
| 84 | + terminalSessions[agent] = [ | ||
| 85 | + ...sessions, | ||
| 86 | + { key, agent, number: nextTerminalNumber(sessions.map((session) => session.number)), ended: false, error: '' }, | ||
| 87 | + ] | ||
| 88 | + activeTerminal[agent] = key | ||
| 89 | + return key | ||
| 90 | +} | ||
| 91 | + | ||
| 92 | +function sessionOf(key: string): TerminalSession | undefined { | ||
| 93 | + for (const sessions of Object.values(terminalSessions)) { | ||
| 94 | + const found = sessions.find((session) => session.key === key) | ||
| 95 | + if (found) return found | ||
| 96 | + } | ||
| 97 | + return undefined | ||
| 98 | +} | ||
| 99 | + | ||
| 100 | +// Starts the shell of a terminal, of the size of its screen. | ||
| 101 | +async function start(key: string) { | ||
| 102 | + const screen = screens.get(key) | ||
| 103 | + const session = sessionOf(key) | ||
| 104 | + if (!screen || !session) return | ||
| 105 | + session.ended = false | ||
| 106 | + session.error = '' | ||
| 107 | + try { | ||
| 108 | + screen.id = (await TerminalService.Open(session.agent, screen.xterm.cols, screen.xterm.rows)) ?? '' | ||
| 109 | + } catch (problem) { | ||
| 110 | + session.ended = true | ||
| 111 | + session.error = problem instanceof Error ? problem.message : String(problem) | ||
| 112 | + return | ||
| 113 | + } | ||
| 114 | + if (!screens.has(key)) { | ||
| 115 | + // Closed while the shell was starting. | ||
| 116 | + void TerminalService.Close(screen.id) | ||
| 117 | + return | ||
| 118 | + } | ||
| 119 | + screen.stop = listenTerminal( | ||
| 120 | + screen.id, | ||
| 121 | + (data) => screen.xterm.write(data), | ||
| 122 | + () => { | ||
| 123 | + screen.id = '' | ||
| 124 | + const ended = sessionOf(key) | ||
| 125 | + if (ended) ended.ended = true | ||
| 126 | + }, | ||
| 127 | + ) | ||
| 128 | +} | ||
| 129 | + | ||
| 130 | +/** | ||
| 131 | + * Shows a terminal in host: its screen moves there, takes the size of host, | ||
| 132 | + * and its shell starts the first time. Returns the function that lets it go | ||
| 133 | + * (the shell keeps running). | ||
| 134 | + */ | ||
| 135 | +export function attachTerminal(key: string, host: HTMLElement): () => void { | ||
| 136 | + const screen = screens.get(key) | ||
| 137 | + if (!screen) return () => {} | ||
| 138 | + host.appendChild(screen.element) | ||
| 139 | + if (!screen.opened) { | ||
| 140 | + screen.xterm.open(screen.element) | ||
| 141 | + screen.opened = true | ||
| 142 | + screen.fit.fit() | ||
| 143 | + void start(key) | ||
| 144 | + } else { | ||
| 145 | + fitTerminal(key) | ||
| 146 | + screen.xterm.refresh(0, screen.xterm.rows - 1) | ||
| 147 | + } | ||
| 148 | + return () => { | ||
| 149 | + if (screen.element.parentElement === host) host.removeChild(screen.element) | ||
| 150 | + } | ||
| 151 | +} | ||
| 152 | + | ||
| 153 | +/** Gives a shown terminal the size of its place; the shell is told. */ | ||
| 154 | +export function fitTerminal(key: string) { | ||
| 155 | + const screen = screens.get(key) | ||
| 156 | + if (!screen?.opened || !screen.element.isConnected || screen.element.clientWidth === 0) return | ||
| 157 | + screen.fit.fit() | ||
| 158 | + if (screen.id) void TerminalService.Resize(screen.id, screen.xterm.cols, screen.xterm.rows) | ||
| 159 | +} | ||
| 160 | + | ||
| 161 | +/** Puts the keyboard in a terminal. */ | ||
| 162 | +export function focusTerminal(key: string) { | ||
| 163 | + screens.get(key)?.xterm.focus() | ||
| 164 | +} | ||
| 165 | + | ||
| 166 | +/** Starts a new shell in a terminal whose shell has ended. */ | ||
| 167 | +export function restartTerminal(key: string) { | ||
| 168 | + const screen = screens.get(key) | ||
| 169 | + if (!screen) return | ||
| 170 | + screen.stop() | ||
| 171 | + screen.xterm.reset() | ||
| 172 | + void start(key) | ||
| 173 | +} | ||
| 174 | + | ||
| 175 | +/** Ends a terminal: its shell and everything it started, then its screen. */ | ||
| 176 | +export function closeTerminal(key: string) { | ||
| 177 | + const session = sessionOf(key) | ||
| 178 | + const screen = screens.get(key) | ||
| 179 | + screens.delete(key) | ||
| 180 | + if (screen) { | ||
| 181 | + screen.stop() | ||
| 182 | + if (screen.id) void TerminalService.Close(screen.id) | ||
| 183 | + screen.element.remove() | ||
| 184 | + screen.xterm.dispose() | ||
| 185 | + } | ||
| 186 | + if (!session) return | ||
| 187 | + const sessions = terminalsOf(session.agent) | ||
| 188 | + const index = sessions.findIndex((candidate) => candidate.key === key) | ||
| 189 | + const rest = sessions.filter((candidate) => candidate.key !== key) | ||
| 190 | + terminalSessions[session.agent] = rest | ||
| 191 | + if (activeTerminal[session.agent] === key) { | ||
| 192 | + activeTerminal[session.agent] = rest[Math.min(index, rest.length - 1)]?.key ?? '' | ||
| 193 | + } | ||
| 194 | +} | ||
modified
frontend/src/lib/terminals.test.ts +9 -1 | @@ -1,7 +1,7 @@ | ||
| 1 | 1 | // Copyright (c) 2026 Philippe Charrière — Sidekicks Source-Available License 1.0, see LICENSE |
| 2 | 2 | |
| 3 | 3 | import { describe, expect, it } from 'vitest' |
| 4 | -import { listenTerminal, terminalExit, terminalOutput } from './terminals' | |
| 4 | +import { listenTerminal, nextTerminalNumber, terminalExit, terminalOutput } from './terminals' | |
| 5 | 5 | |
| 6 | 6 | describe('terminal routing', () => { |
| 7 | 7 | it('keeps the output that arrives before the view listens', () => { |
| @@ -19,3 +19,11 @@ describe('terminal routing', () => { | ||
| 19 | 19 | expect(received).toHaveLength(2) |
| 20 | 20 | }) |
| 21 | 21 | }) |
| 22 | + | |
| 23 | +describe('terminal numbers (issue #73)', () => { | |
| 24 | + it('gives the smallest number not taken', () => { | |
| 25 | + expect(nextTerminalNumber([])).toBe(1) | |
| 26 | + expect(nextTerminalNumber([1, 2])).toBe(3) | |
| 27 | + expect(nextTerminalNumber([1, 3])).toBe(2) | |
| 28 | + }) | |
| 29 | +}) | |
| @@ -1,7 +1,7 @@ | |||
| 1 | // Copyright (c) 2026 Philippe Charrière — Sidekicks Source-Available License 1.0, see LICENSE | 1 | // Copyright (c) 2026 Philippe Charrière — Sidekicks Source-Available License 1.0, see LICENSE |
| 2 | 2 | ||
| 3 | import { describe, expect, it } from 'vitest' | 3 | import { describe, expect, it } from 'vitest' |
| 4 | -import { listenTerminal, terminalExit, terminalOutput } from './terminals' | 4 | +import { listenTerminal, nextTerminalNumber, terminalExit, terminalOutput } from './terminals' |
| 5 | 5 | ||
| 6 | describe('terminal routing', () => { | 6 | describe('terminal routing', () => { |
| 7 | it('keeps the output that arrives before the view listens', () => { | 7 | it('keeps the output that arrives before the view listens', () => { |
| @@ -19,3 +19,11 @@ describe('terminal routing', () => { | |||
| 19 | expect(received).toHaveLength(2) | 19 | expect(received).toHaveLength(2) |
| 20 | }) | 20 | }) |
| 21 | }) | 21 | }) |
| 22 | + | ||
| 23 | +describe('terminal numbers (issue #73)', () => { | ||
| 24 | + it('gives the smallest number not taken', () => { | ||
| 25 | + expect(nextTerminalNumber([])).toBe(1) | ||
| 26 | + expect(nextTerminalNumber([1, 2])).toBe(3) | ||
| 27 | + expect(nextTerminalNumber([1, 3])).toBe(2) | ||
| 28 | + }) | ||
| 29 | +}) | ||
modified
frontend/src/lib/terminals.ts +16 -0 | @@ -35,3 +35,19 @@ export function terminalExit(id: string) { | ||
| 35 | 35 | listeners.get(id)?.exit() |
| 36 | 36 | early.delete(id) |
| 37 | 37 | } |
| 38 | + | |
| 39 | +/** The most terminals an agent may have open at once (issue #73). */ | |
| 40 | +export const maxTerminalsPerAgent = 8 | |
| 41 | + | |
| 42 | +/** | |
| 43 | + * The number of a new terminal: the smallest one not taken, so that closing | |
| 44 | + * "Terminal 2" then opening one gives "Terminal 2" again. | |
| 45 | + * | |
| 46 | + * @example | |
| 47 | + * nextTerminalNumber([1, 3]) // 2 | |
| 48 | + */ | |
| 49 | +export function nextTerminalNumber(taken: number[]): number { | |
| 50 | + let number = 1 | |
| 51 | + while (taken.includes(number)) number++ | |
| 52 | + return number | |
| 53 | +} | |
| @@ -35,3 +35,19 @@ export function terminalExit(id: string) { | |||
| 35 | listeners.get(id)?.exit() | 35 | listeners.get(id)?.exit() |
| 36 | early.delete(id) | 36 | early.delete(id) |
| 37 | } | 37 | } |
| 38 | + | ||
| 39 | +/** The most terminals an agent may have open at once (issue #73). */ | ||
| 40 | +export const maxTerminalsPerAgent = 8 | ||
| 41 | + | ||
| 42 | +/** | ||
| 43 | + * The number of a new terminal: the smallest one not taken, so that closing | ||
| 44 | + * "Terminal 2" then opening one gives "Terminal 2" again. | ||
| 45 | + * | ||
| 46 | + * @example | ||
| 47 | + * nextTerminalNumber([1, 3]) // 2 | ||
| 48 | + */ | ||
| 49 | +export function nextTerminalNumber(taken: number[]): number { | ||
| 50 | + let number = 1 | ||
| 51 | + while (taken.includes(number)) number++ | ||
| 52 | + return number | ||
| 53 | +} | ||