Skip to content

refactor: migrate API key storage from API_KEY.txt to activity_root/data#97

Open
adarshkumar23 wants to merge 5 commits intosugarlabs:mainfrom
adarshkumar23:migrate-api-key-to-activity-root
Open

refactor: migrate API key storage from API_KEY.txt to activity_root/data#97
adarshkumar23 wants to merge 5 commits intosugarlabs:mainfrom
adarshkumar23:migrate-api-key-to-activity-root

Conversation

@adarshkumar23
Copy link
Copy Markdown

@adarshkumar23 adarshkumar23 commented Mar 23, 2026

Summary

Replaces the flat-file API_KEY.txt approach with Sugar's standard activity_root/data/ directory for persistent per-activity configuration, as discussed by @mebinthattil in #60.

Problem

The current code reads the API key from API_KEY.txt in the bundle's working directory. This has several issues:

  1. Risk of accidental commits — the key file sits alongside source code
  2. Not Sugar-standard — Sugar activities should use get_activity_root()/data/ for persistent configuration
  3. Unreliable path — the working directory may not be where the bundle lives depending on how the activity is launched

Changes

  • LLM.py: Read/write the API key from get_activity_root()/data/api_key instead of API_KEY.txt
  • LLM.py: Add save_api_key() public helper for programmatic key persistence (enables future settings UI)
  • LLM.py: Improve error messages to log the expected key file path
  • activity.py: Export save_api_key from the LLM import
  • README.md: Add "API Key Configuration" section documenting the new storage location

How it works

The data/ subdirectory of get_activity_root() survives across activity invocations and is the Sugar-standard location for per-activity persistent state. This is the same pattern already used in chat.py (line 249).

If no key is found, the activity logs the expected path and gracefully falls back to the on-device SLM or AIML brain — no crash.

Related

Replace the flat-file API_KEY.txt approach with Sugar's standard
activity_root/data/ directory for persistent per-activity configuration.

- LLM.py: Read/write API key from get_activity_root()/data/api_key
- LLM.py: Add save_api_key() helper for programmatic key persistence
- activity.py: Export save_api_key for future settings UI integration
- README.md: Add API key configuration section

Ref: sugarlabs#60 (comment)
Copy link
Copy Markdown
Member

@mebinthattil mebinthattil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for these changes, would love to see a screen recording of these changes in action inside sugar / screenshots of these changes working.

Also include info on how to request for an API key from Sugar-AI. Check the Sugar-AI docs for more context.

Comment thread LLM.py Outdated
Comment thread LLM.py Outdated

else:
print("Error, LLM did not respond")
print("Error, LLM did not respond") No newline at end of file
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No newline in EOF. Diff your changes before pushing from next time, you'll be able to catch these.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 57cb868. Here's the diff confirming the change:

Screenshot 2026-03-27 004539

Also noted — will run git diff --staged before every commit going forward.

Screenshot 2026-03-27 004556
Fixing.EOF.Line.in.LLM.py.and.Review.Comments.mp4

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was asking about a screen recording of you running these changes inside the activity and not just running LLM.py in your terminal.
Please send that video. Be sure to test it inside of sugar.

Comment thread README.md Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants