@raidiant/notifai 4.0.0 → 6.0.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/CHANGELOG.md +73 -0
- package/README.md +7 -2
- package/dist/client.d.ts +8 -3
- package/dist/client.js +24 -3
- package/dist/client.js.map +1 -1
- package/dist/commands-auth.d.ts +14 -0
- package/dist/commands-auth.js +154 -0
- package/dist/commands-auth.js.map +1 -0
- package/dist/commands-config.d.ts +31 -0
- package/dist/commands-config.js +246 -0
- package/dist/commands-config.js.map +1 -0
- package/dist/commands-core.d.ts +102 -0
- package/dist/commands-core.js +135 -0
- package/dist/commands-core.js.map +1 -0
- package/dist/commands-devices.d.ts +31 -0
- package/dist/commands-devices.js +124 -0
- package/dist/commands-devices.js.map +1 -0
- package/dist/commands-doctor.d.ts +24 -0
- package/dist/commands-doctor.js +1011 -0
- package/dist/commands-doctor.js.map +1 -0
- package/dist/commands-harness-context.d.ts +61 -0
- package/dist/commands-harness-context.js +93 -0
- package/dist/commands-harness-context.js.map +1 -0
- package/dist/commands-hook-shape.d.ts +17 -0
- package/dist/commands-hook-shape.js +37 -0
- package/dist/commands-hook-shape.js.map +1 -0
- package/dist/commands-hooks.d.ts +115 -0
- package/dist/commands-hooks.js +1225 -0
- package/dist/commands-hooks.js.map +1 -0
- package/dist/commands-init.d.ts +35 -0
- package/dist/commands-init.js +580 -0
- package/dist/commands-init.js.map +1 -0
- package/dist/commands-io.d.ts +2 -0
- package/dist/commands-io.js +110 -0
- package/dist/commands-io.js.map +1 -0
- package/dist/commands-logs.d.ts +30 -0
- package/dist/commands-logs.js +201 -0
- package/dist/commands-logs.js.map +1 -0
- package/dist/commands-send-support.d.ts +44 -0
- package/dist/commands-send-support.js +180 -0
- package/dist/commands-send-support.js.map +1 -0
- package/dist/commands-send.d.ts +33 -0
- package/dist/commands-send.js +519 -0
- package/dist/commands-send.js.map +1 -0
- package/dist/commands-setup-proof.d.ts +15 -0
- package/dist/commands-setup-proof.js +53 -0
- package/dist/commands-setup-proof.js.map +1 -0
- package/dist/commands-skill.d.ts +10 -0
- package/dist/commands-skill.js +71 -0
- package/dist/commands-skill.js.map +1 -0
- package/dist/commands.d.ts +12 -378
- package/dist/commands.js +12 -4420
- package/dist/commands.js.map +1 -1
- package/dist/config-schema.js +15 -6
- package/dist/config-schema.js.map +1 -1
- package/dist/config.d.ts +4 -2
- package/dist/config.js +13 -0
- package/dist/config.js.map +1 -1
- package/dist/credentials.js +13 -3
- package/dist/credentials.js.map +1 -1
- package/dist/file-lock.d.ts +6 -1
- package/dist/file-lock.js +55 -9
- package/dist/file-lock.js.map +1 -1
- package/dist/harnesses.d.ts +1 -1
- package/dist/harnesses.js.map +1 -1
- package/dist/hooks.d.ts +74 -7
- package/dist/hooks.js +522 -166
- package/dist/hooks.js.map +1 -1
- package/dist/install-hooks.d.ts +91 -14
- package/dist/install-hooks.js +226 -34
- package/dist/install-hooks.js.map +1 -1
- package/dist/interactive.d.ts +1 -1
- package/dist/interactive.js +5 -5
- package/dist/interactive.js.map +1 -1
- package/dist/logging.d.ts +8 -0
- package/dist/logging.js +9 -0
- package/dist/logging.js.map +1 -1
- package/dist/main.js +45 -18
- package/dist/main.js.map +1 -1
- package/dist/opencode-plugin.d.ts +8 -0
- package/dist/opencode-plugin.js +24 -162
- package/dist/opencode-plugin.js.map +1 -1
- package/dist/readiness.d.ts +3 -1
- package/dist/readiness.js.map +1 -1
- package/dist/send.d.ts +6 -0
- package/dist/send.js +25 -14
- package/dist/send.js.map +1 -1
- package/dist/ui/help.d.ts +0 -1
- package/dist/ui/help.js +6 -3
- package/dist/ui/help.js.map +1 -1
- package/dist/ui/theme.d.ts +0 -15
- package/dist/ui/theme.js +2 -18
- package/dist/ui/theme.js.map +1 -1
- package/package.json +2 -2
- package/src/client.test.ts +90 -1
- package/src/client.ts +35 -2
- package/src/commands-auth.ts +174 -0
- package/src/commands-config.ts +307 -0
- package/src/commands-core.ts +240 -0
- package/src/commands-devices.ts +150 -0
- package/src/commands-doctor.ts +1209 -0
- package/src/commands-harness-context.ts +126 -0
- package/src/commands-hook-shape.ts +53 -0
- package/src/commands-hooks.ts +1586 -0
- package/src/commands-init.ts +721 -0
- package/src/commands-io.ts +108 -0
- package/src/commands-logs.ts +243 -0
- package/src/commands-send-support.ts +224 -0
- package/src/commands-send.ts +623 -0
- package/src/commands-setup-proof.ts +71 -0
- package/src/commands-skill.ts +84 -0
- package/src/commands.test.ts +677 -89
- package/src/commands.ts +58 -5472
- package/src/config-schema.test.ts +1 -1
- package/src/config-schema.ts +17 -6
- package/src/config.test.ts +50 -9
- package/src/config.ts +19 -1
- package/src/credentials.test.ts +25 -1
- package/src/credentials.ts +13 -3
- package/src/file-lock.test.ts +41 -0
- package/src/file-lock.ts +59 -9
- package/src/harnesses.test.ts +37 -29
- package/src/harnesses.ts +1 -4
- package/src/hooks.test.ts +703 -26
- package/src/hooks.ts +590 -182
- package/src/install-hooks.test.ts +232 -12
- package/src/install-hooks.ts +241 -37
- package/src/interactive.test.ts +19 -1
- package/src/interactive.ts +5 -5
- package/src/logging-process-worker.test.ts +8 -2
- package/src/logging.test.ts +48 -20
- package/src/logging.ts +12 -0
- package/src/main.ts +58 -24
- package/src/opencode-plugin.ts +24 -164
- package/src/question-caps.test.ts +49 -0
- package/src/readiness.ts +3 -1
- package/src/send.ts +32 -16
- package/src/skill.test.ts +199 -146
- package/src/ui/help.ts +8 -3
- package/src/ui/theme.ts +2 -16
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { SHIPPED_CLI_CAPABILITIES, type RecoveryAction } from '@raidiant/notifai-protocol'
|
|
2
|
+
import { type ClaudeWakeAdapters } from './claude-wake.js'
|
|
3
|
+
import {
|
|
4
|
+
ApiCallError,
|
|
5
|
+
NetworkError,
|
|
6
|
+
createClient,
|
|
7
|
+
type ApiClient,
|
|
8
|
+
type ClientOptions,
|
|
9
|
+
} from './client.js'
|
|
10
|
+
import { type CodexWakeAdapters } from './codex-wake.js'
|
|
11
|
+
import { loadConfig, type CliConfig } from './config.js'
|
|
12
|
+
import type { CredentialStore, MachineCredential } from './credentials.js'
|
|
13
|
+
import { type HookAdapterTarget } from './hook-adapter.js'
|
|
14
|
+
import { logConfigResolved, logSettingsFrom, nullLogger, type Logger } from './logging.js'
|
|
15
|
+
import type { NativeSkills } from './native-skills.js'
|
|
16
|
+
import { packageVersion } from './release.js'
|
|
17
|
+
import type { Tone } from './ui/theme.js'
|
|
18
|
+
|
|
19
|
+
export interface CommandIo {
|
|
20
|
+
out(line: string): void
|
|
21
|
+
err(line: string): void
|
|
22
|
+
/** Interactive confirmation; resolves `fallback` (default false) when not interactive. */
|
|
23
|
+
confirm(question: string, fallback?: boolean): Promise<boolean>
|
|
24
|
+
openUrl(url: string): void
|
|
25
|
+
/**
|
|
26
|
+
* True only when a human is demonstrably driving a terminal. Everything below
|
|
27
|
+
* is optional sugar that MUST only be called behind this flag: an agent that
|
|
28
|
+
* reaches an interactive prompt does not error, it hangs — the prompt
|
|
29
|
+
* libraries wait on stdin for ever — so the gate is bypass, not handling.
|
|
30
|
+
* Test fakes leave all of this undefined and exercise the plain paths.
|
|
31
|
+
*/
|
|
32
|
+
interactive?: boolean
|
|
33
|
+
select?(message: string, options: { value: string; label: string; hint?: string }[]): Promise<string | null>
|
|
34
|
+
/** Interactive multi-select. `initial` are pre-selected values. */
|
|
35
|
+
multiselect?(
|
|
36
|
+
message: string,
|
|
37
|
+
options: { value: string; label: string; hint?: string }[],
|
|
38
|
+
initial?: string[],
|
|
39
|
+
): Promise<string[] | null>
|
|
40
|
+
intro?(title: string): Promise<void>
|
|
41
|
+
outro?(message: string): Promise<void>
|
|
42
|
+
note?(message: string, title?: string): Promise<void>
|
|
43
|
+
spinner?(message: string): Promise<CommandSpinner | null>
|
|
44
|
+
/**
|
|
45
|
+
* A report line. `tone` distinguishes the two states that are neither pass
|
|
46
|
+
* nor fail — something optional the user declined, and something that was
|
|
47
|
+
* never evaluated — which a boolean has to round to one or the other.
|
|
48
|
+
* Optional so existing implementations keep type-checking.
|
|
49
|
+
*/
|
|
50
|
+
check?(ok: boolean, message: string, tone?: Tone): Promise<void>
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface CommandSpinner {
|
|
54
|
+
message(message: string): void
|
|
55
|
+
stop(message: string): void
|
|
56
|
+
error(message: string): void
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface CommandDeps {
|
|
60
|
+
io: CommandIo
|
|
61
|
+
store: CredentialStore
|
|
62
|
+
env: NodeJS.ProcessEnv
|
|
63
|
+
cwd: string
|
|
64
|
+
/** Test seam; production fixes the hook adapter under os.homedir(). */
|
|
65
|
+
hookAdapterHome?: string
|
|
66
|
+
/** Test seam; production uses this process's Node and CLI paths. */
|
|
67
|
+
hookInstallTarget?: HookAdapterTarget
|
|
68
|
+
/** Test seam; production uses process.platform for adapter format and quoting. */
|
|
69
|
+
hookPlatform?: NodeJS.Platform
|
|
70
|
+
/** Test seam; production uses fetch against base_url. */
|
|
71
|
+
clientFactory?: (baseUrl: string, bearer: string | null, options?: ClientOptions) => ApiClient
|
|
72
|
+
/** Test seam for bounded polling without wall-clock sleeps. */
|
|
73
|
+
now?: () => number
|
|
74
|
+
/** Test seam for retry/backoff timing. */
|
|
75
|
+
sleep?: (milliseconds: number) => Promise<void>
|
|
76
|
+
/** Test seams for Claude liveness, socket delivery, and cold resume. */
|
|
77
|
+
claudeWake?: ClaudeWakeAdapters
|
|
78
|
+
claudeSourcePid?: number
|
|
79
|
+
/** Test seams for the Codex thread-writer probe and cold resume. */
|
|
80
|
+
codexWake?: CodexWakeAdapters
|
|
81
|
+
codexSourcePid?: number
|
|
82
|
+
/** Test seam and production adapter for the external native skills installer. */
|
|
83
|
+
nativeSkills?: NativeSkills
|
|
84
|
+
/**
|
|
85
|
+
* The local record of what this invocation did. Optional so a test fake need
|
|
86
|
+
* not carry one; `log(deps)` supplies a logger that records nothing.
|
|
87
|
+
*/
|
|
88
|
+
logger?: Logger
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The invocation's logger, or one that discards everything. */
|
|
92
|
+
export function log(deps: CommandDeps): Logger {
|
|
93
|
+
return deps.logger ?? nullLogger()
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function loadLoggedConfig(
|
|
97
|
+
deps: CommandDeps,
|
|
98
|
+
options: Parameters<typeof loadConfig>[0],
|
|
99
|
+
): CliConfig {
|
|
100
|
+
const config = loadConfig(options)
|
|
101
|
+
const logger = log(deps)
|
|
102
|
+
logger.adopt(logSettingsFrom(config))
|
|
103
|
+
logger.bind({ project: config.project.value })
|
|
104
|
+
logConfigResolved(logger, config)
|
|
105
|
+
return config
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export const EXIT = {
|
|
109
|
+
ok: 0,
|
|
110
|
+
failed: 1,
|
|
111
|
+
usage: 2,
|
|
112
|
+
noReply: 3,
|
|
113
|
+
auth: 4,
|
|
114
|
+
network: 5,
|
|
115
|
+
} as const
|
|
116
|
+
|
|
117
|
+
export function makeClient(
|
|
118
|
+
deps: CommandDeps,
|
|
119
|
+
baseUrl: string,
|
|
120
|
+
bearer: string | null,
|
|
121
|
+
options?: ClientOptions,
|
|
122
|
+
): ApiClient {
|
|
123
|
+
return (deps.clientFactory ?? createClient)(baseUrl, bearer, {
|
|
124
|
+
...options,
|
|
125
|
+
logger: log(deps),
|
|
126
|
+
cliVersion: packageVersion(),
|
|
127
|
+
capabilities: SHIPPED_CLI_CAPABILITIES,
|
|
128
|
+
})
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function resolvedBaseUrl(config: CliConfig, credential: MachineCredential | null): string {
|
|
132
|
+
return config.base_url.source === 'default' && credential ? credential.baseUrl : config.base_url.value
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function authedClient(deps: CommandDeps, config: CliConfig): { client: ApiClient; baseUrl: string } | null {
|
|
136
|
+
const credential = deps.store.load()
|
|
137
|
+
if (!credential) {
|
|
138
|
+
// The commonest reason a command does nothing, and one that leaves no other
|
|
139
|
+
// trace: it returns before any request is made, so without this the log
|
|
140
|
+
// shows an exit code and no cause.
|
|
141
|
+
log(deps).error('cli.error', { kind: 'auth', message: 'not signed in', store: deps.store.describe() })
|
|
142
|
+
deps.io.err('Not signed in. Run `notifai login` first.')
|
|
143
|
+
return null
|
|
144
|
+
}
|
|
145
|
+
const baseUrl = resolvedBaseUrl(config, credential)
|
|
146
|
+
return {
|
|
147
|
+
client: makeClient(deps, baseUrl, `Bearer nfm_${credential.machineId}.${credential.secret}`),
|
|
148
|
+
baseUrl,
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export const UPDATE_CLI_COMMAND = 'npm install -g @raidiant/notifai'
|
|
153
|
+
|
|
154
|
+
function localRecovery(action: RecoveryAction | null): string | null {
|
|
155
|
+
switch (action) {
|
|
156
|
+
case 'update_cli':
|
|
157
|
+
return `next: ${UPDATE_CLI_COMMAND}`
|
|
158
|
+
case 'update_companion':
|
|
159
|
+
return 'next: Update Notifai on the named device.'
|
|
160
|
+
case 'wait_for_service':
|
|
161
|
+
return 'next: The service is being updated; try again later.'
|
|
162
|
+
default:
|
|
163
|
+
return null
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function reportError(
|
|
168
|
+
deps: CommandDeps,
|
|
169
|
+
err: unknown,
|
|
170
|
+
context: Record<string, unknown> = {},
|
|
171
|
+
): number {
|
|
172
|
+
// Recorded before it is printed: stderr from an agent's shell call is often
|
|
173
|
+
// the one thing that does not survive into the next turn, and this is exactly
|
|
174
|
+
// the line someone comes back looking for.
|
|
175
|
+
log(deps).error('cli.error', {
|
|
176
|
+
...context,
|
|
177
|
+
...(err instanceof ApiCallError
|
|
178
|
+
? {
|
|
179
|
+
kind: 'api',
|
|
180
|
+
status: err.status,
|
|
181
|
+
code: err.code,
|
|
182
|
+
message: err.message,
|
|
183
|
+
next_action: err.nextAction,
|
|
184
|
+
recovery_action: err.recoveryAction,
|
|
185
|
+
details: err.details,
|
|
186
|
+
}
|
|
187
|
+
: err instanceof NetworkError
|
|
188
|
+
? { kind: 'network', message: err.message }
|
|
189
|
+
: { kind: 'unknown', message: String(err) }),
|
|
190
|
+
})
|
|
191
|
+
if (err instanceof ApiCallError) {
|
|
192
|
+
deps.io.err(
|
|
193
|
+
deps.io.interactive === true && err.recoveryAction !== null
|
|
194
|
+
? err.message
|
|
195
|
+
: `${err.code}: ${err.message}`,
|
|
196
|
+
)
|
|
197
|
+
// The server's field-level details were already being logged above, but
|
|
198
|
+
// stderr is the one surface an agent's next turn actually reads — so the
|
|
199
|
+
// diagnosis has to be on it, the same way the hook path prints it.
|
|
200
|
+
const paths = rejectedPaths(err.details)
|
|
201
|
+
if (paths.length > 0) deps.io.err(`the server rejected: ${paths.join(', ')}`)
|
|
202
|
+
// A 422 on a request this CLI built is not a user error: the two sides
|
|
203
|
+
// disagree about the contract, in whichever direction. `doctor` says which.
|
|
204
|
+
// Codes that name a policy the account chose are the exception — the
|
|
205
|
+
// contract held, the body simply did not satisfy that account.
|
|
206
|
+
if (
|
|
207
|
+
err.status === 422 &&
|
|
208
|
+
err.code !== 'acknowledgement_text_required' &&
|
|
209
|
+
err.code !== 'feature_unavailable'
|
|
210
|
+
) {
|
|
211
|
+
deps.io.err(
|
|
212
|
+
'this build sent a field the server did not accept — the CLI and server ' +
|
|
213
|
+
'disagree about the contract; check with `notifai doctor`',
|
|
214
|
+
)
|
|
215
|
+
}
|
|
216
|
+
const recovery = localRecovery(err.recoveryAction)
|
|
217
|
+
if (recovery !== null) deps.io.err(recovery)
|
|
218
|
+
else if (err.recoveryAction === null && err.nextAction) deps.io.err(`next: ${err.nextAction}`)
|
|
219
|
+
if (err.code === 'auth_required' || err.code === 'machine_revoked') return EXIT.auth
|
|
220
|
+
return err.status >= 500 ? EXIT.network : EXIT.failed
|
|
221
|
+
}
|
|
222
|
+
if (err instanceof NetworkError) {
|
|
223
|
+
deps.io.err(err.message)
|
|
224
|
+
return EXIT.network
|
|
225
|
+
}
|
|
226
|
+
deps.io.err(String(err))
|
|
227
|
+
return EXIT.failed
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export function rejectedPaths(details: unknown): string[] {
|
|
231
|
+
if (!Array.isArray(details)) return []
|
|
232
|
+
return details
|
|
233
|
+
.map((issue) =>
|
|
234
|
+
typeof issue === 'object' && issue !== null && typeof (issue as { path?: unknown }).path === 'string'
|
|
235
|
+
? (issue as { path: string }).path
|
|
236
|
+
: null,
|
|
237
|
+
)
|
|
238
|
+
.filter((path): path is string => path !== null && path !== '')
|
|
239
|
+
.slice(0, 5)
|
|
240
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { PLATFORMS, type Platform, type RoutableDevice } from '@raidiant/notifai-protocol'
|
|
2
|
+
import {
|
|
3
|
+
EXIT,
|
|
4
|
+
authedClient,
|
|
5
|
+
loadLoggedConfig,
|
|
6
|
+
makeClient,
|
|
7
|
+
reportError,
|
|
8
|
+
resolvedBaseUrl,
|
|
9
|
+
type CommandDeps,
|
|
10
|
+
} from './commands-core.js'
|
|
11
|
+
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// devices / capabilities
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
|
|
16
|
+
export async function devicesCommand(deps: CommandDeps, flags: { json?: boolean }): Promise<number> {
|
|
17
|
+
const config = loadLoggedConfig(deps, { cwd: deps.cwd, env: deps.env })
|
|
18
|
+
const authed = authedClient(deps, config)
|
|
19
|
+
if (!authed) return EXIT.auth
|
|
20
|
+
try {
|
|
21
|
+
const result = await authed.client.listDevices()
|
|
22
|
+
if (flags.json) {
|
|
23
|
+
deps.io.out(JSON.stringify(result, null, 2))
|
|
24
|
+
return EXIT.ok
|
|
25
|
+
}
|
|
26
|
+
if (result.devices.length === 0) {
|
|
27
|
+
const supportUrl = supportPageUrl(authed.baseUrl)
|
|
28
|
+
let email: string | null = null
|
|
29
|
+
try {
|
|
30
|
+
email = (await authed.client.accessStatus()).email
|
|
31
|
+
} catch {
|
|
32
|
+
// Best-effort: the empty-state copy still points at /support without it.
|
|
33
|
+
}
|
|
34
|
+
deps.io.out(
|
|
35
|
+
`No devices yet. Install Notifai from ${supportUrl}, ${sameAccountSignInLine(email)}, and allow notifications.`,
|
|
36
|
+
)
|
|
37
|
+
return EXIT.ok
|
|
38
|
+
}
|
|
39
|
+
for (const d of result.devices) {
|
|
40
|
+
deps.io.out(
|
|
41
|
+
`${d.device_id} ${d.display_name} ${d.platform} ${d.status_message ?? 'Working'}`,
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
return EXIT.ok
|
|
45
|
+
} catch (err) {
|
|
46
|
+
return reportError(deps, err)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Devices as data, for surfaces that render them themselves.
|
|
52
|
+
*
|
|
53
|
+
* `devicesCommand` prints and returns an exit code, which is the right shape
|
|
54
|
+
* for a command and the wrong one for a menu that wants to show names beside
|
|
55
|
+
* checkboxes. Silent on every failure by design: the interactive app already
|
|
56
|
+
* shows the credential and connectivity state on its status card, so a second
|
|
57
|
+
* error line here would be reporting the same fault twice.
|
|
58
|
+
*/
|
|
59
|
+
export async function deviceInventory(deps: CommandDeps): Promise<RoutableDevice[] | null> {
|
|
60
|
+
if (!deps.store.load()) return null
|
|
61
|
+
const config = loadLoggedConfig(deps, { cwd: deps.cwd, env: deps.env })
|
|
62
|
+
const authed = authedClient(deps, config)
|
|
63
|
+
if (!authed) return null
|
|
64
|
+
try {
|
|
65
|
+
return (await authed.client.listDevices()).devices
|
|
66
|
+
} catch {
|
|
67
|
+
return null
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Whether a device is in a state where a notification would actually arrive. */
|
|
72
|
+
export function canDeviceReceive(device: RoutableDevice): boolean {
|
|
73
|
+
return deviceCanReceive(device)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export async function capabilitiesCommand(
|
|
77
|
+
deps: CommandDeps,
|
|
78
|
+
flags: { json?: boolean; platform?: Platform },
|
|
79
|
+
): Promise<number> {
|
|
80
|
+
// Locally, and with the same message `send` gives. Spending a round trip to
|
|
81
|
+
// have the server answer "Request validation failed" told the caller neither
|
|
82
|
+
// which flag was wrong nor what it accepts.
|
|
83
|
+
if (flags.platform !== undefined && !(PLATFORMS as readonly string[]).includes(flags.platform)) {
|
|
84
|
+
deps.io.err(`Unknown platform "${flags.platform}" — use ${PLATFORMS.join(' or ')}.`)
|
|
85
|
+
return EXIT.usage
|
|
86
|
+
}
|
|
87
|
+
const config = loadLoggedConfig(deps, { cwd: deps.cwd, env: deps.env })
|
|
88
|
+
const credential = deps.store.load()
|
|
89
|
+
const baseUrl = resolvedBaseUrl(config, credential)
|
|
90
|
+
const client = makeClient(deps, baseUrl, null)
|
|
91
|
+
try {
|
|
92
|
+
const doc = await client.capabilities(flags.platform ?? 'ios')
|
|
93
|
+
if (flags.json) {
|
|
94
|
+
deps.io.out(JSON.stringify(doc, null, 2))
|
|
95
|
+
return EXIT.ok
|
|
96
|
+
}
|
|
97
|
+
deps.io.out(`${doc.platform} capability contract v${doc.schema_version} (payload limit ${doc.payload_limit_bytes} bytes)`)
|
|
98
|
+
for (const field of doc.fields) {
|
|
99
|
+
deps.io.out(` ${field.path}: ${field.status}${field.reason ? ` — ${field.reason}` : ''}`)
|
|
100
|
+
}
|
|
101
|
+
return EXIT.ok
|
|
102
|
+
} catch (err) {
|
|
103
|
+
return reportError(deps, err)
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function supportPageUrl(baseUrl: string): string {
|
|
108
|
+
return `${baseUrl.replace(/\/$/, '')}/support`
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Same-account line for the device hop; prefers the real email when known. */
|
|
112
|
+
export function sameAccountSignInLine(email: string | null | undefined): string {
|
|
113
|
+
return email
|
|
114
|
+
? `sign in with the same email as this account (${email})`
|
|
115
|
+
: 'sign in with the same email as this account'
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function deviceInstallRemedy(options: {
|
|
119
|
+
baseUrl: string
|
|
120
|
+
email: string | null
|
|
121
|
+
devices: readonly RoutableDevice[]
|
|
122
|
+
}): string {
|
|
123
|
+
const support = supportPageUrl(options.baseUrl)
|
|
124
|
+
const sameEmail = sameAccountSignInLine(options.email)
|
|
125
|
+
if (options.devices.length === 0) {
|
|
126
|
+
return (
|
|
127
|
+
`open the install steps at ${support} on your iPhone, ` +
|
|
128
|
+
`install Notifai, ${sameEmail}, and allow notifications`
|
|
129
|
+
)
|
|
130
|
+
}
|
|
131
|
+
if (options.devices.some((d) => d.permission_status === 'denied')) {
|
|
132
|
+
return 'allow notifications for Notifai in iPhone Settings'
|
|
133
|
+
}
|
|
134
|
+
return 'open Notifai on your iPhone and allow its notification prompt'
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function deviceCanReceive(device: RoutableDevice): boolean {
|
|
138
|
+
const receiveIsFloored =
|
|
139
|
+
device.support?.state === 'must_update' &&
|
|
140
|
+
device.support.affected_operation === 'receive_notifications'
|
|
141
|
+
return (
|
|
142
|
+
!receiveIsFloored &&
|
|
143
|
+
device.registration_healthy &&
|
|
144
|
+
(device.permission_status === 'authorized' || device.permission_status === 'provisional')
|
|
145
|
+
)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export function readyIosDevices(devices: readonly RoutableDevice[]): RoutableDevice[] {
|
|
149
|
+
return devices.filter((device) => device.platform === 'ios' && deviceCanReceive(device))
|
|
150
|
+
}
|