@danypops/papyrus 0.35.0 → 0.35.2
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/package.json +1 -1
- package/src/client.ts +21 -0
- package/src/daemon.ts +31 -2
- package/src/discussion-service.ts +4 -1
- package/src/index.ts +1 -1
- package/src/modules/discuss.ts +6 -1
- package/src/service.ts +14 -2
package/package.json
CHANGED
package/src/client.ts
CHANGED
|
@@ -57,3 +57,24 @@ export async function connectPapyrusClient(dir: string = daemonStateDir()): Prom
|
|
|
57
57
|
throw new Error("Papyrus daemon state is stale or unreachable; restart papyrus.service");
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
|
+
|
|
61
|
+
export interface PushChannelTarget {
|
|
62
|
+
/** ws:// URL for the daemon's push-invalidation channel (see push-channel.ts in daemon-kit). */
|
|
63
|
+
url: string;
|
|
64
|
+
token: string;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Narrow surface for a push-channel consumer -- exposes only what's needed to open
|
|
69
|
+
* the WebSocket (url derived from the same handle connectPapyrusClient reads, token),
|
|
70
|
+
* not daemon-state.ts's whole internal handle shape. Returns undefined rather than
|
|
71
|
+
* throwing when the daemon has never started (no token/port on disk yet); a caller
|
|
72
|
+
* wiring this into a UI widget already tolerates "daemon not running" for its own
|
|
73
|
+
* fetch-based refresh and should treat push-channel absence the same way -- fall
|
|
74
|
+
* back to polling rather than surfacing an error.
|
|
75
|
+
*/
|
|
76
|
+
export function resolvePushChannelTarget(dir: string = daemonStateDir()): PushChannelTarget | undefined {
|
|
77
|
+
const handle = readDaemonHandle(dir);
|
|
78
|
+
if (!handle) return undefined;
|
|
79
|
+
return { url: `${handle.baseUrl.replace(/^http/, "ws")}/push`, token: handle.token };
|
|
80
|
+
}
|
package/src/daemon.ts
CHANGED
|
@@ -1,18 +1,47 @@
|
|
|
1
|
+
import { PushChannel } from "@danypops/daemon-kit/push-channel";
|
|
1
2
|
import { DAEMON_HOST, DB_OPTIMIZE_INTERVAL_MS, WAL_CHECKPOINT_INTERVAL_MS, dbPath } from "./constants.ts";
|
|
2
3
|
import { clearDaemonPort, daemonStateDir, loadOrCreateToken, writeDaemonPort } from "./daemon-state.ts";
|
|
3
4
|
import { createApp, createPapyrusService } from "./service.ts";
|
|
4
5
|
import { logEvent } from "./log.ts";
|
|
5
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Operations that never change what a Task-graph reader (the pi-papyrus widget's
|
|
9
|
+
* push subscriber) would see -- excluded from the "tasks" publish so a read call
|
|
10
|
+
* doesn't trigger a pointless extra refresh. Defaults to publishing for anything
|
|
11
|
+
* not in this set, including future operations -- a missed push (stale widget for
|
|
12
|
+
* up to one poll interval, the existing fallback) is a far smaller cost than a
|
|
13
|
+
* silently-uncovered new mutation.
|
|
14
|
+
*/
|
|
15
|
+
const TASK_READ_ONLY_OPERATIONS = new Set([
|
|
16
|
+
"tasks.active", "tasks.context", "tasks.event_feed", "tasks.focused",
|
|
17
|
+
"tasks.graph", "tasks.history", "tasks.list", "tasks.plan", "tasks.scope", "tasks.show",
|
|
18
|
+
]);
|
|
19
|
+
|
|
6
20
|
/** Start the supervised, long-running Papyrus service. */
|
|
7
21
|
export function serveMain(): void {
|
|
8
22
|
const stateDir = daemonStateDir();
|
|
9
23
|
const token = loadOrCreateToken(stateDir);
|
|
10
24
|
const service = createPapyrusService(dbPath());
|
|
11
|
-
const
|
|
25
|
+
const pushChannel = new PushChannel({ token });
|
|
26
|
+
const app = createApp({
|
|
27
|
+
service,
|
|
28
|
+
token,
|
|
29
|
+
onOperationExecuted: (operation) => {
|
|
30
|
+
if (operation.startsWith("tasks.") && !TASK_READ_ONLY_OPERATIONS.has(operation)) {
|
|
31
|
+
pushChannel.publish("tasks", { operation });
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
});
|
|
12
35
|
const server = Bun.serve({
|
|
13
36
|
hostname: DAEMON_HOST,
|
|
14
37
|
port: 0,
|
|
15
|
-
fetch: (request) =>
|
|
38
|
+
fetch: (request, bunServer) => {
|
|
39
|
+
if (new URL(request.url).pathname === "/push") return pushChannel.upgrade(request, bunServer) ?? undefined;
|
|
40
|
+
return app.fetch(request);
|
|
41
|
+
},
|
|
42
|
+
// A no-op fallback when pushChannel never calls server.upgrade() is safe: Bun only
|
|
43
|
+
// invokes these handlers for a connection that actually upgraded.
|
|
44
|
+
websocket: pushChannel.websocketHandlers(),
|
|
16
45
|
});
|
|
17
46
|
if (!server.port) {
|
|
18
47
|
service.close();
|
|
@@ -192,7 +192,10 @@ export class Discussions {
|
|
|
192
192
|
|
|
193
193
|
show(discussionId: string): DiscussionAndRounds {
|
|
194
194
|
const discussion = requireDiscussion(this.artifacts.get(discussionId), discussionId);
|
|
195
|
-
|
|
195
|
+
// DISCUSSION_MAX_ROUNDS is the hard cap enforced at reply() time, so fetching exactly that
|
|
196
|
+
// many always returns the complete transcript -- never the round store's own smaller
|
|
197
|
+
// default page size, which would silently drop the tail of a long deliberation.
|
|
198
|
+
return { discussion, rounds: this.rounds.list({ discussionId, limit: DISCUSSION_MAX_ROUNDS }) };
|
|
196
199
|
}
|
|
197
200
|
|
|
198
201
|
listRounds(discussionId: string, afterRound?: number, limit?: number): DiscussionRound[] {
|
package/src/index.ts
CHANGED
|
@@ -19,7 +19,7 @@ export type { TaskViewSelection } from "./domain/task-scope.ts";
|
|
|
19
19
|
export type { ArtifactStore } from "./ports/artifact-store.ts";
|
|
20
20
|
export type { GraphRenderer } from "./ports/graph-renderer.ts";
|
|
21
21
|
|
|
22
|
-
export { connectPapyrusClient, type PapyrusClient } from "./client.ts";
|
|
22
|
+
export { connectPapyrusClient, resolvePushChannelTarget, type PapyrusClient, type PushChannelTarget } from "./client.ts";
|
|
23
23
|
export type { DiscussionAndRounds } from "./discussion-service.ts";
|
|
24
24
|
export { NOTE_DISPOSITIONS } from "./note-service.ts";
|
|
25
25
|
export type { OperationName, SchemaState } from "./service.ts";
|
package/src/modules/discuss.ts
CHANGED
|
@@ -12,10 +12,15 @@ type OperationInput = Record<string, unknown>;
|
|
|
12
12
|
|
|
13
13
|
function string(input: OperationInput, key: string): string {
|
|
14
14
|
const value = input[key];
|
|
15
|
-
if (typeof value !== "string" || value.length === 0) throw new Error(`${key} is required`);
|
|
15
|
+
if (typeof value !== "string" || value.length === 0) throw new Error(`${key} is required${REQUIRED_FIELD_HINTS[key] ? ` (${REQUIRED_FIELD_HINTS[key]})` : ""}`);
|
|
16
16
|
return value;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
+
/** Fields whose name alone doesn't say what a valid value looks like -- everything else (title, content, id, settlement) is self-explanatory. */
|
|
20
|
+
const REQUIRED_FIELD_HINTS: Record<string, string> = {
|
|
21
|
+
actor: 'a display name for who is posting, e.g. "alice" or "agent"',
|
|
22
|
+
};
|
|
23
|
+
|
|
19
24
|
function optionalString(input: OperationInput, key: string): string | undefined {
|
|
20
25
|
const value = input[key];
|
|
21
26
|
if (value === undefined) return undefined;
|
package/src/service.ts
CHANGED
|
@@ -534,7 +534,17 @@ async function readOperationBody(request: Request): Promise<{ op?: unknown; inpu
|
|
|
534
534
|
return JSON.parse(new TextDecoder().decode(bytes)) as { op?: unknown; input?: unknown };
|
|
535
535
|
}
|
|
536
536
|
|
|
537
|
-
export function createApp(deps: {
|
|
537
|
+
export function createApp(deps: {
|
|
538
|
+
service: PapyrusService;
|
|
539
|
+
token: string;
|
|
540
|
+
/**
|
|
541
|
+
* Fired after an operation executes successfully -- decoupled from any specific
|
|
542
|
+
* consumer (push-invalidation, audit logging, metrics) so this HTTP layer stays
|
|
543
|
+
* agnostic of what a caller does with the notification. The composition root
|
|
544
|
+
* (daemon.ts) wires this to a PushChannel; tests and other embedders can ignore it.
|
|
545
|
+
*/
|
|
546
|
+
onOperationExecuted?: (operation: string, input: OperationInput) => void;
|
|
547
|
+
}): { fetch(request: Request): Promise<Response> } {
|
|
538
548
|
return {
|
|
539
549
|
async fetch(request: Request): Promise<Response> {
|
|
540
550
|
if (request.headers.get("authorization") !== `Bearer ${deps.token}`) {
|
|
@@ -555,7 +565,9 @@ export function createApp(deps: { service: PapyrusService; token: string }): { f
|
|
|
555
565
|
if (typeof input !== "object" || input === null || Array.isArray(input)) {
|
|
556
566
|
return json({ error: "input must be an object" }, { status: 400 });
|
|
557
567
|
}
|
|
558
|
-
|
|
568
|
+
const result = await deps.service.execute(body.op, input as OperationInput);
|
|
569
|
+
deps.onOperationExecuted?.(body.op, input as OperationInput);
|
|
570
|
+
return json({ result });
|
|
559
571
|
} catch (error) {
|
|
560
572
|
const status = error instanceof PayloadTooLargeError ? 413 : error instanceof UnknownOperationError ? 404 : error instanceof InvalidSessionSecretError ? 403 : 400;
|
|
561
573
|
return json({ error: error instanceof Error ? error.message : String(error) }, { status });
|