File size: 12,487 Bytes
7c5ed63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import { useEffect, useState } from 'react';

import { useContext } from '../../hooks/useContext';
import { useSession } from '../../hooks/useSession';
import type { TokenUsage } from '../../types';
import { Pill } from '../shared/Pill';
import { ClearRibbon } from './ClearRibbon';
import { CompactionRibbon } from './CompactionRibbon';
import { MessageBubble } from './MessageBubble';
import { UndoRibbon } from './UndoRibbon';

interface ContextTabProps {
  sessionId: string;
  /** Override starting agentId; defaults to 'main'. */
  initialAgentId?: string;
}

export function ContextTab({ sessionId, initialAgentId = 'main' }: ContextTabProps) {
  const [agentId, setAgentId] = useState<string>(initialAgentId);
  const [history, setHistory] = useState<'model' | 'full'>('model');
  // Re-sync on session OR agent id change — see WireTab for the same
  // rationale (session navigation must reset a stale subagent pick).
  useEffect(() => {
    setAgentId(initialAgentId);
  }, [sessionId, initialAgentId]);
  const { data: detail } = useSession(sessionId);
  const { data: ctx, isLoading, error } = useContext(sessionId, agentId, history);

  const agents = detail?.agents ?? [];
  const messages = ctx?.messages ?? [];
  const sessionUsage = ctx?.usage.byScope.session ?? EMPTY_USAGE;
  const turnUsage = ctx?.usage.byScope.turn ?? EMPTY_USAGE;
  const cumulativeUsage: TokenUsage = {
    inputOther: sessionUsage.inputOther + turnUsage.inputOther,
    output: sessionUsage.output + turnUsage.output,
    inputCacheRead: sessionUsage.inputCacheRead + turnUsage.inputCacheRead,
    inputCacheCreation: sessionUsage.inputCacheCreation + turnUsage.inputCacheCreation,
  };
  // Live context-window fill (latest step.end usage), distinct from the
  // cumulative session-scoped + turn-scoped spend the bar breaks down.
  const contextTokens = ctx?.contextTokens ?? 0;
  const config = ctx?.config ?? {};
  const permissionMode = ctx?.permission.mode ?? null;
  const planActive = ctx?.planMode.active ?? false;
  const goal = ctx?.goal ?? null;
  const swarmActive = ctx?.swarm.active ?? false;

  return (
    <div className="flex min-h-0 flex-1 flex-col">
      {/* Toolbar — agent selector + status pills */}
      <div className="flex shrink-0 items-center gap-3 border-b border-border bg-surface-1 px-3 py-2">
        <label className="flex items-center gap-2 font-mono text-[11px] text-fg-2">
          <span className="text-fg-3">agent</span>
          <select
            value={agentId}
            onChange={(e) => {
              setAgentId(e.target.value);
            }}
            className="border border-border bg-surface-0 px-2 py-1 font-mono text-[12px] text-fg-0 focus:border-border-strong focus:outline-none"
          >
            {agents.length === 0 ? <option value={agentId}>{agentId}</option> : null}
            {agents.map((a) => (
              <option key={a.agentId} value={a.agentId}>
                {a.agentId} ({a.type}
                {a.profileName ? ` · ${a.profileName}` : ''}
                {a.parentAgentId ? ` ← ${a.parentAgentId}` : ''})
              </option>
            ))}
          </select>
        </label>
        <span className="font-mono text-[11px] text-fg-2">
          <span className="tabular text-fg-0">{messages.length}</span>
          <span className="ml-1 text-fg-3">messages</span>
        </span>
        {config.modelAlias ? (
          <span className="font-mono text-[11px] text-fg-2">
            <span className="text-fg-3">model</span>{' '}
            <span className="text-fg-0">{config.modelAlias}</span>
          </span>
        ) : null}
        <div className="ml-auto flex items-center gap-2">
          {/* History view toggle — 'model' (post-compaction, what the model
              sees) vs 'full' (full reconstructed history for debugging). */}
          <div
            role="group"
            aria-label="history view"
            className="flex items-center overflow-hidden border border-border"
          >
            <button
              type="button"
              aria-pressed={history === 'model'}
              onClick={() => {
                setHistory('model');
              }}
              className={[
                'px-2 py-1 font-mono text-[11px] transition-colors',
                history === 'model'
                  ? 'bg-surface-2 text-fg-0'
                  : 'bg-surface-0 text-fg-3 hover:text-fg-1',
              ].join(' ')}
            >
              model
            </button>
            <button
              type="button"
              aria-pressed={history === 'full'}
              onClick={() => {
                setHistory('full');
              }}
              className={[
                'border-l border-border px-2 py-1 font-mono text-[11px] transition-colors',
                history === 'full'
                  ? 'bg-surface-2 text-fg-0'
                  : 'bg-surface-0 text-fg-3 hover:text-fg-1',
              ].join(' ')}
            >
              full history
            </button>
          </div>
          {permissionMode ? (
            <Pill tone="approval" variant="outline">permission: {permissionMode}</Pill>
          ) : null}
          {planActive ? (
            <Pill tone="info" variant="solid">plan mode</Pill>
          ) : null}
          {swarmActive ? (
            <Pill tone="subagent" variant="solid">swarm mode</Pill>
          ) : null}
        </div>
      </div>

      {/* Full-history hint — clarifies that this is the reconstructed history
          and the model itself only sees the compacted view. */}
      {history === 'full' ? (
        <div className="shrink-0 border-b border-border bg-surface-1 px-3 py-1 font-mono text-[10.5px] text-fg-3">
          full reconstructed history — the model actually sees the compacted view
        </div>
      ) : null}

      {/* Live context-window fill (contextTokens) + the 4-segment cumulative
          session-scoped and turn-scoped usage breakdown. */}
      <TokenBar usage={cumulativeUsage} contextTokens={contextTokens} />

      {/* Message stream */}
      <div className="min-h-0 flex-1 overflow-y-auto">
        <div className="flex flex-col gap-3 px-3 py-4">
          {goal ? (
            <div className="rounded border border-[var(--color-cat-lifecycle)]/40 bg-surface-0 p-2">
              <div className="mb-1 flex items-center gap-2">
                <Pill tone="lifecycle" variant="soft">goal</Pill>
                {goal.status ? <Pill tone="info" variant="outline">{goal.status}</Pill> : null}
              </div>
              <div className="font-mono text-[12px] text-fg-1">{goal.objective}</div>
              {goal.completionCriterion ? (
                <div className="mt-1 font-mono text-[11px] text-fg-3">
                  done when: {goal.completionCriterion}
                </div>
              ) : null}
            </div>
          ) : null}
          {config.systemPrompt ? <SystemPromptBubble text={config.systemPrompt} /> : null}
          {isLoading ? (
            <div className="px-3 py-2 font-mono text-[12px] text-fg-3">loading context…</div>
          ) : error ? (
            <div className="px-3 py-2 font-mono text-[12px] text-[var(--color-sev-error)]">
              {(error as Error).message}
            </div>
          ) : messages.length === 0 ? (
            <div className="px-3 py-2 font-mono text-[12px] text-fg-3">
              no messages — session has only lifecycle/config records so far.
            </div>
          ) : (
            messages.map((m) => {
              if (m.source === 'compaction_summary') {
                return <CompactionRibbon key={m.lineNo} message={m} />;
              }
              if (m.source === 'undo') {
                return <UndoRibbon key={m.lineNo} message={m} />;
              }
              if (m.source === 'clear') {
                return <ClearRibbon key={m.lineNo} message={m} />;
              }
              return <MessageBubble key={m.lineNo} message={m} />;
            })
          )}
        </div>
      </div>
    </div>
  );
}

const EMPTY_USAGE: TokenUsage = {
  inputOther: 0,
  output: 0,
  inputCacheRead: 0,
  inputCacheCreation: 0,
};

// Colors are chosen from the existing semantic palette so the bar reads
// coherently with the rest of the app:
//   inputCacheRead     = success   (cache hit — the "good" share)
//   inputOther         = info      (billed input)
//   output             = assistant (what the model produced)
//   inputCacheCreation = warning   (billed once, amortised next call)
const SEG_COLORS = {
  inputCacheRead: 'var(--color-sev-success)',
  inputOther: 'var(--color-sev-info)',
  output: 'var(--color-assistant)',
  inputCacheCreation: 'var(--color-sev-warning)',
} as const;

function TokenBar({ usage, contextTokens }: { usage: TokenUsage; contextTokens: number }) {
  const total =
    usage.inputOther + usage.output + usage.inputCacheRead + usage.inputCacheCreation;
  return (
    <div className="shrink-0">
      {contextTokens > 0 ? (
        <div className="flex items-center justify-end gap-1 border-b border-border bg-surface-1 px-3 py-1 font-mono text-[10px] text-fg-2">
          <span className="text-fg-3">context</span>
          <span className="tabular text-fg-0">{contextTokens.toLocaleString()}</span>
          <span className="text-fg-3">tok</span>
        </div>
      ) : null}
      {total === 0 ? (
        <div className="h-[2px] bg-border" />
      ) : (
        <div
          className="flex h-[3px] w-full"
          title={
            `cache_read ${usage.inputCacheRead.toLocaleString()} · ` +
            `input ${usage.inputOther.toLocaleString()} · ` +
            `output ${usage.output.toLocaleString()} · ` +
            `cache_create ${usage.inputCacheCreation.toLocaleString()}`
          }
        >
          {usage.inputCacheRead > 0 ? (
            <div
              style={{
                width: `${seg(usage.inputCacheRead, total)}%`,
                backgroundColor: SEG_COLORS.inputCacheRead,
              }}
            />
          ) : null}
          {usage.inputOther > 0 ? (
            <div
              style={{
                width: `${seg(usage.inputOther, total)}%`,
                backgroundColor: SEG_COLORS.inputOther,
              }}
            />
          ) : null}
          {usage.output > 0 ? (
            <div
              style={{ width: `${seg(usage.output, total)}%`, backgroundColor: SEG_COLORS.output }}
            />
          ) : null}
          {usage.inputCacheCreation > 0 ? (
            <div
              style={{
                width: `${seg(usage.inputCacheCreation, total)}%`,
                backgroundColor: SEG_COLORS.inputCacheCreation,
              }}
            />
          ) : null}
        </div>
      )}
    </div>
  );
}

function seg(n: number, total: number): number {
  return (n / total) * 100;
}

function SystemPromptBubble({ text }: { text: string }) {
  const [open, setOpen] = useState(false);
  return (
    <article
      className="relative flex max-w-full min-w-0 flex-col border-l-[3px] bg-surface-1"
      style={{ borderLeftColor: 'var(--color-cat-config)' }}
    >
      <button
        type="button"
        onClick={() => {
          setOpen((v) => !v);
        }}
        className="flex w-full items-center justify-between gap-3 px-3 py-2 text-left hover:bg-surface-2"
      >
        <span className="flex items-center gap-2">
          <Pill tone="config" variant="solid">system</Pill>
          <span className="font-mono text-[10px] text-fg-3 tabular">
            {text.length.toLocaleString()} chars
          </span>
        </span>
        <span className="font-mono text-[11px] text-fg-1">
          {open ? '▾ collapse' : '▸ show full'}
        </span>
      </button>
      <div className="relative min-w-0 px-3 pb-2">
        <pre
          className={[
            'min-w-0 whitespace-pre-wrap [overflow-wrap:anywhere] font-mono text-[12.5px] text-fg-0',
            open ? '' : 'max-h-[9em] overflow-hidden',
          ].join(' ')}
        >
          {text}
        </pre>
        {!open ? (
          <div
            aria-hidden="true"
            className="pointer-events-none absolute inset-x-0 bottom-0 h-14"
            style={{
              background: 'linear-gradient(to bottom, transparent 0%, var(--color-surface-1) 85%)',
            }}
          />
        ) : null}
      </div>
    </article>
  );
}