feat: add knowledge_ids field to ApplicationVersion and populate from ResourceMapping - #6532
feat: add knowledge_ids field to ApplicationVersion and populate from ResourceMapping#6532shaohuzhang1 wants to merge 2 commits into
Conversation
|
Seems you are using me but didn't get OPENAI_API_KEY seted in Variables/Secrets for this repo. you could follow readme for more information |
There was a problem hiding this comment.
Pull request overview
Adds a persisted knowledge_ids snapshot to ApplicationVersion so simple-application chat startup can read dataset bindings from the published version (instead of querying ResourceMapping on every request), with a migration to backfill existing data.
Changes:
- Add
knowledge_idsJSONField toApplicationVersion. - Backfill
knowledge_idsfor existing SIMPLE applications fromResourceMappingvia a data migration. - Populate
knowledge_idsduring SIMPLE application publish; useApplicationVersion.knowledge_idsfor non-debug simple chat open/reopen paths.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| apps/chat/serializers/chat.py | Switches simple chat open/reopen to use ApplicationVersion.knowledge_ids when not in debug mode. |
| apps/application/serializers/application.py | Stores knowledge_ids into the newly created ApplicationVersion for SIMPLE app publishes; minor formatting changes. |
| apps/application/models/application.py | Adds the knowledge_ids field to the ApplicationVersion model. |
| apps/application/migrations/0014_applicationversion_knowledge_ids.py | Adds the DB column and backfills knowledge_ids for SIMPLE apps from ResourceMapping. |
Comments suppressed due to low confidence (1)
apps/application/serializers/application.py:1853
- This field definition exceeds the repository's configured Python max line length (120) (see
.editorconfig). Please wrap it to match the codebase style.
file_clean_time = serializers.IntegerField(required=True, min_value=1, max_value=100000, label=_("File clean time"))
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| application_version = QuerySet(ApplicationVersion).filter(application_id=application_id).order_by( | ||
| '-create_time')[0:1].first() | ||
| knowledge_id_list = application_version.knowledge_ids |
| id_list = list( | ||
| QuerySet(Application) | ||
| .filter(id__in=id_list, workspace_id=workspace_id) | ||
| .values_list("id", flat=True) | ||
| QuerySet(Application).filter(id__in=id_list, workspace_id=workspace_id).values_list("id", flat=True) | ||
| ) |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
Seems you are using me but didn't get OPENAI_API_KEY seted in Variables/Secrets for this repo. you could follow readme for more information |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (6)
apps/chat/serializers/chat.py:507
application_versionis re-queried here and then dereferenced without a null check. Even though the caller checks for a published version, this extra query introduces a small race window (or alternate call path) that can raise an AttributeError whenapplication_versionis None. Add a guard and normalizeknowledge_idsto a list.
application_version = QuerySet(ApplicationVersion).filter(application_id=application.id).order_by(
'-create_time')[0:1].first()
knowledge_id_list = application_version.knowledge_ids
apps/chat/serializers/chat.py:600
- Same issue as above:
application_versionis queried again and then used without checking for None. If the version is deleted between the earlier publication check and this query, this will crash. Add a guard and normalizeknowledge_idsto a list.
application_version = QuerySet(ApplicationVersion).filter(application_id=application_id).order_by(
'-create_time')[0:1].first()
knowledge_id_list = application_version.knowledge_ids
apps/application/serializers/application.py:1795
- This refactor collapses a multi-line queryset chain into a long single line, which is inconsistent with the surrounding style in this file (e.g., application.py:302-315 wraps long calls) and makes it harder to read/review. Consider restoring the multi-line formatting.
id_list = list(
QuerySet(Application).filter(id__in=id_list, workspace_id=workspace_id).values_list("id", flat=True)
)
apps/application/serializers/application.py:1280
- This persists
knowledge_idsdirectly fromResourceMappingwithout de-duplication. Sinceresource_mappinghas no uniqueness constraint, duplicates can be stored and later expand__inqueries / downstream lists unnecessarily. Consider de-duping while preserving order (migration already de-dupes viadict.fromkeys).
if application.type == ApplicationTypeChoices.SIMPLE:
work_flow_version.knowledge_ids = [
str(row.target_id)
for row in QuerySet(ResourceMapping).filter(
source_id=str(application.id), source_type="APPLICATION", target_type="KNOWLEDGE"
)
]
apps/application/migrations/0014_applicationversion_knowledge_ids.py:44
- This data migration materializes all matching
ApplicationVersionrows into an in-memoryupdateslist before a singlebulk_update, and iterates over all versions even though only SIMPLE applications are relevant. On large datasets this can consume a lot of memory/time during migrations. Filter to the relevant versions and flushbulk_updatein batches.
updates = []
for obj in ApplicationVersion.objects.using(db_alias).iterator():
app_id = str(obj.application_id)
if app_id not in simple_application_ids:
continue
knowledge_ids = mapping.get(app_id)
if knowledge_ids:
obj.knowledge_ids = knowledge_ids
updates.append(obj)
if updates:
ApplicationVersion.objects.using(db_alias).bulk_update(
updates, ["knowledge_ids"], batch_size=500
)
apps/application/serializers/application.py:1853
- This line exceeds typical line-length/readability patterns used elsewhere in this file (e.g., multi-line serializer field declarations at application.py:302-304). Keeping it multi-line avoids overly long lines and matches the existing style.
file_clean_time = serializers.IntegerField(required=True, min_value=1, max_value=100000, label=_("File clean time"))
feat: add knowledge_ids field to ApplicationVersion and populate from ResourceMapping