Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 54 additions & 26 deletions guides/AI_COACH.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,48 @@

The AI features in OpenStrap Edge (the Coach chat, the morning and evening
briefings, and the pre-sleep journal) are all powered by a model **you**
provide. There's no OpenStrap AI service. There are two ways to set it up:
paste in an API key for any OpenAI-compatible provider, or
[sign in with your ChatGPT plan](#use-your-chatgpt-plan). Either way the app
talks to that provider directly.
choose.
You can [use an API provider or a model you host](#setup-with-an-api-key),
[sign in with ChatGPT](#use-your-chatgpt-plan), or use
[Apple Intelligence](#use-apple-intelligence).

Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
Your key is stored in the device keychain/keystore and is only ever sent to
the base URL you configure — never to OpenStrap.

The same goes for your data: your chat messages, and the health metrics the
coach's tools pull from the on-device database to answer them, are sent to
the provider you configure. Before enabling the feature, check that
provider's data retention and training policies — or point the app at a
model you host yourself.
When you use a cloud provider, it receives your chat messages and the
health metrics the coach reads to answer them. Check the provider's data
retention and training policies before you connect it. A model you host
receives the data at the server address you enter.

## Open AI settings

On **Today**, tap **Set up the coach**. To change your provider later,
tap your avatar and open **Connected apps → AI coach**.

[Download the in-app setup walkthrough (MP4)](images/ai-coach/setup.mp4).

The walkthrough shows one provider-choice step. Tap a provider to move on,
or scroll down to configure the provider already selected. Scroll back to
the picker to change your choice.

## Ask your first question

After saving a model, sync your band so the coach has data to read.
Return to **Today** and tap **Ask the coach**.
Try asking “How has my sleep been this week?”

To review the data the app records as shared, open the Coach menu (**⋯**)
and tap **Briefing, and what was sent**.

## Use your ChatGPT plan

If you pay for ChatGPT Plus or Pro, you can skip the API key: open
**AI Coach → settings**, tap **ChatGPT** and sign in. Eligible coach, briefing and journal requests then use
the usage included in your ChatGPT plan instead of API billing. Tap **List
models** and pick one, then **Save**.
1. In **AI settings**, tap **ChatGPT**. If prompted, tap **Continue with ChatGPT**.
2. Review the terms, sign in in the browser, and allow OpenStrap to use your ChatGPT plan.
3. Back in Edge, dismiss the welcome message if it appears. Tap **List
models** if the list is empty, including when switching back to an
account you already authorized. Choose a model and tap **Save**.

Eligible requests use your ChatGPT plan or credits balance.

Before the sign-in starts, the app links OpenAI's
[Terms of Use](https://openai.com/policies/terms-of-use/) and
Expand All @@ -46,20 +68,26 @@ your use of ChatGPT here, so read them before you continue.
OpenStrap in ChatGPT settings. The coach then stays off until you sign in
again or pick another provider.

## Setup with an API key
## Use Apple Intelligence

<img src="images/ai-coach/settings.png" width="300" align="right">
On a supported iPhone running iOS 26 or later, turn on Apple Intelligence
in iOS Settings. In Edge, turn on **Apple Intelligence on this iPhone** in
**AI settings**. The switch saves your choice. Apple Intelligence processes
your questions and the health data it reads on your iPhone. It can answer
questions about the last two weeks of data.

Open **AI Coach → settings** and fill in three things:
## Setup with an API key

1. **Base URL** — your provider's OpenAI-compatible endpoint (see the table
below). The app calls `{base URL}/chat/completions`, so enter the base
only, without the `/chat/completions` part.
2. **API key** — from your provider's dashboard.
3. **Model** — tap **Fetch** to pull your provider's live model list and pick
one, or just type a model id and it's used as-is.
1. In **AI settings**, choose a preset under **Where the model runs**.
Check **Base URL** against your provider's endpoint. Enter the API base URL
from the table below.
2. Paste a key from your provider's dashboard into **API key**. For a
self-hosted server, use its network address and enter a key if it requires one.
3. Tap **List models** and choose a model that supports tool calling. If the
provider returns no list, type the exact model ID in **Model**.
4. Tap **Save**.

Tap **Save** and you're done.
For OpenRouter, you can also use **Sign in with OpenRouter** to fill in the key.

## Providers

Expand All @@ -68,7 +96,7 @@ Tap **Save** and you're done.
You don't need a paid account to try the AI features.
[OpenRouter](https://openrouter.ai) offers a set of
[free models](https://openrouter.ai/collections/free-models) — sign up, create
a key, set the base URL to `https://openrouter.ai/api/v1`, tap **Fetch** and
a key, set the base URL to `https://openrouter.ai/api/v1`, tap **List models** and
pick a model whose id ends in `:free`. Free models are rate-limited and vary
in quality, so prefer one of the larger ones and check it supports tool
calling. Running a model on your own hardware via Ollama or LM Studio (see
Expand Down Expand Up @@ -118,13 +146,13 @@ Notes:

## Troubleshooting

- **"Enter your API key first"** — Fetch needs the key filled in before it
- **"Enter your API key first"** — List models needs the key filled in before it
can list models.
- **"Provider returned no models"** — the provider answered with an empty
list; type the model id manually.
- **"Models request failed (404)"** — the gateway doesn't implement
`GET /models` (or the base URL is wrong). If the base URL checks out,
skip Fetch and type the model id manually.
skip List models and type the model id manually.
- **Provider error 401/403** — wrong key, or the key isn't enabled for the
model you picked.
- **Provider error 404** — the base URL is usually wrong; check it against
Expand Down
Binary file added guides/images/ai-coach/setup.mp4
Binary file not shown.
7 changes: 7 additions & 0 deletions lib/ui2/guides/guide.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ class GuideStep {
/// Several choices visited by the moving hand; any can perform the step.
final GuideMultiTarget? multi;

/// A divider on the same screen as the previous step. Choose this step
/// when the divider passes the middle of the viewport, or when the previous
/// step's target is tapped. Scrolling back above the divider returns to the
/// previous step. The divider must stay laid out while the page scrolls.
final GuideTarget? enterBelow;

const GuideStep({
required this.id,
required this.target,
Expand All @@ -126,6 +132,7 @@ class GuideStep {
required this.caption,
this.stillCaption,
this.multi,
this.enterBelow,
});

/// Anchor presence for compatibility/review fixtures. The live run uses its
Expand Down
67 changes: 65 additions & 2 deletions lib/ui2/guides/guide_run.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// the top page after every frame and draws its state.

import 'package:flutter/foundation.dart' show ChangeNotifier, visibleForTesting;
import 'package:flutter/widgets.dart' show Element, IndexedStack;
import 'package:flutter/widgets.dart' show Axis, Element, IndexedStack, Scrollable, ScrollPosition;

import '../../l10n/app_localizations.dart';
import '../app_shell.dart' show AppShell;
Expand Down Expand Up @@ -77,6 +77,16 @@ class GuideRun extends ChangeNotifier {
GuideStep? get currentStep => !_exited && _state is GuideAtStep
? variant.steps[(_state as GuideAtStep).index] : null;

/// On either side of a divider, scrolling itself chooses the active step.
bool get followsViewport {
if (_state case GuideAtStep(:final index)) {
final steps = variant.steps;
return steps[index].enterBelow != null ||
(index + 1 < steps.length && steps[index + 1].enterBelow != null);
}
return false;
}

/// The index of the HIGHEST step of [variant] whose anchor resolves
/// ([TargetFound]) under [root] and whose tap requirement is met; null when none does.
/// ↳ CLAIM(run-match-furthest)
Expand Down Expand Up @@ -117,6 +127,41 @@ class GuideRun extends ChangeNotifier {
return onstageElements(content).skip(2).firstOrNull ?? root;
}

ScrollPosition? _dividerScroll;
double? _heldAtPixels;

int? _stepAtDivider(List<int> exact, Element root, AppLocalizations l) {
for (final index in exact) {
final divider = variant.steps[index].enterBelow;
if (divider == null || !exact.contains(index - 1)) continue;
final box = laidOutBox(root);
if (box == null) return null;
final location = divider.resolve(root, l);
if (location is! TargetFound) return index - 1;

final element = location.element;
final scroll = element == null ? null : Scrollable.maybeOf(element, axis: Axis.vertical)?.position;
if (!identical(scroll, _dividerScroll)) {
_dividerScroll = scroll;
_heldAtPixels = null;
}
if (location.rect.top <= box.size.height / 2) {
_heldAtPixels = null;
return index;
}
// A tap advances immediately. An upward scroll can still undo it
// before the divider reaches the middle of the screen.
final pixels = scroll?.pixels;
if (_heldAtPixels case final previous? when pixels != null && pixels >= previous) {
_heldAtPixels = pixels;
return index;
}
_heldAtPixels = null;
return index - 1;
}
return null;
}

/// Called after every frame with the top PAGE's element, or null while a
/// dialog / sheet / picker is on top (the run holds its state then: the
/// user is acting on the step, not leaving it). Notifies only on change.
Expand All @@ -137,6 +182,7 @@ class GuideRun extends ChangeNotifier {
_anchorMatches(variant.steps[i].anchor.resolve(_contentOf(topPage), l)),
);
index =
_stepAtDivider(exact, _contentOf(topPage), l) ??
anchored.lastOrNull ??
(exact.contains(_lastIndex) ? _lastIndex : exact.first);
} else {
Expand Down Expand Up @@ -174,10 +220,13 @@ class GuideRun extends ChangeNotifier {
if (index != null) {
_lastIndex = index;
_matchedPages[key] = index;
if (variant.steps[index].enterBelow == null) _heldAtPixels = null;
if (_state case GuideAtStep(index: final old) when old == index) return;
_state = GuideAtStep(index);
} else {
if (_state is GuideOffPath) return;
_heldAtPixels = null;
_dividerScroll = null;
_state = GuideOffPath(_lastIndex);
}
notifyListeners();
Expand All @@ -187,9 +236,21 @@ class GuideRun extends ChangeNotifier {
/// On any other step: no change (the next frame decides).
void targetTapped() {
if (_exited) return;
if (_state case GuideAtStep(:final index)) _tappedSteps.add(index);
if (_state case GuideAtStep(:final index)) {
_tappedSteps.add(index);
final next = index + 1;
if (next < variant.steps.length && variant.steps[next].enterBelow != null) {
_lastIndex = next;
_heldAtPixels = _dividerScroll?.pixels;
_state = GuideAtStep(next);
notifyListeners();
return;
}
}
if (_state case GuideAtStep(:final index) when index == variant.steps.length - 1) {
_matchedPages.clear();
_dividerScroll = null;
_heldAtPixels = null;
_state = const GuideFinished();
notifyListeners();
}
Expand All @@ -199,6 +260,8 @@ class GuideRun extends ChangeNotifier {
void exit() {
if (_exited) return;
_exited = true;
_dividerScroll = null;
_heldAtPixels = null;
_matchedPages.clear();
notifyListeners();
}
Expand Down
5 changes: 3 additions & 2 deletions lib/ui2/guides/guide_target.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ final class TargetFound extends TargetResolution {
final Rect rect;
/// The actual control used for pointer hit-path validation by live guides.
final RenderBox renderObject;
const TargetFound(this.rect, {required this.renderObject});
final Element? element;
const TargetFound(this.rect, {required this.renderObject, this.element});
}

/// No match in the built tree, but a scrollable under the root could still
Expand Down Expand Up @@ -147,7 +148,7 @@ sealed class GuideTarget {
if (w is GuideAccordionBody && w.offstage) closed.add(w);
return true;
});
return closed.isEmpty ? TargetFound(rect, renderObject: laidOutBox(element)!)
return closed.isEmpty ? TargetFound(rect, renderObject: laidOutBox(element)!, element: element)
: TargetBehindAccordion(this, closed.reversed.toList());
}

Expand Down
5 changes: 4 additions & 1 deletion lib/ui2/guides/live_guide_overlay.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1297,7 +1297,10 @@ class LiveGuideOverlayState extends State<LiveGuideOverlay> with TickerProviderS
final retry = _searchingPage == null && _scrollAtReveal == scrollSignature &&
((targetNow == null && _searchSignature != signature) ||
(outside && _lastRevealRect != targetNow));
if (page != null && step != null && (newStep || retry) && !_touched) {
if (page != null && step != null && widget.run.followsViewport) {
// A viewport-following step shows focus without moving the page.
if (_reveal.value != 1) _reveal.value = 1;
} else if (page != null && step != null && (newStep || retry) && !_touched) {
_revealedPage = page;
_revealedStep = step;
_revealedHeader = accordionHop?.headerKey;
Expand Down
Loading