Repository navigation
feat(mobile): show agent activity in an Android home screen widget #35965
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
Garberchov
wants to merge
8
commits into
pingdotgg:main
Choose a base branch
from
Garberchov:fix/android-agent-activity-widget
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.
+480
−9
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7c261c6
feat(mobile): show agent activity in an Android home screen widget
Garberchov b85fdc7
fix(mobile): match the Android Agent Activity widget to the iOS layout
Garberchov e89fd32
Repaint the Android Agent Activity widget from relay pushes
Garberchov b5c1b55
Merge remote-tracking branch 'github/main' into fix/android-agent-act…
maria-rcks 530c9d8
fix(mobile): route Android widget taps through native intents
maria-rcks f7733fd
fix(mobile): preserve widget card navigation
maria-rcks e79798d
Merge remote-tracking branch 'origin/main' into fix/android-agent-act…
juliusmarminge 2cde272
fix(mobile): document the Android widget's two-hour staleness
juliusmarminge 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
93 changes: 93 additions & 0 deletions
93
...ations/android/src/main/java/expo/modules/t3agentnotifications/AgentActivityWidgetSync.kt
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,93 @@ | ||
| package expo.modules.t3agentnotifications | ||
|
|
||
| import android.content.ComponentName | ||
| import android.content.Context | ||
| import android.content.Intent | ||
| import java.text.SimpleDateFormat | ||
| import java.util.Date | ||
| import java.util.Locale | ||
| import java.util.TimeZone | ||
| import org.json.JSONArray | ||
| import org.json.JSONObject | ||
|
|
||
| /** | ||
| * Repaints the Agent Activity home screen widget from a relay push, so it stays current | ||
| * while the app is closed. The app's foreground refresh still overwrites these props with | ||
| * the full snapshot; this only keeps the widget from sitting on "Out of date" in between. | ||
| */ | ||
| internal object AgentActivityWidgetSync { | ||
| private const val WIDGET = "AgentActivity" | ||
| private const val WIDGET_STORE = "expo.modules.widgets" | ||
| private const val PROPS_KEY = "__expo_widgets_${WIDGET}_props" | ||
| private const val PUSH_AT_KEY = "t3_agent_activity_push_at" | ||
|
|
||
| // Pushes arrive only on changes; match the relay's running-row TTL (and the app's | ||
| // AGENT_ACTIVITY_ANDROID_WIDGET_STALE_AFTER_MS) instead of flagging quiet agents. | ||
| private const val STALE_AFTER_MS = 2 * 60 * 60 * 1000L | ||
| private const val EXPIRY_RECEIVER = "expo.modules.t3widgetexpiry.WidgetExpiryReceiver" | ||
| private const val EXPIRY_NAME = "expo.modules.t3widgetexpiry.NAME" | ||
| private const val EXPIRY_DEADLINES = "expo.modules.t3widgetexpiry.DEADLINES" | ||
|
|
||
| fun update(context: Context, data: Map<String, String>, updatedAt: Long) { | ||
| val store = context.applicationContext.getSharedPreferences(WIDGET_STORE, Context.MODE_PRIVATE) | ||
| // FCM can deliver out of order; an older push must not replace newer rows. | ||
| if (updatedAt <= store.getLong(PUSH_AT_KEY, 0)) return | ||
| val rows = activityRows(data) | ||
| val path = data["activity_path"].orEmpty() | ||
| val scheme = context.getSharedPreferences("t3-agent-notifications", Context.MODE_PRIVATE) | ||
| .getString("scheme", "t3code") ?: "t3code" | ||
| val updated = iso(updatedAt) | ||
| val current = JSONArray() | ||
| val stale = JSONArray() | ||
| var inFlight = false | ||
| rows.forEachIndexed { index, row -> | ||
| val phase = widgetPhase(ActivityPhase.forStatus(row.status)) | ||
| val finished = phase == "completed" || phase == "failed" | ||
| if (!finished) inFlight = true | ||
| fun json(phase: String, status: String) = JSONObject() | ||
| .put("environmentId", "push") | ||
| .put("threadId", index.toString()) | ||
| .put("projectTitle", row.project) | ||
| .put("threadTitle", row.title) | ||
| .put("modelTitle", "") | ||
| .put("phase", phase) | ||
| .put("status", status) | ||
| .put("updatedAt", updated) | ||
| .put("deepLink", data["activity_row_path_$index"] ?: if (index == 0) path else "") | ||
| current.put(json(phase, row.status)) | ||
| stale.put(if (finished) json(phase, row.status) else json("stale", "Out of date")) | ||
| } | ||
| val staleAt = updatedAt + STALE_AFTER_MS | ||
| val props = JSONObject() | ||
| .put("appUrl", "$scheme://") | ||
| .put("title", data["activity_title"].orEmpty()) | ||
| .put("subtitle", if (rows.isEmpty()) "No active agents" else "") | ||
| .put("activeCount", data["activity_active_count"]?.toIntOrNull() ?: 0) | ||
| .put("updatedAt", updated) | ||
| .put("activities", current) | ||
| if (inFlight) props.put("stale", JSONObject().put("at", staleAt).put("activities", stale)) | ||
| store.edit().putString(PROPS_KEY, props.toString()).putLong(PUSH_AT_KEY, updatedAt).commit() | ||
|
|
||
| // The expiry receiver repaints now and sets the alarm that swaps in the stale rows. | ||
| context.sendBroadcast( | ||
| Intent().setComponent(ComponentName(context.packageName, EXPIRY_RECEIVER)) | ||
| .putExtra(EXPIRY_NAME, WIDGET) | ||
| .putExtra(EXPIRY_DEADLINES, if (inFlight) longArrayOf(staleAt) else LongArray(0)) | ||
| ) | ||
| } | ||
|
|
||
| private fun widgetPhase(phase: ActivityPhase?) = when (phase) { | ||
| ActivityPhase.STARTING -> "starting" | ||
| ActivityPhase.APPROVAL -> "waiting_for_approval" | ||
| ActivityPhase.INPUT -> "waiting_for_input" | ||
| ActivityPhase.STALE -> "stale" | ||
| ActivityPhase.COMPLETED -> "completed" | ||
| ActivityPhase.FAILED -> "failed" | ||
| ActivityPhase.RUNNING, null -> "running" | ||
| } | ||
|
|
||
| private fun iso(millis: Long) = | ||
| SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US) | ||
| .apply { timeZone = TimeZone.getTimeZone("UTC") } | ||
| .format(Date(millis)) | ||
| } | ||
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
76 changes: 76 additions & 0 deletions
76
...ns/android/src/test/java/expo/modules/t3agentnotifications/AgentActivityWidgetSyncTest.kt
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,76 @@ | ||
| package expo.modules.t3agentnotifications | ||
|
|
||
| import android.content.Context | ||
| import org.json.JSONObject | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertFalse | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.RobolectricTestRunner | ||
| import org.robolectric.Shadows.shadowOf | ||
| import org.robolectric.annotation.Config | ||
|
|
||
| @RunWith(RobolectricTestRunner::class) | ||
| @Config(sdk = [24, 36], manifest = Config.NONE) | ||
| class AgentActivityWidgetSyncTest : AgentNotificationsFixture() { | ||
| private fun widgetStore() = | ||
| context.getSharedPreferences("expo.modules.widgets", Context.MODE_PRIVATE) | ||
|
|
||
| private fun props() = | ||
| JSONObject(requireNotNull(widgetStore().getString("__expo_widgets_AgentActivity_props", null))) | ||
|
|
||
| private fun push(updatedAt: Long, vararg lines: String) = mapOf( | ||
| "device_id" to "device", | ||
| "user_id" to "user", | ||
| "updated_at" to updatedAt.toString(), | ||
| "activity_title" to "2 active agents", | ||
| "activity_active_count" to "2", | ||
| "activity_path" to "/threads/env/first", | ||
| "activity_row_path_1" to "/threads/env/second", | ||
| ) + lines.mapIndexed { index, line -> "activity_line_$index" to line } | ||
|
|
||
| @Test | ||
| fun pushRepaintsTheWidget() { | ||
| val now = System.currentTimeMillis() | ||
|
|
||
| AgentNotifications.receive( | ||
| context, | ||
| push(now, "Approval\tShip it\tweb", "Working\tRefactor\tapi", "Done\tDocs\tweb") | ||
| ) | ||
|
|
||
| val props = props() | ||
| assertEquals(2, props.getInt("activeCount")) | ||
| val rows = props.getJSONArray("activities") | ||
| assertEquals( | ||
| listOf("waiting_for_approval", "running", "completed"), | ||
| (0 until rows.length()).map { rows.getJSONObject(it).getString("phase") } | ||
| ) | ||
| assertEquals("Refactor", rows.getJSONObject(1).getString("threadTitle")) | ||
| assertEquals("/threads/env/second", rows.getJSONObject(1).getString("deepLink")) | ||
| assertEquals("t3code-dev://", props.getString("appUrl")) | ||
| assertEquals("/threads/env/first", rows.getJSONObject(0).getString("deepLink")) | ||
| val stale = props.getJSONObject("stale") | ||
| assertEquals(now + 2 * 60 * 60 * 1000L, stale.getLong("at")) | ||
| assertEquals( | ||
| listOf("Out of date", "Out of date", "Done"), | ||
| (0 until 3).map { stale.getJSONArray("activities").getJSONObject(it).getString("status") } | ||
| ) | ||
| val broadcast = shadowOf(context).broadcastIntents.last() | ||
| assertEquals("expo.modules.t3widgetexpiry.WidgetExpiryReceiver", broadcast.component?.className) | ||
| assertEquals("AgentActivity", broadcast.getStringExtra("expo.modules.t3widgetexpiry.NAME")) | ||
| } | ||
|
|
||
| @Test | ||
| fun anOlderPushDoesNotReplaceNewerRows() { | ||
| val now = System.currentTimeMillis() | ||
| AgentNotifications.receive(context, push(now, "Done\tNewer\tweb")) | ||
| AgentNotifications.receive(context, push(now - 1000, "Working\tOlder\tweb")) | ||
|
|
||
| val props = props() | ||
| assertEquals( | ||
| "Newer", | ||
| props.getJSONArray("activities").getJSONObject(0).getString("threadTitle") | ||
| ) | ||
| assertFalse(props.has("stale")) | ||
| } | ||
| } |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the declaration spacing that blocks native lint.
The supplied pipeline reports
standard:spacing-between-declarations-with-commentshere. This causesvp run lint:mobileto fail. Add a blank line betweenPUSH_AT_KEYand this comment.GitHub Actions: CI / 2_Mobile Native Static Analysis.txt
[error] 23-23: ktlint failed during
vp run lint:mobile: declaration/comment spacing violation (standard:spacing-between-declarations-with-comments).GitHub Actions: CI / Mobile Native Static Analysis
[error] 23-23: ktlint failed: declaration with a comment needs an empty line between declarations (standard:spacing-between-declarations-with-comments). The
vp run lint:mobilestep failed because ktlint exited with code 1.Source: Pipeline failures