@sjawhar/claude-envoy 2.7.1 → 2.9.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/.claude-plugin/plugin.json +1 -1
- package/.omp-plugin/plugin.json +1 -1
- package/AGENTS.md +67 -71
- package/README.md +26 -33
- package/dist/dispatch.js +159 -164
- package/dist/envoy-channel.js +717 -1385
- package/dist/session-hook.js +27 -78
- package/package.json +1 -1
- package/skills/envoy/SKILL.md +96 -22
- package/skills/envoy/references/deliveries.md +71 -15
- package/skills/envoy/references/legion.md +34 -8
- package/src/agent-stream-runner.ts +2 -2
- package/src/channel-topics.ts +148 -0
- package/src/envoy-channel-server.ts +279 -378
- package/src/replay-state.ts +17 -17
- package/src/channel-forwarder.ts +0 -262
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import type { RelayHeaders } from "@legion/envoy-client/consumer-delivery"
|
|
2
|
+
import { DedupeIdentitySchema } from "@legion/envoy-client/delivery"
|
|
3
|
+
import { messageFor } from "@legion/envoy-client/errors"
|
|
4
|
+
import { expandSubscriptionTopics } from "@legion/envoy-client/transport"
|
|
5
|
+
import { z } from "zod"
|
|
6
|
+
|
|
7
|
+
/** A message as the broker yields it (a nats.js `Msg` satisfies this). */
|
|
8
|
+
export interface ChannelBrokerMessage {
|
|
9
|
+
readonly subject: string
|
|
10
|
+
readonly data: Uint8Array
|
|
11
|
+
/**
|
|
12
|
+
* The message's reply subject. Two kinds of message carry one: a relay frame, whose reply is
|
|
13
|
+
* the relay's own answer token (`RelayMessage`, consumer delivery), and a request on the live
|
|
14
|
+
* view's control subject, answered there (`./agent-stream-runner`). No Envoy envelope is
|
|
15
|
+
* answered on a reply: a session publishes no receipt.
|
|
16
|
+
*/
|
|
17
|
+
readonly reply?: string
|
|
18
|
+
/** The message's NATS headers; only a relay frame (consumer delivery) carries any read here. */
|
|
19
|
+
readonly headers?: RelayHeaders
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface ChannelTopicSubscription extends AsyncIterable<ChannelBrokerMessage> {
|
|
23
|
+
unsubscribe(): void
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** A nats.js connection status event; only its `type` is read. */
|
|
27
|
+
export interface ChannelConnectionStatus {
|
|
28
|
+
readonly type: string
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The NATS connection surface a channel session holds. */
|
|
32
|
+
export interface ChannelConnection {
|
|
33
|
+
/** Subscribes one subject. A channel session opens two: the session's relay subject, where
|
|
34
|
+
* every Envoy message arrives, and the live view's control subject, which a viewer takes
|
|
35
|
+
* the session on (`./agent-stream-runner`). */
|
|
36
|
+
subscribe(subject: string): ChannelTopicSubscription
|
|
37
|
+
publish(subject: string, data: Uint8Array): void
|
|
38
|
+
flush(): Promise<void>
|
|
39
|
+
isClosed(): boolean
|
|
40
|
+
drain(): Promise<void>
|
|
41
|
+
close(): Promise<void>
|
|
42
|
+
/** The connection's status events (nats.js `status()`), which end when it closes. */
|
|
43
|
+
status(): AsyncIterable<ChannelConnectionStatus>
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The topics this session follows, and the one broker connection it holds. Nothing is subscribed
|
|
48
|
+
* for a topic: the listener relays every followed subject from the session's own durable consumer
|
|
49
|
+
* (`ConsumerDelivery`), so a topic is followed by registering it, and the only subscription that
|
|
50
|
+
* carries Envoy traffic is that consumer's relay subject.
|
|
51
|
+
*/
|
|
52
|
+
export interface ChannelTopics {
|
|
53
|
+
/** Start following a topic; one already followed is left alone. */
|
|
54
|
+
follow(topic: string): void
|
|
55
|
+
/** Stop following the given topics, or every topic when the list is empty. */
|
|
56
|
+
unfollow(topics: readonly string[]): void
|
|
57
|
+
/** Topics currently followed, in follow order. */
|
|
58
|
+
topics(): readonly string[]
|
|
59
|
+
/** True once the connection is closed for good. */
|
|
60
|
+
isClosed(): boolean
|
|
61
|
+
/** Drain the NATS connection; never rejects. */
|
|
62
|
+
close(): Promise<void>
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface ChannelTopicsOptions {
|
|
66
|
+
/** How long close waits for a broker drain before closing outright. */
|
|
67
|
+
readonly drainTimeoutMs?: number
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// A delivery's identity fields parse independently: a field the shared rule refuses identifies
|
|
71
|
+
// nothing, and its valid sibling can still recognise a repeat. `topic` follows the same rule
|
|
72
|
+
// before it is exposed as the envelope topic.
|
|
73
|
+
const DeliveryIdentity = DedupeIdentitySchema.extend({
|
|
74
|
+
event_id: DedupeIdentitySchema.shape.event_id.catch(undefined),
|
|
75
|
+
dedupe_key: DedupeIdentitySchema.shape.dedupe_key.catch(undefined),
|
|
76
|
+
source: DedupeIdentitySchema.shape.source.catch(undefined),
|
|
77
|
+
source_event_id: DedupeIdentitySchema.shape.source_event_id.catch(undefined),
|
|
78
|
+
topic: z.string().min(1).optional().catch(undefined),
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
const DEFAULT_DRAIN_TIMEOUT_MS = 1_000
|
|
82
|
+
|
|
83
|
+
/** A frame's identity fields, each kept only when valid; undefined for a frame that is not JSON. */
|
|
84
|
+
export function deliveryIdentity(raw: string): z.infer<typeof DeliveryIdentity> | undefined {
|
|
85
|
+
let parsed: unknown
|
|
86
|
+
try {
|
|
87
|
+
parsed = JSON.parse(raw)
|
|
88
|
+
} catch {
|
|
89
|
+
return undefined
|
|
90
|
+
}
|
|
91
|
+
const identity = DeliveryIdentity.safeParse(parsed)
|
|
92
|
+
return identity.success ? identity.data : undefined
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function report(what: string, error: unknown): void {
|
|
96
|
+
process.stderr.write(`envoy-channel: ${what} — ${messageFor(error)}\n`)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* One channel process follows its direct Envoy route and every topic a human or a tool added to
|
|
101
|
+
* it. Each is a concrete subject the registration carries (`expandSubscriptionTopics`), and the
|
|
102
|
+
* listener relays all of them from the session's own consumer, so nothing here subscribes: the
|
|
103
|
+
* set is what a registration advertises and what the relay is filtered on.
|
|
104
|
+
*/
|
|
105
|
+
export function createChannelTopics(
|
|
106
|
+
connection: ChannelConnection,
|
|
107
|
+
options: ChannelTopicsOptions = {},
|
|
108
|
+
): ChannelTopics {
|
|
109
|
+
const following = new Set<string>()
|
|
110
|
+
const drainTimeoutMs = options.drainTimeoutMs ?? DEFAULT_DRAIN_TIMEOUT_MS
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
follow(topic) {
|
|
114
|
+
for (const subject of expandSubscriptionTopics([topic])) following.add(subject)
|
|
115
|
+
},
|
|
116
|
+
unfollow(topics) {
|
|
117
|
+
if (topics.length === 0) {
|
|
118
|
+
following.clear()
|
|
119
|
+
return
|
|
120
|
+
}
|
|
121
|
+
for (const subject of expandSubscriptionTopics(topics)) following.delete(subject)
|
|
122
|
+
},
|
|
123
|
+
topics() {
|
|
124
|
+
return [...following]
|
|
125
|
+
},
|
|
126
|
+
isClosed() {
|
|
127
|
+
return connection.isClosed()
|
|
128
|
+
},
|
|
129
|
+
async close() {
|
|
130
|
+
following.clear()
|
|
131
|
+
if (connection.isClosed()) return
|
|
132
|
+
const timeout = Promise.withResolvers<void>()
|
|
133
|
+
const timer = setTimeout(timeout.resolve, drainTimeoutMs)
|
|
134
|
+
try {
|
|
135
|
+
await Promise.race([connection.drain(), timeout.promise])
|
|
136
|
+
} catch (error) {
|
|
137
|
+
report("draining the broker connection failed", error)
|
|
138
|
+
}
|
|
139
|
+
clearTimeout(timer)
|
|
140
|
+
if (connection.isClosed()) return
|
|
141
|
+
try {
|
|
142
|
+
await connection.close()
|
|
143
|
+
} catch (error) {
|
|
144
|
+
report("closing the broker connection failed", error)
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
}
|
|
148
|
+
}
|