omni-notify-mcp 1.3.384 → 1.3.386

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,88 @@
1
+ /**
2
+ * The agent stream (#3402): the live feed of a conversation's agent — its thinking and its output as they arrive, told
3
+ * apart — that an MCP caller attaches to instead of waiting for the finished reply alone. The session driver appends a
4
+ * frame for every delta the CLI's own `stream_event` carries (`claudeProvider.ts` reads them; the CLI is started with
5
+ * `--include-partial-messages`), beside a frame for the tool the turn is on and one for each discrete conversation
6
+ * event, so a reader sees the whole turn in arrival order from one stream.
7
+ *
8
+ * The stream is served over the SAME authenticated gateway door as everything else (a Server-Sent Events route in
9
+ * `restDoor.ts`) and is owned by the person, exactly as a native terminal is (#3370): the owner is the door's own
10
+ * `personId()` and another person's key is refused with 404.
11
+ *
12
+ * A frame's `seq` is monotonic per conversation and a reader resumes with `after` = the last `seq` it read, so a
13
+ * dropped client that re-attaches misses nothing it had not already received; frames are held for the conversation's
14
+ * life and freed when it is deleted. The feed is live and in memory, not a durable transcript: a BN restart empties it
15
+ * and its `seq` starts again, so a re-attach with a `seq` from before a restart resumes within the new stream — the
16
+ * finished turn stays readable through the conversation's own log.
17
+ *
18
+ * NOTHING IS CAPPED HERE — no buffer size, no retained-history window and no idle bound is applied, because every such
19
+ * bound is a gate that is Meni's to rule on before it ships (the proposal sits on #3402's decision field).
20
+ */
21
+ import { EventEmitter } from "events";
22
+ import { GatewayError, MAX_WAIT_SECONDS, readConversation } from "./conversations.js";
23
+ /**
24
+ * One conversation's live stream: the frames appended in order, the owner it belongs to, and the things waiting on a
25
+ * new frame. It is created by the session driver the moment the conversation's turn produces anything and bound to a
26
+ * person the first time the door serves it to one.
27
+ */
28
+ class AgentStream {
29
+ /** The person this stream belongs to, fixed by the first sign-in that reads it (#3370); absent until then. */
30
+ owner;
31
+ seq = 0;
32
+ frames = [];
33
+ changed = new EventEmitter();
34
+ /** Appends one frame and wakes every reader; the new frame's own `seq` is returned so a caller can echo it back as `after`. */
35
+ append(kind, text, turn, rest = {}) {
36
+ const frame = { seq: ++this.seq, ts: new Date().toISOString(), kind, text, turn, ...rest };
37
+ this.frames.push(frame);
38
+ this.changed.emit("change");
39
+ return frame;
40
+ }
41
+ /**
42
+ * The frames past `after`, or — a reader with no position — the whole stream so far; `after` is the last `seq` the
43
+ * reader had, so what it returns is exactly what the reader has not seen. `holdMs` bounds the wait for a new frame:
44
+ * the SSE route holds `MAX_WAIT_SECONDS` (the same cap every blocking BN call obeys) and writes each frame the moment
45
+ * it is produced instead of polling, while the MCP read a tool-only caller polls with is given 0 and answers at once.
46
+ */
47
+ async read(after, signal, holdMs = MAX_WAIT_SECONDS * 1000) {
48
+ for (;;) {
49
+ const frames = after === undefined ? this.frames.slice() : this.frames.filter((frame) => frame.seq > after);
50
+ if (frames.length > 0 || signal.aborted || holdMs === 0)
51
+ return { frames, seq: this.seq };
52
+ await new Promise((resolve) => {
53
+ const done = () => { clearTimeout(timer); this.changed.off("change", done); signal.removeEventListener("abort", done); resolve(); };
54
+ const timer = setTimeout(done, holdMs);
55
+ this.changed.once("change", done);
56
+ signal.addEventListener("abort", done, { once: true });
57
+ });
58
+ }
59
+ }
60
+ }
61
+ const streams = new Map();
62
+ /** This conversation's stream, made if it is not there yet: the session driver appends to it from the first frame of a turn. */
63
+ export function agentStream(id) {
64
+ const existing = streams.get(id);
65
+ if (existing !== undefined)
66
+ return existing;
67
+ const stream = new AgentStream();
68
+ streams.set(id, stream);
69
+ return stream;
70
+ }
71
+ /**
72
+ * The stream of `id` as `owner`'s, for the door: the conversation must exist, and the stream belongs to the first person
73
+ * who reads it — another person's key is refused with 404, never handed someone else's stream. Every later reader of the
74
+ * same person is served the same stream, so a re-attach resumes it.
75
+ */
76
+ export function agentStreamFor(owner, id) {
77
+ readConversation(id);
78
+ const stream = agentStream(id);
79
+ if (stream.owner === undefined)
80
+ stream.owner = owner;
81
+ else if (stream.owner !== owner)
82
+ throw new GatewayError(404, "No agent stream for this person.");
83
+ return stream;
84
+ }
85
+ /** The conversation is gone: its stream and every frame it held are dropped. */
86
+ export function dropAgentStream(id) {
87
+ streams.delete(id);
88
+ }
@@ -0,0 +1,210 @@
1
+ // The foyer-pa RPC client: builds positional `application/json+protobuf` arrays (via foyer-codec),
2
+ // POSTs them to Google's Home Graph foyer endpoint, mints the bearer from the master token
3
+ // (gpsoauth), and retries once on 401/403 while advancing scope negotiation.
4
+ // Ported from coredevices/pebble-plugins `googlehome/src/foyer-client.ts` (which names the Kotlin
5
+ // `FoyerRpcClient`/`FoyerAuthImpl` as its origin).
6
+ import * as codec from './foyer-codec.js';
7
+ import { FoyerAuthError } from './gpsoauth.js';
8
+ const BASE = 'https://googlehomefoyer-pa.clients6.google.com/$rpc/google.internal.home.foyer.v1.';
9
+ const HOME_CONTROL_SERVICE = 'HomeControlService';
10
+ const STRUCTURES_SERVICE = 'StructuresService';
11
+ const AUTOMATION_SERVICE = 'AutomationService';
12
+ const CONTENT_TYPE = 'application/json+protobuf';
13
+ // The (app, client_sig, scope) triple that yields a bearer foyer-pa accepts.
14
+ const APP = 'com.google.android.apps.chromecast.app';
15
+ const CLIENT_SIG = '24bb24c05e47e0aefa68a58a766179d9b613a600';
16
+ const SCOPE_CANDIDATES = [
17
+ 'oauth2:https://www.googleapis.com/auth/homegraph',
18
+ 'oauth2:https://www.google.com/accounts/OAuthLogin',
19
+ ];
20
+ /** A refusal the calling agent should read: no structure, a routine that cannot be started, … */
21
+ export class GoogleHomeError extends Error {
22
+ }
23
+ export class FoyerHttpError extends GoogleHomeError {
24
+ status;
25
+ constructor(status, message) {
26
+ super(message);
27
+ this.status = status;
28
+ }
29
+ }
30
+ /** Strips a leading XSSI guard line (`)]}'`) some clients6 endpoints prepend. */
31
+ export function stripXssiPrefix(body) {
32
+ const trimmed = body.replace(/^\s+/, '');
33
+ if (trimmed.startsWith(")]}'")) {
34
+ return trimmed.substring(4).replace(/^[\r\n]+/, '');
35
+ }
36
+ return body;
37
+ }
38
+ /** Mints + caches short-lived foyer bearers from the master token, with scope negotiation. */
39
+ export class FoyerAuth {
40
+ masterToken;
41
+ androidId;
42
+ oauth;
43
+ scopes;
44
+ app;
45
+ clientSig;
46
+ fallbackTtlSeconds;
47
+ expirySkewSeconds;
48
+ nowEpochSeconds;
49
+ cachedToken = null;
50
+ expiresAtEpoch = 0;
51
+ scopeIndex = 0;
52
+ constructor(masterToken, androidId, oauth, scopes = SCOPE_CANDIDATES, app = APP, clientSig = CLIENT_SIG, fallbackTtlSeconds = 55 * 60, expirySkewSeconds = 60, nowEpochSeconds = () => Math.floor(Date.now() / 1000)) {
53
+ this.masterToken = masterToken;
54
+ this.androidId = androidId;
55
+ this.oauth = oauth;
56
+ this.scopes = scopes;
57
+ this.app = app;
58
+ this.clientSig = clientSig;
59
+ this.fallbackTtlSeconds = fallbackTtlSeconds;
60
+ this.expirySkewSeconds = expirySkewSeconds;
61
+ this.nowEpochSeconds = nowEpochSeconds;
62
+ }
63
+ async bearer() {
64
+ const now = this.nowEpochSeconds();
65
+ if (this.cachedToken != null && now < this.expiresAtEpoch)
66
+ return this.cachedToken;
67
+ return this.mint(now);
68
+ }
69
+ invalidate() {
70
+ this.cachedToken = null;
71
+ this.expiresAtEpoch = 0;
72
+ this.scopeIndex = this.scopes.length === 0 ? 0 : (this.scopeIndex + 1) % this.scopes.length;
73
+ }
74
+ async mint(now) {
75
+ if (this.scopes.length === 0)
76
+ throw new FoyerAuthError('no foyer scope candidates configured; cannot mint a bearer');
77
+ let lastError = null;
78
+ for (let offset = 0; offset < this.scopes.length; offset++) {
79
+ const i = (this.scopeIndex + offset) % this.scopes.length;
80
+ try {
81
+ const token = await this.oauth.getAuthToken(this.masterToken, this.androidId, this.scopes[i], this.app, this.clientSig);
82
+ this.scopeIndex = i;
83
+ this.cachedToken = token.auth;
84
+ this.expiresAtEpoch = this.deadlineFor(token.expiryEpochSeconds, now);
85
+ return token.auth;
86
+ }
87
+ catch (e) {
88
+ lastError = e instanceof Error ? e : new FoyerAuthError(String(e));
89
+ }
90
+ }
91
+ throw lastError instanceof Error ? lastError : new FoyerAuthError('no foyer scope candidates could be minted');
92
+ }
93
+ deadlineFor(expiryEpochSeconds, now) {
94
+ const cap = now + this.fallbackTtlSeconds;
95
+ const base = expiryEpochSeconds != null && expiryEpochSeconds > now ? Math.min(expiryEpochSeconds, cap) : cap;
96
+ return base - this.expirySkewSeconds;
97
+ }
98
+ }
99
+ export class FoyerClient {
100
+ auth;
101
+ constructor(auth) {
102
+ this.auth = auth;
103
+ }
104
+ // Reads ---------------------------------------------------------------------------------------
105
+ async fetchHomeGraph() {
106
+ const resp = await this.rpc(STRUCTURES_SERVICE, 'GetHomeGraph', []);
107
+ return codec.parseGetHomeGraph(resp);
108
+ }
109
+ async getHomeGraph() {
110
+ const g = await this.fetchHomeGraph();
111
+ return { homes: g.homes, devices: g.devices };
112
+ }
113
+ async listRooms() {
114
+ return (await this.fetchHomeGraph()).rooms;
115
+ }
116
+ async getTraits(deviceIds) {
117
+ if (deviceIds.length === 0)
118
+ return [];
119
+ const resp = await this.rpc(HOME_CONTROL_SERVICE, 'GetTraits', codec.buildGetTraitsRequest(deviceIds));
120
+ return codec.parseGetTraits(resp);
121
+ }
122
+ // Control (existing devices only; no add/delete) ----------------------------------------------
123
+ async setOnOff(deviceId, agentId, partnerDeviceId, on) {
124
+ return this.update(deviceId, codec.buildOnOff(deviceId, agentId, partnerDeviceId, on), on);
125
+ }
126
+ async setBrightness(deviceId, agentId, partnerDeviceId, pct) {
127
+ return this.update(deviceId, codec.buildBrightness(deviceId, agentId, partnerDeviceId, clamp(pct, 0, 100)));
128
+ }
129
+ async setColorTemperature(deviceId, agentId, partnerDeviceId, kelvin) {
130
+ return this.update(deviceId, codec.buildColorTemperature(deviceId, agentId, partnerDeviceId, kelvin));
131
+ }
132
+ async setVolume(deviceId, agentId, partnerDeviceId, pct) {
133
+ return this.update(deviceId, codec.buildVolume(deviceId, agentId, partnerDeviceId, clamp(pct, 0, 100)));
134
+ }
135
+ async setMuted(deviceId, agentId, partnerDeviceId, muted) {
136
+ return this.update(deviceId, codec.buildMuted(deviceId, agentId, partnerDeviceId, muted));
137
+ }
138
+ async listAutomations() {
139
+ const structureId = (await this.fetchHomeGraph()).homes[0]?.id;
140
+ if (structureId == null)
141
+ throw new GoogleHomeError('no home/structure found; cannot list routines');
142
+ const resp = await this.rpc(AUTOMATION_SERVICE, 'ListAutomations', codec.buildListAutomationsRequest(structureId));
143
+ return codec.parseListAutomations(resp);
144
+ }
145
+ async runAutomation(automation) {
146
+ if (!automation.manuallyRunnable) {
147
+ throw new GoogleHomeError(`routine '${automation.name}' is condition/schedule-triggered and can't be started on demand`);
148
+ }
149
+ const structureId = (await this.fetchHomeGraph()).homes[0]?.id;
150
+ if (structureId == null)
151
+ throw new GoogleHomeError('no home/structure found; cannot run a routine');
152
+ const resp = await this.rpc(AUTOMATION_SERVICE, 'ExecuteAutomation', codec.buildExecuteAutomationRequest(structureId, automation.id));
153
+ return Array.isArray(resp) && resp.length > 0 && String(resp[0]) === '1';
154
+ }
155
+ // Internals -----------------------------------------------------------------------------------
156
+ async update(deviceId, body, fallbackOnOff) {
157
+ const resp = await this.rpc(HOME_CONTROL_SERVICE, 'UpdateTraits', body);
158
+ const echoed = codec.parseGetTraits(resp).find((s) => s.id === deviceId);
159
+ if (echoed)
160
+ return echoed;
161
+ const readback = (await this.getTraits([deviceId])).find((s) => s.id === deviceId);
162
+ if (readback)
163
+ return readback;
164
+ return {
165
+ id: deviceId,
166
+ online: true,
167
+ onOff: fallbackOnOff ?? null,
168
+ brightnessPct: null,
169
+ colorTemperatureK: null,
170
+ volumePct: null,
171
+ muted: null,
172
+ locked: null,
173
+ jammed: null,
174
+ thermostatMode: null,
175
+ setpointC: null,
176
+ setpointF: null,
177
+ ambientC: null,
178
+ temperatureUnit: null,
179
+ };
180
+ }
181
+ /** POSTs body to `<BASE><service>/<method>`; on 401/403 invalidates + retries once. */
182
+ async rpc(service, method, body) {
183
+ const url = `${BASE}${service}/${method}`;
184
+ const bodyJson = JSON.stringify(body);
185
+ let response = await this.post(url, bodyJson, await this.auth.bearer());
186
+ if (response.status === 401 || response.status === 403) {
187
+ this.auth.invalidate();
188
+ response = await this.post(url, bodyJson, await this.auth.bearer());
189
+ }
190
+ if (response.status < 200 || response.status > 299) {
191
+ throw new FoyerHttpError(response.status, `google home ${service}/${method} -> HTTP ${response.status}: ${response.body.substring(0, 300)}`);
192
+ }
193
+ return JSON.parse(stripXssiPrefix(response.body));
194
+ }
195
+ async post(url, bodyJson, bearer) {
196
+ const resp = await fetch(url, {
197
+ method: 'POST',
198
+ headers: {
199
+ Authorization: `Bearer ${bearer}`,
200
+ 'X-User-Agent': 'grpc-web-javascript/0.1',
201
+ 'Content-Type': CONTENT_TYPE,
202
+ },
203
+ body: bodyJson,
204
+ });
205
+ return { status: resp.status, body: await resp.text() };
206
+ }
207
+ }
208
+ function clamp(n, lo, hi) {
209
+ return Math.max(lo, Math.min(hi, n));
210
+ }
@@ -0,0 +1,318 @@
1
+ // Builds and parses the foyer-pa `application/json+protobuf` wire format.
2
+ //
3
+ // The service does NOT use JSON objects — every request/response body is a *positional* JSON array
4
+ // where protobuf field numbers map to array indices (1-based, with `null` holes for absent fields).
5
+ // Ported from coredevices/pebble-plugins `googlehome/src/foyer-codec.ts` (which names the Kotlin
6
+ // `FoyerCodec` as its origin); the field indices were derived from live captures.
7
+ //
8
+ // Value encoding (the scalar wrapper `[i0,i1,i2,i3,…]`): a value sits at a type-specific index:
9
+ // - int / float -> index 1: [null, <num>]
10
+ // - string / enum-> index 2: [null, null, "<str>"]
11
+ // - bool -> index 3: [null, null, null, <0|1>]
12
+ /** The type used for matching: user-assigned type when set, else hardware type. */
13
+ export function effectiveType(d) {
14
+ return d.assignedType && d.assignedType.trim() !== '' ? d.assignedType : d.type;
15
+ }
16
+ // -------------------------------------------------------------------------------------------------
17
+ // Null-safe positional-array accessors (out-of-range / wrong-typed slot -> null)
18
+ // -------------------------------------------------------------------------------------------------
19
+ function arr(x) {
20
+ return Array.isArray(x) ? x : null;
21
+ }
22
+ function at(a, i) {
23
+ return a != null && i >= 0 && i < a.length ? a[i] : null;
24
+ }
25
+ function str(x) {
26
+ return typeof x === 'string' ? x : null;
27
+ }
28
+ function boolAt(a, i) {
29
+ const e = at(a, i);
30
+ if (typeof e === 'boolean')
31
+ return e;
32
+ return e === 1;
33
+ }
34
+ function numAt(a, i) {
35
+ const e = at(a, i);
36
+ return typeof e === 'number' ? e : null;
37
+ }
38
+ function strAt(a, i) {
39
+ return str(at(a, i));
40
+ }
41
+ function arrAt(a, i) {
42
+ return arr(at(a, i));
43
+ }
44
+ // -------------------------------------------------------------------------------------------------
45
+ // Scalar wrappers. In JS there is no int/float distinction, so intWrapper is just [null, n].
46
+ // -------------------------------------------------------------------------------------------------
47
+ function intWrapper(n) {
48
+ return [null, n];
49
+ }
50
+ function boolWrapper(b) {
51
+ return [null, null, null, b ? 1 : 0];
52
+ }
53
+ /** Flattens a trait body `[[fieldName,<wrapper>],…]` into a `fieldName -> wrapper` map. */
54
+ function fieldWrappers(body) {
55
+ const out = new Map();
56
+ if (body == null)
57
+ return out;
58
+ for (const fieldEl of body) {
59
+ const field = arr(fieldEl);
60
+ const name = strAt(field, 0);
61
+ if (name == null)
62
+ continue;
63
+ out.set(name, arrAt(field, 1));
64
+ }
65
+ return out;
66
+ }
67
+ // -------------------------------------------------------------------------------------------------
68
+ // GetTraits
69
+ // -------------------------------------------------------------------------------------------------
70
+ /** Request body for `HomeControlService/GetTraits`: `[[["id1"],["id2"],…]]`. */
71
+ export function buildGetTraitsRequest(ids) {
72
+ return [ids.map((id) => [id])];
73
+ }
74
+ export function parseGetTraits(json) {
75
+ const results = arrAt(arr(json), 0);
76
+ if (results == null)
77
+ return [];
78
+ const out = [];
79
+ for (const result of results) {
80
+ const r = arr(result);
81
+ const id = strAt(arrAt(r, 0), 0);
82
+ if (id == null)
83
+ continue;
84
+ const traits = arrAt(r, 1);
85
+ const s = {
86
+ id,
87
+ online: false,
88
+ onOff: null,
89
+ brightnessPct: null,
90
+ colorTemperatureK: null,
91
+ volumePct: null,
92
+ muted: null,
93
+ locked: null,
94
+ jammed: null,
95
+ thermostatMode: null,
96
+ setpointC: null,
97
+ setpointF: null,
98
+ ambientC: null,
99
+ temperatureUnit: null,
100
+ };
101
+ if (traits != null) {
102
+ for (const entry of traits) {
103
+ const e = arr(entry);
104
+ const fields = fieldWrappers(arrAt(e, 1));
105
+ switch (strAt(e, 0)) {
106
+ case 'deviceStatus':
107
+ if (fields.has('online'))
108
+ s.online = boolAt(fields.get('online') ?? null, 3);
109
+ break;
110
+ case 'onOff':
111
+ if (fields.has('onOff'))
112
+ s.onOff = boolAt(fields.get('onOff') ?? null, 3);
113
+ break;
114
+ case 'brightness':
115
+ s.brightnessPct = numAt(fields.get('brightness') ?? null, 1);
116
+ break;
117
+ case 'color':
118
+ s.colorTemperatureK = numAt(fields.get('colorTemperature') ?? null, 1);
119
+ break;
120
+ case 'volume':
121
+ s.volumePct = numAt(fields.get('currentVolume') ?? null, 1);
122
+ if (fields.has('isMuted'))
123
+ s.muted = boolAt(fields.get('isMuted') ?? null, 3);
124
+ break;
125
+ case 'lockUnlock':
126
+ if (fields.has('isLocked'))
127
+ s.locked = boolAt(fields.get('isLocked') ?? null, 3);
128
+ if (fields.has('isJammed'))
129
+ s.jammed = boolAt(fields.get('isJammed') ?? null, 3);
130
+ break;
131
+ case 'temperatureSetting':
132
+ s.thermostatMode = strAt(fields.get('mode') ?? null, 2);
133
+ s.setpointC = numAt(fields.get('thermostatTemperatureSetpointC') ?? null, 1);
134
+ s.setpointF = numAt(fields.get('thermostatTemperatureSetpointF') ?? null, 1);
135
+ s.ambientC = numAt(fields.get('ambientAirTemperatureC') ?? null, 1);
136
+ // temperatureUnit lives in the trait's params block (index 2), one array deeper.
137
+ s.temperatureUnit = strAt(fieldWrappers(arrAt(arrAt(e, 2), 0)).get('temperatureUnit') ?? null, 2);
138
+ break;
139
+ }
140
+ }
141
+ }
142
+ out.push(s);
143
+ }
144
+ return out;
145
+ }
146
+ // -------------------------------------------------------------------------------------------------
147
+ // UpdateTraits (control)
148
+ // -------------------------------------------------------------------------------------------------
149
+ export function buildUpdate(deviceId, agentId, partnerDeviceId, traitName, fieldName, wrapper) {
150
+ const trait = [traitName, [[fieldName, wrapper]]];
151
+ const deviceKey = [deviceId, [agentId, partnerDeviceId]];
152
+ const command = [deviceKey, [trait]];
153
+ return [[command]];
154
+ }
155
+ export function buildOnOff(deviceId, agentId, partnerDeviceId, on) {
156
+ return buildUpdate(deviceId, agentId, partnerDeviceId, 'onOff', 'onOff', boolWrapper(on));
157
+ }
158
+ export function buildBrightness(deviceId, agentId, partnerDeviceId, pct) {
159
+ return buildUpdate(deviceId, agentId, partnerDeviceId, 'brightness', 'brightness', intWrapper(pct));
160
+ }
161
+ export function buildColorTemperature(deviceId, agentId, partnerDeviceId, kelvin) {
162
+ return buildUpdate(deviceId, agentId, partnerDeviceId, 'color', 'colorTemperature', intWrapper(kelvin));
163
+ }
164
+ export function buildVolume(deviceId, agentId, partnerDeviceId, pct) {
165
+ return buildUpdate(deviceId, agentId, partnerDeviceId, 'volume', 'currentVolume', intWrapper(pct));
166
+ }
167
+ export function buildMuted(deviceId, agentId, partnerDeviceId, muted) {
168
+ return buildUpdate(deviceId, agentId, partnerDeviceId, 'volume', 'isMuted', boolWrapper(muted));
169
+ }
170
+ // -------------------------------------------------------------------------------------------------
171
+ // AutomationService (routines)
172
+ // -------------------------------------------------------------------------------------------------
173
+ /** `ListAutomations` request body: `[<structureId>]`. */
174
+ export function buildListAutomationsRequest(structureId) {
175
+ return [structureId];
176
+ }
177
+ /** `ExecuteAutomation` request body: `[<structureId>, <automationId>, null, 2]`. */
178
+ export function buildExecuteAutomationRequest(structureId, automationId) {
179
+ return [structureId, automationId, null, 2];
180
+ }
181
+ export function parseListAutomations(json) {
182
+ const list = arrAt(arr(json), 0);
183
+ if (list == null)
184
+ return [];
185
+ const out = [];
186
+ for (const recEl of list) {
187
+ const rec = arr(recEl);
188
+ const id = strAt(rec, 0);
189
+ const name = strAt(rec, 3);
190
+ if (id == null || name == null)
191
+ continue;
192
+ out.push({
193
+ id,
194
+ name,
195
+ manuallyRunnable: numAt(rec, 2) === 1,
196
+ starters: strAt(rec, 4),
197
+ actions: strAt(rec, 5),
198
+ triggerInput: strAt(rec, 10),
199
+ });
200
+ }
201
+ return out;
202
+ }
203
+ // -------------------------------------------------------------------------------------------------
204
+ // Capability derivation
205
+ // -------------------------------------------------------------------------------------------------
206
+ export function deriveCapabilities(traits) {
207
+ const caps = new Set();
208
+ for (const raw of traits) {
209
+ const suffix = raw.split('.').pop()?.toLowerCase() ?? '';
210
+ switch (suffix) {
211
+ case 'onoff':
212
+ caps.add('on_off');
213
+ break;
214
+ case 'brightness':
215
+ caps.add('brightness');
216
+ break;
217
+ case 'colorsetting':
218
+ caps.add('color_temperature');
219
+ caps.add('color');
220
+ break;
221
+ case 'volume':
222
+ caps.add('volume');
223
+ break;
224
+ case 'lockunlock':
225
+ caps.add('lock');
226
+ break;
227
+ case 'temperaturesetting':
228
+ case 'temperaturecontrol':
229
+ caps.add('thermostat');
230
+ break;
231
+ case 'transportcontrol':
232
+ case 'mediastate':
233
+ caps.add('media_transport');
234
+ break;
235
+ }
236
+ }
237
+ return caps;
238
+ }
239
+ // -------------------------------------------------------------------------------------------------
240
+ // GetHomeGraph (enumeration)
241
+ // -------------------------------------------------------------------------------------------------
242
+ export function parseGetHomeGraph(json) {
243
+ const homesArr = arrAt(arr(json), 1);
244
+ if (homesArr == null)
245
+ return { homes: [], devices: [], rooms: [] };
246
+ const homes = [];
247
+ const rooms = [];
248
+ const devices = [];
249
+ const deviceRoom = new Map();
250
+ for (const homeEl of homesArr) {
251
+ const home = arr(homeEl);
252
+ const homeId = strAt(home, 0);
253
+ if (homeId == null)
254
+ continue;
255
+ homes.push({ id: homeId, name: strAt(home, 1) ?? '' });
256
+ // Rooms: [5]
257
+ const roomsArr = arrAt(home, 5);
258
+ if (roomsArr != null) {
259
+ for (const roomEl of roomsArr) {
260
+ const room = arr(roomEl);
261
+ const roomId = strAt(room, 0);
262
+ if (roomId == null)
263
+ continue;
264
+ const roomName = strAt(room, 2) ?? '';
265
+ const roomType = strAt(arrAt(room, 3), 0) ?? '';
266
+ const memberIds = [];
267
+ const members = arrAt(room, 4);
268
+ if (members != null) {
269
+ for (const memberEl of members) {
270
+ // member = [[deviceId,[agentId,partnerId]]]
271
+ const devId = strAt(arrAt(arr(memberEl), 0), 0);
272
+ if (devId == null)
273
+ continue;
274
+ memberIds.push(devId);
275
+ deviceRoom.set(devId, roomName);
276
+ }
277
+ }
278
+ rooms.push({ id: roomId, name: roomName, type: roomType, deviceIds: memberIds });
279
+ }
280
+ }
281
+ // Devices: [6]
282
+ const devsArr = arrAt(home, 6);
283
+ if (devsArr != null) {
284
+ for (const devEl of devsArr) {
285
+ const rec = arr(devEl);
286
+ const key = arrAt(rec, 0); // [deviceId,[agentId,partnerId]]
287
+ if (key == null)
288
+ continue;
289
+ const devId = strAt(key, 0);
290
+ if (devId == null)
291
+ continue;
292
+ const agentPair = arrAt(key, 1);
293
+ const agentId = strAt(agentPair, 0);
294
+ const partnerDeviceId = strAt(agentPair, 1);
295
+ const name = strAt(rec, 3) ?? '';
296
+ const type = strAt(rec, 5) ?? '';
297
+ // Field [20] is the USER-ASSIGNED device type; it overrides hardware type [5] for intent.
298
+ const assignedType = strAt(arrAt(rec, 20), 0);
299
+ const traitsArr = arrAt(rec, 6);
300
+ const traits = traitsArr == null ? [] : traitsArr.map((t) => str(t)).filter((t) => t != null);
301
+ devices.push({
302
+ id: devId,
303
+ name,
304
+ type,
305
+ assignedType,
306
+ traits,
307
+ roomName: deviceRoom.get(devId) ?? null,
308
+ agentId,
309
+ partnerDeviceId,
310
+ capabilities: deriveCapabilities(traits),
311
+ });
312
+ }
313
+ }
314
+ }
315
+ // Room membership can appear after a device record within the same home; stamp again.
316
+ const stamped = devices.map((d) => d.roomName == null ? { ...d, roomName: deviceRoom.get(d.id) ?? null } : d);
317
+ return { homes, devices: stamped, rooms };
318
+ }