This tutorial will walk you through adding a new activity to your Flutter project, enabling it to support new guided interactions in your voice assistant app.
You'll need to:
- Create a new activity in
lib/activities.dart
. - Add assets (images) for the new activity.
- Update
LLMService
to recognize the new activity. - Create and configure a new template for the activity’s voice guidance.
- Compile and test your changes.
-
Open
lib/activities.dart
. -
Define your new activity by creating an
Activity
instance with properties likeactivityId
,name
,description
,requiredLevel
,category
,displayOrder
, andduration
.For example:
Activity myNewActivity = Activity( activityId: ActivityId.myNewActivity, // New activityId name: "My New Activity", // Name for display description: 'A new interactive activity to guide the user', // Brief description requiredLevel: 0, // Minimum level required (set to 0 for no requirement) category: ActivityCategory.dreamActivities, // Define a suitable category displayOrder: 2, // Order of display duration: 7, // Duration of the activity in minutes imagePath: 'assets/activities/my_new_activity_image.webp', // Path to the image );
-
Add the New Activity to the Activity List: Update the
initializeActivities
function to include your new activity:List<Activity> initializeActivities() { return [ introductionActivity, dreamAnalystActivity, myNewActivity, // Add here ]; }
-
Add an image file that represents your activity to the
assets/activities/
folder, naming it (e.g.,my_new_activity_image.webp
). -
Confirm that the asset is accessible by adding it to your
pubspec.yaml
under theassets
section:flutter: assets: - assets/activities/my_new_activity_image.webp
-
Open
lib/services/llm_service.dart
. -
Locate the
updateTemplate
function, which sets the template based on the activity’sactivityId
. -
Add a case for your new activity:
void updateTemplate(Activity activity) { switch (activity.activityId) { case ActivityId.introduction: llmChain.setTemplate(templateIntroduction); break; case ActivityId.dreamAnalyst: llmChain.setTemplate(templateDreamAnalyst); break; case ActivityId.myNewActivity: // New case for your activity llmChain.setTemplate(templateMindfulCompanion); // Set to your template break; default: // Handle other cases break; } }
-
Create a Template File:
- In
lib/llm_templates/activities/
, create a new file,example_my_new_activity_template.dart
.
- In
-
Define the Template:
-
Here’s a sample template structure based on an assistant guide. Customize it to match your new activity’s purpose.
String templateMindfulCompanion = """ {language} You are Whisper, the user’s assistant for this activity. Guide the user through structured, engaging steps. 1. **Start by asking the user for initial input or context related to the activity.** 2. **Provide feedback, suggestions, or prompts based on their responses.** 3. **Help the user by asking clarifying questions to keep the conversation natural.** ## USER DETAILS ## {user_information} ## END USER DETAILS ## ## SUMMARY OF PREVIOUS INTERACTIONS ## {session_history} ## END SUMMARY OF PREVIOUS INTERACTIONS ## ## CURRENT CONVERSATION ## {chat_history} Human: {input} AI: """;
-
-
Update the Template Export File:
-
Open
lib/llm_templates/all_templates.dart
and add an export statement for your new template file:export 'package:flutter_voice_friend/llm_templates/activities/example_my_new_activity_template.dart';
-
-
Compile and Test the Changes:
-
Update the list of activity IDs in
lib/models/activity.dart
by addingmyNewActivity
to theActivityId
enum:enum ActivityId { introduction, dreamAnalyst, myNewActivity }
-
After updating, recompile by running the following command to ensure the database and models are in sync:
dart run build_runner build
-
- Run the App: Launch your Flutter app to verify that the new activity appears in the list and functions as expected.
- Interact with the Assistant: Navigate to the activity and ensure that your assistant follows the new template for the voice-guided interaction.
- Check Debugging Logs: If you encounter any issues, use the debug logs to verify that the activity is loading, the template is setting correctly, and the assistant is responding as expected.
You’ve successfully added a new voice-guided activity to your Flutter-based assistant project! This process involved creating a new activity, adding assets, updating the logic for template selection, creating a custom activity template, and running a build to test.