assistsx-js 0.2.2 → 0.2.3

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 CHANGED
@@ -110,6 +110,52 @@ export default defineConfig({
110
110
 
111
111
  **[API开发文档](https://github.com/ven-coder/assistsx-js/blob/main/README-DEV.md)**
112
112
 
113
+ ## StepFlow(流程编排,`assistsx-js/step-flow`)
114
+
115
+ 在保留原有 `Step.run` / `StepImpl` 的前提下,提供基于**状态 + 事件 + on 转移表**的流程编排,适合非线性跳转、多流程共用同一步骤实现。
116
+
117
+ ```ts
118
+ import { Step } from "assistsx-js";
119
+ import {
120
+ StepFlow,
121
+ flowEvent,
122
+ flowRepeat,
123
+ flowEnd,
124
+ createFlowDispatcher,
125
+ buildFlowInitialData,
126
+ createLaunchState,
127
+ } from "assistsx-js/step-flow";
128
+
129
+ // 1. 定义流程(launch 可衔接 legacy finishMethod)
130
+ const config = {
131
+ id: "my.flow",
132
+ initial: "launch",
133
+ states: {} as Record<string, import("assistsx-js/step-flow").FlowStateDef>,
134
+ data: { appName: "抖音", packageName: "com.ss.android.ugc.aweme" },
135
+ };
136
+ const dispatcher = createFlowDispatcher(config);
137
+ config.states = {
138
+ launch: createLaunchState(dispatcher, appLaunch.launch, "mainPage"),
139
+ mainPage: {
140
+ run: async (step) => (isHome(step) ? flowEvent("home") : flowRepeat()),
141
+ on: { home: "next", retry: "mainPage" },
142
+ },
143
+ next: { run: async () => flowEnd(), on: {} },
144
+ };
145
+
146
+ await Step.run(dispatcher, { data: buildFlowInitialData(config) });
147
+ // 或使用:await StepFlow.run(config); // 需在 config.states 填充后再 run,见 createLaunchState 文档
148
+ ```
149
+
150
+ 要点:
151
+
152
+ - **步骤实现**(`FlowStepImpl`)只返回 `flowEvent` / `flowRepeat` / `flowEnd`,或 `legacy` 委托旧版 `StepImpl`。
153
+ - **跳转**由当前状态的 `on` 表决定;同一 `run` 可在不同 Flow 中配置不同的 `on`。
154
+ - **业务数据**建议放在 `step.data.payload`;`getFlowPayload` / `assignFlowPayload` 可读写。
155
+ - **与 legacy 共存**:`import { Step } from "assistsx-js"` 不变;StepFlow 从 `assistsx-js/step-flow` 单独导入。
156
+
157
+ 完整示例见 [ais-douyin-simple](https://github.com/ven-coder/assists-examples) 中 `src/v1/flows/check-unread.ts`。
158
+
113
159
  ## 示例
114
160
 
115
161
  示例源码及使用教程:[assistsx-js-simple](https://github.com/ven-coder/assists-examples/tree/main/assistsx-js-simple)
package/dist/index.cjs CHANGED
@@ -59,6 +59,7 @@ __export(index_exports, {
59
59
  barUtils: () => barUtils,
60
60
  callbacks: () => callbacks,
61
61
  decodeBase64UTF8: () => decodeBase64UTF8,
62
+ ensureAssistsXPinia: () => ensureAssistsXPinia,
62
63
  fileIO: () => fileIO,
63
64
  fileUtils: () => fileUtils,
64
65
  float: () => float,
@@ -1486,6 +1487,17 @@ var useStepStore = (0, import_pinia.defineStore)("step", {
1486
1487
  }
1487
1488
  });
1488
1489
 
1490
+ // src/pinia-ensure.ts
1491
+ var import_pinia2 = require("pinia");
1492
+ var fallbackPinia = null;
1493
+ function ensureAssistsXPinia() {
1494
+ if ((0, import_pinia2.getActivePinia)() !== void 0) return;
1495
+ if (fallbackPinia === null) {
1496
+ fallbackPinia = (0, import_pinia2.createPinia)();
1497
+ }
1498
+ (0, import_pinia2.setActivePinia)(fallbackPinia);
1499
+ }
1500
+
1489
1501
  // src/step-error.ts
1490
1502
  var StepError = class extends Error {
1491
1503
  constructor(message, data, impl, tag, originalError, currentStep) {
@@ -1847,13 +1859,31 @@ var _Step = class _Step {
1847
1859
  this.delayMs = _Step.delayMsDefault;
1848
1860
  this.tag = tag;
1849
1861
  this.stepId = stepId;
1850
- this.data = data != null ? data : {};
1862
+ this.data = _Step.resolveStepData(data);
1851
1863
  this.impl = impl;
1852
1864
  this.delayMs = delayMs;
1853
1865
  this.repeatCountMax = repeatCountMax;
1854
1866
  this.exceptionRetryCountMax = exceptionRetryCountMax;
1855
1867
  this.isEnd = isEnd;
1856
1868
  }
1869
+ /**
1870
+ * 判断步骤数据是否有效(非空且为普通对象)
1871
+ */
1872
+ static isValidStepData(data) {
1873
+ return data !== null && data !== void 0 && typeof data === "object" && !Array.isArray(data);
1874
+ }
1875
+ /**
1876
+ * 解析步骤数据:优先使用传入值,否则使用当前值,均无效时返回空对象
1877
+ */
1878
+ static resolveStepData(provided, fallback) {
1879
+ if (provided !== void 0 && _Step.isValidStepData(provided)) {
1880
+ return provided;
1881
+ }
1882
+ if (fallback !== void 0 && _Step.isValidStepData(fallback)) {
1883
+ return fallback;
1884
+ }
1885
+ return {};
1886
+ }
1857
1887
  /**
1858
1888
  * 运行步骤实现
1859
1889
  * @param impl 步骤实现函数
@@ -1870,7 +1900,9 @@ var _Step = class _Step {
1870
1900
  } = {}) {
1871
1901
  var _a2, _b, _c, _d, _e, _f;
1872
1902
  this.exception = void 0;
1903
+ ensureAssistsXPinia();
1873
1904
  const stepStore = useStepStore();
1905
+ const resolvedData = _Step.resolveStepData(data);
1874
1906
  let implnName = impl.name;
1875
1907
  let currentStep;
1876
1908
  let nextStep;
@@ -1886,12 +1918,12 @@ var _Step = class _Step {
1886
1918
  console.log(`\u751F\u6210\u6B65\u9AA4ID: ${this._stepId}`);
1887
1919
  }
1888
1920
  }
1889
- stepStore.startStep(this._stepId, tag, data);
1921
+ stepStore.startStep(this._stepId, tag, resolvedData);
1890
1922
  currentStep = new _Step({
1891
1923
  stepId: this._stepId,
1892
1924
  impl,
1893
1925
  tag,
1894
- data,
1926
+ data: resolvedData,
1895
1927
  delayMs,
1896
1928
  exceptionRetryCountMax
1897
1929
  });
@@ -2000,7 +2032,7 @@ var _Step = class _Step {
2000
2032
  const errorMsg = JSON.stringify({
2001
2033
  impl: implnName,
2002
2034
  tag,
2003
- data,
2035
+ data: resolvedData,
2004
2036
  error: (_f = e == null ? void 0 : e.message) != null ? _f : String(e)
2005
2037
  });
2006
2038
  stepStore.setError(errorMsg);
@@ -2143,13 +2175,12 @@ var _Step = class _Step {
2143
2175
  repeatCountMax = _Step.repeatCountMaxDefault,
2144
2176
  exceptionRetryCountMax = _Step.exceptionRetryCountMaxDefault
2145
2177
  } = {}) {
2146
- var _a2;
2147
2178
  _Step.assert(this.stepId);
2148
2179
  return new _Step({
2149
2180
  stepId: this.stepId,
2150
2181
  impl,
2151
2182
  tag,
2152
- data: (_a2 = data != null ? data : this.data) != null ? _a2 : {},
2183
+ data: _Step.resolveStepData(data, this.data),
2153
2184
  delayMs,
2154
2185
  repeatCountMax,
2155
2186
  exceptionRetryCountMax
@@ -2162,13 +2193,12 @@ var _Step = class _Step {
2162
2193
  repeatCountMax = _Step.repeatCountMaxDefault,
2163
2194
  exceptionRetryCountMax = _Step.exceptionRetryCountMaxDefault
2164
2195
  } = {}) {
2165
- var _a2;
2166
2196
  _Step.assert(this.stepId);
2167
2197
  return new _Step({
2168
2198
  stepId: this.stepId,
2169
2199
  impl: void 0,
2170
2200
  tag,
2171
- data: (_a2 = data != null ? data : this.data) != null ? _a2 : {},
2201
+ data: _Step.resolveStepData(data, this.data),
2172
2202
  delayMs,
2173
2203
  repeatCountMax,
2174
2204
  exceptionRetryCountMax,
@@ -2186,7 +2216,7 @@ var _Step = class _Step {
2186
2216
  repeat({
2187
2217
  stepId = this.stepId,
2188
2218
  tag = this.tag,
2189
- data = ((_a2) => (_a2 = this.data) != null ? _a2 : {})(),
2219
+ data,
2190
2220
  delayMs = this.delayMs,
2191
2221
  repeatCountMax = this.repeatCountMax,
2192
2222
  exceptionRetryCountMax = this.exceptionRetryCountMax
@@ -2195,7 +2225,7 @@ var _Step = class _Step {
2195
2225
  this.repeatCount++;
2196
2226
  this.stepId = stepId;
2197
2227
  this.tag = tag;
2198
- this.data = data;
2228
+ this.data = _Step.resolveStepData(data, this.data);
2199
2229
  this.delayMs = delayMs;
2200
2230
  this.repeatCountMax = repeatCountMax;
2201
2231
  this.exceptionRetryCountMax = exceptionRetryCountMax;
@@ -7815,6 +7845,17 @@ var Log = class {
7815
7845
  );
7816
7846
  return res.isSuccess();
7817
7847
  }
7848
+ /**
7849
+ * 追加日志(appendTimestampedEntry / appendLine 简写)。
7850
+ * 默认带时间戳;`timestamped: false` 时走 appendLine。
7851
+ */
7852
+ async append(text, options) {
7853
+ const { timestamped = true, maxLength, timeout } = options != null ? options : {};
7854
+ if (timestamped) {
7855
+ return this.appendTimestampedEntry(text, timeout);
7856
+ }
7857
+ return this.appendLine(text, maxLength, timeout);
7858
+ }
7818
7859
  /** 替换全部内容 */
7819
7860
  async replaceAll(content, timeout) {
7820
7861
  const res = await this.asyncCall(
@@ -8021,6 +8062,7 @@ var log = new Log();
8021
8062
  barUtils,
8022
8063
  callbacks,
8023
8064
  decodeBase64UTF8,
8065
+ ensureAssistsXPinia,
8024
8066
  fileIO,
8025
8067
  fileUtils,
8026
8068
  float,