@sjawhar/claude-envoy 4.6.1 → 4.7.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.
@@ -1,13 +1,102 @@
1
+ import { isCleanUrl } from "@legion/contracts/log-text"
1
2
  import { z } from "zod"
2
3
 
3
4
  const SendArgumentsSchema = z.tuple([z.string().min(1), z.string().min(1)]).rest(z.string().min(1))
4
5
 
6
+ /** The flag that sends a message under a key the caller names: the one an unconfirmed send
7
+ * printed, so the listener drops the resend as a repeat if the first copy arrived. */
8
+ export const IDEMPOTENCY_KEY_FLAG = "--idempotency-key"
9
+
5
10
  export type SendArguments = {
6
11
  readonly targetSession: string
7
12
  readonly message: string
13
+ /** The key the send goes under, where the caller names one; one is minted otherwise. */
14
+ readonly idempotencyKey?: string
8
15
  }
9
16
 
17
+ /**
18
+ * `[--idempotency-key <key>] <target session> <message words…>`. The flag comes first, so a
19
+ * message whose words begin with it is still a message.
20
+ */
10
21
  export function parseSendArguments(arguments_: readonly string[]): SendArguments {
11
- const [targetSession, ...words] = SendArgumentsSchema.parse(arguments_)
12
- return { targetSession, message: words.join(" ") }
22
+ let rest = arguments_
23
+ let idempotencyKey: string | undefined
24
+ if (rest[0] === IDEMPOTENCY_KEY_FLAG) {
25
+ idempotencyKey = z.string().min(1).parse(rest[1])
26
+ rest = rest.slice(2)
27
+ }
28
+ const [targetSession, ...words] = SendArgumentsSchema.parse(rest)
29
+ return {
30
+ targetSession,
31
+ message: words.join(" "),
32
+ ...(idempotencyKey === undefined ? {} : { idempotencyKey }),
33
+ }
34
+ }
35
+
36
+ /**
37
+ * `value` as one word of a POSIX shell command that bash and zsh both read as the same text:
38
+ * bare where every character is one neither shell expands, single-quoted otherwise, with each `'`
39
+ * closed, escaped and reopened. A leading `=` is quoted too: zsh's EQUALS option, on by default,
40
+ * reads a bare `=word` as the path of the command `word`.
41
+ */
42
+ export function shellWord(value: string): string {
43
+ return /^[A-Za-z0-9_./:@%+-][A-Za-z0-9_./:=@%+-]*$/.test(value)
44
+ ? value
45
+ : `'${value.replaceAll("'", "'\\''")}'`
46
+ }
47
+
48
+ /** What the resend command an unconfirmed send prints is built of. */
49
+ export interface Resend {
50
+ /** The command's own script (`bun <script>`). */
51
+ readonly script: string
52
+ /** The directory the send ran in: a checkout's own `.envoy/envoy.json` is read from it. */
53
+ readonly cwd: string
54
+ /**
55
+ * The settings, by name and value, that chose the send's listener and credential (the envoy.json
56
+ * it read, `ENVOY_URL`, `ENVOY_TOKEN_FILE`, the session): each is carried as `NAME=value` ahead
57
+ * of the command, so the resend goes where the send went from any shell it is run in.
58
+ */
59
+ readonly settings: readonly (readonly [name: string, value: string])[]
60
+ readonly idempotencyKey: string
61
+ readonly targetSession: string
62
+ readonly message: string
63
+ }
64
+
65
+ /**
66
+ * What an unconfirmed send prints of its resend: the command, or, where a setting it carries is a
67
+ * URL no printed line may name, that setting's name and no command.
68
+ */
69
+ export type ResendCommand = { readonly command: string } | { readonly unprinted: string }
70
+
71
+ /** Whether `name` names a URL setting (`ENVOY_URL`), which prints only as `isCleanUrl` allows. */
72
+ const URL_SETTING = /_URL$/
73
+
74
+ /**
75
+ * The command that sends the same message again under its key, to the same listener, as
76
+ * `envoy-send` was run: `cd <cwd> && NAME=value … bun <script> --idempotency-key <key> <target>
77
+ * <message>`. The listener drops it as a repeat if the first copy arrived.
78
+ *
79
+ * A URL setting is printed only where it is a plain http(s) URL with no place a credential can sit
80
+ * (`isCleanUrl`, `@legion/contracts/log-text`), the one rule every client names a configured URL
81
+ * by. Where one is not, such as a URL with user info of any length, no command is built, only the
82
+ * setting's name (`unprinted`): the scrub of held secrets takes out a held value of 10 characters
83
+ * or more, so a shorter user and password would print whole, and the URL with its user info taken
84
+ * out could still print part of it, since the parser can split such a URL otherwise than its
85
+ * writer meant (`https://user:p@ssw0rd` reads `ssw0rd` as the host).
86
+ */
87
+ export function resendCommand(resend: Resend): ResendCommand {
88
+ const unclean = resend.settings.find(
89
+ ([name, value]) => URL_SETTING.test(name) && !isCleanUrl(value),
90
+ )
91
+ if (unclean !== undefined) return { unprinted: unclean[0] }
92
+ const assignments = resend.settings.map(([name, value]) => `${name}=${shellWord(value)}`)
93
+ const command = [
94
+ "bun",
95
+ resend.script,
96
+ IDEMPOTENCY_KEY_FLAG,
97
+ resend.idempotencyKey,
98
+ resend.targetSession,
99
+ resend.message,
100
+ ].map(shellWord)
101
+ return { command: `cd ${shellWord(resend.cwd)} && ${[...assignments, ...command].join(" ")}` }
13
102
  }