@sjawhar/claude-envoy 4.0.1 → 4.1.1

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,107 @@
1
+ import type { DispatchDelivery, RenderInboundResult } from "@legion/envoy-client/delivery"
2
+ import type { DispatchClient } from "@legion/envoy-client/dispatch-http"
3
+ import { messageFor } from "@legion/envoy-client/errors"
4
+ import { acceptUserTurn } from "@legion/pi-shared/accept-user-turn"
5
+ import { typedInputReply } from "@legion/pi-shared/dispatch-user-turn"
6
+ import type { UserTurnRouter } from "../user-turn-router"
7
+ import type { UserTurnState } from "../user-turn-state"
8
+ import type { CodexInboxItem } from "./codex-inbox"
9
+
10
+ /**
11
+ * What a frame from Dispatch's Agents page becomes in a Codex session, the Codex
12
+ * counterpart of `../user-turn-router`.
13
+ *
14
+ * - A BTW is answered in a throwaway copy of the session's conversation (`askSideQuestion`,
15
+ * `./codex-cli`) and the answer posted back as its reply; the session's own conversation is
16
+ * left as it was.
17
+ * - A person's Send or Aside is accepted through the step both other hosts take
18
+ * (`acceptUserTurn`, `@legion/pi-shared/accept-user-turn`), and the body Dispatch stored is put
19
+ * into the session as the person's own words (`deliver`, `./codex-delivery`).
20
+ * - A `/` message is answered that it was not run: Codex runs no command it did not read from its
21
+ * own terminal.
22
+ *
23
+ * Everything else keeps the card it renders as.
24
+ */
25
+ export interface CodexUserTurnOptions {
26
+ readonly sessionId: () => string
27
+ readonly state: UserTurnState
28
+ readonly dispatch: (ms: number) => DispatchClient | undefined
29
+ readonly reply: (
30
+ delivery: DispatchDelivery,
31
+ result: { readonly body?: string; readonly error?: string },
32
+ ) => Promise<void>
33
+ /** Puts a line into the session (`./codex-delivery`). */
34
+ readonly deliver: (item: CodexInboxItem) => Promise<void>
35
+ /** Answers a side question in a throwaway copy of the session's conversation. */
36
+ readonly ask: (question: string) => Promise<string>
37
+ /** Tells the live view the user message a person's message is about to become. */
38
+ readonly expect: (text: string, dispatchMessageId: string) => void
39
+ readonly warn: (text: string) => void
40
+ }
41
+
42
+ const ACCEPT_TIMEOUT_MS = 10_000
43
+
44
+ /** Why a `/` message from Dispatch was not run in a Codex session. */
45
+ export const CODEX_COMMAND_REFUSAL =
46
+ "a Codex session runs only the commands typed at its own terminal; nothing was sent"
47
+
48
+ export function createCodexUserTurnRouter(options: CodexUserTurnOptions): UserTurnRouter {
49
+ const { state, warn } = options
50
+
51
+ const runBtw = (delivery: DispatchDelivery): void => {
52
+ void (async () => {
53
+ try {
54
+ await options.reply(delivery, { body: await options.ask(delivery.body) })
55
+ } catch (error) {
56
+ await options.reply(delivery, { error: messageFor(error) })
57
+ }
58
+ })().catch((error: unknown) => {
59
+ warn(`a BTW answer did not reach Dispatch — ${messageFor(error)}`)
60
+ })
61
+ }
62
+
63
+ return {
64
+ async route(rendered: RenderInboundResult) {
65
+ const btw = rendered.delivery
66
+ if (btw?.mode === "btw") {
67
+ runBtw(btw)
68
+ return true
69
+ }
70
+ const accepted = await acceptUserTurn({
71
+ actorId: options.sessionId(),
72
+ dispatch: () => options.dispatch(ACCEPT_TIMEOUT_MS),
73
+ handle: state.handle,
74
+ handled: state.handled,
75
+ rendered,
76
+ warn: (refusal) => {
77
+ warn(
78
+ `Dispatch message ${refusal.delivery.id} arrives as a card (${refusal.reason === "failed" ? messageFor(refusal.error) : refusal.reason})`,
79
+ )
80
+ },
81
+ })
82
+ if (accepted === undefined) return false
83
+ const { delivery, turn } = accepted
84
+ if (turn.body.startsWith("/")) {
85
+ const said = typedInputReply(turn.body, { error: CODEX_COMMAND_REFUSAL })
86
+ if (said !== undefined) {
87
+ void options.reply(delivery, { body: said }).catch((error: unknown) => {
88
+ warn(`a reply to message ${delivery.id} did not reach Dispatch — ${messageFor(error)}`)
89
+ })
90
+ }
91
+ return true
92
+ }
93
+ options.expect(turn.body, turn.messageId)
94
+ await options.deliver({
95
+ content: turn.body,
96
+ dispatchMessageId: turn.messageId,
97
+ ...(rendered.envelope?.event_id === undefined
98
+ ? {}
99
+ : { eventId: rendered.envelope.event_id }),
100
+ })
101
+ return true
102
+ },
103
+ observe() {
104
+ // Codex reports no mod events: the live view tags a person's row from the transcript.
105
+ },
106
+ }
107
+ }
@@ -398,6 +398,11 @@ export interface ChannelSessionOptions {
398
398
  * or `source` for a bundle outside its plugin cache).
399
399
  */
400
400
  readonly pluginVersion: string
401
+ /**
402
+ * The package every registration names: `@sjawhar/claude-envoy` under Claude Code, and
403
+ * `@sjawhar/codex-envoy` under Codex, the package each host's sessions update.
404
+ */
405
+ readonly pluginName: string
401
406
  }
402
407
 
403
408
  // The shared tool contract builds schemas from each host's Zod API. Claude's
@@ -1109,7 +1114,7 @@ export async function startChannelSession(options: ChannelSessionOptions): Promi
1109
1114
  driving: false,
1110
1115
  selfSubscribed: true,
1111
1116
  clientInstanceID: options.clientInstanceID,
1112
- plugin: { name: packageName, version: options.pluginVersion },
1117
+ plugin: { name: options.pluginName, version: options.pluginVersion },
1113
1118
  delivery: deliveryRequest,
1114
1119
  })
1115
1120
  // Held as a promise that settles either way, so a deregistration waits for the call to
@@ -2325,6 +2330,7 @@ export async function runEnvoyChannelServer(): Promise<void> {
2325
2330
  clientInstanceID,
2326
2331
  title,
2327
2332
  pluginVersion: installedPluginVersion(bundlePath) ?? "source",
2333
+ pluginName: packageName,
2328
2334
  ...(stamping ? { stampTitle } : {}),
2329
2335
  // The QA override names a fixed identity; only a Claude-minted id follows `/clear`.
2330
2336
  ...(configuredValue(overrideSessionId) === undefined ? { handoffPid: process.ppid } : {}),
@@ -131,7 +131,7 @@ export async function readSessionHandoff(file: string): Promise<string | undefin
131
131
  }
132
132
  }
133
133
 
134
- function processExists(pid: number): boolean {
134
+ export function processExists(pid: number): boolean {
135
135
  try {
136
136
  process.kill(pid, 0)
137
137
  return true