@howaboua/pi-shepherdr 0.2.14 → 0.2.16
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 +25 -0
- package/README.md +40 -33
- package/index.ts +16 -26
- package/package.json +11 -11
- package/src/agents-contract.ts +11 -1
- package/src/agents-discovery.ts +21 -7
- package/src/agents-focus.ts +242 -0
- package/src/agents-spawn.ts +23 -0
- package/src/agents-tool.ts +34 -5
- package/src/attachment-protocol.ts +7 -3
- package/src/attachment-target.ts +3 -1
- package/src/attachment.ts +3 -1
- package/src/board/host.ts +98 -353
- package/src/board/membership.ts +14 -8
- package/src/command-config.ts +51 -0
- package/src/control-panel-actions.ts +0 -20
- package/src/control-panel-items.ts +8 -36
- package/src/control-panel-view.ts +2 -11
- package/src/control-panel.ts +2 -11
- package/src/controller.ts +5 -5
- package/src/delivery.ts +0 -33
- package/src/herdr-client.ts +39 -6
- package/src/messages.ts +61 -6
- package/src/monitor-events.ts +24 -2
- package/src/monitor-questions.ts +12 -3
- package/src/monitor.ts +2 -2
- package/src/peer-channel.ts +39 -0
- package/src/peer-commands.ts +173 -0
- package/src/peer-inbox.ts +218 -55
- package/src/remote/shepherdr-channel.mjs +225 -0
- package/src/remote/shepherdr-checkpoints.mjs +5 -3
- package/src/remote/shepherdr-peer.mjs +151 -22
- package/src/remote/shepherdr.mjs +56 -8
- package/src/remote-client.ts +115 -11
- package/src/settlement.ts +1 -0
- package/src/shared-context.ts +104 -1
- package/src/types.ts +23 -0
- package/src/voice-handoff.ts +177 -0
- package/src/board/archive.ts +0 -77
- package/src/board/awareness.ts +0 -115
- package/src/board/config.ts +0 -121
- package/src/board/contract.ts +0 -181
- package/src/board/identity.ts +0 -169
- package/src/board/paging.ts +0 -55
- package/src/board/protocol.ts +0 -73
- package/src/board/queries.ts +0 -176
- package/src/board/response.ts +0 -56
- package/src/board/store.ts +0 -327
- package/src/board/tool.ts +0 -23
- package/src/board/turns.ts +0 -100
- package/src/context-briefing.ts +0 -226
package/src/board/awareness.ts
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
-
import type {
|
|
3
|
-
ExtensionAPI,
|
|
4
|
-
ExtensionContext,
|
|
5
|
-
} from "@earendil-works/pi-coding-agent";
|
|
6
|
-
import {
|
|
7
|
-
contextBriefingWindow,
|
|
8
|
-
recordContextBriefing,
|
|
9
|
-
registerContextBriefing,
|
|
10
|
-
} from "../context-briefing.js";
|
|
11
|
-
import { boardBriefing } from "../messages.js";
|
|
12
|
-
import { binding } from "./identity.js";
|
|
13
|
-
|
|
14
|
-
const OWNER = "shepherdr-board";
|
|
15
|
-
const STATE = "shepherdr-board-awareness";
|
|
16
|
-
interface AwarenessState {
|
|
17
|
-
sessionId: string;
|
|
18
|
-
signature: string;
|
|
19
|
-
epoch: string;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/** Board state is sampled once per selected window/membership, never on a timer. */
|
|
23
|
-
export class BoardAwareness {
|
|
24
|
-
private readonly pi: ExtensionAPI;
|
|
25
|
-
private readonly read: (ctx: ExtensionContext) => Promise<unknown>;
|
|
26
|
-
constructor(
|
|
27
|
-
pi: ExtensionAPI,
|
|
28
|
-
read: (ctx: ExtensionContext) => Promise<unknown>,
|
|
29
|
-
) {
|
|
30
|
-
this.pi = pi;
|
|
31
|
-
this.read = read;
|
|
32
|
-
registerContextBriefing(
|
|
33
|
-
pi,
|
|
34
|
-
"board",
|
|
35
|
-
(ctx) => binding(ctx).enabled,
|
|
36
|
-
(ctx, windowId) => this.record(ctx, windowId),
|
|
37
|
-
);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
refresh(ctx: ExtensionContext): AwarenessState {
|
|
41
|
-
const own = binding(ctx);
|
|
42
|
-
const signature = JSON.stringify([
|
|
43
|
-
own.boardId,
|
|
44
|
-
own.rootSessionId,
|
|
45
|
-
own.agentName,
|
|
46
|
-
own.upstream,
|
|
47
|
-
own.enabled,
|
|
48
|
-
]);
|
|
49
|
-
const saved = ctx.sessionManager
|
|
50
|
-
.getEntries()
|
|
51
|
-
.findLast(
|
|
52
|
-
(entry) =>
|
|
53
|
-
entry.type === "custom" &&
|
|
54
|
-
entry.customType === STATE &&
|
|
55
|
-
readState(entry.data).sessionId === own.sessionId,
|
|
56
|
-
);
|
|
57
|
-
if (saved?.type === "custom") {
|
|
58
|
-
const state = readState(saved.data);
|
|
59
|
-
if (state.signature === signature) return state;
|
|
60
|
-
}
|
|
61
|
-
const state = { sessionId: own.sessionId, signature, epoch: randomUUID() };
|
|
62
|
-
this.pi.appendEntry(STATE, state);
|
|
63
|
-
return state;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
private async record(ctx: ExtensionContext, windowId?: string) {
|
|
67
|
-
const own = binding(ctx);
|
|
68
|
-
const state = this.refresh(ctx);
|
|
69
|
-
if (!own.enabled) return;
|
|
70
|
-
const window = contextBriefingWindow(ctx, windowId);
|
|
71
|
-
const key = JSON.stringify([own.sessionId, window, state.epoch]);
|
|
72
|
-
await recordContextBriefing(this.pi, ctx, OWNER, key, async () => {
|
|
73
|
-
let population: "empty" | "populated" | "unavailable";
|
|
74
|
-
try {
|
|
75
|
-
const value = await this.read(ctx);
|
|
76
|
-
if (!binding(ctx).enabled) return;
|
|
77
|
-
if (
|
|
78
|
-
!value ||
|
|
79
|
-
typeof value !== "object" ||
|
|
80
|
-
!("results" in value) ||
|
|
81
|
-
!Array.isArray(value.results)
|
|
82
|
-
)
|
|
83
|
-
throw new Error("Invalid board population response");
|
|
84
|
-
population = value.results.length ? "populated" : "empty";
|
|
85
|
-
} catch (error) {
|
|
86
|
-
ctx.signal?.throwIfAborted();
|
|
87
|
-
population = "unavailable";
|
|
88
|
-
ctx.ui.notify(`Board status unavailable: ${String(error)}`, "warning");
|
|
89
|
-
}
|
|
90
|
-
ctx.signal?.throwIfAborted();
|
|
91
|
-
if (!binding(ctx).enabled) return;
|
|
92
|
-
if (
|
|
93
|
-
binding(ctx).enabled !== own.enabled ||
|
|
94
|
-
this.refresh(ctx).epoch !== state.epoch
|
|
95
|
-
)
|
|
96
|
-
throw new Error("Board membership changed during its briefing");
|
|
97
|
-
return boardBriefing(Boolean(own.upstream), population);
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function readState(value: unknown): AwarenessState {
|
|
103
|
-
if (
|
|
104
|
-
!value ||
|
|
105
|
-
typeof value !== "object" ||
|
|
106
|
-
!("sessionId" in value) ||
|
|
107
|
-
typeof value.sessionId !== "string" ||
|
|
108
|
-
!("signature" in value) ||
|
|
109
|
-
typeof value.signature !== "string" ||
|
|
110
|
-
!("epoch" in value) ||
|
|
111
|
-
typeof value.epoch !== "string"
|
|
112
|
-
)
|
|
113
|
-
throw new Error("Invalid saved board awareness");
|
|
114
|
-
return value as AwarenessState;
|
|
115
|
-
}
|
package/src/board/config.ts
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
-
import {
|
|
3
|
-
existsSync,
|
|
4
|
-
mkdirSync,
|
|
5
|
-
readFileSync,
|
|
6
|
-
realpathSync,
|
|
7
|
-
renameSync,
|
|
8
|
-
rmSync,
|
|
9
|
-
writeFileSync,
|
|
10
|
-
} from "node:fs";
|
|
11
|
-
import { dirname, resolve } from "node:path";
|
|
12
|
-
import { getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
13
|
-
|
|
14
|
-
const CONFIG_FILE = "pi-shepherdr.json";
|
|
15
|
-
export type BoardScope = "session" | "folder" | "global";
|
|
16
|
-
|
|
17
|
-
function object(value: unknown): value is Record<string, unknown> {
|
|
18
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function readDocument(path: string): Record<string, unknown> {
|
|
22
|
-
if (!existsSync(path)) return {};
|
|
23
|
-
try {
|
|
24
|
-
const value: unknown = JSON.parse(readFileSync(path, "utf8"));
|
|
25
|
-
if (!object(value)) throw new Error("configuration must be an object");
|
|
26
|
-
if (value["board"] !== undefined && !object(value["board"]))
|
|
27
|
-
throw new Error("board must be an object");
|
|
28
|
-
return value;
|
|
29
|
-
} catch (error) {
|
|
30
|
-
throw new Error(`Could not read ${path}: ${String(error)}`, {
|
|
31
|
-
cause: error,
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function readEnabled(path: string, scope: "folder" | "global") {
|
|
37
|
-
const document = readDocument(path);
|
|
38
|
-
const board = object(document["board"]) ? document["board"] : {};
|
|
39
|
-
const field = scope === "global" ? "enabledGlobally" : "enabled";
|
|
40
|
-
const wrongField = scope === "global" ? "enabled" : "enabledGlobally";
|
|
41
|
-
if (wrongField in board)
|
|
42
|
-
throw new Error(`${path}: use board.${field} for ${scope} enablement`);
|
|
43
|
-
const value = board[field];
|
|
44
|
-
if (value !== undefined && typeof value !== "boolean")
|
|
45
|
-
throw new Error(`${path}: board.${field} must be a boolean`);
|
|
46
|
-
return value;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function boardConfigPaths(folder: string) {
|
|
50
|
-
// Keep aliases on one write target so atomic saves do not replace symlinks.
|
|
51
|
-
function configPath(directory: string) {
|
|
52
|
-
const path = resolve(directory, CONFIG_FILE);
|
|
53
|
-
return existsSync(path) ? realpathSync(path) : path;
|
|
54
|
-
}
|
|
55
|
-
const global = configPath(getAgentDir());
|
|
56
|
-
const local = configPath(resolve(folder, ".pi"));
|
|
57
|
-
return {
|
|
58
|
-
global,
|
|
59
|
-
// The global config is never also a folder config.
|
|
60
|
-
folder: local === global ? undefined : local,
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function ensureBoardConfig() {
|
|
65
|
-
const path = resolve(getAgentDir(), CONFIG_FILE);
|
|
66
|
-
mkdirSync(dirname(path), { recursive: true });
|
|
67
|
-
try {
|
|
68
|
-
writeFileSync(
|
|
69
|
-
path,
|
|
70
|
-
`${JSON.stringify({ board: { enabledGlobally: false } }, null, 2)}\n`,
|
|
71
|
-
{
|
|
72
|
-
flag: "wx",
|
|
73
|
-
mode: 0o600,
|
|
74
|
-
},
|
|
75
|
-
);
|
|
76
|
-
} catch (error) {
|
|
77
|
-
if (!(error instanceof Error && "code" in error && error.code === "EEXIST"))
|
|
78
|
-
throw error;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export function readBoardConfig(folder: string) {
|
|
83
|
-
const paths = boardConfigPaths(folder);
|
|
84
|
-
return {
|
|
85
|
-
paths,
|
|
86
|
-
folder: paths.folder ? readEnabled(paths.folder, "folder") : undefined,
|
|
87
|
-
global: readEnabled(paths.global, "global") ?? false,
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export function writeBoardConfig(
|
|
92
|
-
folder: string,
|
|
93
|
-
scope: "folder" | "global",
|
|
94
|
-
enabled: boolean | undefined,
|
|
95
|
-
) {
|
|
96
|
-
if (scope === "global" && enabled === undefined)
|
|
97
|
-
throw new Error("Global enablement must be on or off");
|
|
98
|
-
const path = boardConfigPaths(folder)[scope];
|
|
99
|
-
if (!path)
|
|
100
|
-
throw new Error(
|
|
101
|
-
"This folder's config is the global config; use a session override instead",
|
|
102
|
-
);
|
|
103
|
-
readEnabled(path, scope);
|
|
104
|
-
const document = readDocument(path);
|
|
105
|
-
const board = object(document["board"]) ? { ...document["board"] } : {};
|
|
106
|
-
const field = scope === "global" ? "enabledGlobally" : "enabled";
|
|
107
|
-
if (enabled === undefined) delete board[field];
|
|
108
|
-
else board[field] = enabled;
|
|
109
|
-
if (enabled === undefined && !existsSync(path)) return;
|
|
110
|
-
document["board"] = board;
|
|
111
|
-
mkdirSync(dirname(path), { recursive: true });
|
|
112
|
-
const temporary = `${path}.${randomUUID()}.tmp`;
|
|
113
|
-
try {
|
|
114
|
-
writeFileSync(temporary, `${JSON.stringify(document, null, 2)}\n`, {
|
|
115
|
-
mode: 0o600,
|
|
116
|
-
});
|
|
117
|
-
renameSync(temporary, path);
|
|
118
|
-
} finally {
|
|
119
|
-
rmSync(temporary, { force: true });
|
|
120
|
-
}
|
|
121
|
-
}
|
package/src/board/contract.ts
DELETED
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
import { StringEnum, Type } from "@earendil-works/pi-ai";
|
|
2
|
-
import type { Static } from "typebox";
|
|
3
|
-
import { Check } from "typebox/value";
|
|
4
|
-
|
|
5
|
-
const BOARD_ACTIONS = [
|
|
6
|
-
"help",
|
|
7
|
-
"list_boards",
|
|
8
|
-
"create_channel",
|
|
9
|
-
"get_channels",
|
|
10
|
-
"post",
|
|
11
|
-
"list_threads",
|
|
12
|
-
"read_thread",
|
|
13
|
-
"read_post",
|
|
14
|
-
"search_posts",
|
|
15
|
-
"subscribe",
|
|
16
|
-
"unsubscribe",
|
|
17
|
-
] as const;
|
|
18
|
-
export const BoardParameters = Type.Object({
|
|
19
|
-
action: StringEnum(BOARD_ACTIONS),
|
|
20
|
-
});
|
|
21
|
-
const positive = Type.Optional(
|
|
22
|
-
Type.Integer({ minimum: 1, maximum: 0xffffffff }),
|
|
23
|
-
);
|
|
24
|
-
const string = Type.Optional(Type.String());
|
|
25
|
-
const Request = Type.Object(
|
|
26
|
-
{
|
|
27
|
-
action: StringEnum(BOARD_ACTIONS),
|
|
28
|
-
channel_name: string,
|
|
29
|
-
new_channel_name: string,
|
|
30
|
-
thread_id: string,
|
|
31
|
-
message_id: string,
|
|
32
|
-
text: string,
|
|
33
|
-
query: string,
|
|
34
|
-
author: string,
|
|
35
|
-
target_agent: string,
|
|
36
|
-
after_message_id: string,
|
|
37
|
-
board_id: string,
|
|
38
|
-
cursor: string,
|
|
39
|
-
subscribe: Type.Optional(Type.Boolean()),
|
|
40
|
-
recent_first: Type.Optional(Type.Boolean()),
|
|
41
|
-
sort: Type.Optional(StringEnum(["created", "activity"])),
|
|
42
|
-
limit: positive,
|
|
43
|
-
max_chars_per_post: positive,
|
|
44
|
-
limit_chars: positive,
|
|
45
|
-
offset_chars: Type.Optional(
|
|
46
|
-
Type.Integer({ minimum: 0, maximum: 0xffffffff }),
|
|
47
|
-
),
|
|
48
|
-
agents_to_notify: Type.Optional(
|
|
49
|
-
Type.Array(Type.String(), { maxItems: 256 }),
|
|
50
|
-
),
|
|
51
|
-
},
|
|
52
|
-
{ additionalProperties: false },
|
|
53
|
-
);
|
|
54
|
-
export type BoardParams = Static<typeof Request>;
|
|
55
|
-
export type BoardAction = BoardParams["action"];
|
|
56
|
-
export const MUTATIONS = new Set<BoardAction>([
|
|
57
|
-
"create_channel",
|
|
58
|
-
"post",
|
|
59
|
-
"subscribe",
|
|
60
|
-
"unsubscribe",
|
|
61
|
-
]);
|
|
62
|
-
const paging = ["limit", "cursor"];
|
|
63
|
-
const reads = ["board_id"];
|
|
64
|
-
const fields: Record<BoardAction, string[]> = {
|
|
65
|
-
help: [],
|
|
66
|
-
list_boards: paging,
|
|
67
|
-
create_channel: ["channel_name", "subscribe"],
|
|
68
|
-
get_channels: ["query", "recent_first", ...paging, ...reads],
|
|
69
|
-
post: [
|
|
70
|
-
"text",
|
|
71
|
-
"channel_name",
|
|
72
|
-
"new_channel_name",
|
|
73
|
-
"thread_id",
|
|
74
|
-
"agents_to_notify",
|
|
75
|
-
],
|
|
76
|
-
list_threads: [
|
|
77
|
-
"channel_name",
|
|
78
|
-
"sort",
|
|
79
|
-
"recent_first",
|
|
80
|
-
"max_chars_per_post",
|
|
81
|
-
...paging,
|
|
82
|
-
...reads,
|
|
83
|
-
],
|
|
84
|
-
read_thread: ["thread_id", "max_chars_per_post", ...paging, ...reads],
|
|
85
|
-
read_post: ["message_id", "offset_chars", "limit_chars", ...reads],
|
|
86
|
-
search_posts: [
|
|
87
|
-
"channel_name",
|
|
88
|
-
"query",
|
|
89
|
-
"author",
|
|
90
|
-
"after_message_id",
|
|
91
|
-
"max_chars_per_post",
|
|
92
|
-
...paging,
|
|
93
|
-
...reads,
|
|
94
|
-
],
|
|
95
|
-
subscribe: ["channel_name", "thread_id", "target_agent"],
|
|
96
|
-
unsubscribe: ["channel_name", "thread_id", "target_agent"],
|
|
97
|
-
};
|
|
98
|
-
const requiredFields: Partial<Record<BoardAction, string[]>> = {
|
|
99
|
-
create_channel: ["channel_name"],
|
|
100
|
-
post: ["text"],
|
|
101
|
-
list_threads: ["channel_name"],
|
|
102
|
-
read_thread: ["thread_id"],
|
|
103
|
-
read_post: ["message_id"],
|
|
104
|
-
};
|
|
105
|
-
export function parseBoardRequest(value: unknown): BoardParams {
|
|
106
|
-
if (!Check(Request, value))
|
|
107
|
-
throw new Error("Invalid board request; call board help");
|
|
108
|
-
const request = { ...(value as BoardParams) };
|
|
109
|
-
const allowed = new Set(["action", ...fields[request.action]]);
|
|
110
|
-
if (Object.keys(request).some((field) => !allowed.has(field)))
|
|
111
|
-
throw new Error(`Unknown ${request.action} field; board_id is read-only`);
|
|
112
|
-
for (const field of requiredFields[request.action] ?? []) {
|
|
113
|
-
if (!(field in request)) throw new Error(`${field} is required`);
|
|
114
|
-
}
|
|
115
|
-
for (const field of [
|
|
116
|
-
"thread_id",
|
|
117
|
-
"message_id",
|
|
118
|
-
"after_message_id",
|
|
119
|
-
"board_id",
|
|
120
|
-
] as const) {
|
|
121
|
-
const id = request[field];
|
|
122
|
-
if (
|
|
123
|
-
id !== undefined &&
|
|
124
|
-
!/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i.test(
|
|
125
|
-
id,
|
|
126
|
-
)
|
|
127
|
-
)
|
|
128
|
-
throw new Error(`${field} must be a UUID`);
|
|
129
|
-
if (id !== undefined) request[field] = id.toLowerCase();
|
|
130
|
-
}
|
|
131
|
-
if (request.action === "post")
|
|
132
|
-
exactlyOne(request, ["channel_name", "new_channel_name", "thread_id"]);
|
|
133
|
-
if (request.action === "subscribe" || request.action === "unsubscribe")
|
|
134
|
-
exactlyOne(request, ["channel_name", "thread_id"]);
|
|
135
|
-
return request;
|
|
136
|
-
}
|
|
137
|
-
function exactlyOne(value: BoardParams, keys: (keyof BoardParams)[]) {
|
|
138
|
-
if (keys.filter((key) => value[key] !== undefined).length !== 1)
|
|
139
|
-
throw new Error(`Provide exactly one of ${keys.join(", ")}`);
|
|
140
|
-
}
|
|
141
|
-
export function required(value: string | undefined, field: string): string {
|
|
142
|
-
if (value === undefined) throw new Error(`${field} is required`);
|
|
143
|
-
return value;
|
|
144
|
-
}
|
|
145
|
-
export function agentPath(value: string, caller: string): string {
|
|
146
|
-
const path = value.startsWith("/") ? value : `${caller}/${value}`;
|
|
147
|
-
if (!/^\/root(?:\/[a-zA-Z0-9_-]+)*$/.test(path))
|
|
148
|
-
throw new Error("Invalid agent path");
|
|
149
|
-
return path;
|
|
150
|
-
}
|
|
151
|
-
export const boardHelp = {
|
|
152
|
-
actions: Object.fromEntries(
|
|
153
|
-
Object.entries(fields).map(([action, values]) => [
|
|
154
|
-
action,
|
|
155
|
-
values
|
|
156
|
-
.map(
|
|
157
|
-
(field) =>
|
|
158
|
-
`${field}${requiredFields[action as BoardAction]?.includes(field) ? "" : "?"}`,
|
|
159
|
-
)
|
|
160
|
-
.join(" "),
|
|
161
|
-
]),
|
|
162
|
-
),
|
|
163
|
-
rules: {
|
|
164
|
-
scope:
|
|
165
|
-
"Defaults to your tree; board_id browses history, never writes. list_boards discovers saved boards",
|
|
166
|
-
post: "text required; exactly one of channel_name, new_channel_name, thread_id. Thread ID is the first post ID. New channel subscribes author to first posts",
|
|
167
|
-
subscriptions:
|
|
168
|
-
"Exactly one of channel_name/thread_id; channel covers first posts, thread covers replies. target_agent defaults to you. Posting subscribes author to thread unless explicitly unsubscribed",
|
|
169
|
-
notify:
|
|
170
|
-
"agents_to_notify is one-time, excludes author; only running turns, no waking or offline notices",
|
|
171
|
-
agents:
|
|
172
|
-
"Use spawn's boardAgent; absolute /root/... or child path relative to you, not a pane or contextAgent",
|
|
173
|
-
paging:
|
|
174
|
-
"limit default20, cap50; cursor opaque, keep query/sort unchanged. Concurrent posts may shift pages",
|
|
175
|
-
order:
|
|
176
|
-
"Newest first; get_channels by activity, list_threads sort=created|activity. read_thread pages newest replies with root on every page",
|
|
177
|
-
text: "Case-insensitive substring search; max_chars_per_post default1000. read_post offsets count Unicode characters, limit_chars default20000. Continue at next_offset_chars<n_chars",
|
|
178
|
-
budget:
|
|
179
|
-
"Post cap64KiB, channel1-128 UTF-8 bytes without edge whitespace/control characters. Results cap8000 serialized UTF-8 bytes; reads may reduce limits, continue with returned cursor/offset",
|
|
180
|
-
},
|
|
181
|
-
};
|
package/src/board/identity.ts
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
import { resolve } from "node:path";
|
|
2
|
-
import { Type } from "@earendil-works/pi-ai";
|
|
3
|
-
import type {
|
|
4
|
-
ExtensionAPI,
|
|
5
|
-
ExtensionContext,
|
|
6
|
-
} from "@earendil-works/pi-coding-agent";
|
|
7
|
-
import type { Static } from "typebox";
|
|
8
|
-
import { Check } from "typebox/value";
|
|
9
|
-
import { readBoardConfig } from "./config.js";
|
|
10
|
-
|
|
11
|
-
const uuid = Type.String({ pattern: "^[a-f0-9-]{36}$" });
|
|
12
|
-
export const BindingSchema = Type.Object(
|
|
13
|
-
{
|
|
14
|
-
protocol: Type.Literal(1),
|
|
15
|
-
boardId: uuid,
|
|
16
|
-
rootSessionId: uuid,
|
|
17
|
-
sessionId: uuid,
|
|
18
|
-
agentName: Type.String({ pattern: "^/root(?:/[a-zA-Z0-9_-]+)*$" }),
|
|
19
|
-
ownerFolder: Type.String({ minLength: 1 }),
|
|
20
|
-
databasePath: Type.String({ minLength: 1 }),
|
|
21
|
-
enabled: Type.Boolean(),
|
|
22
|
-
upstream: Type.Optional(Type.String({ minLength: 1 })),
|
|
23
|
-
},
|
|
24
|
-
{ additionalProperties: false },
|
|
25
|
-
);
|
|
26
|
-
export type BoardBinding = Static<typeof BindingSchema>;
|
|
27
|
-
const ChildSchema = Type.Object({
|
|
28
|
-
parentSessionId: uuid,
|
|
29
|
-
binding: BindingSchema,
|
|
30
|
-
machine: Type.String(),
|
|
31
|
-
sessionFile: Type.String({ minLength: 1 }),
|
|
32
|
-
});
|
|
33
|
-
export type BoardChild = Static<typeof ChildSchema>;
|
|
34
|
-
const BINDING = "shepherdr-board-binding";
|
|
35
|
-
const MEMBER = "shepherdr-board-member";
|
|
36
|
-
const CHILD = "shepherdr-board-child";
|
|
37
|
-
const SETTING = "shepherdr-board-setting";
|
|
38
|
-
const REMOVED = "shepherdr-board-removed";
|
|
39
|
-
const SettingSchema = Type.Object({
|
|
40
|
-
sessionId: uuid,
|
|
41
|
-
enabled: Type.Union([Type.Boolean(), Type.Null()]),
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
export function sessionBoardSetting(
|
|
45
|
-
ctx: ExtensionContext,
|
|
46
|
-
): boolean | undefined {
|
|
47
|
-
let enabled: boolean | undefined;
|
|
48
|
-
for (const entry of ctx.sessionManager.getEntries()) {
|
|
49
|
-
if (entry.type !== "custom" || entry.customType !== SETTING) continue;
|
|
50
|
-
if (!Check(SettingSchema, entry.data))
|
|
51
|
-
throw new Error("Invalid saved board setting");
|
|
52
|
-
const value = entry.data as Static<typeof SettingSchema>;
|
|
53
|
-
if (value.sessionId === ctx.sessionManager.getSessionId())
|
|
54
|
-
enabled = value.enabled ?? undefined;
|
|
55
|
-
}
|
|
56
|
-
return enabled;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export function saveBoardSetting(
|
|
60
|
-
pi: ExtensionAPI,
|
|
61
|
-
ctx: ExtensionContext,
|
|
62
|
-
enabled: boolean | undefined,
|
|
63
|
-
) {
|
|
64
|
-
pi.appendEntry(SETTING, {
|
|
65
|
-
sessionId: ctx.sessionManager.getSessionId(),
|
|
66
|
-
enabled: enabled ?? null,
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export function rootBoardSetting(ctx: ExtensionContext) {
|
|
71
|
-
try {
|
|
72
|
-
const config = readBoardConfig(resolve(ctx.sessionManager.getCwd()));
|
|
73
|
-
const session = sessionBoardSetting(ctx);
|
|
74
|
-
return {
|
|
75
|
-
enabled: session ?? config.folder ?? config.global,
|
|
76
|
-
source:
|
|
77
|
-
session !== undefined
|
|
78
|
-
? "session"
|
|
79
|
-
: config.folder !== undefined
|
|
80
|
-
? "folder"
|
|
81
|
-
: "global default",
|
|
82
|
-
};
|
|
83
|
-
} catch (error) {
|
|
84
|
-
return {
|
|
85
|
-
enabled: false,
|
|
86
|
-
source: "invalid configuration",
|
|
87
|
-
error: String(error),
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
export function parseBinding(value: unknown): BoardBinding {
|
|
93
|
-
if (!Check(BindingSchema, value))
|
|
94
|
-
throw new Error("Invalid Shepherdr board binding");
|
|
95
|
-
return value as BoardBinding;
|
|
96
|
-
}
|
|
97
|
-
export function binding(ctx: ExtensionContext): BoardBinding {
|
|
98
|
-
const sessionId = ctx.sessionManager.getSessionId();
|
|
99
|
-
let saved: BoardBinding | undefined;
|
|
100
|
-
for (const entry of ctx.sessionManager.getEntries()) {
|
|
101
|
-
if (entry.type !== "custom" || entry.customType !== BINDING) continue;
|
|
102
|
-
const candidate = parseBinding(entry.data);
|
|
103
|
-
if (candidate.sessionId === sessionId) saved = candidate;
|
|
104
|
-
}
|
|
105
|
-
const ownerFolder = resolve(ctx.sessionManager.getCwd());
|
|
106
|
-
if (saved?.upstream) return saved;
|
|
107
|
-
return {
|
|
108
|
-
...(saved ?? {
|
|
109
|
-
protocol: 1,
|
|
110
|
-
sessionId,
|
|
111
|
-
rootSessionId: sessionId,
|
|
112
|
-
boardId: sessionId,
|
|
113
|
-
agentName: "/root",
|
|
114
|
-
ownerFolder,
|
|
115
|
-
databasePath: resolve(ownerFolder, ".pi", "agent-message-board.sqlite"),
|
|
116
|
-
enabled: false,
|
|
117
|
-
}),
|
|
118
|
-
enabled: rootBoardSetting(ctx).enabled,
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
export function saveBinding(pi: ExtensionAPI, value: BoardBinding) {
|
|
122
|
-
pi.appendEntry(BINDING, value);
|
|
123
|
-
}
|
|
124
|
-
export function members(ctx: ExtensionContext): BoardBinding[] {
|
|
125
|
-
const own = binding(ctx);
|
|
126
|
-
const found = new Map([[own.agentName, own]]);
|
|
127
|
-
for (const entry of ctx.sessionManager.getEntries()) {
|
|
128
|
-
if (entry.type === "custom" && entry.customType === REMOVED) {
|
|
129
|
-
const value = parseBinding(entry.data);
|
|
130
|
-
if (value.boardId === own.boardId) found.delete(value.agentName);
|
|
131
|
-
continue;
|
|
132
|
-
}
|
|
133
|
-
if (entry.type !== "custom" || entry.customType !== MEMBER) continue;
|
|
134
|
-
const value = parseBinding(entry.data);
|
|
135
|
-
if (value.rootSessionId === own.sessionId && value.boardId === own.boardId)
|
|
136
|
-
found.set(value.agentName, value);
|
|
137
|
-
}
|
|
138
|
-
return [...found.values()];
|
|
139
|
-
}
|
|
140
|
-
export function saveMember(pi: ExtensionAPI, value: BoardBinding) {
|
|
141
|
-
pi.appendEntry(MEMBER, value);
|
|
142
|
-
}
|
|
143
|
-
export function children(ctx: ExtensionContext): BoardChild[] {
|
|
144
|
-
const own = binding(ctx);
|
|
145
|
-
const found = new Map<string, BoardChild>();
|
|
146
|
-
for (const entry of ctx.sessionManager.getEntries()) {
|
|
147
|
-
if (entry.type === "custom" && entry.customType === REMOVED) {
|
|
148
|
-
const value = parseBinding(entry.data);
|
|
149
|
-
if (value.boardId === own.boardId) found.delete(value.agentName);
|
|
150
|
-
continue;
|
|
151
|
-
}
|
|
152
|
-
if (entry.type !== "custom" || entry.customType !== CHILD) continue;
|
|
153
|
-
if (!Check(ChildSchema, entry.data))
|
|
154
|
-
throw new Error("Invalid saved board child route");
|
|
155
|
-
const value = entry.data as BoardChild;
|
|
156
|
-
if (
|
|
157
|
-
value.parentSessionId === own.sessionId &&
|
|
158
|
-
value.binding.boardId === own.boardId
|
|
159
|
-
)
|
|
160
|
-
found.set(value.binding.agentName, value);
|
|
161
|
-
}
|
|
162
|
-
return [...found.values()];
|
|
163
|
-
}
|
|
164
|
-
export function saveChild(pi: ExtensionAPI, value: BoardChild) {
|
|
165
|
-
pi.appendEntry(CHILD, value);
|
|
166
|
-
}
|
|
167
|
-
export function removeMember(pi: ExtensionAPI, value: BoardBinding) {
|
|
168
|
-
pi.appendEntry(REMOVED, value);
|
|
169
|
-
}
|
package/src/board/paging.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import type { BoardParams } from "./contract.js";
|
|
2
|
-
|
|
3
|
-
export interface PostRecord {
|
|
4
|
-
message_id: string;
|
|
5
|
-
channel_name: string;
|
|
6
|
-
author: string;
|
|
7
|
-
thread_id: string;
|
|
8
|
-
created_at: string;
|
|
9
|
-
text: string;
|
|
10
|
-
}
|
|
11
|
-
export const POST_COLUMNS =
|
|
12
|
-
"id AS message_id,channel AS channel_name,author,root AS thread_id,created_at,text";
|
|
13
|
-
export function metadata({ text: _text, ...value }: PostRecord) {
|
|
14
|
-
return value;
|
|
15
|
-
}
|
|
16
|
-
export function preview(post: PostRecord, chars: number) {
|
|
17
|
-
const text = Array.from(post.text);
|
|
18
|
-
return {
|
|
19
|
-
...metadata(post),
|
|
20
|
-
text_preview: text.slice(0, chars).join(""),
|
|
21
|
-
n_chars: text.length,
|
|
22
|
-
truncated: text.length > chars,
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
export function window(params: BoardParams) {
|
|
26
|
-
const limit = Math.min(params.limit ?? 20, 50);
|
|
27
|
-
let offset = 0;
|
|
28
|
-
if (params.cursor !== undefined) {
|
|
29
|
-
const bytes = Buffer.from(params.cursor, "base64url");
|
|
30
|
-
if (bytes.length !== 4 || bytes.toString("base64url") !== params.cursor)
|
|
31
|
-
throw new Error("Invalid cursor");
|
|
32
|
-
offset = bytes.readUInt32BE();
|
|
33
|
-
}
|
|
34
|
-
return { limit, offset };
|
|
35
|
-
}
|
|
36
|
-
export function page<T>(values: T[], params: BoardParams) {
|
|
37
|
-
const { limit, offset } = window(params);
|
|
38
|
-
const has_more = values.length > limit;
|
|
39
|
-
const results = values.slice(0, limit);
|
|
40
|
-
let next_cursor: string | null = null;
|
|
41
|
-
if (has_more) {
|
|
42
|
-
if (offset + results.length > 0xffffffff)
|
|
43
|
-
throw new Error("Cursor exceeds board limit");
|
|
44
|
-
const bytes = Buffer.alloc(4);
|
|
45
|
-
bytes.writeUInt32BE(offset + results.length);
|
|
46
|
-
next_cursor = bytes.toString("base64url");
|
|
47
|
-
}
|
|
48
|
-
return { results, n_returned: results.length, has_more, next_cursor };
|
|
49
|
-
}
|
|
50
|
-
export function budget(params: BoardParams, count: number) {
|
|
51
|
-
return Math.min(
|
|
52
|
-
params.max_chars_per_post ?? 1000,
|
|
53
|
-
Math.floor(20000 / Math.max(1, count)),
|
|
54
|
-
);
|
|
55
|
-
}
|