@swirls/shuttle 0.0.0-stage → 0.1.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.
- package/CHANGELOG.md +18 -0
- package/LICENSE +190 -0
- package/NOTICE +5 -0
- package/README.md +42 -2
- package/dist/api/auth.d.ts +18 -0
- package/dist/api/auth.js +15 -0
- package/dist/api/common.d.ts +8 -0
- package/dist/api/common.js +4 -0
- package/dist/api/connections.d.ts +292 -0
- package/dist/api/connections.js +114 -0
- package/dist/api/github.d.ts +20 -0
- package/dist/api/github.js +13 -0
- package/dist/api/harness.d.ts +97 -0
- package/dist/api/harness.js +47 -0
- package/dist/api/index.d.ts +2059 -0
- package/dist/api/index.js +37 -0
- package/dist/api/runners.d.ts +93 -0
- package/dist/api/runners.js +45 -0
- package/dist/api/sandbox.d.ts +550 -0
- package/dist/api/sandbox.js +210 -0
- package/dist/api/sessions.d.ts +318 -0
- package/dist/api/sessions.js +154 -0
- package/dist/api/tenant.d.ts +54 -0
- package/dist/api/tenant.js +19 -0
- package/dist/api/timings.d.ts +36 -0
- package/dist/api/timings.js +48 -0
- package/dist/api/workspace.d.ts +738 -0
- package/dist/api/workspace.js +246 -0
- package/dist/core/pulls.d.ts +1001 -0
- package/dist/core/pulls.js +170 -0
- package/dist/core/workspace.d.ts +985 -0
- package/dist/core/workspace.js +174 -0
- package/dist/react/components/command-palette.d.ts +22 -0
- package/dist/react/components/command-palette.js +66 -0
- package/dist/react/components/follow-list.d.ts +20 -0
- package/dist/react/components/follow-list.js +98 -0
- package/dist/react/components/json-tree.d.ts +11 -0
- package/dist/react/components/json-tree.js +120 -0
- package/dist/react/components/markdown.d.ts +5 -0
- package/dist/react/components/markdown.js +29 -0
- package/dist/react/components/shuttle-mark.d.ts +7 -0
- package/dist/react/components/shuttle-mark.js +9 -0
- package/dist/react/components/status-dot.d.ts +9 -0
- package/dist/react/components/status-dot.js +27 -0
- package/dist/react/components/status.d.ts +6 -0
- package/dist/react/components/status.js +66 -0
- package/dist/react/components/ui/badge.d.ts +7 -0
- package/dist/react/components/ui/badge.js +19 -0
- package/dist/react/components/ui/button.d.ts +10 -0
- package/dist/react/components/ui/button.js +40 -0
- package/dist/react/components/ui/dialog.d.ts +19 -0
- package/dist/react/components/ui/dialog.js +34 -0
- package/dist/react/components/ui/dropdown-menu.d.ts +14 -0
- package/dist/react/components/ui/dropdown-menu.js +33 -0
- package/dist/react/components/ui/input.d.ts +3 -0
- package/dist/react/components/ui/input.js +6 -0
- package/dist/react/components/ui/panel.d.ts +18 -0
- package/dist/react/components/ui/panel.js +22 -0
- package/dist/react/components/ui/popover.d.ts +7 -0
- package/dist/react/components/ui/popover.js +16 -0
- package/dist/react/components/ui/resizable.d.ts +7 -0
- package/dist/react/components/ui/resizable.js +14 -0
- package/dist/react/components/ui/scroll-area.d.ts +14 -0
- package/dist/react/components/ui/scroll-area.js +12 -0
- package/dist/react/components/ui/select.d.ts +10 -0
- package/dist/react/components/ui/select.js +20 -0
- package/dist/react/components/ui/sheet.d.ts +8 -0
- package/dist/react/components/ui/sheet.js +20 -0
- package/dist/react/components/ui/tabs.d.ts +7 -0
- package/dist/react/components/ui/tabs.js +16 -0
- package/dist/react/components/ui/textarea.d.ts +3 -0
- package/dist/react/components/ui/textarea.js +6 -0
- package/dist/react/components/ui/tooltip.d.ts +8 -0
- package/dist/react/components/ui/tooltip.js +17 -0
- package/dist/react/features/artifacts/artifact-links.d.ts +9 -0
- package/dist/react/features/artifacts/artifact-links.js +38 -0
- package/dist/react/features/artifacts/artifact-pane.d.ts +19 -0
- package/dist/react/features/artifacts/artifact-pane.js +75 -0
- package/dist/react/features/artifacts/artifact-types.d.ts +59 -0
- package/dist/react/features/artifacts/artifact-types.js +69 -0
- package/dist/react/features/artifacts/browser-viewer.d.ts +5 -0
- package/dist/react/features/artifacts/browser-viewer.js +60 -0
- package/dist/react/features/artifacts/diff-style.d.ts +10 -0
- package/dist/react/features/artifacts/diff-style.js +21 -0
- package/dist/react/features/artifacts/file-pane.d.ts +13 -0
- package/dist/react/features/artifacts/file-pane.js +305 -0
- package/dist/react/features/artifacts/file-quick-open.d.ts +15 -0
- package/dist/react/features/artifacts/file-quick-open.js +74 -0
- package/dist/react/features/artifacts/file-search.d.ts +12 -0
- package/dist/react/features/artifacts/file-search.js +90 -0
- package/dist/react/features/artifacts/media-viewer.d.ts +8 -0
- package/dist/react/features/artifacts/media-viewer.js +41 -0
- package/dist/react/features/artifacts/review-diff.d.ts +19 -0
- package/dist/react/features/artifacts/review-diff.js +59 -0
- package/dist/react/features/artifacts/review-viewer.d.ts +4 -0
- package/dist/react/features/artifacts/review-viewer.js +337 -0
- package/dist/react/features/artifacts/sandbox.d.ts +43 -0
- package/dist/react/features/artifacts/sandbox.js +109 -0
- package/dist/react/features/artifacts/tab-strip.d.ts +11 -0
- package/dist/react/features/artifacts/tab-strip.js +55 -0
- package/dist/react/features/artifacts/terminal-theme.d.ts +34 -0
- package/dist/react/features/artifacts/terminal-theme.js +62 -0
- package/dist/react/features/artifacts/terminal-viewer.d.ts +5 -0
- package/dist/react/features/artifacts/terminal-viewer.js +209 -0
- package/dist/react/features/artifacts/unified-diff.d.ts +16 -0
- package/dist/react/features/artifacts/unified-diff.js +55 -0
- package/dist/react/features/artifacts/workspace-viewer.d.ts +16 -0
- package/dist/react/features/artifacts/workspace-viewer.js +275 -0
- package/dist/react/features/connections/agent-sign-in.d.ts +71 -0
- package/dist/react/features/connections/agent-sign-in.js +251 -0
- package/dist/react/features/connections/connections-panel.d.ts +8 -0
- package/dist/react/features/connections/connections-panel.js +42 -0
- package/dist/react/features/connections/login.d.ts +24 -0
- package/dist/react/features/connections/login.js +35 -0
- package/dist/react/features/connections/places.d.ts +20 -0
- package/dist/react/features/connections/places.js +27 -0
- package/dist/react/features/connections/queries.d.ts +28 -0
- package/dist/react/features/connections/queries.js +8 -0
- package/dist/react/features/events/event-cache.d.ts +4 -0
- package/dist/react/features/events/event-cache.js +29 -0
- package/dist/react/features/events/event-catalog.d.ts +17 -0
- package/dist/react/features/events/event-catalog.js +261 -0
- package/dist/react/features/events/event-inspector.d.ts +7 -0
- package/dist/react/features/events/event-inspector.js +29 -0
- package/dist/react/features/events/event-row.d.ts +21 -0
- package/dist/react/features/events/event-row.js +34 -0
- package/dist/react/features/events/event-stream.d.ts +18 -0
- package/dist/react/features/events/event-stream.js +58 -0
- package/dist/react/features/events/global-events.d.ts +122 -0
- package/dist/react/features/events/global-events.js +329 -0
- package/dist/react/features/events/shuttle-events.d.ts +7 -0
- package/dist/react/features/events/shuttle-events.js +1 -0
- package/dist/react/features/playbook/playbook-page.d.ts +2 -0
- package/dist/react/features/playbook/playbook-page.js +36 -0
- package/dist/react/features/playbook/playbook-query.d.ts +111 -0
- package/dist/react/features/playbook/playbook-query.js +17 -0
- package/dist/react/features/pulls/linked-session.d.ts +12 -0
- package/dist/react/features/pulls/linked-session.js +22 -0
- package/dist/react/features/pulls/pull-detail-sheet.d.ts +10 -0
- package/dist/react/features/pulls/pull-detail-sheet.js +41 -0
- package/dist/react/features/pulls/pull-fixtures.d.ts +8 -0
- package/dist/react/features/pulls/pull-fixtures.js +258 -0
- package/dist/react/features/pulls/pull-glyphs.d.ts +24 -0
- package/dist/react/features/pulls/pull-glyphs.js +89 -0
- package/dist/react/features/pulls/pull-inbox-list.d.ts +13 -0
- package/dist/react/features/pulls/pull-inbox-list.js +230 -0
- package/dist/react/features/pulls/pull-inbox-pref.d.ts +2 -0
- package/dist/react/features/pulls/pull-inbox-pref.js +19 -0
- package/dist/react/features/pulls/pull-inbox.d.ts +30 -0
- package/dist/react/features/pulls/pull-inbox.js +110 -0
- package/dist/react/features/pulls/pull-threads.d.ts +39 -0
- package/dist/react/features/pulls/pull-threads.js +145 -0
- package/dist/react/features/pulls/pulls.d.ts +120 -0
- package/dist/react/features/pulls/pulls.js +414 -0
- package/dist/react/features/pulls/session-pull-groups.d.ts +14 -0
- package/dist/react/features/pulls/session-pull-groups.js +29 -0
- package/dist/react/features/pulls/stack-ladder.d.ts +9 -0
- package/dist/react/features/pulls/stack-ladder.js +32 -0
- package/dist/react/features/sessions/agent-tree.d.ts +16 -0
- package/dist/react/features/sessions/agent-tree.js +100 -0
- package/dist/react/features/sessions/agents.d.ts +34 -0
- package/dist/react/features/sessions/agents.js +146 -0
- package/dist/react/features/sessions/attention.d.ts +32 -0
- package/dist/react/features/sessions/attention.js +42 -0
- package/dist/react/features/sessions/composer-attachments.d.ts +33 -0
- package/dist/react/features/sessions/composer-attachments.js +126 -0
- package/dist/react/features/sessions/composer.d.ts +15 -0
- package/dist/react/features/sessions/composer.js +327 -0
- package/dist/react/features/sessions/context-usage-meter.d.ts +5 -0
- package/dist/react/features/sessions/context-usage-meter.js +122 -0
- package/dist/react/features/sessions/context-usage.d.ts +54 -0
- package/dist/react/features/sessions/context-usage.js +206 -0
- package/dist/react/features/sessions/conversation.d.ts +21 -0
- package/dist/react/features/sessions/conversation.js +549 -0
- package/dist/react/features/sessions/harness-switcher.d.ts +26 -0
- package/dist/react/features/sessions/harness-switcher.js +118 -0
- package/dist/react/features/sessions/intake-heuristic.d.ts +5 -0
- package/dist/react/features/sessions/intake-heuristic.js +28 -0
- package/dist/react/features/sessions/interjection-routes.d.ts +13 -0
- package/dist/react/features/sessions/interjection-routes.js +72 -0
- package/dist/react/features/sessions/llm-models.d.ts +128 -0
- package/dist/react/features/sessions/llm-models.js +174 -0
- package/dist/react/features/sessions/media-strip.d.ts +25 -0
- package/dist/react/features/sessions/media-strip.js +56 -0
- package/dist/react/features/sessions/media.d.ts +43 -0
- package/dist/react/features/sessions/media.js +115 -0
- package/dist/react/features/sessions/model-search.d.ts +6 -0
- package/dist/react/features/sessions/model-search.js +16 -0
- package/dist/react/features/sessions/model-switcher.d.ts +16 -0
- package/dist/react/features/sessions/model-switcher.js +262 -0
- package/dist/react/features/sessions/open-pull-request.d.ts +20 -0
- package/dist/react/features/sessions/open-pull-request.js +92 -0
- package/dist/react/features/sessions/pull-publish-card.d.ts +14 -0
- package/dist/react/features/sessions/pull-publish-card.js +88 -0
- package/dist/react/features/sessions/recent-sessions.d.ts +11 -0
- package/dist/react/features/sessions/recent-sessions.js +29 -0
- package/dist/react/features/sessions/seen-sessions.d.ts +16 -0
- package/dist/react/features/sessions/seen-sessions.js +108 -0
- package/dist/react/features/sessions/session-activity.d.ts +95 -0
- package/dist/react/features/sessions/session-activity.js +785 -0
- package/dist/react/features/sessions/session-context.d.ts +8 -0
- package/dist/react/features/sessions/session-context.js +98 -0
- package/dist/react/features/sessions/session-feed.d.ts +243 -0
- package/dist/react/features/sessions/session-feed.js +883 -0
- package/dist/react/features/sessions/session-header.d.ts +24 -0
- package/dist/react/features/sessions/session-header.js +97 -0
- package/dist/react/features/sessions/session-layout.d.ts +7 -0
- package/dist/react/features/sessions/session-layout.js +87 -0
- package/dist/react/features/sessions/session-panes.d.ts +49 -0
- package/dist/react/features/sessions/session-panes.js +142 -0
- package/dist/react/features/sessions/session-parts.d.ts +35 -0
- package/dist/react/features/sessions/session-parts.js +74 -0
- package/dist/react/features/sessions/session-place.d.ts +9 -0
- package/dist/react/features/sessions/session-place.js +109 -0
- package/dist/react/features/sessions/session-pr-pills.d.ts +7 -0
- package/dist/react/features/sessions/session-pr-pills.js +28 -0
- package/dist/react/features/sessions/session-queries.d.ts +172 -0
- package/dist/react/features/sessions/session-queries.js +90 -0
- package/dist/react/features/sessions/session-repos.d.ts +8 -0
- package/dist/react/features/sessions/session-repos.js +15 -0
- package/dist/react/features/sessions/session-share.d.ts +8 -0
- package/dist/react/features/sessions/session-share.js +112 -0
- package/dist/react/features/sessions/session-tabs.d.ts +38 -0
- package/dist/react/features/sessions/session-tabs.js +191 -0
- package/dist/react/features/sessions/session.d.ts +103 -0
- package/dist/react/features/sessions/session.js +216 -0
- package/dist/react/features/sessions/signals.d.ts +13 -0
- package/dist/react/features/sessions/signals.js +34 -0
- package/dist/react/features/sessions/tab-set.d.ts +20 -0
- package/dist/react/features/sessions/tab-set.js +58 -0
- package/dist/react/features/sessions/team.d.ts +16 -0
- package/dist/react/features/sessions/team.js +35 -0
- package/dist/react/features/sessions/timeline.d.ts +53 -0
- package/dist/react/features/sessions/timeline.js +153 -0
- package/dist/react/features/sessions/use-session-tabs.d.ts +18 -0
- package/dist/react/features/sessions/use-session-tabs.js +119 -0
- package/dist/react/features/sessions/vendor-models.d.ts +23 -0
- package/dist/react/features/sessions/vendor-models.js +61 -0
- package/dist/react/features/workspace/activity-dot.d.ts +15 -0
- package/dist/react/features/workspace/activity-dot.js +36 -0
- package/dist/react/features/workspace/app-sidebar.d.ts +16 -0
- package/dist/react/features/workspace/app-sidebar.js +227 -0
- package/dist/react/features/workspace/fixture-toggle.d.ts +2 -0
- package/dist/react/features/workspace/fixture-toggle.js +72 -0
- package/dist/react/features/workspace/folder-group.d.ts +18 -0
- package/dist/react/features/workspace/folder-group.js +83 -0
- package/dist/react/features/workspace/inline-name-input.d.ts +10 -0
- package/dist/react/features/workspace/inline-name-input.js +33 -0
- package/dist/react/features/workspace/link-repo-dialog.d.ts +9 -0
- package/dist/react/features/workspace/link-repo-dialog.js +76 -0
- package/dist/react/features/workspace/repo-chip.d.ts +13 -0
- package/dist/react/features/workspace/repo-chip.js +15 -0
- package/dist/react/features/workspace/session-row.d.ts +20 -0
- package/dist/react/features/workspace/session-row.js +23 -0
- package/dist/react/features/workspace/use-debounced.d.ts +2 -0
- package/dist/react/features/workspace/use-debounced.js +10 -0
- package/dist/react/features/workspace/workspace-context.d.ts +45 -0
- package/dist/react/features/workspace/workspace-context.js +245 -0
- package/dist/react/features/workspace/workspace-fixtures.d.ts +16 -0
- package/dist/react/features/workspace/workspace-fixtures.js +175 -0
- package/dist/react/features/workspace/workspace-tree.d.ts +30 -0
- package/dist/react/features/workspace/workspace-tree.js +69 -0
- package/dist/react/features/workspace/workspace.d.ts +107 -0
- package/dist/react/features/workspace/workspace.js +458 -0
- package/dist/react/index.d.ts +33 -0
- package/dist/react/index.js +35 -0
- package/dist/react/lib/cn.d.ts +6 -0
- package/dist/react/lib/cn.js +20 -0
- package/dist/react/lib/initials.d.ts +6 -0
- package/dist/react/lib/initials.js +32 -0
- package/dist/react/lib/json.d.ts +33 -0
- package/dist/react/lib/json.js +101 -0
- package/dist/react/lib/time.d.ts +8 -0
- package/dist/react/lib/time.js +57 -0
- package/dist/react/lib/use-media-query.d.ts +1 -0
- package/dist/react/lib/use-media-query.js +9 -0
- package/dist/react/lib/use-now.d.ts +2 -0
- package/dist/react/lib/use-now.js +10 -0
- package/dist/react/lib/use-width-bucket.d.ts +6 -0
- package/dist/react/lib/use-width-bucket.js +28 -0
- package/dist/react/parts/index.d.ts +43 -0
- package/dist/react/parts/index.js +43 -0
- package/dist/react/shuttle/device.d.ts +42 -0
- package/dist/react/shuttle/device.js +10 -0
- package/dist/react/shuttle/host.d.ts +1126 -0
- package/dist/react/shuttle/host.js +93 -0
- package/dist/react/shuttle/identity.d.ts +31 -0
- package/dist/react/shuttle/identity.js +20 -0
- package/dist/react/shuttle/index.d.ts +14 -0
- package/dist/react/shuttle/index.js +13 -0
- package/dist/react/shuttle/provider.d.ts +41 -0
- package/dist/react/shuttle/provider.js +59 -0
- package/dist/react/shuttle/router.d.ts +43 -0
- package/dist/react/shuttle/router.js +1 -0
- package/dist/react/theme.css +456 -0
- package/dist/react/ui.d.ts +19 -0
- package/dist/react/ui.js +19 -0
- package/package.json +100 -4
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useMutation, useQuery, useSuspenseQuery } from '@tanstack/react-query';
|
|
3
|
+
import { ArrowUp, Paperclip, SlidersHorizontal, Sparkles, Target, X } from 'lucide-react';
|
|
4
|
+
import { useCallback, useEffect, useRef, useState, } from 'react';
|
|
5
|
+
import { useRunStore } from '@swirls/looms/react';
|
|
6
|
+
import { Button } from '../../components/ui/button.js';
|
|
7
|
+
import { DropdownMenu, DropdownMenuContent, DropdownMenuLabel, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuSeparator, DropdownMenuTrigger, } from '../../components/ui/dropdown-menu.js';
|
|
8
|
+
import { Kbd } from '../../components/ui/panel.js';
|
|
9
|
+
import { Tooltip, TooltipContent, TooltipTrigger } from '../../components/ui/tooltip.js';
|
|
10
|
+
import { cn, errorMessage } from '../../lib/cn.js';
|
|
11
|
+
import { useApi, useShuttleHost, useTenant } from '../../shuttle/index.js';
|
|
12
|
+
import { SignInGate, useAgentGate, useSessionRunsAt } from '../connections/agent-sign-in.js';
|
|
13
|
+
import { connectionsQuery } from '../connections/queries.js';
|
|
14
|
+
import { AttachmentTray, releaseAttachments, useComposerAttachments } from './composer-attachments.js';
|
|
15
|
+
import { ContextUsage } from './context-usage-meter.js';
|
|
16
|
+
import { HarnessSwitcher } from './harness-switcher.js';
|
|
17
|
+
import { routeStyles } from './interjection-routes.js';
|
|
18
|
+
import { readStoredLlmModel, writeStoredLlmModel } from './llm-models.js';
|
|
19
|
+
import { ModelSwitcher } from './model-switcher.js';
|
|
20
|
+
import { interjectionKinds, isInterjectionKind } from './session-feed.js';
|
|
21
|
+
import { sessionQueries, useSetSessionDefaults, useSetSessionHarness, useSetSessionTeam, useSetSessionVendorModel, } from './session-queries.js';
|
|
22
|
+
import { effectiveTeam } from './team.js';
|
|
23
|
+
const AUTO = 'auto';
|
|
24
|
+
const STEERABLE = new Set(['conductor', 'worker']);
|
|
25
|
+
const MENTION_LABELS = {
|
|
26
|
+
worker: 'Shuttle worker',
|
|
27
|
+
codex: 'Codex',
|
|
28
|
+
claude: 'Claude Code',
|
|
29
|
+
cursor: 'Cursor',
|
|
30
|
+
};
|
|
31
|
+
const ADDRESSED = /^@(worker|codex|claude|cursor)\b/i;
|
|
32
|
+
function teamMentions(team) {
|
|
33
|
+
const mentions = [];
|
|
34
|
+
if (team.workers) {
|
|
35
|
+
mentions.push('worker');
|
|
36
|
+
}
|
|
37
|
+
if (team.codex) {
|
|
38
|
+
mentions.push('codex');
|
|
39
|
+
}
|
|
40
|
+
if (team.claudeCode) {
|
|
41
|
+
mentions.push('claude');
|
|
42
|
+
}
|
|
43
|
+
if (team.cursor) {
|
|
44
|
+
mentions.push('cursor');
|
|
45
|
+
}
|
|
46
|
+
return mentions;
|
|
47
|
+
}
|
|
48
|
+
/** Whether a drag is of files, and not of text or a tab being reordered. */
|
|
49
|
+
function carriesFiles(event) {
|
|
50
|
+
return event.dataTransfer.types.includes('Files');
|
|
51
|
+
}
|
|
52
|
+
const STARTER_KINDS = ['question', 'idea', 'constraint', 'correction'];
|
|
53
|
+
export function Composer({ runId, identities, disabled = false, showExamples = true, context, targetThreadId, }) {
|
|
54
|
+
const host = useShuttleHost();
|
|
55
|
+
const api = useApi();
|
|
56
|
+
const [text, setText] = useState('');
|
|
57
|
+
const [kind, setKind] = useState(AUTO);
|
|
58
|
+
const [target, setTarget] = useState(targetThreadId ?? AUTO);
|
|
59
|
+
const [followedThreadId, setFollowedThreadId] = useState(targetThreadId);
|
|
60
|
+
// Opening or leaving an agent's timeline re-aims the composer at it.
|
|
61
|
+
if (followedThreadId !== targetThreadId) {
|
|
62
|
+
setFollowedThreadId(targetThreadId);
|
|
63
|
+
setTarget(targetThreadId ?? AUTO);
|
|
64
|
+
}
|
|
65
|
+
const [model, setModel] = useState(() => readStoredLlmModel());
|
|
66
|
+
const [error, setError] = useState(null);
|
|
67
|
+
// The session page's loader has read these, so they are here from the first render. A change
|
|
68
|
+
// made below is written to the same queries, which is how the page naming the agent sees it.
|
|
69
|
+
const session = sessionQueries(host, runId);
|
|
70
|
+
const { harness } = useSuspenseQuery(session.harness).data;
|
|
71
|
+
const { team } = useSuspenseQuery(session.team).data;
|
|
72
|
+
const { choices: vendorChoices } = useSuspenseQuery(session.vendorModels).data;
|
|
73
|
+
// Each change below shows at once: it is written to the session's query before the Worker
|
|
74
|
+
// answers, and taken back if the Worker refuses it.
|
|
75
|
+
const reportRefusal = (cause) => setError(cause.message);
|
|
76
|
+
const setHarness = useSetSessionHarness(runId, reportRefusal);
|
|
77
|
+
const setTeam = useSetSessionTeam(runId, reportRefusal);
|
|
78
|
+
const setVendorModel = useSetSessionVendorModel(runId, reportRefusal);
|
|
79
|
+
const setDefaults = useSetSessionDefaults(reportRefusal);
|
|
80
|
+
const setLlmModel = useMutation(api.sessions.llmModel.set.mutationOptions({ onError: reportRefusal }));
|
|
81
|
+
const say = useMutation(api.sessions.say.mutationOptions());
|
|
82
|
+
const [mention, setMention] = useState(null);
|
|
83
|
+
const [inputFocused, setInputFocused] = useState(false);
|
|
84
|
+
const inputRef = useRef(null);
|
|
85
|
+
const fileInputRef = useRef(null);
|
|
86
|
+
const files = useComposerAttachments(runId);
|
|
87
|
+
const store = useRunStore(runId);
|
|
88
|
+
const me = useTenant().user;
|
|
89
|
+
/** Counts drag enters and leaves, which fire for every element the pointer crosses. */
|
|
90
|
+
const dragDepth = useRef(0);
|
|
91
|
+
const [dropping, setDropping] = useState(false);
|
|
92
|
+
const syncedRunId = useRef(null);
|
|
93
|
+
/** Messages go out one at a time, so they arrive in the order they were written. */
|
|
94
|
+
const outbox = useRef(Promise.resolve());
|
|
95
|
+
// The model Shuttle runs on is this browser's preference, kept in its storage. Opening a
|
|
96
|
+
// session tells the session what it is, once.
|
|
97
|
+
const { mutate: syncLlmModel } = setLlmModel;
|
|
98
|
+
useEffect(() => {
|
|
99
|
+
if (syncedRunId.current === runId) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
syncedRunId.current = runId;
|
|
103
|
+
syncLlmModel({ id: runId, model: readStoredLlmModel() });
|
|
104
|
+
}, [runId, syncLlmModel]);
|
|
105
|
+
// Only a Shuttle session has a team to address, and only its members can be.
|
|
106
|
+
// A saved team can name an agent that is not connected. It will not run, so it is not offered.
|
|
107
|
+
const runsAt = useSessionRunsAt(runId);
|
|
108
|
+
// An agent that is not signed in where the session runs would only fail the turn, so the
|
|
109
|
+
// message box offers to sign in there first.
|
|
110
|
+
const gate = useAgentGate(harness, runsAt.place);
|
|
111
|
+
// Until the connections are in, the team shows as saved.
|
|
112
|
+
const connections = useQuery(connectionsQuery(host)).data?.connections;
|
|
113
|
+
const activeTeam = connections ? effectiveTeam(team, connections, runsAt.place) : team;
|
|
114
|
+
const mentions = harness === 'shuttle' ? teamMentions(activeTeam) : [];
|
|
115
|
+
const hybrid = mentions.some((member) => member !== 'worker');
|
|
116
|
+
const activeMention = mention && hybrid && mentions.includes(mention) ? mention : null;
|
|
117
|
+
const targets = [...identities.values()].filter((identity) => STEERABLE.has(identity.definitionName));
|
|
118
|
+
const selectedTarget = target === AUTO ? undefined : targets.find((identity) => identity.threadId === target);
|
|
119
|
+
const effectiveTarget = selectedTarget?.threadId ?? AUTO;
|
|
120
|
+
const revealExamples = showExamples && inputFocused && text.length === 0 && !disabled;
|
|
121
|
+
// Agents never block the composer: a message can always be written and sent, even while the
|
|
122
|
+
// one before it is still on its way. The one exception is an agent that is not signed in where
|
|
123
|
+
// the session runs, which could only answer with an error.
|
|
124
|
+
function submit(event) {
|
|
125
|
+
event?.preventDefault();
|
|
126
|
+
if (disabled || gate.blocked) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
const typed = text.trim();
|
|
130
|
+
// A message may be files alone.
|
|
131
|
+
if (!typed && files.attachments.length === 0) {
|
|
132
|
+
inputRef.current?.focus();
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (files.attachments.some((attachment) => attachment.status === 'error')) {
|
|
136
|
+
setError('Remove the file that could not be uploaded, or attach it again.');
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const sending = files.take();
|
|
140
|
+
const message = activeMention && typed ? `@${activeMention} ${typed}` : typed;
|
|
141
|
+
// A message addressed to an agent is work for it, so it must not be answered as a question.
|
|
142
|
+
const addressed = activeMention !== null || (hybrid && ADDRESSED.test(typed));
|
|
143
|
+
const forceKind = addressed ? 'task' : kind === AUTO ? undefined : kind;
|
|
144
|
+
const targetThread = effectiveTarget === AUTO ? undefined : effectiveTarget;
|
|
145
|
+
setError(null);
|
|
146
|
+
setText('');
|
|
147
|
+
inputRef.current?.focus();
|
|
148
|
+
outbox.current = outbox.current
|
|
149
|
+
.then(async () => {
|
|
150
|
+
// Files still on their way finish first, so the message never arrives without them.
|
|
151
|
+
const uploaded = await Promise.all(sending.map((attachment) => attachment.uploaded));
|
|
152
|
+
const messageId = crypto.randomUUID();
|
|
153
|
+
// The store reports a failed send in its own words; this keeps the Worker's reason.
|
|
154
|
+
let refused;
|
|
155
|
+
// The message shows in the conversation at once, as the session will record it. If the
|
|
156
|
+
// send fails it is taken back out and handed back to the message box below.
|
|
157
|
+
await store
|
|
158
|
+
.commit({
|
|
159
|
+
type: 'shuttle.user.said',
|
|
160
|
+
payload: {
|
|
161
|
+
messageId,
|
|
162
|
+
text: message,
|
|
163
|
+
...(forceKind ? { forceKind } : {}),
|
|
164
|
+
...(targetThread ? { targetThreadId: targetThread } : {}),
|
|
165
|
+
author: { id: me.id, name: me.name || me.email },
|
|
166
|
+
...(uploaded.length > 0
|
|
167
|
+
? { attachments: uploaded.map((asset) => ({ ...asset })) }
|
|
168
|
+
: {}),
|
|
169
|
+
},
|
|
170
|
+
}, {
|
|
171
|
+
optimistic: true,
|
|
172
|
+
// The key the session's `say` signal records the message under.
|
|
173
|
+
idempotencyKey: `shuttle-say:${messageId}`,
|
|
174
|
+
send: () => say
|
|
175
|
+
.mutateAsync({
|
|
176
|
+
id: runId,
|
|
177
|
+
messageId,
|
|
178
|
+
text: message,
|
|
179
|
+
...(forceKind ? { forceKind } : {}),
|
|
180
|
+
...(targetThread ? { targetThreadId: targetThread } : {}),
|
|
181
|
+
attachments: uploaded.map((asset) => asset.id),
|
|
182
|
+
})
|
|
183
|
+
.then(() => { }, (cause) => {
|
|
184
|
+
refused = cause;
|
|
185
|
+
throw cause;
|
|
186
|
+
}),
|
|
187
|
+
})
|
|
188
|
+
.catch((cause) => {
|
|
189
|
+
throw refused ?? cause;
|
|
190
|
+
});
|
|
191
|
+
releaseAttachments(sending);
|
|
192
|
+
})
|
|
193
|
+
.catch((cause) => {
|
|
194
|
+
// Hand the message back unless something new has been written since.
|
|
195
|
+
setText((current) => (current.trim() ? current : typed));
|
|
196
|
+
files.restore(sending);
|
|
197
|
+
setError(errorMessage(cause));
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
function onKeyDown(event) {
|
|
201
|
+
if (event.key === 'Enter' && !event.shiftKey && !event.nativeEvent.isComposing) {
|
|
202
|
+
event.preventDefault();
|
|
203
|
+
submit();
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
function onPaste(event) {
|
|
207
|
+
const pasted = [...event.clipboardData.files];
|
|
208
|
+
// A screenshot or copied file is attached. Pasted text is left to the textarea.
|
|
209
|
+
if (pasted.length > 0) {
|
|
210
|
+
event.preventDefault();
|
|
211
|
+
files.add(pasted);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
function onDragEnter(event) {
|
|
215
|
+
if (disabled || !carriesFiles(event)) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
event.preventDefault();
|
|
219
|
+
dragDepth.current += 1;
|
|
220
|
+
setDropping(true);
|
|
221
|
+
}
|
|
222
|
+
function onDragLeave(event) {
|
|
223
|
+
if (!carriesFiles(event)) {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
dragDepth.current = Math.max(0, dragDepth.current - 1);
|
|
227
|
+
if (dragDepth.current === 0) {
|
|
228
|
+
setDropping(false);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
function onDrop(event) {
|
|
232
|
+
if (disabled || !carriesFiles(event)) {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
event.preventDefault();
|
|
236
|
+
dragDepth.current = 0;
|
|
237
|
+
setDropping(false);
|
|
238
|
+
files.add([...event.dataTransfer.files]);
|
|
239
|
+
inputRef.current?.focus();
|
|
240
|
+
}
|
|
241
|
+
function onHarnessChange(next) {
|
|
242
|
+
setError(null);
|
|
243
|
+
// Applies from this session's next turn, and to sessions started after this.
|
|
244
|
+
setHarness.mutate({ id: runId, harness: next });
|
|
245
|
+
setDefaults.mutate({ harness: next });
|
|
246
|
+
}
|
|
247
|
+
function onTeamChange(next) {
|
|
248
|
+
setError(null);
|
|
249
|
+
// Applies from the conductor's next turn, and to sessions started after this.
|
|
250
|
+
setTeam.mutate({ id: runId, team: next });
|
|
251
|
+
setDefaults.mutate({ team: next });
|
|
252
|
+
}
|
|
253
|
+
function onModelChange(next) {
|
|
254
|
+
setModel(next);
|
|
255
|
+
writeStoredLlmModel(next);
|
|
256
|
+
setError(null);
|
|
257
|
+
setLlmModel.mutate({ id: runId, model: next });
|
|
258
|
+
}
|
|
259
|
+
function onVendorChoiceChange(agent, choice) {
|
|
260
|
+
setError(null);
|
|
261
|
+
// Applies from that agent's next turn.
|
|
262
|
+
setVendorModel.mutate({ id: runId, harness: agent, ...choice });
|
|
263
|
+
}
|
|
264
|
+
return (_jsxs("div", { className: "relative z-10 -mt-5 shrink-0 px-4 pt-5 pb-2.5 sm:px-6", children: [_jsx("div", { "aria-hidden": true, className: "from-background via-background/85 pointer-events-none absolute inset-x-0 top-0 bottom-0 -z-10 bg-linear-to-t via-65% to-transparent" }), _jsxs("div", { className: "@container mx-auto w-full max-w-3xl", children: [showExamples ? (_jsx(StarterSuggestions, { visible: revealExamples, onPick: (example) => {
|
|
265
|
+
setText(example);
|
|
266
|
+
setKind(AUTO);
|
|
267
|
+
inputRef.current?.focus();
|
|
268
|
+
} })) : null, _jsxs("form", { onSubmit: submit, onDragEnter: onDragEnter, onDragOver: (event) => {
|
|
269
|
+
// Without this the browser opens a dropped file in place of the page.
|
|
270
|
+
if (carriesFiles(event)) {
|
|
271
|
+
event.preventDefault();
|
|
272
|
+
}
|
|
273
|
+
}, onDragLeave: onDragLeave, onDrop: onDrop, className: cn('bg-card focus-within:border-ring/60 focus-within:ring-ring/20 relative rounded-xl border shadow-lg shadow-black/20 transition-[border-color,box-shadow] duration-150 focus-within:ring-[3px]', dropping && 'border-brand/70 ring-brand/25 ring-[3px]'), children: [dropping ? (_jsxs("div", { className: "bg-card/90 text-foreground pointer-events-none absolute inset-0 z-10 flex items-center justify-center gap-2 rounded-xl text-[13px] font-medium", children: [_jsx(Paperclip, { className: "size-4" }), "Drop files to attach"] })) : null, _jsx("input", { ref: fileInputRef, type: "file", multiple: true, hidden: true, tabIndex: -1, onChange: (event) => {
|
|
274
|
+
files.add([...(event.target.files ?? [])]);
|
|
275
|
+
// Cleared so choosing the same file again still fires a change.
|
|
276
|
+
event.target.value = '';
|
|
277
|
+
inputRef.current?.focus();
|
|
278
|
+
} }), gate.blocked && gate.connection && runsAt.place ? (_jsx(SignInGate, { connection: gate.connection, place: runsAt.place, onSignedIn: gate.reload, onMoveToCloud: () => {
|
|
279
|
+
setError(null);
|
|
280
|
+
runsAt.moveToCloud().catch((cause) => setError(errorMessage(cause)));
|
|
281
|
+
} })) : null, _jsx(AttachmentTray, { attachments: files.attachments, onRemove: files.remove }), _jsx("textarea", { ref: inputRef, value: text, onChange: (event) => setText(event.target.value), onKeyDown: onKeyDown, onPaste: onPaste, onFocus: () => setInputFocused(true), onBlur: () => setInputFocused(false), rows: 1, disabled: disabled, placeholder: "Ask anything. Questions don't interrupt the agents.", "aria-label": "Message the session", className: "placeholder:text-muted-foreground/70 block field-sizing-content max-h-48 min-h-11 w-full resize-none bg-transparent px-3.5 pt-2.5 pb-1.5 text-[13.5px] leading-relaxed outline-none disabled:opacity-50" }), _jsxs("div", { className: "flex items-center justify-between gap-1.5 px-2 pb-2", children: [_jsxs("div", { className: "flex min-w-0 items-center gap-1", children: [_jsxs(Tooltip, { children: [_jsx(TooltipTrigger, { asChild: true, children: _jsx("button", { type: "button", disabled: disabled, onClick: () => fileInputRef.current?.click(), "aria-label": "Attach files", className: "text-muted-foreground hover:bg-accent hover:text-foreground focus-visible:ring-ring/50 ease-snappy inline-flex size-7 shrink-0 items-center justify-center rounded-md transition-[background-color,color,transform] duration-150 outline-none focus-visible:ring-[3px] active:scale-[0.97] disabled:opacity-50", children: _jsx(Paperclip, { className: "size-3.5" }) }) }), _jsx(TooltipContent, { side: "top", align: "start", children: "Attach images, video, audio or files" })] }), _jsx(SteerMenu, { kind: kind, onKindChange: setKind, target: effectiveTarget, onTargetChange: setTarget, targets: targets, mentions: hybrid ? mentions : [], mention: activeMention, onMentionChange: setMention, disabled: disabled }), activeMention ? (_jsx(OverridePill, { label: `@${activeMention}`, icon: _jsx(Target, { className: "size-3 shrink-0 opacity-75" }), clearLabel: `Stop addressing ${MENTION_LABELS[activeMention]}`, onClear: () => setMention(null) })) : null, kind !== AUTO ? (_jsx(OverridePill, { label: routeStyles[kind].label, icon: _jsx("span", { className: cn('size-1.5 shrink-0 rounded-full', routeStyles[kind].dot) }), clearLabel: `Reset route from ${routeStyles[kind].label}`, onClear: () => setKind(AUTO) })) : null, selectedTarget ? (_jsx(OverridePill, { label: selectedTarget.label, icon: _jsx(Target, { className: "size-3 shrink-0 opacity-75" }), clearLabel: `Reset target from ${selectedTarget.label}`, onClear: () => setTarget(AUTO) })) : null] }), _jsx("div", { className: "flex min-w-0 items-center gap-1", children: _jsxs(Tooltip, { children: [_jsx(TooltipTrigger, { asChild: true, children: _jsx(Button, { type: "submit", variant: disabled || gate.blocked ? 'secondary' : 'brand', size: "icon-sm", disabled: disabled || gate.blocked, "aria-label": "Send message", className: "rounded-lg", children: _jsx(ArrowUp, {}) }) }), _jsx(TooltipContent, { side: "top", align: "end", children: _jsxs("span", { className: "inline-flex items-center gap-1", children: ["Send ", _jsx(Kbd, { children: "\u21B5" })] }) })] }) })] })] }), error || files.rejection ? (_jsx("p", { role: "alert", className: "text-status-failed mt-2 text-xs", children: error ?? files.rejection })) : null, _jsxs("div", { className: "mt-1.5 flex items-center justify-between gap-2 px-1.5", children: [_jsx("div", { className: "flex shrink-0 items-center gap-0.5", children: context }), _jsxs("div", { className: "flex min-w-0 flex-1 items-center justify-end gap-0.5", children: [_jsx(HarnessSwitcher, { value: harness, onChange: onHarnessChange, team: team, onTeamChange: onTeamChange, disabled: disabled, place: runsAt.place }), _jsx(ModelSwitcher, { harness: harness, team: activeTeam, value: model, onChange: onModelChange, vendorChoices: vendorChoices, onVendorChoiceChange: onVendorChoiceChange, disabled: disabled }), harness === 'shuttle' ? _jsx(ContextUsage, { runId: runId, modelId: model }) : null] })] })] })] }));
|
|
282
|
+
}
|
|
283
|
+
function StarterSuggestions({ visible, onPick, }) {
|
|
284
|
+
const scrollerRef = useRef(null);
|
|
285
|
+
const [fadeLeft, setFadeLeft] = useState(false);
|
|
286
|
+
const [fadeRight, setFadeRight] = useState(true);
|
|
287
|
+
const syncEdges = useCallback(() => {
|
|
288
|
+
const el = scrollerRef.current;
|
|
289
|
+
if (!el) {
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
setFadeLeft(el.scrollLeft > 2);
|
|
293
|
+
setFadeRight(el.scrollLeft + el.clientWidth < el.scrollWidth - 2);
|
|
294
|
+
}, []);
|
|
295
|
+
useEffect(() => {
|
|
296
|
+
const el = scrollerRef.current;
|
|
297
|
+
if (!el) {
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
syncEdges();
|
|
301
|
+
const observer = new ResizeObserver(syncEdges);
|
|
302
|
+
observer.observe(el);
|
|
303
|
+
return () => observer.disconnect();
|
|
304
|
+
}, [syncEdges]);
|
|
305
|
+
const maskImage = fadeLeft && fadeRight
|
|
306
|
+
? 'linear-gradient(to right, transparent, black 28px, black calc(100% - 28px), transparent)'
|
|
307
|
+
: fadeLeft
|
|
308
|
+
? 'linear-gradient(to right, transparent, black 28px)'
|
|
309
|
+
: fadeRight
|
|
310
|
+
? 'linear-gradient(to right, black calc(100% - 28px), transparent)'
|
|
311
|
+
: undefined;
|
|
312
|
+
return (_jsx("div", { inert: !visible, className: cn('ease-snappy grid transition-[grid-template-rows,opacity,margin-bottom] duration-180 motion-reduce:transition-none', visible ? 'mb-2 grid-rows-[1fr] opacity-100' : 'mb-0 grid-rows-[0fr] opacity-0'), children: _jsx("div", { className: "min-h-0 overflow-hidden", children: _jsx("div", { ref: scrollerRef, onScroll: syncEdges, style: maskImage ? { maskImage } : undefined, className: "flex [scrollbar-width:none] items-center gap-1.5 overflow-x-auto pb-0.5 [&::-webkit-scrollbar]:hidden", children: STARTER_KINDS.map((example) => {
|
|
313
|
+
const style = routeStyles[example];
|
|
314
|
+
const Icon = style.icon;
|
|
315
|
+
return (_jsxs("button", { type: "button", onMouseDown: (event) => event.preventDefault(), onClick: () => onPick(style.example), className: "text-muted-foreground hover:text-foreground hover:bg-accent/60 bg-card/60 ease-snappy inline-flex h-6 shrink-0 items-center gap-1.5 rounded-full border px-2.5 text-[11px] whitespace-nowrap transition-[background-color,color,transform] duration-150 active:scale-[0.97]", children: [_jsx(Icon, { className: cn('size-3 shrink-0', style.tone) }), style.example] }, example));
|
|
316
|
+
}) }) }) }));
|
|
317
|
+
}
|
|
318
|
+
function SteerMenu({ kind, onKindChange, target, onTargetChange, targets, mentions, mention, onMentionChange, disabled, }) {
|
|
319
|
+
const [open, setOpen] = useState(false);
|
|
320
|
+
const hasOverride = kind !== AUTO || target !== AUTO || mention !== null;
|
|
321
|
+
return (_jsxs(DropdownMenu, { open: open, onOpenChange: setOpen, children: [_jsxs(Tooltip, { open: open ? false : undefined, children: [_jsx(TooltipTrigger, { asChild: true, children: _jsx(DropdownMenuTrigger, { disabled: disabled, "aria-label": "Steer routing and target", className: cn('hover:bg-accent hover:text-foreground focus-visible:ring-ring/50 data-[state=open]:bg-accent data-[state=open]:text-foreground ease-snappy inline-flex size-7 shrink-0 items-center justify-center rounded-md transition-[background-color,color,transform] duration-150 outline-none focus-visible:ring-[3px] active:scale-[0.97] disabled:opacity-50', hasOverride ? 'text-foreground' : 'text-muted-foreground'), children: _jsx(SlidersHorizontal, { className: "size-3.5" }) }) }), _jsx(TooltipContent, { side: "top", align: "start", children: "Steer routing & target" })] }), _jsxs(DropdownMenuContent, { align: "start", side: "top", sideOffset: 8, className: "w-60", onCloseAutoFocus: (event) => event.preventDefault(), children: [_jsx(DropdownMenuLabel, { children: "Route as" }), _jsxs(DropdownMenuRadioGroup, { value: kind, onValueChange: (value) => onKindChange(isInterjectionKind(value) ? value : AUTO), children: [_jsxs(DropdownMenuRadioItem, { value: AUTO, description: "Intake classifies the message", children: [_jsx(Sparkles, { className: "text-muted-foreground size-3 shrink-0" }), _jsx("span", { children: "Auto route" })] }), interjectionKinds.map((option) => (_jsxs(DropdownMenuRadioItem, { value: option, description: routeStyles[option].description, children: [_jsx("span", { className: cn('size-1.5 shrink-0 rounded-full', routeStyles[option].dot) }), _jsx("span", { children: routeStyles[option].label })] }, option)))] }), mentions.length > 0 ? (_jsxs(_Fragment, { children: [_jsx(DropdownMenuSeparator, {}), _jsx(DropdownMenuLabel, { children: "Hand to" }), _jsxs(DropdownMenuRadioGroup, { value: mention ?? AUTO,
|
|
322
|
+
// `AUTO` is not a member, so it clears the mention.
|
|
323
|
+
onValueChange: (next) => onMentionChange(mentions.find((member) => member === next) ?? null), children: [_jsx(DropdownMenuRadioItem, { value: AUTO, description: "The conductor picks who does it", children: _jsx("span", { children: "Conductor decides" }) }), mentions.map((member) => (_jsx(DropdownMenuRadioItem, { value: member, description: `Sent as @${member} for the conductor to pass on`, children: _jsx("span", { children: MENTION_LABELS[member] }) }, member)))] })] })) : null, targets.length > 0 ? (_jsxs(_Fragment, { children: [_jsx(DropdownMenuSeparator, {}), _jsx(DropdownMenuLabel, { children: "Target agent" }), _jsxs(DropdownMenuRadioGroup, { value: target, onValueChange: onTargetChange, children: [_jsxs(DropdownMenuRadioItem, { value: AUTO, description: "Session picks the recipient", children: [_jsx(Target, { className: "text-muted-foreground size-3 shrink-0" }), _jsx("span", { children: "Any agent" })] }), targets.map((identity) => (_jsx(DropdownMenuRadioItem, { value: identity.threadId, description: identity.role.blurb, children: _jsx("span", { children: identity.label }) }, identity.threadId)))] })] })) : null] })] }));
|
|
324
|
+
}
|
|
325
|
+
function OverridePill({ label, icon, clearLabel, onClear, }) {
|
|
326
|
+
return (_jsxs("button", { type: "button", onClick: onClear, "aria-label": clearLabel, className: "bg-secondary/80 text-foreground hover:bg-accent ease-snappy inline-flex h-6 max-w-32 min-w-0 items-center gap-1.5 rounded-full border px-2 text-[11px] font-medium transition-[background-color,color,transform] duration-150 active:scale-[0.97]", children: [icon, _jsx("span", { className: "truncate", children: label }), _jsx(X, { className: "text-muted-foreground size-3 shrink-0" })] }));
|
|
327
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Minus } from 'lucide-react';
|
|
3
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
4
|
+
import { useProjection, useRunStore } from '@swirls/looms/react';
|
|
5
|
+
import { Popover, PopoverClose, PopoverContent, PopoverTrigger } from '../../components/ui/popover.js';
|
|
6
|
+
import { cn } from '../../lib/cn.js';
|
|
7
|
+
import { buildContextUsageReport, contextUsage, formatTokenCount, formatTokenTotal, } from './context-usage.js';
|
|
8
|
+
import { getOpenRouterModel } from './llm-models.js';
|
|
9
|
+
import { sessionFeed } from './session-feed.js';
|
|
10
|
+
const HOVER_OPEN_DELAY = 180;
|
|
11
|
+
const HOVER_CLOSE_DELAY = 140;
|
|
12
|
+
const WARN_PERCENT = 80;
|
|
13
|
+
const CRITICAL_PERCENT = 90;
|
|
14
|
+
function supportsFineHover() {
|
|
15
|
+
return (typeof window !== 'undefined' && window.matchMedia('(hover: hover) and (pointer: fine)').matches);
|
|
16
|
+
}
|
|
17
|
+
export function ContextUsage({ runId, modelId }) {
|
|
18
|
+
const [open, setOpen] = useState(false);
|
|
19
|
+
const pinnedRef = useRef(false);
|
|
20
|
+
const openTimerRef = useRef(null);
|
|
21
|
+
const closeTimerRef = useRef(null);
|
|
22
|
+
const store = useRunStore(runId);
|
|
23
|
+
const usage = useProjection(store, contextUsage);
|
|
24
|
+
const feed = useProjection(store, sessionFeed);
|
|
25
|
+
const model = getOpenRouterModel(modelId);
|
|
26
|
+
const windowSize = model?.context ?? 200_000;
|
|
27
|
+
const report = buildContextUsageReport(usage, feed, windowSize);
|
|
28
|
+
const empty = report.used === 0 && report.segments.length === 0;
|
|
29
|
+
const clearTimers = useCallback(() => {
|
|
30
|
+
if (openTimerRef.current !== null) {
|
|
31
|
+
clearTimeout(openTimerRef.current);
|
|
32
|
+
openTimerRef.current = null;
|
|
33
|
+
}
|
|
34
|
+
if (closeTimerRef.current !== null) {
|
|
35
|
+
clearTimeout(closeTimerRef.current);
|
|
36
|
+
closeTimerRef.current = null;
|
|
37
|
+
}
|
|
38
|
+
}, []);
|
|
39
|
+
useEffect(() => clearTimers, [clearTimers]);
|
|
40
|
+
if (empty) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
const showInlinePercent = report.percent >= WARN_PERCENT;
|
|
44
|
+
function scheduleHoverOpen() {
|
|
45
|
+
if (!supportsFineHover()) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (closeTimerRef.current !== null) {
|
|
49
|
+
clearTimeout(closeTimerRef.current);
|
|
50
|
+
closeTimerRef.current = null;
|
|
51
|
+
}
|
|
52
|
+
if (!open && openTimerRef.current === null) {
|
|
53
|
+
openTimerRef.current = setTimeout(() => {
|
|
54
|
+
openTimerRef.current = null;
|
|
55
|
+
setOpen(true);
|
|
56
|
+
}, HOVER_OPEN_DELAY);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function scheduleHoverClose() {
|
|
60
|
+
if (!supportsFineHover() || pinnedRef.current) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (openTimerRef.current !== null) {
|
|
64
|
+
clearTimeout(openTimerRef.current);
|
|
65
|
+
openTimerRef.current = null;
|
|
66
|
+
}
|
|
67
|
+
if (closeTimerRef.current === null) {
|
|
68
|
+
closeTimerRef.current = setTimeout(() => {
|
|
69
|
+
closeTimerRef.current = null;
|
|
70
|
+
setOpen(false);
|
|
71
|
+
}, HOVER_CLOSE_DELAY);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return (_jsxs(Popover, { open: open, onOpenChange: (next) => {
|
|
75
|
+
clearTimers();
|
|
76
|
+
if (!next) {
|
|
77
|
+
pinnedRef.current = false;
|
|
78
|
+
}
|
|
79
|
+
setOpen(next);
|
|
80
|
+
}, children: [_jsxs(PopoverTrigger, { "aria-label": `Context usage: ${report.percent}% full`, onPointerEnter: scheduleHoverOpen, onPointerLeave: scheduleHoverClose, onClick: (event) => {
|
|
81
|
+
clearTimers();
|
|
82
|
+
if (open && !pinnedRef.current) {
|
|
83
|
+
event.preventDefault();
|
|
84
|
+
pinnedRef.current = true;
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
pinnedRef.current = !open;
|
|
88
|
+
}, className: cn('text-muted-foreground hover:bg-accent hover:text-foreground focus-visible:ring-ring/50 data-[state=open]:bg-accent data-[state=open]:text-foreground ease-snappy inline-flex h-7 shrink-0 items-center justify-center rounded-md text-xs tabular-nums transition-[background-color,color,transform] duration-150 outline-none focus-visible:ring-[3px] active:scale-[0.97]', showInlinePercent ? 'gap-1.5 px-2' : 'w-7', report.percent >= CRITICAL_PERCENT && 'text-status-failed hover:text-status-failed', report.percent >= WARN_PERCENT &&
|
|
89
|
+
report.percent < CRITICAL_PERCENT &&
|
|
90
|
+
'text-status-waiting hover:text-status-waiting'), children: [_jsx(ContextRing, { percent: report.percent, hasUsage: report.used > 0 }), showInlinePercent ? _jsxs("span", { children: [report.percent, "%"] }) : null] }), _jsx(PopoverContent, { side: "top", align: "end", collisionPadding: 12, className: "w-[20.5rem]", onPointerEnter: () => {
|
|
91
|
+
if (closeTimerRef.current !== null) {
|
|
92
|
+
clearTimeout(closeTimerRef.current);
|
|
93
|
+
closeTimerRef.current = null;
|
|
94
|
+
}
|
|
95
|
+
}, onPointerLeave: scheduleHoverClose, onOpenAutoFocus: (event) => {
|
|
96
|
+
if (!pinnedRef.current) {
|
|
97
|
+
event.preventDefault();
|
|
98
|
+
}
|
|
99
|
+
}, onCloseAutoFocus: (event) => event.preventDefault(), children: _jsx(ContextUsagePanel, { report: report }) })] }));
|
|
100
|
+
}
|
|
101
|
+
function ContextUsagePanel({ report }) {
|
|
102
|
+
const usedLabel = `${report.estimated ? '~' : ''}${formatTokenCount(report.used)}`;
|
|
103
|
+
const windowLabel = formatTokenTotal(report.window);
|
|
104
|
+
return (_jsxs("div", { className: "p-3.5", children: [_jsxs("div", { className: "flex items-center gap-2", children: [_jsx("h2", { className: "text-foreground text-[13px] font-medium", children: "Context Usage" }), _jsx("span", { className: "text-muted-foreground/70 text-[11px]", children: report.estimated ? 'Estimated' : 'Last model call' }), _jsx(PopoverClose, { "aria-label": "Close", className: "text-muted-foreground hover:bg-accent hover:text-foreground ease-snappy ml-auto inline-flex size-6 items-center justify-center rounded-md transition-[background-color,color,transform] duration-150 active:scale-[0.95]", children: _jsx(Minus, { className: "size-3.5" }) })] }), _jsxs("div", { className: "mt-3 flex items-baseline justify-between gap-3", children: [_jsxs("p", { className: "text-foreground text-[12.5px] font-medium tabular-nums", children: [report.percent, "% Full"] }), _jsxs("p", { className: "text-muted-foreground text-[12px] tabular-nums", children: [usedLabel, " / ", windowLabel, " Tokens"] })] }), _jsx(SegmentedBar, { report: report, className: "mt-2" }), report.segments.length > 0 ? (_jsx("ul", { className: "mt-3 flex flex-col gap-1.5", children: report.segments.map((segment) => (_jsxs("li", { className: "flex items-center gap-2 text-[12px]", children: [_jsx("span", { "aria-hidden": true, className: "size-2.5 shrink-0 rounded-[3px]", style: { backgroundColor: segment.color } }), _jsx("span", { className: "text-foreground/90 min-w-0 flex-1 truncate", children: segment.label }), _jsxs("span", { className: "text-muted-foreground tabular-nums", children: [report.estimated ? '~' : '', formatTokenCount(segment.tokens)] })] }, segment.id))) })) : null, report.totals.input > 0 || report.totals.output > 0 ? (_jsxs("p", { className: "text-muted-foreground/70 mt-3 border-t pt-2.5 text-[11px] tabular-nums", children: ["Session \u00B7 in ", formatTokenCount(report.totals.input), " \u00B7 out", ' ', formatTokenCount(report.totals.output)] })) : null] }));
|
|
105
|
+
}
|
|
106
|
+
function SegmentedBar({ report, className }) {
|
|
107
|
+
const fill = Math.min(100, Math.max(report.used > 0 ? 2 : 0, (report.used / report.window) * 100));
|
|
108
|
+
return (_jsx("div", { className: cn('bg-muted/80 flex h-1.5 w-full overflow-hidden rounded-full', className), role: "meter", "aria-valuemin": 0, "aria-valuemax": 100, "aria-valuenow": report.percent, "aria-label": "Context window fill", children: _jsx("div", { className: "flex h-full gap-px overflow-hidden rounded-full", style: { width: `${fill}%` }, children: report.segments.map((segment) => {
|
|
109
|
+
const share = report.used > 0 ? (segment.tokens / report.used) * 100 : 0;
|
|
110
|
+
return (_jsx("div", { title: `${segment.label}: ${formatTokenCount(segment.tokens)}`, className: "h-full min-w-[3px]", style: {
|
|
111
|
+
width: `${share}%`,
|
|
112
|
+
backgroundColor: segment.color,
|
|
113
|
+
} }, segment.id));
|
|
114
|
+
}) }) }));
|
|
115
|
+
}
|
|
116
|
+
const RING_RADIUS = 5.75;
|
|
117
|
+
const RING_CIRCUMFERENCE = 2 * Math.PI * RING_RADIUS;
|
|
118
|
+
function ContextRing({ percent, hasUsage }) {
|
|
119
|
+
const ratio = hasUsage ? Math.min(1, Math.max(0.06, percent / 100)) : 0;
|
|
120
|
+
const offset = RING_CIRCUMFERENCE * (1 - ratio);
|
|
121
|
+
return (_jsxs("svg", { "aria-hidden": true, viewBox: "0 0 16 16", className: "size-3.5 shrink-0 -rotate-90", fill: "none", children: [_jsx("circle", { cx: "8", cy: "8", r: RING_RADIUS, stroke: "currentColor", strokeOpacity: 0.25, strokeWidth: "1.75" }), _jsx("circle", { cx: "8", cy: "8", r: RING_RADIUS, stroke: "currentColor", strokeWidth: "1.75", strokeLinecap: "round", strokeDasharray: RING_CIRCUMFERENCE, strokeDashoffset: offset, className: "ease-snappy transition-[stroke-dashoffset] duration-200" })] }));
|
|
122
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { ProjectionDefinition } from '@swirls/looms/core';
|
|
2
|
+
import type { SessionFeed } from './session-feed.js';
|
|
3
|
+
export interface TurnUsage {
|
|
4
|
+
readonly threadId: string | null;
|
|
5
|
+
readonly turn: number;
|
|
6
|
+
readonly input: number;
|
|
7
|
+
readonly output: number;
|
|
8
|
+
readonly ts: number;
|
|
9
|
+
}
|
|
10
|
+
export interface ContextUsageState {
|
|
11
|
+
/** Most recent `agent.turn.usage` across the run. */
|
|
12
|
+
readonly lastTurn: TurnUsage | null;
|
|
13
|
+
/** Cumulative totals folded from `agent.turn.usage` only. */
|
|
14
|
+
readonly totals: {
|
|
15
|
+
readonly input: number;
|
|
16
|
+
readonly output: number;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export type ContextCategoryId = 'brief' | 'playbook' | 'tools' | 'conversation' | 'other';
|
|
20
|
+
export interface ContextSegment {
|
|
21
|
+
readonly id: ContextCategoryId;
|
|
22
|
+
readonly label: string;
|
|
23
|
+
readonly tokens: number;
|
|
24
|
+
readonly color: string;
|
|
25
|
+
}
|
|
26
|
+
export interface ContextUsageReport {
|
|
27
|
+
/** Tokens attributed to the current context window fill. */
|
|
28
|
+
readonly used: number;
|
|
29
|
+
readonly window: number;
|
|
30
|
+
/** 0–100, capped. */
|
|
31
|
+
readonly percent: number;
|
|
32
|
+
/** True when `used` comes from char estimates rather than billed input. */
|
|
33
|
+
readonly estimated: boolean;
|
|
34
|
+
readonly segments: readonly ContextSegment[];
|
|
35
|
+
readonly lastTurn: TurnUsage | null;
|
|
36
|
+
readonly totals: {
|
|
37
|
+
readonly input: number;
|
|
38
|
+
readonly output: number;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/** ~4 chars per token — good enough for relative segment sizing. */
|
|
42
|
+
export declare function estimateTokens(text: string): number;
|
|
43
|
+
export declare function formatTokenCount(tokens: number): string;
|
|
44
|
+
export declare function formatTokenTotal(tokens: number): string;
|
|
45
|
+
/**
|
|
46
|
+
* Build a Cursor-style context report.
|
|
47
|
+
*
|
|
48
|
+
* Prefers billed last-turn input when present; otherwise falls back to char-based
|
|
49
|
+
* estimates from the session feed so the meter still works while streaming usage
|
|
50
|
+
* reports `input: 0`.
|
|
51
|
+
*/
|
|
52
|
+
export declare function buildContextUsageReport(usage: ContextUsageState, feed: SessionFeed, window: number): ContextUsageReport;
|
|
53
|
+
/** Client-side fold of `agent.turn.usage` for the context meter. */
|
|
54
|
+
export declare const contextUsage: ProjectionDefinition<ContextUsageState>;
|