paseo-office 0.2.5 → 0.2.7
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 +10 -1
- package/client/alert-card.tsx +195 -0
- package/client/host-switches.tsx +50 -0
- package/client/office-surface.tsx +79 -10
- package/client/timeline-open.ts +3 -0
- package/client/use-alerts.ts +71 -0
- package/client/vault-card.tsx +316 -0
- package/docs/office.md +49 -0
- package/index.server.ts +92 -2
- package/package.json +1 -1
- package/server/live-ports.ts +7 -0
- package/server/paths.ts +5 -0
- package/server/vault.ts +168 -0
- package/server/watchers.ts +308 -0
- package/shared/alerts.ts +48 -0
- package/shared/contracts.ts +40 -0
- package/shared/settings.ts +7 -1
- package/shared/vault.ts +127 -0
package/server/vault.ts
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { OfficeAgent } from "../shared/contracts.js";
|
|
3
|
+
import { agentTitle } from "../shared/model.js";
|
|
4
|
+
import { LOBBY } from "../shared/names.js";
|
|
5
|
+
import {
|
|
6
|
+
VAULT_HITS_MAX,
|
|
7
|
+
VAULT_MESSAGES_MAX,
|
|
8
|
+
VAULT_TALKS_MAX,
|
|
9
|
+
VAULT_TEXT_MAX,
|
|
10
|
+
VaultMessageSchema,
|
|
11
|
+
promptReport,
|
|
12
|
+
searchWords,
|
|
13
|
+
snippetOf,
|
|
14
|
+
type PromptReport,
|
|
15
|
+
type VaultHit,
|
|
16
|
+
type VaultTalk,
|
|
17
|
+
} from "../shared/vault.js";
|
|
18
|
+
import { errorMessage } from "./errors.js";
|
|
19
|
+
import type { DirectoryView } from "./hub.js";
|
|
20
|
+
|
|
21
|
+
/** How many of an agent's newest timeline items are read after each of its turns. */
|
|
22
|
+
export const VAULT_TAIL_LIMIT = 80;
|
|
23
|
+
|
|
24
|
+
const TalkSchema = z.object({
|
|
25
|
+
name: z.string(),
|
|
26
|
+
team: z.string(),
|
|
27
|
+
/** The last timeline sequence number read: what comes after is new. */
|
|
28
|
+
seq: z.number(),
|
|
29
|
+
messages: z.array(VaultMessageSchema),
|
|
30
|
+
});
|
|
31
|
+
type Talk = z.output<typeof TalkSchema>;
|
|
32
|
+
|
|
33
|
+
export const VaultFileSchema = z.object({
|
|
34
|
+
version: z.literal(1),
|
|
35
|
+
talks: z.record(z.string(), TalkSchema),
|
|
36
|
+
});
|
|
37
|
+
export type VaultFile = z.output<typeof VaultFileSchema>;
|
|
38
|
+
|
|
39
|
+
/** A timeline entry as the vault reads it: when, where in the timeline, and the item. */
|
|
40
|
+
export interface VaultEntry {
|
|
41
|
+
at: number;
|
|
42
|
+
seq: number;
|
|
43
|
+
item: { type: string; text?: unknown };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface VaultPorts {
|
|
47
|
+
/** The newest entries of an agent's timeline, oldest first. */
|
|
48
|
+
tail(agentId: string): Promise<readonly VaultEntry[]>;
|
|
49
|
+
/** Whether the owner has turned the vault on; off, nothing is read or kept. */
|
|
50
|
+
on(): boolean;
|
|
51
|
+
save(file: VaultFile): void;
|
|
52
|
+
log(message: string): void;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface Vault {
|
|
56
|
+
/** The office as the directory reports it; an agent seen for the first time, or whose turn ended, is read. */
|
|
57
|
+
view(view: DirectoryView | null): void;
|
|
58
|
+
search(query: string): VaultHit[];
|
|
59
|
+
talk(agentId: string): VaultTalk | null;
|
|
60
|
+
/** The prompts sent since `from` against the prompt helper's rules. */
|
|
61
|
+
report(from: number): PromptReport;
|
|
62
|
+
/** Resolves when every read asked for so far has ended. */
|
|
63
|
+
settled(): Promise<void>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function cut(text: string): string {
|
|
67
|
+
return text.length > VAULT_TEXT_MAX ? `${text.slice(0, VAULT_TEXT_MAX - 1)}…` : text;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function lastAt(talk: Talk): number {
|
|
71
|
+
return talk.messages.at(-1)?.at ?? 0;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* What was said in each conversation, kept on the host after the agent is gone: the prompts and
|
|
76
|
+
* the replies, not the tool calls. It reads an agent's timeline when it first sees the agent and
|
|
77
|
+
* after each of its turns, so it holds what was said while an office was open.
|
|
78
|
+
*/
|
|
79
|
+
export function createVault(ports: VaultPorts, kept: VaultFile | null): Vault {
|
|
80
|
+
const talks = new Map<string, Talk>(Object.entries(kept?.talks ?? {}));
|
|
81
|
+
const resting = new Map<string, boolean>();
|
|
82
|
+
let chain: Promise<void> = Promise.resolve();
|
|
83
|
+
|
|
84
|
+
function saved(): void {
|
|
85
|
+
if (talks.size > VAULT_TALKS_MAX) {
|
|
86
|
+
const oldest = [...talks].sort((a, b) => lastAt(a[1]) - lastAt(b[1]));
|
|
87
|
+
for (const [id] of oldest.slice(0, talks.size - VAULT_TALKS_MAX)) talks.delete(id);
|
|
88
|
+
}
|
|
89
|
+
ports.save({ version: 1, talks: Object.fromEntries(talks) });
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function read(agent: OfficeAgent, team: string): Promise<void> {
|
|
93
|
+
const entries = await ports.tail(agent.id);
|
|
94
|
+
const talk = talks.get(agent.id) ?? { name: "", team, seq: -1, messages: [] };
|
|
95
|
+
let added = false;
|
|
96
|
+
for (const entry of entries) {
|
|
97
|
+
const { type, text } = entry.item;
|
|
98
|
+
if (entry.seq <= talk.seq) continue;
|
|
99
|
+
talk.seq = entry.seq;
|
|
100
|
+
if ((type !== "user_message" && type !== "assistant_message") || typeof text !== "string") {
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (text.trim() === "") continue;
|
|
104
|
+
const role = type === "user_message" ? "user" : "assistant";
|
|
105
|
+
talk.messages.push({ at: entry.at, role, text: cut(text) });
|
|
106
|
+
added = true;
|
|
107
|
+
}
|
|
108
|
+
if (!added) return;
|
|
109
|
+
talk.name = agentTitle(agent);
|
|
110
|
+
talk.team = team;
|
|
111
|
+
if (talk.messages.length > VAULT_MESSAGES_MAX) {
|
|
112
|
+
talk.messages = talk.messages.slice(-VAULT_MESSAGES_MAX);
|
|
113
|
+
}
|
|
114
|
+
talks.set(agent.id, talk);
|
|
115
|
+
saved();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return {
|
|
119
|
+
view(view) {
|
|
120
|
+
if (view === null || !ports.on()) {
|
|
121
|
+
// Seen again later, each agent is read again: what it said meanwhile is then new.
|
|
122
|
+
resting.clear();
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
const teams = new Map(view.workspaces.map((workspace) => [workspace.id, workspace.name]));
|
|
126
|
+
for (const agent of view.agents) {
|
|
127
|
+
const rests = agent.status === "idle" || agent.status === "error";
|
|
128
|
+
const before = resting.get(agent.id);
|
|
129
|
+
resting.set(agent.id, rests);
|
|
130
|
+
if (before !== undefined && !(rests && !before)) continue;
|
|
131
|
+
const team = (agent.workspaceId && teams.get(agent.workspaceId)) || LOBBY;
|
|
132
|
+
chain = chain
|
|
133
|
+
.then(() => read(agent, team))
|
|
134
|
+
.catch((error: unknown) =>
|
|
135
|
+
ports.log(`[office] history of ${agent.id}: ${errorMessage(error)}`),
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
},
|
|
139
|
+
search(query) {
|
|
140
|
+
const words = searchWords(query);
|
|
141
|
+
const hits: VaultHit[] = [];
|
|
142
|
+
for (const [agentId, talk] of talks) {
|
|
143
|
+
for (const message of talk.messages) {
|
|
144
|
+
const snippet = snippetOf(message.text, words);
|
|
145
|
+
if (snippet === null) continue;
|
|
146
|
+
const { at, role } = message;
|
|
147
|
+
hits.push({ agentId, name: talk.name, team: talk.team, at, role, snippet });
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return hits.sort((a, b) => b.at - a.at).slice(0, VAULT_HITS_MAX);
|
|
151
|
+
},
|
|
152
|
+
talk(agentId) {
|
|
153
|
+
const talk = talks.get(agentId);
|
|
154
|
+
if (talk === undefined) return null;
|
|
155
|
+
return { agentId, name: talk.name, team: talk.team, messages: talk.messages };
|
|
156
|
+
},
|
|
157
|
+
report(from) {
|
|
158
|
+
const prompts: string[] = [];
|
|
159
|
+
for (const talk of talks.values()) {
|
|
160
|
+
for (const message of talk.messages) {
|
|
161
|
+
if (message.role === "user" && message.at >= from) prompts.push(message.text);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return promptReport(prompts);
|
|
165
|
+
},
|
|
166
|
+
settled: () => chain,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import type { Alert } from "../shared/alerts.js";
|
|
2
|
+
import type { OfficeAgent } from "../shared/contracts.js";
|
|
3
|
+
import { agentTitle } from "../shared/model.js";
|
|
4
|
+
import type { DirectoryView } from "./hub.js";
|
|
5
|
+
|
|
6
|
+
/** A timeline entry as the watchers read it: where it is in the timeline, and the item. */
|
|
7
|
+
export interface WatchedEntry {
|
|
8
|
+
seq: number;
|
|
9
|
+
item: {
|
|
10
|
+
type: string;
|
|
11
|
+
status?: unknown;
|
|
12
|
+
name?: unknown;
|
|
13
|
+
callId?: unknown;
|
|
14
|
+
detail?: unknown;
|
|
15
|
+
error?: unknown;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface WatcherPorts {
|
|
20
|
+
now(): number;
|
|
21
|
+
/** Whether alerts may name a file or a command: the host's streamer mode is off. */
|
|
22
|
+
named(): boolean;
|
|
23
|
+
/** The owner's pinned notes; empty for none. */
|
|
24
|
+
pinnedNotes(): string;
|
|
25
|
+
/** Says the pinned notes to an agent whose conversation was compacted mid-turn. */
|
|
26
|
+
remind(agentId: string, notes: string): void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface Watchers {
|
|
30
|
+
/** The office as the directory reports it; null when the office stops watching. */
|
|
31
|
+
view(view: DirectoryView | null): void;
|
|
32
|
+
/** The newest entries of a running agent's timeline, oldest first, as each activity read sees them. */
|
|
33
|
+
tail(agentId: string, entries: readonly WatchedEntry[]): void;
|
|
34
|
+
alerts(): Alert[];
|
|
35
|
+
dismiss(id: string): void;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The same failing call this many times running is a loop. */
|
|
39
|
+
export const LOOP_CALLS = 3;
|
|
40
|
+
/** Two agents changing one file within this long is a clash; a clash and a refusal are shown this long. */
|
|
41
|
+
export const CLASH_WINDOW_MS = 10 * 60_000;
|
|
42
|
+
export const ALERT_TTL_MS = 30 * 60_000;
|
|
43
|
+
const CALLS_KEPT = 30;
|
|
44
|
+
const DETAIL_MAX = 200;
|
|
45
|
+
|
|
46
|
+
const REFUSED = /auto mode classifier|denied by the .{0,40}classifier/i;
|
|
47
|
+
const PORT_TAKEN = /EADDRINUSE|address already in use/i;
|
|
48
|
+
|
|
49
|
+
interface Call {
|
|
50
|
+
id: string;
|
|
51
|
+
sig: string;
|
|
52
|
+
tool: string;
|
|
53
|
+
label: string;
|
|
54
|
+
file: string | null;
|
|
55
|
+
status: string;
|
|
56
|
+
error: string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function field(value: unknown, key: string): unknown {
|
|
60
|
+
return typeof value === "object" && value !== null ? Reflect.get(value, key) : undefined;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function text(value: unknown): string {
|
|
64
|
+
if (typeof value === "string") return value;
|
|
65
|
+
if (value === null || value === undefined) return "";
|
|
66
|
+
const message = field(value, "message");
|
|
67
|
+
if (typeof message === "string") return message;
|
|
68
|
+
try {
|
|
69
|
+
return JSON.stringify(value) ?? "";
|
|
70
|
+
} catch {
|
|
71
|
+
return "";
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function cut(value: string, max = DETAIL_MAX): string {
|
|
76
|
+
const line = value.replace(/\s+/g, " ").trim();
|
|
77
|
+
return line.length > max ? `${line.slice(0, max - 1)}…` : line;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function baseName(path: string): string {
|
|
81
|
+
return path.split(/[\\/]/).at(-1) ?? path;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function callOf(item: WatchedEntry["item"]): Call | null {
|
|
85
|
+
if (item.type !== "tool_call" || typeof item.callId !== "string" || item.callId === "") {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
const tool = typeof item.name === "string" ? cut(item.name, 40) : "a tool";
|
|
89
|
+
const command = field(item.detail, "command");
|
|
90
|
+
const filePath = field(item.detail, "filePath");
|
|
91
|
+
const isEdit = ["edit", "write"].includes(String(field(item.detail, "type")));
|
|
92
|
+
let label = "";
|
|
93
|
+
if (typeof command === "string") label = cut(command, 80);
|
|
94
|
+
else if (typeof filePath === "string") label = baseName(filePath);
|
|
95
|
+
return {
|
|
96
|
+
id: item.callId,
|
|
97
|
+
// What makes two calls the same call: the tool and what it was given.
|
|
98
|
+
sig: `${tool}\n${text(item.detail)}`,
|
|
99
|
+
tool,
|
|
100
|
+
label,
|
|
101
|
+
file: isEdit && typeof filePath === "string" ? filePath : null,
|
|
102
|
+
status: typeof item.status === "string" ? item.status : "",
|
|
103
|
+
error: text(item.error),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
interface Watched {
|
|
108
|
+
calls: Call[];
|
|
109
|
+
/** The last timeline sequence number read: a compaction after it is new. */
|
|
110
|
+
seq: number;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
interface Edit {
|
|
114
|
+
agentId: string;
|
|
115
|
+
at: number;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Notices what Claude Code's hooks would: a loop of failing calls, a call auto mode refused, two
|
|
120
|
+
* agents in one file, a taken port, failing checks. It reads what the office reads anyway (each
|
|
121
|
+
* running agent's newest timeline entries every 5 s, and the directory), so it notices only while
|
|
122
|
+
* an office is open, and for every provider.
|
|
123
|
+
*/
|
|
124
|
+
export function createWatchers(ports: WatcherPorts): Watchers {
|
|
125
|
+
const watched = new Map<string, Watched>();
|
|
126
|
+
const names = new Map<string, string>();
|
|
127
|
+
const edits = new Map<string, Edit[]>();
|
|
128
|
+
const alerts = new Map<string, Alert>();
|
|
129
|
+
const dismissed = new Set<string>();
|
|
130
|
+
|
|
131
|
+
function raise(alert: Omit<Alert, "at">): void {
|
|
132
|
+
if (alerts.has(alert.id) || dismissed.has(alert.id)) return;
|
|
133
|
+
alerts.set(alert.id, { ...alert, at: ports.now() });
|
|
134
|
+
}
|
|
135
|
+
const nameOf = (agentId: string) => names.get(agentId) ?? "An agent";
|
|
136
|
+
const what = (call: Call) => (ports.named() && call.label !== "" ? call.label : call.tool);
|
|
137
|
+
|
|
138
|
+
function noticeLoop(agentId: string, calls: readonly Call[]): void {
|
|
139
|
+
const ended = calls.filter((call) => call.status !== "running");
|
|
140
|
+
const last = ended.slice(-LOOP_CALLS);
|
|
141
|
+
const [first] = last;
|
|
142
|
+
const looping =
|
|
143
|
+
last.length === LOOP_CALLS &&
|
|
144
|
+
first !== undefined &&
|
|
145
|
+
last.every((call) => call.status === "failed" && call.sig === first.sig);
|
|
146
|
+
const id = `loop:${agentId}`;
|
|
147
|
+
if (!looping || first === undefined) {
|
|
148
|
+
// It got past it, or tried something else: the loop is over, and a new one is a new alert.
|
|
149
|
+
alerts.delete(id);
|
|
150
|
+
dismissed.delete(id);
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
raise({
|
|
154
|
+
id,
|
|
155
|
+
kind: "loop",
|
|
156
|
+
agentId,
|
|
157
|
+
title: `${nameOf(agentId)} is stuck in a loop`,
|
|
158
|
+
detail: cut(`${what(first)} failed ${LOOP_CALLS} times running: ${first.error}`),
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function noticeFailure(agentId: string, call: Call): void {
|
|
163
|
+
if (call.status !== "failed") return;
|
|
164
|
+
if (REFUSED.test(call.error)) {
|
|
165
|
+
const reason = /Reason:\s*(.+?)(?:\. If you|$)/s.exec(call.error)?.[1] ?? call.error;
|
|
166
|
+
raise({
|
|
167
|
+
id: `refused:${agentId}:${call.id}`,
|
|
168
|
+
kind: "refused",
|
|
169
|
+
agentId,
|
|
170
|
+
title: `Auto mode refused a call of ${nameOf(agentId)}`,
|
|
171
|
+
detail: cut(`${what(call)}: ${reason}`),
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
const port = PORT_TAKEN.test(call.error) ? /(?::|port\s+)(\d{2,5})\b/i.exec(call.error) : null;
|
|
175
|
+
if (port?.[1] !== undefined) {
|
|
176
|
+
raise({
|
|
177
|
+
id: `port:${agentId}:${port[1]}`,
|
|
178
|
+
kind: "port",
|
|
179
|
+
agentId,
|
|
180
|
+
port: Number(port[1]),
|
|
181
|
+
title: `${nameOf(agentId)} wants port ${port[1]}, which is taken`,
|
|
182
|
+
detail: cut(what(call)),
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function noticeEdit(agentId: string, file: string): void {
|
|
188
|
+
const now = ports.now();
|
|
189
|
+
const recent = (edits.get(file) ?? []).filter((edit) => now - edit.at < CLASH_WINDOW_MS);
|
|
190
|
+
const other = recent.find((edit) => edit.agentId !== agentId);
|
|
191
|
+
edits.set(file, [...recent.filter((edit) => edit.agentId !== agentId), { agentId, at: now }]);
|
|
192
|
+
if (other === undefined) return;
|
|
193
|
+
const [a, b] = [agentId, other.agentId].sort();
|
|
194
|
+
const shown = ports.named() ? baseName(file) : "the same file";
|
|
195
|
+
raise({
|
|
196
|
+
id: `clash:${a}:${b}:${file}`,
|
|
197
|
+
kind: "clash",
|
|
198
|
+
agentId,
|
|
199
|
+
otherAgentId: other.agentId,
|
|
200
|
+
title: `${nameOf(agentId)} and ${nameOf(other.agentId)} are in ${shown}`,
|
|
201
|
+
detail: `Both changed ${shown} within ${CLASH_WINDOW_MS / 60_000} minutes.`,
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function noticeChecks(view: DirectoryView): void {
|
|
206
|
+
const failing = new Set<string>();
|
|
207
|
+
for (const workspace of view.workspaces) {
|
|
208
|
+
const request = workspace.pullRequest;
|
|
209
|
+
if (request?.checks !== "failure") continue;
|
|
210
|
+
const id = `checks:${workspace.id}`;
|
|
211
|
+
failing.add(id);
|
|
212
|
+
const fixer = view.agents.find(
|
|
213
|
+
(agent) => agent.workspaceId === workspace.id && agent.status === "idle",
|
|
214
|
+
);
|
|
215
|
+
const runs = request.runs.filter((run) => run.status === "failure");
|
|
216
|
+
const kept = alerts.get(id);
|
|
217
|
+
// The agent that could fix them changes as agents start and stop: the alert follows.
|
|
218
|
+
if (kept !== undefined) {
|
|
219
|
+
const { agentId: _before, ...rest } = kept;
|
|
220
|
+
alerts.set(id, fixer ? { ...rest, agentId: fixer.id } : rest);
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
raise({
|
|
224
|
+
id,
|
|
225
|
+
kind: "checks",
|
|
226
|
+
workspaceId: workspace.id,
|
|
227
|
+
...(fixer ? { agentId: fixer.id } : {}),
|
|
228
|
+
title: `${workspace.name}: the checks fail`,
|
|
229
|
+
detail: cut(runs.map((run) => run.name).join(", ")),
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
for (const id of alerts.keys()) {
|
|
233
|
+
if (!id.startsWith("checks:") || failing.has(id)) continue;
|
|
234
|
+
alerts.delete(id);
|
|
235
|
+
dismissed.delete(id);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function forget(gone: (agent: string) => boolean): void {
|
|
240
|
+
for (const [id, alert] of alerts) {
|
|
241
|
+
if (alert.kind !== "checks" && alert.agentId !== undefined && gone(alert.agentId)) {
|
|
242
|
+
alerts.delete(id);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function seeAgents(agents: readonly OfficeAgent[]): void {
|
|
248
|
+
const here = new Set(agents.map((agent) => agent.id));
|
|
249
|
+
for (const agent of agents) names.set(agent.id, agentTitle(agent));
|
|
250
|
+
for (const id of watched.keys()) if (!here.has(id)) watched.delete(id);
|
|
251
|
+
for (const id of names.keys()) if (!here.has(id)) names.delete(id);
|
|
252
|
+
forget((id) => !here.has(id));
|
|
253
|
+
// A turn that ended is past its loop.
|
|
254
|
+
for (const agent of agents) {
|
|
255
|
+
if (agent.status === "idle") alerts.delete(`loop:${agent.id}`);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return {
|
|
260
|
+
view(view) {
|
|
261
|
+
if (view === null) {
|
|
262
|
+
watched.clear();
|
|
263
|
+
edits.clear();
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
seeAgents(view.agents);
|
|
267
|
+
noticeChecks(view);
|
|
268
|
+
},
|
|
269
|
+
tail(agentId, entries) {
|
|
270
|
+
const state = watched.get(agentId) ?? { calls: [], seq: -1 };
|
|
271
|
+
const first = !watched.has(agentId);
|
|
272
|
+
watched.set(agentId, state);
|
|
273
|
+
let compacted = false;
|
|
274
|
+
for (const entry of entries) {
|
|
275
|
+
const fresh = entry.seq > state.seq;
|
|
276
|
+
if (fresh && entry.item.type === "compaction" && entry.item.status === "completed") {
|
|
277
|
+
compacted = true;
|
|
278
|
+
}
|
|
279
|
+
const call = callOf(entry.item);
|
|
280
|
+
if (call === null) continue;
|
|
281
|
+
const known = state.calls.findIndex((kept) => kept.id === call.id);
|
|
282
|
+
const changed = known === -1 || state.calls[known]?.status !== call.status;
|
|
283
|
+
if (known === -1) state.calls.push(call);
|
|
284
|
+
else state.calls[known] = call;
|
|
285
|
+
// What the first read finds was there before the office looked: it is not news.
|
|
286
|
+
if (first || !changed) continue;
|
|
287
|
+
noticeFailure(agentId, call);
|
|
288
|
+
if (call.file !== null && call.status !== "failed") noticeEdit(agentId, call.file);
|
|
289
|
+
}
|
|
290
|
+
state.seq = Math.max(state.seq, ...entries.map((entry) => entry.seq));
|
|
291
|
+
state.calls = state.calls.slice(-CALLS_KEPT);
|
|
292
|
+
noticeLoop(agentId, state.calls);
|
|
293
|
+
const notes = ports.pinnedNotes().trim();
|
|
294
|
+
if (compacted && !first && notes !== "") ports.remind(agentId, notes);
|
|
295
|
+
},
|
|
296
|
+
alerts() {
|
|
297
|
+
const now = ports.now();
|
|
298
|
+
for (const [id, alert] of alerts) {
|
|
299
|
+
const passing = alert.kind === "refused" || alert.kind === "clash" || alert.kind === "port";
|
|
300
|
+
if (passing && now - alert.at > ALERT_TTL_MS) alerts.delete(id);
|
|
301
|
+
}
|
|
302
|
+
return [...alerts.values()].sort((a, b) => b.at - a.at);
|
|
303
|
+
},
|
|
304
|
+
dismiss(id) {
|
|
305
|
+
if (alerts.delete(id)) dismissed.add(id);
|
|
306
|
+
},
|
|
307
|
+
};
|
|
308
|
+
}
|
package/shared/alerts.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What the office noticed and the owner may want to act on:
|
|
5
|
+
* - `loop`: an agent made the same failing call three times running;
|
|
6
|
+
* - `refused`: the provider's auto mode refused a call;
|
|
7
|
+
* - `clash`: two agents changed the same file within a few minutes;
|
|
8
|
+
* - `port`: a command failed because its port is taken;
|
|
9
|
+
* - `checks`: the checks of a workspace's pull request fail.
|
|
10
|
+
*/
|
|
11
|
+
export const ALERT_KINDS = ["loop", "refused", "clash", "port", "checks"] as const;
|
|
12
|
+
export type AlertKind = (typeof ALERT_KINDS)[number];
|
|
13
|
+
|
|
14
|
+
export const AlertSchema = z.object({
|
|
15
|
+
id: z.string(),
|
|
16
|
+
kind: z.enum(ALERT_KINDS),
|
|
17
|
+
/** Epoch ms it was first noticed. */
|
|
18
|
+
at: z.number(),
|
|
19
|
+
/** Whom it is about; for `checks`, an agent of the workspace that rests and could fix them. */
|
|
20
|
+
agentId: z.string().optional(),
|
|
21
|
+
/** `clash`: the other agent. */
|
|
22
|
+
otherAgentId: z.string().optional(),
|
|
23
|
+
workspaceId: z.string().optional(),
|
|
24
|
+
/** `port`: the port that is taken. */
|
|
25
|
+
port: z.number().optional(),
|
|
26
|
+
title: z.string(),
|
|
27
|
+
detail: z.string(),
|
|
28
|
+
});
|
|
29
|
+
export type Alert = z.output<typeof AlertSchema>;
|
|
30
|
+
|
|
31
|
+
/** The most characters of a pinned note, and of a hint sent from an alert. */
|
|
32
|
+
export const PINNED_NOTES_MAX = 1000;
|
|
33
|
+
export const HINT_MAX = 500;
|
|
34
|
+
|
|
35
|
+
/** What "Tell both", "Tell the agent" and "Fix it" send. One line each: an agent reads it mid-turn. */
|
|
36
|
+
export function clashNote(file: string, other: string): string {
|
|
37
|
+
return `Office notice: another session (${other}) changed ${file} in the last few minutes. Read the file again before you change it, and do not undo that session's work.`;
|
|
38
|
+
}
|
|
39
|
+
export function portNote(port: number, holder: string | null): string {
|
|
40
|
+
const who = holder === null ? "another process" : `another session's server (${holder})`;
|
|
41
|
+
return `Office notice: port ${port} is held by ${who}. Do not stop it; start yours on another port.`;
|
|
42
|
+
}
|
|
43
|
+
export function checksNote(failing: string): string {
|
|
44
|
+
return `The checks of this workspace's pull request are failing${failing === "" ? "" : ` (${failing})`}. Find out why and fix it, then say what you changed.`;
|
|
45
|
+
}
|
|
46
|
+
export function pinnedNote(notes: string): string {
|
|
47
|
+
return `Office notice: this conversation was just compacted. The owner's pinned notes still apply:\n${notes}`;
|
|
48
|
+
}
|
package/shared/contracts.ts
CHANGED
|
@@ -4,6 +4,8 @@ import { WORDS_LANGUAGES } from "./scene-language.js";
|
|
|
4
4
|
import { defineRpc, defineSubscription } from "@getpaseo/plugin";
|
|
5
5
|
import { PlanUsageSchema } from "./plan-usage.js";
|
|
6
6
|
import { TimelineSchema } from "./timeline.js";
|
|
7
|
+
import { AlertSchema } from "./alerts.js";
|
|
8
|
+
import { PromptReportSchema, VaultHitSchema, VaultTalkSchema } from "./vault.js";
|
|
7
9
|
import { z } from "zod";
|
|
8
10
|
// In their own file, so code that needs only a name does not load the schemas.
|
|
9
11
|
import { ACTIVITY_KINDS, TEST_RESULTS } from "./names.js";
|
|
@@ -811,6 +813,44 @@ export const timelineRpc = defineRpc({
|
|
|
811
813
|
]),
|
|
812
814
|
});
|
|
813
815
|
|
|
816
|
+
/**
|
|
817
|
+
* The history vault. `on` false means the owner has it off: nothing is kept and nothing found.
|
|
818
|
+
* A search answers with the newest messages that hold every word of the query.
|
|
819
|
+
*/
|
|
820
|
+
export const vaultSearchRpc = defineRpc({
|
|
821
|
+
name: "office.vault.search.request",
|
|
822
|
+
input: z.object({ query: z.string().max(200) }),
|
|
823
|
+
output: z.object({ on: z.boolean(), hits: z.array(VaultHitSchema) }),
|
|
824
|
+
});
|
|
825
|
+
|
|
826
|
+
/** One kept conversation; null when the vault has none for this agent. */
|
|
827
|
+
export const vaultTalkRpc = defineRpc({
|
|
828
|
+
name: "office.vault.talk.request",
|
|
829
|
+
input: z.object({ agentId: z.string() }),
|
|
830
|
+
output: z.object({ talk: VaultTalkSchema.nullable() }),
|
|
831
|
+
});
|
|
832
|
+
|
|
833
|
+
/** The last seven days' prompts against the prompt helper's rules, worked out on the host. */
|
|
834
|
+
export const vaultReportRpc = defineRpc({
|
|
835
|
+
name: "office.vault.report.request",
|
|
836
|
+
input: z.object({}),
|
|
837
|
+
output: z.object({ on: z.boolean(), report: PromptReportSchema }),
|
|
838
|
+
});
|
|
839
|
+
|
|
840
|
+
/** What the office noticed and nobody dismissed, newest first. */
|
|
841
|
+
export const alertsRpc = defineRpc({
|
|
842
|
+
name: "office.alerts.request",
|
|
843
|
+
input: z.object({}),
|
|
844
|
+
output: z.object({ alerts: z.array(AlertSchema) }),
|
|
845
|
+
});
|
|
846
|
+
|
|
847
|
+
/** Dismisses an alert; the same thing is not raised again while it lasts. */
|
|
848
|
+
export const alertDismissRpc = defineRpc({
|
|
849
|
+
name: "office.alerts.dismiss.request",
|
|
850
|
+
input: z.object({ id: z.string() }),
|
|
851
|
+
output: z.object({ alerts: z.array(AlertSchema) }),
|
|
852
|
+
});
|
|
853
|
+
|
|
814
854
|
export const WriterModesSchema = z.object({
|
|
815
855
|
/** The provider the writer would start on now, by its label. */
|
|
816
856
|
provider: z.string(),
|
package/shared/settings.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { LANGUAGE_CHOICES } from "./scene-language.js";
|
|
|
3
3
|
import { defineSettings } from "@getpaseo/plugin";
|
|
4
4
|
import { WEATHER_MODES, MANUAL_CHOICES } from "./weather.js";
|
|
5
5
|
import { z } from "zod";
|
|
6
|
+
import { PINNED_NOTES_MAX } from "./alerts.js";
|
|
6
7
|
|
|
7
8
|
/** A Jira project key and a Sentry slug: only these reach a JQL query or a Sentry address. */
|
|
8
9
|
export const JIRA_KEY = /^[A-Z][A-Z0-9_]+$/;
|
|
@@ -77,6 +78,8 @@ const HostValuesSchema = z.object({
|
|
|
77
78
|
// A plan this build does not know (a later build's, or a hand edit) reads as the company floor.
|
|
78
79
|
floorPlan: z.enum(FLOOR_PLANS).default("pro").catch("pro"),
|
|
79
80
|
streamerMode: z.boolean().default(true),
|
|
81
|
+
historyVault: z.boolean().default(false),
|
|
82
|
+
pinnedNotes: z.string().max(PINNED_NOTES_MAX).default(""),
|
|
80
83
|
stickyNotes: z.string().max(STICKY_NOTES_TEXT_MAX).default(""),
|
|
81
84
|
jiraProjects: z.string().default(""),
|
|
82
85
|
sentryOrg: z.string().default(""),
|
|
@@ -143,7 +146,10 @@ const SUNDAY_TO_THURSDAY = [0, 1, 2, 3, 4];
|
|
|
143
146
|
* the 3D floor's plan: `pro` the company floor, `cozy` one open room, or `hq` headquarters.
|
|
144
147
|
* `streamerMode` on (the default) keeps file names, commands and sites out of what the office
|
|
145
148
|
* sends to the floor; off, a person's bubble names what they are at. `stickyNotes` are the
|
|
146
|
-
* owner's notes on the 3D whiteboard, with `;` between them. `
|
|
149
|
+
* owner's notes on the 3D whiteboard, with `;` between them. `historyVault` on has the office
|
|
150
|
+
* keep what is said in each conversation in a file on the host; it is off until the owner turns
|
|
151
|
+
* it on. `pinnedNotes` is said again to an agent whose conversation is compacted during a turn;
|
|
152
|
+
* empty says nothing. `jiraProjects`
|
|
147
153
|
* and `sentryProjects` are the comma separated projects checked for incidents, `sentryOrg` their
|
|
148
154
|
* Sentry organisation and `sentryUrl` the Sentry the token is sent to.
|
|
149
155
|
*/
|