Skip to content

Commit 8a7af9a

Browse files
committed
feat: enhance annotation API to support optional message_id and content fields
1 parent 8a28515 commit 8a7af9a

File tree

3 files changed

+29
-58
lines changed

3 files changed

+29
-58
lines changed

api/controllers/console/app/annotation.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,10 @@ def get(self, app_id):
175175
api.model(
176176
"CreateAnnotationRequest",
177177
{
178-
"question": fields.String(required=True, description="Question text"),
179-
"answer": fields.String(required=True, description="Answer text"),
178+
"message_id": fields.String(description="Message ID (optional)"),
179+
"question": fields.String(description="Question text (required when message_id not provided)"),
180+
"answer": fields.String(description="Answer text (use 'answer' or 'content')"),
181+
"content": fields.String(description="Content text (use 'answer' or 'content')"),
180182
"annotation_reply": fields.Raw(description="Annotation reply data"),
181183
},
182184
)
@@ -193,11 +195,14 @@ def post(self, app_id):
193195
app_id = str(app_id)
194196
parser = (
195197
reqparse.RequestParser()
196-
.add_argument("question", required=True, type=str, location="json")
197-
.add_argument("answer", required=True, type=str, location="json")
198+
.add_argument("message_id", required=False, type=str, location="json")
199+
.add_argument("question", required=False, type=str, location="json")
200+
.add_argument("answer", required=False, type=str, location="json")
201+
.add_argument("content", required=False, type=str, location="json")
202+
.add_argument("annotation_reply", required=False, type=dict, location="json")
198203
)
199204
args = parser.parse_args()
200-
annotation = AppAnnotationService.insert_app_annotation_directly(args, app_id)
205+
annotation = AppAnnotationService.up_insert_app_annotation_from_message(args, app_id)
201206
return annotation
202207

203208
@setup_required

api/controllers/console/app/message.py

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,18 @@
1616
from controllers.console.explore.error import AppSuggestedQuestionsAfterAnswerDisabledError
1717
from controllers.console.wraps import (
1818
account_initialization_required,
19-
cloud_edition_billing_resource_check,
2019
edit_permission_required,
2120
setup_required,
2221
)
2322
from core.app.entities.app_invoke_entities import InvokeFrom
2423
from core.errors.error import ModelCurrentlyNotSupportError, ProviderTokenNotInitError, QuotaExceededError
2524
from core.model_runtime.errors.invoke import InvokeError
2625
from extensions.ext_database import db
27-
from fields.conversation_fields import annotation_fields, message_detail_fields
26+
from fields.conversation_fields import message_detail_fields
2827
from libs.helper import uuid_value
2928
from libs.infinite_scroll_pagination import InfiniteScrollPagination
3029
from libs.login import current_account_with_tenant, login_required
3130
from models.model import AppMode, Conversation, Message, MessageAnnotation, MessageFeedback
32-
from services.annotation_service import AppAnnotationService
3331
from services.errors.conversation import ConversationNotExistsError
3432
from services.errors.message import MessageNotExistsError, SuggestedQuestionsAfterAnswerDisabledError
3533
from services.message_service import MessageService
@@ -194,45 +192,6 @@ def post(self, app_model):
194192
return {"result": "success"}
195193

196194

197-
@console_ns.route("/apps/<uuid:app_id>/annotations")
198-
class MessageAnnotationApi(Resource):
199-
@api.doc("create_message_annotation")
200-
@api.doc(description="Create message annotation")
201-
@api.doc(params={"app_id": "Application ID"})
202-
@api.expect(
203-
api.model(
204-
"MessageAnnotationRequest",
205-
{
206-
"message_id": fields.String(description="Message ID"),
207-
"question": fields.String(required=True, description="Question text"),
208-
"answer": fields.String(required=True, description="Answer text"),
209-
"annotation_reply": fields.Raw(description="Annotation reply"),
210-
},
211-
)
212-
)
213-
@api.response(200, "Annotation created successfully", annotation_fields)
214-
@api.response(403, "Insufficient permissions")
215-
@marshal_with(annotation_fields)
216-
@get_app_model
217-
@setup_required
218-
@login_required
219-
@cloud_edition_billing_resource_check("annotation")
220-
@account_initialization_required
221-
@edit_permission_required
222-
def post(self, app_model):
223-
parser = (
224-
reqparse.RequestParser()
225-
.add_argument("message_id", required=False, type=uuid_value, location="json")
226-
.add_argument("question", required=True, type=str, location="json")
227-
.add_argument("answer", required=True, type=str, location="json")
228-
.add_argument("annotation_reply", required=False, type=dict, location="json")
229-
)
230-
args = parser.parse_args()
231-
annotation = AppAnnotationService.up_insert_app_annotation_from_message(args, app_model.id)
232-
233-
return annotation
234-
235-
236195
@console_ns.route("/apps/<uuid:app_id>/annotations/count")
237196
class MessageAnnotationCountApi(Resource):
238197
@api.doc("get_annotation_count")

api/services/annotation_service.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,41 +32,48 @@ def up_insert_app_annotation_from_message(cls, args: dict, app_id: str) -> Messa
3232

3333
if not app:
3434
raise NotFound("App not found")
35+
36+
answer = args.get("answer") or args.get("content")
37+
if not answer:
38+
raise ValueError("Either 'answer' or 'content' must be provided")
39+
3540
if args.get("message_id"):
3641
message_id = str(args["message_id"])
37-
# get message info
3842
message = db.session.query(Message).where(Message.id == message_id, Message.app_id == app.id).first()
3943

4044
if not message:
4145
raise NotFound("Message Not Exists.")
4246

47+
question = args.get("question") or message.query or ""
48+
4349
annotation: MessageAnnotation | None = message.annotation
44-
# save the message annotation
4550
if annotation:
46-
annotation.content = args["answer"]
47-
annotation.question = args["question"]
51+
annotation.content = answer
52+
annotation.question = question
4853
else:
4954
annotation = MessageAnnotation(
5055
app_id=app.id,
5156
conversation_id=message.conversation_id,
5257
message_id=message.id,
53-
content=args["answer"],
54-
question=args["question"],
58+
content=answer,
59+
question=question,
5560
account_id=current_user.id,
5661
)
5762
else:
58-
annotation = MessageAnnotation(
59-
app_id=app.id, content=args["answer"], question=args["question"], account_id=current_user.id
60-
)
63+
question = args.get("question")
64+
if not question:
65+
raise ValueError("'question' is required when 'message_id' is not provided")
66+
67+
annotation = MessageAnnotation(app_id=app.id, content=answer, question=question, account_id=current_user.id)
6168
db.session.add(annotation)
6269
db.session.commit()
63-
# if annotation reply is enabled , add annotation to index
70+
6471
annotation_setting = db.session.query(AppAnnotationSetting).where(AppAnnotationSetting.app_id == app_id).first()
6572
assert current_tenant_id is not None
6673
if annotation_setting:
6774
add_annotation_to_index_task.delay(
6875
annotation.id,
69-
args["question"],
76+
annotation.question,
7077
current_tenant_id,
7178
app_id,
7279
annotation_setting.collection_binding_id,

0 commit comments

Comments
 (0)