@saga-a2ui/core 0.0.0-stage → 0.1.0-rc.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/LICENSE +202 -0
- package/README.md +10 -2
- package/package.json +1 -6
- package/src/actions/action-button-schema.ts +22 -0
- package/src/actions/binding.ts +172 -0
- package/src/actions/category.ts +60 -0
- package/src/actions/definition.ts +454 -0
- package/src/actions/index.ts +73 -0
- package/src/actions/instance-host.ts +432 -0
- package/src/actions/invocation.ts +874 -0
- package/src/actions/limits.ts +110 -0
- package/src/actions/outcome.ts +125 -0
- package/src/actions/prerequisite.ts +73 -0
- package/src/actions/scope.ts +267 -0
- package/src/actions/server.ts +99 -0
- package/src/actions/thenable.ts +31 -0
- package/src/platform.d.ts +17 -0
- package/src/protocols/a2ui/v0_9/decoder.ts +161 -0
- package/src/protocols/a2ui/v0_9/index.ts +11 -0
- package/src/protocols/a2ui/v0_9/message.ts +29 -0
- package/src/protocols/a2ui/v0_9/session.ts +92 -0
- package/src/protocols/a2ui/zod3.ts +13 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @saga-a2ui/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Renderer-independent actions and A2UI v0.9 decoding. Entry points: `/actions`, `/actions/action-button-schema`, `/protocols/a2ui/v0_9`, `/protocols/a2ui/zod3`.
|
|
4
|
+
|
|
5
|
+
Requires Node >=22.12.0 and a TypeScript-aware bundler; exports are TypeScript source. Apache-2.0, with dependency licenses retained separately.
|
|
6
|
+
|
|
7
|
+
The prepared prerelease is `0.1.0-rc.1`, intended for the `next` tag. Preparation does not establish npm availability. Install only after the release is announced.
|
|
8
|
+
|
|
9
|
+
Use matching exact SAGA versions. At the consumer workspace root, pin `@a2ui/markdown-it` to `0.2.1` using your package manager override. React consumers use `@saga-a2ui/react/a2ui`, without direct A2UI dependencies. Keep the A2UI Zod 3 and MCP Zod 4 contexts separate.
|
|
10
|
+
|
|
11
|
+
See [the installation recipe](https://github.com/MachineWisdomAI/saga-a2ui/blob/main/docs/release-packages.md) and [React consumer dependencies](https://github.com/MachineWisdomAI/saga-a2ui/blob/main/docs/react-consumers.md).
|
package/package.json
CHANGED
|
@@ -1,6 +1 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@saga-a2ui/core",
|
|
3
|
-
"version": "0.0.0-stage",
|
|
4
|
-
"stub": true,
|
|
5
|
-
"description": "Temporary package placeholder for staged publishing"
|
|
6
|
-
}
|
|
1
|
+
{"name":"@saga-a2ui/core","private":false,"description":"Renderer-independent SAGA A2UI processing: version-specific decoding, validated message cases and the message session, and the renderer-independent action runtime (local and server actions, prerequisites, bounded hooks, resource ownership) at /actions","license":"Apache-2.0","type":"module","exports":{"./actions":"./src/actions/index.ts","./actions/action-button-schema":"./src/actions/action-button-schema.ts","./protocols/a2ui/v0_9":"./src/protocols/a2ui/v0_9/index.ts","./protocols/a2ui/zod3":"./src/protocols/a2ui/zod3.ts"},"files":["src","!src/**/*.test.ts","!src/**/*.test-support.ts"],"scripts":{"typecheck":"tsc --noEmit && tsc --noEmit -p tsconfig.test.json"},"dependencies":{"@a2ui/web_core":"0.13.0","zod3":"npm:zod@3.25.76"},"devDependencies":{"@types/node":"22.20.5","typescript":"^5.9.0","vitest":"^5.0.4"},"version":"0.1.0-rc.1","engines":{"node":">=22.12.0"},"repository":{"type":"git","url":"https://github.com/MachineWisdomAI/saga-a2ui.git","directory":"packages/core"},"publishConfig":{"access":"public","tag":"next"}}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { CommonSchemas } from '@a2ui/web_core/v0_9';
|
|
2
|
+
import { z } from '../protocols/a2ui/zod3.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Contract of the opt-in action control. It has no `action` property at all: a layout names the
|
|
6
|
+
* control by ID and type, and application code attaches the callable to the rendered instance.
|
|
7
|
+
* The schema is strict, so a layout that also supplies an upstream `action`, or a handler name,
|
|
8
|
+
* is rejected when it is ingested, before any property is resolved or any function evaluated.
|
|
9
|
+
*
|
|
10
|
+
* The contract depends on the A2UI schema types and Zod, not on Lit, so another renderer can
|
|
11
|
+
* share it.
|
|
12
|
+
*/
|
|
13
|
+
export const ActionButtonApi = {
|
|
14
|
+
name: 'ActionButton',
|
|
15
|
+
schema: z
|
|
16
|
+
.object({
|
|
17
|
+
child: CommonSchemas.ComponentId,
|
|
18
|
+
// The same field the upstream Button has; it yields `isValid`.
|
|
19
|
+
checks: z.array(CommonSchemas.CheckRule).optional(),
|
|
20
|
+
})
|
|
21
|
+
.strict(),
|
|
22
|
+
};
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { planOf, type ActionDefinition } from './definition.js';
|
|
2
|
+
import { emitObservation, Invocation, type InvocationOwner, type Seam } from './invocation.js';
|
|
3
|
+
import { resolveLimits, type Limits } from './limits.js';
|
|
4
|
+
import { discardOutcome, isThenable } from './thenable.js';
|
|
5
|
+
|
|
6
|
+
export type BindingPhase = 'idle' | 'running' | 'settling' | 'disposed';
|
|
7
|
+
|
|
8
|
+
/** Replaced on every change and never mutated, so comparing identities shows whether anything changed. */
|
|
9
|
+
export interface BindingState {
|
|
10
|
+
readonly phase: BindingPhase;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** One definition attached to one event of one rendered instance. */
|
|
14
|
+
export interface ActionBinding {
|
|
15
|
+
/** Opaque and unique within the page session. It finds nothing: no member of this API maps an id to a binding. */
|
|
16
|
+
readonly id: string;
|
|
17
|
+
readonly state: BindingState;
|
|
18
|
+
/**
|
|
19
|
+
* Calls `listener` synchronously after each state change and returns the unsubscribe function.
|
|
20
|
+
* The listener carries no event data, and a listener that throws, or returns a promise that
|
|
21
|
+
* rejects, never affects the action. Nothing is delivered after the final `disposed` state.
|
|
22
|
+
*/
|
|
23
|
+
subscribe(listener: () => void): () => void;
|
|
24
|
+
/**
|
|
25
|
+
* Idempotent. Cancels an undecided invocation with cause `disposed`, detaches this binding's own
|
|
26
|
+
* callable, so later events do nothing, and ends the subscription after one final `disposed`
|
|
27
|
+
* state. A decided outcome is never rewritten. Other bindings of the same definition are untouched.
|
|
28
|
+
*/
|
|
29
|
+
dispose(): void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** What a binding needs from the adapter and the application besides the definition. */
|
|
33
|
+
export interface BindingSeam extends Seam {
|
|
34
|
+
/**
|
|
35
|
+
* Removes this binding's own installation from its slot. Called once, when the binding is
|
|
36
|
+
* disposed. A no-op when the slot holds a newer installation.
|
|
37
|
+
*/
|
|
38
|
+
readonly detach: () => void;
|
|
39
|
+
/**
|
|
40
|
+
* The configuration of the SDK instance that owns this binding, already validated and copied.
|
|
41
|
+
* It supplies each bound the action did not override; absent means the defaults apply.
|
|
42
|
+
*/
|
|
43
|
+
readonly limits?: Limits;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
let bindingCount = 0;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The runtime end of a binding. An activation arrives only through `activate`, and the binding
|
|
50
|
+
* applies the definition's busy policy to it.
|
|
51
|
+
*
|
|
52
|
+
* While an invocation is `running` or `settling`, a new activation first revalidates it, which
|
|
53
|
+
* applies the outcome rule to whatever changed since its last checkpoint, and only then meets the
|
|
54
|
+
* policy. `ignore` (the default) drops it: no hook runs, no invocation exists. `replace` cancels a
|
|
55
|
+
* running invocation with cause `replaced` and starts the new one at once; an invocation already
|
|
56
|
+
* settling keeps its outcome and finishes its cleanup beside the new one.
|
|
57
|
+
*
|
|
58
|
+
* An invocation that is entirely synchronous passes `running` and `settling` and is `idle` again
|
|
59
|
+
* before `activate` returns, with a listener call for each change. A failure decided in that call
|
|
60
|
+
* stack is rethrown to the caller of `activate` when the definition has no `onFailure` to receive
|
|
61
|
+
* it: swallowing it would hide it. A failure decided later has no caller to return to.
|
|
62
|
+
*/
|
|
63
|
+
export function createBinding<Event>(
|
|
64
|
+
definition: ActionDefinition<Event>,
|
|
65
|
+
seam: BindingSeam,
|
|
66
|
+
): { readonly binding: ActionBinding; readonly activate: (event: Event) => void } {
|
|
67
|
+
const plan = planOf(definition);
|
|
68
|
+
// Resolved here, once, from two immutable layers: an invocation's clocks cannot change afterwards.
|
|
69
|
+
const limits = resolveLimits(plan.limits, seam.limits ?? {});
|
|
70
|
+
const listeners = new Set<() => void>();
|
|
71
|
+
let state: BindingState = Object.freeze({ phase: 'idle' });
|
|
72
|
+
let disposing = false;
|
|
73
|
+
/** The newest invocation that has not settled; it alone drives the phase. */
|
|
74
|
+
let owner: Invocation<Event> | undefined;
|
|
75
|
+
const unsettled = new Set<Invocation<Event>>();
|
|
76
|
+
const phase = (): BindingPhase => state.phase;
|
|
77
|
+
const id = `binding-${++bindingCount}`;
|
|
78
|
+
|
|
79
|
+
const notify = (): void => {
|
|
80
|
+
for (const listener of [...listeners]) {
|
|
81
|
+
// A callback may have unsubscribed this listener, or disposed the binding, which ended every
|
|
82
|
+
// subscription after its own final notification. A listener removed meanwhile gets nothing more.
|
|
83
|
+
if (!listeners.has(listener)) continue;
|
|
84
|
+
try {
|
|
85
|
+
const returned: unknown = listener();
|
|
86
|
+
if (isThenable(returned)) discardOutcome(returned);
|
|
87
|
+
} catch {
|
|
88
|
+
// A listener cannot affect the action.
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
const setPhase = (next: BindingPhase): void => {
|
|
93
|
+
if (state.phase === next) return;
|
|
94
|
+
state = Object.freeze({ phase: next });
|
|
95
|
+
notify();
|
|
96
|
+
};
|
|
97
|
+
const isDisposed = (): boolean => disposing || phase() === 'disposed';
|
|
98
|
+
|
|
99
|
+
const invocationOwner: InvocationOwner<Event> = {
|
|
100
|
+
bindingId: id,
|
|
101
|
+
isDisposed,
|
|
102
|
+
decided(invocation) {
|
|
103
|
+
if (owner === invocation && !isDisposed() && phase() === 'running') setPhase('settling');
|
|
104
|
+
},
|
|
105
|
+
settled(invocation) {
|
|
106
|
+
unsettled.delete(invocation);
|
|
107
|
+
if (owner !== invocation) return;
|
|
108
|
+
owner = undefined;
|
|
109
|
+
// An older invocation's cleanup, or a disposed binding, never rewrites the phase.
|
|
110
|
+
if (!isDisposed()) setPhase('idle');
|
|
111
|
+
},
|
|
112
|
+
retire() {
|
|
113
|
+
binding.dispose();
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const binding: ActionBinding = {
|
|
118
|
+
id,
|
|
119
|
+
get state() {
|
|
120
|
+
return state;
|
|
121
|
+
},
|
|
122
|
+
subscribe(listener) {
|
|
123
|
+
if (phase() === 'disposed') return () => undefined;
|
|
124
|
+
listeners.add(listener);
|
|
125
|
+
return () => {
|
|
126
|
+
listeners.delete(listener);
|
|
127
|
+
};
|
|
128
|
+
},
|
|
129
|
+
dispose() {
|
|
130
|
+
if (isDisposed()) return;
|
|
131
|
+
disposing = true;
|
|
132
|
+
seam.detach();
|
|
133
|
+
setPhase('disposed');
|
|
134
|
+
listeners.clear();
|
|
135
|
+
for (const invocation of [...unsettled]) invocation.disposed();
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const ignore = (busy: Invocation<Event>): void => {
|
|
140
|
+
emitObservation(plan, id, Date.now(), { phase: 'ignored', ignoredReason: 'busy', busyInvocationId: busy.id });
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
const activate = (event: Event): void => {
|
|
144
|
+
if (isDisposed()) return;
|
|
145
|
+
const busy = owner;
|
|
146
|
+
if (busy !== undefined) {
|
|
147
|
+
busy.recheck();
|
|
148
|
+
// Revalidating may have ended it, or retired this binding.
|
|
149
|
+
if (isDisposed()) return;
|
|
150
|
+
}
|
|
151
|
+
if (owner !== undefined && plan.busy === 'ignore') {
|
|
152
|
+
ignore(owner);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
const invocation = new Invocation<Event>(plan, invocationOwner, seam, limits);
|
|
156
|
+
const replaced = owner;
|
|
157
|
+
owner = invocation;
|
|
158
|
+
replaced?.replaced();
|
|
159
|
+
setPhase('running');
|
|
160
|
+
// A subscriber is application code and may have disposed the binding while being told. A
|
|
161
|
+
// disposed binding has detached and starts no work.
|
|
162
|
+
if (isDisposed()) {
|
|
163
|
+
owner = undefined;
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
unsettled.add(invocation);
|
|
167
|
+
const failure = invocation.start(event);
|
|
168
|
+
if (failure !== undefined) throw failure.error;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
return { binding, activate };
|
|
172
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// Why an action failed, as far as the evidence goes. The category supplements the stage and the
|
|
2
|
+
// failure kind that already say how far the work got; it never rewrites a decided outcome, and it
|
|
3
|
+
// is not a message to show: raw causes, server text and stack details stay with the application.
|
|
4
|
+
|
|
5
|
+
/** What the SDK itself can establish. An application may use any other non-empty string. */
|
|
6
|
+
export type KnownErrorCategory =
|
|
7
|
+
| 'timeout'
|
|
8
|
+
| 'canceled'
|
|
9
|
+
| 'authentication-required'
|
|
10
|
+
| 'permission-denied'
|
|
11
|
+
| 'unavailable'
|
|
12
|
+
| 'validation'
|
|
13
|
+
| 'application'
|
|
14
|
+
| 'unknown';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Open on purpose: a consumer must handle a category it does not know, and `'unknown'` is the
|
|
18
|
+
* fallback when nothing established a cause. `(string & {})` keeps the known names in editors.
|
|
19
|
+
*/
|
|
20
|
+
export type ErrorCategory = KnownErrorCategory | (string & Record<never, never>);
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A cause that says what happened. An application's handler, prerequisite or hook throws it when
|
|
24
|
+
* it knows, for instance because the response was a 401 or the error was typed, and wraps the
|
|
25
|
+
* original error as `cause`. Nothing is inferred from a message, a status code or a field a
|
|
26
|
+
* foreign error merely happens to have: an error that is not an `ActionError` (or the platform's
|
|
27
|
+
* own timeout and abort errors) is `'unknown'`, so an unfamiliar failure is never taken for an
|
|
28
|
+
* authentication failure.
|
|
29
|
+
*/
|
|
30
|
+
export class ActionError extends Error {
|
|
31
|
+
readonly category: ErrorCategory;
|
|
32
|
+
|
|
33
|
+
constructor(category: ErrorCategory, message: string, options?: { readonly cause?: unknown }) {
|
|
34
|
+
super(message, options);
|
|
35
|
+
if (typeof category !== 'string' || category === '') {
|
|
36
|
+
throw new TypeError('An ActionError needs a non-empty category.');
|
|
37
|
+
}
|
|
38
|
+
this.name = 'ActionError';
|
|
39
|
+
this.category = category;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The category the evidence in `error` supports, or `fallback` when it supports none: the kind of
|
|
45
|
+
* failure the runtime itself decided on (for example input that could not be read) is that
|
|
46
|
+
* evidence and is passed in as the fallback.
|
|
47
|
+
*/
|
|
48
|
+
export function categorize(error: unknown, fallback: ErrorCategory = 'unknown'): ErrorCategory {
|
|
49
|
+
if (error instanceof ActionError) return error.category;
|
|
50
|
+
// The platform's own typed errors (what `AbortSignal.timeout` and `fetch` reject with) are
|
|
51
|
+
// DOMExceptions with these exact names. A plain Error that merely carries the same `name` is
|
|
52
|
+
// not typed evidence.
|
|
53
|
+
const platform: unknown = (globalThis as { DOMException?: unknown }).DOMException;
|
|
54
|
+
if (typeof platform === 'function' && error instanceof platform) {
|
|
55
|
+
const { name } = error as { name?: unknown };
|
|
56
|
+
if (name === 'TimeoutError') return 'timeout';
|
|
57
|
+
if (name === 'AbortError') return 'canceled';
|
|
58
|
+
}
|
|
59
|
+
return fallback;
|
|
60
|
+
}
|