@hcmai/sdk 0.22.0 → 0.22.1

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/dist/index.js CHANGED
@@ -4501,6 +4501,15 @@ function commandFor(decision, interactionId, conversationId) {
4501
4501
  }
4502
4502
  }
4503
4503
 
4504
+ // src/mini/runtime-version.ts
4505
+ var MINI_RUNTIME_SDK_VERSION = "0.1.0";
4506
+ var MINI_SEMVER_PATTERN = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[A-Za-z-][0-9A-Za-z-]*)(?:\.(?:0|[1-9]\d*|\d*[A-Za-z-][0-9A-Za-z-]*))*))?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/;
4507
+ function miniRuntimeSdkVersion(value = MINI_RUNTIME_SDK_VERSION) {
4508
+ if (!MINI_SEMVER_PATTERN.test(value))
4509
+ throw new Error("runtimeSdkVersion must be a valid semver, for example 0.1.0");
4510
+ return value;
4511
+ }
4512
+
4504
4513
  // src/mini/types.ts
4505
4514
  var MINI_CONTEXT_FILE = "hcm.context.json";
4506
4515
  var MINI_STATE_DIR = ".hcm";
@@ -4508,7 +4517,6 @@ var MINI_PULL_MANIFEST_FILE = "mini-manifest.json";
4508
4517
  var MINI_REMOTE_ROOT = "mini_apps";
4509
4518
  var MINI_CONTEXT_KIND = "hcm-mini-app";
4510
4519
  var MINI_CONTEXT_VERSION = 1;
4511
- var MINI_APP_RUNTIME_SDK_VERSION = "0.1.0";
4512
4520
 
4513
4521
  // src/mini/workspace-files.ts
4514
4522
  var MODEL = "TenantExtensionWorkspaceFile";
@@ -4720,7 +4728,7 @@ function buildMiniAppTemplateFiles(args) {
4720
4728
  code: args.appCode,
4721
4729
  name: displayName,
4722
4730
  version: SDK_VERSION,
4723
- sdkVersion: MINI_APP_RUNTIME_SDK_VERSION,
4731
+ sdkVersion: miniRuntimeSdkVersion(args.runtimeSdkVersion),
4724
4732
  surfaces: ["embedded", "standalone"],
4725
4733
  defaultSurface: args.defaultSurface,
4726
4734
  entry: { html: "index.html", css: "index.css", js: "index.js" },
@@ -4748,7 +4756,8 @@ function buildMiniAppTemplateFiles(args) {
4748
4756
  ];
4749
4757
  }
4750
4758
  function templateHtml(templateKind, appCode, displayName, writableConfig) {
4751
- if (templateKind === "writable-workbench") return writableHtml(appCode, displayName, writableConfig);
4759
+ if (templateKind === "writable-workbench")
4760
+ return writableHtml(appCode, displayName, writableConfig);
4752
4761
  if (templateKind === "multi-page") return multiPageHtml(appCode, displayName);
4753
4762
  return dashboardHtml(appCode, displayName);
4754
4763
  }
@@ -4808,7 +4817,9 @@ function inferWritableDefaults(fields, writableFieldSet) {
4808
4817
  return defaults;
4809
4818
  }
4810
4819
  function inferTodayDefaultFields(fields, writableFieldSet) {
4811
- return ["effecDate", "effectiveDate", "startDate"].filter((name) => hasWritableField(fields, writableFieldSet, name));
4820
+ return ["effecDate", "effectiveDate", "startDate"].filter(
4821
+ (name) => hasWritableField(fields, writableFieldSet, name)
4822
+ );
4812
4823
  }
4813
4824
  function hasWritableField(fields, writableFieldSet, name) {
4814
4825
  return writableFieldSet.has(name) && fields.some((field) => field.name === name);
@@ -4849,7 +4860,11 @@ function dashboardHtml(appCode, displayName) {
4849
4860
  <h1>${displayName}</h1>
4850
4861
  </header>
4851
4862
  <main class="hcm-mini-app__body">
4852
- <span data-field="status">Ready</span>
4863
+ <span data-field="status" role="status">\u51C6\u5907\u8BFB\u53D6</span>
4864
+ <button type="button" data-action="refresh">\u5237\u65B0</button>
4865
+ <button type="button" data-action="retry-reads" hidden>\u91CD\u8BFB\u5931\u8D25\u9879</button>
4866
+ <ul data-field="records"></ul>
4867
+ <pre data-field="error" hidden></pre>
4853
4868
  </main>
4854
4869
  </section>
4855
4870
  `;
@@ -4881,18 +4896,133 @@ function dashboardCss() {
4881
4896
  `;
4882
4897
  }
4883
4898
  function dashboardJs() {
4884
- return `export async function mount(container, hcm) {
4885
- const status = container.querySelector('[data-field="status"]');
4886
- if (status) {
4887
- status.textContent = "Tenant " + hcm.context.tenantId;
4888
- }
4899
+ return `${readGroupRuntimeJs()}
4900
+ const cleanups = new WeakMap();
4901
+ export function mount(container, hcm) {
4902
+ cleanups.get(container)?.();
4903
+ const reads = createReadGroup(container, hcm, "dashboard", "\u8BFB\u53D6\u5458\u5DE5\u9884\u89C8");
4904
+ if (!reads) return;
4905
+ // Immutable query snapshot: changing parameters must create a new full task list.
4906
+ const refresh = (mode) => reads.load([{
4907
+ key: "employees", label: "\u5458\u5DE5\u9884\u89C8",
4908
+ read: signal => hcm.query({ model: "Employee", page: 0, size: 10 }, { signal }),
4909
+ commit: result => {
4910
+ const list = container.querySelector('[data-field="records"]');
4911
+ if (!list) return;
4912
+ list.innerHTML = "";
4913
+ for (const employee of result.rows) {
4914
+ const item = document.createElement("li");
4915
+ item.textContent = employee.name || employee.employeeName || employee.id;
4916
+ list.appendChild(item);
4917
+ }
4918
+ if (!result.rows.length) list.textContent = "\u6682\u65E0\u5458\u5DE5\u8BB0\u5F55";
4919
+ },
4920
+ }], mode);
4921
+ const onClick = event => {
4922
+ if (event.target.closest('[data-action="refresh"]')) void refresh("refresh");
4923
+ if (event.target.closest('[data-action="retry-reads"]')) void reads.retry();
4924
+ };
4925
+ container.addEventListener("click", onClick);
4926
+ cleanups.set(container, () => { reads.dispose(); container.removeEventListener("click", onClick); });
4927
+ void refresh("initial");
4889
4928
  }
4890
4929
 
4891
4930
  export function unmount(container) {
4931
+ cleanups.get(container)?.();
4932
+ cleanups.delete(container);
4892
4933
  container.innerHTML = "";
4893
4934
  }
4894
4935
  `;
4895
4936
  }
4937
+ function readGroupRuntimeJs() {
4938
+ return `const createReadGroup = (container, hcm, key, title) => {
4939
+ const status = container.querySelector('[data-field="status"]');
4940
+ const errorBox = container.querySelector('[data-field="error"]');
4941
+ const retryButton = container.querySelector('[data-action="retry-reads"]');
4942
+ const showError = error => {
4943
+ if (!errorBox) return;
4944
+ errorBox.hidden = !error;
4945
+ errorBox.textContent = error ? [error.code || "LOAD_FAILED", error.message || String(error), error.traceId ? "traceId: " + error.traceId : ""].filter(Boolean).join("\\n") : "";
4946
+ };
4947
+ if (!hcm.loading?.run) {
4948
+ if (status) status.textContent = "\u8FD0\u884C\u65F6\u9700\u8981\u5347\u7EA7";
4949
+ showError({ code: "MINI_LOADING_UNSUPPORTED", message: "\u8BF7\u5347\u7EA7 HCM \u5BBF\u4E3B\u4EE5\u652F\u6301 hcm.loading.run\uFF0C\u7136\u540E\u91CD\u65B0\u6253\u5F00\u5E94\u7528\u3002" });
4950
+ return null;
4951
+ }
4952
+ let mounted = true;
4953
+ let callId = 0;
4954
+ let activeController;
4955
+ let definitions = [];
4956
+ let outcomes = new Map();
4957
+ const failedKeys = () => [...outcomes].filter(([, result]) => result.status === "rejected").map(([key]) => key);
4958
+ const renderOutcomes = (busy = false) => {
4959
+ const failed = failedKeys();
4960
+ if (retryButton) { retryButton.hidden = !failed.length; retryButton.disabled = busy; }
4961
+ const errors = failed.map(key => {
4962
+ const reason = outcomes.get(key).reason;
4963
+ const label = definitions.find(item => item.key === key).label;
4964
+ return label + ": " + (reason?.message || String(reason)) + (reason?.traceId ? " (traceId: " + reason.traceId + ")" : "");
4965
+ });
4966
+ showError(errors.length ? { code: "READS_PARTIAL_FAILURE", message: errors.join("\\n") } : null);
4967
+ };
4968
+ const start = (keys, mode, present) => {
4969
+ if (!mounted || !keys.length) return Promise.resolve();
4970
+ const invocation = ++callId;
4971
+ activeController?.abort();
4972
+ activeController = new AbortController();
4973
+ const tasks = keys.map(key => definitions.find(item => item.key === key));
4974
+ let currentScope;
4975
+ const current = scope => mounted && invocation === callId && scope.isCurrent();
4976
+ if (status) status.textContent = mode === "initial" ? title : "\u6B63\u5728\u91CD\u8BFB " + tasks.length + " \u9879";
4977
+ renderOutcomes(true);
4978
+ return hcm.loading.run({ key, title, mode, signal: activeController.signal,
4979
+ tasks: tasks.map(task => ({ key: task.key, label: task.label })),
4980
+ }, async scope => {
4981
+ currentScope = scope;
4982
+ // Start the complete declared set synchronously before awaiting any receipt.
4983
+ const commits = tasks.map(task => {
4984
+ const receipt = scope.read(task.key, signal => task.read(signal));
4985
+ return (async () => {
4986
+ const [result] = await Promise.allSettled([receipt]);
4987
+ if (!current(scope) || scope.signal.aborted) return;
4988
+ outcomes.set(task.key, result);
4989
+ if (result.status === "fulfilled") await task.commit(result.value, () => current(scope) && !scope.signal.aborted);
4990
+ if (!current(scope) || scope.signal.aborted) return;
4991
+ renderOutcomes(true);
4992
+ })();
4993
+ });
4994
+ if (present) commits.push((async () => {
4995
+ if (!current(scope) || scope.signal.aborted) return;
4996
+ await present(() => current(scope) && !scope.signal.aborted);
4997
+ })());
4998
+ const settled = await Promise.allSettled(commits);
4999
+ // A successful read cannot certify a successful DOM commit.
5000
+ const failedCommit = settled.find(item => item.status === "rejected");
5001
+ if (failedCommit) throw failedCommit.reason;
5002
+ if (!current(scope) || scope.signal.aborted) return;
5003
+ scope.phase("presenting");
5004
+ renderOutcomes();
5005
+ if (status) status.textContent = failedKeys().length ? "\u90E8\u5206\u8BFB\u53D6\u5931\u8D25" : "\u6570\u636E\u5DF2\u66F4\u65B0";
5006
+ }).catch(error => {
5007
+ if (!mounted || invocation !== callId || error?.code === "LOAD_GROUP_CANCELLED") return;
5008
+ if (currentScope && !currentScope.isCurrent()) return;
5009
+ if (status) status.textContent = "\u8BFB\u53D6\u6216\u5448\u73B0\u5931\u8D25";
5010
+ renderOutcomes();
5011
+ showError(error ?? { code: "LOAD_FAILED", message: String(error) });
5012
+ });
5013
+ };
5014
+ return {
5015
+ load: (next, mode = "initial", present) => {
5016
+ definitions = Object.freeze(next.map(item => Object.freeze({ ...item })));
5017
+ outcomes = new Map();
5018
+ return start(definitions.map(item => item.key), mode, present);
5019
+ },
5020
+ retry: () => start(failedKeys(), "refresh"),
5021
+ cancel: () => { callId++; activeController?.abort(); definitions = []; outcomes = new Map(); renderOutcomes(); },
5022
+ dispose: () => { mounted = false; callId++; activeController?.abort(); },
5023
+ };
5024
+ };`;
5025
+ }
4896
5026
  function multiPageHtml(appCode, displayName) {
4897
5027
  return `<section class="mini-routed-app" data-mini-app="${appCode}">
4898
5028
  <header class="mini-routed-app__header">
@@ -4903,6 +5033,8 @@ function multiPageHtml(appCode, displayName) {
4903
5033
  <span class="mini-routed-app__status" data-field="status">\u51C6\u5907\u5C31\u7EEA</span>
4904
5034
  </header>
4905
5035
 
5036
+ <button type="button" data-action="refresh">\u5237\u65B0</button>
5037
+ <button type="button" data-action="retry-reads" hidden>\u91CD\u8BFB\u5931\u8D25\u9879</button>
4906
5038
  <nav class="mini-routed-app__nav" data-field="nav" aria-label="\u8F7B\u5E94\u7528\u9875\u9762"></nav>
4907
5039
  <main class="mini-routed-app__view" data-field="view"></main>
4908
5040
  <pre class="mini-routed-app__error" data-field="error" hidden></pre>
@@ -5061,7 +5193,9 @@ function multiPageCss() {
5061
5193
  `;
5062
5194
  }
5063
5195
  function multiPageJs(appCode, displayName, config) {
5064
- return `const APP_CODE = ${JSON.stringify(appCode)};
5196
+ return `${readGroupRuntimeJs()}
5197
+ const cleanups = new WeakMap();
5198
+ const APP_CODE = ${JSON.stringify(appCode)};
5065
5199
  const APP_NAME = ${JSON.stringify(displayName)};
5066
5200
  const DATA_MODEL = ${JSON.stringify(config.model)};
5067
5201
  const MODEL_LABEL = ${JSON.stringify(config.modelLabel)};
@@ -5071,11 +5205,11 @@ const ROUTES = [
5071
5205
  { path: "/settings", label: "\u8BBE\u7F6E" },
5072
5206
  ];
5073
5207
 
5208
+ const createApp = (container, hcm, reads) => {
5074
5209
  const state = {
5075
- records: [],
5076
- loaded: false,
5077
- details: new Map(),
5078
- unsubscribe: null,
5210
+ records: null,
5211
+ detail: null,
5212
+ detailId: "",
5079
5213
  };
5080
5214
 
5081
5215
  const textOf = (value, fallback = "-") => String(value ?? fallback);
@@ -5120,22 +5254,6 @@ const renderNav = (container, hcm) => {
5120
5254
  }
5121
5255
  };
5122
5256
 
5123
- const loadRecords = async (container, hcm) => {
5124
- if (state.loaded) return;
5125
- setStatus(container, "\u52A0\u8F7D\u4E2D");
5126
- const result = await hcm.query({ model: DATA_MODEL, page: 0, size: 10 });
5127
- state.records = result.rows || [];
5128
- state.loaded = true;
5129
- setStatus(container, "\u5DF2\u66F4\u65B0");
5130
- };
5131
-
5132
- const loadDetail = async (hcm, id) => {
5133
- if (!state.details.has(id)) {
5134
- state.details.set(id, await hcm.get({ model: DATA_MODEL, id }));
5135
- }
5136
- return state.details.get(id);
5137
- };
5138
-
5139
5257
  const renderOverview = (container, hcm) => {
5140
5258
  const view = viewOf(container);
5141
5259
  if (!view) return;
@@ -5153,6 +5271,7 @@ const renderOverview = (container, hcm) => {
5153
5271
  ["\u5E94\u7528", APP_NAME],
5154
5272
  ["\u6570\u636E\u6A21\u578B", DATA_MODEL],
5155
5273
  ["\u5F53\u524D\u79DF\u6237", hcm.context.tenantId],
5274
+ ["\u9884\u89C8\u8BB0\u5F55", state.records === null ? "\u8BFB\u53D6\u4E2D" : state.records.length],
5156
5275
  ];
5157
5276
  for (const [label, value] of metrics) {
5158
5277
  const card = createNode("div", "mini-routed-app__metric");
@@ -5164,8 +5283,7 @@ const renderOverview = (container, hcm) => {
5164
5283
  view.appendChild(grid);
5165
5284
  };
5166
5285
 
5167
- const renderRecords = async (container, hcm) => {
5168
- await loadRecords(container, hcm);
5286
+ const renderRecords = (container, hcm) => {
5169
5287
  const view = viewOf(container);
5170
5288
  if (!view) return;
5171
5289
  view.innerHTML = "";
@@ -5190,8 +5308,7 @@ const renderRecords = async (container, hcm) => {
5190
5308
  view.appendChild(list);
5191
5309
  };
5192
5310
 
5193
- const renderRecordDetail = async (container, hcm, id) => {
5194
- const record = await loadDetail(hcm, id);
5311
+ const renderRecordDetail = (container, hcm, record) => {
5195
5312
  const view = viewOf(container);
5196
5313
  if (!view) return;
5197
5314
  view.innerHTML = "";
@@ -5233,54 +5350,80 @@ const renderSettings = (container, hcm) => {
5233
5350
  view.appendChild(section);
5234
5351
  };
5235
5352
 
5236
- const renderRoute = async (container, hcm) => {
5237
- showError(container, null);
5353
+ const renderRoute = (mode = "initial") => {
5238
5354
  renderNav(container, hcm);
5239
- const detailRoute = hcm.router.match("/records/:id");
5240
- if (detailRoute) {
5241
- await renderRecordDetail(container, hcm, detailRoute.params.id);
5242
- return;
5243
- }
5244
- if (hcm.router.match("/records")) {
5245
- await renderRecords(container, hcm);
5246
- return;
5247
- }
5248
5355
  if (hcm.router.match("/settings")) {
5356
+ reads.cancel();
5357
+ showError(container, null);
5249
5358
  renderSettings(container, hcm);
5250
5359
  return;
5251
5360
  }
5252
- if (hcm.router.current().path !== "/") {
5361
+ const detailRoute = hcm.router.match("/records/:id");
5362
+ if (detailRoute) {
5363
+ const id = detailRoute.params.id; // Snapshot this route parameter before reading.
5364
+ const view = viewOf(container);
5365
+ if (state.detailId === id) renderRecordDetail(container, hcm, state.detail);
5366
+ else if (view) view.textContent = "\u6B63\u5728\u8BFB\u53D6\u8BE6\u60C5";
5367
+ return reads.load([{
5368
+ key: "detail", label: MODEL_LABEL + "\u8BE6\u60C5",
5369
+ read: signal => hcm.get({ model: DATA_MODEL, id }, { signal }),
5370
+ commit: record => {
5371
+ state.detail = record;
5372
+ state.detailId = id;
5373
+ renderRecordDetail(container, hcm, record);
5374
+ },
5375
+ }], mode);
5376
+ }
5377
+ if (!hcm.router.match("/records") && hcm.router.current().path !== "/") {
5253
5378
  hcm.router.replace("/");
5254
5379
  return;
5255
5380
  }
5256
- renderOverview(container, hcm);
5381
+ const route = hcm.router.current().path;
5382
+ if (route === "/") renderOverview(container, hcm);
5383
+ else if (state.records !== null) renderRecords(container, hcm);
5384
+ else { const view = viewOf(container); if (view) view.textContent = "\u6B63\u5728\u8BFB\u53D6" + MODEL_LABEL; }
5385
+ return reads.load([{
5386
+ key: "records", label: MODEL_LABEL + "\u5217\u8868",
5387
+ read: signal => hcm.query({ model: DATA_MODEL, page: 0, size: 10 }, { signal }),
5388
+ commit: result => {
5389
+ state.records = result.rows;
5390
+ if (route === "/") renderOverview(container, hcm);
5391
+ else renderRecords(container, hcm);
5392
+ },
5393
+ }], mode);
5257
5394
  };
5258
-
5259
- export async function mount(container, hcm) {
5260
5395
  setText(container, '[data-field="tenant-id"]', hcm.context.tenantId);
5261
5396
  const onClick = (event) => {
5262
- const routeTarget = event.target instanceof Element ? event.target.closest("[data-route]") : null;
5397
+ if (!(event.target instanceof Element)) return;
5398
+ if (event.target.closest('[data-action="retry-reads"]')) { void reads.retry(); return; }
5399
+ if (event.target.closest('[data-action="refresh"]')) { void renderRoute("refresh"); return; }
5400
+ const routeTarget = event.target.closest("[data-route]");
5263
5401
  if (routeTarget?.dataset.route) {
5264
5402
  event.preventDefault();
5265
5403
  hcm.router.push(routeTarget.dataset.route);
5266
5404
  return;
5267
5405
  }
5268
- const recordTarget = event.target instanceof Element ? event.target.closest("[data-record-id]") : null;
5269
- if (recordTarget?.dataset.recordId) {
5270
- hcm.router.push("/records/" + encodeURIComponent(recordTarget.dataset.recordId));
5271
- }
5406
+ const recordTarget = event.target.closest("[data-record-id]");
5407
+ if (recordTarget?.dataset.recordId) hcm.router.push("/records/" + encodeURIComponent(recordTarget.dataset.recordId));
5272
5408
  };
5273
5409
  container.addEventListener("click", onClick);
5274
- state.unsubscribe = hcm.router.subscribe(() => void renderRoute(container, hcm).catch((error) => showError(container, error)));
5275
- await renderRoute(container, hcm);
5276
- container.__miniCleanup = () => {
5410
+ const unsubscribe = hcm.router.subscribe(() => void renderRoute());
5411
+ void renderRoute();
5412
+ return () => {
5413
+ reads.dispose();
5277
5414
  container.removeEventListener("click", onClick);
5278
- state.unsubscribe?.();
5415
+ unsubscribe();
5279
5416
  };
5417
+ };
5418
+ export function mount(container, hcm) {
5419
+ cleanups.get(container)?.();
5420
+ const reads = createReadGroup(container, hcm, "main", "\u8BFB\u53D6" + MODEL_LABEL);
5421
+ if (!reads) return;
5422
+ cleanups.set(container, createApp(container, hcm, reads));
5280
5423
  }
5281
-
5282
5424
  export function unmount(container) {
5283
- container.__miniCleanup?.();
5425
+ cleanups.get(container)?.();
5426
+ cleanups.delete(container);
5284
5427
  container.innerHTML = "";
5285
5428
  }
5286
5429
  `;
@@ -5303,6 +5446,7 @@ function writableHtml(appCode, displayName, config) {
5303
5446
  <div class="mini-workbench__actions">
5304
5447
  <button type="button" data-action="new-record">\u65B0\u5EFA</button>
5305
5448
  <button type="button" data-action="refresh">\u5237\u65B0</button>
5449
+ <button type="button" data-action="retry-reads" hidden>\u91CD\u8BFB\u5931\u8D25\u9879</button>
5306
5450
  </div>
5307
5451
  </div>
5308
5452
  <div class="mini-workbench__list" data-field="record-list"></div>
@@ -5531,14 +5675,21 @@ function writableJs(config) {
5531
5675
  const fields = config.fields?.length ? JSON.stringify(config.fields) : "null";
5532
5676
  const defaults = JSON.stringify(config.defaults, null, 2);
5533
5677
  const todayDefaults = JSON.stringify(config.todayDefaultFields);
5534
- return `const MODEL = ${model};
5678
+ return `${readGroupRuntimeJs()}
5679
+ const cleanups = new WeakMap();
5680
+ const MODEL = ${model};
5535
5681
  const MODEL_LABEL = ${modelLabel};
5536
5682
  const ROUTE_BASE = "/records";
5537
5683
  const FORM_FIELDS = ${fields};
5538
5684
  const CREATE_DEFAULTS = ${defaults};
5539
5685
  const TODAY_DEFAULT_FIELDS = ${todayDefaults};
5686
+ const createWorkbench = (container, hcm, reads) => {
5687
+ let mounted = true;
5688
+ let routeCall = 0;
5540
5689
  const state = {
5541
- records: [],
5690
+ records: null,
5691
+ detail: null,
5692
+ detailId: "",
5542
5693
  activeId: "",
5543
5694
  mode: "list",
5544
5695
  meta: null,
@@ -5758,8 +5909,9 @@ const renderFormControl = (field, value, hcm) => {
5758
5909
  }
5759
5910
  return input;
5760
5911
  };
5761
- const renderForm = async (container, hcm, values = {}) => {
5912
+ const renderForm = async (container, hcm, values = {}, isCurrent = () => mounted) => {
5762
5913
  const meta = await ensureMeta(hcm);
5914
+ if (!isCurrent()) return;
5763
5915
  const defaults = formDefaults();
5764
5916
  const fields = hcm.form.fields(meta, formOptions());
5765
5917
  const fieldBox = container.querySelector('[data-field="editor-fields"]');
@@ -5806,6 +5958,7 @@ const renderList = (container, hcm) => {
5806
5958
  const list = container.querySelector('[data-field="record-list"]');
5807
5959
  if (!list) return;
5808
5960
  list.innerHTML = "";
5961
+ if (state.records === null) { list.textContent = "\u6B63\u5728\u8BFB\u53D6" + MODEL_LABEL; return; }
5809
5962
  for (const item of state.records) {
5810
5963
  const button = document.createElement("button");
5811
5964
  button.type = "button";
@@ -5815,46 +5968,50 @@ const renderList = (container, hcm) => {
5815
5968
  list.appendChild(button);
5816
5969
  }
5817
5970
  if (!state.records.length) list.textContent = "\u6682\u65E0" + MODEL_LABEL;
5818
- if (hcm.router.current().path === "/") hcm.router.replace(ROUTE_BASE);
5819
- };
5820
- const loadRecords = async (container, hcm) => {
5821
- setStatus(container, "\u52A0\u8F7D\u4E2D");
5822
- showError(container, null);
5823
- const result = await hcm.query({ model: MODEL, page: 0, size: 50 });
5824
- state.records = result.rows;
5825
- renderList(container, hcm);
5826
- setStatus(container, "\u5DF2\u66F4\u65B0");
5827
5971
  };
5828
- const openEditor = async (container, hcm, id) => {
5829
- state.activeId = id;
5830
- state.mode = "edit";
5972
+ const recordTask = () => ({
5973
+ key: "records", label: MODEL_LABEL + "\u5217\u8868",
5974
+ read: signal => hcm.query({ model: MODEL, page: 0, size: 50 }, { signal }),
5975
+ commit: result => { state.records = result.rows; renderList(container, hcm); },
5976
+ });
5977
+ const metaTask = () => ({
5978
+ key: "meta", label: MODEL_LABEL + "\u8868\u5355\u5B9A\u4E49",
5979
+ read: () => hcm.describe({ model: MODEL, type: "info" }),
5980
+ commit: async (meta, isCurrent) => { state.meta = meta; await presentRoute(isCurrent); },
5981
+ });
5982
+ const detailTask = id => ({
5983
+ key: "detail", label: MODEL_LABEL + "\u8BE6\u60C5",
5984
+ read: signal => hcm.get({ model: MODEL, id }, { signal }),
5985
+ commit: async (record, isCurrent) => { state.detailId = id; state.detail = record; await presentRoute(isCurrent); },
5986
+ });
5987
+ const loadRecords = (container, hcm) => reads.load([recordTask()], "refresh");
5988
+ const openEditor = async (record, isCurrent) => {
5831
5989
  setEditorVisible(container, true);
5832
- setStatus(container, "\u8BFB\u53D6\u8BE6\u60C5");
5833
- showError(container, null);
5834
- const record = await hcm.get({ model: MODEL, id });
5835
5990
  setText(container, '[data-field="editor-title"]', recordName(record));
5836
- await renderForm(container, hcm, record);
5991
+ await renderForm(container, hcm, record, isCurrent);
5992
+ if (!isCurrent()) return;
5837
5993
  setDeleteVisible(container, true);
5838
- setStatus(container, "\u53EF\u7F16\u8F91");
5839
5994
  };
5840
- const openNew = async (container, hcm) => {
5995
+ const openNew = async (isCurrent) => {
5841
5996
  state.activeId = "";
5842
5997
  state.mode = "new";
5843
5998
  setEditorVisible(container, true);
5844
- showError(container, null);
5845
5999
  setText(container, '[data-field="editor-title"]', "\u65B0\u5EFA" + MODEL_LABEL);
5846
- await renderForm(container, hcm, formDefaults());
6000
+ await renderForm(container, hcm, formDefaults(), isCurrent);
6001
+ if (!isCurrent()) return;
5847
6002
  setDeleteVisible(container, false);
5848
- setStatus(container, "\u53EF\u521B\u5EFA");
5849
6003
  };
5850
- const renderRoute = async (container, hcm) => {
6004
+ const presentRoute = async (isCurrent) => {
6005
+ if (!isCurrent()) return;
5851
6006
  if (hcm.router.match(ROUTE_BASE + "/new")) {
5852
- await openNew(container, hcm);
6007
+ if (state.meta) await openNew(isCurrent);
5853
6008
  return;
5854
6009
  }
5855
6010
  const route = hcm.router.match(ROUTE_BASE + "/:id");
5856
6011
  if (route) {
5857
- await openEditor(container, hcm, route.params.id);
6012
+ state.activeId = route.params.id;
6013
+ state.mode = "edit";
6014
+ if (state.meta && state.detailId === route.params.id) await openEditor(state.detail, isCurrent);
5858
6015
  return;
5859
6016
  }
5860
6017
  state.activeId = "";
@@ -5866,6 +6023,34 @@ const renderRoute = async (container, hcm) => {
5866
6023
  setDeleteVisible(container, false);
5867
6024
  renderList(container, hcm);
5868
6025
  };
6026
+ const renderRoute = (initial = false, refreshRecords = false) => {
6027
+ const invocation = ++routeCall;
6028
+ reads.cancel();
6029
+ const current = () => mounted && invocation === routeCall;
6030
+ const tasks = [];
6031
+ if (initial || !state.meta) tasks.push(metaTask());
6032
+ if (initial || refreshRecords || state.records === null) tasks.push(recordTask());
6033
+ if (hcm.router.match(ROUTE_BASE + "/new")) {
6034
+ state.activeId = "";
6035
+ state.mode = "new";
6036
+ setEditorVisible(container, false);
6037
+ setDeleteVisible(container, false);
6038
+ setText(container, '[data-field="editor-title"]', "\u65B0\u5EFA" + MODEL_LABEL);
6039
+ }
6040
+ const route = hcm.router.match(ROUTE_BASE + "/:id");
6041
+ if (route && route.params.id !== "new") {
6042
+ if (state.detailId !== route.params.id) {
6043
+ state.activeId = "";
6044
+ state.mode = "list";
6045
+ setEditorVisible(container, false);
6046
+ setDeleteVisible(container, false);
6047
+ setText(container, '[data-field="editor-title"]', "\u6B63\u5728\u8BFB\u53D6\u8BE6\u60C5");
6048
+ }
6049
+ tasks.push(detailTask(route.params.id));
6050
+ }
6051
+ if (tasks.length) return reads.load(tasks, initial ? "initial" : "refresh", presentRoute);
6052
+ return presentRoute(current).catch(error => { if (current()) showError(container, error); });
6053
+ };
5869
6054
  const saveActive = async (container, hcm) => {
5870
6055
  if (state.mode !== "new" && !state.activeId) return;
5871
6056
  const meta = await ensureMeta(hcm);
@@ -5905,17 +6090,19 @@ const deleteActive = async (container, hcm) => {
5905
6090
  hcm.router.push(ROUTE_BASE);
5906
6091
  setStatus(container, "\u5DF2\u5220\u9664");
5907
6092
  };
5908
- export async function mount(container, hcm) {
5909
6093
  setText(container, '[data-field="tenant-id"]', hcm.context.tenantId);
6094
+ if (hcm.router.current().path === "/") hcm.router.replace(ROUTE_BASE);
5910
6095
  const onClick = (event) => {
5911
6096
  const target = event.target instanceof Element ? event.target.closest("[data-action]") : null;
5912
6097
  if (!target) return;
5913
- if (target.dataset.action === "refresh") void loadRecords(container, hcm).catch((error) => showError(container, error));
6098
+ if (target.dataset.action === "retry-reads") void reads.retry();
6099
+ if (target.dataset.action === "refresh") void renderRoute(false, true);
5914
6100
  if (target.dataset.action === "new-record") hcm.router.push(ROUTE_BASE + "/new");
5915
6101
  if (target.dataset.action === "open-record" && target.dataset.id) hcm.router.push(ROUTE_BASE + "/" + encodeURIComponent(target.dataset.id));
5916
6102
  if (target.dataset.action === "back") hcm.router.push(ROUTE_BASE);
5917
6103
  if (target.dataset.action === "delete-record") {
5918
6104
  void deleteActive(container, hcm).catch((error) => {
6105
+ if (!mounted) return;
5919
6106
  setStatus(container, "\u5220\u9664\u5931\u8D25");
5920
6107
  showError(container, error);
5921
6108
  });
@@ -5924,25 +6111,33 @@ export async function mount(container, hcm) {
5924
6111
  const onSubmit = (event) => {
5925
6112
  event.preventDefault();
5926
6113
  void saveActive(container, hcm).catch((error) => {
6114
+ if (!mounted) return;
5927
6115
  setStatus(container, "\u4FDD\u5B58\u5931\u8D25");
5928
6116
  showError(container, error);
5929
6117
  });
5930
6118
  };
5931
6119
  container.addEventListener("click", onClick);
5932
6120
  container.querySelector('[data-field="editor-form"]')?.addEventListener("submit", onSubmit);
5933
- state.unsubscribe = hcm.router.subscribe(() => void renderRoute(container, hcm).catch((error) => showError(container, error)));
5934
- await ensureMeta(hcm);
5935
- await loadRecords(container, hcm);
5936
- await renderRoute(container, hcm);
5937
- container.__miniCleanup = () => {
6121
+ state.unsubscribe = hcm.router.subscribe(() => void renderRoute());
6122
+ void renderRoute(true);
6123
+ return () => {
6124
+ mounted = false;
6125
+ routeCall++;
6126
+ reads.dispose();
5938
6127
  container.removeEventListener("click", onClick);
5939
6128
  container.querySelector('[data-field="editor-form"]')?.removeEventListener("submit", onSubmit);
5940
6129
  state.unsubscribe?.();
5941
6130
  };
6131
+ };
6132
+ export function mount(container, hcm) {
6133
+ cleanups.get(container)?.();
6134
+ const reads = createReadGroup(container, hcm, "workbench", "\u8BFB\u53D6" + MODEL_LABEL + "\u5DE5\u4F5C\u53F0");
6135
+ if (!reads) return;
6136
+ cleanups.set(container, createWorkbench(container, hcm, reads));
5942
6137
  }
5943
-
5944
6138
  export function unmount(container) {
5945
- container.__miniCleanup?.();
6139
+ cleanups.get(container)?.();
6140
+ cleanups.delete(container);
5946
6141
  container.innerHTML = "";
5947
6142
  }
5948
6143
  `;
@@ -5951,7 +6146,7 @@ export function unmount(container) {
5951
6146
  // src/mini/project.ts
5952
6147
  var MINI_APP_CODE_PATTERN = /^[a-z][a-z0-9-]*$/;
5953
6148
  var SCOPE_PATTERN = /^[A-Z][A-Za-z0-9_]*\.[a-z][A-Za-z0-9]*$/;
5954
- var SEMVER_PATTERN = /^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$/;
6149
+ var SEMVER_PATTERN = MINI_SEMVER_PATTERN;
5955
6150
  var TOP_LEVEL_MANIFEST_KEYS = /* @__PURE__ */ new Set([
5956
6151
  "schemaVersion",
5957
6152
  "code",
@@ -5985,6 +6180,7 @@ async function initMiniAppProject(args) {
5985
6180
  const files = buildMiniAppTemplateFiles2({
5986
6181
  appCode,
5987
6182
  name: args.name,
6183
+ runtimeSdkVersion: args.runtimeSdkVersion,
5988
6184
  defaultSurface: args.defaultSurface ?? "embedded",
5989
6185
  templateKind,
5990
6186
  writableModel: args.writableModel,
@@ -6575,9 +6771,9 @@ async function validateManifestObject(manifest2, context, root, errors, warnings
6575
6771
  requireString(manifest2, "name", errors, (value) => value.trim().length > 0);
6576
6772
  requireString(manifest2, "version", errors, (value) => SEMVER_PATTERN.test(value));
6577
6773
  requireString(manifest2, "sdkVersion", errors, (value) => SEMVER_PATTERN.test(value));
6578
- if (typeof manifest2.sdkVersion === "string" && manifest2.sdkVersion !== MINI_APP_RUNTIME_SDK_VERSION) {
6579
- errors.push(
6580
- `SDK_VERSION_MISMATCH: manifest sdkVersion '${manifest2.sdkVersion}' is incompatible with mini-app runtime SDK '${MINI_APP_RUNTIME_SDK_VERSION}'. Set manifest.sdkVersion to '${MINI_APP_RUNTIME_SDK_VERSION}' and rerun workspace verify.`
6774
+ if (typeof manifest2.sdkVersion === "string" && manifest2.sdkVersion !== MINI_RUNTIME_SDK_VERSION) {
6775
+ warnings.push(
6776
+ `manifest sdkVersion '${manifest2.sdkVersion}' differs from the Core source default '${MINI_RUNTIME_SDK_VERSION}'; target Core compatibility is unknown until workspace verify server preflight. Core requires an exact ABI match.`
6581
6777
  );
6582
6778
  }
6583
6779
  const code = typeof manifest2.code === "string" ? manifest2.code : "";
@@ -26322,12 +26518,13 @@ export {
26322
26518
  METHOD_REPO_DISPLAY,
26323
26519
  METHOD_REPO_SLUG,
26324
26520
  METHOD_REPO_URL,
26325
- MINI_APP_RUNTIME_SDK_VERSION,
26521
+ MINI_RUNTIME_SDK_VERSION as MINI_APP_RUNTIME_SDK_VERSION,
26326
26522
  MINI_CONTEXT_FILE,
26327
26523
  MINI_CONTEXT_KIND,
26328
26524
  MINI_CONTEXT_VERSION,
26329
26525
  MINI_PULL_MANIFEST_FILE,
26330
26526
  MINI_REMOTE_ROOT,
26527
+ MINI_RUNTIME_SDK_VERSION,
26331
26528
  MINI_STATE_DIR,
26332
26529
  MODELS_PATH,
26333
26530
  MODEL_META_PATH,