Skip to content

refactor(api): migrate web chat endpoints to BaseModel#37962

Open
cqjjjzr wants to merge 2 commits into
langgenius:mainfrom
cqjjjzr:refac-basemodel-webchat
Open

refactor(api): migrate web chat endpoints to BaseModel#37962
cqjjjzr wants to merge 2 commits into
langgenius:mainfrom
cqjjjzr:refac-basemodel-webchat

Conversation

@cqjjjzr

@cqjjjzr cqjjjzr commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue and you have been assigned to it
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

See #28015 . Migrating Flask-RESTX request/response payload and query param models to Pydantic BaseModels.

Affected:

  • web chat, completion, file, workflow APIs

Also fixing generated response contract output to stay fresh for this slice.

Recommended/Required to merge: None.

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran make lint && make type-check (backend) and cd web && pnpm exec vp staged (frontend) to appease the lint gods

@cqjjjzr cqjjjzr requested review from Copilot and removed request for QuantumGhost, crazywoola, iamjoel, laipz8200, lyzno1 and zxhlyh June 25, 2026 13:28
@dosubot dosubot Bot added the size:XXL This PR changes 1000+ lines, ignoring generated files. label Jun 25, 2026
@dosubot dosubot Bot added the refactor label Jun 25, 2026
@github-actions github-actions Bot added the web This relates to changes on the web. label Jun 25, 2026
@github-actions

github-actions Bot commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-06-27 18:36:58.555014340 +0000
+++ /tmp/pyrefly_pr.txt	2026-06-27 18:36:43.710965525 +0000
@@ -624,8 +624,6 @@
    --> tests/test_containers_integration_tests/controllers/mcp/test_mcp.py:455:37
 ERROR Class member `SessionMatcher.__eq__` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
   --> tests/test_containers_integration_tests/controllers/service_api/dataset/test_dataset.py:28:9
-ERROR `in` is not supported between `Literal['webapp-logo']` and `None` [not-iterable]
-   --> tests/test_containers_integration_tests/controllers/web/test_site.py:136:16
 ERROR Argument `SimpleNamespace` is not assignable to parameter `webapp_settings` with type `WebAppSettings | None` in function `controllers.web.wraps._validate_user_accessibility` [bad-argument-type]
   --> tests/test_containers_integration_tests/controllers/web/test_wraps.py:75:33
 ERROR Argument `SimpleNamespace` is not assignable to parameter `webapp_settings` with type `WebAppSettings | None` in function `controllers.web.wraps._validate_user_accessibility` [bad-argument-type]
@@ -2446,14 +2444,6 @@
   --> tests/unit_tests/controllers/trigger/test_webhook.py:84:16
 ERROR Cannot index into `Response` [bad-index]
    --> tests/unit_tests/controllers/trigger/test_webhook.py:101:16
-ERROR Argument `SimpleNamespace` is not assignable to parameter `app_model` with type `App` in function `controllers.web.site.serialize_app_site_payload` [bad-argument-type]
-   --> tests/unit_tests/controllers/web/test_human_input_form.py:217:54
-ERROR Argument `SimpleNamespace` is not assignable to parameter `site` with type `Site` in function `controllers.web.site.serialize_app_site_payload` [bad-argument-type]
-   --> tests/unit_tests/controllers/web/test_human_input_form.py:217:65
-ERROR Argument `SimpleNamespace` is not assignable to parameter `app_model` with type `App` in function `controllers.web.site.serialize_app_site_payload` [bad-argument-type]
-   --> tests/unit_tests/controllers/web/test_human_input_form.py:251:54
-ERROR Argument `SimpleNamespace` is not assignable to parameter `site` with type `Site` in function `controllers.web.site.serialize_app_site_payload` [bad-argument-type]
-   --> tests/unit_tests/controllers/web/test_human_input_form.py:251:65
 ERROR Object of class `ModuleType` has no attribute `web_ns` [missing-attribute]
   --> tests/unit_tests/controllers/web/test_message_list.py:41:9
 ERROR Object of class `ModuleType` has no attribute `WebApiResource` [missing-attribute]
@@ -4836,10 +4826,10 @@
   --> tests/unit_tests/core/rag/extractor/test_excel_extractor.py:62:9
 ERROR Class member `_FieldExpression.__eq__` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
   --> tests/unit_tests/core/rag/extractor/test_excel_extractor.py:62:9
-ERROR Argument `SimpleNamespace` is not assignable to parameter `upload_file` with type `UploadFile` in function `core.rag.extractor.extract_processor.ExtractProcessor.load_from_upload_file` [bad-argument-type]
-  --> tests/unit_tests/core/rag/extractor/test_extract_processor.py:73:67
-ERROR Argument `SimpleNamespace` is not assignable to parameter `upload_file` with type `UploadFile` in function `core.rag.extractor.extract_processor.ExtractProcessor.load_from_upload_file` [bad-argument-type]
-  --> tests/unit_tests/core/rag/extractor/test_extract_processor.py:74:67
+ERROR No matching overload found for function `core.rag.extractor.extract_processor.ExtractProcessor.load_from_upload_file` called with arguments: (upload_file=SimpleNamespace, return_text=Literal[False]) [no-matching-overload]
+  --> tests/unit_tests/core/rag/extractor/test_extract_processor.py:73:54
+ERROR No matching overload found for function `core.rag.extractor.extract_processor.ExtractProcessor.load_from_upload_file` called with arguments: (upload_file=SimpleNamespace, return_text=Literal[True]) [no-matching-overload]
+  --> tests/unit_tests/core/rag/extractor/test_extract_processor.py:74:54
 ERROR Argument `SimpleNamespace` is not assignable to parameter `extract_setting` with type `ExtractSetting` in function `core.rag.extractor.extract_processor.ExtractProcessor.extract` [bad-argument-type]
    --> tests/unit_tests/core/rag/extractor/test_extract_processor.py:150:41
 ERROR Argument `SimpleNamespace` is not assignable to parameter `extract_setting` with type `ExtractSetting` in function `core.rag.extractor.extract_processor.ExtractProcessor.extract` [bad-argument-type]

@github-actions

github-actions Bot commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 51.48% 51.51% +0.02%
Strict coverage 51.00% 51.02% +0.02%
Typed symbols 30,824 30,833 +9
Untyped symbols 29,325 29,307 -18
Modules 2931 2931 0

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Migrates several web-facing Flask-RESTX request/response definitions toward Pydantic ResponseModel/BaseModel shapes, and updates the generated OpenAPI markdown + TypeScript contracts to match the new schemas (notably around human-input forms and web site metadata).

Changes:

  • Refactors multiple web controllers to return ResponseModel().model_dump(mode="json") / dump_response(...) payloads and updates response registrations accordingly.
  • Updates human-input form payload typing end-to-end (backend responses + frontend types/guards + generated contracts).
  • Adds OpenAPI post-processing in openapi-ts.api.config.ts to forge an “opaque” schema for responses that lack explicit schemas during codegen.

Reviewed changes

Copilot reviewed 21 out of 26 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
web/types/workflow.ts Makes form_token / expiration_time nullable for human-input form typing.
web/features/agent-v2/agent-detail/configure/components/preview/chat.tsx Simplifies debug message answer extraction (but currently breaks typing).
web/contract/console/trigger.ts Adjusts trigger subscription update contract output shape.
web/app/components/base/chat/chat/answer/human-input-content/human-input-form.tsx Prevents submitting human-input actions when formToken is missing.
packages/contracts/openapi-ts.api.config.ts Enhances OpenAPI normalization and zod resolver behavior for codegen.
packages/contracts/generated/api/web/zod.gen.ts Regenerated web zod schemas after OpenAPI/model changes.
packages/contracts/generated/api/web/types.gen.ts Regenerated web TS types after OpenAPI/model changes.
packages/contracts/generated/api/web/orpc.gen.ts Regenerated web ORPC router bindings after OpenAPI/model changes.
packages/contracts/generated/api/console/files/zod.gen.ts Regenerated console files zod schemas due to UploadConfig requiredness changes.
packages/contracts/generated/api/console/files/types.gen.ts Regenerated console files TS types due to UploadConfig requiredness changes.
api/tests/unit_tests/controllers/web/test_human_input_form.py Updates unit tests to match new controller return shapes and site disclaimer defaults.
api/tests/test_containers_integration_tests/controllers/web/test_site.py Updates integration tests for new WebAppSiteResponse / WebModelConfigResponse models.
api/services/file_service.py Tightens return type + simplifies get_file_preview implementation.
api/openapi/markdown/web-openapi.md Regenerated web OpenAPI markdown (new schemas / response descriptions).
api/openapi/markdown/console-openapi.md Regenerated console OpenAPI markdown (UploadConfig requiredness changes).
api/fields/file_fields.py Makes UploadConfig limits required (no longer nullable).
api/controllers/web/workflow.py Removes GeneratedAppResponse response model wiring; returns SimpleResultResponse JSON for stop.
api/controllers/web/site.py Replaces RESTX marshalling with Pydantic response models (WebSiteResponse, WebAppSiteResponse, etc.).
api/controllers/web/saved_message.py Minor formatting + documents 204 delete response.
api/controllers/web/remote_files.py Streamlines response dumping and return flow.
api/controllers/web/message.py Removes GeneratedAppResponse response model wiring; keeps compact response generation.
api/controllers/web/human_input_form.py Converts HITL endpoints to ResponseModel responses + richer OpenAPI docs.
api/controllers/web/human_input_file_upload.py Uses dump_response for local upload response and returns pre-dumped payload.
api/controllers/web/files.py Uses dump_response for uploaded file responses.
api/controllers/web/completion.py Removes GeneratedAppResponse response model wiring; keeps compact response generation + returns typed stop responses.
api/controllers/web/audio.py Introduces AudioToTextResponse ResponseModel and uses dump_response for ASR results.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 220 to 222
const getAgentDebugMessageAnswer = (message: MessageDetailResponse) => {
const legacyAnswer = (message as AgentDebugMessageWithLegacyAnswer).answer

return message.re_sign_file_url_answer ?? legacyAnswer ?? ''
return message.answer ?? ''
}
Comment on lines +217 to +221
const hasOpaqueContractSuccessResponse = (operation: SwaggerOperation) => {
return Object.entries(operation.responses ?? {}).some(([status, response]) => {
return /^2\d\d$/.test(status) && isObject(response) && isOpaqueContractResponse(response)
})
}
Comment on lines +223 to +242
const normalizeOpaqueContractResponses = (document: SwaggerDocument) => {
// Some backend endpoints has no schema (e.g. external) and will trap heyapi here
// So we forge an opaque schema here
for (const pathItem of Object.values(document.paths ?? {})) {
for (const [method, operation] of Object.entries(pathItem)) {
if (!operationMethods.has(method) || !isObject(operation))
continue

const swaggerOperation = operation as SwaggerOperation
if (!hasOpaqueContractSuccessResponse(swaggerOperation))
continue

Object.values(swaggerOperation.responses ?? {})
.filter(response => isObject(response) && isOpaqueContractResponse(response))
.forEach((response) => {
response.content = opaqueJsonContent()
})
}
}
}
@cqjjjzr cqjjjzr requested a review from JohnJyong as a code owner June 25, 2026 13:39
@cqjjjzr cqjjjzr force-pushed the refac-basemodel-webchat branch 5 times, most recently from ffe3a57 to eb199e4 Compare June 27, 2026 14:52
@cqjjjzr cqjjjzr requested a review from Copilot June 27, 2026 14:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 23 out of 28 changed files in this pull request and generated 5 comments.

Comment on lines 7 to 10
import services
from controllers.common.controller_schemas import TextToAudioPayload as TextToAudioPayloadBase
from controllers.common.fields import AudioBinaryResponse, AudioTranscriptResponse
from controllers.web import web_ns
from controllers.web.error import (
Comment on lines 52 to +53
register_schema_models(web_ns, TextToAudioPayload)
register_response_schema_models(web_ns, AudioBinaryResponse, AudioTranscriptResponse)
register_response_schema_models(web_ns, AudioToTextResponse)
Comment on lines +122 to 124
# response-contract:ignore provider audio bytes; TODO: model binary audio response if shape is standardized.
@web_ns.response(200, "Success")
def post(self, app_model: App, end_user: EndUser):
Comment on lines +71 to +72
class HumanInputFormSubmitResponse(ResponseModel):
pass
Comment on lines +159 to +162
assert response.site.input_placeholder == "Ask the app"
assert response.site.custom_disclaimer == ""

def test_nullable_site_fields_preserve_none(self) -> None:
@cqjjjzr cqjjjzr force-pushed the refac-basemodel-webchat branch from 2df04db to 9bb23b7 Compare June 27, 2026 15:30
cqjjjzr added 2 commits June 28, 2026 00:47
Squashes the response contract generation fixes from PR langgenius#38042 into the branch base.
Squashes the branch payload, including generated markdown and TypeScript contract updates.
@cqjjjzr cqjjjzr force-pushed the refac-basemodel-webchat branch from 9bb23b7 to 871a6f1 Compare June 27, 2026 18:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor size:XXL This PR changes 1000+ lines, ignoring generated files. web This relates to changes on the web.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants