-
-
Notifications
You must be signed in to change notification settings - Fork 91
feat: Add Apple Intelligence on-device formatting #92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kentaro
wants to merge
7
commits into
amicalhq:main
Choose a base branch
from
kentaro:feat/apple-intelligence-formatting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3df09db
feat: Add Apple Intelligence on-device formatting via Foundation Models
kentaro 5de22b9
fix: Address review comments for Apple Intelligence integration
kentaro bbaaf11
fix: Cast maxTokens from Double to Int for GenerationOptions
kentaro 9390d6d
fix: Wrap user prompt to prevent on-device model from responding conv…
kentaro a097068
fix: Use amical-notes Markdown formatting for Apple Intelligence
kentaro 426f88f
fix: Add few-shot examples to amical-notes formatting rules
kentaro df11fe6
fix: Remove bold/italic markup from amical-notes formatting rules
kentaro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
apps/desktop/src/pipeline/providers/formatting/apple-intelligence-formatter.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { FormattingProvider, FormatParams } from "../../core/pipeline-types"; | ||
| import { logger } from "../../../main/logger"; | ||
| import { constructFormatterPrompt } from "./formatter-prompt"; | ||
| import type { NativeBridge } from "../../../services/platform/native-bridge-service"; | ||
|
|
||
| export class AppleIntelligenceFormatter implements FormattingProvider { | ||
| readonly name = "apple-intelligence"; | ||
|
|
||
| constructor(private nativeBridge: NativeBridge) {} | ||
|
|
||
| async format(params: FormatParams): Promise<string> { | ||
| try { | ||
| const { text, context } = params; | ||
| // Use amical-notes formatting for on-device models to ensure | ||
| // consistent Markdown output with smart structure detection. | ||
| const { systemPrompt } = constructFormatterPrompt(context, { | ||
| overrideAppType: "amical-notes", | ||
| }); | ||
|
|
||
| logger.pipeline.debug("Apple Intelligence formatting request", { | ||
| systemPrompt, | ||
| userPrompt: text, | ||
| }); | ||
|
|
||
| // Wrap user text explicitly so the on-device model treats it as | ||
| // text to format rather than a conversational query to respond to. | ||
| const userPrompt = `Format the following transcribed text:\n\n${text}`; | ||
|
|
||
| const result = await this.nativeBridge.call( | ||
| "generateWithFoundationModel", | ||
| { | ||
| systemPrompt, | ||
| userPrompt, | ||
| temperature: 0.1, | ||
| }, | ||
| 30000, | ||
| ); | ||
|
|
||
| logger.pipeline.debug("Apple Intelligence formatting raw response", { | ||
| rawResponse: result.content, | ||
| }); | ||
|
|
||
| // Extract formatted text from XML tags (same pattern as Ollama/OpenRouter) | ||
| const match = result.content.match( | ||
| /<formatted_text>([\s\S]*?)<\/formatted_text>/, | ||
| ); | ||
| const formattedText = match ? match[1] : result.content; | ||
|
|
||
| logger.pipeline.debug("Apple Intelligence formatting completed", { | ||
| original: text, | ||
| formatted: formattedText, | ||
| hadXmlTags: !!match, | ||
| }); | ||
|
|
||
| // If formatted text is empty, fall back to original text | ||
| // On-device models may return empty tags for short inputs | ||
| if (!formattedText || formattedText.trim().length === 0) { | ||
| logger.pipeline.warn( | ||
| "Apple Intelligence returned empty formatted text, using original", | ||
| ); | ||
| return text; | ||
| } | ||
|
|
||
| return formattedText; | ||
| } catch (error) { | ||
| logger.pipeline.error("Apple Intelligence formatting failed:", error); | ||
| return params.text; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...top/src/renderer/main/pages/settings/ai-models/components/apple-intelligence-provider.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| "use client"; | ||
| import { useState } from "react"; | ||
| import { Button } from "@/components/ui/button"; | ||
| import { Badge } from "@/components/ui/badge"; | ||
| import { Loader2 } from "lucide-react"; | ||
| import { cn } from "@/lib/utils"; | ||
| import { api } from "@/trpc/react"; | ||
| import { toast } from "sonner"; | ||
| import { useTranslation } from "react-i18next"; | ||
|
|
||
| export default function AppleIntelligenceProvider() { | ||
| const { t } = useTranslation(); | ||
| const [isSyncing, setIsSyncing] = useState(false); | ||
|
|
||
| const isMac = window.electronAPI?.platform === "darwin"; | ||
|
|
||
| const availabilityQuery = | ||
| api.models.checkAppleIntelligenceAvailability.useQuery(undefined, { | ||
| enabled: isMac, | ||
| }); | ||
|
|
||
| const utils = api.useUtils(); | ||
| const syncMutation = api.models.syncAppleIntelligenceModel.useMutation({ | ||
| onMutate: () => setIsSyncing(true), | ||
| onSuccess: (result) => { | ||
| setIsSyncing(false); | ||
| if (result.available) { | ||
| toast.success(t("settings.aiModels.appleIntelligence.toast.synced")); | ||
| utils.models.getSyncedProviderModels.invalidate(); | ||
| utils.models.getDefaultLanguageModel.invalidate(); | ||
| utils.models.getModels.invalidate(); | ||
| } else { | ||
| toast.error( | ||
| t("settings.aiModels.appleIntelligence.toast.notAvailable"), | ||
| ); | ||
| } | ||
| }, | ||
| onError: () => { | ||
| setIsSyncing(false); | ||
| toast.error(t("settings.aiModels.appleIntelligence.toast.syncFailed")); | ||
| }, | ||
| }); | ||
|
|
||
| if (!isMac) return null; | ||
|
|
||
| const available = availabilityQuery.data?.available ?? false; | ||
| const reason = availabilityQuery.data?.reason; | ||
| const isLoading = availabilityQuery.isLoading; | ||
|
|
||
| return ( | ||
| <div className="rounded-lg border p-4 space-y-3"> | ||
| <div className="flex items-center justify-between"> | ||
| <div className="flex items-center gap-2"> | ||
| <span className="font-medium"> | ||
| {t("settings.aiModels.providers.appleIntelligence")} | ||
| </span> | ||
| {isLoading ? ( | ||
| <Badge variant="secondary" className="text-xs"> | ||
| <Loader2 className="h-3 w-3 animate-spin mr-1" /> | ||
| {t("settings.aiModels.appleIntelligence.checking")} | ||
| </Badge> | ||
| ) : ( | ||
| <Badge | ||
| variant="secondary" | ||
| className={cn( | ||
| "text-xs flex items-center gap-1", | ||
| available | ||
| ? "text-green-500 border-green-500" | ||
| : "text-muted-foreground border-muted", | ||
| )} | ||
| > | ||
| <span | ||
| className={cn( | ||
| "w-2 h-2 rounded-full inline-block mr-1", | ||
| available ? "bg-green-500 animate-pulse" : "bg-muted-foreground", | ||
| )} | ||
| /> | ||
| {available | ||
| ? t("settings.aiModels.appleIntelligence.available") | ||
| : t("settings.aiModels.appleIntelligence.unavailable")} | ||
| </Badge> | ||
| )} | ||
| </div> | ||
| {available && ( | ||
| <Button | ||
| variant="outline" | ||
| size="sm" | ||
| onClick={() => syncMutation.mutate()} | ||
| disabled={isSyncing} | ||
| > | ||
| {isSyncing ? ( | ||
| <> | ||
| <Loader2 className="mr-2 h-4 w-4 animate-spin" /> | ||
| {t("settings.aiModels.appleIntelligence.syncing")} | ||
| </> | ||
| ) : ( | ||
| t("settings.aiModels.appleIntelligence.sync") | ||
| )} | ||
| </Button> | ||
| )} | ||
| </div> | ||
| <p className="text-xs text-muted-foreground"> | ||
| {available | ||
| ? t("settings.aiModels.appleIntelligence.descriptionAvailable") | ||
| : reason | ||
| ? t("settings.aiModels.appleIntelligence.descriptionUnavailable", { | ||
| reason, | ||
| }) | ||
| : t( | ||
| "settings.aiModels.appleIntelligence.descriptionUnavailableGeneric", | ||
| )} | ||
| </p> | ||
| </div> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.