apple-fm 1.2.0 → 1.3.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.
@@ -0,0 +1,32 @@
1
+ interface ReadIdentity {
2
+ path: string;
3
+ device: string;
4
+ inode: string;
5
+ birth: string;
6
+ }
7
+ type ReadSnapshot = {
8
+ path: string;
9
+ identity: ReadIdentity;
10
+ } | {
11
+ path: string;
12
+ error: string;
13
+ };
14
+ type ReadWorkerRequest = {
15
+ kind: 'inspect';
16
+ path: string;
17
+ } | {
18
+ kind: 'rules';
19
+ paths: string[];
20
+ } | {
21
+ kind: 'read';
22
+ identity: ReadIdentity;
23
+ maxBytes: number;
24
+ offset?: number;
25
+ limit?: number;
26
+ };
27
+ declare function inspectReadPath(path: string): ReadSnapshot;
28
+ declare function readAuthorizedFile(request: Extract<ReadWorkerRequest, {
29
+ kind: 'read';
30
+ }>): string;
31
+
32
+ export { type ReadIdentity, type ReadSnapshot, type ReadWorkerRequest, inspectReadPath, readAuthorizedFile };
@@ -0,0 +1,91 @@
1
+ // src/tools/builtin/read-worker.ts
2
+ import { closeSync, constants, fstatSync, lstatSync, openSync, readSync } from "fs";
3
+
4
+ // src/tools/output.ts
5
+ var MAX_TOOL_OUTPUT_CHARS = 3e3;
6
+ function capOutput(text, max = MAX_TOOL_OUTPUT_CHARS) {
7
+ if (text.length <= max) return text;
8
+ return `${text.slice(0, max)}
9
+ \u2026(${String(text.length - max)} more chars truncated)`;
10
+ }
11
+
12
+ // src/tools/builtin/readPaths.ts
13
+ import { realpathSync } from "fs";
14
+ import { basename, dirname, join, resolve, sep } from "path";
15
+ function canonicalPath(path) {
16
+ const absolute = resolve(path);
17
+ try {
18
+ return realpathSync(absolute);
19
+ } catch {
20
+ try {
21
+ return join(realpathSync(dirname(absolute)), basename(absolute));
22
+ } catch {
23
+ return absolute;
24
+ }
25
+ }
26
+ }
27
+
28
+ // src/tools/builtin/read-worker.ts
29
+ function requireRegularFile(path, stats) {
30
+ if (stats.isFile()) return;
31
+ const kind = stats.isDirectory() ? "directory" : stats.isFIFO() ? "named pipe (fifo)" : "special file";
32
+ throw new Error(`read: "${path}" is a ${kind}, not a regular file \u2014 refusing to read it`);
33
+ }
34
+ function inspectReadPath(path) {
35
+ const canonical = canonicalPath(path);
36
+ try {
37
+ const stats = lstatSync(canonical, { bigint: true });
38
+ requireRegularFile(canonical, stats);
39
+ return { path: canonical, identity: {
40
+ path: canonical,
41
+ device: String(stats.dev),
42
+ inode: String(stats.ino),
43
+ birth: String(stats.birthtimeNs)
44
+ } };
45
+ } catch (error) {
46
+ return { path: canonical, error: error instanceof Error ? error.message : String(error) };
47
+ }
48
+ }
49
+ function readAuthorizedFile(request) {
50
+ const { identity, maxBytes, offset, limit } = request;
51
+ const noFollow = process.platform === "darwin" ? 536870912 : constants.O_NOFOLLOW;
52
+ const descriptor = openSync(identity.path, constants.O_RDONLY | constants.O_NONBLOCK | noFollow);
53
+ try {
54
+ const stats = fstatSync(descriptor, { bigint: true });
55
+ requireRegularFile(identity.path, stats);
56
+ if (String(stats.dev) !== identity.device || String(stats.ino) !== identity.inode || String(stats.birthtimeNs) !== identity.birth) {
57
+ throw new Error("read: approved file changed before it could be read");
58
+ }
59
+ const buffer = Buffer.alloc(maxBytes);
60
+ const length = readSync(descriptor, buffer, 0, maxBytes, 0);
61
+ const text = buffer.toString("utf8", 0, length);
62
+ if (offset === void 0 && limit === void 0) return capOutput(text);
63
+ const start = offset ?? 0;
64
+ return capOutput(text.split("\n").slice(start, limit === void 0 ? void 0 : start + limit).join("\n"));
65
+ } finally {
66
+ closeSync(descriptor);
67
+ }
68
+ }
69
+ function execute(request) {
70
+ switch (request.kind) {
71
+ case "inspect":
72
+ return inspectReadPath(request.path);
73
+ case "rules":
74
+ return request.paths.map(canonicalPath);
75
+ case "read":
76
+ return readAuthorizedFile(request);
77
+ }
78
+ }
79
+ if (process.argv[2] === "--apple-fm-read-worker") {
80
+ try {
81
+ const request = JSON.parse(process.argv[3] ?? "");
82
+ process.stdout.write(JSON.stringify({ value: execute(request) }));
83
+ } catch (error) {
84
+ process.stdout.write(JSON.stringify({ error: error instanceof Error ? error.message : String(error) }));
85
+ }
86
+ }
87
+ export {
88
+ inspectReadPath,
89
+ readAuthorizedFile
90
+ };
91
+ //# sourceMappingURL=read-worker.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apple-fm",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Command-line and programmatic access to Apple's on-device Foundation Models (Apple Intelligence) on macOS 26+.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -48,6 +48,7 @@
48
48
  "dev": "tsup --watch",
49
49
  "test": "vitest run --coverage",
50
50
  "test:watch": "vitest",
51
+ "test:device": "node scripts/device-smoke.mjs",
51
52
  "lint": "eslint src/ tests/",
52
53
  "typecheck": "tsc --noEmit",
53
54
  "clean": "rm -rf dist coverage node_modules/.cache",
@@ -59,16 +60,24 @@
59
60
  "devDependencies": {
60
61
  "@eslint/js": "^10.0.1",
61
62
  "@types/node": "^25.9.3",
62
- "@vitest/coverage-v8": "^4.1.9",
63
- "domotion-svg": "^0.21.1",
64
- "eslint": "^10.5.0",
65
- "eslint-plugin-import-x": "^4.16.2",
63
+ "@vitest/coverage-v8": "^4.1.12",
64
+ "domotion-svg": "^0.35.0",
65
+ "eslint": "^10.15.0",
66
+ "eslint-plugin-import-x": "^4.18.1",
66
67
  "eslint-plugin-simple-import-sort": "^12.1.1",
67
- "eslint-plugin-tsdoc": "^0.5.2",
68
+ "eslint-plugin-tsdoc": "^0.5.4",
68
69
  "tsup": "^8.5.1",
69
70
  "tsx": "^4.22.4",
70
71
  "typescript": "^5.9.3",
71
- "typescript-eslint": "^8.61.1",
72
- "vitest": "^4.1.9"
72
+ "typescript-eslint": "^8.74.0",
73
+ "vitest": "^4.1.12"
74
+ },
75
+ "overrides": {
76
+ "eslint-plugin-tsdoc": {
77
+ "@typescript-eslint/utils": "8.74.0"
78
+ },
79
+ "tsup": {
80
+ "esbuild": "^0.28.1"
81
+ }
73
82
  }
74
83
  }
@@ -38,7 +38,13 @@ trap 'rm -f "$LOG"' EXIT
38
38
  # Apple Intelligence is arm64-only and needs the macOS 26 SDK for FoundationModels.
39
39
  # Compile every .swift in the helper dir into one binary (one shared module).
40
40
  sources=("$SRCDIR"/*.swift)
41
- if ! swiftc -O -target arm64-apple-macos26 "${sources[@]}" -o "$OUT" 2>"$LOG"; then
41
+ # Compile optional image APIs on SDK 27+ while retaining the macOS 26 deployment target.
42
+ sdk_version="$(xcrun --sdk macosx --show-sdk-version 2>/dev/null || true)"
43
+ swift_flags=(-O -target arm64-apple-macos26)
44
+ if [[ "${sdk_version%%.*}" =~ ^[0-9]+$ ]] && [[ "${sdk_version%%.*}" -ge 27 ]]; then
45
+ swift_flags+=(-D APPLE_FM_IMAGES)
46
+ fi
47
+ if ! swiftc "${swift_flags[@]}" "${sources[@]}" -o "$OUT" 2>"$LOG"; then
42
48
  echo "[apple-fm] build failed (needs the macOS 26 SDK / Xcode 26) — skipping:"
43
49
  sed 's/^/[apple-fm] /' "$LOG" || true
44
50
  exit 0