paseo-office 0.2.4 → 0.2.5
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/client/activity-panel.tsx +12 -1
- package/client/office-surface.tsx +53 -8
- package/client/timeline-card.tsx +446 -0
- package/client/timeline-open.ts +4 -0
- package/docs/office.md +19 -0
- package/index.server.ts +45 -2
- package/package.json +1 -1
- package/server/paths.ts +5 -0
- package/server/timeline.ts +218 -0
- package/shared/contracts.ts +14 -0
- package/shared/timeline.ts +177 -0
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { useCallback, useMemo, useState, useSyncExternalStore } from "react";
|
|
1
|
+
import { useCallback, useContext, useMemo, useState, useSyncExternalStore } from "react";
|
|
2
2
|
import { Pressable, Text, View } from "react-native";
|
|
3
3
|
import { ScrollView } from "@getpaseo/plugin/client/ui";
|
|
4
4
|
import type { OfficeAgent } from "../shared/contracts.js";
|
|
5
5
|
import { dotTone, type DotTone } from "../shared/model.js";
|
|
6
6
|
import { ActionButton } from "./action-button.js";
|
|
7
|
+
import { OpenTimelineContext } from "./timeline-open.js";
|
|
7
8
|
import {
|
|
8
9
|
feedCards,
|
|
9
10
|
feedStore,
|
|
@@ -223,6 +224,8 @@ export function ActivityPanel({ office, styles, link, layout }: ColumnPanelProps
|
|
|
223
224
|
});
|
|
224
225
|
}, []);
|
|
225
226
|
const problem = loadProblem(subscription);
|
|
227
|
+
// What the agents did before now: the lanes, their replay and the week.
|
|
228
|
+
const openTimeline = useContext(OpenTimelineContext);
|
|
226
229
|
return (
|
|
227
230
|
<View style={styles.screen}>
|
|
228
231
|
<ScrollView contentContainerStyle={styles.panelFeed}>
|
|
@@ -239,6 +242,14 @@ export function ActivityPanel({ office, styles, link, layout }: ColumnPanelProps
|
|
|
239
242
|
<Text style={styles.panelLiveText}>Live</Text>
|
|
240
243
|
</View>
|
|
241
244
|
) : null}
|
|
245
|
+
{openTimeline ? (
|
|
246
|
+
<ActionButton
|
|
247
|
+
label="Timeline"
|
|
248
|
+
pendingLabel="Timeline"
|
|
249
|
+
onPress={openTimeline}
|
|
250
|
+
styles={styles}
|
|
251
|
+
/>
|
|
252
|
+
) : null}
|
|
242
253
|
</View>
|
|
243
254
|
<View role="group" aria-label="Feed" style={styles.activityTabs}>
|
|
244
255
|
<Pill<FeedTab>
|
|
@@ -60,6 +60,8 @@ import { weatherEntry } from "./weather-text.js";
|
|
|
60
60
|
import { incidentsEntry } from "./incidents-text.js";
|
|
61
61
|
import { janitorNote, janitorRows } from "./context-text.js";
|
|
62
62
|
import { JanitorCard } from "./janitor-card.js";
|
|
63
|
+
import { TimelineCard } from "./timeline-card.js";
|
|
64
|
+
import { OpenTimelineContext } from "./timeline-open.js";
|
|
63
65
|
import { useSshServers } from "./ssh-hosts.js";
|
|
64
66
|
import { MachineCard } from "./machine-card.js";
|
|
65
67
|
import { hostNote, memoryPercent, namedMachine, type HostNote } from "../shared/machine-text.js";
|
|
@@ -113,7 +115,7 @@ import { useScriptLog } from "./use-script-log.js";
|
|
|
113
115
|
import { useStandup } from "./use-standup.js";
|
|
114
116
|
import { useMachine } from "./use-machine.js";
|
|
115
117
|
import { PlanUsageContext, usePlanUsage } from "./use-plan-usage.js";
|
|
116
|
-
import { planNote } from "../shared/plan-usage.js";
|
|
118
|
+
import { planNote, type PlanUsage } from "../shared/plan-usage.js";
|
|
117
119
|
import { useOffice, type OfficeState } from "./use-office.js";
|
|
118
120
|
import { UNSAVED_SOUND, useSoundSwitch, type SoundSwitch } from "./use-sound.js";
|
|
119
121
|
import { ViewSwitch } from "./view-switch.js";
|
|
@@ -873,6 +875,7 @@ interface ViewControlsProps {
|
|
|
873
875
|
onTeam(): void;
|
|
874
876
|
machine: Machine | null;
|
|
875
877
|
onMachine?: () => void;
|
|
878
|
+
onTimeline?: () => void;
|
|
876
879
|
onChange(next: OfficeViewChoice): void;
|
|
877
880
|
compact: boolean;
|
|
878
881
|
touchScreen: boolean;
|
|
@@ -936,6 +939,7 @@ function ViewControls({
|
|
|
936
939
|
onTeam,
|
|
937
940
|
machine,
|
|
938
941
|
onMachine,
|
|
942
|
+
onTimeline,
|
|
939
943
|
onChange,
|
|
940
944
|
compact,
|
|
941
945
|
touchScreen,
|
|
@@ -978,6 +982,17 @@ function ViewControls({
|
|
|
978
982
|
styles={styles}
|
|
979
983
|
/>
|
|
980
984
|
)}
|
|
985
|
+
{sidebars && onTimeline ? (
|
|
986
|
+
<PanelButton
|
|
987
|
+
label="Timeline"
|
|
988
|
+
icon="History"
|
|
989
|
+
screenId="timeline"
|
|
990
|
+
size={touchScreen ? 20 : 16}
|
|
991
|
+
color={color}
|
|
992
|
+
onOpen={onTimeline}
|
|
993
|
+
styles={styles}
|
|
994
|
+
/>
|
|
995
|
+
) : null}
|
|
981
996
|
{full.available ? (
|
|
982
997
|
<PanelButton
|
|
983
998
|
label={full.active ? "Exit full screen" : "Full screen"}
|
|
@@ -1776,6 +1791,35 @@ function useWorkspaceOverlay(
|
|
|
1776
1791
|
return { workspace, closeWorkspace, showWorkspace, showAgentWorkspace, openWorkspaceBoard };
|
|
1777
1792
|
}
|
|
1778
1793
|
|
|
1794
|
+
/** What the surface's parts read without it being handed down: the brand, the plans' limits, the timeline's opener. */
|
|
1795
|
+
function OfficeContexts({
|
|
1796
|
+
brand,
|
|
1797
|
+
planUsage,
|
|
1798
|
+
onTimeline,
|
|
1799
|
+
children,
|
|
1800
|
+
}: {
|
|
1801
|
+
brand: OfficeState["state"]["brand"];
|
|
1802
|
+
planUsage: readonly PlanUsage[];
|
|
1803
|
+
onTimeline: (() => void) | undefined;
|
|
1804
|
+
children: ReactNode;
|
|
1805
|
+
}) {
|
|
1806
|
+
return (
|
|
1807
|
+
<OfficeBrandContext.Provider value={brand}>
|
|
1808
|
+
<PlanUsageContext.Provider value={planUsage}>
|
|
1809
|
+
<OpenTimelineContext.Provider value={onTimeline}>{children}</OpenTimelineContext.Provider>
|
|
1810
|
+
</PlanUsageContext.Provider>
|
|
1811
|
+
</OfficeBrandContext.Provider>
|
|
1812
|
+
);
|
|
1813
|
+
}
|
|
1814
|
+
|
|
1815
|
+
/** The timeline card: offered and shown only while the office's data is live. */
|
|
1816
|
+
function useTimelineCard(live: boolean) {
|
|
1817
|
+
const [open, setOpen] = useState(false);
|
|
1818
|
+
const show = useCallback(() => setOpen(true), []);
|
|
1819
|
+
const close = useCallback(() => setOpen(false), []);
|
|
1820
|
+
return { open: open && live, onOpen: live ? show : undefined, close };
|
|
1821
|
+
}
|
|
1822
|
+
|
|
1779
1823
|
function useHostResources(machine: Machine | null, live: boolean) {
|
|
1780
1824
|
const [machineOpen, setMachineOpen] = useState(false);
|
|
1781
1825
|
const openMachine = useCallback(() => setMachineOpen(true), []);
|
|
@@ -1853,6 +1897,7 @@ export function OfficeSurface(
|
|
|
1853
1897
|
live,
|
|
1854
1898
|
);
|
|
1855
1899
|
const planUsage = usePlanUsage(live);
|
|
1900
|
+
const timeline = useTimelineCard(live);
|
|
1856
1901
|
const board = useBoardSheet(model, live);
|
|
1857
1902
|
const dutyCard = useCardOpening(live);
|
|
1858
1903
|
// Until the host sends its duty list (an older host has none) nothing offers the duty board.
|
|
@@ -2070,6 +2115,7 @@ export function OfficeSurface(
|
|
|
2070
2115
|
onTeam={openTeam}
|
|
2071
2116
|
machine={machine}
|
|
2072
2117
|
onMachine={hostEntry?.onOpen}
|
|
2118
|
+
onTimeline={timeline.onOpen}
|
|
2073
2119
|
onChange={changeView}
|
|
2074
2120
|
compact={compact}
|
|
2075
2121
|
touchScreen={touchScreen}
|
|
@@ -2200,6 +2246,7 @@ export function OfficeSurface(
|
|
|
2200
2246
|
styles={styles}
|
|
2201
2247
|
onClose={closeMachine}
|
|
2202
2248
|
/>
|
|
2249
|
+
<TimelineCard open={timeline.open} styles={styles} onClose={timeline.close} />
|
|
2203
2250
|
<JanitorCard
|
|
2204
2251
|
rows={contexts}
|
|
2205
2252
|
open={janitorOpen && live}
|
|
@@ -2310,12 +2357,10 @@ export function OfficeSurface(
|
|
|
2310
2357
|
</View>
|
|
2311
2358
|
);
|
|
2312
2359
|
return (
|
|
2313
|
-
<
|
|
2314
|
-
<
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
</PlanUsageContext.Provider>
|
|
2319
|
-
</OfficeBrandContext.Provider>
|
|
2360
|
+
<OfficeContexts brand={office.state.brand} planUsage={planUsage} onTimeline={timeline.onOpen}>
|
|
2361
|
+
<OfficeDialogs fullscreen={full.integrated} theme={theme} styles={styles}>
|
|
2362
|
+
{surface}
|
|
2363
|
+
</OfficeDialogs>
|
|
2364
|
+
</OfficeContexts>
|
|
2320
2365
|
);
|
|
2321
2366
|
}
|
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
import { StyleSheet, Text, View, type ViewStyle } from "react-native";
|
|
3
|
+
import { Icon } from "@getpaseo/plugin/client/ui";
|
|
4
|
+
import { useRpc } from "@getpaseo/plugin/client";
|
|
5
|
+
import { timelineRpc } from "../shared/contracts.js";
|
|
6
|
+
import {
|
|
7
|
+
DAY_MS,
|
|
8
|
+
REPLAY_SPEEDS,
|
|
9
|
+
TIMELINE_RANGES,
|
|
10
|
+
doingAt,
|
|
11
|
+
durationText,
|
|
12
|
+
weekRecap,
|
|
13
|
+
type ReplaySpeed,
|
|
14
|
+
type Timeline,
|
|
15
|
+
type TimelineLane,
|
|
16
|
+
type TimelineRange,
|
|
17
|
+
type TimelineState,
|
|
18
|
+
} from "../shared/timeline.js";
|
|
19
|
+
import { ActionButton } from "./action-button.js";
|
|
20
|
+
import { OfficeModal as Modal } from "./office-modal.js";
|
|
21
|
+
import { SegmentPill } from "./segment-pill.js";
|
|
22
|
+
import type { OfficeStyles } from "./styles.js";
|
|
23
|
+
|
|
24
|
+
type Tab = TimelineRange | "week";
|
|
25
|
+
const WEEK_MS = 7 * DAY_MS;
|
|
26
|
+
/** A replay's head moves this often; each move is this long times the speed. */
|
|
27
|
+
const TICK_MS = 100;
|
|
28
|
+
/** While nothing is replayed the lanes are read again this often. */
|
|
29
|
+
const REFRESH_MS = 30_000;
|
|
30
|
+
|
|
31
|
+
const layout = StyleSheet.create({
|
|
32
|
+
lanes: { gap: 10 },
|
|
33
|
+
lane: { gap: 4 },
|
|
34
|
+
laneHead: { flexDirection: "row", justifyContent: "space-between", gap: 8 },
|
|
35
|
+
laneName: { flexShrink: 1 },
|
|
36
|
+
track: { height: 14, borderRadius: 4, overflow: "hidden" },
|
|
37
|
+
piece: { position: "absolute", top: 0, bottom: 0 },
|
|
38
|
+
step: { position: "absolute", top: 0, bottom: 0, width: 1, opacity: 0.55 },
|
|
39
|
+
head: { position: "absolute", top: 0, bottom: 0, width: 2 },
|
|
40
|
+
controls: { flexDirection: "row", flexWrap: "wrap", alignItems: "center", gap: 8 },
|
|
41
|
+
figures: { flexDirection: "row", flexWrap: "wrap", gap: 12 },
|
|
42
|
+
figure: { flexBasis: 120, flexGrow: 1, gap: 2 },
|
|
43
|
+
bars: { flexDirection: "row", alignItems: "flex-end", gap: 6, height: 72 },
|
|
44
|
+
barColumn: { flex: 1, alignItems: "center", justifyContent: "flex-end", gap: 4, height: "100%" },
|
|
45
|
+
bar: { width: "100%", borderRadius: 3, minHeight: 2 },
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
function clockText(at: number): string {
|
|
49
|
+
const date = new Date(at);
|
|
50
|
+
const pad = (value: number) => String(value).padStart(2, "0");
|
|
51
|
+
return `${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
|
55
|
+
|
|
56
|
+
function stateColor(styles: OfficeStyles, state: TimelineState): string {
|
|
57
|
+
const dot = { work: "running", wait: "warning", fail: "danger", rest: "idle" } as const;
|
|
58
|
+
return String(styles.dot[dot[state]].backgroundColor);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
interface LaneProps {
|
|
62
|
+
lane: TimelineLane;
|
|
63
|
+
from: number;
|
|
64
|
+
to: number;
|
|
65
|
+
/** Where the replay is; null while nothing is replayed. */
|
|
66
|
+
head: number | null;
|
|
67
|
+
styles: OfficeStyles;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function Lane({ lane, from, to, head, styles }: LaneProps) {
|
|
71
|
+
const width = to - from;
|
|
72
|
+
const pieces = useMemo(() => {
|
|
73
|
+
const percent = (at: number) => `${((at - from) / width) * 100}%` as const;
|
|
74
|
+
return {
|
|
75
|
+
spans: lane.spans.map((span) => ({
|
|
76
|
+
key: `${span.from}`,
|
|
77
|
+
style: [
|
|
78
|
+
layout.piece,
|
|
79
|
+
{
|
|
80
|
+
left: percent(span.from),
|
|
81
|
+
width: percent(from + (span.to - span.from)),
|
|
82
|
+
backgroundColor: stateColor(styles, span.state),
|
|
83
|
+
opacity: span.state === "rest" ? 0.3 : 1,
|
|
84
|
+
} satisfies ViewStyle,
|
|
85
|
+
],
|
|
86
|
+
})),
|
|
87
|
+
steps: lane.steps.map((step, index) => ({
|
|
88
|
+
key: `${step.at}:${index}`,
|
|
89
|
+
style: [
|
|
90
|
+
layout.step,
|
|
91
|
+
{ left: percent(step.at), backgroundColor: String(styles.host.name.color) },
|
|
92
|
+
],
|
|
93
|
+
})),
|
|
94
|
+
};
|
|
95
|
+
}, [lane, from, width, styles]);
|
|
96
|
+
const headStyle = useMemo(
|
|
97
|
+
() =>
|
|
98
|
+
head === null
|
|
99
|
+
? null
|
|
100
|
+
: [
|
|
101
|
+
layout.head,
|
|
102
|
+
{
|
|
103
|
+
left: `${((head - from) / width) * 100}%` as const,
|
|
104
|
+
backgroundColor: String(styles.host.name.color),
|
|
105
|
+
},
|
|
106
|
+
],
|
|
107
|
+
[head, from, width, styles],
|
|
108
|
+
);
|
|
109
|
+
const doing = doingAt(lane, head ?? to - 1);
|
|
110
|
+
const track = useMemo(() => [styles.host.track, layout.track], [styles]);
|
|
111
|
+
const name = useMemo(() => [styles.host.name, layout.laneName], [styles]);
|
|
112
|
+
return (
|
|
113
|
+
<View
|
|
114
|
+
accessible
|
|
115
|
+
accessibilityRole="text"
|
|
116
|
+
accessibilityLabel={`${lane.name}, ${lane.team}: ${doing}`}
|
|
117
|
+
style={layout.lane}
|
|
118
|
+
>
|
|
119
|
+
<View style={layout.laneHead}>
|
|
120
|
+
<Text style={name} numberOfLines={1}>
|
|
121
|
+
{lane.name}
|
|
122
|
+
</Text>
|
|
123
|
+
<Text style={styles.host.note} numberOfLines={1}>
|
|
124
|
+
{lane.team} · {doing}
|
|
125
|
+
</Text>
|
|
126
|
+
</View>
|
|
127
|
+
<View style={track}>
|
|
128
|
+
{pieces.spans.map((piece) => (
|
|
129
|
+
<View key={piece.key} style={piece.style} />
|
|
130
|
+
))}
|
|
131
|
+
{pieces.steps.map((piece) => (
|
|
132
|
+
<View key={piece.key} style={piece.style} />
|
|
133
|
+
))}
|
|
134
|
+
{headStyle && <View style={headStyle} />}
|
|
135
|
+
</View>
|
|
136
|
+
</View>
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function Figure({ label, value, styles }: { label: string; value: string; styles: OfficeStyles }) {
|
|
141
|
+
return (
|
|
142
|
+
<View accessible accessibilityLabel={`${label}: ${value}`} style={layout.figure}>
|
|
143
|
+
<Text style={styles.host.title}>{value}</Text>
|
|
144
|
+
<Text style={styles.host.note}>{label}</Text>
|
|
145
|
+
</View>
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function Week({ timeline, styles }: { timeline: Timeline; styles: OfficeStyles }) {
|
|
150
|
+
const recap = useMemo(() => weekRecap(timeline), [timeline]);
|
|
151
|
+
const most = Math.max(1, ...recap.days.map((day) => day.workMs));
|
|
152
|
+
const bars = useMemo(
|
|
153
|
+
() =>
|
|
154
|
+
recap.days.map((day) => ({
|
|
155
|
+
key: String(day.from),
|
|
156
|
+
// The day a bar's 24 hours end in: the last one is today.
|
|
157
|
+
label: WEEKDAYS[new Date(day.from + DAY_MS - 1).getDay()],
|
|
158
|
+
text: durationText(day.workMs),
|
|
159
|
+
style: [
|
|
160
|
+
layout.bar,
|
|
161
|
+
{
|
|
162
|
+
height: `${(day.workMs / most) * 100}%` as const,
|
|
163
|
+
backgroundColor: stateColor(styles, "work"),
|
|
164
|
+
opacity: day.workMs === 0 ? 0.25 : 1,
|
|
165
|
+
},
|
|
166
|
+
],
|
|
167
|
+
})),
|
|
168
|
+
[recap.days, most, styles],
|
|
169
|
+
);
|
|
170
|
+
if (recap.workMs === 0) {
|
|
171
|
+
return (
|
|
172
|
+
<Text style={styles.host.empty}>
|
|
173
|
+
Nobody was seen at work in the last seven days. The office sees only while it is open.
|
|
174
|
+
</Text>
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
return (
|
|
178
|
+
<View style={styles.host.body}>
|
|
179
|
+
<View style={layout.figures}>
|
|
180
|
+
<Figure label="At work" value={durationText(recap.workMs)} styles={styles} />
|
|
181
|
+
<Figure label="Turns" value={String(recap.prompts)} styles={styles} />
|
|
182
|
+
<Figure label="Edits" value={String(recap.edits)} styles={styles} />
|
|
183
|
+
<Figure label="Commands" value={String(recap.commands)} styles={styles} />
|
|
184
|
+
<Figure
|
|
185
|
+
label="Cost"
|
|
186
|
+
value={recap.costUsd === null ? "Not reported" : `$${recap.costUsd.toFixed(2)}`}
|
|
187
|
+
styles={styles}
|
|
188
|
+
/>
|
|
189
|
+
<Figure label="Busiest team" value={recap.busiestTeam ?? "None"} styles={styles} />
|
|
190
|
+
</View>
|
|
191
|
+
<View style={layout.bars}>
|
|
192
|
+
{bars.map((bar) => (
|
|
193
|
+
<View
|
|
194
|
+
key={bar.key}
|
|
195
|
+
accessible
|
|
196
|
+
accessibilityLabel={`${bar.label}: ${bar.text} at work`}
|
|
197
|
+
style={layout.barColumn}
|
|
198
|
+
>
|
|
199
|
+
<View style={bar.style} />
|
|
200
|
+
<Text style={styles.host.note}>{bar.label}</Text>
|
|
201
|
+
</View>
|
|
202
|
+
))}
|
|
203
|
+
</View>
|
|
204
|
+
<Text style={styles.host.note}>
|
|
205
|
+
Counted from what the office saw while it was open, in the 24 hours each bar ends.
|
|
206
|
+
</Text>
|
|
207
|
+
</View>
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
interface Replay {
|
|
212
|
+
head: number;
|
|
213
|
+
playing: boolean;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** A replay's head, moved while it plays; it stops at the end. */
|
|
217
|
+
function useReplay(timeline: Timeline | null, speed: ReplaySpeed) {
|
|
218
|
+
const [replay, setReplay] = useState<Replay | null>(null);
|
|
219
|
+
const end = timeline?.to ?? 0;
|
|
220
|
+
const playing = replay?.playing ?? false;
|
|
221
|
+
useEffect(() => {
|
|
222
|
+
if (!playing) return undefined;
|
|
223
|
+
const timer = setInterval(() => {
|
|
224
|
+
setReplay((now) => {
|
|
225
|
+
if (now === null || !now.playing) return now;
|
|
226
|
+
const head = now.head + TICK_MS * speed;
|
|
227
|
+
return head >= end ? { head: end, playing: false } : { head, playing: true };
|
|
228
|
+
});
|
|
229
|
+
}, TICK_MS);
|
|
230
|
+
return () => clearInterval(timer);
|
|
231
|
+
}, [playing, speed, end]);
|
|
232
|
+
const toggle = useCallback(() => {
|
|
233
|
+
if (timeline === null) return;
|
|
234
|
+
setReplay((now) => {
|
|
235
|
+
// From the start when there is none, and again when the last one reached the end.
|
|
236
|
+
if (now === null || now.head >= timeline.to) return { head: timeline.from, playing: true };
|
|
237
|
+
return { head: now.head, playing: !now.playing };
|
|
238
|
+
});
|
|
239
|
+
}, [timeline]);
|
|
240
|
+
const stop = useCallback(() => setReplay(null), []);
|
|
241
|
+
return { replay, toggle, stop };
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
interface LanesProps {
|
|
245
|
+
timeline: Timeline;
|
|
246
|
+
replay: Replay | null;
|
|
247
|
+
speed: ReplaySpeed;
|
|
248
|
+
onSpeed(next: ReplaySpeed): void;
|
|
249
|
+
onToggle(): void;
|
|
250
|
+
onStop(): void;
|
|
251
|
+
styles: OfficeStyles;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** The replay's controls over a lane per agent. */
|
|
255
|
+
function Lanes({ timeline, replay, speed, onSpeed, onToggle, onStop, styles }: LanesProps) {
|
|
256
|
+
let playLabel = "Replay";
|
|
257
|
+
if (replay?.playing) playLabel = "Pause";
|
|
258
|
+
else if (replay !== null && replay.head < timeline.to) playLabel = "Resume";
|
|
259
|
+
return (
|
|
260
|
+
<>
|
|
261
|
+
<View style={layout.controls}>
|
|
262
|
+
<ActionButton
|
|
263
|
+
label={playLabel}
|
|
264
|
+
pendingLabel={playLabel}
|
|
265
|
+
onPress={onToggle}
|
|
266
|
+
disabled={timeline.lanes.length === 0}
|
|
267
|
+
styles={styles}
|
|
268
|
+
primary
|
|
269
|
+
/>
|
|
270
|
+
{replay !== null && (
|
|
271
|
+
<ActionButton label="Now" pendingLabel="Now" onPress={onStop} styles={styles} />
|
|
272
|
+
)}
|
|
273
|
+
<View role="group" aria-label="Replay speed" style={styles.segments}>
|
|
274
|
+
{REPLAY_SPEEDS.map((value) => (
|
|
275
|
+
<SegmentPill
|
|
276
|
+
key={value}
|
|
277
|
+
value={value}
|
|
278
|
+
name={`${value} times the clock`}
|
|
279
|
+
text={`${value}×`}
|
|
280
|
+
on={speed === value}
|
|
281
|
+
disabled={false}
|
|
282
|
+
onPress={onSpeed}
|
|
283
|
+
styles={styles}
|
|
284
|
+
/>
|
|
285
|
+
))}
|
|
286
|
+
</View>
|
|
287
|
+
<Text accessibilityLiveRegion="none" style={styles.machineValue}>
|
|
288
|
+
{clockText(replay?.head ?? timeline.to)}
|
|
289
|
+
</Text>
|
|
290
|
+
</View>
|
|
291
|
+
{timeline.lanes.length === 0 ? (
|
|
292
|
+
<Text style={styles.host.empty}>
|
|
293
|
+
Nobody was seen in this time. The office sees only while it is open.
|
|
294
|
+
</Text>
|
|
295
|
+
) : (
|
|
296
|
+
<View style={layout.lanes}>
|
|
297
|
+
{timeline.lanes.map((lane) => (
|
|
298
|
+
<Lane
|
|
299
|
+
key={lane.id}
|
|
300
|
+
lane={lane}
|
|
301
|
+
from={timeline.from}
|
|
302
|
+
to={timeline.to}
|
|
303
|
+
head={replay?.head ?? null}
|
|
304
|
+
styles={styles}
|
|
305
|
+
/>
|
|
306
|
+
))}
|
|
307
|
+
</View>
|
|
308
|
+
)}
|
|
309
|
+
<Text style={styles.host.note}>
|
|
310
|
+
Green is at work, amber waiting for you, red stopped on an error, grey resting. A gap is a
|
|
311
|
+
time the office was closed. The marks are tool calls.
|
|
312
|
+
</Text>
|
|
313
|
+
</>
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
interface Read {
|
|
318
|
+
timeline: Timeline | null;
|
|
319
|
+
problem: string | null;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** The lanes of the last `ms`, read when the card shows and again while `fresh`. */
|
|
323
|
+
function useTimeline(open: boolean, ms: number, fresh: boolean): Read {
|
|
324
|
+
const ask = useRpc(timelineRpc);
|
|
325
|
+
// Kept in a ref: the read restarts with the range, not with the call's identity.
|
|
326
|
+
const asking = useRef(ask);
|
|
327
|
+
asking.current = ask;
|
|
328
|
+
const [read, setRead] = useState<Read & { ms: number }>({ timeline: null, problem: null, ms });
|
|
329
|
+
useEffect(() => {
|
|
330
|
+
if (!open) return undefined;
|
|
331
|
+
let ended = false;
|
|
332
|
+
const load = async () => {
|
|
333
|
+
const to = Date.now();
|
|
334
|
+
try {
|
|
335
|
+
const answer = await asking.current({ from: to - ms, to });
|
|
336
|
+
if (ended) return;
|
|
337
|
+
setRead(
|
|
338
|
+
answer.ok
|
|
339
|
+
? { timeline: answer.timeline, problem: null, ms }
|
|
340
|
+
: { timeline: null, problem: answer.problem, ms },
|
|
341
|
+
);
|
|
342
|
+
} catch (error) {
|
|
343
|
+
const problem = error instanceof Error ? error.message : String(error);
|
|
344
|
+
if (!ended) setRead({ timeline: null, problem, ms });
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
void load();
|
|
348
|
+
const timer = fresh ? setInterval(() => void load(), REFRESH_MS) : null;
|
|
349
|
+
return () => {
|
|
350
|
+
ended = true;
|
|
351
|
+
if (timer !== null) clearInterval(timer);
|
|
352
|
+
};
|
|
353
|
+
}, [open, ms, fresh]);
|
|
354
|
+
// Another range's lanes are not shown under this one's pills.
|
|
355
|
+
return read.ms === ms ? read : { timeline: null, problem: null };
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
export interface TimelineCardProps {
|
|
359
|
+
open: boolean;
|
|
360
|
+
styles: OfficeStyles;
|
|
361
|
+
onClose(): void;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* What each agent was at over the last hours, a lane each, replayed at up to 120 times the
|
|
366
|
+
* clock; and the week in figures. It shows what the office saw while it was open.
|
|
367
|
+
*/
|
|
368
|
+
export function TimelineCard({ open, styles, onClose }: TimelineCardProps) {
|
|
369
|
+
const [tab, setTab] = useState<Tab>("1h");
|
|
370
|
+
const [speed, setSpeed] = useState<ReplaySpeed>(60);
|
|
371
|
+
const ms = tab === "week" ? WEEK_MS : (TIMELINE_RANGES.find((r) => r.id === tab)?.ms ?? 0);
|
|
372
|
+
const [replaying, setReplaying] = useState(false);
|
|
373
|
+
const { timeline, problem } = useTimeline(open, ms, !replaying);
|
|
374
|
+
const { replay, toggle, stop } = useReplay(tab === "week" ? null : timeline, speed);
|
|
375
|
+
useEffect(() => setReplaying(replay !== null), [replay]);
|
|
376
|
+
const choose = useCallback(
|
|
377
|
+
(next: Tab) => {
|
|
378
|
+
stop();
|
|
379
|
+
setTab(next);
|
|
380
|
+
},
|
|
381
|
+
[stop],
|
|
382
|
+
);
|
|
383
|
+
const change = useCallback(
|
|
384
|
+
(next: boolean) => {
|
|
385
|
+
if (next) return;
|
|
386
|
+
stop();
|
|
387
|
+
onClose();
|
|
388
|
+
},
|
|
389
|
+
[onClose, stop],
|
|
390
|
+
);
|
|
391
|
+
const icon = useMemo(
|
|
392
|
+
() => <Icon name="History" size={18} color={String(styles.host.label.color)} />,
|
|
393
|
+
[styles],
|
|
394
|
+
);
|
|
395
|
+
return (
|
|
396
|
+
<Modal icon={icon} title="Timeline" open={open} onOpenChange={change}>
|
|
397
|
+
<Modal.Content>
|
|
398
|
+
{open && (
|
|
399
|
+
<View style={styles.host.body}>
|
|
400
|
+
<View role="group" aria-label="Range" style={styles.segments}>
|
|
401
|
+
{TIMELINE_RANGES.map((range) => (
|
|
402
|
+
<SegmentPill
|
|
403
|
+
key={range.id}
|
|
404
|
+
value={range.id}
|
|
405
|
+
name={range.label}
|
|
406
|
+
on={tab === range.id}
|
|
407
|
+
disabled={false}
|
|
408
|
+
onPress={choose}
|
|
409
|
+
styles={styles}
|
|
410
|
+
/>
|
|
411
|
+
))}
|
|
412
|
+
<SegmentPill
|
|
413
|
+
value="week"
|
|
414
|
+
name="This week"
|
|
415
|
+
on={tab === "week"}
|
|
416
|
+
disabled={false}
|
|
417
|
+
onPress={choose}
|
|
418
|
+
styles={styles}
|
|
419
|
+
/>
|
|
420
|
+
</View>
|
|
421
|
+
{problem && (
|
|
422
|
+
<Text accessibilityRole="alert" style={styles.actionError}>
|
|
423
|
+
The timeline cannot be read: {problem}
|
|
424
|
+
</Text>
|
|
425
|
+
)}
|
|
426
|
+
{timeline === null && problem === null && (
|
|
427
|
+
<Text style={styles.host.empty}>Reading the timeline...</Text>
|
|
428
|
+
)}
|
|
429
|
+
{timeline !== null && tab === "week" && <Week timeline={timeline} styles={styles} />}
|
|
430
|
+
{timeline !== null && tab !== "week" && (
|
|
431
|
+
<Lanes
|
|
432
|
+
timeline={timeline}
|
|
433
|
+
replay={replay}
|
|
434
|
+
speed={speed}
|
|
435
|
+
onSpeed={setSpeed}
|
|
436
|
+
onToggle={toggle}
|
|
437
|
+
onStop={stop}
|
|
438
|
+
styles={styles}
|
|
439
|
+
/>
|
|
440
|
+
)}
|
|
441
|
+
</View>
|
|
442
|
+
)}
|
|
443
|
+
</Modal.Content>
|
|
444
|
+
</Modal>
|
|
445
|
+
);
|
|
446
|
+
}
|
package/docs/office.md
CHANGED
|
@@ -497,6 +497,25 @@ The gear in fullscreen opens **Office settings** over the floor. You can also op
|
|
|
497
497
|
|
|
498
498
|
View and sound preferences apply to the device. The host stores the floor plan, branding, automation and integration settings. The [branding defaults](#custom-branding) can also come from the daemon environment.
|
|
499
499
|
|
|
500
|
+
## Timeline and the week
|
|
501
|
+
|
|
502
|
+
The **Timeline** button, in the toolbar of a wide office and at the top of the Activity tab, opens a card with a lane for each agent over the last hour,
|
|
503
|
+
6 hours or 24 hours: green at work, amber waiting for the user, red stopped on an error, grey
|
|
504
|
+
resting, and a mark at each tool call. **Replay** moves a head across the lanes from the start of
|
|
505
|
+
the range at 30, 60 or 120 times the clock, and each lane says what its agent was at then
|
|
506
|
+
("editing", "running tests", "waiting for you"). **Now** ends the replay. The replay is in the card
|
|
507
|
+
only: the 3D floor keeps showing the present.
|
|
508
|
+
|
|
509
|
+
**This week** adds the last seven days up: time at work, turns started, edits, commands, cost where
|
|
510
|
+
the provider reports one, the busiest team, and a bar for each 24 hours.
|
|
511
|
+
|
|
512
|
+
The office's server keeps this log itself (`server/timeline.ts`, `office/timeline.json`, mode 600):
|
|
513
|
+
each change of an agent's state, the kind of each tool call (never its file, command or text) and
|
|
514
|
+
each rise of its cost, for eight days and at most 40,000 marks. It sees only while an office
|
|
515
|
+
surface is open on some device, at the directory's changes and the 5-second activity reads. A time
|
|
516
|
+
the office was closed is a gap in every lane, not a rest, and the week's figures count only what
|
|
517
|
+
was seen. `office.timeline.request` reads it.
|
|
518
|
+
|
|
500
519
|
## Streamer mode, graphics and sticky notes
|
|
501
520
|
|
|
502
521
|
**Streamer mode** (Settings, host-wide, on by default) keeps file names, commands, search queries
|
package/index.server.ts
CHANGED
|
@@ -9,7 +9,7 @@ import { createStreetWatch, streetReaders } from "./server/street-watch.js";
|
|
|
9
9
|
import { streetSubscription } from "./shared/contracts.js";
|
|
10
10
|
import { createLifeFeed } from "./server/life-feed.js";
|
|
11
11
|
import { createLifeFile, createSaveQueue, type LifeFile } from "./server/life-store.js";
|
|
12
|
-
import { liftFile, tenureFile } from "./server/paths.js";
|
|
12
|
+
import { liftFile, tenureFile, timelineFile } from "./server/paths.js";
|
|
13
13
|
import { createDepartureArchiver } from "./server/home-departure.js";
|
|
14
14
|
import {
|
|
15
15
|
lifeSubscription,
|
|
@@ -45,6 +45,13 @@ import {
|
|
|
45
45
|
import { probeSentry } from "./server/incidents-sentry.js";
|
|
46
46
|
import { createIncidentsStore, type IncidentsStore } from "./server/incidents-store.js";
|
|
47
47
|
import { projectPlanUsage } from "./server/plan-usage.js";
|
|
48
|
+
import {
|
|
49
|
+
TIMELINE_KEEP_MS,
|
|
50
|
+
TimelineFileSchema,
|
|
51
|
+
createTimelineLog,
|
|
52
|
+
type TimelineFile,
|
|
53
|
+
type TimelineLog,
|
|
54
|
+
} from "./server/timeline.js";
|
|
48
55
|
import { TEAM_NAME_KEY, teamNameOf, tidyTeamName } from "./shared/team-name.js";
|
|
49
56
|
import {
|
|
50
57
|
changeSavedTrackers,
|
|
@@ -112,6 +119,7 @@ import {
|
|
|
112
119
|
incidentsDismissRpc,
|
|
113
120
|
notesModesRpc,
|
|
114
121
|
planUsageRpc,
|
|
122
|
+
timelineRpc,
|
|
115
123
|
teamRenameRpc,
|
|
116
124
|
trackersSaveRpc,
|
|
117
125
|
trackersTestRpc,
|
|
@@ -149,6 +157,8 @@ export interface OfficePluginDeps {
|
|
|
149
157
|
/** Where the team chat keeps its conversation. */
|
|
150
158
|
chatStore: ChatStore;
|
|
151
159
|
tenureFile: LifeFile<TenureLedger>;
|
|
160
|
+
/** Where the timeline is kept; without it the timeline lasts as long as the server. */
|
|
161
|
+
timelineFile?: LifeFile<TimelineFile>;
|
|
152
162
|
liftFile: LifeFile<LiftKept>;
|
|
153
163
|
/** The owner's duty list. */
|
|
154
164
|
dutiesFile: LifeFile<DutiesFile>;
|
|
@@ -212,7 +222,30 @@ export function createOfficePlugin(
|
|
|
212
222
|
const holder = createPaseoHolder();
|
|
213
223
|
// Off until the settings are read: nothing is named before the host has said it may be.
|
|
214
224
|
let stepsNamed = false;
|
|
215
|
-
const
|
|
225
|
+
const hubPorts = liveHubPorts(holder, deps.clock, log, () => stepsNamed);
|
|
226
|
+
// Set once its file is read; what the office sees before that is not kept.
|
|
227
|
+
let timeline: TimelineLog | null = null;
|
|
228
|
+
const hub = createHub({
|
|
229
|
+
...hubPorts,
|
|
230
|
+
startActivity: (running, onChange) =>
|
|
231
|
+
hubPorts.startActivity(running, (activity, doing, ...rest) => {
|
|
232
|
+
timeline?.steps(doing);
|
|
233
|
+
onChange(activity, doing, ...rest);
|
|
234
|
+
}),
|
|
235
|
+
});
|
|
236
|
+
const timelineSaves =
|
|
237
|
+
deps.timelineFile === undefined
|
|
238
|
+
? null
|
|
239
|
+
: createSaveQueue(deps.timelineFile, deps.clock, log, 30_000);
|
|
240
|
+
let stopTimeline: (() => void) | undefined;
|
|
241
|
+
const timelineReady = (async () => {
|
|
242
|
+
const kept = (await deps.timelineFile?.load()) ?? null;
|
|
243
|
+
timeline = createTimelineLog(
|
|
244
|
+
{ now: () => deps.clock.now(), save: (file) => timelineSaves?.save(file) },
|
|
245
|
+
kept,
|
|
246
|
+
);
|
|
247
|
+
stopTimeline = hub.watch(timeline.view);
|
|
248
|
+
})().catch((error: unknown) => log(`[office] timeline unavailable: ${errorMessage(error)}`));
|
|
216
249
|
hub.setBrand(hostBrand({}, deps.env));
|
|
217
250
|
const office = createOffice({
|
|
218
251
|
store: deps.store,
|
|
@@ -659,6 +692,13 @@ export function createOfficePlugin(
|
|
|
659
692
|
throw error;
|
|
660
693
|
}
|
|
661
694
|
}
|
|
695
|
+
server.handle(timelineRpc, async ({ from, to }) => {
|
|
696
|
+
await timelineReady;
|
|
697
|
+
if (timeline === null) return { ok: false as const, problem: "The timeline cannot be read" };
|
|
698
|
+
const end = Math.min(to, deps.clock.now());
|
|
699
|
+
const start = Math.max(from, end - TIMELINE_KEEP_MS);
|
|
700
|
+
return { ok: true as const, timeline: timeline.read(start, end) };
|
|
701
|
+
});
|
|
662
702
|
server.handle(planUsageRpc, async (_input, context) => {
|
|
663
703
|
holder.remember(context.paseo);
|
|
664
704
|
try {
|
|
@@ -876,6 +916,8 @@ export function createOfficePlugin(
|
|
|
876
916
|
streets.stop();
|
|
877
917
|
await lifeReady;
|
|
878
918
|
stopLife?.();
|
|
919
|
+
stopTimeline?.();
|
|
920
|
+
await timelineSaves?.flush();
|
|
879
921
|
for (const close of listeners.values()) close();
|
|
880
922
|
await Promise.all([tenureSaves.flush(), liftSaved]);
|
|
881
923
|
machine.stop();
|
|
@@ -906,6 +948,7 @@ export default function contribute(server: PluginServerContext): () => Promise<v
|
|
|
906
948
|
log,
|
|
907
949
|
}),
|
|
908
950
|
tenureFile: createLifeFile(tenureFile(process.env, homedir()), TenureLedgerSchema),
|
|
951
|
+
timelineFile: createLifeFile(timelineFile(process.env, homedir()), TimelineFileSchema),
|
|
909
952
|
liftFile: createLifeFile(liftFile(process.env, homedir()), LiftKeptSchema),
|
|
910
953
|
dutiesFile: dutiesFileStore(dutiesFile(process.env, homedir()), { now: () => Date.now(), log }),
|
|
911
954
|
chatStore: createChatStore({
|
package/package.json
CHANGED
package/server/paths.ts
CHANGED
|
@@ -76,6 +76,11 @@ export function liftFile(env: NodeJS.ProcessEnv, home: string) {
|
|
|
76
76
|
return join(officeDir(env, home), "lift.json");
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
+
/** What the office saw each agent do, for the timeline and the week's recap. */
|
|
80
|
+
export function timelineFile(env: NodeJS.ProcessEnv, home: string) {
|
|
81
|
+
return join(officeDir(env, home), "timeline.json");
|
|
82
|
+
}
|
|
83
|
+
|
|
79
84
|
export function tenureFile(env: NodeJS.ProcessEnv, home: string) {
|
|
80
85
|
return join(officeDir(env, home), "tenure.json");
|
|
81
86
|
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { ACTIVITY_KINDS, LOBBY, type ActivityKind, type ActivityStep } from "../shared/names.js";
|
|
3
|
+
import type { OfficeAgent } from "../shared/contracts.js";
|
|
4
|
+
import { agentTitle } from "../shared/model.js";
|
|
5
|
+
import {
|
|
6
|
+
DAY_MS,
|
|
7
|
+
LANE_STEPS_MAX,
|
|
8
|
+
TIMELINE_STATES,
|
|
9
|
+
isCommand,
|
|
10
|
+
isEdit,
|
|
11
|
+
type Timeline,
|
|
12
|
+
type TimelineLane,
|
|
13
|
+
type TimelineState,
|
|
14
|
+
} from "../shared/timeline.js";
|
|
15
|
+
import type { DirectoryView } from "./hub.js";
|
|
16
|
+
|
|
17
|
+
/** How long the office remembers, and the most marks it keeps: the oldest go first. */
|
|
18
|
+
export const TIMELINE_KEEP_MS = 8 * DAY_MS;
|
|
19
|
+
export const TIMELINE_MARKS_MAX = 40_000;
|
|
20
|
+
|
|
21
|
+
const STATES = [...TIMELINE_STATES, "off"] as const;
|
|
22
|
+
type Seen = (typeof STATES)[number];
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* One thing seen of an agent at a time: its state changed (`s`, "off" when the office stopped
|
|
26
|
+
* watching it), it started a tool call (`k`), or its turns cost more (`c`, the US dollars added).
|
|
27
|
+
*/
|
|
28
|
+
const MarkSchema = z.object({
|
|
29
|
+
t: z.number(),
|
|
30
|
+
a: z.string(),
|
|
31
|
+
s: z.enum(STATES).optional(),
|
|
32
|
+
k: z.enum(ACTIVITY_KINDS).optional(),
|
|
33
|
+
c: z.number().optional(),
|
|
34
|
+
});
|
|
35
|
+
type Mark = z.output<typeof MarkSchema>;
|
|
36
|
+
|
|
37
|
+
const WhoSchema = z.object({ name: z.string(), team: z.string(), provider: z.string() });
|
|
38
|
+
|
|
39
|
+
export const TimelineFileSchema = z.object({
|
|
40
|
+
version: z.literal(1),
|
|
41
|
+
who: z.record(z.string(), WhoSchema),
|
|
42
|
+
marks: z.array(MarkSchema),
|
|
43
|
+
});
|
|
44
|
+
export type TimelineFile = z.output<typeof TimelineFileSchema>;
|
|
45
|
+
|
|
46
|
+
export interface TimelinePorts {
|
|
47
|
+
now(): number;
|
|
48
|
+
/** Called with the whole log after each change; the caller writes it when it sees fit. */
|
|
49
|
+
save(file: TimelineFile): void;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface TimelineLog {
|
|
53
|
+
/** The office as the directory reports it; null when the office stops watching. */
|
|
54
|
+
view(view: DirectoryView | null): void;
|
|
55
|
+
/** The tool call each running agent is at, as the activity reads see it. */
|
|
56
|
+
steps(doing: Readonly<Record<string, ActivityStep>>): void;
|
|
57
|
+
read(from: number, to: number): Timeline;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function seenOf(agent: OfficeAgent): Seen {
|
|
61
|
+
if (agent.status === "closed") return "off";
|
|
62
|
+
if (agent.status === "error") return "fail";
|
|
63
|
+
if (agent.permissions.length > 0) return "wait";
|
|
64
|
+
return agent.status === "idle" ? "rest" : "work";
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
interface Building {
|
|
68
|
+
lane: TimelineLane;
|
|
69
|
+
state: Seen;
|
|
70
|
+
since: number;
|
|
71
|
+
cost: number | null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* What the office saw each agent do, kept for eight days. It sees only while an office is open:
|
|
76
|
+
* the time between is a gap in every lane, not a rest. `kept` is the file of an earlier run; its
|
|
77
|
+
* agents are closed at its last mark, since nobody saw when they stopped.
|
|
78
|
+
*/
|
|
79
|
+
export function createTimelineLog(ports: TimelinePorts, kept: TimelineFile | null): TimelineLog {
|
|
80
|
+
const who: TimelineFile["who"] = { ...kept?.who };
|
|
81
|
+
let marks: Mark[] = kept?.marks.filter((mark) => mark.t >= ports.now() - TIMELINE_KEEP_MS) ?? [];
|
|
82
|
+
const states = new Map<string, Seen>();
|
|
83
|
+
const costs = new Map<string, number>();
|
|
84
|
+
const calls = new Map<string, string>();
|
|
85
|
+
for (const mark of marks) if (mark.s !== undefined) states.set(mark.a, mark.s);
|
|
86
|
+
const last = marks.at(-1)?.t ?? 0;
|
|
87
|
+
for (const [id, state] of states) if (state !== "off") marks.push({ t: last, a: id, s: "off" });
|
|
88
|
+
states.clear();
|
|
89
|
+
|
|
90
|
+
function saved(): void {
|
|
91
|
+
if (marks.length > TIMELINE_MARKS_MAX) marks = marks.slice(-TIMELINE_MARKS_MAX);
|
|
92
|
+
const listed = new Set(marks.map((mark) => mark.a));
|
|
93
|
+
for (const id of Object.keys(who)) if (!listed.has(id)) delete who[id];
|
|
94
|
+
ports.save({ version: 1, who, marks });
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function seeAgent(agent: OfficeAgent, team: string, t: number): boolean {
|
|
98
|
+
let changed = false;
|
|
99
|
+
const state = seenOf(agent);
|
|
100
|
+
if ((states.get(agent.id) ?? "off") !== state) {
|
|
101
|
+
states.set(agent.id, state);
|
|
102
|
+
marks.push({ t, a: agent.id, s: state });
|
|
103
|
+
changed = true;
|
|
104
|
+
}
|
|
105
|
+
if (state !== "off" || changed) {
|
|
106
|
+
who[agent.id] = { name: agentTitle(agent), team, provider: agent.provider };
|
|
107
|
+
}
|
|
108
|
+
const cost = agent.costUsd;
|
|
109
|
+
if (cost === undefined) return changed;
|
|
110
|
+
const before = costs.get(agent.id);
|
|
111
|
+
costs.set(agent.id, cost);
|
|
112
|
+
// The first reading is what the agent cost before the office looked: not this range's.
|
|
113
|
+
if (before === undefined || cost <= before) return changed;
|
|
114
|
+
marks.push({ t, a: agent.id, c: cost - before });
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function stopWatching(ids: Iterable<string>, t: number): boolean {
|
|
119
|
+
let changed = false;
|
|
120
|
+
for (const id of ids) {
|
|
121
|
+
if ((states.get(id) ?? "off") === "off") continue;
|
|
122
|
+
states.set(id, "off");
|
|
123
|
+
calls.delete(id);
|
|
124
|
+
marks.push({ t, a: id, s: "off" });
|
|
125
|
+
changed = true;
|
|
126
|
+
}
|
|
127
|
+
return changed;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
view(view) {
|
|
132
|
+
const t = ports.now();
|
|
133
|
+
if (view === null) {
|
|
134
|
+
if (stopWatching([...states.keys()], t)) saved();
|
|
135
|
+
costs.clear();
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
const teams = new Map(view.workspaces.map((workspace) => [workspace.id, workspace.name]));
|
|
139
|
+
let changed = false;
|
|
140
|
+
const here = new Set<string>();
|
|
141
|
+
for (const agent of view.agents) {
|
|
142
|
+
here.add(agent.id);
|
|
143
|
+
const team = (agent.workspaceId && teams.get(agent.workspaceId)) || LOBBY;
|
|
144
|
+
if (seeAgent(agent, team, t)) changed = true;
|
|
145
|
+
}
|
|
146
|
+
const gone = [...states.keys()].filter((id) => !here.has(id));
|
|
147
|
+
if (stopWatching(gone, t)) changed = true;
|
|
148
|
+
if (changed) saved();
|
|
149
|
+
},
|
|
150
|
+
steps(doing) {
|
|
151
|
+
const t = ports.now();
|
|
152
|
+
let changed = false;
|
|
153
|
+
for (const [id, step] of Object.entries(doing)) {
|
|
154
|
+
if (calls.get(id) === step.key || states.get(id) === undefined) continue;
|
|
155
|
+
calls.set(id, step.key);
|
|
156
|
+
marks.push({ t, a: id, k: step.kind });
|
|
157
|
+
changed = true;
|
|
158
|
+
}
|
|
159
|
+
if (changed) saved();
|
|
160
|
+
},
|
|
161
|
+
read(from, to) {
|
|
162
|
+
const end = Math.min(to, ports.now());
|
|
163
|
+
const building = new Map<string, Building>();
|
|
164
|
+
const close = (lane: Building, at: number) => {
|
|
165
|
+
const start = Math.max(lane.since, from);
|
|
166
|
+
const stop = Math.min(at, end);
|
|
167
|
+
if (lane.state !== "off" && stop > start) {
|
|
168
|
+
lane.lane.spans.push({ from: start, to: stop, state: lane.state as TimelineState });
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
for (const mark of marks) {
|
|
172
|
+
if (mark.t > end) break;
|
|
173
|
+
let lane = building.get(mark.a);
|
|
174
|
+
if (lane === undefined) {
|
|
175
|
+
const known = who[mark.a] ?? { name: mark.a, team: LOBBY, provider: "" };
|
|
176
|
+
lane = {
|
|
177
|
+
lane: {
|
|
178
|
+
id: mark.a,
|
|
179
|
+
...known,
|
|
180
|
+
spans: [],
|
|
181
|
+
steps: [],
|
|
182
|
+
edits: 0,
|
|
183
|
+
commands: 0,
|
|
184
|
+
costUsd: null,
|
|
185
|
+
},
|
|
186
|
+
state: "off",
|
|
187
|
+
since: 0,
|
|
188
|
+
cost: null,
|
|
189
|
+
};
|
|
190
|
+
building.set(mark.a, lane);
|
|
191
|
+
}
|
|
192
|
+
if (mark.s !== undefined) {
|
|
193
|
+
close(lane, mark.t);
|
|
194
|
+
lane.state = mark.s;
|
|
195
|
+
lane.since = mark.t;
|
|
196
|
+
}
|
|
197
|
+
if (mark.t < from) continue;
|
|
198
|
+
if (mark.k !== undefined) countStep(lane.lane, mark.t, mark.k);
|
|
199
|
+
if (mark.c !== undefined) lane.lane.costUsd = (lane.lane.costUsd ?? 0) + mark.c;
|
|
200
|
+
}
|
|
201
|
+
const lanes: TimelineLane[] = [];
|
|
202
|
+
for (const lane of building.values()) {
|
|
203
|
+
close(lane, end);
|
|
204
|
+
if (lane.lane.spans.length === 0) continue;
|
|
205
|
+
lane.lane.steps = lane.lane.steps.slice(-LANE_STEPS_MAX);
|
|
206
|
+
lanes.push(lane.lane);
|
|
207
|
+
}
|
|
208
|
+
lanes.sort((a, b) => a.team.localeCompare(b.team) || a.name.localeCompare(b.name));
|
|
209
|
+
return { from, to: end, lanes };
|
|
210
|
+
},
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function countStep(lane: TimelineLane, at: number, kind: ActivityKind): void {
|
|
215
|
+
lane.steps.push({ at, kind });
|
|
216
|
+
if (isEdit(kind)) lane.edits += 1;
|
|
217
|
+
if (isCommand(kind)) lane.commands += 1;
|
|
218
|
+
}
|
package/shared/contracts.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { AppearanceSchema } from "./appearance.js";
|
|
|
3
3
|
import { WORDS_LANGUAGES } from "./scene-language.js";
|
|
4
4
|
import { defineRpc, defineSubscription } from "@getpaseo/plugin";
|
|
5
5
|
import { PlanUsageSchema } from "./plan-usage.js";
|
|
6
|
+
import { TimelineSchema } from "./timeline.js";
|
|
6
7
|
import { z } from "zod";
|
|
7
8
|
// In their own file, so code that needs only a name does not load the schemas.
|
|
8
9
|
import { ACTIVITY_KINDS, TEST_RESULTS } from "./names.js";
|
|
@@ -797,6 +798,19 @@ export const planUsageRpc = defineRpc({
|
|
|
797
798
|
]),
|
|
798
799
|
});
|
|
799
800
|
|
|
801
|
+
/**
|
|
802
|
+
* What the office saw each agent do between two times (epoch ms), a lane per agent. It holds
|
|
803
|
+
* what was seen while an office was open, for the last eight days.
|
|
804
|
+
*/
|
|
805
|
+
export const timelineRpc = defineRpc({
|
|
806
|
+
name: "office.timeline.request",
|
|
807
|
+
input: z.object({ from: z.number(), to: z.number() }),
|
|
808
|
+
output: z.union([
|
|
809
|
+
z.object({ ok: z.literal(true), timeline: TimelineSchema }),
|
|
810
|
+
z.object({ ok: z.literal(false), problem: z.string() }),
|
|
811
|
+
]),
|
|
812
|
+
});
|
|
813
|
+
|
|
800
814
|
export const WriterModesSchema = z.object({
|
|
801
815
|
/** The provider the writer would start on now, by its label. */
|
|
802
816
|
provider: z.string(),
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { ACTIVITY_KINDS, type ActivityKind } from "./names.js";
|
|
3
|
+
|
|
4
|
+
/** What an agent is at, as the timeline draws it: at work, waiting for the user, resting, stopped on an error. */
|
|
5
|
+
export const TIMELINE_STATES = ["work", "wait", "rest", "fail"] as const;
|
|
6
|
+
export type TimelineState = (typeof TIMELINE_STATES)[number];
|
|
7
|
+
|
|
8
|
+
const SpanSchema = z.object({ from: z.number(), to: z.number(), state: z.enum(TIMELINE_STATES) });
|
|
9
|
+
export type TimelineSpan = z.output<typeof SpanSchema>;
|
|
10
|
+
|
|
11
|
+
const StepSchema = z.object({ at: z.number(), kind: z.enum(ACTIVITY_KINDS) });
|
|
12
|
+
export type TimelineStep = z.output<typeof StepSchema>;
|
|
13
|
+
|
|
14
|
+
/** The most steps of one lane that are sent: the newest. The counts cover them all. */
|
|
15
|
+
export const LANE_STEPS_MAX = 600;
|
|
16
|
+
|
|
17
|
+
export const TimelineLaneSchema = z.object({
|
|
18
|
+
id: z.string(),
|
|
19
|
+
name: z.string(),
|
|
20
|
+
team: z.string(),
|
|
21
|
+
provider: z.string(),
|
|
22
|
+
/** In order, none overlapping; a time the office was not watching has no span. */
|
|
23
|
+
spans: z.array(SpanSchema),
|
|
24
|
+
steps: z.array(StepSchema),
|
|
25
|
+
/** Tool calls in the range by what they were for: files changed, commands run. */
|
|
26
|
+
edits: z.number(),
|
|
27
|
+
commands: z.number(),
|
|
28
|
+
/** What the agent's turns cost in the range; null when its provider says no cost. */
|
|
29
|
+
costUsd: z.number().nullable(),
|
|
30
|
+
});
|
|
31
|
+
export type TimelineLane = z.output<typeof TimelineLaneSchema>;
|
|
32
|
+
|
|
33
|
+
export const TimelineSchema = z.object({
|
|
34
|
+
from: z.number(),
|
|
35
|
+
to: z.number(),
|
|
36
|
+
lanes: z.array(TimelineLaneSchema),
|
|
37
|
+
});
|
|
38
|
+
export type Timeline = z.output<typeof TimelineSchema>;
|
|
39
|
+
|
|
40
|
+
const HOUR = 3_600_000;
|
|
41
|
+
export const DAY_MS = 24 * HOUR;
|
|
42
|
+
export const TIMELINE_RANGES = [
|
|
43
|
+
{ id: "1h", label: "1 hour", ms: HOUR },
|
|
44
|
+
{ id: "6h", label: "6 hours", ms: 6 * HOUR },
|
|
45
|
+
{ id: "24h", label: "24 hours", ms: DAY_MS },
|
|
46
|
+
] as const;
|
|
47
|
+
export type TimelineRange = (typeof TIMELINE_RANGES)[number]["id"];
|
|
48
|
+
|
|
49
|
+
/** How many times faster than the clock a replay runs. */
|
|
50
|
+
export const REPLAY_SPEEDS = [30, 60, 120] as const;
|
|
51
|
+
export type ReplaySpeed = (typeof REPLAY_SPEEDS)[number];
|
|
52
|
+
|
|
53
|
+
const EDITS: ReadonlySet<ActivityKind> = new Set(["edit", "write"]);
|
|
54
|
+
const COMMANDS: ReadonlySet<ActivityKind> = new Set(["command", "test", "deploy"]);
|
|
55
|
+
export const isEdit = (kind: ActivityKind) => EDITS.has(kind);
|
|
56
|
+
export const isCommand = (kind: ActivityKind) => COMMANDS.has(kind);
|
|
57
|
+
|
|
58
|
+
/** What the agent was at then; null while the office was not watching it. */
|
|
59
|
+
export function stateAt(lane: TimelineLane, at: number): TimelineState | null {
|
|
60
|
+
return lane.spans.find((span) => span.from <= at && at < span.to)?.state ?? null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// How long a tool call stays what the agent "is at" in a replay: the reads are 5 s apart.
|
|
64
|
+
const STEP_SHOWN_MS = 60_000;
|
|
65
|
+
|
|
66
|
+
const KIND_WORDS: Readonly<Record<ActivityKind, string>> = {
|
|
67
|
+
read: "reading",
|
|
68
|
+
search: "searching",
|
|
69
|
+
skill: "reading a skill",
|
|
70
|
+
edit: "editing",
|
|
71
|
+
write: "writing a file",
|
|
72
|
+
command: "running a command",
|
|
73
|
+
web: "on the web",
|
|
74
|
+
tool: "using a tool",
|
|
75
|
+
subagent: "handing work to a sub-agent",
|
|
76
|
+
plan: "planning",
|
|
77
|
+
test: "running tests",
|
|
78
|
+
commit: "committing",
|
|
79
|
+
push: "pushing",
|
|
80
|
+
deploy: "deploying",
|
|
81
|
+
};
|
|
82
|
+
const STATE_WORDS: Readonly<Record<TimelineState, string>> = {
|
|
83
|
+
work: "at work",
|
|
84
|
+
wait: "waiting for you",
|
|
85
|
+
rest: "resting",
|
|
86
|
+
fail: "stopped on an error",
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/** What the agent was doing then, in a few words. */
|
|
90
|
+
export function doingAt(lane: TimelineLane, at: number): string {
|
|
91
|
+
const state = stateAt(lane, at);
|
|
92
|
+
if (state === null) return "not watched";
|
|
93
|
+
if (state !== "work") return STATE_WORDS[state];
|
|
94
|
+
// (no findLast: the phone's engine lacks it)
|
|
95
|
+
let kind: ActivityKind | null = null;
|
|
96
|
+
for (const step of lane.steps) {
|
|
97
|
+
if (step.at > at) break;
|
|
98
|
+
if (at - step.at < STEP_SHOWN_MS) kind = step.kind;
|
|
99
|
+
}
|
|
100
|
+
return kind === null ? STATE_WORDS.work : KIND_WORDS[kind];
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** "3 h 20 min", "45 min", "under a minute". */
|
|
104
|
+
export function durationText(ms: number): string {
|
|
105
|
+
const minutes = Math.round(ms / 60_000);
|
|
106
|
+
if (minutes < 1) return "under a minute";
|
|
107
|
+
if (minutes < 60) return `${minutes} min`;
|
|
108
|
+
const rest = minutes % 60;
|
|
109
|
+
return rest === 0 ? `${Math.floor(minutes / 60)} h` : `${Math.floor(minutes / 60)} h ${rest} min`;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export interface WeekRecap {
|
|
113
|
+
/** Time at work, of every agent together. */
|
|
114
|
+
workMs: number;
|
|
115
|
+
/** Turns started: a rest, an error or an unwatched time that turned into work. */
|
|
116
|
+
prompts: number;
|
|
117
|
+
edits: number;
|
|
118
|
+
commands: number;
|
|
119
|
+
/** Null when no provider said a cost. */
|
|
120
|
+
costUsd: number | null;
|
|
121
|
+
/** The team with the most time at work; null when nobody worked. */
|
|
122
|
+
busiestTeam: string | null;
|
|
123
|
+
/** Time at work in each of the 24 hours ending at `to`, oldest first. */
|
|
124
|
+
days: { from: number; workMs: number }[];
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function overlap(span: TimelineSpan, from: number, to: number): number {
|
|
128
|
+
return Math.max(0, Math.min(span.to, to) - Math.max(span.from, from));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function promptsOf(lane: TimelineLane): number {
|
|
132
|
+
let prompts = 0;
|
|
133
|
+
lane.spans.forEach((span, index) => {
|
|
134
|
+
if (span.state !== "work") return;
|
|
135
|
+
const before = lane.spans[index - 1];
|
|
136
|
+
// Work straight after a wait is the same turn going on: the user answered, nobody prompted.
|
|
137
|
+
const sameTurn = before !== undefined && before.state === "wait" && before.to === span.from;
|
|
138
|
+
if (!sameTurn) prompts += 1;
|
|
139
|
+
});
|
|
140
|
+
return prompts;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** A timeline's totals, with its time at work in days that end at the timeline's end. */
|
|
144
|
+
export function weekRecap(timeline: Timeline): WeekRecap {
|
|
145
|
+
const days: WeekRecap["days"] = [];
|
|
146
|
+
for (let from = timeline.to - DAY_MS; from >= timeline.from; from -= DAY_MS) {
|
|
147
|
+
days.unshift({ from, workMs: 0 });
|
|
148
|
+
}
|
|
149
|
+
const byTeam = new Map<string, number>();
|
|
150
|
+
let costUsd: number | null = null;
|
|
151
|
+
let workMs = 0;
|
|
152
|
+
for (const lane of timeline.lanes) {
|
|
153
|
+
if (lane.costUsd !== null) costUsd = (costUsd ?? 0) + lane.costUsd;
|
|
154
|
+
for (const span of lane.spans) {
|
|
155
|
+
if (span.state !== "work") continue;
|
|
156
|
+
const worked = span.to - span.from;
|
|
157
|
+
workMs += worked;
|
|
158
|
+
byTeam.set(lane.team, (byTeam.get(lane.team) ?? 0) + worked);
|
|
159
|
+
for (const day of days) day.workMs += overlap(span, day.from, day.from + DAY_MS);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
let busiestTeam: string | null = null;
|
|
163
|
+
for (const [team, worked] of byTeam) {
|
|
164
|
+
if (busiestTeam === null || worked > (byTeam.get(busiestTeam) ?? 0)) busiestTeam = team;
|
|
165
|
+
}
|
|
166
|
+
const sum = (pick: (lane: TimelineLane) => number) =>
|
|
167
|
+
timeline.lanes.reduce((total, lane) => total + pick(lane), 0);
|
|
168
|
+
return {
|
|
169
|
+
workMs,
|
|
170
|
+
prompts: sum(promptsOf),
|
|
171
|
+
edits: sum((lane) => lane.edits),
|
|
172
|
+
commands: sum((lane) => lane.commands),
|
|
173
|
+
costUsd,
|
|
174
|
+
busiestTeam,
|
|
175
|
+
days,
|
|
176
|
+
};
|
|
177
|
+
}
|