@koinzhang/paseo-plugin-activity 0.3.0 → 0.5.0
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 +42 -2
- package/README.md +7 -6
- package/client/agent-directory.test.ts +191 -0
- package/client/agent-directory.ts +150 -0
- package/client/attention-agents.test.ts +94 -0
- package/client/attention-agents.ts +67 -0
- package/client/attention-pill.tsx +166 -0
- package/client/attention-popover.tsx +161 -0
- package/client/attention-status-store.ts +47 -0
- package/client/global-surface.tsx +3 -1
- package/client/latest-user-message.test.ts +34 -0
- package/client/latest-user-message.ts +96 -0
- package/client/open-agent.ts +32 -0
- package/client/panel.tsx +2 -0
- package/client/pill-directory.ts +20 -68
- package/client/use-agent-turn-end.ts +23 -3
- package/client/use-latest-user-message.ts +15 -0
- package/client/use-workspace-agent-statuses.ts +23 -0
- package/client/workspace/agent-row.tsx +49 -22
- package/client/workspace/agents-section.tsx +90 -22
- package/client/workspace/constants.test.ts +66 -0
- package/client/workspace/constants.ts +39 -1
- package/client/workspace/filters.test.ts +59 -0
- package/client/workspace/filters.ts +9 -0
- package/client/workspace/host-archive-sync.test.ts +46 -0
- package/client/workspace/host-archive-sync.ts +15 -0
- package/client/workspace/list-host-agents.test.ts +44 -38
- package/client/workspace/list-host-agents.ts +60 -57
- package/client/workspace/panel.tsx +43 -26
- package/index.client.tsx +7 -1
- package/package.json +3 -3
- package/paseo-plugin.json +1 -1
- package/shared/explorer-agent-display.ts +1 -1
- package/shared/usage.test.ts +8 -2
- package/shared/usage.ts +7 -0
- package/client/pill-directory.test.ts +0 -92
- package/client/workspace/status-cache.test.ts +0 -62
- package/client/workspace/status-cache.ts +0 -19
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type PluginButtonIconProps,
|
|
3
|
+
type PluginButtonRegistration,
|
|
4
|
+
type PluginClientContext,
|
|
5
|
+
} from "@getpaseo/plugin/client";
|
|
6
|
+
import { Icon } from "@getpaseo/plugin/client/react-native";
|
|
7
|
+
import { useMemo } from "react";
|
|
8
|
+
import {
|
|
9
|
+
attentionPillIconName,
|
|
10
|
+
attentionPillTint,
|
|
11
|
+
filterAttentionAgents,
|
|
12
|
+
type AttentionAgentItem,
|
|
13
|
+
} from "./attention-agents.ts";
|
|
14
|
+
import { AttentionPopover } from "./attention-popover.tsx";
|
|
15
|
+
import {
|
|
16
|
+
clearAttentionStatuses,
|
|
17
|
+
getAttentionStatuses,
|
|
18
|
+
publishAttentionStatuses,
|
|
19
|
+
useAttentionStatuses,
|
|
20
|
+
} from "./attention-status-store.ts";
|
|
21
|
+
import { watchAgentDirectory } from "./agent-directory.ts";
|
|
22
|
+
import { agentStatusInfo } from "./workspace/list-host-agents.ts";
|
|
23
|
+
import type { AgentStatusInfo } from "./workspace/constants.ts";
|
|
24
|
+
|
|
25
|
+
const TITLE = "Needs attention";
|
|
26
|
+
const PILL_ID = "attention";
|
|
27
|
+
|
|
28
|
+
type PillEntry = {
|
|
29
|
+
registration?: PluginButtonRegistration;
|
|
30
|
+
workspaceId: string;
|
|
31
|
+
/** Last applied sync signature (avoid no-op updates that close surfaces). */
|
|
32
|
+
signature: string;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
function attentionSignature(
|
|
36
|
+
items: ReadonlyArray<{ agentId: string; kind: string; permissionCount: number }>,
|
|
37
|
+
): string {
|
|
38
|
+
if (items.length === 0) return "0";
|
|
39
|
+
return items.map((i) => `${i.agentId}:${i.kind}:${i.permissionCount}`).join("|");
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Entry-owned directory drives pill registrations and attention, with 15s reconciliation. */
|
|
43
|
+
export function contributeAttentionPills(client: PluginClientContext): () => void {
|
|
44
|
+
const pills = new Map<string, PillEntry>();
|
|
45
|
+
function applyPill(
|
|
46
|
+
agentId: string,
|
|
47
|
+
entry: PillEntry,
|
|
48
|
+
statuses: Record<string, AgentStatusInfo> | undefined,
|
|
49
|
+
) {
|
|
50
|
+
const pill = entry.registration;
|
|
51
|
+
if (!pill) return;
|
|
52
|
+
const items = filterAttentionAgents(statuses, agentId);
|
|
53
|
+
const tint = attentionPillTint(items);
|
|
54
|
+
const signature = `${attentionSignature(items)}:${tint ?? ""}`;
|
|
55
|
+
if (entry.signature === signature) return;
|
|
56
|
+
entry.signature = signature;
|
|
57
|
+
|
|
58
|
+
if (items.length === 0) {
|
|
59
|
+
pill.update({ visible: false, label: TITLE, disabled: false });
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// One shared pill for finished / permission / error; always the same popover list.
|
|
64
|
+
pill.update({
|
|
65
|
+
visible: true,
|
|
66
|
+
label: String(items.length),
|
|
67
|
+
title: TITLE,
|
|
68
|
+
disabled: false,
|
|
69
|
+
behavior: {
|
|
70
|
+
kind: "popover",
|
|
71
|
+
Content: AttentionPopover,
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function applyWorkspace(workspaceId: string, statuses: Record<string, AgentStatusInfo>) {
|
|
77
|
+
publishAttentionStatuses(workspaceId, statuses);
|
|
78
|
+
for (const [agentId, entry] of pills) {
|
|
79
|
+
if (entry.workspaceId !== workspaceId) continue;
|
|
80
|
+
applyPill(agentId, entry, statuses);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function createPillIcon(agentId: string) {
|
|
85
|
+
return function AttentionPillIcon({
|
|
86
|
+
size,
|
|
87
|
+
color,
|
|
88
|
+
theme,
|
|
89
|
+
workspaceId,
|
|
90
|
+
}: PluginButtonIconProps) {
|
|
91
|
+
// Same push-driven store as contribute visibility — not the 15s Explorer query.
|
|
92
|
+
const statuses = useAttentionStatuses(workspaceId);
|
|
93
|
+
const items = useMemo(
|
|
94
|
+
() => filterAttentionAgents(statuses, agentId),
|
|
95
|
+
[statuses, agentId],
|
|
96
|
+
);
|
|
97
|
+
const tint = attentionPillTint(items);
|
|
98
|
+
const iconColor =
|
|
99
|
+
tint === "permission"
|
|
100
|
+
? theme.colors.statusWarning
|
|
101
|
+
: tint === "error"
|
|
102
|
+
? theme.colors.statusDanger
|
|
103
|
+
: tint === "finished"
|
|
104
|
+
? theme.colors.statusSuccess
|
|
105
|
+
: color;
|
|
106
|
+
return <Icon name={attentionPillIconName(tint)} size={size} color={iconColor} />;
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function addPill(agentId: string, workspaceId: string) {
|
|
111
|
+
if (pills.has(agentId)) return;
|
|
112
|
+
const entry: PillEntry = { workspaceId, signature: "" };
|
|
113
|
+
entry.registration = client.addComposerPill({
|
|
114
|
+
id: PILL_ID,
|
|
115
|
+
workspaceId,
|
|
116
|
+
agentId,
|
|
117
|
+
button: {
|
|
118
|
+
title: TITLE,
|
|
119
|
+
label: TITLE,
|
|
120
|
+
visible: false,
|
|
121
|
+
icon: createPillIcon(agentId),
|
|
122
|
+
behavior: {
|
|
123
|
+
kind: "action",
|
|
124
|
+
onPress() {
|
|
125
|
+
/* replaced on first successful sync with peers */
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
pills.set(agentId, entry);
|
|
131
|
+
const cached = getAttentionStatuses(workspaceId);
|
|
132
|
+
if (cached) applyPill(agentId, entry, cached);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function removePill(agentId: string) {
|
|
136
|
+
pills.get(agentId)?.registration?.remove();
|
|
137
|
+
pills.delete(agentId);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const stopDirectory = watchAgentDirectory(client.paseo.agents, (snapshot) => {
|
|
141
|
+
const workspaces = new Map<string, Record<string, AgentStatusInfo>>();
|
|
142
|
+
for (const [id, entry] of pills) {
|
|
143
|
+
const agent = snapshot.get(id);
|
|
144
|
+
if (!agent || agent.archivedAt || agent.workspaceId !== entry.workspaceId) removePill(id);
|
|
145
|
+
workspaces.set(entry.workspaceId, {});
|
|
146
|
+
}
|
|
147
|
+
for (const agent of snapshot.values()) {
|
|
148
|
+
if (agent.archivedAt || !agent.workspaceId) continue;
|
|
149
|
+
const statuses = workspaces.get(agent.workspaceId) ?? {};
|
|
150
|
+
statuses[agent.id] = agentStatusInfo(agent);
|
|
151
|
+
workspaces.set(agent.workspaceId, statuses);
|
|
152
|
+
addPill(agent.id, agent.workspaceId);
|
|
153
|
+
}
|
|
154
|
+
for (const [workspaceId, statuses] of workspaces) applyWorkspace(workspaceId, statuses);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
return () => {
|
|
158
|
+
stopDirectory();
|
|
159
|
+
pills.forEach((entry) => entry.registration?.remove());
|
|
160
|
+
pills.clear();
|
|
161
|
+
clearAttentionStatuses();
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Exported for tests / popover typing only. */
|
|
166
|
+
export type { AttentionAgentItem };
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type PluginButtonContentProps,
|
|
3
|
+
useAgent,
|
|
4
|
+
} from "@getpaseo/plugin/client";
|
|
5
|
+
import { useEffect, useMemo, type ReactNode } from "react";
|
|
6
|
+
import { Text, View, type TextStyle, type ViewStyle } from "react-native";
|
|
7
|
+
import {
|
|
8
|
+
filterAttentionAgents,
|
|
9
|
+
type AttentionAgentItem,
|
|
10
|
+
} from "./attention-agents.ts";
|
|
11
|
+
import { openAttentionAgent } from "./open-agent.ts";
|
|
12
|
+
import { useLatestUserMessagePreview } from "./use-latest-user-message.ts";
|
|
13
|
+
import { useAttentionStatuses } from "./attention-status-store.ts";
|
|
14
|
+
import { AgentRow, type AgentRowStyles } from "./workspace/agent-row.tsx";
|
|
15
|
+
|
|
16
|
+
function AttentionRow({
|
|
17
|
+
item,
|
|
18
|
+
theme,
|
|
19
|
+
styles,
|
|
20
|
+
onOpen,
|
|
21
|
+
}: {
|
|
22
|
+
item: AttentionAgentItem;
|
|
23
|
+
theme: PluginButtonContentProps["theme"];
|
|
24
|
+
styles: AgentRowStyles;
|
|
25
|
+
onOpen: () => void;
|
|
26
|
+
}): ReactNode {
|
|
27
|
+
const title = useAgent(item.agentId, (agent) => agent.title);
|
|
28
|
+
const preview = useLatestUserMessagePreview(item.agentId);
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
if (preview.isError) {
|
|
31
|
+
console.warn("[activity] latest user message preview failed", item.agentId, preview.error);
|
|
32
|
+
}
|
|
33
|
+
}, [preview.isError, preview.error, item.agentId]);
|
|
34
|
+
|
|
35
|
+
const meta =
|
|
36
|
+
preview.data ??
|
|
37
|
+
(preview.isLoading ? "…" : null);
|
|
38
|
+
|
|
39
|
+
return (
|
|
40
|
+
<AgentRow
|
|
41
|
+
label={title ?? item.agentId}
|
|
42
|
+
archived={false}
|
|
43
|
+
canOpen
|
|
44
|
+
meta={meta}
|
|
45
|
+
permissionCount={item.permissionCount}
|
|
46
|
+
attentionKind={item.kind}
|
|
47
|
+
running={false}
|
|
48
|
+
busy={false}
|
|
49
|
+
actionDisabled
|
|
50
|
+
theme={theme}
|
|
51
|
+
styles={styles}
|
|
52
|
+
onOpen={onOpen}
|
|
53
|
+
/>
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Popover body: other agents in this workspace that need attention. */
|
|
58
|
+
export function AttentionPopover(props: PluginButtonContentProps) {
|
|
59
|
+
if (props.context !== "agent") {
|
|
60
|
+
return (
|
|
61
|
+
<Text style={{ color: props.theme.colors.foregroundMuted }}>
|
|
62
|
+
Attention is only available for an agent
|
|
63
|
+
</Text>
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
return <AttentionPopoverAgent {...props} context="agent" />;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function AttentionPopoverAgent(
|
|
70
|
+
props: PluginButtonContentProps & { context: "agent" },
|
|
71
|
+
) {
|
|
72
|
+
const { theme, workspaceId, agentId, close } = props;
|
|
73
|
+
const statuses = useAttentionStatuses(workspaceId);
|
|
74
|
+
const items = useMemo(
|
|
75
|
+
() => filterAttentionAgents(statuses, agentId),
|
|
76
|
+
[statuses, agentId],
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
const styles = useMemo(() => {
|
|
80
|
+
const row: ViewStyle = {
|
|
81
|
+
flexDirection: "row",
|
|
82
|
+
alignItems: "center",
|
|
83
|
+
gap: 10,
|
|
84
|
+
paddingVertical: 6,
|
|
85
|
+
};
|
|
86
|
+
const title: TextStyle = {
|
|
87
|
+
color: theme.colors.foreground,
|
|
88
|
+
fontSize: 14,
|
|
89
|
+
fontWeight: "500",
|
|
90
|
+
};
|
|
91
|
+
return {
|
|
92
|
+
agentListRow: row,
|
|
93
|
+
agentIconWrap: {
|
|
94
|
+
width: 18,
|
|
95
|
+
height: 18,
|
|
96
|
+
alignItems: "center",
|
|
97
|
+
justifyContent: "center",
|
|
98
|
+
} satisfies ViewStyle,
|
|
99
|
+
runningBadge: { display: "none" } as ViewStyle,
|
|
100
|
+
subAgentBadge: { display: "none" } as ViewStyle,
|
|
101
|
+
subAgentBadgeText: { display: "none" } as TextStyle,
|
|
102
|
+
listMain: { flex: 1, minWidth: 0, gap: 3 } satisfies ViewStyle,
|
|
103
|
+
listLink: title,
|
|
104
|
+
listTitle: title,
|
|
105
|
+
listMeta: {
|
|
106
|
+
color: theme.colors.foregroundMuted,
|
|
107
|
+
fontSize: 12,
|
|
108
|
+
} satisfies TextStyle,
|
|
109
|
+
titleAction: { width: 0, height: 0 } satisfies ViewStyle,
|
|
110
|
+
permissionBadge: {
|
|
111
|
+
flexDirection: "row",
|
|
112
|
+
alignItems: "center",
|
|
113
|
+
gap: 3,
|
|
114
|
+
height: 18,
|
|
115
|
+
paddingHorizontal: 5,
|
|
116
|
+
borderRadius: 9,
|
|
117
|
+
borderWidth: 1,
|
|
118
|
+
borderColor: theme.colors.border,
|
|
119
|
+
backgroundColor: theme.colors.surface1,
|
|
120
|
+
flexShrink: 0,
|
|
121
|
+
} satisfies ViewStyle,
|
|
122
|
+
permissionBadgeText: {
|
|
123
|
+
color: theme.colors.statusWarning,
|
|
124
|
+
fontSize: 11,
|
|
125
|
+
fontWeight: "600",
|
|
126
|
+
fontVariant: ["tabular-nums"],
|
|
127
|
+
} satisfies TextStyle,
|
|
128
|
+
} satisfies AgentRowStyles;
|
|
129
|
+
}, [theme]);
|
|
130
|
+
|
|
131
|
+
if (!statuses) {
|
|
132
|
+
return (
|
|
133
|
+
<Text style={{ color: theme.colors.foregroundMuted }}>Loading…</Text>
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (items.length === 0) {
|
|
138
|
+
return (
|
|
139
|
+
<Text style={{ color: theme.colors.foregroundMuted }}>
|
|
140
|
+
No other agents need attention
|
|
141
|
+
</Text>
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return (
|
|
146
|
+
<View style={{ gap: 2, minWidth: 240 }}>
|
|
147
|
+
{items.map((item) => (
|
|
148
|
+
<AttentionRow
|
|
149
|
+
key={item.agentId}
|
|
150
|
+
item={item}
|
|
151
|
+
theme={theme}
|
|
152
|
+
styles={styles}
|
|
153
|
+
onOpen={() => {
|
|
154
|
+
openAttentionAgent(item.agentId);
|
|
155
|
+
close();
|
|
156
|
+
}}
|
|
157
|
+
/>
|
|
158
|
+
))}
|
|
159
|
+
</View>
|
|
160
|
+
);
|
|
161
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { useSyncExternalStore } from "react";
|
|
2
|
+
import type { AgentStatusInfo } from "./workspace/constants.ts";
|
|
3
|
+
|
|
4
|
+
const byWorkspace = new Map<string, Record<string, AgentStatusInfo>>();
|
|
5
|
+
const listeners = new Set<() => void>();
|
|
6
|
+
|
|
7
|
+
function emit(): void {
|
|
8
|
+
for (const listener of listeners) listener();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function subscribe(listener: () => void): () => void {
|
|
12
|
+
listeners.add(listener);
|
|
13
|
+
return () => {
|
|
14
|
+
listeners.delete(listener);
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Publish push/poll status for a workspace; icon + popover read this immediately. */
|
|
19
|
+
export function publishAttentionStatuses(
|
|
20
|
+
workspaceId: string,
|
|
21
|
+
statuses: Record<string, AgentStatusInfo>,
|
|
22
|
+
): void {
|
|
23
|
+
byWorkspace.set(workspaceId, statuses);
|
|
24
|
+
emit();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function getAttentionStatuses(
|
|
28
|
+
workspaceId: string,
|
|
29
|
+
): Record<string, AgentStatusInfo> | undefined {
|
|
30
|
+
return byWorkspace.get(workspaceId);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function clearAttentionStatuses(): void {
|
|
34
|
+
byWorkspace.clear();
|
|
35
|
+
emit();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Re-renders when contribute-layer status for this workspace is republished. */
|
|
39
|
+
export function useAttentionStatuses(
|
|
40
|
+
workspaceId: string,
|
|
41
|
+
): Record<string, AgentStatusInfo> | undefined {
|
|
42
|
+
return useSyncExternalStore(
|
|
43
|
+
subscribe,
|
|
44
|
+
() => byWorkspace.get(workspaceId),
|
|
45
|
+
() => undefined,
|
|
46
|
+
);
|
|
47
|
+
}
|
|
@@ -30,6 +30,7 @@ import { selectProviderOptions } from "./provider-filter.ts";
|
|
|
30
30
|
import { useAppLanguage } from "./use-app-language.ts";
|
|
31
31
|
import { buildActivityInsights, ACTIVITY_LIST_LIMIT } from "../shared/insights.ts";
|
|
32
32
|
import { rangeFrom, RANGE_OPTIONS, type RangeId } from "./range.ts";
|
|
33
|
+
import { useRegisterOpenAgent } from "./open-agent.ts";
|
|
33
34
|
|
|
34
35
|
/** Max rows for Activity insights and Most used skills / MCP. */
|
|
35
36
|
const LIST_LIMIT = ACTIVITY_LIST_LIMIT;
|
|
@@ -211,7 +212,8 @@ function RankList({
|
|
|
211
212
|
);
|
|
212
213
|
}
|
|
213
214
|
|
|
214
|
-
export function GlobalUsageSurface({ theme, layout }: PluginSurfaceProps) {
|
|
215
|
+
export function GlobalUsageSurface({ theme, layout, navigation }: PluginSurfaceProps) {
|
|
216
|
+
useRegisterOpenAgent(navigation?.openAgent);
|
|
215
217
|
const [range, setRange] = useState<RangeId>("all");
|
|
216
218
|
const [providerFilter, setProviderFilter] = useState<string>("all");
|
|
217
219
|
const [heatmapMode, setHeatmapMode] = useState<HeatmapMode>("daily");
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { describe, it } from "node:test";
|
|
3
|
+
import {
|
|
4
|
+
formatMessagePreview,
|
|
5
|
+
pickLatestUserMessageText,
|
|
6
|
+
} from "./latest-user-message.ts";
|
|
7
|
+
|
|
8
|
+
describe("pickLatestUserMessageText", () => {
|
|
9
|
+
it("picks the highest-seq non-empty user_message", () => {
|
|
10
|
+
const text = pickLatestUserMessageText([
|
|
11
|
+
{ item: { type: "user_message", text: "first" }, seqStart: 1, timestamp: "a" },
|
|
12
|
+
{ item: { type: "assistant_message", text: "reply" }, seqStart: 2, timestamp: "b" },
|
|
13
|
+
{ item: { type: "user_message", text: " " }, seqStart: 3, timestamp: "c" },
|
|
14
|
+
{ item: { type: "user_message", text: "latest" }, seqStart: 4, timestamp: "d" },
|
|
15
|
+
]);
|
|
16
|
+
assert.equal(text, "latest");
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("when seq missing, prefers later list entry", () => {
|
|
20
|
+
const text = pickLatestUserMessageText([
|
|
21
|
+
{ item: { type: "user_message", text: "older" } },
|
|
22
|
+
{ item: { type: "assistant_message", text: "reply" } },
|
|
23
|
+
{ item: { type: "user_message", text: "newer" } },
|
|
24
|
+
]);
|
|
25
|
+
assert.equal(text, "newer");
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe("formatMessagePreview", () => {
|
|
30
|
+
it("collapses whitespace and truncates", () => {
|
|
31
|
+
assert.equal(formatMessagePreview("hello\n world"), "hello world");
|
|
32
|
+
assert.equal(formatMessagePreview("abcdefghij", 6), "abcde…");
|
|
33
|
+
});
|
|
34
|
+
});
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Structural timeline entry shape for user-message previews.
|
|
3
|
+
*
|
|
4
|
+
* Do not import `@getpaseo/protocol/*` from client/: Paseo compiles plugins
|
|
5
|
+
* without installing that package, and type-only imports still fail the
|
|
6
|
+
* `paseo-plugin-client-runtime-boundary` build (see server/host-types.ts).
|
|
7
|
+
*/
|
|
8
|
+
export type TimelineTailEntry = {
|
|
9
|
+
item: {
|
|
10
|
+
type: string;
|
|
11
|
+
text?: string | null;
|
|
12
|
+
};
|
|
13
|
+
seqStart?: number;
|
|
14
|
+
timestamp?: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type TimelineApi = {
|
|
18
|
+
agents: {
|
|
19
|
+
ref: (agentId: string) => {
|
|
20
|
+
timeline: {
|
|
21
|
+
refetch: (options: {
|
|
22
|
+
projection?: "canonical" | "projected";
|
|
23
|
+
direction?: "tail" | "before" | "after";
|
|
24
|
+
limit?: number;
|
|
25
|
+
}) => Promise<{
|
|
26
|
+
error?: string | null;
|
|
27
|
+
entries?: ReadonlyArray<TimelineTailEntry> | null;
|
|
28
|
+
}>;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/** Newest non-empty user_message; prefers higher seq/ts, else later list index (tail order). */
|
|
35
|
+
export function pickLatestUserMessageText(
|
|
36
|
+
entries: ReadonlyArray<TimelineTailEntry>,
|
|
37
|
+
): string | null {
|
|
38
|
+
let best: { text: string; seq: number; ts: string; index: number } | null = null;
|
|
39
|
+
for (let index = 0; index < entries.length; index++) {
|
|
40
|
+
const entry = entries[index]!;
|
|
41
|
+
if (entry.item.type !== "user_message") continue;
|
|
42
|
+
const text = entry.item.text?.trim();
|
|
43
|
+
if (!text) continue;
|
|
44
|
+
const seq = entry.seqStart ?? -1;
|
|
45
|
+
const ts = entry.timestamp ?? "";
|
|
46
|
+
if (
|
|
47
|
+
!best ||
|
|
48
|
+
seq > best.seq ||
|
|
49
|
+
(seq === best.seq && ts > best.ts) ||
|
|
50
|
+
(seq === best.seq && ts === best.ts && index > best.index)
|
|
51
|
+
) {
|
|
52
|
+
best = { text, seq, ts, index };
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return best?.text ?? null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Single-line preview for list meta; collapses whitespace. */
|
|
59
|
+
export function formatMessagePreview(text: string, maxChars = 72): string {
|
|
60
|
+
const oneLine = text.replace(/\s+/g, " ").trim();
|
|
61
|
+
if (oneLine.length <= maxChars) return oneLine;
|
|
62
|
+
return `${oneLine.slice(0, Math.max(1, maxChars - 1)).trimEnd()}…`;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function refetchPreview(
|
|
66
|
+
paseo: TimelineApi,
|
|
67
|
+
agentId: string,
|
|
68
|
+
projection: "projected" | "canonical",
|
|
69
|
+
): Promise<string | null> {
|
|
70
|
+
const page = await paseo.agents.ref(agentId).timeline.refetch({
|
|
71
|
+
projection,
|
|
72
|
+
direction: "tail",
|
|
73
|
+
limit: 80,
|
|
74
|
+
});
|
|
75
|
+
if (page.error) throw new Error(page.error);
|
|
76
|
+
const text = pickLatestUserMessageText(page.entries ?? []);
|
|
77
|
+
return text ? formatMessagePreview(text) : null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Read the newest user prompt from the host timeline (not local SQLite —
|
|
82
|
+
* message bodies are intentionally not stored in usage.db).
|
|
83
|
+
* Prefer projected (UI text), then canonical.
|
|
84
|
+
*/
|
|
85
|
+
export async function fetchLatestUserMessagePreview(
|
|
86
|
+
paseo: TimelineApi,
|
|
87
|
+
agentId: string,
|
|
88
|
+
): Promise<string | null> {
|
|
89
|
+
try {
|
|
90
|
+
const projected = await refetchPreview(paseo, agentId, "projected");
|
|
91
|
+
if (projected) return projected;
|
|
92
|
+
} catch (error) {
|
|
93
|
+
console.warn("[activity] projected timeline preview failed", agentId, error);
|
|
94
|
+
}
|
|
95
|
+
return refetchPreview(paseo, agentId, "canonical");
|
|
96
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
|
|
3
|
+
type OpenAgent = (input: { agentId: string }) => void;
|
|
4
|
+
|
|
5
|
+
/** Last-registered wins; cleanup pops this registration only. */
|
|
6
|
+
const stack: OpenAgent[] = [];
|
|
7
|
+
|
|
8
|
+
export function setOpenAgent(open: OpenAgent | null | undefined): () => void {
|
|
9
|
+
if (!open) return () => {};
|
|
10
|
+
stack.push(open);
|
|
11
|
+
return () => {
|
|
12
|
+
const index = stack.lastIndexOf(open);
|
|
13
|
+
if (index >= 0) stack.splice(index, 1);
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Register host `navigation.openAgent` while a navigable surface/panel is mounted. */
|
|
18
|
+
export function useRegisterOpenAgent(open: OpenAgent | null | undefined): void {
|
|
19
|
+
useEffect(() => setOpenAgent(open), [open]);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function tryOpenAgent(agentId: string): boolean {
|
|
23
|
+
const open = stack[stack.length - 1];
|
|
24
|
+
if (!open) return false;
|
|
25
|
+
open({ agentId });
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Focus another agent conversation via the registered host navigation bridge. */
|
|
30
|
+
export function openAttentionAgent(agentId: string): void {
|
|
31
|
+
tryOpenAgent(agentId);
|
|
32
|
+
}
|
package/client/panel.tsx
CHANGED
|
@@ -2,6 +2,7 @@ import { type PluginAgentPanelProps, useAgent, useRpc } from "@getpaseo/plugin/c
|
|
|
2
2
|
import { Icon } from "@getpaseo/plugin/client/react-native";
|
|
3
3
|
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
|
4
4
|
import { useEffect, useMemo, useState, type ReactNode } from "react";
|
|
5
|
+
import { useRegisterOpenAgent } from "./open-agent.ts";
|
|
5
6
|
import {
|
|
6
7
|
ActivityIndicator,
|
|
7
8
|
Platform,
|
|
@@ -53,6 +54,7 @@ export function UsagePanel({ theme, layout, agentId, navigation }: PluginAgentPa
|
|
|
53
54
|
const padding = layout.compact ? 16 : 28;
|
|
54
55
|
const agentTitle = useAgent(agentId, (agent) => agent.title);
|
|
55
56
|
const openAgent = navigation?.openAgent;
|
|
57
|
+
useRegisterOpenAgent(openAgent);
|
|
56
58
|
const conversationLabel = agentTitle ?? agentId;
|
|
57
59
|
|
|
58
60
|
const skillsByNameRpc = useRpc(usageSkillsByNameRpc);
|
package/client/pill-directory.ts
CHANGED
|
@@ -1,79 +1,31 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import { listAllAgentPages } from "../shared/list-agent-pages.ts";
|
|
1
|
+
import { watchAgentDirectory, type AgentsApi, type DirectoryAgent } from "./agent-directory.ts";
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
type Update = Parameters<Parameters<AgentsApi["subscribe"]>[0]>[0];
|
|
6
|
-
type Agent = Extract<Update, { kind: "upsert" }>["agent"];
|
|
7
|
-
|
|
8
|
-
/** 0.8 has one shared directory slot. Plain paged reads own our fallback, never that slot. */
|
|
3
|
+
/** Pill registrations follow the owned directory; archived agents have no composer pill. */
|
|
9
4
|
export function watchPillDirectory(
|
|
10
5
|
agents: AgentsApi,
|
|
11
|
-
onAgent: (agent:
|
|
6
|
+
onAgent: (agent: DirectoryAgent) => void,
|
|
12
7
|
onRemove: (agentId: string) => void,
|
|
13
8
|
intervalMs = 15_000,
|
|
14
|
-
onPolled?: (agent:
|
|
9
|
+
onPolled?: (agent: DirectoryAgent) => void,
|
|
15
10
|
): () => void {
|
|
16
|
-
let disposed = false;
|
|
17
|
-
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
18
|
-
let pending: Update[] | null = null;
|
|
19
|
-
let known = new Set<string>();
|
|
20
11
|
const versions = new Map<string, string>();
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
agent
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
function remove(id: string) {
|
|
29
|
-
versions.delete(id);
|
|
30
|
-
onRemove(id);
|
|
31
|
-
}
|
|
32
|
-
function apply(update: Update) {
|
|
33
|
-
if (update.kind === "remove") {
|
|
34
|
-
known.delete(update.agentId);
|
|
35
|
-
remove(update.agentId);
|
|
36
|
-
} else {
|
|
37
|
-
known.add(update.agent.id);
|
|
38
|
-
publish(update.agent);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
// Opportunistic acceleration only; correctness does not depend on host observation.
|
|
42
|
-
const unsubscribe = agents.subscribe((update) => {
|
|
43
|
-
if (disposed) return;
|
|
44
|
-
pending?.push(update);
|
|
45
|
-
apply(update);
|
|
46
|
-
});
|
|
47
|
-
async function sync() {
|
|
48
|
-
pending = [];
|
|
49
|
-
try {
|
|
50
|
-
const entries = await listAllAgentPages((cursor) => agents.list({
|
|
51
|
-
page: { limit: 200, ...(cursor ? { cursor } : {}) },
|
|
52
|
-
}));
|
|
53
|
-
if (disposed) return;
|
|
54
|
-
const snapshot = new Map(entries.map(({ agent }) => [agent.id, agent]));
|
|
55
|
-
// Pushes received during pagination are newer than its snapshot.
|
|
56
|
-
for (const update of pending) {
|
|
57
|
-
if (update.kind === "remove") snapshot.delete(update.agentId);
|
|
58
|
-
else snapshot.set(update.agent.id, update.agent);
|
|
12
|
+
return watchAgentDirectory(agents, (snapshot, update) => {
|
|
13
|
+
for (const id of versions.keys()) {
|
|
14
|
+
const agent = snapshot.get(id);
|
|
15
|
+
if (!agent || agent.archivedAt) {
|
|
16
|
+
versions.delete(id);
|
|
17
|
+
onRemove(id);
|
|
59
18
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
19
|
+
}
|
|
20
|
+
for (const agent of snapshot.values()) {
|
|
21
|
+
if (agent.archivedAt) continue;
|
|
22
|
+
const version = JSON.stringify([agent.workspaceId, agent.status, agent.updatedAt,
|
|
23
|
+
agent.lastUserMessageAt, agent.activeTurn?.turnId]);
|
|
24
|
+
if (versions.get(agent.id) !== version) {
|
|
25
|
+
versions.set(agent.id, version);
|
|
26
|
+
onAgent(agent);
|
|
65
27
|
}
|
|
66
|
-
|
|
67
|
-
if (!disposed) console.error("[activity] could not sync composer pills", error);
|
|
68
|
-
} finally {
|
|
69
|
-
pending = null;
|
|
70
|
-
if (!disposed) timer = setTimeout(() => void sync(), intervalMs);
|
|
28
|
+
if (!update) onPolled?.(agent);
|
|
71
29
|
}
|
|
72
|
-
}
|
|
73
|
-
void sync();
|
|
74
|
-
return () => {
|
|
75
|
-
disposed = true;
|
|
76
|
-
unsubscribe();
|
|
77
|
-
clearTimeout(timer);
|
|
78
|
-
};
|
|
30
|
+
}, intervalMs);
|
|
79
31
|
}
|