@folotoy/folotoy-openclaw-plugin 0.7.0 → 0.8.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,195 +0,0 @@
1
- import { execSync } from 'node:child_process'
2
- import qrcode from 'qrcode-terminal'
3
- import { DEFAULT_MQTT_HOST, DEFAULT_MQTT_PORT } from '../config.js'
4
- import { getInstallSpec, getPluginName } from './package-info.js'
5
- import { loadPreset, listPresets, type Preset } from './preset.js'
6
-
7
- const PAIR_API_BASE = process.env.PAIR_API_BASE ?? 'https://pair.folotoy.cn'
8
- const POLL_INTERVAL_MS = 3000
9
- const POLL_TIMEOUT_MS = 300_000 // 5 minutes
10
-
11
- // ── Types ──────────────────────────────────────────────
12
-
13
- type CreateSessionResponse = {
14
- session_id: string
15
- pair_url: string
16
- expires_at: string
17
- }
18
-
19
- type PollResponse =
20
- | { status: 'pending' }
21
- | { status: 'completed'; toy_sn: string; toy_key: string; mqtt_host?: string; mqtt_port?: number }
22
- | { status: 'expired' }
23
-
24
- // ── Helpers ────────────────────────────────────────────
25
-
26
- function checkOpenClaw(): void {
27
- try {
28
- execSync('openclaw --version', { stdio: 'pipe' })
29
- } catch {
30
- console.error('Error: openclaw is not installed or not in PATH.')
31
- console.error('Install it first: npm i -g openclaw')
32
- process.exit(1)
33
- }
34
- }
35
-
36
- function installPlugin(): void {
37
- try {
38
- const list = execSync('openclaw plugins list', { stdio: 'pipe' }).toString()
39
- if (list.includes('folotoy-openclaw-plugin')) {
40
- return // already installed
41
- }
42
- } catch {
43
- // ignore
44
- }
45
- const spec = getInstallSpec()
46
- console.log(`Installing FoloToy plugin (${spec})...`)
47
- execSync(`openclaw plugins install ${spec}`, { stdio: 'inherit' })
48
- }
49
-
50
- async function createSession(): Promise<CreateSessionResponse> {
51
- const res = await fetch(`${PAIR_API_BASE}/api/pair`, { method: 'POST' })
52
- if (!res.ok) throw new Error(`Failed to create pairing session (HTTP ${res.status})`)
53
- return res.json() as Promise<CreateSessionResponse>
54
- }
55
-
56
- function displayQR(url: string): void {
57
- qrcode.generate(url, { small: true }, (qr: string) => {
58
- console.log(qr)
59
- })
60
- console.log(`Or open this URL on your phone: ${url}\n`)
61
- }
62
-
63
- async function sleep(ms: number): Promise<void> {
64
- return new Promise((r) => setTimeout(r, ms))
65
- }
66
-
67
- async function pollSession(sessionId: string): Promise<PollResponse & { status: 'completed' }> {
68
- const deadline = Date.now() + POLL_TIMEOUT_MS
69
- const frames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']
70
- let i = 0
71
-
72
- while (Date.now() < deadline) {
73
- process.stdout.write(`\r${frames[i++ % frames.length]} Waiting for pairing...`)
74
-
75
- const res = await fetch(`${PAIR_API_BASE}/api/pair/${sessionId}`)
76
- if (!res.ok) throw new Error(`Poll failed (HTTP ${res.status})`)
77
- const data = (await res.json()) as PollResponse
78
-
79
- if (data.status === 'completed') {
80
- process.stdout.write('\r\x1b[32m✓\x1b[0m Paired successfully! \n')
81
- return data as PollResponse & { status: 'completed' }
82
- }
83
- if (data.status === 'expired') {
84
- process.stdout.write('\r')
85
- throw new Error('Pairing session expired. Please try again.')
86
- }
87
-
88
- await sleep(POLL_INTERVAL_MS)
89
- }
90
- throw new Error('Pairing timed out after 5 minutes.')
91
- }
92
-
93
- function restartGateway(): void {
94
- try {
95
- execSync('openclaw gateway restart', { stdio: 'inherit' })
96
- } catch {
97
- console.warn('⚠ Failed to restart gateway. You can restart manually: openclaw gateway restart')
98
- }
99
- }
100
-
101
- function writeConfig(
102
- result: { toy_sn: string; toy_key: string; mqtt_host?: string; mqtt_port?: number },
103
- preset: Preset = {},
104
- ): void {
105
- execSync(`openclaw config set channels.folotoy.flow direct`, { stdio: 'pipe' })
106
- execSync(`openclaw config set channels.folotoy.toy_sn ${result.toy_sn}`, { stdio: 'pipe' })
107
- execSync(`openclaw config set channels.folotoy.toy_key ${result.toy_key}`, { stdio: 'pipe' })
108
-
109
- const mqttHost = result.mqtt_host ?? DEFAULT_MQTT_HOST
110
- const mqttPort = result.mqtt_port ?? DEFAULT_MQTT_PORT
111
- execSync(`openclaw config set channels.folotoy.mqtt_host ${mqttHost}`, { stdio: 'pipe' })
112
- execSync(`openclaw config set channels.folotoy.mqtt_port ${mqttPort}`, { stdio: 'pipe' })
113
-
114
- for (const [key, value] of Object.entries(preset)) {
115
- execSync(`openclaw config set channels.folotoy.${key} ${value}`, { stdio: 'pipe' })
116
- }
117
- }
118
-
119
- function parsePresetArg(argv: readonly string[]): string | undefined {
120
- for (let i = 0; i < argv.length; i++) {
121
- const arg = argv[i]
122
- if (arg === '--preset') {
123
- const value = argv[i + 1]
124
- if (!value || value.startsWith('--')) {
125
- throw new Error('--preset requires a name (e.g. --preset single-soothing)')
126
- }
127
- return value
128
- }
129
- if (arg && arg.startsWith('--preset=')) {
130
- return arg.slice('--preset='.length)
131
- }
132
- }
133
- return undefined
134
- }
135
-
136
- // ── Main ───────────────────────────────────────────────
137
-
138
- async function main() {
139
- const argv = process.argv.slice(2)
140
- const command = argv[0]
141
-
142
- if (command !== 'install') {
143
- const presets = listPresets()
144
- console.log(`Usage: npx ${getPluginName()} install [--preset <name>]`)
145
- if (presets.length) console.log(`Available presets: ${presets.join(', ')}`)
146
- process.exit(command ? 1 : 0)
147
- }
148
-
149
- // Resolve preset before any side effects (pairing, plugin install) so a bad
150
- // preset name fails fast without making the user scan a QR.
151
- const presetName = parsePresetArg(argv.slice(1))
152
- const preset: Preset = presetName ? loadPreset(presetName) : {}
153
-
154
- console.log('🧸 FoloToy OpenClaw Plugin Installer\n')
155
- if (presetName) {
156
- console.log(`Using preset: ${presetName} → ${JSON.stringify(preset)}\n`)
157
- }
158
-
159
- // Step 1: check prerequisites
160
- console.log('Checking openclaw...')
161
- checkOpenClaw()
162
- console.log('✓ openclaw found\n')
163
-
164
- // Step 2: install plugin if not present
165
- installPlugin()
166
-
167
- // Step 3: create pairing session
168
- console.log('Creating pairing session...\n')
169
- const session = await createSession()
170
-
171
- // Step 4: display QR code
172
- console.log('Scan this QR code with your phone,')
173
- console.log('then scan your toy\'s QR code on the phone:\n')
174
- displayQR(session.pair_url)
175
-
176
- // Step 5: poll for result
177
- const result = await pollSession(session.session_id)
178
-
179
- // Step 6: write config
180
- console.log('\nWriting configuration...')
181
- writeConfig(result, preset)
182
-
183
- // Step 7: restart gateway
184
- console.log('\nRestarting gateway...')
185
- restartGateway()
186
-
187
- // Step 8: done
188
- console.log('\n\x1b[32m✓ FoloToy plugin installed and configured!\x1b[0m')
189
- console.log(` Toy SN: ${result.toy_sn}`)
190
- }
191
-
192
- main().catch((err) => {
193
- console.error(`\n\x1b[31mError:\x1b[0m ${err instanceof Error ? err.message : String(err)}`)
194
- process.exit(1)
195
- })
@@ -1,48 +0,0 @@
1
- import { existsSync, readFileSync } from 'node:fs'
2
- import { dirname, join } from 'node:path'
3
- import { fileURLToPath } from 'node:url'
4
-
5
- export function findPackageRoot(): string {
6
- let dir = dirname(fileURLToPath(import.meta.url))
7
- while (true) {
8
- if (existsSync(join(dir, 'package.json'))) return dir
9
- const parent = dirname(dir)
10
- if (parent === dir) throw new Error('package.json not found while resolving package root')
11
- dir = parent
12
- }
13
- }
14
-
15
- type PackageInfo = { name: string; version: string }
16
-
17
- let cached: PackageInfo | undefined
18
-
19
- function readPackageInfo(): PackageInfo {
20
- if (cached) return cached
21
- const pkg = JSON.parse(
22
- readFileSync(join(findPackageRoot(), 'package.json'), 'utf8'),
23
- ) as { name?: unknown; version?: unknown }
24
- if (typeof pkg.name !== 'string' || typeof pkg.version !== 'string') {
25
- throw new Error('package.json is missing name or version')
26
- }
27
- cached = { name: pkg.name, version: pkg.version }
28
- return cached
29
- }
30
-
31
- export function getPluginName(): string {
32
- return readPackageInfo().name
33
- }
34
-
35
- export function getPluginVersion(): string {
36
- return readPackageInfo().version
37
- }
38
-
39
- /**
40
- * Returns the npm spec string `<name>@<version>` to pass to
41
- * `openclaw plugins install`. Pinning the exact version prevents OpenClaw
42
- * from rejecting the install when `latest` happens to point at a prerelease
43
- * — without this, `openclaw plugins install <bare-name>` resolves through
44
- * the `latest` dist-tag and refuses prereleases for safety.
45
- */
46
- export function getInstallSpec(): string {
47
- return `${getPluginName()}@${getPluginVersion()}`
48
- }
package/src/cli/preset.ts DELETED
@@ -1,54 +0,0 @@
1
- import { existsSync, readFileSync, readdirSync } from 'node:fs'
2
- import { join } from 'node:path'
3
- import { findPackageRoot } from './package-info.js'
4
-
5
- export const PRESET_WHITELIST = ['soothing_loop_enabled'] as const
6
- export type PresetKey = (typeof PRESET_WHITELIST)[number]
7
- export type Preset = Partial<Record<PresetKey, boolean>>
8
-
9
- function presetDir(): string {
10
- return join(findPackageRoot(), 'src', 'presets')
11
- }
12
-
13
- export function listPresets(dir: string = presetDir()): string[] {
14
- if (!existsSync(dir)) return []
15
- return readdirSync(dir)
16
- .filter((f) => f.endsWith('.json'))
17
- .map((f) => f.slice(0, -'.json'.length))
18
- .sort()
19
- }
20
-
21
- export function loadPreset(name: string, dir: string = presetDir()): Preset {
22
- const file = join(dir, `${name}.json`)
23
- if (!existsSync(file)) {
24
- const available = listPresets(dir)
25
- throw new Error(
26
- `Preset "${name}" not found. Available: ${available.length ? available.join(', ') : '(none)'}`,
27
- )
28
- }
29
-
30
- let parsed: unknown
31
- try {
32
- parsed = JSON.parse(readFileSync(file, 'utf8'))
33
- } catch (err) {
34
- throw new Error(`Preset "${name}" is not valid JSON: ${(err as Error).message}`)
35
- }
36
-
37
- if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
38
- throw new Error(`Preset "${name}" must be a JSON object`)
39
- }
40
-
41
- const allowed = PRESET_WHITELIST as readonly string[]
42
- const result: Preset = {}
43
- for (const [key, value] of Object.entries(parsed as Record<string, unknown>)) {
44
- if (!allowed.includes(key)) {
45
- throw new Error(`Preset "${name}" has unknown key "${key}". Allowed: ${PRESET_WHITELIST.join(', ')}`)
46
- }
47
- if (typeof value !== 'boolean') {
48
- throw new Error(`Preset "${name}" key "${key}" must be a boolean (got ${typeof value})`)
49
- }
50
- result[key as PresetKey] = value
51
- }
52
-
53
- return result
54
- }
@@ -1,3 +0,0 @@
1
- declare module 'qrcode-terminal' {
2
- export function generate(text: string, opts?: { small?: boolean }, cb?: (qr: string) => void): void
3
- }
@@ -1,3 +0,0 @@
1
- {
2
- "soothing_loop_enabled": false
3
- }