@qingflow-tech/qingflow-cli 1.1.50-beta.2 → 1.1.50-beta.4
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.
- package/README.md +3 -3
- package/package.json +1 -1
- package/pyproject.toml +7 -2
- package/skills/qingflow-cli/SKILL.md +74 -0
- package/skills/qingflow-cli/reference/QINGFLOW_CLI_BUILDER_VIEWS_WORKFLOW.md +3 -9
- package/skills/qingflow-cli/reference/builder/20-build-complete-system.md +1 -0
- package/skills/qingflow-cli/reference/builder/50-views.md +180 -191
- package/skills/qingflow-cli/reference/builder/README.md +1 -1
- package/skills/qingflow-cli/reference/builder/reference/app-delivery-sop.md +3 -3
- package/skills/qingflow-cli/reference/examples/README.md +1 -1
- package/skills/qingflow-cli/reference/examples/views/views_apply_v1alpha1.example.json +14 -0
- package/skills/qingflow-cli/reference/examples/views/views_upsert_table_minimal.example.json +30 -36
- package/src/qingflow_mcp/builder_facade/models.py +227 -17
- package/src/qingflow_mcp/builder_facade/schemas/views/manifest.json +42 -0
- package/src/qingflow_mcp/builder_facade/schemas/views/v1alpha1/complete.json +1245 -0
- package/src/qingflow_mcp/builder_facade/schemas/views/v1alpha1/details/button-config.json +53 -0
- package/src/qingflow_mcp/builder_facade/schemas/views/v1alpha1/details/data-permission.json +60 -0
- package/src/qingflow_mcp/builder_facade/schemas/views/v1alpha1/details/display.json +73 -0
- package/src/qingflow_mcp/builder_facade/schemas/views/v1alpha1/details/operation-permission.json +48 -0
- package/src/qingflow_mcp/builder_facade/schemas/views/v1alpha1/details/query-condition.json +47 -0
- package/src/qingflow_mcp/builder_facade/schemas/views/v1alpha1/summary.json +319 -0
- package/src/qingflow_mcp/builder_facade/service.py +1460 -410
- package/src/qingflow_mcp/builder_facade/views_extract.py +592 -0
- package/src/qingflow_mcp/builder_facade/views_schema.py +400 -0
- package/src/qingflow_mcp/cli/commands/builder.py +72 -53
- package/src/qingflow_mcp/public_surface.py +3 -0
- package/src/qingflow_mcp/response_trim.py +11 -2
- package/src/qingflow_mcp/server_app_builder.py +32 -8
- package/src/qingflow_mcp/solution/compiler/view_compiler.py +2 -1
- package/src/qingflow_mcp/solution/executor.py +96 -8
- package/src/qingflow_mcp/solution/spec_models.py +1 -0
- package/src/qingflow_mcp/tools/ai_builder_tools.py +211 -393
- package/src/qingflow_mcp/tools/solution_tools.py +2 -0
- package/src/qingflow_mcp/tools/view_tools.py +0 -6
- package/src/qingflow_mcp/tools/view_writer.py +44 -0
- package/skills/qingflow-cli/reference/examples/views/views_batch_full.example.json +0 -159
- package/skills/qingflow-cli/reference/views_upsert_table_minimal.example.json +0 -42
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
from copy import deepcopy
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
from jsonschema import Draft202012Validator, FormatChecker
|
|
11
|
+
from referencing import Registry, Resource
|
|
12
|
+
|
|
13
|
+
from .app_form_schema import compact_schema_errors, schema_error_message
|
|
14
|
+
|
|
15
|
+
JSONObject = dict[str, Any]
|
|
16
|
+
VIEWS_SCHEMA_ROOT = Path(__file__).with_name("schemas") / "views"
|
|
17
|
+
VIEWS_SUPPORTED_SCHEMA_KINDS = (
|
|
18
|
+
"summary",
|
|
19
|
+
"complete",
|
|
20
|
+
"display",
|
|
21
|
+
"data-permission",
|
|
22
|
+
"operation-permission",
|
|
23
|
+
"query-condition",
|
|
24
|
+
"button-config",
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class ViewsContractError(ValueError):
|
|
29
|
+
"""Raised when a views declaration violates the versioned schema contract."""
|
|
30
|
+
|
|
31
|
+
def __init__(self, error_code: str, message: str, *, path: str | None = None, details: JSONObject | None = None) -> None:
|
|
32
|
+
super().__init__(message)
|
|
33
|
+
self.error_code = error_code
|
|
34
|
+
self.message = message
|
|
35
|
+
self.path = path
|
|
36
|
+
self.details = details or {}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def compact_none_values(value: Any) -> Any:
|
|
40
|
+
"""Recursively drop None values so pydantic dumps match the JSON Schema contract."""
|
|
41
|
+
if isinstance(value, dict):
|
|
42
|
+
return {key: compact_none_values(item) for key, item in value.items() if item is not None}
|
|
43
|
+
if isinstance(value, list):
|
|
44
|
+
return [compact_none_values(item) for item in value]
|
|
45
|
+
return value
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@dataclass(frozen=True, slots=True)
|
|
49
|
+
class ViewsSchemaContext:
|
|
50
|
+
"""Resolved schema context for one views schema version."""
|
|
51
|
+
|
|
52
|
+
schema_version: str
|
|
53
|
+
api_version: str
|
|
54
|
+
schemas: dict[str, JSONObject]
|
|
55
|
+
schema_files: dict[str, str]
|
|
56
|
+
ref_registry: Registry
|
|
57
|
+
supported_schema_kinds: tuple[str, ...]
|
|
58
|
+
contract_source: str
|
|
59
|
+
contract_version: str
|
|
60
|
+
|
|
61
|
+
@property
|
|
62
|
+
def summary_schema(self) -> JSONObject:
|
|
63
|
+
return self.schemas["summary"]
|
|
64
|
+
|
|
65
|
+
@property
|
|
66
|
+
def complete_schema(self) -> JSONObject:
|
|
67
|
+
return self.schemas["complete"]
|
|
68
|
+
|
|
69
|
+
def schema_for_kind(self, schema_kind: str) -> JSONObject:
|
|
70
|
+
requested = str(schema_kind or "").strip()
|
|
71
|
+
schema = self.schemas.get(requested)
|
|
72
|
+
if schema is not None:
|
|
73
|
+
return schema
|
|
74
|
+
raise ViewsContractError(
|
|
75
|
+
"UNSUPPORTED_SCHEMA_KIND",
|
|
76
|
+
f"unsupported views schema kind: {requested}",
|
|
77
|
+
details={"supportedKinds": list(self.supported_schema_kinds)},
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
def _validate(self, payload: JSONObject, *, schema_kind: str) -> None:
|
|
81
|
+
schema = self.schema_for_kind(schema_kind)
|
|
82
|
+
validator = Draft202012Validator(schema, format_checker=FormatChecker(), registry=self.ref_registry)
|
|
83
|
+
errors = sorted(validator.iter_errors(payload), key=lambda item: (list(item.absolute_path), item.message))
|
|
84
|
+
if not errors:
|
|
85
|
+
return
|
|
86
|
+
compact_errors = compact_schema_errors(errors, schema)
|
|
87
|
+
error = compact_errors[0] if compact_errors else errors[0]
|
|
88
|
+
pointer = "".join(f"/{str(part).replace('~', '~0').replace('/', '~1')}" for part in error.absolute_path) or "/"
|
|
89
|
+
raise ViewsContractError(
|
|
90
|
+
"INVALID_SPEC",
|
|
91
|
+
schema_error_message(error),
|
|
92
|
+
path=pointer,
|
|
93
|
+
details={
|
|
94
|
+
"validator": error.validator,
|
|
95
|
+
"schemaPath": "/".join(str(part) for part in error.absolute_schema_path),
|
|
96
|
+
"schemaKind": schema_kind,
|
|
97
|
+
},
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
def validate_upsert(self, payload: JSONObject, *, schema_kind: str = "complete") -> None:
|
|
101
|
+
"""Validate a single (alias-normalized) view declaration."""
|
|
102
|
+
self._validate(payload, schema_kind=schema_kind)
|
|
103
|
+
|
|
104
|
+
def validate_declaration(self, payload: JSONObject, *, schema_kind: str = "complete") -> None:
|
|
105
|
+
"""Validate one view declaration used by ``app_views_apply.upsert_view``."""
|
|
106
|
+
if not isinstance(payload, dict):
|
|
107
|
+
raise ViewsContractError("INVALID_SPEC", "views declaration must be a JSON object", path="/")
|
|
108
|
+
batch_keys = ("views", "upsert_views", "upsertViews", "apps")
|
|
109
|
+
if any(key in payload for key in batch_keys):
|
|
110
|
+
raise ViewsContractError(
|
|
111
|
+
"VIEW_BATCH_CREATE_UNSUPPORTED",
|
|
112
|
+
"views schema validates one view declaration; batch envelopes are unsupported",
|
|
113
|
+
path="/",
|
|
114
|
+
details={"unsupportedKeys": [key for key in batch_keys if key in payload], "schemaKind": schema_kind},
|
|
115
|
+
)
|
|
116
|
+
self._validate(payload, schema_kind=schema_kind)
|
|
117
|
+
|
|
118
|
+
class ViewsSchemaRegistry:
|
|
119
|
+
"""Registry for versioned views declaration schemas (mirrors AppFormSchemaRegistry)."""
|
|
120
|
+
|
|
121
|
+
def __init__(self, root: Path = VIEWS_SCHEMA_ROOT, *, enabled_versions: tuple[str, ...] | None = None) -> None:
|
|
122
|
+
self.root = root
|
|
123
|
+
self.manifest = self._read_json(root / "manifest.json")
|
|
124
|
+
versions = self.manifest.get("versions")
|
|
125
|
+
if not isinstance(versions, dict) or not versions:
|
|
126
|
+
raise RuntimeError("views schema manifest must define versions")
|
|
127
|
+
env_versions = self._configured_versions()
|
|
128
|
+
defaults = tuple(
|
|
129
|
+
str(version)
|
|
130
|
+
for version, metadata in versions.items()
|
|
131
|
+
if isinstance(metadata, dict) and bool(metadata.get("enabledByDefault"))
|
|
132
|
+
)
|
|
133
|
+
configured = tuple(enabled_versions) if enabled_versions is not None else (env_versions or defaults)
|
|
134
|
+
self.stored_versions = tuple(str(version) for version in versions)
|
|
135
|
+
self.latest_version = str(self.manifest.get("latestVersion") or "")
|
|
136
|
+
self._stored_api_to_version: dict[str, str] = {}
|
|
137
|
+
for version, metadata in versions.items():
|
|
138
|
+
if not isinstance(metadata, dict):
|
|
139
|
+
raise RuntimeError(f"views schema metadata must be an object: {version}")
|
|
140
|
+
api_version = str(metadata.get("apiVersion") or "")
|
|
141
|
+
if not api_version:
|
|
142
|
+
raise RuntimeError(f"views schema version has no apiVersion: {version}")
|
|
143
|
+
if api_version in self._stored_api_to_version:
|
|
144
|
+
raise RuntimeError(f"duplicate views apiVersion: {api_version}")
|
|
145
|
+
self._stored_api_to_version[api_version] = str(version)
|
|
146
|
+
self.enabled_versions = configured
|
|
147
|
+
if self.latest_version not in versions:
|
|
148
|
+
raise RuntimeError("views latestVersion is not stored")
|
|
149
|
+
if self.latest_version not in self.enabled_versions:
|
|
150
|
+
raise RuntimeError("views latestVersion must remain enabled")
|
|
151
|
+
self._contexts: dict[str, ViewsSchemaContext] = {}
|
|
152
|
+
self._api_to_version: dict[str, str] = {}
|
|
153
|
+
for version in self.enabled_versions:
|
|
154
|
+
metadata = versions.get(version)
|
|
155
|
+
if not isinstance(metadata, dict):
|
|
156
|
+
raise RuntimeError(f"enabled views schema version is not stored: {version}")
|
|
157
|
+
api_version = str(metadata.get("apiVersion") or "")
|
|
158
|
+
schema_files = metadata.get("schemaFiles")
|
|
159
|
+
if not isinstance(schema_files, dict):
|
|
160
|
+
schema_files = {kind: f"{version}/{kind}.json" for kind in ("summary", "complete")}
|
|
161
|
+
supported_schema_kinds = tuple(str(item) for item in metadata.get("schemaKinds") or schema_files)
|
|
162
|
+
if not {"summary", "complete"}.issubset(supported_schema_kinds):
|
|
163
|
+
raise RuntimeError(f"views schema version must support summary and complete kinds: {version}")
|
|
164
|
+
schemas: dict[str, JSONObject] = {}
|
|
165
|
+
paths: dict[str, Path] = {}
|
|
166
|
+
for name in supported_schema_kinds:
|
|
167
|
+
relative_path = schema_files.get(name)
|
|
168
|
+
if not isinstance(relative_path, str) or not relative_path:
|
|
169
|
+
raise RuntimeError(f"views schema kind has no schema file: {version}/{name}")
|
|
170
|
+
path = root / relative_path
|
|
171
|
+
schema = self._read_json(path)
|
|
172
|
+
schemas[name] = schema
|
|
173
|
+
paths[name] = path
|
|
174
|
+
ref_registry = Registry()
|
|
175
|
+
for name, schema in schemas.items():
|
|
176
|
+
schema_id = str(schema.get("$id") or "")
|
|
177
|
+
if version not in schema_id:
|
|
178
|
+
raise RuntimeError(f"views {name} schema $id mismatch for {version}: {schema_id}")
|
|
179
|
+
Draft202012Validator.check_schema(schema)
|
|
180
|
+
resource = Resource.from_contents(schema)
|
|
181
|
+
if schema_id:
|
|
182
|
+
ref_registry = ref_registry.with_resource(schema_id, resource)
|
|
183
|
+
ref_registry = ref_registry.with_resource(paths[name].resolve().as_uri(), resource)
|
|
184
|
+
for name, schema in schemas.items():
|
|
185
|
+
self._validate_local_refs(schema, paths[name], schemas, paths)
|
|
186
|
+
context = ViewsSchemaContext(
|
|
187
|
+
version,
|
|
188
|
+
api_version,
|
|
189
|
+
schemas,
|
|
190
|
+
{name: str(schema_files[name]) for name in supported_schema_kinds},
|
|
191
|
+
ref_registry,
|
|
192
|
+
supported_schema_kinds,
|
|
193
|
+
str(metadata.get("contractSource") or "app_views_apply"),
|
|
194
|
+
str(metadata.get("contractVersion") or version),
|
|
195
|
+
)
|
|
196
|
+
self._contexts[version] = context
|
|
197
|
+
self._api_to_version[api_version] = version
|
|
198
|
+
|
|
199
|
+
def schema(
|
|
200
|
+
self,
|
|
201
|
+
*,
|
|
202
|
+
schema_version: str | None = None,
|
|
203
|
+
schema_kind: str | None = None,
|
|
204
|
+
) -> JSONObject:
|
|
205
|
+
context = self.resolve_schema_version(schema_version)
|
|
206
|
+
requested_kind = str(schema_kind or "summary").strip()
|
|
207
|
+
if requested_kind not in context.supported_schema_kinds:
|
|
208
|
+
raise ViewsContractError(
|
|
209
|
+
"UNSUPPORTED_SCHEMA_KIND",
|
|
210
|
+
f"unsupported views schema kind: {requested_kind}",
|
|
211
|
+
details={"supportedKinds": list(context.supported_schema_kinds)},
|
|
212
|
+
)
|
|
213
|
+
schema, dependencies = self._bundle_schema(
|
|
214
|
+
context.schema_for_kind(requested_kind),
|
|
215
|
+
schema_path=(self.root / context.schema_files[requested_kind]),
|
|
216
|
+
schemas=context.schemas,
|
|
217
|
+
schema_files=context.schema_files,
|
|
218
|
+
)
|
|
219
|
+
return {
|
|
220
|
+
"schemaVersion": context.schema_version,
|
|
221
|
+
"apiVersion": context.api_version,
|
|
222
|
+
"latestVersion": self.latest_version,
|
|
223
|
+
"storedVersions": list(self.stored_versions),
|
|
224
|
+
"enabledVersions": list(self.enabled_versions),
|
|
225
|
+
"supportedSchemaKinds": list(context.supported_schema_kinds),
|
|
226
|
+
"schemaKind": requested_kind,
|
|
227
|
+
"schemaFile": context.schema_files[requested_kind],
|
|
228
|
+
"schemaBundled": bool(dependencies),
|
|
229
|
+
"schemaDependencies": dependencies,
|
|
230
|
+
"schema": schema,
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
@classmethod
|
|
234
|
+
def _bundle_schema(
|
|
235
|
+
cls,
|
|
236
|
+
schema: JSONObject,
|
|
237
|
+
*,
|
|
238
|
+
schema_path: Path,
|
|
239
|
+
schemas: dict[str, JSONObject],
|
|
240
|
+
schema_files: dict[str, str],
|
|
241
|
+
) -> tuple[JSONObject, list[str]]:
|
|
242
|
+
"""Inline reachable cross-file definitions for standalone consumers."""
|
|
243
|
+
bundled = deepcopy(schema)
|
|
244
|
+
definitions = bundled.get("$defs")
|
|
245
|
+
if not isinstance(definitions, dict):
|
|
246
|
+
definitions = {}
|
|
247
|
+
dependencies: set[str] = set()
|
|
248
|
+
# schema_path is <root>/<version>/details/<file> or <root>/<version>/<file>.
|
|
249
|
+
# Find the manifest root from the configured path instead of relying on
|
|
250
|
+
# a fixed details depth.
|
|
251
|
+
cls_root = schema_path.parent
|
|
252
|
+
for candidate in schema_path.parents:
|
|
253
|
+
if (candidate / "manifest.json").is_file():
|
|
254
|
+
cls_root = candidate
|
|
255
|
+
break
|
|
256
|
+
paths = {name: cls_root / relative for name, relative in schema_files.items()}
|
|
257
|
+
|
|
258
|
+
def resolve_pointer(document: JSONObject, fragment: str, *, ref: str) -> Any:
|
|
259
|
+
target: Any = document
|
|
260
|
+
for raw_part in fragment[1:].split("/") if fragment.startswith("/") else ():
|
|
261
|
+
part = raw_part.replace("~1", "/").replace("~0", "~")
|
|
262
|
+
if not isinstance(target, dict) or part not in target:
|
|
263
|
+
raise RuntimeError(f"views schema contains a dangling $ref in {schema_path}: {ref}")
|
|
264
|
+
target = target[part]
|
|
265
|
+
return target
|
|
266
|
+
|
|
267
|
+
pending: list[tuple[str, JSONObject, Path]] = []
|
|
268
|
+
|
|
269
|
+
def add_definition(name: str, source: JSONObject, source_path: Path) -> None:
|
|
270
|
+
if name in definitions:
|
|
271
|
+
return
|
|
272
|
+
source_definitions = source.get("$defs")
|
|
273
|
+
if not isinstance(source_definitions, dict) or name not in source_definitions:
|
|
274
|
+
raise RuntimeError(f"views schema references an unknown definition: {name}")
|
|
275
|
+
definitions[name] = deepcopy(source_definitions[name])
|
|
276
|
+
pending.append((name, source, source_path))
|
|
277
|
+
|
|
278
|
+
def rewrite(value: Any, source: JSONObject, source_path: Path) -> None:
|
|
279
|
+
if isinstance(value, dict):
|
|
280
|
+
ref = value.get("$ref")
|
|
281
|
+
if isinstance(ref, str):
|
|
282
|
+
ref_file, _, fragment = ref.partition("#")
|
|
283
|
+
target_source = source
|
|
284
|
+
target_path = source_path
|
|
285
|
+
if ref_file:
|
|
286
|
+
target_path = (source_path.parent / ref_file).resolve()
|
|
287
|
+
target_name = next(
|
|
288
|
+
(name for name, path in paths.items() if path.resolve() == target_path),
|
|
289
|
+
None,
|
|
290
|
+
)
|
|
291
|
+
if target_name is None:
|
|
292
|
+
raise RuntimeError(f"views schema contains an external $ref in {source_path}: {ref}")
|
|
293
|
+
target_source = schemas[target_name]
|
|
294
|
+
dependencies.add(schema_files[target_name])
|
|
295
|
+
value["$ref"] = f"#{fragment}"
|
|
296
|
+
local_ref = str(value["$ref"])
|
|
297
|
+
if local_ref.startswith("#/$defs/"):
|
|
298
|
+
name = local_ref.removeprefix("#/$defs/").split("/", 1)[0]
|
|
299
|
+
add_definition(name, target_source, target_path)
|
|
300
|
+
else:
|
|
301
|
+
resolve_pointer(target_source, fragment, ref=ref)
|
|
302
|
+
for child in value.values():
|
|
303
|
+
rewrite(child, source, source_path)
|
|
304
|
+
elif isinstance(value, list):
|
|
305
|
+
for child in value:
|
|
306
|
+
rewrite(child, source, source_path)
|
|
307
|
+
|
|
308
|
+
for key, value in bundled.items():
|
|
309
|
+
if key != "$defs":
|
|
310
|
+
rewrite(value, schema, schema_path)
|
|
311
|
+
while pending:
|
|
312
|
+
name, source, source_path = pending.pop()
|
|
313
|
+
rewrite(definitions[name], source, source_path)
|
|
314
|
+
if definitions:
|
|
315
|
+
bundled["$defs"] = definitions
|
|
316
|
+
return bundled, sorted(dependencies)
|
|
317
|
+
|
|
318
|
+
def resolve_schema_version(self, schema_version: str | None) -> ViewsSchemaContext:
|
|
319
|
+
version = str(schema_version or self.latest_version).strip()
|
|
320
|
+
if version not in self.stored_versions:
|
|
321
|
+
raise self._version_error("UNSUPPORTED_SCHEMA_VERSION", version)
|
|
322
|
+
if version not in self.enabled_versions:
|
|
323
|
+
raise self._version_error("SCHEMA_VERSION_DISABLED", version)
|
|
324
|
+
return self._contexts[version]
|
|
325
|
+
|
|
326
|
+
def resolve_api_version(self, api_version: str) -> ViewsSchemaContext:
|
|
327
|
+
version = self._api_to_version.get(api_version)
|
|
328
|
+
if version is not None:
|
|
329
|
+
return self._contexts[version]
|
|
330
|
+
stored = self._stored_api_to_version.get(api_version)
|
|
331
|
+
if stored is not None:
|
|
332
|
+
raise self._version_error("SCHEMA_VERSION_DISABLED", stored)
|
|
333
|
+
raise self._version_error("UNSUPPORTED_SCHEMA_VERSION", api_version)
|
|
334
|
+
|
|
335
|
+
def _version_error(self, error_code: str, requested: str) -> ViewsContractError:
|
|
336
|
+
return ViewsContractError(
|
|
337
|
+
error_code,
|
|
338
|
+
f"unsupported views schema selection: {requested}",
|
|
339
|
+
details={
|
|
340
|
+
"requestedVersion": requested,
|
|
341
|
+
"latestVersion": self.latest_version,
|
|
342
|
+
"storedVersions": list(self.stored_versions),
|
|
343
|
+
"enabledVersions": list(self.enabled_versions),
|
|
344
|
+
},
|
|
345
|
+
)
|
|
346
|
+
|
|
347
|
+
@staticmethod
|
|
348
|
+
def _read_json(path: Path) -> JSONObject:
|
|
349
|
+
value = json.loads(path.read_text(encoding="utf-8"))
|
|
350
|
+
if not isinstance(value, dict):
|
|
351
|
+
raise RuntimeError(f"views schema artifact must be an object: {path}")
|
|
352
|
+
return value
|
|
353
|
+
|
|
354
|
+
@staticmethod
|
|
355
|
+
def _validate_local_refs(
|
|
356
|
+
document: JSONObject,
|
|
357
|
+
path: Path,
|
|
358
|
+
documents: dict[str, JSONObject],
|
|
359
|
+
paths: dict[str, Path],
|
|
360
|
+
) -> None:
|
|
361
|
+
stack: list[Any] = [document]
|
|
362
|
+
while stack:
|
|
363
|
+
value = stack.pop()
|
|
364
|
+
if isinstance(value, dict):
|
|
365
|
+
ref = value.get("$ref")
|
|
366
|
+
if ref is not None:
|
|
367
|
+
if not isinstance(ref, str):
|
|
368
|
+
raise RuntimeError(f"views schema contains an invalid $ref in {path}: {ref}")
|
|
369
|
+
ref_file, _, fragment = ref.partition("#")
|
|
370
|
+
target_document = document
|
|
371
|
+
if ref_file:
|
|
372
|
+
target_path = (path.parent / ref_file).resolve()
|
|
373
|
+
if target_path not in {item.resolve() for item in paths.values()}:
|
|
374
|
+
raise RuntimeError(f"views schema contains an external $ref in {path}: {ref}")
|
|
375
|
+
target_name = next(name for name, item in paths.items() if item.resolve() == target_path)
|
|
376
|
+
target_document = documents[target_name]
|
|
377
|
+
target: Any = target_document
|
|
378
|
+
for raw_part in fragment[1:].split("/") if fragment.startswith("/") else ():
|
|
379
|
+
part = raw_part.replace("~1", "/").replace("~0", "~")
|
|
380
|
+
if not isinstance(target, dict) or part not in target:
|
|
381
|
+
raise RuntimeError(f"views schema contains a dangling $ref in {path}: {ref}")
|
|
382
|
+
target = target[part]
|
|
383
|
+
stack.extend(value.values())
|
|
384
|
+
elif isinstance(value, list):
|
|
385
|
+
stack.extend(value)
|
|
386
|
+
|
|
387
|
+
@staticmethod
|
|
388
|
+
def _configured_versions() -> tuple[str, ...]:
|
|
389
|
+
raw = os.environ.get("QINGFLOW_VIEWS_SCHEMA_VERSIONS", "")
|
|
390
|
+
return tuple(part.strip() for part in raw.split(",") if part.strip())
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
_REGISTRY: ViewsSchemaRegistry | None = None
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
def views_schema_registry() -> ViewsSchemaRegistry:
|
|
397
|
+
global _REGISTRY
|
|
398
|
+
if _REGISTRY is None:
|
|
399
|
+
_REGISTRY = ViewsSchemaRegistry()
|
|
400
|
+
return _REGISTRY
|
|
@@ -4,24 +4,30 @@ import argparse
|
|
|
4
4
|
from copy import deepcopy
|
|
5
5
|
from pathlib import Path
|
|
6
6
|
|
|
7
|
-
from ..context import CliContext
|
|
8
7
|
from ...app_form_validator import (
|
|
9
8
|
AppFormValidationError,
|
|
10
9
|
build_encoded_id_map,
|
|
11
|
-
referenced_formula_field_ids,
|
|
12
|
-
translate_formula_test_case_values,
|
|
13
10
|
encode_code_block_field_ids,
|
|
14
11
|
referenced_code_block_field_ids,
|
|
12
|
+
referenced_formula_field_ids,
|
|
15
13
|
restore_decimal_ids_in_text,
|
|
16
14
|
strip_placeholder_quotes,
|
|
15
|
+
translate_formula_test_case_values,
|
|
17
16
|
translate_select_test_case_values,
|
|
18
17
|
validate_code_block_static,
|
|
19
18
|
validate_declaration,
|
|
20
19
|
validate_formula_expression,
|
|
21
20
|
)
|
|
21
|
+
from ...builder_facade.views_schema import VIEWS_SUPPORTED_SCHEMA_KINDS
|
|
22
|
+
from ..context import CliContext
|
|
22
23
|
from ..json_io import load_json_value
|
|
23
|
-
from .common import
|
|
24
|
-
|
|
24
|
+
from .common import (
|
|
25
|
+
load_list_arg,
|
|
26
|
+
load_object_arg,
|
|
27
|
+
parse_bool_text,
|
|
28
|
+
raise_config_error,
|
|
29
|
+
require_list_arg,
|
|
30
|
+
)
|
|
25
31
|
|
|
26
32
|
def register(subparsers: argparse._SubParsersAction[argparse.ArgumentParser]) -> None:
|
|
27
33
|
parser = subparsers.add_parser("builder", aliases=["build"], help="稳定 builder 命令")
|
|
@@ -282,14 +288,32 @@ def register(subparsers: argparse._SubParsersAction[argparse.ArgumentParser]) ->
|
|
|
282
288
|
|
|
283
289
|
views_apply = builder_subparsers.add_parser("views", help="视图")
|
|
284
290
|
views_apply_subparsers = views_apply.add_subparsers(dest="builder_views_command", required=True)
|
|
291
|
+
views_schema = views_apply_subparsers.add_parser("schema", help="读取视图声明 Schema")
|
|
292
|
+
views_schema.add_argument("--schema-version")
|
|
293
|
+
views_schema.add_argument(
|
|
294
|
+
"--schema-kind",
|
|
295
|
+
choices=VIEWS_SUPPORTED_SCHEMA_KINDS,
|
|
296
|
+
)
|
|
297
|
+
views_schema.set_defaults(handler=_handle_views_schema, format_hint="builder_summary", force_json_output=True)
|
|
298
|
+
|
|
299
|
+
views_get = views_apply_subparsers.add_parser("get", help="读取 canonical 视图声明")
|
|
300
|
+
views_get.add_argument("--app-key", required=True)
|
|
301
|
+
views_get.add_argument("--schema-version")
|
|
302
|
+
views_get.add_argument("--schema-kind", choices=("summary", "complete"), help="summary=投影字段;complete=逐视图读 viewConfig 反向编译(含覆盖度 gaps)")
|
|
303
|
+
views_get.set_defaults(handler=_handle_views_get, format_hint="builder_summary", force_json_output=True)
|
|
304
|
+
|
|
305
|
+
views_validate = views_apply_subparsers.add_parser("validate", help="只校验视图声明,不访问后端")
|
|
306
|
+
views_validate.add_argument("--file", required=True, help="视图声明 JSON 文件,- 表示 stdin")
|
|
307
|
+
views_validate.add_argument("--schema-version", help="固定使用的视图 Schema 版本;缺省读文件内 schema_version,再缺省 latest")
|
|
308
|
+
views_validate.set_defaults(handler=_handle_views_validate, format_hint="builder_summary", force_json_output=True)
|
|
309
|
+
|
|
285
310
|
views_apply_apply = views_apply_subparsers.add_parser("apply", help="执行视图变更")
|
|
286
|
-
views_apply_apply.add_argument("--app-key",
|
|
287
|
-
views_apply_apply.add_argument("--views-file", help='推荐批量视图 JSON 对象:{"views":[...]};每项包含 app_key、operation、视图配置')
|
|
288
|
-
views_apply_apply.add_argument("--apps-file", help="多应用视图 JSON 数组;每项包含 app_key 和 upsert/patch/remove payload")
|
|
311
|
+
views_apply_apply.add_argument("--app-key", required=True)
|
|
289
312
|
views_apply_apply.add_argument("--publish", action=argparse.BooleanOptionalAction, default=True)
|
|
290
|
-
views_apply_apply.add_argument("--
|
|
313
|
+
views_apply_apply.add_argument("--view-file", help="单条新建或完整更新视图的 JSON 对象")
|
|
291
314
|
views_apply_apply.add_argument("--patch-views-file")
|
|
292
315
|
views_apply_apply.add_argument("--remove-views-file")
|
|
316
|
+
views_apply_apply.add_argument("--schema-version", help="声明使用的 Schema 版本;缺省 latest,信封回显 schemaVersion")
|
|
293
317
|
views_apply_apply.set_defaults(handler=_handle_views_apply, format_hint="builder_summary", force_json_output=True)
|
|
294
318
|
|
|
295
319
|
flow_apply = builder_subparsers.add_parser("flow", help="流程")
|
|
@@ -1187,30 +1211,6 @@ def _coerce_apps_file_bool(value: object, *, field_name: str, default: bool) ->
|
|
|
1187
1211
|
)
|
|
1188
1212
|
|
|
1189
1213
|
|
|
1190
|
-
def _load_views_file_arg(path: str | None) -> list[object] | None:
|
|
1191
|
-
if not path:
|
|
1192
|
-
return None
|
|
1193
|
-
payload = load_json_value(path, option_name="--views-file")
|
|
1194
|
-
if isinstance(payload, dict):
|
|
1195
|
-
views = payload.get("views")
|
|
1196
|
-
if not isinstance(views, list):
|
|
1197
|
-
raise_config_error(
|
|
1198
|
-
"--views-file JSON object must contain a views array.",
|
|
1199
|
-
fix_hint='Use {"views": [{"operation": "upsert", "app_key": "...", "name": "...", "type": "table", "columns": [...]}]}.',
|
|
1200
|
-
error_code="VIEWS_FILE_SHAPE_INVALID",
|
|
1201
|
-
details={"expected": '{"views": [...]}', "actual_type": type(views).__name__},
|
|
1202
|
-
)
|
|
1203
|
-
return views
|
|
1204
|
-
if isinstance(payload, list):
|
|
1205
|
-
return payload
|
|
1206
|
-
raise_config_error(
|
|
1207
|
-
"--views-file must be a JSON object with views[] or a JSON array.",
|
|
1208
|
-
fix_hint='Prefer {"views": [...]} for new automation. A bare array is accepted only for compatibility.',
|
|
1209
|
-
error_code="VIEWS_FILE_SHAPE_INVALID",
|
|
1210
|
-
details={"actual_type": type(payload).__name__},
|
|
1211
|
-
)
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
1214
|
def _handle_layout_apply(args: argparse.Namespace, context: CliContext) -> dict:
|
|
1215
1215
|
apps = load_list_arg(args.apps_file, option_name="--apps-file") if getattr(args, "apps_file", None) else None
|
|
1216
1216
|
if apps is None and not (args.app_key or "").strip():
|
|
@@ -1235,34 +1235,53 @@ def _handle_layout_apply(args: argparse.Namespace, context: CliContext) -> dict:
|
|
|
1235
1235
|
)
|
|
1236
1236
|
|
|
1237
1237
|
|
|
1238
|
+
def _handle_views_schema(args: argparse.Namespace, context: CliContext) -> dict:
|
|
1239
|
+
return context.builder.app_views_schema(
|
|
1240
|
+
schema_version=args.schema_version,
|
|
1241
|
+
schema_kind=args.schema_kind,
|
|
1242
|
+
)
|
|
1243
|
+
|
|
1244
|
+
|
|
1245
|
+
def _handle_views_get(args: argparse.Namespace, context: CliContext) -> dict:
|
|
1246
|
+
return context.builder.app_views_get(
|
|
1247
|
+
profile=args.profile,
|
|
1248
|
+
app_key=args.app_key,
|
|
1249
|
+
schema_version=args.schema_version,
|
|
1250
|
+
schema_kind=getattr(args, "schema_kind", None) or "summary",
|
|
1251
|
+
)
|
|
1252
|
+
|
|
1253
|
+
|
|
1254
|
+
def _handle_views_validate(args: argparse.Namespace, context: CliContext) -> dict:
|
|
1255
|
+
payload = load_json_value(
|
|
1256
|
+
args.file,
|
|
1257
|
+
option_name="--file",
|
|
1258
|
+
max_bytes=2 * 1024 * 1024,
|
|
1259
|
+
max_depth=64,
|
|
1260
|
+
)
|
|
1261
|
+
if not isinstance(payload, dict):
|
|
1262
|
+
raise_config_error("builder views validate --file must contain a JSON object")
|
|
1263
|
+
return context.builder.app_views_validate(
|
|
1264
|
+
declaration=payload,
|
|
1265
|
+
schema_version=args.schema_version,
|
|
1266
|
+
)
|
|
1267
|
+
|
|
1268
|
+
|
|
1238
1269
|
def _handle_views_apply(args: argparse.Namespace, context: CliContext) -> dict:
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
fix_hint="Use one shape: preferred --views-file for flat batch jobs, or legacy --app-key/--apps-file for grouped payloads.",
|
|
1246
|
-
)
|
|
1247
|
-
return context.builder.app_views_apply(
|
|
1248
|
-
profile=args.profile,
|
|
1249
|
-
app_key="",
|
|
1250
|
-
publish=bool(args.publish),
|
|
1251
|
-
upsert_views=[],
|
|
1252
|
-
patch_views=[],
|
|
1253
|
-
remove_views=[],
|
|
1254
|
-
views=views,
|
|
1270
|
+
view = load_object_arg(args.view_file, option_name="--view-file") if getattr(args, "view_file", None) else None
|
|
1271
|
+
if view is None and not (getattr(args, "patch_views_file", None) or getattr(args, "remove_views_file", None)):
|
|
1272
|
+
raise_config_error(
|
|
1273
|
+
"builder views apply requires --view-file, --patch-views-file, or --remove-views-file.",
|
|
1274
|
+
fix_hint="Pass one JSON object through --view-file to create or fully update one view.",
|
|
1275
|
+
error_code="ARGUMENT_ERROR",
|
|
1255
1276
|
)
|
|
1256
|
-
if apps is None and not (args.app_key or "").strip():
|
|
1257
|
-
raise_config_error("builder views apply requires --views-file, --apps-file, or --app-key")
|
|
1258
1277
|
return context.builder.app_views_apply(
|
|
1259
1278
|
profile=args.profile,
|
|
1260
1279
|
app_key=args.app_key,
|
|
1261
1280
|
publish=bool(args.publish),
|
|
1262
|
-
|
|
1281
|
+
upsert_view=view,
|
|
1263
1282
|
patch_views=load_list_arg(args.patch_views_file, option_name="--patch-views-file"),
|
|
1264
1283
|
remove_views=load_list_arg(args.remove_views_file, option_name="--remove-views-file"),
|
|
1265
|
-
|
|
1284
|
+
schema_version=getattr(args, "schema_version", None),
|
|
1266
1285
|
)
|
|
1267
1286
|
|
|
1268
1287
|
|
|
@@ -178,6 +178,9 @@ BUILDER_PUBLIC_TOOL_SPECS: tuple[PublicToolSpec, ...] = (
|
|
|
178
178
|
PublicToolSpec(BUILDER_DOMAIN, "app_flow_list_versions", ("app_flow_list_versions",), ("builder", "flow", "versions"), has_contract=True, cli_show_effective_context=True),
|
|
179
179
|
PublicToolSpec(BUILDER_DOMAIN, "app_flow_restore_version", ("app_flow_restore_version",), ("builder", "flow", "restore"), has_contract=True, cli_show_effective_context=True, cli_context_write=True),
|
|
180
180
|
PublicToolSpec(BUILDER_DOMAIN, "app_flow_apply", ("app_flow_apply",), ("builder", "flow", "apply"), has_contract=True, cli_show_effective_context=True, cli_context_write=True),
|
|
181
|
+
PublicToolSpec(BUILDER_DOMAIN, "app_views_schema", ("app_views_schema",), ("builder", "views", "schema")),
|
|
182
|
+
PublicToolSpec(BUILDER_DOMAIN, "app_views_get", ("app_views_get",), ("builder", "views", "get"), cli_show_effective_context=True),
|
|
183
|
+
PublicToolSpec(BUILDER_DOMAIN, "app_views_validate", (), ("builder", "views", "validate"), mcp_public=True),
|
|
181
184
|
PublicToolSpec(BUILDER_DOMAIN, "app_views_apply", ("app_views_apply",), ("builder", "views", "apply"), has_contract=True, cli_show_effective_context=True, cli_context_write=True),
|
|
182
185
|
PublicToolSpec(BUILDER_DOMAIN, "app_charts_apply", ("app_charts_apply",), ("builder", "charts", "apply"), has_contract=True, cli_show_effective_context=True, cli_context_write=True),
|
|
183
186
|
PublicToolSpec(BUILDER_DOMAIN, "portal_apply", ("portal_apply",), ("builder", "portal", "apply"), has_contract=True, cli_show_effective_context=True, cli_context_write=True),
|
|
@@ -51,9 +51,15 @@ COMMON_ERROR_DROP_TOP = {
|
|
|
51
51
|
|
|
52
52
|
SUCCESS_POLICY_BY_TOOL: dict[str, TransformFn] = {}
|
|
53
53
|
|
|
54
|
-
#:
|
|
54
|
+
#: 这些校验命令的产出契约明确包含 ok 字段(供阶段 2 SKILL.md 引用),
|
|
55
55
|
#: 需在 success 裁剪时保留 ok(默认 COMMON_SUCCESS_DROP_TOP 会丢弃 ok)。
|
|
56
|
-
|
|
56
|
+
#: `builder:app_views_validate`(views 声明式校验)同属校验类工具,ok 同样进产出契约。
|
|
57
|
+
_KEEP_OK_TOOLS = {
|
|
58
|
+
"builder:app_form_validate_formula",
|
|
59
|
+
"builder:app_form_validate_code_block",
|
|
60
|
+
"builder:app_flow_validate",
|
|
61
|
+
"builder:app_views_validate",
|
|
62
|
+
}
|
|
57
63
|
|
|
58
64
|
|
|
59
65
|
def trim_public_response(tool_name: str | None, payload: dict[str, Any]) -> dict[str, Any]:
|
|
@@ -1310,6 +1316,9 @@ _register_policy(
|
|
|
1310
1316
|
"app_flow_validate",
|
|
1311
1317
|
"app_flow_list_versions",
|
|
1312
1318
|
"app_flow_restore_version",
|
|
1319
|
+
"app_views_schema",
|
|
1320
|
+
"app_views_get",
|
|
1321
|
+
"app_views_validate",
|
|
1313
1322
|
"app_get_charts",
|
|
1314
1323
|
"app_get_buttons",
|
|
1315
1324
|
"app_get_associated_resources",
|