@qingflow-tech/qingflow-app-builder-mcp 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/README.md +32 -0
  2. package/docs/local-agent-install.md +332 -0
  3. package/entry_point.py +13 -0
  4. package/npm/bin/qingflow-app-builder-mcp.mjs +7 -0
  5. package/npm/lib/runtime.mjs +339 -0
  6. package/npm/scripts/postinstall.mjs +16 -0
  7. package/package.json +34 -0
  8. package/pyproject.toml +67 -0
  9. package/qingflow-app-builder-mcp +15 -0
  10. package/skills/qingflow-app-builder/SKILL.md +251 -0
  11. package/skills/qingflow-app-builder/agents/openai.yaml +4 -0
  12. package/skills/qingflow-app-builder/references/create-app.md +128 -0
  13. package/skills/qingflow-app-builder/references/environments.md +63 -0
  14. package/skills/qingflow-app-builder/references/flow-actors-and-permissions.md +123 -0
  15. package/skills/qingflow-app-builder/references/gotchas.md +64 -0
  16. package/skills/qingflow-app-builder/references/solution-playbooks.md +53 -0
  17. package/skills/qingflow-app-builder/references/tool-selection.md +93 -0
  18. package/skills/qingflow-app-builder/references/update-flow.md +158 -0
  19. package/skills/qingflow-app-builder/references/update-layout.md +68 -0
  20. package/skills/qingflow-app-builder/references/update-schema.md +68 -0
  21. package/skills/qingflow-app-builder/references/update-views.md +162 -0
  22. package/skills/qingflow-app-builder-code-integrations/SKILL.md +137 -0
  23. package/skills/qingflow-app-builder-code-integrations/agents/openai.yaml +4 -0
  24. package/skills/qingflow-app-builder-code-integrations/references/code-block.md +66 -0
  25. package/skills/qingflow-app-builder-code-integrations/references/q-linker.md +77 -0
  26. package/src/qingflow_mcp/__init__.py +5 -0
  27. package/src/qingflow_mcp/__main__.py +5 -0
  28. package/src/qingflow_mcp/backend_client.py +649 -0
  29. package/src/qingflow_mcp/builder_facade/__init__.py +3 -0
  30. package/src/qingflow_mcp/builder_facade/models.py +1836 -0
  31. package/src/qingflow_mcp/builder_facade/service.py +15044 -0
  32. package/src/qingflow_mcp/cli/__init__.py +1 -0
  33. package/src/qingflow_mcp/cli/commands/__init__.py +18 -0
  34. package/src/qingflow_mcp/cli/commands/app.py +40 -0
  35. package/src/qingflow_mcp/cli/commands/auth.py +44 -0
  36. package/src/qingflow_mcp/cli/commands/builder.py +538 -0
  37. package/src/qingflow_mcp/cli/commands/chart.py +18 -0
  38. package/src/qingflow_mcp/cli/commands/common.py +62 -0
  39. package/src/qingflow_mcp/cli/commands/imports.py +96 -0
  40. package/src/qingflow_mcp/cli/commands/portal.py +25 -0
  41. package/src/qingflow_mcp/cli/commands/record.py +331 -0
  42. package/src/qingflow_mcp/cli/commands/repo.py +80 -0
  43. package/src/qingflow_mcp/cli/commands/task.py +89 -0
  44. package/src/qingflow_mcp/cli/commands/view.py +18 -0
  45. package/src/qingflow_mcp/cli/commands/workspace.py +25 -0
  46. package/src/qingflow_mcp/cli/context.py +60 -0
  47. package/src/qingflow_mcp/cli/formatters.py +334 -0
  48. package/src/qingflow_mcp/cli/json_io.py +50 -0
  49. package/src/qingflow_mcp/cli/main.py +178 -0
  50. package/src/qingflow_mcp/config.py +513 -0
  51. package/src/qingflow_mcp/errors.py +66 -0
  52. package/src/qingflow_mcp/import_store.py +121 -0
  53. package/src/qingflow_mcp/json_types.py +18 -0
  54. package/src/qingflow_mcp/list_type_labels.py +76 -0
  55. package/src/qingflow_mcp/public_surface.py +233 -0
  56. package/src/qingflow_mcp/repository_store.py +71 -0
  57. package/src/qingflow_mcp/response_trim.py +470 -0
  58. package/src/qingflow_mcp/server.py +212 -0
  59. package/src/qingflow_mcp/server_app_builder.py +533 -0
  60. package/src/qingflow_mcp/server_app_user.py +362 -0
  61. package/src/qingflow_mcp/session_store.py +302 -0
  62. package/src/qingflow_mcp/solution/__init__.py +6 -0
  63. package/src/qingflow_mcp/solution/build_assembly_store.py +181 -0
  64. package/src/qingflow_mcp/solution/compiler/__init__.py +282 -0
  65. package/src/qingflow_mcp/solution/compiler/chart_compiler.py +96 -0
  66. package/src/qingflow_mcp/solution/compiler/form_compiler.py +495 -0
  67. package/src/qingflow_mcp/solution/compiler/icon_utils.py +187 -0
  68. package/src/qingflow_mcp/solution/compiler/navigation_compiler.py +57 -0
  69. package/src/qingflow_mcp/solution/compiler/package_compiler.py +19 -0
  70. package/src/qingflow_mcp/solution/compiler/portal_compiler.py +60 -0
  71. package/src/qingflow_mcp/solution/compiler/view_compiler.py +51 -0
  72. package/src/qingflow_mcp/solution/compiler/workflow_compiler.py +173 -0
  73. package/src/qingflow_mcp/solution/design_session.py +222 -0
  74. package/src/qingflow_mcp/solution/design_store.py +100 -0
  75. package/src/qingflow_mcp/solution/executor.py +2398 -0
  76. package/src/qingflow_mcp/solution/normalizer.py +23 -0
  77. package/src/qingflow_mcp/solution/requirements_builder.py +536 -0
  78. package/src/qingflow_mcp/solution/run_store.py +244 -0
  79. package/src/qingflow_mcp/solution/spec_models.py +855 -0
  80. package/src/qingflow_mcp/tools/__init__.py +1 -0
  81. package/src/qingflow_mcp/tools/ai_builder_tools.py +3419 -0
  82. package/src/qingflow_mcp/tools/app_tools.py +925 -0
  83. package/src/qingflow_mcp/tools/approval_tools.py +1062 -0
  84. package/src/qingflow_mcp/tools/auth_tools.py +875 -0
  85. package/src/qingflow_mcp/tools/base.py +388 -0
  86. package/src/qingflow_mcp/tools/code_block_tools.py +777 -0
  87. package/src/qingflow_mcp/tools/custom_button_tools.py +202 -0
  88. package/src/qingflow_mcp/tools/directory_tools.py +675 -0
  89. package/src/qingflow_mcp/tools/feedback_tools.py +238 -0
  90. package/src/qingflow_mcp/tools/file_tools.py +409 -0
  91. package/src/qingflow_mcp/tools/import_tools.py +2189 -0
  92. package/src/qingflow_mcp/tools/navigation_tools.py +210 -0
  93. package/src/qingflow_mcp/tools/package_tools.py +326 -0
  94. package/src/qingflow_mcp/tools/portal_tools.py +158 -0
  95. package/src/qingflow_mcp/tools/qingbi_report_tools.py +374 -0
  96. package/src/qingflow_mcp/tools/record_tools.py +14037 -0
  97. package/src/qingflow_mcp/tools/repository_dev_tools.py +552 -0
  98. package/src/qingflow_mcp/tools/resource_read_tools.py +421 -0
  99. package/src/qingflow_mcp/tools/role_tools.py +112 -0
  100. package/src/qingflow_mcp/tools/solution_tools.py +4054 -0
  101. package/src/qingflow_mcp/tools/task_context_tools.py +2228 -0
  102. package/src/qingflow_mcp/tools/task_tools.py +890 -0
  103. package/src/qingflow_mcp/tools/view_tools.py +335 -0
  104. package/src/qingflow_mcp/tools/workflow_tools.py +376 -0
  105. package/src/qingflow_mcp/tools/workspace_tools.py +125 -0
@@ -0,0 +1,2189 @@
1
+ from __future__ import annotations
2
+
3
+ import hashlib
4
+ import json
5
+ import mimetypes
6
+ import re
7
+ import shutil
8
+ import tempfile
9
+ from io import BytesIO
10
+ from copy import deepcopy
11
+ from datetime import datetime, timedelta, timezone
12
+ from pathlib import Path
13
+ from typing import Any
14
+ from uuid import uuid4
15
+
16
+ from mcp.server.fastmcp import FastMCP
17
+ from openpyxl import Workbook, load_workbook
18
+
19
+ from ..config import DEFAULT_PROFILE
20
+ from ..errors import QingflowApiError
21
+ from ..import_store import ImportJobStore, ImportVerificationStore
22
+ from ..json_types import JSONObject
23
+ from .app_tools import _derive_import_capability
24
+ from .base import ToolBase, tool_cn_name
25
+ from .file_tools import FileTools
26
+ from .record_tools import RecordTools, _build_field_index, _normalize_form_schema
27
+
28
+
29
+ SUPPORTED_IMPORT_EXTENSIONS = {".xlsx", ".xls"}
30
+ REPAIRABLE_IMPORT_EXTENSIONS = {".xlsx"}
31
+ SAFE_REPAIRS = {
32
+ "normalize_headers",
33
+ "trim_trailing_blank_rows",
34
+ "normalize_enum_values",
35
+ "normalize_date_formats",
36
+ "normalize_number_formats",
37
+ "normalize_url_cells",
38
+ }
39
+ EMAIL_PATTERN = re.compile(r"^[^@\s]+@[^@\s]+\.[^@\s]+$")
40
+
41
+
42
+ class ImportTools(ToolBase):
43
+ """导入工具(中文名:数据导入与校验)。
44
+
45
+ 类型:批量数据导入工具。
46
+ 主要职责:
47
+ 1. 获取导入模板与导入 schema;
48
+ 2. 执行导入文件校验与本地修复;
49
+ 3. 启动导入任务并查询导入进度与结果。
50
+ """
51
+
52
+ def __init__(
53
+ self,
54
+ sessions,
55
+ backend,
56
+ *,
57
+ verification_store: ImportVerificationStore | None = None,
58
+ job_store: ImportJobStore | None = None,
59
+ ) -> None:
60
+ """执行内部辅助逻辑。"""
61
+ super().__init__(sessions, backend)
62
+ self._record_tools = RecordTools(sessions, backend)
63
+ self._file_tools = FileTools(sessions, backend)
64
+ self._verification_store = verification_store or ImportVerificationStore()
65
+ self._job_store = job_store or ImportJobStore()
66
+
67
+ def register(self, mcp: FastMCP) -> None:
68
+ """注册当前工具到 MCP 服务。"""
69
+ @mcp.tool()
70
+ def record_import_schema_get(
71
+ app_key: str = "",
72
+ output_profile: str = "normal",
73
+ ) -> dict[str, Any]:
74
+ return self.record_import_schema_get(
75
+ profile=DEFAULT_PROFILE,
76
+ app_key=app_key,
77
+ output_profile=output_profile,
78
+ )
79
+
80
+ @mcp.tool(description="Get the official app import template and the expected applicant import columns.")
81
+ def record_import_template_get(
82
+ profile: str = DEFAULT_PROFILE,
83
+ app_key: str = "",
84
+ download_to_path: str | None = None,
85
+ ) -> dict[str, Any]:
86
+ return self.record_import_template_get(
87
+ profile=profile,
88
+ app_key=app_key,
89
+ download_to_path=download_to_path,
90
+ )
91
+
92
+ @mcp.tool(description="Verify a local Excel import file and produce the only verification_id allowed for import start.")
93
+ def record_import_verify(
94
+ profile: str = DEFAULT_PROFILE,
95
+ app_key: str = "",
96
+ file_path: str = "",
97
+ ) -> dict[str, Any]:
98
+ return self.record_import_verify(
99
+ profile=profile,
100
+ app_key=app_key,
101
+ file_path=file_path,
102
+ )
103
+
104
+ @mcp.tool(description="Repair a local .xlsx import file after explicit user authorization, then re-verify it.")
105
+ def record_import_repair_local(
106
+ profile: str = DEFAULT_PROFILE,
107
+ verification_id: str = "",
108
+ authorized_file_modification: bool = False,
109
+ output_path: str | None = None,
110
+ selected_repairs: list[str] | None = None,
111
+ ) -> dict[str, Any]:
112
+ return self.record_import_repair_local(
113
+ profile=profile,
114
+ verification_id=verification_id,
115
+ authorized_file_modification=authorized_file_modification,
116
+ output_path=output_path,
117
+ selected_repairs=selected_repairs,
118
+ )
119
+
120
+ @mcp.tool(description="Start import from a successful verification_id. being_enter_auditing must be passed explicitly.")
121
+ def record_import_start(
122
+ profile: str = DEFAULT_PROFILE,
123
+ app_key: str = "",
124
+ verification_id: str = "",
125
+ being_enter_auditing: bool | None = None,
126
+ view_key: str | None = None,
127
+ ) -> dict[str, Any]:
128
+ return self.record_import_start(
129
+ profile=profile,
130
+ app_key=app_key,
131
+ verification_id=verification_id,
132
+ being_enter_auditing=being_enter_auditing,
133
+ view_key=view_key,
134
+ )
135
+
136
+ @mcp.tool(description="Get import status by process_id_str, import_id, or the latest remembered import in the current app.")
137
+ def record_import_status_get(
138
+ profile: str = DEFAULT_PROFILE,
139
+ app_key: str = "",
140
+ import_id: str | None = None,
141
+ process_id_str: str | None = None,
142
+ ) -> dict[str, Any]:
143
+ selector_count = sum(
144
+ 1
145
+ for item in (
146
+ bool(_normalize_optional_text(process_id_str)),
147
+ bool(_normalize_optional_text(import_id)),
148
+ bool(str(app_key or "").strip()),
149
+ )
150
+ if item
151
+ )
152
+ if selector_count != 1:
153
+ return self._failed_status_result(
154
+ error_code="CONFIG_ERROR",
155
+ message="record_import_status_get accepts exactly one selector: process_id_str, import_id, or app_key",
156
+ extra={
157
+ "details": {
158
+ "fix_hint": "Use `process_id_str` or `import_id` for a known import, or use only `app_key` to inspect the latest import in that app.",
159
+ }
160
+ },
161
+ )
162
+ return self.record_import_status_get(
163
+ profile=profile,
164
+ app_key=app_key,
165
+ import_id=import_id,
166
+ process_id_str=process_id_str,
167
+ )
168
+
169
+ @tool_cn_name("导入 Schema")
170
+ def record_import_schema_get(
171
+ self,
172
+ *,
173
+ profile: str = DEFAULT_PROFILE,
174
+ app_key: str,
175
+ output_profile: str = "normal",
176
+ ) -> dict[str, Any]:
177
+ """执行记录相关逻辑。"""
178
+ if not app_key.strip():
179
+ return {
180
+ "ok": False,
181
+ "status": "blocked",
182
+ "app_key": app_key,
183
+ "error_code": "IMPORT_SCHEMA_UNAVAILABLE",
184
+ "message": "app_key is required",
185
+ }
186
+
187
+ def runner(session_profile, context):
188
+ import_capability, import_warnings = self._fetch_import_capability(context, app_key)
189
+ _index, expected_columns, schema_fingerprint = self._resolve_import_schema_bundle(
190
+ profile,
191
+ context,
192
+ app_key,
193
+ import_capability=import_capability,
194
+ )
195
+ columns: list[JSONObject] = []
196
+ for column in expected_columns:
197
+ payload: JSONObject = {
198
+ "title": column["title"],
199
+ "kind": column["write_kind"],
200
+ "required": bool(column.get("required")),
201
+ }
202
+ if isinstance(column.get("options"), list) and column.get("options"):
203
+ payload["options"] = column["options"]
204
+ if bool(column.get("requires_lookup")):
205
+ payload["accepts_natural_input"] = True
206
+ if bool(column.get("requires_upload")):
207
+ payload["requires_upload"] = True
208
+ if isinstance(column.get("target_app_key"), str):
209
+ payload["target_app_key"] = column["target_app_key"]
210
+ if isinstance(column.get("target_app_name"), str):
211
+ payload["target_app_name"] = column["target_app_name"]
212
+ if isinstance(column.get("searchable_fields"), list) and column.get("searchable_fields"):
213
+ payload["searchable_fields"] = column["searchable_fields"]
214
+ columns.append(payload)
215
+ response: dict[str, Any] = {
216
+ "ok": True,
217
+ "status": "success",
218
+ "app_key": app_key,
219
+ "ws_id": session_profile.selected_ws_id,
220
+ "request_route": self.backend.describe_route(context),
221
+ "warnings": import_warnings,
222
+ "schema_scope": "import_ready",
223
+ "columns": columns,
224
+ "schema_fingerprint": schema_fingerprint,
225
+ }
226
+ if output_profile == "verbose":
227
+ response["expected_columns"] = expected_columns
228
+ response["import_capability"] = import_capability
229
+ return response
230
+
231
+ return self._run(profile, runner)
232
+
233
+ @tool_cn_name("导入模板")
234
+ def record_import_template_get(
235
+ self,
236
+ *,
237
+ profile: str,
238
+ app_key: str,
239
+ download_to_path: str | None = None,
240
+ ) -> dict[str, Any]:
241
+ """执行记录相关逻辑。"""
242
+ if not app_key.strip():
243
+ return self._failed_template_result(app_key=app_key, error_code="IMPORT_TEMPLATE_UNAUTHORIZED", message="app_key is required")
244
+
245
+ def runner(session_profile, context):
246
+ import_capability, import_warnings = self._fetch_import_capability(context, app_key)
247
+ field_index, expected_columns, schema_fingerprint = self._resolve_import_schema_bundle(
248
+ profile,
249
+ context,
250
+ app_key,
251
+ import_capability=import_capability,
252
+ )
253
+ try:
254
+ payload = self.backend.request("GET", context, f"/app/{app_key}/apply/excelTemplate")
255
+ except QingflowApiError as exc:
256
+ if import_capability.get("auth_source") == "apply_auth":
257
+ downloaded_to_path = self._write_local_template(
258
+ expected_columns=expected_columns,
259
+ destination_hint=download_to_path,
260
+ app_key=app_key,
261
+ )
262
+ return {
263
+ "ok": True,
264
+ "status": "partial_success",
265
+ "app_key": app_key,
266
+ "ws_id": session_profile.selected_ws_id,
267
+ "request_route": self.backend.describe_route(context),
268
+ "template_url": None,
269
+ "downloaded_to_path": downloaded_to_path,
270
+ "expected_columns": expected_columns,
271
+ "schema_fingerprint": schema_fingerprint,
272
+ "warnings": import_warnings
273
+ + [
274
+ {
275
+ "code": "IMPORT_TEMPLATE_LOCAL_FALLBACK",
276
+ "message": "Official template download requires data management permission; MCP generated a local applicant-import template instead.",
277
+ }
278
+ ],
279
+ "verification": {
280
+ "schema_fingerprint": schema_fingerprint,
281
+ "template_url_resolved": False,
282
+ "template_downloaded": True,
283
+ "template_source": "local_generated",
284
+ },
285
+ }
286
+ return self._failed_template_result(
287
+ app_key=app_key,
288
+ error_code="IMPORT_TEMPLATE_UNAUTHORIZED",
289
+ message=exc.message,
290
+ request_route=self.backend.describe_route(context),
291
+ )
292
+ template_url = _pick_template_url(payload)
293
+ if not template_url:
294
+ return self._failed_template_result(
295
+ app_key=app_key,
296
+ error_code="IMPORT_TEMPLATE_UNAUTHORIZED",
297
+ message="template endpoint did not return excelUrl",
298
+ request_route=self.backend.describe_route(context),
299
+ )
300
+ downloaded_to_path = None
301
+ warnings: list[JSONObject] = list(import_warnings)
302
+ verification = {
303
+ "schema_fingerprint": schema_fingerprint,
304
+ "template_url_resolved": True,
305
+ "template_downloaded": False,
306
+ "template_source": "official",
307
+ }
308
+ if download_to_path:
309
+ destination = _resolve_template_download_path(download_to_path, app_key=app_key)
310
+ destination.parent.mkdir(parents=True, exist_ok=True)
311
+ content = self.backend.download_binary(template_url)
312
+ destination.write_bytes(content)
313
+ downloaded_to_path = str(destination)
314
+ verification["template_downloaded"] = True
315
+ return {
316
+ "ok": True,
317
+ "status": "success",
318
+ "app_key": app_key,
319
+ "ws_id": session_profile.selected_ws_id,
320
+ "request_route": self.backend.describe_route(context),
321
+ "template_url": template_url,
322
+ "downloaded_to_path": downloaded_to_path,
323
+ "expected_columns": expected_columns,
324
+ "schema_fingerprint": schema_fingerprint,
325
+ "warnings": warnings,
326
+ "verification": verification,
327
+ }
328
+
329
+ try:
330
+ return self._run(profile, runner)
331
+ except RuntimeError as exc:
332
+ return self._runtime_error_as_result(exc, error_code="IMPORT_TEMPLATE_UNAUTHORIZED")
333
+
334
+ @tool_cn_name("导入校验")
335
+ def record_import_verify(
336
+ self,
337
+ *,
338
+ profile: str,
339
+ app_key: str,
340
+ file_path: str,
341
+ ) -> dict[str, Any]:
342
+ """执行记录相关逻辑。"""
343
+ if not app_key.strip():
344
+ return self._failed_verify_result(app_key=app_key, file_path=file_path, error_code="IMPORT_VERIFICATION_FAILED", message="app_key is required")
345
+ path = Path(file_path).expanduser()
346
+ if not path.is_file():
347
+ return self._failed_verify_result(app_key=app_key, file_path=file_path, error_code="IMPORT_VERIFICATION_FAILED", message="file_path must point to an existing file")
348
+
349
+ def runner(session_profile, context):
350
+ import_capability, import_warnings = self._fetch_import_capability(context, app_key)
351
+ precheck_known = import_capability.get("auth_source") != "unknown"
352
+ if not bool(import_capability.get("can_import")):
353
+ if import_capability.get("auth_source") != "unknown":
354
+ return self._failed_verify_result(
355
+ app_key=app_key,
356
+ file_path=file_path,
357
+ error_code="IMPORT_AUTH_PRECHECK_FAILED",
358
+ message="the current user does not have import permission for this app",
359
+ extra={
360
+ "warnings": import_warnings,
361
+ "verification": {
362
+ "import_auth_prechecked": True,
363
+ "import_auth_precheck_passed": False,
364
+ "backend_verification_passed": False,
365
+ },
366
+ "import_capability": import_capability,
367
+ },
368
+ )
369
+ import_warnings = list(import_warnings) + [
370
+ {
371
+ "code": "IMPORT_AUTH_PRECHECK_SKIPPED",
372
+ "message": "record_import_verify could not determine import permission from app metadata; continuing with file verification only.",
373
+ }
374
+ ]
375
+ field_index, expected_columns, schema_fingerprint = self._resolve_import_schema_bundle(
376
+ profile,
377
+ context,
378
+ app_key,
379
+ import_capability=import_capability,
380
+ )
381
+ template_header_profile, header_warnings = self._load_template_header_profile(
382
+ context,
383
+ app_key,
384
+ import_capability=import_capability,
385
+ expected_columns=expected_columns,
386
+ )
387
+ template_header_titles = template_header_profile.get("allowed_titles")
388
+ local_check = self._local_verify(
389
+ profile=profile,
390
+ context=context,
391
+ path=path,
392
+ app_key=app_key,
393
+ field_index=field_index,
394
+ expected_columns=expected_columns,
395
+ allowed_header_titles=template_header_titles,
396
+ schema_fingerprint=schema_fingerprint,
397
+ )
398
+ effective_path = path
399
+ effective_local_check = local_check
400
+ auto_normalization = None
401
+ try:
402
+ auto_normalization = self._maybe_auto_normalize_file(
403
+ source_path=path,
404
+ expected_columns=expected_columns,
405
+ template_header_profile=template_header_profile,
406
+ local_check=local_check,
407
+ )
408
+ except Exception as exc:
409
+ effective_local_check = deepcopy(local_check)
410
+ effective_local_check["issues"].append(
411
+ _issue(
412
+ "IMPORT_AUTO_NORMALIZATION_FAILED",
413
+ f"Workbook compatibility normalization failed before backend verification: {exc}",
414
+ severity="error",
415
+ )
416
+ )
417
+ effective_local_check["warnings"].append(
418
+ {
419
+ "code": "IMPORT_AUTO_NORMALIZATION_FAILED",
420
+ "message": "Workbook compatibility normalization failed during local precheck; returning a structured verification failure instead of crashing.",
421
+ }
422
+ )
423
+ effective_local_check["local_precheck_passed"] = False
424
+ effective_local_check["can_import"] = False
425
+ effective_local_check["error_code"] = "IMPORT_VERIFICATION_FAILED"
426
+ if auto_normalization is not None:
427
+ effective_path = Path(str(auto_normalization["verified_file_path"]))
428
+ effective_local_check = self._local_verify(
429
+ profile=profile,
430
+ context=context,
431
+ path=effective_path,
432
+ app_key=app_key,
433
+ field_index=field_index,
434
+ expected_columns=expected_columns,
435
+ allowed_header_titles=list(auto_normalization["header_titles"]),
436
+ schema_fingerprint=schema_fingerprint,
437
+ )
438
+ warnings = import_warnings + deepcopy(effective_local_check["warnings"]) + header_warnings
439
+ if auto_normalization is not None:
440
+ warnings.extend(deepcopy(auto_normalization["warnings"]))
441
+ issues = deepcopy(effective_local_check["issues"])
442
+ can_import = bool(effective_local_check["can_import"])
443
+ backend_verification = None
444
+ if can_import:
445
+ try:
446
+ payload = self.backend.request_multipart(
447
+ "POST",
448
+ context,
449
+ f"/app/{app_key}/upload/verification",
450
+ files={
451
+ "file": (
452
+ effective_path.name,
453
+ effective_path.read_bytes(),
454
+ mimetypes.guess_type(effective_path.name)[0] or "application/octet-stream",
455
+ )
456
+ },
457
+ )
458
+ if isinstance(payload, dict):
459
+ backend_verification = payload
460
+ else:
461
+ backend_verification = {}
462
+ being_validated = backend_verification.get("beingValidated", True)
463
+ if being_validated is False:
464
+ can_import = False
465
+ issues.append(
466
+ _issue(
467
+ "BACKEND_IMPORT_VERIFICATION_REJECTED",
468
+ "Backend verification rejected the file for import.",
469
+ severity="error",
470
+ )
471
+ )
472
+ except QingflowApiError as exc:
473
+ can_import = False
474
+ issues.append(
475
+ _issue(
476
+ "BACKEND_IMPORT_VERIFICATION_FAILED",
477
+ exc.message or "Backend import verification failed.",
478
+ severity="error",
479
+ )
480
+ )
481
+ warnings.append(
482
+ {
483
+ "code": "IMPORT_VERIFICATION_FAILED",
484
+ "message": "Backend verification failed; the file cannot be imported until verification succeeds.",
485
+ }
486
+ )
487
+ verification_id = str(uuid4())
488
+ verification_payload = {
489
+ "id": verification_id,
490
+ "created_at": _utc_now().isoformat(),
491
+ "profile": profile,
492
+ "app_key": app_key,
493
+ "file_path": str(path.resolve()),
494
+ "source_file_path": str(path.resolve()),
495
+ "verified_file_path": str(effective_path.resolve()) if effective_path != path else None,
496
+ "file_name": path.name,
497
+ "file_sha256": local_check["file_sha256"],
498
+ "verified_file_sha256": effective_local_check["file_sha256"] if effective_path != path else None,
499
+ "file_size": local_check["file_size"],
500
+ "schema_fingerprint": schema_fingerprint,
501
+ "can_import": can_import,
502
+ "issues": issues,
503
+ "warnings": warnings,
504
+ "import_capability": import_capability,
505
+ "apply_rows": backend_verification.get("applyRows") if isinstance(backend_verification, dict) else None,
506
+ "backend_verification": backend_verification,
507
+ "local_precheck": effective_local_check,
508
+ "source_local_precheck": local_check,
509
+ "auto_normalization": auto_normalization,
510
+ }
511
+ self._verification_store.put(verification_id, verification_payload)
512
+ return {
513
+ "ok": True,
514
+ "status": "success" if can_import else "failed",
515
+ "error_code": None if can_import else (effective_local_check.get("error_code") or local_check.get("error_code") or "IMPORT_VERIFICATION_FAILED"),
516
+ "can_import": can_import,
517
+ "verification_id": verification_id,
518
+ "file_path": str(path.resolve()),
519
+ "verified_file_path": str(effective_path.resolve()) if effective_path != path else None,
520
+ "file_name": path.name,
521
+ "file_sha256": local_check["file_sha256"],
522
+ "verified_file_sha256": effective_local_check["file_sha256"] if effective_path != path else None,
523
+ "file_size": local_check["file_size"],
524
+ "schema_fingerprint": schema_fingerprint,
525
+ "apply_rows": backend_verification.get("applyRows") if isinstance(backend_verification, dict) else None,
526
+ "issues": issues,
527
+ "repair_suggestions": local_check["repair_suggestions"],
528
+ "warnings": warnings,
529
+ "import_capability": import_capability,
530
+ "verification": {
531
+ "import_auth_prechecked": precheck_known,
532
+ "import_auth_precheck_passed": True if precheck_known else None,
533
+ "import_auth_source": import_capability.get("auth_source"),
534
+ "local_precheck_passed": bool(effective_local_check["local_precheck_passed"]),
535
+ "backend_verification_passed": isinstance(backend_verification, dict)
536
+ and backend_verification.get("beingValidated", True) is not False,
537
+ "schema_fingerprint": schema_fingerprint,
538
+ "file_sha256": local_check["file_sha256"],
539
+ "verified_file_sha256": effective_local_check["file_sha256"] if effective_path != path else None,
540
+ "file_format": local_check["extension"],
541
+ "local_precheck_limited": bool(effective_local_check["local_precheck_limited"]),
542
+ "auto_normalized": effective_path != path,
543
+ },
544
+ }
545
+
546
+ try:
547
+ return self._run(profile, runner)
548
+ except RuntimeError as exc:
549
+ return self._runtime_error_as_result(exc, error_code="IMPORT_VERIFICATION_FAILED", extra={"can_import": False})
550
+
551
+ @tool_cn_name("导入修复")
552
+ def record_import_repair_local(
553
+ self,
554
+ *,
555
+ profile: str,
556
+ verification_id: str,
557
+ authorized_file_modification: bool,
558
+ output_path: str | None = None,
559
+ selected_repairs: list[str] | None = None,
560
+ ) -> dict[str, Any]:
561
+ """执行记录相关逻辑。"""
562
+ if not verification_id.strip():
563
+ return self._failed_repair_result(error_code="IMPORT_VERIFICATION_FAILED", message="verification_id is required")
564
+ if not authorized_file_modification:
565
+ return self._failed_repair_result(
566
+ error_code="IMPORT_REPAIR_NOT_AUTHORIZED",
567
+ message="record_import_repair_local requires authorized_file_modification=true",
568
+ )
569
+ unknown_repairs = sorted({item for item in (selected_repairs or []) if item not in SAFE_REPAIRS})
570
+ if unknown_repairs:
571
+ return self._failed_repair_result(
572
+ error_code="IMPORT_REPAIR_FORMAT_UNSUPPORTED",
573
+ message=f"unknown selected_repairs: {', '.join(unknown_repairs)}",
574
+ )
575
+
576
+ def runner(_session_profile, context):
577
+ stored = self._verification_store.get(verification_id)
578
+ if stored is None:
579
+ return self._failed_repair_result(error_code="IMPORT_VERIFICATION_STALE", message="verification_id is missing or expired")
580
+ source_path = Path(str(stored.get("source_file_path") or stored["file_path"]))
581
+ extension = source_path.suffix.lower()
582
+ if extension not in REPAIRABLE_IMPORT_EXTENSIONS:
583
+ return self._failed_repair_result(
584
+ error_code="IMPORT_REPAIR_FORMAT_UNSUPPORTED",
585
+ message="record_import_repair_local v1 only supports .xlsx files",
586
+ extra={"source_file_path": str(source_path)},
587
+ )
588
+ expected_columns, _ = self._expected_import_columns(profile, context, str(stored["app_key"]))
589
+ normalized_repairs = set(selected_repairs or SAFE_REPAIRS)
590
+ destination = _resolve_repaired_output_path(source_path, output_path=output_path)
591
+ destination.parent.mkdir(parents=True, exist_ok=True)
592
+ shutil.copy2(source_path, destination)
593
+
594
+ workbook = load_workbook(destination)
595
+ sheet = workbook[workbook.sheetnames[0]]
596
+ applied_repairs: list[str] = []
597
+ skipped_repairs: list[str] = []
598
+ if "normalize_headers" in normalized_repairs:
599
+ if _repair_headers(sheet, expected_columns):
600
+ applied_repairs.append("normalize_headers")
601
+ else:
602
+ skipped_repairs.append("normalize_headers")
603
+ if "trim_trailing_blank_rows" in normalized_repairs:
604
+ if _trim_trailing_blank_rows(sheet):
605
+ applied_repairs.append("trim_trailing_blank_rows")
606
+ else:
607
+ skipped_repairs.append("trim_trailing_blank_rows")
608
+ if "normalize_enum_values" in normalized_repairs:
609
+ if _normalize_enum_values(sheet, expected_columns):
610
+ applied_repairs.append("normalize_enum_values")
611
+ else:
612
+ skipped_repairs.append("normalize_enum_values")
613
+ if "normalize_date_formats" in normalized_repairs:
614
+ if _normalize_date_formats(sheet):
615
+ applied_repairs.append("normalize_date_formats")
616
+ else:
617
+ skipped_repairs.append("normalize_date_formats")
618
+ if "normalize_number_formats" in normalized_repairs:
619
+ if _normalize_number_formats(sheet):
620
+ applied_repairs.append("normalize_number_formats")
621
+ else:
622
+ skipped_repairs.append("normalize_number_formats")
623
+ if "normalize_url_cells" in normalized_repairs:
624
+ if _normalize_url_cells(sheet):
625
+ applied_repairs.append("normalize_url_cells")
626
+ else:
627
+ skipped_repairs.append("normalize_url_cells")
628
+ workbook.save(destination)
629
+
630
+ verification_result = self.record_import_verify(
631
+ profile=profile,
632
+ app_key=str(stored["app_key"]),
633
+ file_path=str(destination),
634
+ )
635
+ new_verification_id = verification_result.get("verification_id")
636
+ return {
637
+ "ok": bool(verification_result.get("ok")),
638
+ "status": verification_result.get("status"),
639
+ "error_code": verification_result.get("error_code"),
640
+ "source_file_path": str(source_path),
641
+ "repaired_file_path": str(destination),
642
+ "applied_repairs": applied_repairs,
643
+ "skipped_repairs": skipped_repairs,
644
+ "new_verification_id": new_verification_id,
645
+ "can_import_after_repair": bool(verification_result.get("can_import")),
646
+ "post_repair_issues": verification_result.get("issues", []),
647
+ "warnings": verification_result.get("warnings", []),
648
+ "verification": {
649
+ "source_preserved": True,
650
+ "repair_authorized": True,
651
+ "reverified": True,
652
+ "selected_repairs": sorted(normalized_repairs),
653
+ },
654
+ }
655
+
656
+ try:
657
+ return self._run(profile, runner)
658
+ except RuntimeError as exc:
659
+ return self._runtime_error_as_result(exc, error_code="IMPORT_REPAIR_FORMAT_UNSUPPORTED")
660
+
661
+ @tool_cn_name("开始导入")
662
+ def record_import_start(
663
+ self,
664
+ *,
665
+ profile: str,
666
+ app_key: str,
667
+ verification_id: str,
668
+ being_enter_auditing: bool | None,
669
+ view_key: str | None = None,
670
+ ) -> dict[str, Any]:
671
+ """执行记录相关逻辑。"""
672
+ if being_enter_auditing is None:
673
+ return self._failed_start_result(error_code="IMPORT_VERIFICATION_FAILED", message="being_enter_auditing must be passed explicitly")
674
+
675
+ def runner(session_profile, context):
676
+ stored = self._verification_store.get(verification_id)
677
+ if stored is None:
678
+ return self._failed_start_result(error_code="IMPORT_VERIFICATION_STALE", message="verification_id is missing or expired")
679
+ if str(stored.get("app_key")) != app_key:
680
+ return self._failed_start_result(error_code="IMPORT_VERIFICATION_STALE", message="verification_id does not belong to the requested app")
681
+ if not bool(stored.get("can_import")):
682
+ return self._failed_start_result(error_code="IMPORT_VERIFICATION_FAILED", message="verification_id is not importable", extra={"accepted": False})
683
+ current_path = Path(str(stored.get("verified_file_path") or stored["file_path"]))
684
+ if not current_path.is_file():
685
+ return self._failed_start_result(error_code="IMPORT_VERIFICATION_STALE", message="verified file no longer exists")
686
+ current_sha256 = _sha256_file(current_path)
687
+ expected_sha256 = stored.get("verified_file_sha256") or stored.get("file_sha256")
688
+ if current_sha256 != expected_sha256:
689
+ return self._failed_start_result(
690
+ error_code="IMPORT_FILE_CHANGED_AFTER_VERIFY",
691
+ message="the file changed after verification; run record_import_verify again",
692
+ extra={"accepted": False},
693
+ )
694
+ stored_import_capability = stored.get("import_capability")
695
+ _, current_schema_fingerprint = self._expected_import_columns(
696
+ profile,
697
+ context,
698
+ app_key,
699
+ import_capability=stored_import_capability if isinstance(stored_import_capability, dict) else None,
700
+ )
701
+ if current_schema_fingerprint != stored.get("schema_fingerprint"):
702
+ return self._failed_start_result(
703
+ error_code="IMPORT_SCHEMA_CHANGED_AFTER_VERIFY",
704
+ message="the applicant schema changed after verification; run record_import_verify again",
705
+ extra={"accepted": False},
706
+ )
707
+ upload_result = self._file_tools.file_upload_local(
708
+ profile=profile,
709
+ upload_kind="login",
710
+ file_path=str(current_path),
711
+ )
712
+ file_url = upload_result.get("download_url")
713
+ if not isinstance(file_url, str) or not file_url.strip():
714
+ return self._failed_start_result(error_code="IMPORT_VERIFICATION_FAILED", message="file upload did not return download_url")
715
+ try:
716
+ socket_result = self.backend.start_socket_data_import(
717
+ context,
718
+ app_key=app_key,
719
+ being_enter_auditing=bool(being_enter_auditing),
720
+ view_key=view_key,
721
+ excel_url=file_url,
722
+ excel_name=str(stored.get("file_name") or current_path.name),
723
+ )
724
+ except QingflowApiError as exc:
725
+ error_code = "IMPORT_SOCKET_ACK_TIMEOUT" if exc.details and exc.details.get("error_code") == "IMPORT_SOCKET_ACK_TIMEOUT" else "IMPORT_VERIFICATION_FAILED"
726
+ return self._failed_start_result(error_code=error_code, message=exc.message, extra={"accepted": False, "file_url": file_url})
727
+ import_id = str(socket_result.get("import_id") or "")
728
+ process_id_str = _normalize_optional_text(socket_result.get("process_id_str"))
729
+ started_at = _utc_now().isoformat()
730
+ self._job_store.put(
731
+ import_id,
732
+ {
733
+ "created_at": started_at,
734
+ "profile": profile,
735
+ "app_key": app_key,
736
+ "import_id": import_id,
737
+ "process_id_str": process_id_str,
738
+ "source_file_name": str(stored.get("file_name") or current_path.name),
739
+ "started_at": started_at,
740
+ "file_url": file_url,
741
+ "verification_id": verification_id,
742
+ },
743
+ )
744
+ warnings = deepcopy(socket_result.get("warnings", []))
745
+ return {
746
+ "ok": True,
747
+ "status": "accepted",
748
+ "accepted": True,
749
+ "import_id": import_id,
750
+ "process_id_str": process_id_str,
751
+ "source_file_name": str(stored.get("file_name") or current_path.name),
752
+ "file_url": file_url,
753
+ "warnings": warnings,
754
+ "verification": {
755
+ "verification_id_valid": True,
756
+ "file_hash_verified": True,
757
+ "schema_fingerprint_verified": True,
758
+ "upload_staged": True,
759
+ "import_acknowledged": bool(import_id),
760
+ },
761
+ }
762
+
763
+ try:
764
+ return self._run(profile, runner)
765
+ except RuntimeError as exc:
766
+ return self._runtime_error_as_result(exc, error_code="IMPORT_VERIFICATION_FAILED", extra={"accepted": False})
767
+
768
+ @tool_cn_name("导入状态")
769
+ def record_import_status_get(
770
+ self,
771
+ *,
772
+ profile: str,
773
+ app_key: str = "",
774
+ import_id: str | None = None,
775
+ process_id_str: str | None = None,
776
+ ) -> dict[str, Any]:
777
+ """执行记录相关逻辑。"""
778
+ normalized_app_key = (app_key or "").strip()
779
+ normalized_import_id = _normalize_optional_text(import_id)
780
+ normalized_process_id = _normalize_optional_text(process_id_str)
781
+ if normalized_import_id and normalized_process_id:
782
+ return self._failed_status_result(
783
+ error_code="CONFIG_ERROR",
784
+ message="record_import_status_get accepts import_id or process_id_str, but not both at the same time",
785
+ extra={
786
+ "details": {
787
+ "fix_hint": "Use only one of `import_id` or `process_id_str`. You may pass `app_key` as an optional routing hint for direct method compatibility.",
788
+ }
789
+ },
790
+ )
791
+ if not normalized_process_id and not normalized_import_id and not normalized_app_key:
792
+ return self._failed_status_result(
793
+ error_code="CONFIG_ERROR",
794
+ message="record_import_status_get requires at least one selector: process_id_str, import_id, or app_key",
795
+ extra={
796
+ "details": {
797
+ "fix_hint": "Use `process_id_str` or `import_id` for a known import, or use only `app_key` to inspect the latest import in that app.",
798
+ }
799
+ },
800
+ )
801
+
802
+ def runner(_session_profile, context):
803
+ local_job = None
804
+ if normalized_import_id:
805
+ local_job = self._job_store.get(normalized_import_id)
806
+ if local_job is None and normalized_process_id:
807
+ matches = [item for item in self._job_store.list() if _normalize_optional_text(item.get("process_id_str")) == normalized_process_id]
808
+ local_job = matches[0] if len(matches) == 1 else None
809
+ resolved_app_key = normalized_app_key
810
+ if not resolved_app_key and isinstance(local_job, dict):
811
+ resolved_app_key = str(local_job.get("app_key") or "").strip()
812
+ if not resolved_app_key:
813
+ return self._failed_status_result(
814
+ error_code="CONFIG_ERROR",
815
+ message="record_import_status_get could not determine app_key from the provided selector",
816
+ extra={
817
+ "details": {
818
+ "fix_hint": "Use the original `app_key`, or call import status with the latest-import mode: only `app_key`.",
819
+ }
820
+ },
821
+ )
822
+ if local_job is None and not normalized_import_id and not normalized_process_id:
823
+ recent = [item for item in self._job_store.list() if str(item.get("app_key")) == resolved_app_key]
824
+ local_job = recent[0] if recent else None
825
+ page = self.backend.request(
826
+ "GET",
827
+ context,
828
+ "/app/apply/dataImport/record",
829
+ params={"appKey": resolved_app_key, "pageNum": 1, "pageSize": 100},
830
+ )
831
+ records = _extract_import_records(page)
832
+ matched_record, matched_by = _match_import_record(
833
+ records,
834
+ local_job=local_job,
835
+ process_id_str=normalized_process_id,
836
+ )
837
+ if matched_record is None:
838
+ return self._failed_status_result(
839
+ error_code="IMPORT_STATUS_AMBIGUOUS",
840
+ message="could not uniquely resolve an import record from the provided identifiers",
841
+ extra={"matched_by": matched_by},
842
+ )
843
+ normalized_process = _normalize_optional_text(
844
+ matched_record.get("processIdStr") or matched_record.get("processId") or matched_record.get("process_id_str")
845
+ )
846
+ if local_job is not None and normalized_import_id:
847
+ self._job_store.put(
848
+ normalized_import_id,
849
+ {
850
+ **local_job,
851
+ "created_at": local_job.get("created_at") or _utc_now().isoformat(),
852
+ "process_id_str": normalized_process,
853
+ },
854
+ )
855
+ total_rows = _coerce_int(matched_record.get("totalNumber") or matched_record.get("total_rows"))
856
+ success_rows = _coerce_int(matched_record.get("successNum") or matched_record.get("success_rows"))
857
+ failed_rows = _coerce_int(matched_record.get("errorNum") or matched_record.get("failed_rows"))
858
+ progress = _coerce_int(matched_record.get("importPercentage") or matched_record.get("progress"))
859
+ return {
860
+ "ok": True,
861
+ "status": _normalize_optional_text(matched_record.get("processStatus")) or "unknown",
862
+ "app_key": resolved_app_key,
863
+ "import_id": normalized_import_id or (local_job.get("import_id") if isinstance(local_job, dict) else None),
864
+ "process_id_str": normalized_process,
865
+ "matched_by": matched_by,
866
+ "source_file_name": matched_record.get("sourceFileName") or matched_record.get("source_file_name"),
867
+ "total_rows": total_rows,
868
+ "success_rows": success_rows,
869
+ "failed_rows": failed_rows,
870
+ "progress": progress,
871
+ "error_file_urls": _normalize_error_file_urls(matched_record.get("errorFileUrls")),
872
+ "operate_time": matched_record.get("operateTime"),
873
+ "operate_user": matched_record.get("operateUser"),
874
+ "warnings": [],
875
+ "verification": {
876
+ "status_lookup_completed": True,
877
+ "matched_by": matched_by,
878
+ "process_id_verified": bool(normalized_process),
879
+ },
880
+ }
881
+
882
+ try:
883
+ return self._run(profile, runner)
884
+ except RuntimeError as exc:
885
+ return self._runtime_error_as_result(exc, error_code="IMPORT_STATUS_AMBIGUOUS")
886
+
887
+ def _resolve_import_schema_bundle(
888
+ self,
889
+ profile: str,
890
+ context,
891
+ app_key: str,
892
+ *,
893
+ import_capability: JSONObject | None = None,
894
+ ) -> tuple[Any, list[JSONObject], str]: # type: ignore[no-untyped-def]
895
+ """执行内部辅助逻辑。"""
896
+ auth_source = _normalize_optional_text((import_capability or {}).get("auth_source")) or "unknown"
897
+ if auth_source == "data_manage_auth":
898
+ schema = self.backend.request("GET", context, f"/app/{app_key}/form", params={"type": 1})
899
+ index = _build_field_index(_normalize_form_schema(schema))
900
+ else:
901
+ index = self._record_tools._get_field_index(profile, context, app_key, force_refresh=False)
902
+ ws_id = self.sessions.get_profile(profile).selected_ws_id
903
+ expected_columns: list[JSONObject] = []
904
+ for field in index.by_id.values():
905
+ payload = self._record_tools._schema_field_payload(
906
+ profile,
907
+ context,
908
+ field,
909
+ workflow_node_id=None,
910
+ ws_id=ws_id,
911
+ schema_mode="applicant",
912
+ )
913
+ if not bool(payload.get("writable")):
914
+ continue
915
+ expected_columns.append(
916
+ {
917
+ "field_id": payload["field_id"],
918
+ "title": payload["title"],
919
+ "que_type": payload["que_type"],
920
+ "required": bool(field.required),
921
+ "write_kind": payload["write_kind"],
922
+ "options": payload.get("options", []),
923
+ "requires_lookup": bool(payload.get("requires_lookup")),
924
+ "requires_upload": bool(payload.get("requires_upload")),
925
+ "target_app_key": payload.get("target_app_key"),
926
+ "target_app_name": payload.get("target_app_name"),
927
+ "searchable_fields": payload.get("searchable_fields", []),
928
+ }
929
+ )
930
+ expected_columns.sort(key=lambda item: int(item["field_id"]))
931
+ schema_fingerprint = _stable_import_schema_fingerprint(expected_columns)
932
+ return index, expected_columns, schema_fingerprint
933
+
934
+ def _expected_import_columns(
935
+ self,
936
+ profile: str,
937
+ context,
938
+ app_key: str,
939
+ *,
940
+ import_capability: JSONObject | None = None,
941
+ ) -> tuple[list[JSONObject], str]: # type: ignore[no-untyped-def]
942
+ """执行内部辅助逻辑。"""
943
+ _, expected_columns, schema_fingerprint = self._resolve_import_schema_bundle(
944
+ profile,
945
+ context,
946
+ app_key,
947
+ import_capability=import_capability,
948
+ )
949
+ return expected_columns, schema_fingerprint
950
+
951
+ def _local_verify(
952
+ self,
953
+ *,
954
+ profile: str,
955
+ context,
956
+ path: Path,
957
+ app_key: str,
958
+ field_index: Any,
959
+ expected_columns: list[JSONObject],
960
+ allowed_header_titles: list[str] | None,
961
+ schema_fingerprint: str,
962
+ ) -> dict[str, Any]:
963
+ """执行内部辅助逻辑。"""
964
+ extension = path.suffix.lower()
965
+ file_sha256 = _sha256_file(path)
966
+ base_result = {
967
+ "app_key": app_key,
968
+ "file_path": str(path.resolve()),
969
+ "file_size": path.stat().st_size,
970
+ "file_sha256": file_sha256,
971
+ "schema_fingerprint": schema_fingerprint,
972
+ "issues": [],
973
+ "warnings": [],
974
+ "repair_suggestions": [],
975
+ "local_precheck_passed": True,
976
+ "local_precheck_limited": False,
977
+ "can_import": True,
978
+ "extension": extension,
979
+ "error_code": None,
980
+ }
981
+ if extension not in SUPPORTED_IMPORT_EXTENSIONS:
982
+ base_result["issues"].append(_issue("UNSUPPORTED_FILE_FORMAT", "Only .xlsx and .xls files are supported in import v1.", severity="error"))
983
+ base_result["local_precheck_passed"] = False
984
+ base_result["can_import"] = False
985
+ base_result["error_code"] = "IMPORT_FILE_FORMAT_UNSUPPORTED"
986
+ return base_result
987
+ if extension == ".xls":
988
+ base_result["warnings"].append(
989
+ {
990
+ "code": "IMPORT_LOCAL_PRECHECK_LIMITED",
991
+ "message": ".xls files are allowed for verify/start, but v1 local precheck is limited and repair is unsupported.",
992
+ }
993
+ )
994
+ base_result["local_precheck_limited"] = True
995
+ return base_result
996
+
997
+ try:
998
+ workbook = load_workbook(path, read_only=True, data_only=False)
999
+ except Exception as exc:
1000
+ base_result["issues"].append(_issue("WORKBOOK_OPEN_FAILED", f"Workbook could not be opened: {exc}", severity="error"))
1001
+ base_result["local_precheck_passed"] = False
1002
+ base_result["can_import"] = False
1003
+ base_result["error_code"] = "IMPORT_VERIFICATION_FAILED"
1004
+ return base_result
1005
+
1006
+ if not workbook.sheetnames:
1007
+ base_result["issues"].append(_issue("SHEET_MISSING", "Workbook does not contain any sheets.", severity="error"))
1008
+ base_result["local_precheck_passed"] = False
1009
+ base_result["can_import"] = False
1010
+ base_result["error_code"] = "IMPORT_VERIFICATION_FAILED"
1011
+ return base_result
1012
+ try:
1013
+ sheet = workbook[workbook.sheetnames[0]]
1014
+ header_row = [cell.value for cell in next(sheet.iter_rows(min_row=1, max_row=1), [])]
1015
+ header_analysis = _analyze_headers(
1016
+ header_row,
1017
+ expected_columns,
1018
+ allowed_titles=allowed_header_titles,
1019
+ )
1020
+ base_result["issues"].extend(header_analysis["issues"])
1021
+ base_result["repair_suggestions"].extend(header_analysis["repair_suggestions"])
1022
+ if not any(issue.get("severity") == "error" for issue in base_result["issues"]):
1023
+ semantic_issues, semantic_warnings = self._inspect_semantic_cells(
1024
+ profile=profile,
1025
+ context=context,
1026
+ sheet=sheet,
1027
+ expected_columns=expected_columns,
1028
+ field_index=field_index,
1029
+ )
1030
+ base_result["issues"].extend(semantic_issues)
1031
+ base_result["warnings"].extend(semantic_warnings)
1032
+ trailing_blank_rows = _count_trailing_blank_rows(sheet)
1033
+ if trailing_blank_rows > 0:
1034
+ base_result["warnings"].append(
1035
+ {
1036
+ "code": "TRAILING_BLANK_ROWS",
1037
+ "message": f"Workbook contains {trailing_blank_rows} trailing blank rows that can be safely removed.",
1038
+ }
1039
+ )
1040
+ base_result["repair_suggestions"].append("trim_trailing_blank_rows")
1041
+ enum_suggestions = _find_enum_repairs(sheet, expected_columns)
1042
+ if enum_suggestions:
1043
+ base_result["warnings"].append(
1044
+ {
1045
+ "code": "ENUM_VALUE_NORMALIZATION_AVAILABLE",
1046
+ "message": "Some enum-like cells can be normalized to exact template values without changing meaning.",
1047
+ }
1048
+ )
1049
+ base_result["repair_suggestions"].append("normalize_enum_values")
1050
+ base_result["repair_suggestions"] = sorted(set(base_result["repair_suggestions"]))
1051
+ except Exception as exc:
1052
+ base_result["issues"].append(
1053
+ _issue(
1054
+ "IMPORT_LOCAL_PRECHECK_FAILED",
1055
+ f"Workbook content could not be fully inspected during local precheck: {exc}",
1056
+ severity="error",
1057
+ )
1058
+ )
1059
+ base_result["warnings"].append(
1060
+ {
1061
+ "code": "IMPORT_LOCAL_PRECHECK_FAILED",
1062
+ "message": "Workbook local precheck encountered an unexpected compatibility problem; returning a structured verification failure instead of crashing.",
1063
+ }
1064
+ )
1065
+ if any(issue.get("severity") == "error" for issue in base_result["issues"]):
1066
+ base_result["local_precheck_passed"] = False
1067
+ base_result["can_import"] = False
1068
+ base_result["error_code"] = "IMPORT_VERIFICATION_FAILED"
1069
+ return base_result
1070
+
1071
+ def _inspect_semantic_cells(
1072
+ self,
1073
+ *,
1074
+ profile: str,
1075
+ context,
1076
+ sheet,
1077
+ expected_columns: list[JSONObject],
1078
+ field_index: Any,
1079
+ ) -> tuple[list[JSONObject], list[JSONObject]]: # type: ignore[no-untyped-def]
1080
+ """执行内部辅助逻辑。"""
1081
+ issues: list[JSONObject] = []
1082
+ warnings: list[JSONObject] = []
1083
+ header_positions = _sheet_header_positions(sheet)
1084
+ expected_by_key: dict[str, list[JSONObject]] = {}
1085
+ for column in expected_columns:
1086
+ key = _normalize_header_key(column.get("title"))
1087
+ if key:
1088
+ expected_by_key.setdefault(key, []).append(column)
1089
+ for key, columns in expected_by_key.items():
1090
+ positions = header_positions.get(key, [])
1091
+ if len(columns) != 1 or len(positions) != 1:
1092
+ continue
1093
+ column = columns[0]
1094
+ column_index = positions[0]
1095
+ write_kind = _normalize_optional_text(column.get("write_kind")) or "scalar"
1096
+ if column.get("options"):
1097
+ issue = _inspect_enum_column(sheet, column_index=column_index, column=column)
1098
+ if issue is not None:
1099
+ issues.append(issue)
1100
+ continue
1101
+ if write_kind == "relation":
1102
+ issue = _inspect_relation_column(sheet, column_index=column_index, column=column)
1103
+ if issue is not None:
1104
+ issues.append(issue)
1105
+ continue
1106
+ field = field_index.by_id.get(str(column.get("field_id"))) if field_index is not None else None
1107
+ if (
1108
+ write_kind == "member"
1109
+ and field is not None
1110
+ and (
1111
+ field.member_select_scope_type is not None
1112
+ or field.member_select_scope is not None
1113
+ )
1114
+ ):
1115
+ member_issue, member_warning = self._inspect_member_column(
1116
+ context=context,
1117
+ sheet=sheet,
1118
+ column_index=column_index,
1119
+ column=column,
1120
+ field=field,
1121
+ )
1122
+ if member_issue is not None:
1123
+ issues.append(member_issue)
1124
+ continue
1125
+ if member_warning is not None:
1126
+ warnings.append(member_warning)
1127
+ continue
1128
+ if (
1129
+ write_kind == "department"
1130
+ and field is not None
1131
+ and (
1132
+ field.dept_select_scope_type is not None
1133
+ or field.dept_select_scope is not None
1134
+ )
1135
+ ):
1136
+ department_issue, department_warning = self._inspect_department_column(
1137
+ context=context,
1138
+ sheet=sheet,
1139
+ column_index=column_index,
1140
+ column=column,
1141
+ field=field,
1142
+ )
1143
+ if department_issue is not None:
1144
+ issues.append(department_issue)
1145
+ continue
1146
+ if department_warning is not None:
1147
+ warnings.append(department_warning)
1148
+ continue
1149
+ return issues, warnings
1150
+
1151
+ def _inspect_member_column(
1152
+ self,
1153
+ *,
1154
+ context,
1155
+ sheet,
1156
+ column_index: int,
1157
+ column: JSONObject,
1158
+ field,
1159
+ ) -> tuple[JSONObject | None, JSONObject | None]: # type: ignore[no-untyped-def]
1160
+ """执行内部辅助逻辑。"""
1161
+ invalid_email_samples: list[str] = []
1162
+ scope_miss_samples: list[str] = []
1163
+ checked_values: set[str] = set()
1164
+ for row_index in range(2, sheet.max_row + 1):
1165
+ text = _normalize_optional_text(sheet.cell(row=row_index, column=column_index).value)
1166
+ if text is None:
1167
+ continue
1168
+ normalized = text.strip()
1169
+ if normalized in checked_values:
1170
+ continue
1171
+ checked_values.add(normalized)
1172
+ if not EMAIL_PATTERN.fullmatch(normalized):
1173
+ invalid_email_samples.append(f"row {row_index}: {normalized}")
1174
+ if len(invalid_email_samples) >= 3:
1175
+ break
1176
+ continue
1177
+ try:
1178
+ candidates = self._record_tools._resolve_member_candidates(context, field, keyword=normalized)
1179
+ matches = self._record_tools._match_member_candidates(candidates, normalized)
1180
+ except QingflowApiError as exc:
1181
+ if exc.category == "not_supported":
1182
+ return None, {
1183
+ "code": "MEMBER_CANDIDATE_VALIDATION_SKIPPED",
1184
+ "message": f"Member candidate scope for column '{column['title']}' could not be resolved safely during local precheck.",
1185
+ }
1186
+ raise
1187
+ except RuntimeError:
1188
+ return None, {
1189
+ "code": "MEMBER_CANDIDATE_VALIDATION_SKIPPED",
1190
+ "message": f"Member candidate scope for column '{column['title']}' could not be resolved safely during local precheck.",
1191
+ }
1192
+ if len(matches) != 1:
1193
+ scope_miss_samples.append(f"row {row_index}: {normalized}")
1194
+ if len(scope_miss_samples) >= 3:
1195
+ break
1196
+ if invalid_email_samples:
1197
+ return _issue(
1198
+ "MEMBER_IMPORT_REQUIRES_EMAIL",
1199
+ f"Column '{column['title']}' must use member email values in import files. Samples: {', '.join(invalid_email_samples)}",
1200
+ severity="error",
1201
+ ), None
1202
+ if scope_miss_samples:
1203
+ return _issue(
1204
+ "MEMBER_NOT_IN_CANDIDATE_SCOPE",
1205
+ f"Column '{column['title']}' contains members outside the current candidate scope. Samples: {', '.join(scope_miss_samples)}",
1206
+ severity="error",
1207
+ ), None
1208
+ return None, None
1209
+
1210
+ def _inspect_department_column(
1211
+ self,
1212
+ *,
1213
+ context,
1214
+ sheet,
1215
+ column_index: int,
1216
+ column: JSONObject,
1217
+ field,
1218
+ ) -> tuple[JSONObject | None, JSONObject | None]: # type: ignore[no-untyped-def]
1219
+ """执行内部辅助逻辑。"""
1220
+ scope_miss_samples: list[str] = []
1221
+ checked_values: set[str] = set()
1222
+ for row_index in range(2, sheet.max_row + 1):
1223
+ value = sheet.cell(row=row_index, column=column_index).value
1224
+ text = _normalize_optional_text(value)
1225
+ if text is None:
1226
+ continue
1227
+ normalized = text.strip()
1228
+ if normalized in checked_values:
1229
+ continue
1230
+ checked_values.add(normalized)
1231
+ try:
1232
+ candidates = self._record_tools._resolve_department_candidates(context, field, keyword=normalized)
1233
+ matches = self._record_tools._match_department_candidates(candidates, normalized)
1234
+ except QingflowApiError as exc:
1235
+ if exc.category == "not_supported":
1236
+ return None, {
1237
+ "code": "DEPARTMENT_CANDIDATE_VALIDATION_SKIPPED",
1238
+ "message": f"Department candidate scope for column '{column['title']}' could not be resolved safely during local precheck.",
1239
+ }
1240
+ raise
1241
+ except RuntimeError:
1242
+ return None, {
1243
+ "code": "DEPARTMENT_CANDIDATE_VALIDATION_SKIPPED",
1244
+ "message": f"Department candidate scope for column '{column['title']}' could not be resolved safely during local precheck.",
1245
+ }
1246
+ if len(matches) != 1:
1247
+ scope_miss_samples.append(f"row {row_index}: {normalized}")
1248
+ if len(scope_miss_samples) >= 3:
1249
+ break
1250
+ if scope_miss_samples:
1251
+ return _issue(
1252
+ "DEPARTMENT_NOT_IN_CANDIDATE_SCOPE",
1253
+ f"Column '{column['title']}' contains departments outside the current candidate scope. Samples: {', '.join(scope_miss_samples)}",
1254
+ severity="error",
1255
+ ), None
1256
+ return None, None
1257
+
1258
+ def _load_template_header_profile(
1259
+ self,
1260
+ context,
1261
+ app_key: str,
1262
+ *,
1263
+ import_capability: JSONObject | None = None,
1264
+ expected_columns: list[JSONObject] | None = None,
1265
+ ) -> tuple[dict[str, Any], list[JSONObject]]: # type: ignore[no-untyped-def]
1266
+ """执行内部辅助逻辑。"""
1267
+ warnings: list[JSONObject] = []
1268
+ try:
1269
+ payload = self.backend.request("GET", context, f"/app/{app_key}/apply/excelTemplate")
1270
+ template_url = _pick_template_url(payload)
1271
+ if not template_url:
1272
+ return {"allowed_titles": None, "leaf_titles": None, "header_depth": 1}, warnings
1273
+ content = self.backend.download_binary(template_url)
1274
+ workbook = load_workbook(BytesIO(content), read_only=False, data_only=False)
1275
+ if not workbook.sheetnames:
1276
+ return {"allowed_titles": None, "leaf_titles": None, "header_depth": 1}, warnings
1277
+ sheet = workbook[workbook.sheetnames[0]]
1278
+ header_row = [cell.value for cell in next(sheet.iter_rows(min_row=1, max_row=1), [])]
1279
+ titles = [_normalize_optional_text(value) for value in header_row]
1280
+ normalized_titles = [title for title in titles if title]
1281
+ header_depth = _infer_header_depth(sheet)
1282
+ leaf_titles = [title for title in _extract_leaf_header_titles(sheet, header_depth) if title]
1283
+ return {
1284
+ "allowed_titles": normalized_titles or None,
1285
+ "leaf_titles": leaf_titles or None,
1286
+ "header_depth": header_depth,
1287
+ }, warnings
1288
+ except Exception:
1289
+ if (
1290
+ _normalize_optional_text((import_capability or {}).get("auth_source")) == "apply_auth"
1291
+ and expected_columns
1292
+ ):
1293
+ warnings.append(
1294
+ {
1295
+ "code": "IMPORT_TEMPLATE_HEADER_LOCAL_FALLBACK",
1296
+ "message": "Official template headers require data management permission; local precheck fell back to applicant import columns.",
1297
+ }
1298
+ )
1299
+ fallback_titles = [str(item["title"]) for item in expected_columns]
1300
+ return {"allowed_titles": fallback_titles, "leaf_titles": fallback_titles, "header_depth": 1}, warnings
1301
+ warnings.append(
1302
+ {
1303
+ "code": "IMPORT_TEMPLATE_HEADER_UNAVAILABLE",
1304
+ "message": "Official template headers could not be loaded during local precheck; falling back to applicant writable columns only.",
1305
+ }
1306
+ )
1307
+ return {"allowed_titles": None, "leaf_titles": None, "header_depth": 1}, warnings
1308
+
1309
+ def _maybe_auto_normalize_file(
1310
+ self,
1311
+ *,
1312
+ source_path: Path,
1313
+ expected_columns: list[JSONObject],
1314
+ template_header_profile: dict[str, Any],
1315
+ local_check: dict[str, Any],
1316
+ ) -> dict[str, Any] | None:
1317
+ """执行内部辅助逻辑。"""
1318
+ if source_path.suffix.lower() != ".xlsx":
1319
+ return None
1320
+ try:
1321
+ workbook = load_workbook(source_path, read_only=False, data_only=False)
1322
+ if not workbook.sheetnames:
1323
+ return None
1324
+ sheet = workbook[workbook.sheetnames[0]]
1325
+ rows = [list(row) for row in sheet.iter_rows(values_only=True)]
1326
+ header_depth = _infer_header_depth(sheet)
1327
+ return _build_auto_normalized_file(
1328
+ source_path=source_path,
1329
+ sheet_title=sheet.title,
1330
+ rows=rows,
1331
+ header_depth=header_depth,
1332
+ template_leaf_titles=template_header_profile.get("leaf_titles"),
1333
+ local_check=local_check,
1334
+ )
1335
+ except Exception as exc:
1336
+ workbook = load_workbook(source_path, read_only=True, data_only=False)
1337
+ if not workbook.sheetnames:
1338
+ return None
1339
+ sheet = workbook[workbook.sheetnames[0]]
1340
+ rows = [list(row) for row in sheet.iter_rows(values_only=True)]
1341
+ header_depth = _infer_header_depth_from_rows(
1342
+ rows,
1343
+ template_header_profile=template_header_profile,
1344
+ local_check=local_check,
1345
+ )
1346
+ normalized = _build_auto_normalized_file(
1347
+ source_path=source_path,
1348
+ sheet_title=sheet.title,
1349
+ rows=rows,
1350
+ header_depth=header_depth,
1351
+ template_leaf_titles=template_header_profile.get("leaf_titles"),
1352
+ local_check=local_check,
1353
+ )
1354
+ if normalized is not None:
1355
+ normalized["warnings"].insert(
1356
+ 0,
1357
+ {
1358
+ "code": "IMPORT_AUTO_NORMALIZATION_COMPATIBILITY_FALLBACK",
1359
+ "message": f"Workbook compatibility normalization retried in compatibility mode after a workbook parsing error: {exc}",
1360
+ },
1361
+ )
1362
+ return normalized
1363
+
1364
+ def _fetch_import_capability(self, context, app_key: str) -> tuple[JSONObject, list[JSONObject]]: # type: ignore[no-untyped-def]
1365
+ """执行内部辅助逻辑。"""
1366
+ try:
1367
+ payload = self.backend.request("GET", context, f"/app/{app_key}/baseInfo")
1368
+ except QingflowApiError:
1369
+ payload = None
1370
+ return _derive_import_capability(payload)
1371
+
1372
+ def _write_local_template(
1373
+ self,
1374
+ *,
1375
+ expected_columns: list[JSONObject],
1376
+ destination_hint: str | None,
1377
+ app_key: str,
1378
+ ) -> str:
1379
+ """执行内部辅助逻辑。"""
1380
+ if destination_hint:
1381
+ destination = _resolve_template_download_path(destination_hint, app_key=app_key)
1382
+ else:
1383
+ destination = Path(tempfile.gettempdir()) / f"qingflow-import-template-{app_key}-{uuid4().hex[:8]}.xlsx"
1384
+ destination.parent.mkdir(parents=True, exist_ok=True)
1385
+ workbook = Workbook()
1386
+ sheet = workbook.active
1387
+ sheet.title = "导入模板"
1388
+ sheet.append([str(item["title"]) for item in expected_columns])
1389
+ workbook.save(destination)
1390
+ return str(destination)
1391
+
1392
+ def _failed_template_result(
1393
+ self,
1394
+ *,
1395
+ app_key: str,
1396
+ error_code: str,
1397
+ message: str,
1398
+ request_route: JSONObject | None = None,
1399
+ ) -> dict[str, Any]:
1400
+ """执行内部辅助逻辑。"""
1401
+ return {
1402
+ "ok": False,
1403
+ "status": "failed",
1404
+ "error_code": error_code,
1405
+ "app_key": app_key,
1406
+ "template_url": None,
1407
+ "downloaded_to_path": None,
1408
+ "expected_columns": [],
1409
+ "schema_fingerprint": None,
1410
+ "request_route": request_route,
1411
+ "warnings": [],
1412
+ "verification": {"template_url_resolved": False},
1413
+ "message": message,
1414
+ }
1415
+
1416
+ def _failed_verify_result(
1417
+ self,
1418
+ *,
1419
+ app_key: str,
1420
+ file_path: str,
1421
+ error_code: str,
1422
+ message: str,
1423
+ extra: dict[str, Any] | None = None,
1424
+ ) -> dict[str, Any]:
1425
+ """执行内部辅助逻辑。"""
1426
+ payload = {
1427
+ "ok": True,
1428
+ "status": "failed",
1429
+ "error_code": error_code,
1430
+ "app_key": app_key,
1431
+ "can_import": False,
1432
+ "verification_id": None,
1433
+ "file_path": str(Path(file_path).expanduser()) if file_path else file_path,
1434
+ "verified_file_path": None,
1435
+ "file_name": Path(file_path).name if file_path else None,
1436
+ "file_sha256": None,
1437
+ "verified_file_sha256": None,
1438
+ "file_size": None,
1439
+ "schema_fingerprint": None,
1440
+ "apply_rows": None,
1441
+ "issues": [_issue(error_code, message, severity="error")],
1442
+ "repair_suggestions": [],
1443
+ "warnings": [],
1444
+ "verification": {
1445
+ "import_auth_prechecked": False,
1446
+ "import_auth_precheck_passed": False,
1447
+ "local_precheck_passed": False,
1448
+ "backend_verification_passed": False,
1449
+ },
1450
+ "import_capability": None,
1451
+ "message": message,
1452
+ }
1453
+ if extra:
1454
+ payload.update(extra)
1455
+ return payload
1456
+
1457
+ def _failed_repair_result(self, *, error_code: str, message: str, extra: dict[str, Any] | None = None) -> dict[str, Any]:
1458
+ """执行内部辅助逻辑。"""
1459
+ payload = {
1460
+ "ok": False,
1461
+ "status": "failed",
1462
+ "error_code": error_code,
1463
+ "source_file_path": None,
1464
+ "repaired_file_path": None,
1465
+ "applied_repairs": [],
1466
+ "skipped_repairs": [],
1467
+ "new_verification_id": None,
1468
+ "can_import_after_repair": False,
1469
+ "post_repair_issues": [_issue(error_code, message, severity="error")],
1470
+ "warnings": [],
1471
+ "verification": {
1472
+ "repair_authorized": False,
1473
+ "reverified": False,
1474
+ },
1475
+ "message": message,
1476
+ }
1477
+ if extra:
1478
+ payload.update(extra)
1479
+ return payload
1480
+
1481
+ def _failed_start_result(self, *, error_code: str, message: str, extra: dict[str, Any] | None = None) -> dict[str, Any]:
1482
+ """执行内部辅助逻辑。"""
1483
+ payload = {
1484
+ "ok": False,
1485
+ "status": "failed",
1486
+ "error_code": error_code,
1487
+ "accepted": False,
1488
+ "import_id": None,
1489
+ "process_id_str": None,
1490
+ "source_file_name": None,
1491
+ "file_url": None,
1492
+ "warnings": [],
1493
+ "verification": {
1494
+ "verification_id_valid": False,
1495
+ "file_hash_verified": False,
1496
+ "schema_fingerprint_verified": False,
1497
+ "upload_staged": False,
1498
+ "import_acknowledged": False,
1499
+ },
1500
+ "message": message,
1501
+ }
1502
+ if extra:
1503
+ payload.update(extra)
1504
+ return payload
1505
+
1506
+ def _failed_status_result(self, *, error_code: str, message: str, extra: dict[str, Any] | None = None) -> dict[str, Any]:
1507
+ """执行内部辅助逻辑。"""
1508
+ payload = {
1509
+ "ok": False,
1510
+ "status": "failed",
1511
+ "error_code": error_code,
1512
+ "import_id": None,
1513
+ "process_id_str": None,
1514
+ "matched_by": None,
1515
+ "source_file_name": None,
1516
+ "total_rows": None,
1517
+ "success_rows": None,
1518
+ "failed_rows": None,
1519
+ "progress": None,
1520
+ "error_file_urls": [],
1521
+ "operate_time": None,
1522
+ "operate_user": None,
1523
+ "warnings": [],
1524
+ "verification": {
1525
+ "status_lookup_completed": False,
1526
+ "process_id_verified": False,
1527
+ },
1528
+ "message": message,
1529
+ }
1530
+ if extra:
1531
+ payload.update(extra)
1532
+ return payload
1533
+
1534
+ def _runtime_error_as_result(
1535
+ self,
1536
+ error: RuntimeError,
1537
+ *,
1538
+ error_code: str,
1539
+ extra: dict[str, Any] | None = None,
1540
+ ) -> dict[str, Any]:
1541
+ """执行内部辅助逻辑。"""
1542
+ try:
1543
+ payload = json.loads(str(error))
1544
+ except json.JSONDecodeError:
1545
+ payload = {"message": str(error)}
1546
+ response = {
1547
+ "ok": False,
1548
+ "status": "failed",
1549
+ "error_code": ((payload.get("details") or {}) if isinstance(payload.get("details"), dict) else {}).get("error_code") or error_code,
1550
+ "warnings": [],
1551
+ "verification": {},
1552
+ "message": payload.get("message") or str(error),
1553
+ }
1554
+ if extra:
1555
+ response.update(extra)
1556
+ return response
1557
+
1558
+
1559
+ def _pick_template_url(payload: Any) -> str | None:
1560
+ if isinstance(payload, dict):
1561
+ for key in ("excelUrl", "url", "downloadUrl"):
1562
+ value = payload.get(key)
1563
+ if isinstance(value, str) and value.strip():
1564
+ return value.strip()
1565
+ return None
1566
+
1567
+
1568
+ def _resolve_template_download_path(raw_path: str, *, app_key: str) -> Path:
1569
+ path = Path(raw_path).expanduser()
1570
+ if path.exists() and path.is_dir():
1571
+ return path / f"{app_key}_import_template.xlsx"
1572
+ if path.suffix:
1573
+ return path
1574
+ return path / f"{app_key}_import_template.xlsx"
1575
+
1576
+
1577
+ def _resolve_repaired_output_path(source_path: Path, *, output_path: str | None) -> Path:
1578
+ if output_path:
1579
+ path = Path(output_path).expanduser()
1580
+ if path.exists() and path.is_dir():
1581
+ return path / f"{source_path.stem}.repaired{source_path.suffix}"
1582
+ if path.suffix:
1583
+ return path
1584
+ return path / f"{source_path.stem}.repaired{source_path.suffix}"
1585
+ return source_path.with_name(f"{source_path.stem}.repaired{source_path.suffix}")
1586
+
1587
+
1588
+ def _resolve_verified_output_path(source_path: Path) -> Path:
1589
+ return Path(tempfile.gettempdir()) / f"qingflow-import-verified-{source_path.stem}-{uuid4().hex[:8]}{source_path.suffix}"
1590
+
1591
+
1592
+ def _utc_now() -> datetime:
1593
+ return datetime.now(timezone.utc)
1594
+
1595
+
1596
+ def _sha256_file(path: Path) -> str:
1597
+ digest = hashlib.sha256()
1598
+ with path.open("rb") as handle:
1599
+ for chunk in iter(lambda: handle.read(1024 * 1024), b""):
1600
+ digest.update(chunk)
1601
+ return digest.hexdigest()
1602
+
1603
+
1604
+ def _normalize_optional_text(value: Any) -> str | None:
1605
+ if value is None:
1606
+ return None
1607
+ normalized = str(value).strip()
1608
+ return normalized or None
1609
+
1610
+
1611
+ def _normalize_header_key(value: Any) -> str:
1612
+ text = _normalize_optional_text(value)
1613
+ return (text or "").casefold()
1614
+
1615
+
1616
+ def _issue(code: str, message: str, *, severity: str, repairable: bool = False, repair_code: str | None = None) -> JSONObject:
1617
+ payload: JSONObject = {
1618
+ "code": code,
1619
+ "message": message,
1620
+ "severity": severity,
1621
+ "repairable": repairable,
1622
+ }
1623
+ if repair_code:
1624
+ payload["repair_code"] = repair_code
1625
+ return payload
1626
+
1627
+
1628
+ def _analyze_headers(
1629
+ header_row: list[Any],
1630
+ expected_columns: list[JSONObject],
1631
+ *,
1632
+ allowed_titles: list[str] | None = None,
1633
+ ) -> dict[str, Any]:
1634
+ expected_titles = [str(item["title"]) for item in expected_columns]
1635
+ allowed_title_list = allowed_titles if allowed_titles else expected_titles
1636
+ allowed_counts = _header_title_counts(allowed_title_list)
1637
+ allowed_by_key = {
1638
+ key: title
1639
+ for key, title in (
1640
+ (_normalize_header_key(title), _normalize_optional_text(title))
1641
+ for title in allowed_title_list
1642
+ )
1643
+ if key and title
1644
+ }
1645
+ seen: dict[str, int] = {}
1646
+ actual_headers: list[str] = []
1647
+ for item in header_row:
1648
+ text = _normalize_optional_text(item)
1649
+ if text is None:
1650
+ actual_headers.append("")
1651
+ continue
1652
+ actual_headers.append(text)
1653
+ key = _normalize_header_key(text)
1654
+ seen[key] = seen.get(key, 0) + 1
1655
+ missing: list[str] = []
1656
+ for key, expected_count in allowed_counts.items():
1657
+ actual_count = seen.get(key, 0)
1658
+ if actual_count >= expected_count:
1659
+ continue
1660
+ title = allowed_by_key.get(key) or key
1661
+ if expected_count <= 1:
1662
+ missing.append(title)
1663
+ else:
1664
+ missing.append(f"{title} (need {expected_count}, got {actual_count})")
1665
+ extra = [text for text in actual_headers if text and _normalize_header_key(text) not in allowed_by_key]
1666
+ duplicates = []
1667
+ for key, count in seen.items():
1668
+ if not key:
1669
+ continue
1670
+ allowed_count = allowed_counts.get(key, 0)
1671
+ if count > max(allowed_count, 1 if allowed_count == 0 else allowed_count):
1672
+ duplicates.append(allowed_by_key.get(key) or key)
1673
+ issues: list[JSONObject] = []
1674
+ repair_suggestions: list[str] = []
1675
+ if missing:
1676
+ issues.append(
1677
+ _issue(
1678
+ "MISSING_COLUMNS",
1679
+ f"Missing expected columns: {', '.join(missing)}",
1680
+ severity="error",
1681
+ repairable=True,
1682
+ repair_code="normalize_headers",
1683
+ )
1684
+ )
1685
+ if extra:
1686
+ issues.append(
1687
+ _issue(
1688
+ "EXTRA_COLUMNS",
1689
+ f"Unexpected columns: {', '.join(extra)}",
1690
+ severity="error",
1691
+ repairable=True,
1692
+ repair_code="normalize_headers",
1693
+ )
1694
+ )
1695
+ if duplicates:
1696
+ issues.append(
1697
+ _issue(
1698
+ "DUPLICATE_COLUMNS",
1699
+ f"Duplicate columns: {', '.join(sorted(set(duplicates)))}",
1700
+ severity="error",
1701
+ repairable=True,
1702
+ repair_code="normalize_headers",
1703
+ )
1704
+ )
1705
+ normalized_changes = []
1706
+ for text in actual_headers:
1707
+ if not text:
1708
+ continue
1709
+ canonical = allowed_by_key.get(_normalize_header_key(text))
1710
+ if canonical and canonical != text:
1711
+ normalized_changes.append((text, canonical))
1712
+ if missing or extra or duplicates or normalized_changes:
1713
+ repair_suggestions.append("normalize_headers")
1714
+ return {"issues": issues, "repair_suggestions": repair_suggestions}
1715
+
1716
+
1717
+ def _header_title_counts(titles: list[str]) -> dict[str, int]:
1718
+ counts: dict[str, int] = {}
1719
+ for title in titles:
1720
+ key = _normalize_header_key(title)
1721
+ if not key:
1722
+ continue
1723
+ counts[key] = counts.get(key, 0) + 1
1724
+ return counts
1725
+
1726
+
1727
+ def _sheet_header_positions(sheet) -> dict[str, list[int]]: # type: ignore[no-untyped-def]
1728
+ mapping: dict[str, list[int]] = {}
1729
+ for index, cell in enumerate(next(sheet.iter_rows(min_row=1, max_row=1), []), start=1):
1730
+ key = _normalize_header_key(cell.value)
1731
+ if not key:
1732
+ continue
1733
+ mapping.setdefault(key, []).append(index)
1734
+ return mapping
1735
+
1736
+
1737
+ def _inspect_enum_column(sheet, *, column_index: int, column: JSONObject) -> JSONObject | None: # type: ignore[no-untyped-def]
1738
+ options = [str(item).strip() for item in column.get("options", []) if str(item).strip()]
1739
+ if not options:
1740
+ return None
1741
+ option_map = {_normalize_header_key(item): item for item in options}
1742
+ invalid_samples: list[str] = []
1743
+ for row_index in range(2, sheet.max_row + 1):
1744
+ text = _normalize_optional_text(sheet.cell(row=row_index, column=column_index).value)
1745
+ if text is None:
1746
+ continue
1747
+ if _normalize_header_key(text) in option_map:
1748
+ continue
1749
+ invalid_samples.append(f"row {row_index}: {text}")
1750
+ if len(invalid_samples) >= 3:
1751
+ break
1752
+ if not invalid_samples:
1753
+ return None
1754
+ return _issue(
1755
+ "INVALID_ENUM_VALUES",
1756
+ f"Column '{column['title']}' contains values outside the allowed options. Samples: {', '.join(invalid_samples)}",
1757
+ severity="error",
1758
+ )
1759
+
1760
+
1761
+ def _inspect_relation_column(sheet, *, column_index: int, column: JSONObject) -> JSONObject | None: # type: ignore[no-untyped-def]
1762
+ invalid_samples: list[str] = []
1763
+ for row_index in range(2, sheet.max_row + 1):
1764
+ value = sheet.cell(row=row_index, column=column_index).value
1765
+ text = _normalize_optional_text(value)
1766
+ if text is None:
1767
+ continue
1768
+ relation_id = _coerce_positive_relation_id(value)
1769
+ if relation_id is not None:
1770
+ continue
1771
+ invalid_samples.append(f"row {row_index}: {text}")
1772
+ if len(invalid_samples) >= 3:
1773
+ break
1774
+ if not invalid_samples:
1775
+ return None
1776
+ return _issue(
1777
+ "RELATION_IMPORT_REQUIRES_APPLY_ID",
1778
+ f"Column '{column['title']}' must use target record apply_id values during import. Samples: {', '.join(invalid_samples)}",
1779
+ severity="error",
1780
+ )
1781
+
1782
+
1783
+ def _stable_import_schema_fingerprint(expected_columns: list[JSONObject]) -> str:
1784
+ stable_columns = []
1785
+ for item in expected_columns:
1786
+ stable_columns.append(
1787
+ {
1788
+ "field_id": item["field_id"],
1789
+ "title": item["title"],
1790
+ "que_type": item["que_type"],
1791
+ "required": item["required"],
1792
+ "write_kind": item["write_kind"],
1793
+ "options": item.get("options", []),
1794
+ "requires_lookup": bool(item.get("requires_lookup")),
1795
+ "requires_upload": bool(item.get("requires_upload")),
1796
+ "target_app_key": item.get("target_app_key"),
1797
+ }
1798
+ )
1799
+ return hashlib.sha256(
1800
+ json.dumps(stable_columns, ensure_ascii=False, sort_keys=True).encode("utf-8")
1801
+ ).hexdigest()
1802
+
1803
+
1804
+ def _coerce_positive_relation_id(value: Any) -> int | None:
1805
+ if isinstance(value, bool):
1806
+ return None
1807
+ if isinstance(value, int):
1808
+ return value if value > 0 else None
1809
+ if isinstance(value, float):
1810
+ if value.is_integer() and value > 0:
1811
+ return int(value)
1812
+ return None
1813
+ text = _normalize_optional_text(value)
1814
+ if text is None:
1815
+ return None
1816
+ if text.isdigit():
1817
+ parsed = int(text)
1818
+ return parsed if parsed > 0 else None
1819
+ return None
1820
+
1821
+
1822
+ def _infer_header_depth(sheet) -> int: # type: ignore[no-untyped-def]
1823
+ header_depth = 1
1824
+ merged_cells = getattr(sheet, "merged_cells", None)
1825
+ merged_ranges = getattr(merged_cells, "ranges", merged_cells) if merged_cells is not None else []
1826
+ row_one_has_merge = False
1827
+ for merged_range in merged_ranges or []:
1828
+ min_row = int(getattr(merged_range, "min_row", 1))
1829
+ max_row = int(getattr(merged_range, "max_row", 1))
1830
+ if min_row == 1:
1831
+ row_one_has_merge = True
1832
+ header_depth = max(header_depth, max_row)
1833
+ if row_one_has_merge and sheet.max_row >= 2:
1834
+ row_two_values = [cell.value for cell in sheet[2]]
1835
+ if any(_normalize_optional_text(value) for value in row_two_values):
1836
+ header_depth = max(header_depth, 2)
1837
+ return min(header_depth, max(1, int(sheet.max_row)))
1838
+
1839
+
1840
+ def _extract_leaf_header_titles(sheet, header_depth: int) -> list[str]: # type: ignore[no-untyped-def]
1841
+ titles: list[str] = []
1842
+ max_column = max(1, int(sheet.max_column))
1843
+ depth = max(1, min(header_depth, int(sheet.max_row)))
1844
+ for column_index in range(1, max_column + 1):
1845
+ selected = ""
1846
+ for row_index in range(depth, 0, -1):
1847
+ text = _normalize_optional_text(sheet.cell(row=row_index, column=column_index).value)
1848
+ if text:
1849
+ selected = text
1850
+ break
1851
+ titles.append(selected)
1852
+ return titles
1853
+
1854
+
1855
+ def _overlay_header_titles(actual_titles: list[str], template_leaf_titles: Any) -> list[str]:
1856
+ normalized = list(actual_titles)
1857
+ if not isinstance(template_leaf_titles, list):
1858
+ return normalized
1859
+ for index, title in enumerate(template_leaf_titles):
1860
+ normalized_title = _normalize_optional_text(title)
1861
+ if normalized_title is None:
1862
+ continue
1863
+ if index < len(normalized):
1864
+ normalized[index] = normalized_title
1865
+ return normalized
1866
+
1867
+
1868
+ def _infer_header_depth_from_rows(
1869
+ rows: list[list[Any]],
1870
+ *,
1871
+ template_header_profile: dict[str, Any],
1872
+ local_check: dict[str, Any],
1873
+ ) -> int:
1874
+ template_depth = max(1, int(template_header_profile.get("header_depth") or 1))
1875
+ header_depth = min(template_depth, max(1, len(rows)))
1876
+ if header_depth > 1:
1877
+ return header_depth
1878
+ if "normalize_headers" in (local_check.get("repair_suggestions") or []) and len(rows) >= 2:
1879
+ if any(_normalize_optional_text(value) for value in rows[1]):
1880
+ return 2
1881
+ return 1
1882
+
1883
+
1884
+ def _extract_leaf_header_titles_from_rows(rows: list[list[Any]], header_depth: int) -> list[str]:
1885
+ titles: list[str] = []
1886
+ max_column = max((len(row) for row in rows[: max(1, header_depth)]), default=0)
1887
+ depth = max(1, min(header_depth, len(rows)))
1888
+ for column_index in range(max_column):
1889
+ selected = ""
1890
+ for row_index in range(depth - 1, -1, -1):
1891
+ value = rows[row_index][column_index] if column_index < len(rows[row_index]) else None
1892
+ text = _normalize_optional_text(value)
1893
+ if text:
1894
+ selected = text
1895
+ break
1896
+ titles.append(selected)
1897
+ return titles
1898
+
1899
+
1900
+ def _count_trailing_blank_rows_from_rows(rows: list[list[Any]], *, min_data_index: int = 1) -> int:
1901
+ count = 0
1902
+ for row in reversed(rows[min_data_index:]):
1903
+ if any(value not in (None, "") for value in row):
1904
+ break
1905
+ count += 1
1906
+ return count
1907
+
1908
+
1909
+ def _build_auto_normalized_file(
1910
+ *,
1911
+ source_path: Path,
1912
+ sheet_title: str,
1913
+ rows: list[list[Any]],
1914
+ header_depth: int,
1915
+ template_leaf_titles: Any,
1916
+ local_check: dict[str, Any],
1917
+ ) -> dict[str, Any] | None:
1918
+ if not rows:
1919
+ return None
1920
+ normalized_header_depth = max(1, min(header_depth, len(rows)))
1921
+ trailing_blank_rows = _count_trailing_blank_rows_from_rows(rows, min_data_index=normalized_header_depth)
1922
+ if normalized_header_depth <= 1 and trailing_blank_rows <= 0:
1923
+ return None
1924
+ extracted_headers = _extract_leaf_header_titles_from_rows(rows, normalized_header_depth)
1925
+ target_headers = _overlay_header_titles(extracted_headers, template_leaf_titles)
1926
+ row_width = max(len(target_headers), max((len(row) for row in rows), default=0))
1927
+ if row_width <= 0:
1928
+ return None
1929
+ padded_headers = list(target_headers) + [""] * max(0, row_width - len(target_headers))
1930
+ verified_path = _resolve_verified_output_path(source_path)
1931
+ normalized_workbook = Workbook()
1932
+ normalized_sheet = normalized_workbook.active
1933
+ normalized_sheet.title = sheet_title
1934
+ normalized_sheet.append(padded_headers)
1935
+ last_nonblank_row = max(normalized_header_depth, len(rows) - trailing_blank_rows)
1936
+ for row in rows[normalized_header_depth:last_nonblank_row]:
1937
+ normalized_sheet.append(list(row) + [None] * max(0, row_width - len(row)))
1938
+ verified_path.parent.mkdir(parents=True, exist_ok=True)
1939
+ normalized_workbook.save(verified_path)
1940
+ warnings: list[JSONObject] = []
1941
+ applied_repairs: list[str] = []
1942
+ if normalized_header_depth > 1:
1943
+ applied_repairs.append("normalize_headers")
1944
+ warnings.append(
1945
+ {
1946
+ "code": "IMPORT_HEADERS_AUTO_NORMALIZED",
1947
+ "message": f"Workbook used {normalized_header_depth} header rows; record_import_verify normalized it to a single leaf-header row automatically.",
1948
+ }
1949
+ )
1950
+ if trailing_blank_rows > 0:
1951
+ applied_repairs.append("trim_trailing_blank_rows")
1952
+ warnings.append(
1953
+ {
1954
+ "code": "TRAILING_BLANK_ROWS_AUTO_TRIMMED",
1955
+ "message": f"Removed {trailing_blank_rows} trailing blank rows before backend verification.",
1956
+ }
1957
+ )
1958
+ return {
1959
+ "verified_file_path": str(verified_path.resolve()),
1960
+ "header_titles": target_headers or padded_headers,
1961
+ "warnings": warnings,
1962
+ "applied_repairs": applied_repairs,
1963
+ "header_depth": normalized_header_depth,
1964
+ "trailing_blank_rows": trailing_blank_rows,
1965
+ "source_local_check": local_check,
1966
+ }
1967
+
1968
+
1969
+ def _count_trailing_blank_rows(sheet) -> int: # type: ignore[no-untyped-def]
1970
+ count = 0
1971
+ for row_index in range(sheet.max_row, 1, -1):
1972
+ values = [cell.value for cell in sheet[row_index]]
1973
+ if any(value not in (None, "") for value in values):
1974
+ break
1975
+ count += 1
1976
+ return count
1977
+
1978
+
1979
+ def _find_enum_repairs(sheet, expected_columns: list[JSONObject]) -> list[str]: # type: ignore[no-untyped-def]
1980
+ header_map = _sheet_header_map(sheet)
1981
+ found: list[str] = []
1982
+ for column in expected_columns:
1983
+ options = [str(item).strip() for item in column.get("options", []) if str(item).strip()]
1984
+ if not options:
1985
+ continue
1986
+ column_index = header_map.get(_normalize_header_key(column["title"]))
1987
+ if column_index is None:
1988
+ continue
1989
+ option_map = {_normalize_header_key(item): item for item in options}
1990
+ for row in range(2, min(sheet.max_row, 50) + 1):
1991
+ value = sheet.cell(row=row, column=column_index).value
1992
+ text = _normalize_optional_text(value)
1993
+ if text is None:
1994
+ continue
1995
+ exact = option_map.get(_normalize_header_key(text))
1996
+ if exact and exact != text:
1997
+ found.append(column["title"])
1998
+ break
1999
+ return found
2000
+
2001
+
2002
+ def _sheet_header_map(sheet) -> dict[str, int]: # type: ignore[no-untyped-def]
2003
+ mapping: dict[str, int] = {}
2004
+ for index, cell in enumerate(next(sheet.iter_rows(min_row=1, max_row=1), []), start=1):
2005
+ key = _normalize_header_key(cell.value)
2006
+ if key and key not in mapping:
2007
+ mapping[key] = index
2008
+ return mapping
2009
+
2010
+
2011
+ def _repair_headers(sheet, expected_columns: list[JSONObject]) -> bool: # type: ignore[no-untyped-def]
2012
+ changed = False
2013
+ expected_by_key = {_normalize_header_key(item["title"]): item["title"] for item in expected_columns}
2014
+ header_cells = list(next(sheet.iter_rows(min_row=1, max_row=1), []))
2015
+ for cell in header_cells:
2016
+ text = _normalize_optional_text(cell.value)
2017
+ if text is None:
2018
+ continue
2019
+ canonical = expected_by_key.get(_normalize_header_key(text))
2020
+ if canonical and canonical != text:
2021
+ cell.value = canonical
2022
+ changed = True
2023
+ if changed:
2024
+ return True
2025
+
2026
+ # Fallback for template-based files where headers were edited into non-canonical
2027
+ # values but column order is still intact. Keep any extra trailing system columns.
2028
+ for index, column in enumerate(expected_columns, start=1):
2029
+ if index > len(header_cells):
2030
+ break
2031
+ expected_title = str(column["title"]).strip()
2032
+ current_title = _normalize_optional_text(header_cells[index - 1].value)
2033
+ if current_title != expected_title:
2034
+ header_cells[index - 1].value = expected_title
2035
+ changed = True
2036
+ return changed
2037
+
2038
+
2039
+ def _trim_trailing_blank_rows(sheet) -> bool: # type: ignore[no-untyped-def]
2040
+ removed = 0
2041
+ while sheet.max_row > 1:
2042
+ values = [cell.value for cell in sheet[sheet.max_row]]
2043
+ if any(value not in (None, "") for value in values):
2044
+ break
2045
+ sheet.delete_rows(sheet.max_row, 1)
2046
+ removed += 1
2047
+ return removed > 0
2048
+
2049
+
2050
+ def _normalize_enum_values(sheet, expected_columns: list[JSONObject]) -> bool: # type: ignore[no-untyped-def]
2051
+ changed = False
2052
+ header_map = _sheet_header_map(sheet)
2053
+ for column in expected_columns:
2054
+ options = [str(item).strip() for item in column.get("options", []) if str(item).strip()]
2055
+ if not options:
2056
+ continue
2057
+ column_index = header_map.get(_normalize_header_key(column["title"]))
2058
+ if column_index is None:
2059
+ continue
2060
+ option_map = {_normalize_header_key(item): item for item in options}
2061
+ for row in range(2, sheet.max_row + 1):
2062
+ cell = sheet.cell(row=row, column=column_index)
2063
+ text = _normalize_optional_text(cell.value)
2064
+ if text is None:
2065
+ continue
2066
+ canonical = option_map.get(_normalize_header_key(text))
2067
+ if canonical and canonical != text:
2068
+ cell.value = canonical
2069
+ changed = True
2070
+ return changed
2071
+
2072
+
2073
+ def _normalize_date_formats(sheet) -> bool: # type: ignore[no-untyped-def]
2074
+ changed = False
2075
+ for row in sheet.iter_rows(min_row=2):
2076
+ for cell in row:
2077
+ if getattr(cell, "is_date", False):
2078
+ if cell.number_format != "yyyy-mm-dd hh:mm:ss":
2079
+ cell.number_format = "yyyy-mm-dd hh:mm:ss"
2080
+ changed = True
2081
+ return changed
2082
+
2083
+
2084
+ def _normalize_number_formats(sheet) -> bool: # type: ignore[no-untyped-def]
2085
+ changed = False
2086
+ for row in sheet.iter_rows(min_row=2):
2087
+ for cell in row:
2088
+ if isinstance(cell.value, (int, float)) and not getattr(cell, "is_date", False):
2089
+ if cell.number_format == "General":
2090
+ cell.number_format = "0.00" if isinstance(cell.value, float) else "0"
2091
+ changed = True
2092
+ return changed
2093
+
2094
+
2095
+ def _normalize_url_cells(sheet) -> bool: # type: ignore[no-untyped-def]
2096
+ changed = False
2097
+ for row in sheet.iter_rows(min_row=2):
2098
+ for cell in row:
2099
+ text = _normalize_optional_text(cell.value)
2100
+ if text and (text.startswith("http://") or text.startswith("https://")) and text != cell.value:
2101
+ cell.value = text
2102
+ changed = True
2103
+ return changed
2104
+
2105
+
2106
+ def _extract_import_records(payload: Any) -> list[JSONObject]:
2107
+ if isinstance(payload, dict):
2108
+ for key in ("list", "records", "items"):
2109
+ value = payload.get(key)
2110
+ if isinstance(value, list):
2111
+ return [item for item in value if isinstance(item, dict)]
2112
+ if isinstance(payload, list):
2113
+ return [item for item in payload if isinstance(item, dict)]
2114
+ return []
2115
+
2116
+
2117
+ def _match_import_record(
2118
+ records: list[JSONObject],
2119
+ *,
2120
+ local_job: dict[str, Any] | None,
2121
+ process_id_str: str | None,
2122
+ ) -> tuple[JSONObject | None, str | None]:
2123
+ if process_id_str:
2124
+ exact = [
2125
+ item
2126
+ for item in records
2127
+ if _normalize_optional_text(item.get("processIdStr") or item.get("processId") or item.get("process_id_str")) == process_id_str
2128
+ ]
2129
+ if len(exact) == 1:
2130
+ return exact[0], "process_id_str"
2131
+ if len(exact) > 1:
2132
+ return None, "process_id_str"
2133
+ if isinstance(local_job, dict):
2134
+ source_file_name = _normalize_optional_text(local_job.get("source_file_name"))
2135
+ started_at = _parse_utc(local_job.get("started_at"))
2136
+ candidates = records
2137
+ if source_file_name:
2138
+ candidates = [
2139
+ item
2140
+ for item in candidates
2141
+ if _normalize_optional_text(item.get("sourceFileName") or item.get("source_file_name")) == source_file_name
2142
+ ]
2143
+ if started_at is not None:
2144
+ window_end = started_at + timedelta(minutes=10)
2145
+ timed = []
2146
+ for item in candidates:
2147
+ operate_time = _parse_utc(item.get("operateTime"))
2148
+ if operate_time is None:
2149
+ continue
2150
+ if started_at - timedelta(minutes=1) <= operate_time <= window_end:
2151
+ timed.append(item)
2152
+ if len(timed) == 1:
2153
+ return timed[0], "local_job_window"
2154
+ if len(timed) > 1:
2155
+ return None, "local_job_window"
2156
+ if len(candidates) == 1:
2157
+ return candidates[0], "source_file_name"
2158
+ if len(candidates) > 1:
2159
+ return None, "source_file_name"
2160
+ return None, None
2161
+
2162
+
2163
+ def _parse_utc(value: Any) -> datetime | None:
2164
+ text = _normalize_optional_text(value)
2165
+ if text is None:
2166
+ return None
2167
+ normalized = text.replace("Z", "+00:00")
2168
+ try:
2169
+ parsed = datetime.fromisoformat(normalized)
2170
+ except ValueError:
2171
+ return None
2172
+ if parsed.tzinfo is None:
2173
+ return parsed.replace(tzinfo=timezone.utc)
2174
+ return parsed.astimezone(timezone.utc)
2175
+
2176
+
2177
+ def _coerce_int(value: Any) -> int | None:
2178
+ if value is None or value == "":
2179
+ return None
2180
+ try:
2181
+ return int(value)
2182
+ except (TypeError, ValueError):
2183
+ return None
2184
+
2185
+
2186
+ def _normalize_error_file_urls(value: Any) -> list[str]:
2187
+ if isinstance(value, list):
2188
+ return [str(item).strip() for item in value if str(item).strip()]
2189
+ return []