@agentic-kit/session 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +23 -0
- package/README.md +207 -2
- package/account-store.d.ts +78 -0
- package/account-store.js +162 -0
- package/agent-store.d.ts +79 -0
- package/agent-store.js +251 -0
- package/agent.d.ts +137 -0
- package/agent.js +514 -0
- package/api-key.d.ts +53 -0
- package/api-key.js +60 -0
- package/app-session.d.ts +48 -0
- package/app-session.js +105 -0
- package/auth-error.d.ts +54 -0
- package/auth-error.js +186 -0
- package/auth.d.ts +169 -0
- package/auth.js +514 -0
- package/backend-store.d.ts +134 -0
- package/backend-store.js +259 -0
- package/env.d.ts +9 -0
- package/env.js +10 -0
- package/esm/account-store.d.ts +78 -0
- package/esm/account-store.js +146 -0
- package/esm/agent-store.d.ts +79 -0
- package/esm/agent-store.js +201 -0
- package/esm/agent.d.ts +137 -0
- package/esm/agent.js +503 -0
- package/esm/api-key.d.ts +53 -0
- package/esm/api-key.js +52 -0
- package/esm/app-session.d.ts +48 -0
- package/esm/app-session.js +66 -0
- package/esm/auth-error.d.ts +54 -0
- package/esm/auth-error.js +171 -0
- package/esm/auth.d.ts +169 -0
- package/esm/auth.js +467 -0
- package/esm/backend-store.d.ts +134 -0
- package/esm/backend-store.js +239 -0
- package/esm/env.d.ts +9 -0
- package/esm/env.js +6 -0
- package/esm/index.d.ts +12 -0
- package/esm/index.js +11 -0
- package/esm/operation.d.ts +45 -0
- package/esm/operation.js +87 -0
- package/esm/session.d.ts +192 -0
- package/esm/session.js +297 -0
- package/esm/tools-host.d.ts +29 -0
- package/esm/tools-host.js +71 -0
- package/index.d.ts +12 -0
- package/index.js +63 -0
- package/operation.d.ts +45 -0
- package/operation.js +92 -0
- package/package.json +43 -4
- package/session.d.ts +192 -0
- package/session.js +307 -0
- package/tools-host.d.ts +29 -0
- package/tools-host.js +74 -0
package/esm/session.js
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import { checkContextName, currentContextName, loadSession, openStore, stashBaseDir } from './account-store';
|
|
2
|
+
import { AgentRecordUnreadable, checkAgentName, readAgent } from './agent-store';
|
|
3
|
+
import { backendFromEndpoints, differingPlanes, EndpointError, planeEndpoint, PLANES, readBackend } from './backend-store';
|
|
4
|
+
import { getConstructiveEnv, STASH_DIR_ENV } from './env';
|
|
5
|
+
const CREDENTIAL_VARS = ['CONSTRUCTIVE_USER_ID', 'CONSTRUCTIVE_ACCESS_TOKEN', 'CONSTRUCTIVE_API_KEY'];
|
|
6
|
+
const REQUIRED_CREDENTIAL_VARS = ['CONSTRUCTIVE_USER_ID', 'CONSTRUCTIVE_ACCESS_TOKEN'];
|
|
7
|
+
const endpointVar = (plane) => `CONSTRUCTIVE_${plane.toUpperCase()}_ENDPOINT`;
|
|
8
|
+
const ENDPOINT_VARS = PLANES.map(endpointVar);
|
|
9
|
+
/** Every environment variable `resolveSession` reads, besides `CONSTRUCTIVE_STASH_DIR`. */
|
|
10
|
+
export const OVERRIDE_ENV_VARS = [...CREDENTIAL_VARS, ...ENDPOINT_VARS];
|
|
11
|
+
const isSet = (env, name) => env[name] !== undefined;
|
|
12
|
+
function envBackend(env) {
|
|
13
|
+
const empty = ENDPOINT_VARS.find((name) => env[name] === '');
|
|
14
|
+
if (empty)
|
|
15
|
+
return { kind: 'invalid-override', variable: empty, problem: `${empty} is set but empty.`, fix: `Set ${empty} to an https URL, or unset it.` };
|
|
16
|
+
const api = env.CONSTRUCTIVE_API_ENDPOINT;
|
|
17
|
+
if (api === undefined) {
|
|
18
|
+
const stray = ENDPOINT_VARS.find((name) => isSet(env, name));
|
|
19
|
+
if (!stray)
|
|
20
|
+
return { kind: 'none' };
|
|
21
|
+
return {
|
|
22
|
+
kind: 'invalid-override',
|
|
23
|
+
variable: 'CONSTRUCTIVE_API_ENDPOINT',
|
|
24
|
+
problem: `${stray} is set, but CONSTRUCTIVE_API_ENDPOINT is not, so the environment names no backend.`,
|
|
25
|
+
fix: `Set CONSTRUCTIVE_API_ENDPOINT to the api endpoint of the backend ${stray} belongs to, or unset ${stray}.`
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
const given = Object.fromEntries(PLANES.filter((plane) => plane !== 'api' && isSet(env, endpointVar(plane))).map((plane) => [plane, env[endpointVar(plane)]]));
|
|
29
|
+
try {
|
|
30
|
+
return { kind: 'backend', backend: backendFromEndpoints({ api, ...given }) };
|
|
31
|
+
}
|
|
32
|
+
catch (err) {
|
|
33
|
+
if (!(err instanceof EndpointError))
|
|
34
|
+
throw err;
|
|
35
|
+
const variable = err.reason === 'not-derivable' ? 'CONSTRUCTIVE_API_ENDPOINT' : endpointVar(err.plane);
|
|
36
|
+
return { kind: 'invalid-override', variable, problem: err.message, fix: ENDPOINT_FIX[err.reason](endpointVar(err.plane), env) };
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/** What to set for each reason an endpoint variable is refused. */
|
|
40
|
+
const ENDPOINT_FIX = {
|
|
41
|
+
'not-a-url': (variable) => `Set ${variable} to an absolute https URL, or unset it.`,
|
|
42
|
+
scheme: (variable) => `Set ${variable} to an https URL, or unset it.`,
|
|
43
|
+
insecure: (variable) => `Set ${variable} to its https URL, or unset it; plain http is accepted only for this machine.`,
|
|
44
|
+
userinfo: (variable) => `Set ${variable} to the URL with no user name or password in it, or unset it.`,
|
|
45
|
+
'not-derivable': (_variable, env) => `Set every plane explicitly: ${ENDPOINT_VARS.filter((name) => !isSet(env, name)).join(', ')}.`
|
|
46
|
+
};
|
|
47
|
+
function envAccount(env) {
|
|
48
|
+
const set = CREDENTIAL_VARS.filter((name) => isSet(env, name));
|
|
49
|
+
if (set.length === 0)
|
|
50
|
+
return { kind: 'none' };
|
|
51
|
+
const missing = REQUIRED_CREDENTIAL_VARS.find((name) => !env[name]);
|
|
52
|
+
if (missing || env.CONSTRUCTIVE_API_KEY === '') {
|
|
53
|
+
const variable = missing ?? 'CONSTRUCTIVE_API_KEY';
|
|
54
|
+
return {
|
|
55
|
+
kind: 'invalid-override',
|
|
56
|
+
variable,
|
|
57
|
+
problem: `${set.join(' and ')} ${set.length > 1 ? 'are' : 'is'} set, so the credential comes from the environment, but ${variable} is ${isSet(env, variable) ? 'empty' : 'not set'}.`,
|
|
58
|
+
fix: `Set CONSTRUCTIVE_USER_ID and CONSTRUCTIVE_ACCESS_TOKEN (and optionally CONSTRUCTIVE_API_KEY), or unset all three to use the stored session.`
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
const apiKey = env.CONSTRUCTIVE_API_KEY;
|
|
62
|
+
return {
|
|
63
|
+
kind: 'account',
|
|
64
|
+
account: {
|
|
65
|
+
userId: env.CONSTRUCTIVE_USER_ID,
|
|
66
|
+
accessToken: env.CONSTRUCTIVE_ACCESS_TOKEN,
|
|
67
|
+
key: apiKey ? { kind: 'active', apiKey } : { kind: 'none' }
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
const sameBackend = (a, b) => differingPlanes(a, b).length === 0;
|
|
72
|
+
/** The stored side: the context `named` (default: the active one), with the session filed under it. */
|
|
73
|
+
function storedSession(store, named) {
|
|
74
|
+
const context = named ?? currentContextName(store);
|
|
75
|
+
const stored = context ? readBackend(store, context) : null;
|
|
76
|
+
if (!stored)
|
|
77
|
+
return { kind: 'no-backend', context: context ?? null };
|
|
78
|
+
if (stored.status === 'incomplete') {
|
|
79
|
+
return { kind: 'incomplete-backend', context, missing: stored.missing, ...(stored.apiEndpoint ? { apiEndpoint: stored.apiEndpoint } : {}) };
|
|
80
|
+
}
|
|
81
|
+
if (stored.status === 'invalid')
|
|
82
|
+
return { kind: 'invalid-backend', context, plane: stored.plane, problem: stored.problem };
|
|
83
|
+
const session = loadSession(store, context);
|
|
84
|
+
if (!session)
|
|
85
|
+
return { kind: 'signed-out', source: 'store', context, backend: stored.backend };
|
|
86
|
+
const differs = session.issuedFor ? differingPlanes(session.issuedFor, stored.backend) : [];
|
|
87
|
+
if (!session.issuedFor || differs.length > 0) {
|
|
88
|
+
return { kind: 'issuer-mismatch', source: 'store', context, backend: stored.backend, issuedFor: session.issuedFor ?? null, differs };
|
|
89
|
+
}
|
|
90
|
+
const key = session.apiKey
|
|
91
|
+
? { kind: 'active', apiKey: session.apiKey, ...(session.keyId ? { keyId: session.keyId } : {}), ...(session.apiKeyExpiresAt ? { expiresAt: session.apiKeyExpiresAt } : {}) }
|
|
92
|
+
: { kind: 'none' };
|
|
93
|
+
return {
|
|
94
|
+
kind: 'signed-in',
|
|
95
|
+
source: 'store',
|
|
96
|
+
context,
|
|
97
|
+
backend: stored.backend,
|
|
98
|
+
account: {
|
|
99
|
+
userId: session.userId,
|
|
100
|
+
...(session.email ? { email: session.email } : {}),
|
|
101
|
+
accessToken: session.accessToken,
|
|
102
|
+
...(session.accessTokenExpiresAt ? { accessTokenExpiresAt: session.accessTokenExpiresAt } : {}),
|
|
103
|
+
key
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
/** The store to read and the environment's backend and credential, or the environment variable that is wrong. */
|
|
108
|
+
function prepare({ context, env, store }) {
|
|
109
|
+
if (context !== undefined)
|
|
110
|
+
checkContextName(context);
|
|
111
|
+
let opened = store;
|
|
112
|
+
if (!opened) {
|
|
113
|
+
try {
|
|
114
|
+
opened = openStore(stashBaseDir(env));
|
|
115
|
+
}
|
|
116
|
+
catch (err) {
|
|
117
|
+
if (env[STASH_DIR_ENV] !== '')
|
|
118
|
+
throw err;
|
|
119
|
+
return { kind: 'invalid-override', variable: STASH_DIR_ENV, problem: err.message, fix: `Set ${STASH_DIR_ENV} to a directory, or unset it.` };
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
const backend = envBackend(env);
|
|
123
|
+
if (backend.kind === 'invalid-override')
|
|
124
|
+
return backend;
|
|
125
|
+
const account = envAccount(env);
|
|
126
|
+
if (account.kind === 'invalid-override')
|
|
127
|
+
return account;
|
|
128
|
+
return { store: opened, backend, account };
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* The person's session, resolved once from the environment and the store together.
|
|
132
|
+
*
|
|
133
|
+
* - Any credential variable selects the environment as the credential source;
|
|
134
|
+
* it needs CONSTRUCTIVE_USER_ID, CONSTRUCTIVE_ACCESS_TOKEN and the backend
|
|
135
|
+
* from CONSTRUCTIVE_API_ENDPOINT.
|
|
136
|
+
* - An endpoint override alone gives that backend `signed-out`, unless it names
|
|
137
|
+
* exactly the backend of the stored context, whose session then applies.
|
|
138
|
+
* - Otherwise the stored context `context` (default: the active one) and the
|
|
139
|
+
* session filed under it, used only when the context's auth endpoint issued it.
|
|
140
|
+
* - A `context` named while the environment decides the session is `conflict`.
|
|
141
|
+
*
|
|
142
|
+
* Returns `invalid-override` or `conflict` rather than throwing for a wrong
|
|
143
|
+
* environment. Throws `ContextNameError` for a `context` that is not a valid name.
|
|
144
|
+
*/
|
|
145
|
+
export function resolveSession({ context, env = getConstructiveEnv(), store } = {}) {
|
|
146
|
+
const prepared = prepare({ context, env, store });
|
|
147
|
+
if ('kind' in prepared)
|
|
148
|
+
return prepared;
|
|
149
|
+
const { store: opened, backend, account } = prepared;
|
|
150
|
+
if (account.kind === 'account') {
|
|
151
|
+
if (backend.kind === 'none') {
|
|
152
|
+
return {
|
|
153
|
+
kind: 'invalid-override',
|
|
154
|
+
variable: 'CONSTRUCTIVE_API_ENDPOINT',
|
|
155
|
+
problem: 'The credential comes from the environment, but CONSTRUCTIVE_API_ENDPOINT is not set, so it names no backend to send it to.',
|
|
156
|
+
fix: 'Set CONSTRUCTIVE_API_ENDPOINT to the api endpoint the credential belongs to.'
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
if (context !== undefined)
|
|
160
|
+
return conflict(context, CREDENTIAL_VARS.find((name) => isSet(env, name)), backend.backend, 'credential');
|
|
161
|
+
return { kind: 'signed-in', source: 'env', context: null, backend: backend.backend, account: account.account };
|
|
162
|
+
}
|
|
163
|
+
const stored = storedSession(opened, context);
|
|
164
|
+
if (backend.kind === 'none')
|
|
165
|
+
return stored;
|
|
166
|
+
const storedBackend = stored.kind === 'signed-in' || stored.kind === 'signed-out' || stored.kind === 'issuer-mismatch' ? stored.backend : null;
|
|
167
|
+
if (storedBackend && sameBackend(storedBackend, backend.backend))
|
|
168
|
+
return stored;
|
|
169
|
+
if (context !== undefined)
|
|
170
|
+
return conflict(context, 'CONSTRUCTIVE_API_ENDPOINT', backend.backend, 'backend');
|
|
171
|
+
return { kind: 'signed-out', source: 'env', context: null, backend: backend.backend };
|
|
172
|
+
}
|
|
173
|
+
function agentConflict(context, agent, variable) {
|
|
174
|
+
return {
|
|
175
|
+
kind: 'conflict',
|
|
176
|
+
context,
|
|
177
|
+
variable,
|
|
178
|
+
problem: `The agent "${agent}" was named, but ${variable} is set, so the environment would decide the session, not the agent.`,
|
|
179
|
+
fix: `Name no agent to use the environment, or unset ${variable} and the other CONSTRUCTIVE_* credential and endpoint variables to act as the agent.`
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
export const agentFix = (agent) => `Remove the agent "${agent}" and add it again, which needs the person signed in.`;
|
|
183
|
+
/** The fix for an agent whose key has ended or was refused: a new key keeps its principal and grants. */
|
|
184
|
+
export const renewFix = (agent) => `Renew the key of the agent "${agent}", which needs the person's password; if the agent was removed in the app, add it again.`;
|
|
185
|
+
/**
|
|
186
|
+
* The stored agent `agent` under the context `context` (default: the active
|
|
187
|
+
* one), checked against the context's endpoints and its key's end, with no
|
|
188
|
+
* request. With any CONSTRUCTIVE_* credential or endpoint variable set, it is
|
|
189
|
+
* `conflict`: the environment would decide the session, not the agent.
|
|
190
|
+
*/
|
|
191
|
+
export function agentBinding({ agent, context: named, env = getConstructiveEnv(), store }) {
|
|
192
|
+
checkAgentName(agent);
|
|
193
|
+
const prepared = prepare({ context: named, env, store });
|
|
194
|
+
if ('kind' in prepared)
|
|
195
|
+
return prepared;
|
|
196
|
+
const opened = prepared.store;
|
|
197
|
+
const variable = OVERRIDE_ENV_VARS.find((name) => isSet(env, name));
|
|
198
|
+
if (variable)
|
|
199
|
+
return agentConflict(named ?? currentContextName(opened) ?? '', agent, variable);
|
|
200
|
+
const context = named ?? currentContextName(opened);
|
|
201
|
+
const stored = context ? readBackend(opened, context) : null;
|
|
202
|
+
if (!stored)
|
|
203
|
+
return { kind: 'no-backend', context: context ?? null };
|
|
204
|
+
if (stored.status === 'incomplete') {
|
|
205
|
+
return { kind: 'incomplete-backend', context, missing: stored.missing, ...(stored.apiEndpoint ? { apiEndpoint: stored.apiEndpoint } : {}) };
|
|
206
|
+
}
|
|
207
|
+
if (stored.status === 'invalid')
|
|
208
|
+
return { kind: 'invalid-backend', context, plane: stored.plane, problem: stored.problem };
|
|
209
|
+
const unusable = (reason, problem, fix = agentFix(agent)) => ({ kind: 'agent-unusable', context, agent, reason, problem, fix });
|
|
210
|
+
let record;
|
|
211
|
+
try {
|
|
212
|
+
record = readAgent(opened, context, agent);
|
|
213
|
+
}
|
|
214
|
+
catch (err) {
|
|
215
|
+
if (err instanceof AgentRecordUnreadable)
|
|
216
|
+
return unusable('unreadable', err.message.replace('of the agent', `of the agent "${agent}"`));
|
|
217
|
+
throw err;
|
|
218
|
+
}
|
|
219
|
+
if (!record) {
|
|
220
|
+
return unusable('not-found', `There is no agent named "${agent}" under the context "${context}".`, `Add the agent first, or name one that exists.`);
|
|
221
|
+
}
|
|
222
|
+
const differs = differingPlanes(record.issuedFor, stored.backend);
|
|
223
|
+
if (differs.length > 0) {
|
|
224
|
+
const changes = differs.map((plane) => `the ${plane} endpoint is ${planeEndpoint(stored.backend, plane)}, but the agent was set up for ${planeEndpoint(record.issuedFor, plane)}`);
|
|
225
|
+
return unusable('issuer-mismatch', `The agent "${agent}" was set up for another backend: ${changes.join('; ')}. Its credential is not used or sent anywhere.`);
|
|
226
|
+
}
|
|
227
|
+
if (Date.parse(record.keyExpiresAt) <= Date.now()) {
|
|
228
|
+
return unusable('set-up-again', `The key of the agent "${agent}" ended at ${record.keyExpiresAt}.`, renewFix(agent));
|
|
229
|
+
}
|
|
230
|
+
return { kind: 'bound', context, backend: stored.backend, record };
|
|
231
|
+
}
|
|
232
|
+
function conflict(context, variable, backend, what) {
|
|
233
|
+
const unset = what === 'credential' ? 'CONSTRUCTIVE_USER_ID, CONSTRUCTIVE_ACCESS_TOKEN and CONSTRUCTIVE_API_KEY' : 'CONSTRUCTIVE_API_ENDPOINT and the other CONSTRUCTIVE_*_ENDPOINT variables';
|
|
234
|
+
return {
|
|
235
|
+
kind: 'conflict',
|
|
236
|
+
context,
|
|
237
|
+
variable,
|
|
238
|
+
problem: `The context "${context}" was named, but ${variable} is set, so the environment decides the session: the ${what} for ${backend.apiEndpoint}, not the context.`,
|
|
239
|
+
fix: `Name no context to use the environment, or unset ${unset} to use the context.`
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
/** The tools' account for a signed-in session: an agent's names the agent, so a plan says who makes the change. */
|
|
243
|
+
export function hostAccount(session) {
|
|
244
|
+
const { account } = session;
|
|
245
|
+
return {
|
|
246
|
+
userId: account.userId,
|
|
247
|
+
...(account.email ? { email: account.email } : {}),
|
|
248
|
+
...(session.source === 'agent' ? { agent: { name: session.agent.name, principalId: session.agent.principalId } } : {}),
|
|
249
|
+
accessToken: account.accessToken,
|
|
250
|
+
backend: hostBackendConfig(session.backend),
|
|
251
|
+
...(account.key.kind === 'active' ? { apiKey: account.key.apiKey } : {})
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
export function hostBackendConfig(backend) {
|
|
255
|
+
return {
|
|
256
|
+
apiEndpoint: backend.apiEndpoint,
|
|
257
|
+
authEndpoint: backend.authEndpoint,
|
|
258
|
+
modulesEndpoint: backend.modulesEndpoint,
|
|
259
|
+
agentEndpoint: backend.agentEndpoint,
|
|
260
|
+
computeEndpoint: backend.computeEndpoint
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
const planeList = (missing) => `${missing.join(', ')} endpoint${missing.length > 1 ? 's' : ''}`;
|
|
264
|
+
/**
|
|
265
|
+
* Why a session cannot be used, in one or two sentences with the fix, or null
|
|
266
|
+
* for a session that is only absent or signed out. `repairHint` names the
|
|
267
|
+
* command of the binary that stores a complete context. No secret is in it.
|
|
268
|
+
*/
|
|
269
|
+
export function sessionProblem(session, repairHint = 'Store the context again with every endpoint.') {
|
|
270
|
+
switch (session.kind) {
|
|
271
|
+
case 'incomplete-backend':
|
|
272
|
+
return `The backend context "${session.context}" lacks the ${planeList(session.missing)}, so it cannot be used; no endpoint is filled in on its own. ${repairHint}`;
|
|
273
|
+
case 'invalid-backend':
|
|
274
|
+
return `The backend context "${session.context}" cannot be used, and nothing is sent to it: ${session.problem} ${repairHint}`;
|
|
275
|
+
case 'invalid-override':
|
|
276
|
+
case 'conflict':
|
|
277
|
+
case 'agent-unusable':
|
|
278
|
+
return `${session.problem} ${session.fix}`;
|
|
279
|
+
case 'issuer-mismatch': {
|
|
280
|
+
const where = `The session stored under "${session.context}"`;
|
|
281
|
+
if (!session.issuedFor) {
|
|
282
|
+
return `${where} does not record the backend it was issued for, so it is not used or sent anywhere: it may not come from ${session.backend.authEndpoint}. A new sign-in replaces it.`;
|
|
283
|
+
}
|
|
284
|
+
const { issuedFor } = session;
|
|
285
|
+
const changes = session.differs.map((plane) => `the ${plane} endpoint is ${planeEndpoint(session.backend, plane)}, but the session was issued for ${planeEndpoint(issuedFor, plane)}`);
|
|
286
|
+
return `${where} was issued for another backend: ${changes.join('; ')}. It is not used or sent anywhere. A new sign-in replaces it.`;
|
|
287
|
+
}
|
|
288
|
+
case 'no-backend':
|
|
289
|
+
case 'signed-out':
|
|
290
|
+
case 'signed-in':
|
|
291
|
+
return null;
|
|
292
|
+
default: {
|
|
293
|
+
const unknown = session;
|
|
294
|
+
throw new Error(`Unknown session state ${JSON.stringify(unknown)}.`);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { ToolsHost } from '@agentic-kit/db-tools';
|
|
2
|
+
import { type ConstructiveEnv } from './env';
|
|
3
|
+
/**
|
|
4
|
+
* The `configureHost` input of `@agentic-kit/db-tools`. Each call resolves the
|
|
5
|
+
* session again, so a sign-in mid-session, or an agent token another process
|
|
6
|
+
* minted, is picked up without a restart, and each answer comes from one
|
|
7
|
+
* resolution. Absence is null (no account) or undefined (no backend). A
|
|
8
|
+
* session that cannot be used, and a store that cannot be read, throw their
|
|
9
|
+
* cause from both getters, so the tools report it whichever they read first.
|
|
10
|
+
* A stored session another backend issued throws from `account()` only, since
|
|
11
|
+
* the backend itself is usable.
|
|
12
|
+
*
|
|
13
|
+
* With `agent`, the host acts as that agent of the person: `account()` is
|
|
14
|
+
* asynchronous, and gives an access token minted with the agent's key when the
|
|
15
|
+
* cached one is near its end.
|
|
16
|
+
*
|
|
17
|
+
* `previewToken` gives the app sign-in the store keeps for a database under the
|
|
18
|
+
* person's context (`appSignIn`), which the record tools act as.
|
|
19
|
+
*/
|
|
20
|
+
export declare function sessionToolsHost({ signInHint, appSignInHint, repairHint, context, env, agent }: {
|
|
21
|
+
signInHint: string;
|
|
22
|
+
/** The step that signs in to an app, for a record tool that finds no one signed in to it. */
|
|
23
|
+
appSignInHint?: string;
|
|
24
|
+
/** The command that stores a complete context, for the `incomplete-backend` message. */
|
|
25
|
+
repairHint?: string;
|
|
26
|
+
context?: string;
|
|
27
|
+
env?: ConstructiveEnv;
|
|
28
|
+
agent?: string;
|
|
29
|
+
}): ToolsHost;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { openStore, stashBaseDir } from './account-store';
|
|
2
|
+
import { agentSession } from './agent';
|
|
3
|
+
import { loadAppSession } from './app-session';
|
|
4
|
+
import { getConstructiveEnv } from './env';
|
|
5
|
+
import { agentBinding, hostAccount, hostBackendConfig, resolveSession, sessionProblem } from './session';
|
|
6
|
+
/**
|
|
7
|
+
* The `configureHost` input of `@agentic-kit/db-tools`. Each call resolves the
|
|
8
|
+
* session again, so a sign-in mid-session, or an agent token another process
|
|
9
|
+
* minted, is picked up without a restart, and each answer comes from one
|
|
10
|
+
* resolution. Absence is null (no account) or undefined (no backend). A
|
|
11
|
+
* session that cannot be used, and a store that cannot be read, throw their
|
|
12
|
+
* cause from both getters, so the tools report it whichever they read first.
|
|
13
|
+
* A stored session another backend issued throws from `account()` only, since
|
|
14
|
+
* the backend itself is usable.
|
|
15
|
+
*
|
|
16
|
+
* With `agent`, the host acts as that agent of the person: `account()` is
|
|
17
|
+
* asynchronous, and gives an access token minted with the agent's key when the
|
|
18
|
+
* cached one is near its end.
|
|
19
|
+
*
|
|
20
|
+
* `previewToken` gives the app sign-in the store keeps for a database under the
|
|
21
|
+
* person's context (`appSignIn`), which the record tools act as.
|
|
22
|
+
*/
|
|
23
|
+
export function sessionToolsHost({ signInHint, appSignInHint, repairHint, context, env, agent }) {
|
|
24
|
+
const person = () => {
|
|
25
|
+
const session = resolveSession({ context, env });
|
|
26
|
+
const problem = session.kind === 'issuer-mismatch' ? null : sessionProblem(session, repairHint);
|
|
27
|
+
if (problem)
|
|
28
|
+
throw new Error(problem);
|
|
29
|
+
return session;
|
|
30
|
+
};
|
|
31
|
+
const agentAccount = async () => {
|
|
32
|
+
const stash = env ?? getConstructiveEnv();
|
|
33
|
+
const session = await agentSession({ store: openStore(stashBaseDir(stash)), name: agent, context, env: stash });
|
|
34
|
+
const problem = sessionProblem(session, repairHint);
|
|
35
|
+
if (problem)
|
|
36
|
+
throw new Error(problem);
|
|
37
|
+
return session.kind === 'signed-in' ? hostAccount(session) : null;
|
|
38
|
+
};
|
|
39
|
+
return {
|
|
40
|
+
account() {
|
|
41
|
+
if (agent !== undefined)
|
|
42
|
+
return agentAccount();
|
|
43
|
+
const session = person();
|
|
44
|
+
if (session.kind === 'issuer-mismatch')
|
|
45
|
+
throw new Error(sessionProblem(session));
|
|
46
|
+
return session.kind === 'signed-in' ? hostAccount(session) : null;
|
|
47
|
+
},
|
|
48
|
+
backendConfig() {
|
|
49
|
+
if (agent !== undefined) {
|
|
50
|
+
const bound = agentBinding({ context, env, agent });
|
|
51
|
+
if (bound.kind === 'bound')
|
|
52
|
+
return hostBackendConfig(bound.backend);
|
|
53
|
+
const problem = sessionProblem(bound, repairHint);
|
|
54
|
+
if (problem)
|
|
55
|
+
throw new Error(problem);
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
const session = person();
|
|
59
|
+
return session.kind === 'signed-in' || session.kind === 'signed-out' || session.kind === 'issuer-mismatch' ? hostBackendConfig(session.backend) : undefined;
|
|
60
|
+
},
|
|
61
|
+
async previewToken({ databaseId }) {
|
|
62
|
+
const stash = env ?? getConstructiveEnv();
|
|
63
|
+
const session = resolveSession({ context, env: stash });
|
|
64
|
+
const name = 'context' in session ? session.context : null;
|
|
65
|
+
const app = name ? loadAppSession(openStore(stashBaseDir(stash)), name, databaseId) : null;
|
|
66
|
+
return app ? { accessToken: app.accessToken, userId: app.userId, email: app.email, ...(app.accessTokenExpiresAt ? { accessTokenExpiresAt: app.accessTokenExpiresAt } : {}) } : null;
|
|
67
|
+
},
|
|
68
|
+
signInHint,
|
|
69
|
+
...(appSignInHint ? { appSignInHint } : {})
|
|
70
|
+
};
|
|
71
|
+
}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { type AccountSession, checkContextName, ContextNameError, openStore, saveSession, STASH_DIR_ENV, STASH_NAME, stashBaseDir, stashDirectories } from './account-store';
|
|
2
|
+
export { type AgentDetails, AgentError, type AgentErrorKind, type AgentInfo, type AgentRemoval, type AgentRenewal, agentSession, createAgent, grantSchemaChanges, listAgents, removeAgent, renewAgentKey, revokeSchemaChanges, SCHEMA_READ_ONLY_MASK } from './agent';
|
|
3
|
+
export { checkAgentName } from './agent-store';
|
|
4
|
+
export { keyEndsSoon, type KeyRevocation, SIGN_IN_AGAIN_WITHIN_MS } from './api-key';
|
|
5
|
+
export { appAuthEndpoint, type AppSession, appSignIn, appSignOut, loadAppSession } from './app-session';
|
|
6
|
+
export { clearSignInChallenge, type CodeMethod, type CredentialCheck, loadSignInChallenge, saveSignInChallenge, SecondFactorRequired, signIn, type SignInChallenge, type SignInProof, type SignInResult, type SignInTarget, signInTarget, signOut, type SignOutResult, verifySession } from './auth';
|
|
7
|
+
export { SignInError, type SignInErrorKind } from './auth-error';
|
|
8
|
+
export { addBackend, type BackendConfig, type BackendContext, type BackendEndpoints, backendFromEndpoints, ContextInUseError, contextNameFor, type Endpoint, EndpointError, type EndpointProblem, listBackends, type Plane, PLANES, readBackend, removeBackend, selectBackend } from './backend-store';
|
|
9
|
+
export { type ConstructiveEnv, getConstructiveEnv } from './env';
|
|
10
|
+
export { type AgentIdentity, type AgentUnusableReason, hostAccount, OVERRIDE_ENV_VARS, type PersonSession, type ResolvedAgentSession, resolveSession, type Session, type SessionAccount, type SessionKey, sessionProblem, type SessionSource } from './session';
|
|
11
|
+
export { sessionToolsHost } from './tools-host';
|
|
12
|
+
export type { ConfigStore } from 'appstash';
|
package/index.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sessionToolsHost = exports.sessionProblem = exports.resolveSession = exports.OVERRIDE_ENV_VARS = exports.hostAccount = exports.getConstructiveEnv = exports.selectBackend = exports.removeBackend = exports.readBackend = exports.PLANES = exports.listBackends = exports.EndpointError = exports.contextNameFor = exports.ContextInUseError = exports.backendFromEndpoints = exports.addBackend = exports.SignInError = exports.verifySession = exports.signOut = exports.signInTarget = exports.signIn = exports.SecondFactorRequired = exports.saveSignInChallenge = exports.loadSignInChallenge = exports.clearSignInChallenge = exports.loadAppSession = exports.appSignOut = exports.appSignIn = exports.appAuthEndpoint = exports.SIGN_IN_AGAIN_WITHIN_MS = exports.keyEndsSoon = exports.checkAgentName = exports.SCHEMA_READ_ONLY_MASK = exports.revokeSchemaChanges = exports.renewAgentKey = exports.removeAgent = exports.listAgents = exports.grantSchemaChanges = exports.createAgent = exports.agentSession = exports.AgentError = exports.stashDirectories = exports.stashBaseDir = exports.STASH_NAME = exports.STASH_DIR_ENV = exports.saveSession = exports.openStore = exports.ContextNameError = exports.checkContextName = void 0;
|
|
4
|
+
var account_store_1 = require("./account-store");
|
|
5
|
+
Object.defineProperty(exports, "checkContextName", { enumerable: true, get: function () { return account_store_1.checkContextName; } });
|
|
6
|
+
Object.defineProperty(exports, "ContextNameError", { enumerable: true, get: function () { return account_store_1.ContextNameError; } });
|
|
7
|
+
Object.defineProperty(exports, "openStore", { enumerable: true, get: function () { return account_store_1.openStore; } });
|
|
8
|
+
Object.defineProperty(exports, "saveSession", { enumerable: true, get: function () { return account_store_1.saveSession; } });
|
|
9
|
+
Object.defineProperty(exports, "STASH_DIR_ENV", { enumerable: true, get: function () { return account_store_1.STASH_DIR_ENV; } });
|
|
10
|
+
Object.defineProperty(exports, "STASH_NAME", { enumerable: true, get: function () { return account_store_1.STASH_NAME; } });
|
|
11
|
+
Object.defineProperty(exports, "stashBaseDir", { enumerable: true, get: function () { return account_store_1.stashBaseDir; } });
|
|
12
|
+
Object.defineProperty(exports, "stashDirectories", { enumerable: true, get: function () { return account_store_1.stashDirectories; } });
|
|
13
|
+
var agent_1 = require("./agent");
|
|
14
|
+
Object.defineProperty(exports, "AgentError", { enumerable: true, get: function () { return agent_1.AgentError; } });
|
|
15
|
+
Object.defineProperty(exports, "agentSession", { enumerable: true, get: function () { return agent_1.agentSession; } });
|
|
16
|
+
Object.defineProperty(exports, "createAgent", { enumerable: true, get: function () { return agent_1.createAgent; } });
|
|
17
|
+
Object.defineProperty(exports, "grantSchemaChanges", { enumerable: true, get: function () { return agent_1.grantSchemaChanges; } });
|
|
18
|
+
Object.defineProperty(exports, "listAgents", { enumerable: true, get: function () { return agent_1.listAgents; } });
|
|
19
|
+
Object.defineProperty(exports, "removeAgent", { enumerable: true, get: function () { return agent_1.removeAgent; } });
|
|
20
|
+
Object.defineProperty(exports, "renewAgentKey", { enumerable: true, get: function () { return agent_1.renewAgentKey; } });
|
|
21
|
+
Object.defineProperty(exports, "revokeSchemaChanges", { enumerable: true, get: function () { return agent_1.revokeSchemaChanges; } });
|
|
22
|
+
Object.defineProperty(exports, "SCHEMA_READ_ONLY_MASK", { enumerable: true, get: function () { return agent_1.SCHEMA_READ_ONLY_MASK; } });
|
|
23
|
+
var agent_store_1 = require("./agent-store");
|
|
24
|
+
Object.defineProperty(exports, "checkAgentName", { enumerable: true, get: function () { return agent_store_1.checkAgentName; } });
|
|
25
|
+
var api_key_1 = require("./api-key");
|
|
26
|
+
Object.defineProperty(exports, "keyEndsSoon", { enumerable: true, get: function () { return api_key_1.keyEndsSoon; } });
|
|
27
|
+
Object.defineProperty(exports, "SIGN_IN_AGAIN_WITHIN_MS", { enumerable: true, get: function () { return api_key_1.SIGN_IN_AGAIN_WITHIN_MS; } });
|
|
28
|
+
var app_session_1 = require("./app-session");
|
|
29
|
+
Object.defineProperty(exports, "appAuthEndpoint", { enumerable: true, get: function () { return app_session_1.appAuthEndpoint; } });
|
|
30
|
+
Object.defineProperty(exports, "appSignIn", { enumerable: true, get: function () { return app_session_1.appSignIn; } });
|
|
31
|
+
Object.defineProperty(exports, "appSignOut", { enumerable: true, get: function () { return app_session_1.appSignOut; } });
|
|
32
|
+
Object.defineProperty(exports, "loadAppSession", { enumerable: true, get: function () { return app_session_1.loadAppSession; } });
|
|
33
|
+
var auth_1 = require("./auth");
|
|
34
|
+
Object.defineProperty(exports, "clearSignInChallenge", { enumerable: true, get: function () { return auth_1.clearSignInChallenge; } });
|
|
35
|
+
Object.defineProperty(exports, "loadSignInChallenge", { enumerable: true, get: function () { return auth_1.loadSignInChallenge; } });
|
|
36
|
+
Object.defineProperty(exports, "saveSignInChallenge", { enumerable: true, get: function () { return auth_1.saveSignInChallenge; } });
|
|
37
|
+
Object.defineProperty(exports, "SecondFactorRequired", { enumerable: true, get: function () { return auth_1.SecondFactorRequired; } });
|
|
38
|
+
Object.defineProperty(exports, "signIn", { enumerable: true, get: function () { return auth_1.signIn; } });
|
|
39
|
+
Object.defineProperty(exports, "signInTarget", { enumerable: true, get: function () { return auth_1.signInTarget; } });
|
|
40
|
+
Object.defineProperty(exports, "signOut", { enumerable: true, get: function () { return auth_1.signOut; } });
|
|
41
|
+
Object.defineProperty(exports, "verifySession", { enumerable: true, get: function () { return auth_1.verifySession; } });
|
|
42
|
+
var auth_error_1 = require("./auth-error");
|
|
43
|
+
Object.defineProperty(exports, "SignInError", { enumerable: true, get: function () { return auth_error_1.SignInError; } });
|
|
44
|
+
var backend_store_1 = require("./backend-store");
|
|
45
|
+
Object.defineProperty(exports, "addBackend", { enumerable: true, get: function () { return backend_store_1.addBackend; } });
|
|
46
|
+
Object.defineProperty(exports, "backendFromEndpoints", { enumerable: true, get: function () { return backend_store_1.backendFromEndpoints; } });
|
|
47
|
+
Object.defineProperty(exports, "ContextInUseError", { enumerable: true, get: function () { return backend_store_1.ContextInUseError; } });
|
|
48
|
+
Object.defineProperty(exports, "contextNameFor", { enumerable: true, get: function () { return backend_store_1.contextNameFor; } });
|
|
49
|
+
Object.defineProperty(exports, "EndpointError", { enumerable: true, get: function () { return backend_store_1.EndpointError; } });
|
|
50
|
+
Object.defineProperty(exports, "listBackends", { enumerable: true, get: function () { return backend_store_1.listBackends; } });
|
|
51
|
+
Object.defineProperty(exports, "PLANES", { enumerable: true, get: function () { return backend_store_1.PLANES; } });
|
|
52
|
+
Object.defineProperty(exports, "readBackend", { enumerable: true, get: function () { return backend_store_1.readBackend; } });
|
|
53
|
+
Object.defineProperty(exports, "removeBackend", { enumerable: true, get: function () { return backend_store_1.removeBackend; } });
|
|
54
|
+
Object.defineProperty(exports, "selectBackend", { enumerable: true, get: function () { return backend_store_1.selectBackend; } });
|
|
55
|
+
var env_1 = require("./env");
|
|
56
|
+
Object.defineProperty(exports, "getConstructiveEnv", { enumerable: true, get: function () { return env_1.getConstructiveEnv; } });
|
|
57
|
+
var session_1 = require("./session");
|
|
58
|
+
Object.defineProperty(exports, "hostAccount", { enumerable: true, get: function () { return session_1.hostAccount; } });
|
|
59
|
+
Object.defineProperty(exports, "OVERRIDE_ENV_VARS", { enumerable: true, get: function () { return session_1.OVERRIDE_ENV_VARS; } });
|
|
60
|
+
Object.defineProperty(exports, "resolveSession", { enumerable: true, get: function () { return session_1.resolveSession; } });
|
|
61
|
+
Object.defineProperty(exports, "sessionProblem", { enumerable: true, get: function () { return session_1.sessionProblem; } });
|
|
62
|
+
var tools_host_1 = require("./tools-host");
|
|
63
|
+
Object.defineProperty(exports, "sessionToolsHost", { enumerable: true, get: function () { return tools_host_1.sessionToolsHost; } });
|
package/operation.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/** How to undo a step, and what the person reads when the undo worked or failed. */
|
|
2
|
+
export interface Undo<T> {
|
|
3
|
+
run(value: T): Promise<unknown> | unknown;
|
|
4
|
+
/** Read when the undo worked; empty for an undo the person has nothing to do about. */
|
|
5
|
+
undone: string;
|
|
6
|
+
notUndone(value: T, reason: string): string;
|
|
7
|
+
}
|
|
8
|
+
/** One undo after a failure, as the person reads it. */
|
|
9
|
+
export interface Undone {
|
|
10
|
+
step: string;
|
|
11
|
+
undone: boolean;
|
|
12
|
+
text: string;
|
|
13
|
+
}
|
|
14
|
+
/** A step or the commit failed: `cause` is what it threw, `undone` what the steps before it gave back, last first. */
|
|
15
|
+
export interface StepFailure {
|
|
16
|
+
step: string;
|
|
17
|
+
cause: unknown;
|
|
18
|
+
undone: Undone[];
|
|
19
|
+
}
|
|
20
|
+
/** The undo texts of a failure as one sentence run, for the end of its message. */
|
|
21
|
+
export declare const undoneText: (undone: readonly Undone[]) => string;
|
|
22
|
+
/**
|
|
23
|
+
* An operation that changes the backend and the store, as steps with one commit point:
|
|
24
|
+
*
|
|
25
|
+
* pending --step ok--> pending --commit ok--> committed --settle--> committed
|
|
26
|
+
* | |
|
|
27
|
+
* +--step fails--------+--commit fails--> failed (every done step undone, last first)
|
|
28
|
+
*
|
|
29
|
+
* Before the commit, a failed step or commit undoes the steps done so far and throws what `fail` makes of it. After
|
|
30
|
+
* the commit nothing is undone: what is left runs in `settle`, which cannot fail the operation.
|
|
31
|
+
*/
|
|
32
|
+
export declare class Operation {
|
|
33
|
+
private readonly fail;
|
|
34
|
+
private phase;
|
|
35
|
+
private undos;
|
|
36
|
+
constructor(fail: (failure: StepFailure) => Error);
|
|
37
|
+
/** Run a step before the commit. `undo` reverses it when a later step or the commit fails. */
|
|
38
|
+
step<T>(name: string, run: () => Promise<T> | T, undo?: Undo<T>): Promise<T>;
|
|
39
|
+
/** The point after which nothing is undone. A failed commit undoes every step, as a failed step does. */
|
|
40
|
+
commit<T>(name: string, write: () => Promise<T> | T): Promise<T>;
|
|
41
|
+
/** Work after the commit. It cannot fail the operation, so a throw becomes what `onError` makes of it. */
|
|
42
|
+
settle<T>(name: string, run: () => Promise<T> | T, onError: (cause: unknown) => T): Promise<T>;
|
|
43
|
+
private rollBack;
|
|
44
|
+
private expect;
|
|
45
|
+
}
|
package/operation.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Operation = exports.undoneText = void 0;
|
|
4
|
+
const api_key_1 = require("./api-key");
|
|
5
|
+
/** The undo texts of a failure as one sentence run, for the end of its message. */
|
|
6
|
+
const undoneText = (undone) => undone
|
|
7
|
+
.map(({ text }) => text)
|
|
8
|
+
.filter(Boolean)
|
|
9
|
+
.join(' ');
|
|
10
|
+
exports.undoneText = undoneText;
|
|
11
|
+
/**
|
|
12
|
+
* An operation that changes the backend and the store, as steps with one commit point:
|
|
13
|
+
*
|
|
14
|
+
* pending --step ok--> pending --commit ok--> committed --settle--> committed
|
|
15
|
+
* | |
|
|
16
|
+
* +--step fails--------+--commit fails--> failed (every done step undone, last first)
|
|
17
|
+
*
|
|
18
|
+
* Before the commit, a failed step or commit undoes the steps done so far and throws what `fail` makes of it. After
|
|
19
|
+
* the commit nothing is undone: what is left runs in `settle`, which cannot fail the operation.
|
|
20
|
+
*/
|
|
21
|
+
class Operation {
|
|
22
|
+
fail;
|
|
23
|
+
phase = 'pending';
|
|
24
|
+
undos = [];
|
|
25
|
+
constructor(fail) {
|
|
26
|
+
this.fail = fail;
|
|
27
|
+
}
|
|
28
|
+
/** Run a step before the commit. `undo` reverses it when a later step or the commit fails. */
|
|
29
|
+
async step(name, run, undo) {
|
|
30
|
+
this.expect('pending', name);
|
|
31
|
+
let value;
|
|
32
|
+
try {
|
|
33
|
+
value = await run();
|
|
34
|
+
}
|
|
35
|
+
catch (cause) {
|
|
36
|
+
throw await this.rollBack(name, cause);
|
|
37
|
+
}
|
|
38
|
+
if (undo) {
|
|
39
|
+
this.undos.push({
|
|
40
|
+
step: name,
|
|
41
|
+
run: async () => {
|
|
42
|
+
try {
|
|
43
|
+
await undo.run(value);
|
|
44
|
+
return { step: name, undone: true, text: undo.undone };
|
|
45
|
+
}
|
|
46
|
+
catch (err) {
|
|
47
|
+
return { step: name, undone: false, text: undo.notUndone(value, (0, api_key_1.errorText)(err)) };
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
return value;
|
|
53
|
+
}
|
|
54
|
+
/** The point after which nothing is undone. A failed commit undoes every step, as a failed step does. */
|
|
55
|
+
async commit(name, write) {
|
|
56
|
+
this.expect('pending', name);
|
|
57
|
+
let value;
|
|
58
|
+
try {
|
|
59
|
+
value = await write();
|
|
60
|
+
}
|
|
61
|
+
catch (cause) {
|
|
62
|
+
throw await this.rollBack(name, cause);
|
|
63
|
+
}
|
|
64
|
+
this.phase = 'committed';
|
|
65
|
+
this.undos = [];
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
/** Work after the commit. It cannot fail the operation, so a throw becomes what `onError` makes of it. */
|
|
69
|
+
async settle(name, run, onError) {
|
|
70
|
+
this.expect('committed', name);
|
|
71
|
+
try {
|
|
72
|
+
return await run();
|
|
73
|
+
}
|
|
74
|
+
catch (cause) {
|
|
75
|
+
return onError(cause);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
async rollBack(step, cause) {
|
|
79
|
+
this.phase = 'failed';
|
|
80
|
+
const undone = [];
|
|
81
|
+
for (const undo of this.undos.reverse())
|
|
82
|
+
undone.push(await undo.run());
|
|
83
|
+
this.undos = [];
|
|
84
|
+
return this.fail({ step, cause, undone });
|
|
85
|
+
}
|
|
86
|
+
expect(phase, name) {
|
|
87
|
+
if (this.phase !== phase) {
|
|
88
|
+
throw new Error(`"${name}" ran while the operation was ${this.phase}, but it may only run while it is ${phase}. This is a bug in @agentic-kit/session; report it.`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
exports.Operation = Operation;
|