@pinet/broker-core 0.1.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.
@@ -0,0 +1,312 @@
1
+ import type { PinetMailClass } from "./mail-classification.js";
2
+ export declare const DEFAULT_EXTERNAL_THREAD_SOURCE = "external";
3
+ export interface AgentInfo {
4
+ id: string;
5
+ stableId?: string | null;
6
+ name: string;
7
+ emoji: string;
8
+ pid: number;
9
+ connectedAt: string;
10
+ lastSeen: string;
11
+ lastHeartbeat: string;
12
+ metadata: Record<string, unknown> | null;
13
+ status: "working" | "idle";
14
+ disconnectedAt?: string | null;
15
+ resumableUntil?: string | null;
16
+ idleSince?: string | null;
17
+ lastActivity?: string | null;
18
+ outboundCount?: number;
19
+ pendingInboxCount?: number;
20
+ }
21
+ export type ClientAgentInfo = Omit<AgentInfo, "stableId">;
22
+ export interface ThreadInfo {
23
+ threadId: string;
24
+ source: string;
25
+ channel: string;
26
+ ownerAgent: string | null;
27
+ ownerBinding?: "explicit" | null;
28
+ metadata?: Record<string, unknown> | null;
29
+ createdAt: string;
30
+ updatedAt: string;
31
+ }
32
+ export interface BrokerMessage {
33
+ id: number;
34
+ threadId: string;
35
+ source: string;
36
+ direction: "inbound" | "outbound";
37
+ sender: string;
38
+ body: string;
39
+ metadata: Record<string, unknown> | null;
40
+ createdAt: string;
41
+ externalId?: string;
42
+ externalTs?: string;
43
+ }
44
+ export interface InboxEntry {
45
+ id: number;
46
+ agentId: string;
47
+ messageId: number;
48
+ delivered: boolean;
49
+ readAt: string | null;
50
+ createdAt: string;
51
+ }
52
+ export interface InboxReadOptions {
53
+ threadId?: string;
54
+ limit?: number;
55
+ unreadOnly?: boolean;
56
+ markRead?: boolean;
57
+ }
58
+ export interface InboxThreadUnreadSummary {
59
+ threadId: string;
60
+ source: string;
61
+ channel: string;
62
+ unreadCount: number;
63
+ latestMessageId: number;
64
+ latestAt: string;
65
+ highestMailClass: PinetMailClass;
66
+ mailClassCounts: Record<PinetMailClass, number>;
67
+ }
68
+ export interface InboxReadResult {
69
+ messages: Array<{
70
+ entry: InboxEntry;
71
+ message: BrokerMessage;
72
+ }>;
73
+ unreadCountBefore: number;
74
+ unreadCountAfter: number;
75
+ unreadThreads: InboxThreadUnreadSummary[];
76
+ markedReadIds: number[];
77
+ }
78
+ export interface DeliveredInboundMessageResult {
79
+ entry: InboxEntry;
80
+ message: BrokerMessage;
81
+ freshDelivery: boolean;
82
+ }
83
+ export interface ChannelAssignment {
84
+ channel: string;
85
+ agentId: string;
86
+ }
87
+ export interface BacklogEntry {
88
+ id: number;
89
+ threadId: string;
90
+ channel: string;
91
+ messageId: number;
92
+ reason: string;
93
+ status: "pending" | "assigned" | "dropped";
94
+ preferredAgentId: string | null;
95
+ assignedAgentId: string | null;
96
+ attemptCount: number;
97
+ lastAttemptAt: string | null;
98
+ createdAt: string;
99
+ updatedAt: string;
100
+ }
101
+ export type TaskAssignmentStatus = "assigned" | "branch_pushed" | "pr_open" | "pr_merged" | "pr_closed";
102
+ export type TaskAssignmentKind = "implementation" | "review" | "qa" | "merge" | "interactive" | "unknown";
103
+ export interface TaskAssignmentInfo {
104
+ id: number;
105
+ agentId: string;
106
+ issueNumber: number;
107
+ branch: string | null;
108
+ prNumber: number | null;
109
+ status: TaskAssignmentStatus;
110
+ threadId: string;
111
+ sourceMessageId: number | null;
112
+ repoOwner: string | null;
113
+ repoName: string | null;
114
+ repoRoot: string | null;
115
+ taskKind: TaskAssignmentKind;
116
+ createdAt: string;
117
+ updatedAt: string;
118
+ }
119
+ export interface ScheduledWakeupInfo {
120
+ id: number;
121
+ agentId: string;
122
+ threadId: string;
123
+ body: string;
124
+ fireAt: string;
125
+ createdAt: string;
126
+ }
127
+ export interface ScheduledWakeupDelivery {
128
+ wakeup: ScheduledWakeupInfo;
129
+ message: BrokerMessage;
130
+ }
131
+ export type PortLeaseStatus = "active" | "released" | "expired";
132
+ export interface PortLeaseInfo {
133
+ id: string;
134
+ purpose: string;
135
+ port: number;
136
+ host: string;
137
+ ownerAgentId: string | null;
138
+ pid: number | null;
139
+ status: PortLeaseStatus;
140
+ metadata: Record<string, unknown> | null;
141
+ acquiredAt: string;
142
+ renewedAt: string;
143
+ expiresAt: string;
144
+ releasedAt: string | null;
145
+ }
146
+ export interface PortLeaseAcquireInput {
147
+ purpose: string;
148
+ ttlMs: number;
149
+ ownerAgentId?: string | null;
150
+ host?: string;
151
+ port?: number;
152
+ minPort?: number;
153
+ maxPort?: number;
154
+ pid?: number | null;
155
+ metadata?: Record<string, unknown> | null;
156
+ }
157
+ export interface PortLeaseRenewInput {
158
+ leaseId: string;
159
+ ttlMs: number;
160
+ ownerAgentId?: string | null;
161
+ }
162
+ export interface PortLeaseReleaseInput {
163
+ leaseId: string;
164
+ ownerAgentId?: string | null;
165
+ }
166
+ export interface PortLeaseListOptions {
167
+ includeInactive?: boolean;
168
+ expiredOnly?: boolean;
169
+ ownerAgentId?: string;
170
+ purpose?: string;
171
+ host?: string;
172
+ }
173
+ export type PinetLaneState = "planned" | "active" | "blocked" | "review" | "ready" | "done" | "cancelled" | "detached";
174
+ export type PinetLaneRole = "broker" | "coordinator" | "pm" | "lead" | "implementer" | "reviewer" | "second_pass_reviewer" | "observer";
175
+ export interface PinetLaneParticipantInfo {
176
+ laneId: string;
177
+ agentId: string;
178
+ role: PinetLaneRole;
179
+ status: string | null;
180
+ summary: string | null;
181
+ metadata: Record<string, unknown> | null;
182
+ createdAt: string;
183
+ updatedAt: string;
184
+ lastActivityAt: string;
185
+ }
186
+ export interface PinetLaneInfo {
187
+ laneId: string;
188
+ name: string | null;
189
+ task: string | null;
190
+ issueNumber: number | null;
191
+ prNumber: number | null;
192
+ threadId: string | null;
193
+ ownerAgentId: string | null;
194
+ implementationLeadAgentId: string | null;
195
+ pmMode: boolean;
196
+ state: PinetLaneState;
197
+ summary: string | null;
198
+ metadata: Record<string, unknown> | null;
199
+ createdAt: string;
200
+ updatedAt: string;
201
+ lastActivityAt: string;
202
+ participants: PinetLaneParticipantInfo[];
203
+ }
204
+ export interface PinetLaneUpsertInput {
205
+ laneId: string;
206
+ name?: string | null;
207
+ task?: string | null;
208
+ issueNumber?: number | null;
209
+ prNumber?: number | null;
210
+ threadId?: string | null;
211
+ ownerAgentId?: string | null;
212
+ implementationLeadAgentId?: string | null;
213
+ pmMode?: boolean;
214
+ state?: PinetLaneState;
215
+ summary?: string | null;
216
+ metadata?: Record<string, unknown> | null;
217
+ }
218
+ export interface PinetLaneParticipantUpsertInput {
219
+ laneId: string;
220
+ agentId: string;
221
+ role: PinetLaneRole;
222
+ status?: string | null;
223
+ summary?: string | null;
224
+ metadata?: Record<string, unknown> | null;
225
+ }
226
+ export interface PinetLaneListOptions {
227
+ state?: PinetLaneState;
228
+ ownerAgentId?: string;
229
+ includeDone?: boolean;
230
+ }
231
+ export type RoutingDecision = {
232
+ action: "deliver";
233
+ agentId: string;
234
+ } | {
235
+ action: "broadcast";
236
+ agentIds: string[];
237
+ } | {
238
+ action: "unrouted";
239
+ } | {
240
+ action: "reject";
241
+ reason: string;
242
+ };
243
+ export interface JsonRpcRequest {
244
+ jsonrpc: "2.0";
245
+ id: number | string;
246
+ method: string;
247
+ params?: Record<string, unknown>;
248
+ }
249
+ export interface JsonRpcResponse {
250
+ jsonrpc: "2.0";
251
+ id: number | string | null;
252
+ result?: unknown;
253
+ error?: JsonRpcError;
254
+ }
255
+ export interface JsonRpcError {
256
+ code: number;
257
+ message: string;
258
+ data?: unknown;
259
+ }
260
+ export declare const RPC_PARSE_ERROR = -32700;
261
+ export declare const RPC_INVALID_REQUEST = -32600;
262
+ export declare const RPC_METHOD_NOT_FOUND = -32601;
263
+ export declare const RPC_INVALID_PARAMS = -32602;
264
+ export declare const RPC_INTERNAL_ERROR = -32603;
265
+ export declare const RPC_AUTH_REQUIRED = -32001;
266
+ export declare const RPC_AGENT_NAME_CONFLICT = -32002;
267
+ export declare const RPC_AGENT_STABLE_ID_CONFLICT = -32003;
268
+ import { buildCompatibilityInstanceScope as _buildCompatibilityInstanceScope, buildCompatibilityWorkspaceScope as _buildCompatibilityWorkspaceScope, buildRuntimeScopeCarrier as _buildRuntimeScopeCarrier } from "@pinet/transport-core";
269
+ import type { InboundMessage as _InboundMessage, NormalizedMessageContent as _NormalizedMessageContent, OutboundMessage as _OutboundMessage, AdapterCapabilityRequest as _AdapterCapabilityRequest, AdapterCapabilityResult as _AdapterCapabilityResult, AdapterCapabilityEffects as _AdapterCapabilityEffects, AdapterThreadClaimEffect as _AdapterThreadClaimEffect, MessageAdapter as _MessageAdapter, RuntimeScopeCarrier as _RuntimeScopeCarrier, WorkspaceInstallScopeCarrier as _WorkspaceInstallScopeCarrier, InstanceScopeCarrier as _InstanceScopeCarrier } from "@pinet/transport-core";
270
+ export type InboundMessage = _InboundMessage;
271
+ export type NormalizedMessageContent = _NormalizedMessageContent;
272
+ export type OutboundMessage = _OutboundMessage;
273
+ export type AdapterCapabilityRequest = _AdapterCapabilityRequest;
274
+ export type AdapterCapabilityResult = _AdapterCapabilityResult;
275
+ export type AdapterCapabilityEffects = _AdapterCapabilityEffects;
276
+ export type AdapterThreadClaimEffect = _AdapterThreadClaimEffect;
277
+ export type MessageAdapter = _MessageAdapter;
278
+ export type RuntimeScopeCarrier = _RuntimeScopeCarrier;
279
+ export type WorkspaceInstallScopeCarrier = _WorkspaceInstallScopeCarrier;
280
+ export type InstanceScopeCarrier = _InstanceScopeCarrier;
281
+ export declare const buildCompatibilityWorkspaceScope: typeof _buildCompatibilityWorkspaceScope;
282
+ export declare const buildCompatibilityInstanceScope: typeof _buildCompatibilityInstanceScope;
283
+ export declare const buildRuntimeScopeCarrier: typeof _buildRuntimeScopeCarrier;
284
+ export interface BrokerDBInterface {
285
+ getThread(threadId: string): ThreadInfo | null;
286
+ getAgentById(agentId: string): AgentInfo | null;
287
+ getAgentByStableId(stableId: string): AgentInfo | null;
288
+ getAgents(): AgentInfo[];
289
+ getChannelAssignment(channel: string): ChannelAssignment | null;
290
+ acquirePortLease?(input: PortLeaseAcquireInput): PortLeaseInfo;
291
+ renewPortLease?(input: PortLeaseRenewInput): PortLeaseInfo;
292
+ releasePortLease?(input: PortLeaseReleaseInput): PortLeaseInfo;
293
+ getPortLease?(leaseId: string): PortLeaseInfo | null;
294
+ listPortLeases?(options?: PortLeaseListOptions): PortLeaseInfo[];
295
+ expirePortLeases?(nowIso?: string): PortLeaseInfo[];
296
+ /**
297
+ * Inbound user access policy for routing.
298
+ * - `null` => explicit allow-all
299
+ * - non-empty Set => explicit allowlist
300
+ * - empty Set => default-deny / allow nobody
301
+ */
302
+ getAllowedUsers(): Set<string> | null;
303
+ createThread(thread: ThreadInfo): void;
304
+ updateThread(threadId: string, updates: Partial<ThreadInfo>): void;
305
+ /**
306
+ * Atomically claim a thread for an agent (first-responder-wins).
307
+ * Creates the thread if it doesn't exist. Returns true if the claim
308
+ * succeeded, false if another agent already owns the thread.
309
+ */
310
+ claimThread(threadId: string, agentId: string, source?: string, channel?: string): boolean;
311
+ queueMessage(agentId: string, message: InboundMessage): void;
312
+ }
package/dist/types.js ADDED
@@ -0,0 +1,16 @@
1
+ export const DEFAULT_EXTERNAL_THREAD_SOURCE = "external";
2
+ // Standard JSON-RPC error codes
3
+ export const RPC_PARSE_ERROR = -32700;
4
+ export const RPC_INVALID_REQUEST = -32600;
5
+ export const RPC_METHOD_NOT_FOUND = -32601;
6
+ export const RPC_INVALID_PARAMS = -32602;
7
+ export const RPC_INTERNAL_ERROR = -32603;
8
+ // Server-defined broker auth / registration error codes
9
+ export const RPC_AUTH_REQUIRED = -32001;
10
+ export const RPC_AGENT_NAME_CONFLICT = -32002;
11
+ export const RPC_AGENT_STABLE_ID_CONFLICT = -32003;
12
+ // ─── Message adapter (canonical transport contracts) ─────
13
+ import { buildCompatibilityInstanceScope as _buildCompatibilityInstanceScope, buildCompatibilityWorkspaceScope as _buildCompatibilityWorkspaceScope, buildRuntimeScopeCarrier as _buildRuntimeScopeCarrier, } from "@pinet/transport-core";
14
+ export const buildCompatibilityWorkspaceScope = _buildCompatibilityWorkspaceScope;
15
+ export const buildCompatibilityInstanceScope = _buildCompatibilityInstanceScope;
16
+ export const buildRuntimeScopeCarrier = _buildRuntimeScopeCarrier;
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@pinet/broker-core",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "description": "Transport-neutral broker kernel primitives for pi transports",
6
+ "author": "Will Porcellini <5994936+gugu91@users.noreply.github.com>",
7
+ "license": "MIT",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/gugu91/extensions.git",
11
+ "directory": "broker-core"
12
+ },
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
16
+ "main": "./dist/index.js",
17
+ "exports": {
18
+ ".": "./dist/index.js",
19
+ "./agent-messaging": "./dist/agent-messaging.js",
20
+ "./auth": "./dist/auth.js",
21
+ "./leader": "./dist/leader.js",
22
+ "./maintenance": "./dist/maintenance.js",
23
+ "./mail-classification": "./dist/mail-classification.js",
24
+ "./message-send": "./dist/message-send.js",
25
+ "./paths": "./dist/paths.js",
26
+ "./raw-tcp-loopback": "./dist/raw-tcp-loopback.js",
27
+ "./router": "./dist/router.js",
28
+ "./schema": "./dist/schema.js",
29
+ "./types": "./dist/types.js",
30
+ "./package.json": "./package.json"
31
+ },
32
+ "files": [
33
+ "README.md",
34
+ "LICENSE",
35
+ "dist/"
36
+ ],
37
+ "pi": {},
38
+ "scripts": {
39
+ "build": "node ../scripts/build-package.mjs",
40
+ "prepack": "pnpm run build",
41
+ "lint": "eslint . --ext .ts",
42
+ "typecheck": "tsc --noEmit",
43
+ "test": "vitest run *.test.ts"
44
+ },
45
+ "dependencies": {
46
+ "@pinet/transport-core": "0.1.0"
47
+ },
48
+ "types": "./dist/index.d.ts"
49
+ }