ink-framework-lightos 0.1.0-alpha.12 → 0.1.0-alpha.14

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/ink-native.json CHANGED
@@ -9,6 +9,15 @@
9
9
  ],
10
10
  "src/push.ts": [
11
11
  "light-sdk-push"
12
+ ],
13
+ "src/push-hooks.ts": [
14
+ "light-sdk-push"
15
+ ],
16
+ "src/push-delivery.ts": [
17
+ "light-sdk-push"
18
+ ],
19
+ "src/tool-manager.ts": [
20
+ "light-sdk-tool-manager"
12
21
  ]
13
22
  }
14
23
  }
package/package.json CHANGED
@@ -1,13 +1,15 @@
1
1
  {
2
2
  "name": "ink-framework-lightos",
3
- "version": "0.1.0-alpha.12",
3
+ "version": "0.1.0-alpha.14",
4
4
  "exports": {
5
5
  ".": "./src/index.ts",
6
6
  "./ringtone": "./src/ringtone.ts",
7
- "./push": "./src/push.ts"
7
+ "./push": "./src/push.ts",
8
+ "./push/delivery": "./src/push-delivery.ts",
9
+ "./tool-manager": "./src/tool-manager.ts"
8
10
  },
9
11
  "peerDependencies": {
10
- "ink": "npm:ink-framework@0.1.0-alpha.12",
12
+ "ink": "npm:ink-framework@0.1.0-alpha.14",
11
13
  "react": "19.2.8"
12
14
  },
13
15
  "license": "MIT",
@@ -19,6 +21,7 @@
19
21
  "files": [
20
22
  "src",
21
23
  "*.json",
22
- "*.ts"
24
+ "*.ts",
25
+ "*.md"
23
26
  ]
24
27
  }
@@ -0,0 +1,44 @@
1
+ import { callNative, NativeError } from "ink/native";
2
+
3
+ export interface PushMessage {
4
+ id: string;
5
+ groupKey: string;
6
+ title: string;
7
+ body: string;
8
+ route: string;
9
+ receivedAt: number;
10
+ }
11
+ export function fields(value: unknown) {
12
+ if (typeof value !== "object" || value === null) throw new NativeError("protocol", "Invalid push state");
13
+ return {
14
+ text(name: string) {
15
+ const field: unknown = Reflect.get(value, name);
16
+ if (typeof field !== "string") throw new NativeError("protocol", `Invalid push ${name}`);
17
+ return field;
18
+ },
19
+ time(name: string) {
20
+ const field: unknown = Reflect.get(value, name);
21
+ if (typeof field !== "number" || !Number.isSafeInteger(field) || field < 0) throw new NativeError("protocol", `Invalid push ${name}`);
22
+ return field;
23
+ },
24
+ };
25
+ }
26
+ export function decodeMessage(value: unknown): PushMessage {
27
+ const item = fields(value);
28
+ return { id: item.text("id"), groupKey: item.text("groupKey"), title: item.text("title"), body: item.text("body"), route: item.text("route"), receivedAt: item.time("receivedAtMs") };
29
+ }
30
+ export interface PushDelivery { messages: readonly PushMessage[]; cancelled: readonly string[] }
31
+ export function decodePushDelivery(value: unknown): PushDelivery {
32
+ if (typeof value !== "object" || value === null || !("messages" in value) || !Array.isArray(value.messages)
33
+ || !("cancelled" in value) || !Array.isArray(value.cancelled)) throw new TypeError("Invalid push delivery");
34
+ return {
35
+ messages: value.messages.map(decodeMessage),
36
+ cancelled: value.cancelled.map((key: unknown) => {
37
+ if (typeof key !== "string") throw new TypeError("Invalid cancelled push group");
38
+ return key;
39
+ }),
40
+ };
41
+ }
42
+ export async function setPushTask(task: { id: string } | null): Promise<void> {
43
+ await callNative("background", "set-push-task", { task: task?.id ?? null });
44
+ }
@@ -0,0 +1,81 @@
1
+ import { fields, decodeMessage, type PushMessage } from "./push-delivery";
2
+ import { useEffect, useMemo, useRef, useState } from "ink/internal/react";
3
+ import { NativeError } from "ink/native";
4
+ import { attachNativeController } from "ink/native/controller";
5
+
6
+ export interface PushState {
7
+ status: "idle" | "registering" | "synchronising" | "ready" | "error";
8
+ endpoint: string;
9
+ registeredAt: number;
10
+ openedKey: string;
11
+ messages: readonly PushMessage[];
12
+ error: Error | null;
13
+ }
14
+ export interface PushRegistration {
15
+ url: string;
16
+ bearerToken?: string;
17
+ encryptionKey?: string;
18
+ }
19
+ export interface PushOptions {
20
+ foregroundGroup?: string | null;
21
+ }
22
+ const initial: PushState = { status: "idle", endpoint: "", registeredAt: 0, openedKey: "", messages: [], error: null };
23
+ function decode(value: unknown): PushState {
24
+ const field = fields(value);
25
+ const status = field.text("status");
26
+ if (status !== "idle" && status !== "registering" && status !== "synchronising" && status !== "ready" && status !== "error") throw new NativeError("protocol", "Invalid push status");
27
+ if (typeof value !== "object" || value === null || !("messages" in value) || !Array.isArray(value.messages)) throw new NativeError("protocol", "Invalid push messages");
28
+ let error: Error | null = null;
29
+ if (status === "error") {
30
+ if (!("error" in value) || typeof value.error !== "object" || value.error === null) throw new NativeError("protocol", "Invalid push error");
31
+ const detail = fields(value.error);
32
+ error = new NativeError(detail.text("kind"), detail.text("message"), "retryable" in value.error && value.error.retryable === true);
33
+ }
34
+ return { status, endpoint: field.text("endpoint"), registeredAt: field.time("registeredAtMs"), openedKey: field.text("openedKey"), error,
35
+ messages: value.messages.map(decodeMessage) };
36
+ }
37
+
38
+ export function usePush({ foregroundGroup = null }: PushOptions = {}) {
39
+ const [state, setState] = useState(initial);
40
+ const [ready, setReady] = useState(false);
41
+ const controller = useRef<ReturnType<typeof attachNativeController> | null>(null);
42
+ useEffect(() => {
43
+ setState(initial);
44
+ setReady(false);
45
+ const fail = (error: unknown) => setState({ ...initial, status: "error", error: error instanceof Error ? error : new Error(String(error)) });
46
+ const attachment = attachNativeController("notifications", { kind: "light-push" }, value => {
47
+ try { setState(decode(value)); } catch (error) { fail(error); }
48
+ });
49
+ controller.current = attachment;
50
+ void attachment.ready.then(() => {
51
+ if (controller.current === attachment) setReady(true);
52
+ }, error => { if (controller.current === attachment) fail(error); });
53
+ return () => {
54
+ controller.current = null;
55
+ void attachment.dispose().catch(error => console.error("Could not release push inbox", error));
56
+ };
57
+ }, []);
58
+ useEffect(() => {
59
+ const attachment = controller.current;
60
+ if (!attachment) return;
61
+ let active = true;
62
+ void attachment.call("foreground", { groupKey: foregroundGroup }).catch(error => {
63
+ if (active) setState(current => ({ ...current, status: "error", error: error instanceof Error ? error : new Error(String(error)) }));
64
+ });
65
+ return () => { active = false; };
66
+ }, [foregroundGroup]);
67
+ const commands = useMemo(() => {
68
+ const call = async (operation: string, payload: unknown = {}) => {
69
+ if (!controller.current) throw new NativeError("unavailable", "Push inbox is not attached");
70
+ await controller.current.call(operation, payload);
71
+ };
72
+ return {
73
+ register: (options: PushRegistration) => call("register", options),
74
+ retry: () => call("retry"), unregister: () => call("unregister"),
75
+ dismiss: (groupKey: string) => call("dismiss", { groupKey }), clear: () => call("clear"),
76
+ notifyForeground: (groupKey: string) => call("foreground-message", { groupKey }),
77
+ };
78
+ }, []);
79
+ return { state, ready, ...commands };
80
+ }
81
+
package/src/push.ts CHANGED
@@ -1,123 +1,2 @@
1
- import { useEffect, useMemo, useRef, useState } from "react";
2
- import { callNative, NativeError } from "ink/native";
3
- import { attachNativeController } from "ink/native/controller";
4
-
5
- export interface PushMessage {
6
- id: string;
7
- groupKey: string;
8
- title: string;
9
- body: string;
10
- route: string;
11
- receivedAt: number;
12
- }
13
- export interface PushState {
14
- status: "idle" | "registering" | "synchronising" | "ready" | "error";
15
- endpoint: string;
16
- registeredAt: number;
17
- openedKey: string;
18
- messages: readonly PushMessage[];
19
- error: Error | null;
20
- }
21
- export interface PushRegistration {
22
- url: string;
23
- bearerToken?: string;
24
- encryptionKey?: string;
25
- fetchOnWake?: boolean;
26
- }
27
- export interface PushOptions {
28
- foregroundGroup?: string | null;
29
- }
30
- const initial: PushState = { status: "idle", endpoint: "", registeredAt: 0, openedKey: "", messages: [], error: null };
31
- function fields(value: unknown) {
32
- if (typeof value !== "object" || value === null) throw new NativeError("protocol", "Invalid push state");
33
- return {
34
- text(name: string) {
35
- const field: unknown = Reflect.get(value, name);
36
- if (typeof field !== "string") throw new NativeError("protocol", `Invalid push ${name}`);
37
- return field;
38
- },
39
- time(name: string) {
40
- const field: unknown = Reflect.get(value, name);
41
- if (typeof field !== "number" || !Number.isSafeInteger(field) || field < 0) throw new NativeError("protocol", `Invalid push ${name}`);
42
- return field;
43
- },
44
- };
45
- }
46
- function decodeMessage(value: unknown): PushMessage {
47
- const item = fields(value);
48
- return { id: item.text("id"), groupKey: item.text("groupKey"), title: item.text("title"), body: item.text("body"), route: item.text("route"), receivedAt: item.time("receivedAtMs") };
49
- }
50
- function decode(value: unknown): PushState {
51
- const field = fields(value);
52
- const status = field.text("status");
53
- if (status !== "idle" && status !== "registering" && status !== "synchronising" && status !== "ready" && status !== "error") throw new NativeError("protocol", "Invalid push status");
54
- if (typeof value !== "object" || value === null || !("messages" in value) || !Array.isArray(value.messages)) throw new NativeError("protocol", "Invalid push messages");
55
- let error: Error | null = null;
56
- if (status === "error") {
57
- if (!("error" in value) || typeof value.error !== "object" || value.error === null) throw new NativeError("protocol", "Invalid push error");
58
- const detail = fields(value.error);
59
- error = new NativeError(detail.text("kind"), detail.text("message"), "retryable" in value.error && value.error.retryable === true);
60
- }
61
- return { status, endpoint: field.text("endpoint"), registeredAt: field.time("registeredAtMs"), openedKey: field.text("openedKey"), error,
62
- messages: value.messages.map(decodeMessage) };
63
- }
64
-
65
- export function usePush({ foregroundGroup = null }: PushOptions = {}) {
66
- const [state, setState] = useState(initial);
67
- const [ready, setReady] = useState(false);
68
- const controller = useRef<ReturnType<typeof attachNativeController> | null>(null);
69
- useEffect(() => {
70
- setState(initial);
71
- setReady(false);
72
- const fail = (error: unknown) => setState({ ...initial, status: "error", error: error instanceof Error ? error : new Error(String(error)) });
73
- const attachment = attachNativeController("notifications", { kind: "light-push" }, value => {
74
- try { setState(decode(value)); } catch (error) { fail(error); }
75
- });
76
- controller.current = attachment;
77
- void attachment.ready.then(() => {
78
- if (controller.current === attachment) setReady(true);
79
- }, error => { if (controller.current === attachment) fail(error); });
80
- return () => {
81
- controller.current = null;
82
- void attachment.dispose().catch(error => console.error("Could not release push inbox", error));
83
- };
84
- }, []);
85
- useEffect(() => {
86
- const attachment = controller.current;
87
- if (!attachment) return;
88
- let active = true;
89
- void attachment.call("foreground", { groupKey: foregroundGroup }).catch(error => {
90
- if (active) setState(current => ({ ...current, status: "error", error: error instanceof Error ? error : new Error(String(error)) }));
91
- });
92
- return () => { active = false; };
93
- }, [foregroundGroup]);
94
- const commands = useMemo(() => {
95
- const call = async (operation: string, payload: unknown = {}) => {
96
- if (!controller.current) throw new NativeError("unavailable", "Push inbox is not attached");
97
- await controller.current.call(operation, payload);
98
- };
99
- return {
100
- register: (options: PushRegistration) => call("register", options),
101
- retry: () => call("retry"), unregister: () => call("unregister"),
102
- dismiss: (groupKey: string) => call("dismiss", { groupKey }), clear: () => call("clear"),
103
- notifyForeground: (groupKey: string) => call("foreground-message", { groupKey }),
104
- };
105
- }, []);
106
- return { state, ready, ...commands };
107
- }
108
-
109
- export interface PushDelivery { messages: readonly PushMessage[]; cancelled: readonly string[] }
110
- export function decodePushDelivery(value: unknown): PushDelivery {
111
- if (typeof value !== "object" || value === null || !("messages" in value) || !Array.isArray(value.messages)
112
- || !("cancelled" in value) || !Array.isArray(value.cancelled)) throw new TypeError("Invalid push delivery");
113
- return {
114
- messages: value.messages.map(decodeMessage),
115
- cancelled: value.cancelled.map((key: unknown) => {
116
- if (typeof key !== "string") throw new TypeError("Invalid cancelled push group");
117
- return key;
118
- }),
119
- };
120
- }
121
- export async function setPushTask(task: { id: string } | null): Promise<void> {
122
- await callNative("background", "set-push-task", { task: task?.id ?? null });
123
- }
1
+ export { usePush, type PushState, type PushRegistration, type PushOptions } from "./push-hooks";
2
+ export { decodePushDelivery, setPushTask, type PushDelivery, type PushMessage } from "./push-delivery";
@@ -0,0 +1,89 @@
1
+ import { useEffect, useState } from "ink/internal/react";
2
+ import { callNative, callNativeBytes, NativeError } from "ink/native";
3
+
4
+ export interface SharedFile {
5
+ name: string;
6
+ path: string;
7
+ uri: string;
8
+ isFile: boolean;
9
+ size: number;
10
+ lastModified: number;
11
+ }
12
+
13
+ function decodeFiles(source: string): SharedFile[] {
14
+ const files: unknown = JSON.parse(source);
15
+ if (!Array.isArray(files)) throw new NativeError("protocol", "Invalid shared file list");
16
+ return files.map((file: unknown) => {
17
+ if (typeof file !== "object" || file === null
18
+ || !["name", "path", "uri"].every(key => typeof Reflect.get(file, key) === "string")
19
+ || typeof Reflect.get(file, "isFile") !== "boolean"
20
+ || !["size", "lastModified"].every(key => {
21
+ const value: unknown = Reflect.get(file, key);
22
+ return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
23
+ })) throw new NativeError("protocol", "Invalid shared file");
24
+ return file as SharedFile;
25
+ });
26
+ }
27
+
28
+ export async function setSharedFilesTask(task: { id: string } | null): Promise<void> {
29
+ await callNative("background", "set-shared-files-task", { task: task?.id ?? null });
30
+ }
31
+
32
+ export const sharedFiles = {
33
+ async list(path = ""): Promise<SharedFile[]> {
34
+ return decodeFiles(await callNative("tool-manager", "list", { path }));
35
+ },
36
+ async exists(path: string): Promise<boolean> {
37
+ return await callNative("tool-manager", "exists", { path }) === "true";
38
+ },
39
+ async readBytes(path: string, options: { offset?: number; length?: number; signal?: AbortSignal } = {}): Promise<Uint8Array> {
40
+ if (options.offset !== undefined && (!Number.isSafeInteger(options.offset) || options.offset < 0)) throw new RangeError("Invalid shared file offset");
41
+ if (options.length !== undefined && (!Number.isSafeInteger(options.length) || options.length < 0 || options.length > 524_288)) throw new RangeError("Shared reads accept up to 512 KiB");
42
+ return (await callNativeBytes("tool-manager", "read", { path, offset: options.offset, length: options.length }, { signal: options.signal })).bytes;
43
+ },
44
+ async writeBytes(path: string, bytes: Uint8Array): Promise<void> {
45
+ await callNativeBytes("tool-manager", "write", { path }, { bytes });
46
+ },
47
+ async copyFrom(path: string, source: string, options: { signal?: AbortSignal } = {}): Promise<void> {
48
+ await callNative("tool-manager", "copy-from", { path, source }, { signal: options.signal, timeoutMs: 600_000 });
49
+ },
50
+ async readText(path: string): Promise<string> {
51
+ return await callNative("tool-manager", "read-text", { path });
52
+ },
53
+ async writeText(path: string, text: string): Promise<void> {
54
+ await callNative("tool-manager", "write-text", { path, text });
55
+ },
56
+ async remove(path: string): Promise<boolean> {
57
+ return await callNative("tool-manager", "remove", { path }) === "true";
58
+ },
59
+ async mkdir(path: string): Promise<void> {
60
+ await callNative("tool-manager", "mkdir", { path });
61
+ },
62
+ };
63
+
64
+ export function useSharedFiles(path = "") {
65
+ const [state, setState] = useState<{ files: SharedFile[]; loading: boolean; error: Error | null }>({
66
+ files: [], loading: true, error: null,
67
+ });
68
+ useEffect(() => {
69
+ const controller = new AbortController();
70
+ setState({ files: [], loading: true, error: null });
71
+ void (async () => {
72
+ let revision = -1;
73
+ while (!controller.signal.aborted) {
74
+ const next = Number(await callNative("tool-manager", "observe", { revision }, { signal: controller.signal }));
75
+ if (!Number.isSafeInteger(next) || next < 0) throw new NativeError("protocol", "Invalid shared file state");
76
+ if (next !== revision) {
77
+ const files = await sharedFiles.list(path);
78
+ if (!controller.signal.aborted) setState({ files, loading: false, error: null });
79
+ }
80
+ revision = next;
81
+ }
82
+ })().catch(error => {
83
+ if (!controller.signal.aborted) setState(current => ({ ...current, loading: false,
84
+ error: error instanceof Error ? error : new Error(String(error)) }));
85
+ });
86
+ return () => controller.abort();
87
+ }, [path]);
88
+ return state;
89
+ }