@bendyline/gezel-capacitor 0.0.0-stage → 0.1.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.
Files changed (121) hide show
  1. package/LICENSE +21 -0
  2. package/Package.swift +26 -0
  3. package/README.md +230 -2
  4. package/android/build.gradle +30 -0
  5. package/android/src/main/AndroidManifest.xml +1 -0
  6. package/android/src/main/java/com/bendyline/gezel/capacitor/GezelKnowledgePlugin.java +153 -0
  7. package/android/src/main/java/com/bendyline/gezel/capacitor/GezelOfflineSpeechPlugin.java +216 -0
  8. package/android/src/main/java/com/bendyline/gezel/capacitor/GezelRuntimePlugin.java +84 -0
  9. package/android/src/main/java/com/bendyline/gezel/capacitor/KnowledgeFiles.java +101 -0
  10. package/android/src/main/java/com/bendyline/gezel/capacitor/SpeechModels.java +230 -0
  11. package/android/src/main/java/com/bendyline/gezel/speech/SpeechRuntime.java +38 -0
  12. package/dist/index.d.ts +561 -0
  13. package/dist/index.js +7512 -0
  14. package/dist/knowledge-worker.js +211 -0
  15. package/dist/licenses/npm/manifest.json +76 -0
  16. package/dist/licenses/npm/texts/1583809bf1155510d2961f94aa9bca6dc4b44b6f59fd773de33bd6c20860d781.txt +29 -0
  17. package/dist/licenses/npm/texts/1cd39aa4a566eba49eb206b969ef423295e79b50503df567bbdb2e8877d78892.txt +8 -0
  18. package/dist/licenses/npm/texts/3f1189b28e3866e0d979968d466b78f813f76827cfdca1fbb124cc0a5c8841f8.txt +21 -0
  19. package/dist/licenses/npm/texts/5b37224c080cdcc97c871ada971c224e9926370fe74f11b539aa1cf9f3b1aca1.txt +21 -0
  20. package/dist/licenses/npm/texts/c71d239df91726fc519c6eb72d318ec65820627232b2f796219e87dcf35d0ab4.txt +201 -0
  21. package/dist/sqlite3.wasm +0 -0
  22. package/embedding-manifest.json +210 -0
  23. package/ios/Sources/GezelCapacitor/GezelKnowledgePlugin.swift +200 -0
  24. package/ios/Sources/GezelCapacitor/GezelOfflineSpeechPlugin.swift +484 -0
  25. package/ios/Sources/GezelCapacitor/GezelRuntimePlugin.swift +102 -0
  26. package/kokoro-lexicon/LICENSE +202 -0
  27. package/kokoro-lexicon/lexicon-gb-en.txt.gz +0 -0
  28. package/kokoro-lexicon/lexicon-us-en.txt.gz +0 -0
  29. package/native/android/LICENSE-gezel.txt +21 -0
  30. package/native/android/PUBLIC-DISTRIBUTION.md +97 -0
  31. package/native/android/THIRD_PARTY_LICENSES/LICENSE-arm-optimized-routines-MIT-OR-Apache-2.0.txt +249 -0
  32. package/native/android/THIRD_PARTY_LICENSES/LICENSE-cpp-httplib-MIT.txt +22 -0
  33. package/native/android/THIRD_PARTY_LICENSES/LICENSE-fp16-MIT.txt +11 -0
  34. package/native/android/THIRD_PARTY_LICENSES/LICENSE-ggml-MIT.txt +21 -0
  35. package/native/android/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-llamafile-sgemm-MIT.txt +21 -0
  36. package/native/android/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-openvino-APACHE.txt +201 -0
  37. package/native/android/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-pillow-MIT-CMU.txt +30 -0
  38. package/native/android/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-rotate-bits-MIT.txt +21 -0
  39. package/native/android/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-unicode-data-Unicode-3.0.txt +39 -0
  40. package/native/android/THIRD_PARTY_LICENSES/LICENSE-nlohmann-json-MIT.txt +21 -0
  41. package/native/android/THIRD_PARTY_LICENSES/LICENSE-nvidia-cub-BSD-3-Clause.txt +24 -0
  42. package/native/android/THIRD_PARTY_LICENSES/LICENSE-vulkan-headers-Apache-2.0-OR-MIT.txt +33 -0
  43. package/native/android/THIRD_PARTY_LICENSES/LICENSE-vulkan-loader-APACHE.txt +201 -0
  44. package/native/android/THIRD_PARTY_LICENSES/LICENSE-vulkan-loader-cjson-MIT.txt +32 -0
  45. package/native/android/THIRD_PARTY_LICENSES/LICENSE-yarn-MIT.txt +21 -0
  46. package/native/android/THIRD_PARTY_LICENSES/manifest.json +21 -0
  47. package/native/android/engine-manifest.json +88 -0
  48. package/native/android/maven/com/bendyline/gezel/gezel-llama/0.1.57/gezel-llama-0.1.57.aar +0 -0
  49. package/native/android/maven/com/bendyline/gezel/gezel-llama/0.1.57/gezel-llama-0.1.57.aar.sha256 +1 -0
  50. package/native/android/maven/com/bendyline/gezel/gezel-llama/0.1.57/gezel-llama-0.1.57.pom +8 -0
  51. package/native/android/maven/com/bendyline/gezel/gezel-llama/0.1.57/gezel-llama-0.1.57.pom.sha256 +1 -0
  52. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57-sources.jar +0 -0
  53. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57-sources.jar.md5 +1 -0
  54. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57-sources.jar.sha1 +1 -0
  55. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57-sources.jar.sha256 +1 -0
  56. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57-sources.jar.sha512 +1 -0
  57. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.aar +0 -0
  58. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.aar.md5 +1 -0
  59. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.aar.sha1 +1 -0
  60. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.aar.sha256 +1 -0
  61. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.aar.sha512 +1 -0
  62. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.module +118 -0
  63. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.module.md5 +1 -0
  64. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.module.sha1 +1 -0
  65. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.module.sha256 +1 -0
  66. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.module.sha512 +1 -0
  67. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.pom +51 -0
  68. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.pom.md5 +1 -0
  69. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.pom.sha1 +1 -0
  70. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.pom.sha256 +1 -0
  71. package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.pom.sha512 +1 -0
  72. package/native/android/maven/com/bendyline/gezel/gezel-runtime/maven-metadata.xml +13 -0
  73. package/native/android/maven/com/bendyline/gezel/gezel-runtime/maven-metadata.xml.md5 +1 -0
  74. package/native/android/maven/com/bendyline/gezel/gezel-runtime/maven-metadata.xml.sha1 +1 -0
  75. package/native/android/maven/com/bendyline/gezel/gezel-runtime/maven-metadata.xml.sha256 +1 -0
  76. package/native/android/maven/com/bendyline/gezel/gezel-runtime/maven-metadata.xml.sha512 +1 -0
  77. package/native/android/sdk-manifest.json +98 -0
  78. package/native/ios/GezelLlama.xcframework/Info.plist +47 -0
  79. package/native/ios/GezelLlama.xcframework/ios-arm64/Headers/gezel_llama.h +290 -0
  80. package/native/ios/GezelLlama.xcframework/ios-arm64/Headers/module.modulemap +9 -0
  81. package/native/ios/GezelLlama.xcframework/ios-arm64/libGezelLlama.a +0 -0
  82. package/native/ios/GezelLlama.xcframework/ios-arm64-simulator/Headers/gezel_llama.h +290 -0
  83. package/native/ios/GezelLlama.xcframework/ios-arm64-simulator/Headers/module.modulemap +9 -0
  84. package/native/ios/GezelLlama.xcframework/ios-arm64-simulator/libGezelLlama.a +0 -0
  85. package/native/ios/LICENSE-gezel.txt +21 -0
  86. package/native/ios/LICENSE-ggml.txt +21 -0
  87. package/native/ios/LICENSE-llama-cpp.txt +21 -0
  88. package/native/ios/PUBLIC-DISTRIBUTION.md +97 -0
  89. package/native/ios/Package.swift +17 -0
  90. package/native/ios/Sources/GezelModelStorage/MobileModelStore.swift +286 -0
  91. package/native/ios/Sources/GezelModelStorage/ModelDownloadHTTP.swift +55 -0
  92. package/native/ios/Sources/GezelModelStorage/ModelDownloadSource.swift +46 -0
  93. package/native/ios/Sources/GezelModelStorage/ModelDownloads.swift +289 -0
  94. package/native/ios/Sources/GezelModelStorage/PrivacyInfo.xcprivacy +32 -0
  95. package/native/ios/Sources/GezelRuntime/AppleFoundationProvider.swift +292 -0
  96. package/native/ios/Sources/GezelRuntime/AppleNativeTools.swift +152 -0
  97. package/native/ios/Sources/GezelRuntime/GezelNativeRuntime.swift +1349 -0
  98. package/native/ios/Sources/GezelRuntime/NativeCall.swift +35 -0
  99. package/native/ios/Sources/GezelRuntime/PrivacyInfo.xcprivacy +31 -0
  100. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-arm-optimized-routines-MIT-OR-Apache-2.0.txt +249 -0
  101. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-cpp-httplib-MIT.txt +22 -0
  102. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-fp16-MIT.txt +11 -0
  103. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-ggml-MIT.txt +21 -0
  104. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-llamafile-sgemm-MIT.txt +21 -0
  105. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-openvino-APACHE.txt +201 -0
  106. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-pillow-MIT-CMU.txt +30 -0
  107. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-rotate-bits-MIT.txt +21 -0
  108. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-unicode-data-Unicode-3.0.txt +39 -0
  109. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-nlohmann-json-MIT.txt +21 -0
  110. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-nvidia-cub-BSD-3-Clause.txt +24 -0
  111. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-vulkan-headers-Apache-2.0-OR-MIT.txt +33 -0
  112. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-vulkan-loader-APACHE.txt +201 -0
  113. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-vulkan-loader-cjson-MIT.txt +32 -0
  114. package/native/ios/THIRD_PARTY_LICENSES/LICENSE-yarn-MIT.txt +21 -0
  115. package/native/ios/THIRD_PARTY_LICENSES/manifest.json +21 -0
  116. package/native/ios/engine-manifest.json +102 -0
  117. package/native/ios/sdk-manifest.json +89 -0
  118. package/package.json +94 -3
  119. package/scripts/embedding-package.d.mts +23 -0
  120. package/scripts/embedding-package.mjs +185 -0
  121. package/scripts/stage-native.mjs +168 -0
package/package.json CHANGED
@@ -1,6 +1,97 @@
1
1
  {
2
2
  "name": "@bendyline/gezel-capacitor",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "On-device Gezel App SDK transport and native host for Capacitor.",
5
+ "license": "MIT",
6
+ "keywords": [
7
+ "gezel",
8
+ "capacitor",
9
+ "capacitor-plugin",
10
+ "ai",
11
+ "on-device",
12
+ "local-first",
13
+ "llama.cpp",
14
+ "ios",
15
+ "android",
16
+ "speech"
17
+ ],
18
+ "homepage": "https://gezel.com",
19
+ "bugs": {
20
+ "url": "https://github.com/bendyline/gezel/issues"
21
+ },
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/bendyline/gezel.git",
25
+ "directory": "packages/capacitor"
26
+ },
27
+ "author": {
28
+ "name": "Bendyline",
29
+ "email": "support@bendyline.com"
30
+ },
31
+ "publishConfig": {
32
+ "access": "public",
33
+ "provenance": true
34
+ },
35
+ "type": "module",
36
+ "main": "./dist/index.js",
37
+ "types": "./dist/index.d.ts",
38
+ "exports": {
39
+ ".": {
40
+ "types": "./dist/index.d.ts",
41
+ "import": "./dist/index.js"
42
+ },
43
+ "./package.json": "./package.json",
44
+ "./packaging": {
45
+ "types": "./scripts/embedding-package.d.mts",
46
+ "import": "./scripts/embedding-package.mjs"
47
+ }
48
+ },
49
+ "files": [
50
+ "dist",
51
+ "kokoro-lexicon",
52
+ "ios/Sources",
53
+ "android/src",
54
+ "android/build.gradle",
55
+ "native",
56
+ "Package.swift",
57
+ "README.md",
58
+ "LICENSE",
59
+ "!native/**/.build/**",
60
+ "!native/**/.swiftpm/**",
61
+ "scripts/embedding-package.mjs",
62
+ "scripts/embedding-package.d.mts",
63
+ "scripts/stage-native.mjs",
64
+ "embedding-manifest.json"
65
+ ],
66
+ "capacitor": {
67
+ "ios": {
68
+ "src": "ios"
69
+ },
70
+ "android": {
71
+ "src": "android"
72
+ }
73
+ },
74
+ "dependencies": {
75
+ "@bendyline/gezel": "1.2.12",
76
+ "@bendyline/gezel-app-sdk": "1.1.12"
77
+ },
78
+ "peerDependencies": {
79
+ "@capacitor/core": "^8.5.2"
80
+ },
81
+ "devDependencies": {
82
+ "@capacitor/core": "8.5.2",
83
+ "@huggingface/tokenizers": "0.2.0",
84
+ "@sqlite.org/sqlite-wasm": "3.53.4-build2",
85
+ "brotli": "1.3.3",
86
+ "tsup": "^8.5.1",
87
+ "typescript": "^6.0.3",
88
+ "vitest": "^4.1.10",
89
+ "@bendyline/gezel-inference": "1.0.7"
90
+ },
91
+ "scripts": {
92
+ "build": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\" && tsup && node scripts/build-knowledge-worker.mjs && node scripts/copy-web-assets.mjs && node scripts/stage-bundled-licenses.mjs",
93
+ "typecheck": "tsc --noEmit && tsc -p worker",
94
+ "test": "vitest run && node --test scripts/*.test.mjs",
95
+ "stage:native": "node scripts/stage-native.mjs"
96
+ }
6
97
  }
@@ -0,0 +1,23 @@
1
+ import type { EmbeddingPackageManifest } from '@bendyline/gezel/app-models';
2
+ export type { EmbeddingPackageManifest } from '@bendyline/gezel/app-models';
3
+ export function writeEmbeddingManifest(root?: string): Promise<EmbeddingPackageManifest>;
4
+ export function verifyCapacitorPackage(root?: string): Promise<EmbeddingPackageManifest>;
5
+ export function verifyRuntime(root: string, target: 'ios' | 'android'): Promise<unknown>;
6
+ export function stageNative(
7
+ target: 'ios' | 'android',
8
+ source: string,
9
+ destination?: string,
10
+ ): Promise<void>;
11
+ export function verifySpeech(root: string, target: 'ios' | 'android'): Promise<unknown>;
12
+ /** Stage the optional offline speech runtime (native/mobile/speech/build.py --package). */
13
+ export function stageSpeech(
14
+ target: 'ios' | 'android',
15
+ source: string,
16
+ destination?: string,
17
+ ): Promise<void>;
18
+
19
+ export function configureCapacitorProject(options: {
20
+ projectRoot: string;
21
+ platform: 'ios' | 'android';
22
+ sdkRoot?: string;
23
+ }): Promise<{ changed: string[]; compatibility: EmbeddingPackageManifest }>;
@@ -0,0 +1,185 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { lstat, readFile, readdir, realpath, writeFile } from 'node:fs/promises';
3
+ import path from 'node:path';
4
+ import { fileURLToPath } from 'node:url';
5
+ import { EmbeddingPackageManifestSchema } from '@bendyline/gezel/app-models';
6
+ import { stageNative, stageSpeech, verifyRuntime, verifySpeech } from './stage-native.mjs';
7
+ export { stageNative, stageSpeech, verifyRuntime, verifySpeech };
8
+ const packageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
9
+ const hash = (bytes) => createHash('sha256').update(bytes).digest('hex');
10
+ async function filesIn(root, relative = '') {
11
+ const files = [];
12
+ for (const entry of await readdir(path.join(root, relative), { withFileTypes: true })) {
13
+ if (['.build', '.swiftpm'].includes(entry.name)) continue;
14
+ const name = path.posix.join(relative, entry.name);
15
+ if (entry.isSymbolicLink()) throw new Error(`Unexpected package symlink: ${name}`);
16
+ if (entry.isDirectory()) files.push(...(await filesIn(root, name)));
17
+ else if (entry.isFile()) files.push(name);
18
+ else throw new Error(`Unexpected package file: ${name}`);
19
+ }
20
+ return files;
21
+ }
22
+ async function payload(root, target) {
23
+ const directory = path.join(root, 'native', target);
24
+ const runtime = await verifyRuntime(directory, target);
25
+ const actual = (await filesIn(directory)).filter((name) => name !== 'sdk-manifest.json').sort();
26
+ if (JSON.stringify(actual) !== JSON.stringify(Object.keys(runtime.files).sort()))
27
+ throw new Error(`Unexpected ${target} native file inventory`);
28
+ const engine = JSON.parse(await readFile(path.join(directory, 'engine-manifest.json'), 'utf8'));
29
+ if (engine.gezelABIVersion !== runtime.gezelABIVersion || engine.target !== target)
30
+ throw new Error(`Incompatible ${target} engine ABI`);
31
+ const licenses = actual.filter((name) => /(^|\/)LICENSE|NOTICE/i.test(name));
32
+ if (!licenses.length) throw new Error(`Missing ${target} native notices`);
33
+ return {
34
+ version: runtime.packageVersion,
35
+ abi: runtime.gezelABIVersion,
36
+ settings: engine.settings,
37
+ toolchains: engine.toolchains,
38
+ deviceInference: runtime.deviceInference,
39
+ licenses,
40
+ privacyManifests: actual.filter((name) => name.endsWith('.xcprivacy')),
41
+ };
42
+ }
43
+ const exists = (file) =>
44
+ lstat(file).then(
45
+ () => true,
46
+ () => false,
47
+ );
48
+ /** The optional speech runtime, verified exactly as staged; null when this build has none. */
49
+ async function speechPayload(root, target) {
50
+ const directory = path.join(root, 'native', `speech-${target}`);
51
+ if (!(await exists(directory))) return null;
52
+ const runtime = await verifySpeech(directory, target);
53
+ const actual = (await filesIn(directory)).filter((name) => name !== 'sdk-manifest.json').sort();
54
+ if (JSON.stringify(actual) !== JSON.stringify(Object.keys(runtime.files).sort()))
55
+ throw new Error(`Unexpected ${target} speech file inventory`);
56
+ return runtime.packageVersion;
57
+ }
58
+ /** Build-time provenance. The package tarball's registry integrity authenticates this manifest. */
59
+ export async function writeEmbeddingManifest(root = packageRoot) {
60
+ const manifest = JSON.parse(await readFile(path.join(root, 'package.json'), 'utf8'));
61
+ const native = { ios: await payload(root, 'ios'), android: await payload(root, 'android') };
62
+ if (native.ios.version !== native.android.version)
63
+ throw new Error('iOS and Android native package versions differ');
64
+ const files = {};
65
+ for (const directory of ['dist', 'kokoro-lexicon', 'ios/Sources', 'android/src', 'native']) {
66
+ // Speech dictionaries are optional, like the speech runtime they serve.
67
+ if (directory === 'kokoro-lexicon' && !(await exists(path.join(root, directory)))) continue;
68
+ for (const name of await filesIn(path.join(root, directory))) {
69
+ if (name.endsWith('.map')) continue;
70
+ const relative = `${directory}/${name}`;
71
+ files[relative] = hash(await readFile(path.join(root, relative)));
72
+ }
73
+ }
74
+ for (const name of [
75
+ 'Package.swift',
76
+ 'android/build.gradle',
77
+ 'scripts/embedding-package.mjs',
78
+ 'scripts/embedding-package.d.mts',
79
+ 'scripts/stage-native.mjs',
80
+ ])
81
+ files[name] = hash(await readFile(path.join(root, name)));
82
+ const value = {
83
+ schemaVersion: 1,
84
+ package: manifest.name,
85
+ version: manifest.version,
86
+ capacitor: manifest.peerDependencies['@capacitor/core'],
87
+ native,
88
+ files,
89
+ };
90
+ EmbeddingPackageManifestSchema.parse(value);
91
+ await writeFile(
92
+ path.join(root, 'embedding-manifest.json'),
93
+ `${JSON.stringify(value, null, 2)}\n`,
94
+ );
95
+ return value;
96
+ }
97
+ /** Read-only installed-package doctor. It never downloads or repairs executable bytes. */
98
+ export async function verifyCapacitorPackage(root = packageRoot) {
99
+ const value = EmbeddingPackageManifestSchema.parse(
100
+ JSON.parse(await readFile(path.join(root, 'embedding-manifest.json'), 'utf8')),
101
+ );
102
+ const pkg = JSON.parse(await readFile(path.join(root, 'package.json'), 'utf8'));
103
+ if (
104
+ value.schemaVersion !== 1 ||
105
+ value.package !== pkg.name ||
106
+ value.version !== pkg.version ||
107
+ value.capacitor !== pkg.peerDependencies['@capacitor/core'] ||
108
+ !value.files ||
109
+ typeof value.files !== 'object' ||
110
+ !Object.keys(value.files).length
111
+ )
112
+ throw new Error('Invalid Gezel embedding manifest');
113
+ const physicalRoot = await realpath(root);
114
+ for (const [relative, expected] of Object.entries(value.files)) {
115
+ if (
116
+ relative.split('/').some((part) => !part || part === '.' || part === '..') ||
117
+ /[\\:]/.test(relative) ||
118
+ [...relative].some((character) => character.charCodeAt(0) < 32) ||
119
+ !/^[a-f0-9]{64}$/.test(expected)
120
+ )
121
+ throw new Error('Invalid embedding file pin');
122
+ const file = path.join(root, relative);
123
+ if (
124
+ (await lstat(file)).isSymbolicLink() ||
125
+ !(await realpath(file)).startsWith(`${physicalRoot}${path.sep}`) ||
126
+ hash(await readFile(file)) !== expected
127
+ )
128
+ throw new Error(`Embedding package integrity mismatch: ${relative}`);
129
+ }
130
+ const native = { ios: await payload(root, 'ios'), android: await payload(root, 'android') };
131
+ if (JSON.stringify(native) !== JSON.stringify(value.native))
132
+ throw new Error('Embedding compatibility metadata changed');
133
+ // Speech files are pinned with every other native file above; this checks their own manifests too.
134
+ await speechPayload(root, 'ios');
135
+ await speechPayload(root, 'android');
136
+ return value;
137
+ }
138
+
139
+ /** Raise generated app deployment floors after cap sync; preserve higher app requirements. */
140
+ export async function configureCapacitorProject({ projectRoot, platform, sdkRoot = packageRoot }) {
141
+ if (!['ios', 'android'].includes(platform)) throw new Error('Expected ios or android');
142
+ const metadata = await verifyCapacitorPackage(sdkRoot);
143
+ const changed = [];
144
+ async function update(relative, transform) {
145
+ const file = path.resolve(projectRoot, relative);
146
+ const before = await readFile(file, 'utf8');
147
+ const after = transform(before);
148
+ if (after !== before) {
149
+ await writeFile(file, after);
150
+ changed.push(file);
151
+ }
152
+ }
153
+ if (platform === 'ios') {
154
+ const minimum = metadata.native.ios.settings.minimumOS;
155
+ if (typeof minimum !== 'string' || !/^\d+\.\d+$/.test(minimum))
156
+ throw new Error('Invalid iOS minimum in SDK metadata');
157
+ const below = (version) => {
158
+ const [major, minor = 0] = version.split('.').map(Number);
159
+ const [requiredMajor, requiredMinor] = minimum.split('.').map(Number);
160
+ return major < requiredMajor || (major === requiredMajor && minor < requiredMinor);
161
+ };
162
+ await update('ios/App/App.xcodeproj/project.pbxproj', (source) =>
163
+ source.replace(/IPHONEOS_DEPLOYMENT_TARGET = "?(\d+(?:\.\d+)?)"?;/g, (match, version) =>
164
+ below(version) ? `IPHONEOS_DEPLOYMENT_TARGET = ${minimum};` : match,
165
+ ),
166
+ );
167
+ await update('ios/App/CapApp-SPM/Package.swift', (source) =>
168
+ source.replace(
169
+ /\.iOS\((?:"(\d+(?:\.\d+)?)"|\.v(\d+)(?:_(\d+))?)\)/g,
170
+ (match, quoted, major, minor) =>
171
+ below(quoted ?? `${major}.${minor ?? 0}`) ? `.iOS("${minimum}")` : match,
172
+ ),
173
+ );
174
+ } else {
175
+ const minimum = metadata.native.android.settings.minimumAPI;
176
+ if (!Number.isInteger(minimum) || minimum < 1 || minimum > 1000)
177
+ throw new Error('Invalid Android minimum in SDK metadata');
178
+ await update('android/variables.gradle', (source) =>
179
+ source.replace(/minSdkVersion\s*=\s*(\d+)/g, (match, value) =>
180
+ Number(value) < minimum ? `minSdkVersion = ${minimum}` : match,
181
+ ),
182
+ );
183
+ }
184
+ return { changed, compatibility: metadata };
185
+ }
@@ -0,0 +1,168 @@
1
+ import { createHash } from 'node:crypto';
2
+ import {
3
+ access,
4
+ cp,
5
+ lstat,
6
+ mkdir,
7
+ mkdtemp,
8
+ readFile,
9
+ realpath,
10
+ rename,
11
+ rm,
12
+ } from 'node:fs/promises';
13
+ import path from 'node:path';
14
+ import { fileURLToPath, pathToFileURL } from 'node:url';
15
+
16
+ const packageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
17
+
18
+ const VERSION = /^\d+\.\d+\.\d+(?:-[A-Za-z0-9.-]+)?$/;
19
+ /** GEZEL_SPEECH_ABI in native/mobile/speech/gezel_speech.h, as the plugins' bindings expect it. */
20
+ export const SPEECH_ABI = 3;
21
+
22
+ export async function verifyRuntime(root, target) {
23
+ const manifest = JSON.parse(await readFile(path.join(root, 'sdk-manifest.json'), 'utf8'));
24
+ if (
25
+ manifest.scope !== 'provider-model-runtime' ||
26
+ manifest.target !== target ||
27
+ manifest.gezelABIVersion !== 1 ||
28
+ !VERSION.test(manifest.packageVersion) ||
29
+ !Object.keys(manifest.files ?? {}).length
30
+ ) {
31
+ throw new Error(`Expected a staged ${target} provider/model runtime`);
32
+ }
33
+ return verifyFiles(root, manifest);
34
+ }
35
+
36
+ /** The optional offline speech runtime (native/mobile/speech/build.py --package). */
37
+ export async function verifySpeech(root, target) {
38
+ const manifest = JSON.parse(await readFile(path.join(root, 'sdk-manifest.json'), 'utf8'));
39
+ if (
40
+ manifest.scope !== 'speech-runtime' ||
41
+ manifest.target !== target ||
42
+ manifest.speechABIVersion !== SPEECH_ABI ||
43
+ !VERSION.test(manifest.packageVersion) ||
44
+ !Object.keys(manifest.files ?? {}).length
45
+ ) {
46
+ throw new Error(`Expected a staged ${target} speech runtime`);
47
+ }
48
+ return verifyFiles(root, manifest);
49
+ }
50
+
51
+ async function verifyFiles(root, manifest) {
52
+ const realRoot = await realpath(root);
53
+ for (const [name, sha] of Object.entries(manifest.files)) {
54
+ const file = path.resolve(root, name);
55
+ if (
56
+ !file.startsWith(`${path.resolve(root)}${path.sep}`) ||
57
+ (await lstat(file)).isSymbolicLink() ||
58
+ !(await realpath(file)).startsWith(`${realRoot}${path.sep}`) ||
59
+ createHash('sha256')
60
+ .update(await readFile(file))
61
+ .digest('hex') !== sha
62
+ ) {
63
+ throw new Error(`Native package integrity mismatch: ${name}`);
64
+ }
65
+ }
66
+ return manifest;
67
+ }
68
+
69
+ // The license inventory is shared with every desktop engine and changes no code
70
+ // in the staged runtime; a notice edit there must not block packing. The next
71
+ // native build refreshes the runtime's notices.
72
+ const LICENSE_INPUTS = 'native/licenses/';
73
+
74
+ /**
75
+ * `allowStale` is for packs outside a release. A native release is cut from a
76
+ * merged commit, so a pull request that changes the wrapper sources cannot pin
77
+ * its own, and failing its packs would make that change impossible to merge.
78
+ */
79
+ export async function verifyProducerSources(
80
+ manifest,
81
+ repo,
82
+ warn = console.warn,
83
+ { allowStale = false } = {},
84
+ ) {
85
+ const staleNotices = [];
86
+ const staleSources = [];
87
+ for (const [name, sha] of Object.entries(manifest.sources ?? {})) {
88
+ const file = path.resolve(repo, name);
89
+ if (!file.startsWith(`${path.resolve(repo)}${path.sep}`))
90
+ throw new Error(`Restage the native runtime after changing ${name}`);
91
+ const current = await readFile(file).then(
92
+ (bytes) => createHash('sha256').update(bytes).digest('hex'),
93
+ () => null,
94
+ );
95
+ if (current === sha) continue;
96
+ const relative = path.relative(path.resolve(repo), file).split(path.sep).join('/');
97
+ if (relative.startsWith(LICENSE_INPUTS)) staleNotices.push(name);
98
+ else if (allowStale) staleSources.push(name);
99
+ else throw new Error(`Restage the native runtime after changing ${name}`);
100
+ }
101
+ if (staleSources.length)
102
+ warn(
103
+ `Native runtime ${manifest.packageVersion} predates changes to ${staleSources.join(', ')}; a release needs a new native build first.`,
104
+ );
105
+ if (staleNotices.length)
106
+ warn(
107
+ `Native runtime ${manifest.packageVersion} predates changes to ${staleNotices.join(', ')}; its license notices refresh with the next native build.`,
108
+ );
109
+ return staleNotices;
110
+ }
111
+
112
+ export function stageNative(
113
+ target,
114
+ source,
115
+ destination = path.join(packageRoot, 'native', target),
116
+ ) {
117
+ return stage(source, destination, (root) => verifyRuntime(root, target));
118
+ }
119
+
120
+ export function stageSpeech(
121
+ target,
122
+ source,
123
+ destination = path.join(packageRoot, 'native', `speech-${target}`),
124
+ ) {
125
+ return stage(source, destination, (root) => verifySpeech(root, target));
126
+ }
127
+
128
+ async function stage(source, destination, verify) {
129
+ const manifest = await verify(source);
130
+ await mkdir(path.dirname(destination), { recursive: true });
131
+ const work = await mkdtemp(`${destination}.stage-`);
132
+ const previous = `${work}/previous`;
133
+ let moved = false;
134
+ try {
135
+ // Only declared, verified bytes enter the distribution (no build caches or
136
+ // undeclared artifacts accidentally left beside a producer's package).
137
+ for (const name of ['sdk-manifest.json', ...Object.keys(manifest.files)]) {
138
+ const destination = path.join(work, 'next', name);
139
+ await mkdir(path.dirname(destination), { recursive: true });
140
+ await cp(path.join(source, name), destination);
141
+ }
142
+ await verify(`${work}/next`);
143
+ try {
144
+ await access(destination);
145
+ moved = true;
146
+ } catch {}
147
+ if (moved) await rename(destination, previous);
148
+ try {
149
+ await rename(`${work}/next`, destination);
150
+ } catch (error) {
151
+ if (moved) await rename(previous, destination);
152
+ throw error;
153
+ }
154
+ } finally {
155
+ await rm(work, { recursive: true, force: true });
156
+ }
157
+ }
158
+
159
+ if (process.argv[1] && import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href) {
160
+ const [target, source] = process.argv.slice(2);
161
+ const [, speech, platform] = /^(speech-)?(ios|android)$/.exec(target ?? '') ?? [];
162
+ if (!platform || !source)
163
+ throw new Error(
164
+ 'Usage: node scripts/stage-native.mjs ios|android|speech-ios|speech-android <staged-runtime>',
165
+ );
166
+ await (speech ? stageSpeech : stageNative)(platform, path.resolve(source));
167
+ console.log(`Prepared Gezel Capacitor ${target} package.`);
168
+ }