@adecore/agents-react 0.47.0 → 0.48.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/dist/agents/accounts.d.ts +9 -1
  2. package/dist/agents/accounts.js +31 -7
  3. package/dist/chat/account-choice.d.ts +5 -4
  4. package/dist/chat/account-choice.js +47 -39
  5. package/dist/chat/preferences.d.ts +2 -2
  6. package/dist/chat/preferences.js +11 -5
  7. package/dist/chat/ui/AccountPill.js +9 -6
  8. package/dist/chat/ui/LimitState.js +180 -172
  9. package/dist/chat/ui/UnifiedDiff.js +22 -19
  10. package/dist/chat/ui/composer/ComposerControls.js +1 -1
  11. package/dist/chat/ui/composer/use-chat-controls.js +70 -67
  12. package/dist/chat/ui/diff/EditorEditDiff.d.ts +3 -1
  13. package/dist/chat/ui/diff/EditorEditDiff.js +24 -18
  14. package/dist/chat/ui/diff/EditorPatchDiff.d.ts +3 -1
  15. package/dist/chat/ui/diff/EditorPatchDiff.js +4 -2
  16. package/dist/chat/ui/diff/diff-props.d.ts +2 -0
  17. package/dist/host-hooks.d.ts +3 -0
  18. package/dist/host-hooks.js +8 -0
  19. package/dist/host.d.ts +3 -0
  20. package/dist/locales/en/agent-providers.d.ts +5 -0
  21. package/dist/locales/en/agent-providers.js +8 -3
  22. package/dist/locales/nl/agent-providers.d.ts +5 -0
  23. package/dist/locales/nl/agent-providers.js +8 -3
  24. package/dist/providers/AccountOrderList.d.ts +10 -0
  25. package/dist/providers/AccountOrderList.js +158 -0
  26. package/dist/providers/CliDetail.js +55 -61
  27. package/dist/providers/account-actions.d.ts +1 -0
  28. package/dist/providers/account-actions.js +39 -0
  29. package/dist/providers/index.d.ts +1 -1
  30. package/dist/providers/index.js +1 -1
  31. package/dist/usage/limit-groups.js +6 -1
  32. package/package.json +8 -8
  33. package/src/agents/accounts.ts +42 -6
  34. package/src/chat/account-choice.ts +31 -10
  35. package/src/chat/preferences.ts +33 -8
  36. package/src/chat/ui/AccountPill.tsx +3 -3
  37. package/src/chat/ui/LimitState.tsx +4 -2
  38. package/src/chat/ui/UnifiedDiff.tsx +12 -1
  39. package/src/chat/ui/composer/ComposerControls.tsx +1 -1
  40. package/src/chat/ui/composer/use-chat-controls.ts +4 -1
  41. package/src/chat/ui/diff/EditorEditDiff.tsx +9 -4
  42. package/src/chat/ui/diff/EditorPatchDiff.tsx +5 -1
  43. package/src/chat/ui/diff/diff-props.ts +3 -0
  44. package/src/host-hooks.ts +13 -0
  45. package/src/host.ts +8 -0
  46. package/src/locales/en/agent-providers.ts +8 -3
  47. package/src/locales/nl/agent-providers.ts +8 -3
  48. package/src/providers/AccountOrderList.tsx +124 -0
  49. package/src/providers/CliDetail.tsx +33 -28
  50. package/src/providers/account-actions.ts +30 -0
  51. package/src/providers/index.ts +11 -1
  52. package/src/usage/limit-groups.ts +8 -1
@@ -1,5 +1,13 @@
1
1
  import { chatStorageKey, chatStorageLegacyKeys, createChatStore, onChatStorageConfiguration } from '../storage';
2
- import type { AgentKind, ChatPreferencesPayload, ModelSelection, ProviderInfo, ProviderAccounts, RuntimeMode } from '@adecore/agent-contracts';
2
+ import {
3
+ topAccountOf,
4
+ type AgentKind,
5
+ type ChatPreferencesPayload,
6
+ type ModelSelection,
7
+ type ProviderInfo,
8
+ type ProviderAccounts,
9
+ type RuntimeMode
10
+ } from '@adecore/agent-contracts';
3
11
  import { persistedJson } from './persisted-json';
4
12
  import { DEFAULT_READING_WIDTH, isChatReadingWidth, type ChatReadingWidth } from './reading-width';
5
13
  import { DEFAULT_SEND_KEY, isChatSendKey, type ChatSendKey } from './send-key';
@@ -14,7 +22,7 @@ export interface ChatPreferences {
14
22
  runtimeMode: RuntimeMode;
15
23
  // What an agent started as a terminal node runs in; a chat picks its own mode in the composer.
16
24
  terminalRuntimeMode: RuntimeMode;
17
- /* Per machine, the account a new agent of each CLI starts under; an account id only means something on its own machine. */
25
+ /* Per machine that keeps no order of its accounts, the account a new agent of each CLI starts under; an account id only means something on its own machine. */
18
26
  accountByMachine: Record<string, Partial<Record<AgentKind, string>>>;
19
27
  /* How wide a chat of its own reads. It never reaches a machine. */
20
28
  readingWidth: ChatReadingWidth;
@@ -137,12 +145,29 @@ export function withAccount(preferences: ChatPreferences, scopeId: string, provi
137
145
  return { ...preferences, accountByMachine: Object.keys(picks).length === 0 ? machines : { ...machines, [scopeId]: picks } };
138
146
  }
139
147
 
140
- /* What a machine starts a chat with when it starts one with no client mounting it; `accounts` as `accountFor` takes them. */
141
- export function chatPreferencesPayload(preferences: ChatPreferences, scopeId: string, accounts?: ProviderAccounts | null): ChatPreferencesPayload {
142
- const picks = Object.keys(preferences.accountByMachine[scopeId] ?? {}).flatMap((provider) => {
143
- const account = accountFor(preferences, scopeId, provider as AgentKind, accounts);
144
-
145
- return account === null ? [] : [[provider, account] as const];
148
+ /*
149
+ * What a machine starts a chat with when it starts one with no client mounting it; `accounts` as
150
+ * `accountFor` takes them. A machine that keeps an order of the accounts (`ordered`) starts on its top
151
+ * account, which this sends too; an older one still starts on the account picked here.
152
+ */
153
+ export function chatPreferencesPayload(
154
+ preferences: ChatPreferences,
155
+ scopeId: string,
156
+ accounts?: ProviderAccounts | null,
157
+ ordered = false
158
+ ): ChatPreferencesPayload {
159
+ const kinds = ordered
160
+ ? [...new Set(Object.values(accounts?.accounts ?? {}).map((account) => account.kind))]
161
+ : Object.keys(preferences.accountByMachine[scopeId] ?? {});
162
+
163
+ const picks = kinds.flatMap((provider) => {
164
+ const account = ordered
165
+ ? accounts
166
+ ? topAccountOf(accounts.accounts, provider)
167
+ : null
168
+ : accountFor(preferences, scopeId, provider as AgentKind, accounts);
169
+
170
+ return account === null || (ordered && account === provider) ? [] : [[provider, account] as const];
146
171
  });
147
172
 
148
173
  return {
@@ -6,9 +6,9 @@ import { useChats } from '../../state/chats';
6
6
  import { useChatScope } from '../../scope';
7
7
  import { Pill, Tooltip } from '@adecore/ui';
8
8
 
9
- function AccountPillOf({ provider, account }: { provider: AgentKind; account: string | undefined }) {
9
+ function AccountPillOf({ chatId, provider, account }: { chatId: string; provider: AgentKind; account: string | undefined }) {
10
10
  const { t } = useTranslation('agent-chat');
11
- const choice = useAccountChoice(provider, account);
11
+ const choice = useAccountChoice(provider, account, chatId);
12
12
 
13
13
  if (choice === null) {
14
14
  return null;
@@ -31,5 +31,5 @@ export function AccountPill({ chatId }: { chatId: string }) {
31
31
  const provider = useChats((s) => s.statusByKey[keyOf(chatId)]?.info.provider);
32
32
  const account = useChats((s) => s.statusByKey[keyOf(chatId)]?.info.account);
33
33
 
34
- return provider === undefined ? null : <AccountPillOf provider={provider} account={account} />;
34
+ return provider === undefined ? null : <AccountPillOf chatId={chatId} provider={provider} account={account} />;
35
35
  }
@@ -81,7 +81,9 @@ function ContinueOn({ chatId, info, choice }: { chatId: string; info: ChatInfo;
81
81
  const actions = useChatActions();
82
82
  const limits = useUsageLimits();
83
83
  const [busy, setBusy] = useState(false);
84
- const targets = continueTargets(choice.accounts, limits, info.provider, info.account);
84
+ const targets = continueTargets(choice.accounts, limits, info.provider, info.account).filter((target) =>
85
+ choice.offered.some((entry) => entry.id === target.id)
86
+ );
85
87
  const unread = limits !== null && hasUnreadAccount(choice.accounts, limits, info.provider, info.account);
86
88
 
87
89
  useEffect(() => {
@@ -201,7 +203,7 @@ export function LimitDock({ chatId, info }: { chatId: string; info: ChatInfo })
201
203
  const { t } = useTranslation('agent-chat');
202
204
  useFormatLocale();
203
205
  const now = useNow(MINUTE_MS, info.limit !== undefined);
204
- const choice = useAccountChoice(info.provider, info.account);
206
+ const choice = useAccountChoice(info.provider, info.account, chatId);
205
207
  const view = limitView(info, now);
206
208
 
207
209
  if (view === null) {
@@ -15,7 +15,17 @@ const EditorPatchDiff = lazyNamed(() => import('./diff/EditorPatchDiff'), 'defau
15
15
  export default function UnifiedDiff(props: UnifiedDiffProps) {
16
16
  const host = useChatHost();
17
17
  const { t } = useTranslation('agent-chat');
18
- const { change, contents, allowEditor = true, ignoreWhitespace = false, overflow = 'wrap', diffStyle = 'unified', fill = false, maxHeight } = props;
18
+ const {
19
+ change,
20
+ contents,
21
+ allowEditor = true,
22
+ ignoreWhitespace = false,
23
+ overflow = 'wrap',
24
+ diffStyle = 'unified',
25
+ fill = false,
26
+ maxHeight,
27
+ onSource
28
+ } = props;
19
29
  const loadEngine = host.code.editor;
20
30
  const oldText = contents?.old ?? '';
21
31
  const newText = contents?.new ?? '';
@@ -64,6 +74,7 @@ export default function UnifiedDiff(props: UnifiedDiffProps) {
64
74
  layout={diffStyle}
65
75
  loadEngine={loadEngine!}
66
76
  fallback={unavailable}
77
+ onSource={onSource}
67
78
  />
68
79
  </LazyBoundary>
69
80
  </DiffFallbackBoundary>
@@ -120,7 +120,7 @@ export function ComposerControls({
120
120
  const scope = useChatScope();
121
121
  const Slot = useChatHost().ComposerSlot;
122
122
  const providers = useProviders((row) => row.providers);
123
- const accountChoice = useAccountChoice(info.provider, info.account);
123
+ const accountChoice = useAccountChoice(info.provider, info.account, chatId);
124
124
  const fileInputRef = useRef<HTMLInputElement>(null);
125
125
  const busy = info.activeTurnId !== null;
126
126
  const model = providers.find((entry) => entry.kind === info.provider)?.models.find((entry) => entry.slug === info.selection.model);
@@ -2,6 +2,7 @@ import { useRef, useState } from 'react';
2
2
  import { useTranslation } from 'react-i18next';
3
3
  import type { AgentKind, ChatInfo, ModelSelection, RuntimeMode } from '@adecore/agent-contracts';
4
4
  import { useChatHost } from '../../../host';
5
+ import { useHostCanReorderAccounts } from '../../../host-hooks';
5
6
  import { useChatScope } from '../../../scope';
6
7
  import { errorCode } from '../../../transport';
7
8
  import { useChatActions } from '../../actions';
@@ -21,6 +22,7 @@ export function useChatControls({ chatId, info, notice: { setNotice, showError }
21
22
  const { t } = useTranslation('agent-chat');
22
23
  const host = useChatHost();
23
24
  const scope = useChatScope();
25
+ const ordered = useHostCanReorderAccounts(scope.id);
24
26
  const actions = useChatActions();
25
27
  const configuring = useRef(false);
26
28
  const [configurationPending, setConfigurationPending] = useState(false);
@@ -52,7 +54,8 @@ export function useChatControls({ chatId, info, notice: { setNotice, showError }
52
54
  rememberChatSelection(info.provider, patch.selection);
53
55
  }
54
56
 
55
- if (patch.account) {
57
+ // On a machine that keeps an order of its accounts an account picked here is this chat's alone; new chats start on its top one.
58
+ if (patch.account && !ordered) {
56
59
  rememberChatAccount(scope.id, info.provider, patch.account);
57
60
  }
58
61
  })
@@ -3,7 +3,7 @@ import { useEffect, useState, type ReactNode } from 'react';
3
3
  import { useTranslation } from 'react-i18next';
4
4
  import { getFiletypeFromFileName } from '@pierre/diffs';
5
5
  import type { EditorEngine } from '@adecore/editor';
6
- import { DiffView } from '@adecore/editor-react';
6
+ import { DiffView, type DiffSource } from '@adecore/editor-react';
7
7
  import { diffDocument, patchOf, type DiffDocument } from '@adecore/merge';
8
8
  import type { FileChange } from '../../logic/tools';
9
9
  import { useDiffSourceTokens } from '../use-diff-source-tokens';
@@ -24,7 +24,8 @@ function ReadyDiff({
24
24
  wrap,
25
25
  fill,
26
26
  layout,
27
- maxHeight
27
+ maxHeight,
28
+ onSource
28
29
  }: {
29
30
  document: DiffDocument;
30
31
  path: string;
@@ -33,6 +34,7 @@ function ReadyDiff({
33
34
  fill: boolean;
34
35
  layout: 'unified' | 'split';
35
36
  maxHeight: number;
37
+ onSource?(source: DiffSource): void | (() => void);
36
38
  }) {
37
39
  useReviewSettled();
38
40
  const { t } = useTranslation('agent-chat');
@@ -54,6 +56,7 @@ function ReadyDiff({
54
56
  wrap={wrap}
55
57
  fill={fill}
56
58
  className="chat-diff select-text"
59
+ onSource={onSource}
57
60
  />
58
61
  );
59
62
  }
@@ -66,7 +69,8 @@ export function EditorDocumentDiff({
66
69
  wrap = true,
67
70
  fill = false,
68
71
  layout = 'unified',
69
- maxHeight = 480
72
+ maxHeight = 480,
73
+ onSource
70
74
  }: {
71
75
  document: DiffDocument;
72
76
  path: string;
@@ -76,6 +80,7 @@ export function EditorDocumentDiff({
76
80
  fill?: boolean;
77
81
  layout?: 'unified' | 'split';
78
82
  maxHeight?: number;
83
+ onSource?(source: DiffSource): void | (() => void);
79
84
  }) {
80
85
  const { t } = useTranslation('agent-chat');
81
86
  const withinLimits = canRenderEditorDiff(document);
@@ -125,7 +130,7 @@ export function EditorDocumentDiff({
125
130
  return engine === null ? (
126
131
  fallback
127
132
  ) : (
128
- <ReadyDiff document={document} path={path} engine={engine} wrap={wrap} fill={fill} layout={layout} maxHeight={maxHeight} />
133
+ <ReadyDiff document={document} path={path} engine={engine} wrap={wrap} fill={fill} layout={layout} maxHeight={maxHeight} onSource={onSource} />
129
134
  );
130
135
  }
131
136
 
@@ -1,6 +1,7 @@
1
1
  import { useMemo, type ReactNode } from 'react';
2
2
  import type { ChatFileChange } from '@adecore/agent-contracts';
3
3
  import type { EditorEngine } from '@adecore/editor';
4
+ import type { DiffSource } from '@adecore/editor-react';
4
5
  import { diffDocument, parsePatch } from '@adecore/merge';
5
6
  import type { DiffContents } from '../full-diff';
6
7
  import { diffSourceBudget, patchSourceBudget } from './diff-source-budget';
@@ -17,7 +18,8 @@ export default function EditorPatchDiff({
17
18
  maxHeight,
18
19
  layout = 'unified',
19
20
  loadEngine,
20
- fallback
21
+ fallback,
22
+ onSource
21
23
  }: {
22
24
  change: ChatFileChange;
23
25
  sourceChecked?: boolean;
@@ -29,6 +31,7 @@ export default function EditorPatchDiff({
29
31
  maxHeight?: number;
30
32
  loadEngine: () => Promise<EditorEngine>;
31
33
  fallback: ReactNode;
34
+ onSource?(source: DiffSource): void | (() => void);
32
35
  }) {
33
36
  const oldText = contents?.old;
34
37
  const newText = contents?.new;
@@ -59,6 +62,7 @@ export default function EditorPatchDiff({
59
62
  fill={fill}
60
63
  layout={layout}
61
64
  maxHeight={maxHeight}
65
+ onSource={onSource}
62
66
  />
63
67
  );
64
68
  }
@@ -1,4 +1,5 @@
1
1
  import type { ChatFileChange } from '@adecore/agent-contracts';
2
+ import type { DiffSource } from '@adecore/editor-react';
2
3
  import type { DiffContents } from '../full-diff';
3
4
 
4
5
  export interface UnifiedDiffProps {
@@ -13,4 +14,6 @@ export interface UnifiedDiffProps {
13
14
  fill?: boolean;
14
15
  /* Both whole texts the patch was made between, with which a person can unfold the lines between hunks. */
15
16
  contents?: DiffContents;
17
+ /* Runs per editor of the diff with a way to ask where its caret stands in the new file. Only the editor diff calls it; the plain fallbacks have no caret. */
18
+ onSource?(source: DiffSource): void | (() => void);
16
19
  }
package/src/host-hooks.ts CHANGED
@@ -4,6 +4,7 @@
4
4
  * hooks are called through these wrappers only. `ChatHostProvider` pins its host, so they never change.
5
5
  */
6
6
  import type { ProviderPlatform } from '@adecore/agent-contracts';
7
+ import type { AccountRule } from './agents/accounts';
7
8
  import { referablesOfChats, type Referables } from './chat/chat-references';
8
9
  import { useChatHost, type ChatHost, type CommandLook, type ComposerSendAction } from './host';
9
10
 
@@ -134,3 +135,15 @@ export function useCommandNames(
134
135
  ): ReturnType<NonNullable<CommandLook['useNames']>> {
135
136
  return look.useNames(scopeId, chatId);
136
137
  }
138
+
139
+ export function useHostCanReorderAccounts(scopeId: string): boolean {
140
+ const useCanReorder = useChatHost().useCanReorderAccounts ?? (() => true);
141
+
142
+ return useCanReorder(scopeId);
143
+ }
144
+
145
+ export function useHostAccountRule(scopeId: string, chatId: string): AccountRule | null {
146
+ const useRule = useChatHost().useAccountRule ?? (() => null);
147
+
148
+ return useRule(scopeId, chatId);
149
+ }
package/src/host.ts CHANGED
@@ -23,6 +23,7 @@ import type {
23
23
  RuntimeMode
24
24
  } from '@adecore/agent-contracts';
25
25
  import { isApplePlatform } from '@adecore/ui';
26
+ import type { AccountRule } from './agents/accounts';
26
27
  import type { ReferableKind, Referables } from './chat/chat-references';
27
28
  import type { TimelineRow } from './chat/logic/timeline';
28
29
  import type { FindReveal } from './chat/ui/find-reveal';
@@ -459,6 +460,13 @@ export interface ChatHost {
459
460
  openLogin: ((scopeId: string, kind: AgentKind, accountId: string, name: string) => Promise<void>) | null;
460
461
  /* Why a login cannot start from here right now, or null when it can. */
461
462
  useLoginBlocked(scopeId: string): string | null;
463
+ /* Whether the machine in scope keeps a person's order of the accounts (`accounts.reorder`); left out, it does. */
464
+ useCanReorderAccounts?(scopeId: string): boolean;
465
+ /*
466
+ * What the project of a chat says about its accounts: per CLI the account its new chats start on and
467
+ * the accounts it never offers. Left out, or null, every account that is on is offered.
468
+ */
469
+ useAccountRule?(scopeId: string, chatId: string): AccountRule | null;
462
470
  /*
463
471
  * Runs a command a provider's install page shows, its install or its sign-in, where a person sees it run,
464
472
  * such as in a new terminal on the scope's machine; left out, the page offers to copy the command only.
@@ -75,7 +75,8 @@ export default {
75
75
  onlyDefault: '{{provider}} supports only its default account.',
76
76
  defaults: {
77
77
  title: 'Defaults for new chats',
78
- description: 'Picking a model or account in a chat also changes its default here.',
78
+ description: 'Picking a model in a chat also changes its default here.',
79
+ descriptionWithAccount: 'Picking a model or account in a chat also changes its default here.',
79
80
  account: 'Account',
80
81
  accountFor: 'Default account for {{provider}}',
81
82
  model: 'Model',
@@ -85,7 +86,10 @@ export default {
85
86
  providerDefault: 'Provider default',
86
87
  legacyModel: '{{name}} (legacy)'
87
88
  },
88
- accounts: 'Accounts'
89
+ accounts: 'Accounts',
90
+ accountsOrder: 'New chats and agents start on the top account that is on.',
91
+ defaultTag: 'Default',
92
+ reorderHandle: 'Move {{account}}. Alt+Up and Alt+Down move it too.'
89
93
  },
90
94
  account: {
91
95
  kind: '{{provider}} account',
@@ -144,7 +148,8 @@ export default {
144
148
  confirmDescription: 'Its folder and login stay at {{folder}}.',
145
149
  confirm: 'Remove account'
146
150
  },
147
- saveFailed: 'Could not save the account'
151
+ saveFailed: 'Could not save the account',
152
+ reorderFailed: 'Could not change the order of the accounts'
148
153
  },
149
154
  add: {
150
155
  title: 'Add {{provider}} account',
@@ -75,7 +75,8 @@ export default {
75
75
  onlyDefault: '{{provider}} kent alleen zijn standaardaccount.',
76
76
  defaults: {
77
77
  title: 'Standaarden voor nieuwe chats',
78
- description: 'Een model of account kiezen in een chat verandert hier ook de standaard.',
78
+ description: 'Een model kiezen in een chat verandert hier ook de standaard.',
79
+ descriptionWithAccount: 'Een model of account kiezen in een chat verandert hier ook de standaard.',
79
80
  account: 'Account',
80
81
  accountFor: 'Standaardaccount voor {{provider}}',
81
82
  model: 'Model',
@@ -85,7 +86,10 @@ export default {
85
86
  providerDefault: 'Standaard van de provider',
86
87
  legacyModel: '{{name}} (legacy)'
87
88
  },
88
- accounts: 'Accounts'
89
+ accounts: 'Accounts',
90
+ accountsOrder: 'Nieuwe chats en agents starten op het bovenste account dat aanstaat.',
91
+ defaultTag: 'Standaard',
92
+ reorderHandle: 'Verplaats {{account}}. Alt+Omhoog en Alt+Omlaag verplaatsen het ook.'
89
93
  },
90
94
  account: {
91
95
  kind: '{{provider}}-account',
@@ -144,7 +148,8 @@ export default {
144
148
  confirmDescription: 'De map en de login blijven in {{folder}}.',
145
149
  confirm: 'Account verwijderen'
146
150
  },
147
- saveFailed: 'Kon het account niet opslaan'
151
+ saveFailed: 'Kon het account niet opslaan',
152
+ reorderFailed: 'Kon de volgorde van de accounts niet wijzigen'
148
153
  },
149
154
  add: {
150
155
  title: '{{provider}}-account toevoegen',
@@ -0,0 +1,124 @@
1
+ import { useState, type DragEvent, type KeyboardEvent } from 'react';
2
+ import { useTranslation } from 'react-i18next';
3
+ import { ChevronRight, GripVertical } from 'lucide-react';
4
+ import { Icon, Pill, Tooltip } from '@adecore/ui';
5
+ import { AccountDot } from '../agents/AccountDot';
6
+ import { ACCOUNT_TONE_CLASSES, accountName, accountStatusLine, movedIds, topAccountEntry, type AccountEntry, type AccountLogin } from '../agents/accounts';
7
+
8
+ const DRAG_TYPE = 'application/x-adecore-account';
9
+
10
+ interface AccountOrderListProps {
11
+ entries: readonly AccountEntry[];
12
+ providerName: string;
13
+ login: AccountLogin;
14
+ /* Null while the machine keeps no order, which draws the rows without handles. */
15
+ onReorder: ((ids: string[]) => void) | null;
16
+ onPickAccount(id: string): void;
17
+ }
18
+
19
+ /* The accounts of one CLI in the person's order, dragged by their handle or moved with Alt+Up and Alt+Down; the top one that is on is tagged as the default. */
20
+ export function AccountOrderList({ entries, providerName, login, onReorder, onPickAccount }: AccountOrderListProps) {
21
+ const { t } = useTranslation('agent-providers');
22
+ const [dragged, setDragged] = useState<string | null>(null);
23
+ const [over, setOver] = useState<number | null>(null);
24
+ const ids = entries.map((entry) => entry.id);
25
+ const top = topAccountEntry(entries);
26
+ const reorder = entries.length >= 2 ? onReorder : null;
27
+
28
+ const move = (id: string, to: number): void => {
29
+ const next = movedIds(ids, id, to);
30
+
31
+ if (reorder !== null && next.some((candidate, index) => candidate !== ids[index])) {
32
+ reorder(next);
33
+ }
34
+ };
35
+
36
+ const onKeyDown = (event: KeyboardEvent, id: string, index: number): void => {
37
+ if (reorder === null || !event.altKey || (event.key !== 'ArrowUp' && event.key !== 'ArrowDown')) {
38
+ return;
39
+ }
40
+
41
+ event.preventDefault();
42
+ move(id, event.key === 'ArrowUp' ? index - 1 : index + 1);
43
+ };
44
+
45
+ const placeOf = (event: DragEvent, index: number): number => {
46
+ const box = event.currentTarget.getBoundingClientRect();
47
+
48
+ return event.clientY > box.top + box.height / 2 ? index + 1 : index;
49
+ };
50
+
51
+ const onDrop = (event: DragEvent, index: number): void => {
52
+ event.preventDefault();
53
+ const id = event.dataTransfer.getData(DRAG_TYPE);
54
+ const from = ids.indexOf(id);
55
+ const place = placeOf(event, index);
56
+ setDragged(null);
57
+ setOver(null);
58
+
59
+ if (from !== -1) {
60
+ move(id, place > from ? place - 1 : place);
61
+ }
62
+ };
63
+
64
+ return (
65
+ <>
66
+ {entries.map((entry, index) => {
67
+ const status = accountStatusLine(entry.status, login === 'terminal');
68
+ const name = accountName(entry, providerName);
69
+
70
+ return (
71
+ <div
72
+ key={entry.id}
73
+ className={`relative flex w-full min-w-0 items-center first:rounded-t-xl last:rounded-b-xl hover:bg-surface-hover ${dragged === entry.id ? 'opacity-50' : ''}`}
74
+ onDragOver={(event) => {
75
+ if (dragged !== null) {
76
+ event.preventDefault();
77
+ setOver(placeOf(event, index));
78
+ }
79
+ }}
80
+ onDrop={(event) => onDrop(event, index)}
81
+ >
82
+ {over === index && <span aria-hidden className="absolute inset-x-3 top-0 h-0.5 rounded-full bg-accent" />}
83
+ {over === index + 1 && index === entries.length - 1 && (
84
+ <span aria-hidden className="absolute inset-x-3 bottom-0 h-0.5 rounded-full bg-accent" />
85
+ )}
86
+ {reorder !== null && (
87
+ <Tooltip label={t('cli.reorderHandle', { account: name })}>
88
+ <span
89
+ aria-hidden
90
+ draggable
91
+ className="flex shrink-0 cursor-grab items-center self-stretch pl-3 text-text-faint"
92
+ onDragStart={(event) => {
93
+ event.dataTransfer.setData(DRAG_TYPE, entry.id);
94
+ event.dataTransfer.effectAllowed = 'move';
95
+ setDragged(entry.id);
96
+ }}
97
+ onDragEnd={() => {
98
+ setDragged(null);
99
+ setOver(null);
100
+ }}
101
+ >
102
+ <Icon icon={GripVertical} size={14} />
103
+ </span>
104
+ </Tooltip>
105
+ )}
106
+ <button
107
+ type="button"
108
+ aria-keyshortcuts={reorder === null ? undefined : 'Alt+ArrowUp Alt+ArrowDown'}
109
+ className={`flex min-w-0 grow items-center gap-3 py-3 pr-4.5 text-left ${reorder === null ? 'pl-4.5' : 'pl-2.5'}`}
110
+ onClick={() => onPickAccount(entry.id)}
111
+ onKeyDown={(event) => onKeyDown(event, entry.id, index)}
112
+ >
113
+ <AccountDot color={entry.account.color} />
114
+ <span className="min-w-0 truncate text-sm text-text">{name}</span>
115
+ {entries.length >= 2 && entry.id === top?.id && <Pill size="sm">{t('cli.defaultTag')}</Pill>}
116
+ <span className={`ml-auto shrink-0 text-xs ${ACCOUNT_TONE_CLASSES[status.tone]}`}>{status.text}</span>
117
+ <Icon icon={ChevronRight} size={14} className="shrink-0 text-text-faint" />
118
+ </button>
119
+ </div>
120
+ );
121
+ })}
122
+ </>
123
+ );
124
+ }
@@ -1,18 +1,22 @@
1
1
  import { useTranslation } from 'react-i18next';
2
- import { ChevronRight, Plus } from 'lucide-react';
2
+ import { Plus } from 'lucide-react';
3
3
  import type { AgentKind, ModelInfo, ProviderInfo } from '@adecore/agent-contracts';
4
- import { AccountDot } from '../agents/AccountDot';
5
4
  import { ModelOptionControl } from '../agents/ModelOptionControl';
6
5
  import { carryOptions } from '../agents/model-options';
7
6
  import { optionWord } from '../agents/option-words';
8
- import { ACCOUNT_TONE_CLASSES, accountName, accountStatusLine, offeredAccounts, type AccountEntry, type AccountLogin } from '../agents/accounts';
7
+ import { AccountDot } from '../agents/AccountDot';
8
+ import { accountName, offeredAccounts, type AccountEntry, type AccountLogin } from '../agents/accounts';
9
9
  import { accountFor, forgetChatSelection, rememberChatAccount, rememberChatSelection, selectionFor, useChatPreferences } from '../chat/preferences';
10
+ import { knownAccounts, useProviderAccounts } from '../state/provider-accounts';
11
+ import { useChatHost } from '../host';
12
+ import { useHostCanReorderAccounts } from '../host-hooks';
10
13
  import { useChatScope } from '../scope';
14
+ import { reorderAccounts } from './account-actions';
15
+ import { AccountOrderList } from './AccountOrderList';
11
16
  import { providerAbilities } from './provider-abilities';
12
17
  import { DetailHeader, SettingsRow, SettingsSection } from '@adecore/ui/settings';
13
18
  import { Button, Icon, Select } from '@adecore/ui';
14
19
  import { CliTile } from './parts';
15
- import { knownAccounts, useProviderAccounts } from '../state/provider-accounts';
16
20
 
17
21
  const PROVIDER_DEFAULT = '';
18
22
 
@@ -50,14 +54,18 @@ interface CliDetailProps {
50
54
  /* One CLI: what it is, what a new chat of it starts with unless `defaults` is off, and its accounts. */
51
55
  export function CliDetail({ provider, entries, defaults, login, onAddAccount, onPickAccount }: CliDetailProps) {
52
56
  const { t } = useTranslation('agent-providers');
53
- const { id: scopeId } = useChatScope();
57
+ const scope = useChatScope();
58
+ const host = useChatHost();
59
+ const canReorder = useHostCanReorderAccounts(scope.id);
54
60
  const preferences = useChatPreferences();
55
61
  const selection = selectionFor(preferences, provider.kind);
56
62
  const model = provider.models.find((entry) => entry.slug === selection?.model);
57
- const machineAccounts = useProviderAccounts(knownAccounts);
58
- const picked = accountFor(preferences, scopeId, provider.kind, machineAccounts) ?? provider.kind;
59
- const choosable = offeredAccounts(entries ?? [], picked);
60
63
  const canAddAccount = login !== null;
64
+ const ordered = canReorder && entries !== null && entries.length >= 2;
65
+ // A machine that keeps no order of its accounts starts a new chat on the account picked here.
66
+ const machineAccounts = useProviderAccounts(knownAccounts);
67
+ const picked = accountFor(preferences, scope.id, provider.kind, machineAccounts) ?? provider.kind;
68
+ const choosable = canReorder ? [] : offeredAccounts(entries ?? [], picked);
61
69
 
62
70
  return (
63
71
  <>
@@ -75,7 +83,10 @@ export function CliDetail({ provider, entries, defaults, login, onAddAccount, on
75
83
  }
76
84
  />
77
85
  {defaults && (
78
- <SettingsSection title={t('cli.defaults.title')} description={t('cli.defaults.description')}>
86
+ <SettingsSection
87
+ title={t('cli.defaults.title')}
88
+ description={t(canReorder ? 'cli.defaults.description' : 'cli.defaults.descriptionWithAccount')}
89
+ >
79
90
  {choosable.length > 0 && (
80
91
  <SettingsRow
81
92
  searchId="providers.defaults.account"
@@ -90,7 +101,7 @@ export function CliDetail({ provider, entries, defaults, login, onAddAccount, on
90
101
  label: accountName(entry, provider.name),
91
102
  icon: <AccountDot color={entry.account.color} className="size-2" />
92
103
  }))}
93
- onValueChange={(id) => rememberChatAccount(scopeId, provider.kind, id)}
104
+ onValueChange={(id) => rememberChatAccount(scope.id, provider.kind, id)}
94
105
  />
95
106
  }
96
107
  />
@@ -134,24 +145,18 @@ export function CliDetail({ provider, entries, defaults, login, onAddAccount, on
134
145
  {entries === null ? (
135
146
  <p className="text-xs text-text-faint">{t('list.noAccounts')}</p>
136
147
  ) : (
137
- <SettingsSection title={t('cli.accounts')} footer={canAddAccount ? undefined : t('cli.onlyDefault', { provider: provider.name })}>
138
- {entries.map((entry) => {
139
- const status = accountStatusLine(entry.status, login === 'terminal');
140
-
141
- return (
142
- <button
143
- key={entry.id}
144
- type="button"
145
- className="flex w-full min-w-0 items-center gap-3 px-4.5 py-3 text-left first:rounded-t-xl last:rounded-b-xl hover:bg-surface-hover"
146
- onClick={() => onPickAccount(entry.id)}
147
- >
148
- <AccountDot color={entry.account.color} />
149
- <span className="min-w-0 grow truncate text-sm text-text">{accountName(entry, provider.name)}</span>
150
- <span className={`shrink-0 text-xs ${ACCOUNT_TONE_CLASSES[status.tone]}`}>{status.text}</span>
151
- <Icon icon={ChevronRight} size={14} className="shrink-0 text-text-faint" />
152
- </button>
153
- );
154
- })}
148
+ <SettingsSection
149
+ title={t('cli.accounts')}
150
+ description={ordered ? t('cli.accountsOrder') : undefined}
151
+ footer={canAddAccount ? undefined : t('cli.onlyDefault', { provider: provider.name })}
152
+ >
153
+ <AccountOrderList
154
+ entries={entries}
155
+ providerName={provider.name}
156
+ login={login}
157
+ onReorder={ordered ? (ids) => void reorderAccounts(scope, host, provider.kind, ids) : null}
158
+ onPickAccount={onPickAccount}
159
+ />
155
160
  </SettingsSection>
156
161
  )}
157
162
  </>
@@ -54,6 +54,36 @@ export async function removeAccount(scope: ChatScope, host: Pick<ChatHost, 'noti
54
54
  }
55
55
  }
56
56
 
57
+ /* Puts the accounts of one CLI in this order, showing it at once; a refusal puts the host's order back and says why. */
58
+ export async function reorderAccounts(scope: ChatScope, host: Pick<ChatHost, 'notify'>, kind: AgentKind, ids: readonly string[]): Promise<void> {
59
+ const store = useProviderAccountsStore.getState();
60
+ const before = providerAccountsOf(scope.id).accounts;
61
+
62
+ if (before !== null) {
63
+ const accounts = { ...before.accounts };
64
+
65
+ for (const [order, id] of ids.entries()) {
66
+ const account = accounts[id];
67
+
68
+ if (account !== undefined) {
69
+ accounts[id] = { ...account, order };
70
+ }
71
+ }
72
+
73
+ store.set(scope.id, { ...before, accounts });
74
+ }
75
+
76
+ try {
77
+ store.set(scope.id, await linkTo(scope).request('accounts.reorder', { kind, ids: [...ids] }));
78
+ } catch (e) {
79
+ if (before !== null) {
80
+ store.set(scope.id, before);
81
+ }
82
+
83
+ host.notify({ id: `providers-reorder-${scope.id}`, kind: 'error', title: words('account.reorderFailed'), description: errorMessage(e) });
84
+ }
85
+ }
86
+
57
87
  /* An account in a folder the host makes for it; answers its id. Throws what the host said, for the form to show. */
58
88
  export async function createAccount(scope: ChatScope, kind: AgentKind, label: string, color: string): Promise<string> {
59
89
  const { id, ...accounts } = await linkTo(scope).request('accounts.create', { kind, label, color });