add feature to clear ml downloads

This commit is contained in:
Fabian Freund
2026-05-28 22:43:49 +02:00
parent f1b18b223e
commit 72cdd500e0
9 changed files with 176 additions and 3 deletions
@@ -6,8 +6,11 @@
const {
createEngine,
EngineProcess,
FEATURES,
} = ChromeUtils.importESModule("chrome://global/content/ml/EngineProcess.sys.mjs");
const { ModelHub } = ChromeUtils.importESModule("chrome://global/content/ml/ModelHub.sys.mjs");
const { OPFS } = ChromeUtils.importESModule("chrome://global/content/ml/OPFS.sys.mjs");
const ML_TASK_FEATURE_EXTRACTION = "feature-extraction";
const ML_TASK_TEXT2TEXT = "text2text-generation";
@@ -154,7 +157,7 @@ async function createMlEngine(engineConfig, progressCallback) {
numThreads,
};
return await createEngine(initData, progressCallback);
return await createEngine(initData, progressCallback);
}
this.ml = class extends ExtensionAPI {
@@ -234,6 +237,16 @@ this.ml = class extends ExtensionAPI {
const generated = cutAtDuplicateWords((res[0]["generated_text"] || "").trim());
return generated;
},
async clearCache() {
self.embeddingEngine = null;
self.topicEngine = null;
await EngineProcess.destroyMLEngine();
await new ModelHub().purgeDatabase();
await OPFS.remove("mlRuntimeFiles", { recursive: true });
return true;
}
}
}
@@ -56,6 +56,12 @@ port.onMessage.addListener(async (message) => {
sendJsonResultForRequest(requestId)(result);
break;
}
case "clearMlCache": {
const result = await browser.experiments.ml.clearCache();
sendJsonResultForRequest(requestId)(result);
break;
}
default:
throw new Error(`Unsupported ML action: ${message["action"]}`);
}
@@ -85,7 +85,14 @@
"description": "Array of document titles/content"
}
]
},
{
"name": "clearCache",
"type": "function",
"description": "Clear downloaded ML models and runtime files",
"async": true,
"parameters": []
}
]
}
]
]
@@ -131,4 +131,16 @@ class GeckoMlApiImpl(
})
}
}
override fun clearMlCache(callback: (Result<Unit>) -> Unit) {
MLEngineFeature.scheduleRequest("clearMlCache", JSONObject(), object : ResultConsumer<JSONObject> {
override fun success(result: JSONObject) {
callback(Result.success(Unit))
}
override fun error(errorCode: String, errorMessage: String?, errorDetails: Any?) {
callback(Result.failure(Exception("$errorCode $errorMessage $errorDetails")))
}
})
}
}
@@ -8315,6 +8315,7 @@ interface GeckoPrefApi {
interface GeckoMlApi {
fun predictDocumentTopic(documents: List<String>, callback: (Result<String>) -> Unit)
fun generateDocumentEmbeddings(documents: List<String>, callback: (Result<List<Any?>>) -> Unit)
fun clearMlCache(callback: (Result<Unit>) -> Unit)
companion object {
/** The codec used by GeckoMlApi. */
@@ -8365,6 +8366,23 @@ interface GeckoMlApi {
channel.setMessageHandler(null)
}
}
run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.flutter_mozilla_components.GeckoMlApi.clearMlCache$separatedMessageChannelSuffix", codec)
if (api != null) {
channel.setMessageHandler { _, reply ->
api.clearMlCache{ result: Result<Unit> ->
val error = result.exceptionOrNull()
if (error != null) {
reply.reply(GeckoPigeonUtils.wrapError(error))
} else {
reply.reply(GeckoPigeonUtils.wrapResult(null))
}
}
}
} else {
channel.setMessageHandler(null)
}
}
}
}
}