@feltdb/feltkit 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 +21 -0
- package/README.md +22 -2
- package/bin/feltkit.js +3 -0
- package/dist/app.d.ts +247 -0
- package/dist/app.js +181 -0
- package/dist/app.js.map +1 -0
- package/dist/cli/deploy.d.ts +8 -0
- package/dist/cli/deploy.js +125 -0
- package/dist/cli/deploy.js.map +1 -0
- package/dist/cli/main.d.ts +9 -0
- package/dist/cli/main.js +581 -0
- package/dist/cli/main.js.map +1 -0
- package/dist/cli/output.d.ts +18 -0
- package/dist/cli/output.js +87 -0
- package/dist/cli/output.js.map +1 -0
- package/dist/cli/skills.d.ts +39 -0
- package/dist/cli/skills.js +125 -0
- package/dist/cli/skills.js.map +1 -0
- package/dist/client/index.d.ts +125 -0
- package/dist/client/index.js +306 -0
- package/dist/client/index.js.map +1 -0
- package/dist/dashboard/assets/index--1Tqbv_A.css +1 -0
- package/dist/dashboard/assets/index-Bm4qCYpv.js +15 -0
- package/dist/dashboard/index.html +16 -0
- package/dist/errors.d.ts +33 -0
- package/dist/errors.js +56 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/react/index.d.ts +40 -0
- package/dist/react/index.js +85 -0
- package/dist/react/index.js.map +1 -0
- package/dist/runtime/activity.d.ts +110 -0
- package/dist/runtime/activity.js +113 -0
- package/dist/runtime/activity.js.map +1 -0
- package/dist/runtime/agent.d.ts +353 -0
- package/dist/runtime/agent.js +1345 -0
- package/dist/runtime/agent.js.map +1 -0
- package/dist/runtime/authority.d.ts +98 -0
- package/dist/runtime/authority.js +240 -0
- package/dist/runtime/authority.js.map +1 -0
- package/dist/runtime/changes.d.ts +42 -0
- package/dist/runtime/changes.js +85 -0
- package/dist/runtime/changes.js.map +1 -0
- package/dist/runtime/content.d.ts +33 -0
- package/dist/runtime/content.js +46 -0
- package/dist/runtime/content.js.map +1 -0
- package/dist/runtime/context.d.ts +76 -0
- package/dist/runtime/context.js +2 -0
- package/dist/runtime/context.js.map +1 -0
- package/dist/runtime/credentials.d.ts +42 -0
- package/dist/runtime/credentials.js +32 -0
- package/dist/runtime/credentials.js.map +1 -0
- package/dist/runtime/data.d.ts +145 -0
- package/dist/runtime/data.js +439 -0
- package/dist/runtime/data.js.map +1 -0
- package/dist/runtime/decisions.d.ts +131 -0
- package/dist/runtime/decisions.js +301 -0
- package/dist/runtime/decisions.js.map +1 -0
- package/dist/runtime/environment.d.ts +36 -0
- package/dist/runtime/environment.js +73 -0
- package/dist/runtime/environment.js.map +1 -0
- package/dist/runtime/idempotency.d.ts +37 -0
- package/dist/runtime/idempotency.js +88 -0
- package/dist/runtime/idempotency.js.map +1 -0
- package/dist/runtime/instance.d.ts +82 -0
- package/dist/runtime/instance.js +253 -0
- package/dist/runtime/instance.js.map +1 -0
- package/dist/runtime/integrations.d.ts +120 -0
- package/dist/runtime/integrations.js +412 -0
- package/dist/runtime/integrations.js.map +1 -0
- package/dist/runtime/loader.d.ts +15 -0
- package/dist/runtime/loader.js +121 -0
- package/dist/runtime/loader.js.map +1 -0
- package/dist/runtime/projects.d.ts +52 -0
- package/dist/runtime/projects.js +141 -0
- package/dist/runtime/projects.js.map +1 -0
- package/dist/runtime/readiness.d.ts +29 -0
- package/dist/runtime/readiness.js +154 -0
- package/dist/runtime/readiness.js.map +1 -0
- package/dist/runtime/runtime.d.ts +227 -0
- package/dist/runtime/runtime.js +664 -0
- package/dist/runtime/runtime.js.map +1 -0
- package/dist/runtime/services.d.ts +327 -0
- package/dist/runtime/services.js +686 -0
- package/dist/runtime/services.js.map +1 -0
- package/dist/runtime/sessions.d.ts +44 -0
- package/dist/runtime/sessions.js +109 -0
- package/dist/runtime/sessions.js.map +1 -0
- package/dist/schema.d.ts +56 -0
- package/dist/schema.js +44 -0
- package/dist/schema.js.map +1 -0
- package/dist/server/admin.d.ts +141 -0
- package/dist/server/admin.js +828 -0
- package/dist/server/admin.js.map +1 -0
- package/dist/server/http.d.ts +19 -0
- package/dist/server/http.js +99 -0
- package/dist/server/http.js.map +1 -0
- package/dist/server/index.d.ts +54 -0
- package/dist/server/index.js +305 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/management.d.ts +12 -0
- package/dist/server/management.js +177 -0
- package/dist/server/management.js.map +1 -0
- package/dist/server/mcp.d.ts +214 -0
- package/dist/server/mcp.js +135 -0
- package/dist/server/mcp.js.map +1 -0
- package/dist/server/operators.d.ts +77 -0
- package/dist/server/operators.js +162 -0
- package/dist/server/operators.js.map +1 -0
- package/dist/server/project.d.ts +60 -0
- package/dist/server/project.js +344 -0
- package/dist/server/project.js.map +1 -0
- package/dist/server/public.d.ts +14 -0
- package/dist/server/public.js +15 -0
- package/dist/server/public.js.map +1 -0
- package/dist/testing/index.d.ts +20 -0
- package/dist/testing/index.js +65 -0
- package/dist/testing/index.js.map +1 -0
- package/package.json +91 -4
- package/skills/.claude-plugin/marketplace.json +16 -0
- package/skills/.claude-plugin/plugin.json +24 -0
- package/skills/README.md +44 -0
- package/skills/feltkit-agent/SKILL.md +84 -0
- package/skills/feltkit-build/SKILL.md +143 -0
- package/skills/feltkit-data/SKILL.md +79 -0
- package/skills/feltkit-operations/SKILL.md +72 -0
- package/skills/feltkit-services/SKILL.md +102 -0
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `ctx.db`: the thinnest correct layer over FeltDB.
|
|
3
|
+
*
|
|
4
|
+
* Reads map one-to-one onto FeltDB:
|
|
5
|
+
* get → collection.get(id)
|
|
6
|
+
* find → collection.find(equalityFilter, { orderBy, limit })
|
|
7
|
+
* query → db.query({ collection, where, orderBy, limit, cursor }) (bounded page)
|
|
8
|
+
* count → collection.find(filter).length
|
|
9
|
+
*
|
|
10
|
+
* Writes in a mutation are buffered and committed as ONE FeltDB atomic transaction
|
|
11
|
+
* when the handler returns:
|
|
12
|
+
* - inserts are `requireAbsent` creates,
|
|
13
|
+
* - updates and deletes carry `ifVersion` = the version this mutation observed,
|
|
14
|
+
* - records the mutation read with `get` but did not write become preconditions.
|
|
15
|
+
* If anything the mutation read by id changed before commit, FeltDB rejects the whole
|
|
16
|
+
* transaction and nothing is written (CONFLICT). If the handler throws, nothing is
|
|
17
|
+
* written. Reads inside a mutation see the mutation's own pending writes.
|
|
18
|
+
*
|
|
19
|
+
* Reads through `find`/`query`/`count` are fenced with FeltDB read bases: the basis
|
|
20
|
+
* for the read's equality predicate is captured before the read and re-checked at
|
|
21
|
+
* commit, so a concurrent insert, update, or delete of any record matching it (a
|
|
22
|
+
* phantom included) fails the commit with CONFLICT. Range clauses widen the fence to
|
|
23
|
+
* their equality-only superset: extra conflicts, never missed ones. A read with no
|
|
24
|
+
* equality clause fences the collection's change marker (see COLLECTION_MARKERS),
|
|
25
|
+
* which detects every commit made through a Writer, but not writes made through the
|
|
26
|
+
* `ctx.unsafe.feltdb` escape hatch.
|
|
27
|
+
*
|
|
28
|
+
* Not provided: a consistent snapshot across reads within one call. A mutation that
|
|
29
|
+
* only reads commits nothing, so nothing is checked; queries are not fenced.
|
|
30
|
+
*/
|
|
31
|
+
import { classifyError, FeltDBErrorCode, FeltDBServiceError } from "@feltdb/core";
|
|
32
|
+
import { randomBytes } from "node:crypto";
|
|
33
|
+
import { FeltKitError } from "../errors.js";
|
|
34
|
+
import { validate } from "../schema.js";
|
|
35
|
+
const MAX_FIND = 10_000;
|
|
36
|
+
/**
|
|
37
|
+
* Per-collection change markers. Every commit through a Writer touches the marker of
|
|
38
|
+
* each collection it writes (unguarded, so concurrent writers never conflict on it).
|
|
39
|
+
* A read with no equality clause fences the marker, because FeltDB fences only
|
|
40
|
+
* predicates that name at least one field.
|
|
41
|
+
*/
|
|
42
|
+
export const COLLECTION_MARKERS = "_feltkit_collections";
|
|
43
|
+
export class CollectionResolver {
|
|
44
|
+
collections;
|
|
45
|
+
constructor(collections) {
|
|
46
|
+
this.collections = collections;
|
|
47
|
+
}
|
|
48
|
+
resolve(ref) {
|
|
49
|
+
const name = typeof ref === "string" ? ref : ref?.name;
|
|
50
|
+
const definition = name ? this.collections.get(name) : undefined;
|
|
51
|
+
if (!definition) {
|
|
52
|
+
throw new FeltKitError("NOT_FOUND", `Collection "${String(name)}" is not declared by this application.`, {
|
|
53
|
+
hint: `Declare it with app.collection("${String(name)}", { ... }). Platform collections are not reachable through ctx.db.`,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return definition;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export function newId() {
|
|
60
|
+
return `${Date.now().toString(36)}${randomBytes(6).toString("hex")}`;
|
|
61
|
+
}
|
|
62
|
+
function stripSystem(value) {
|
|
63
|
+
const { id: _id, __version: _v, ...rest } = value;
|
|
64
|
+
return rest;
|
|
65
|
+
}
|
|
66
|
+
function validateRecord(definition, value) {
|
|
67
|
+
const result = validate(definition.schema, value);
|
|
68
|
+
if (!result.ok) {
|
|
69
|
+
throw new FeltKitError("VALIDATION_FAILED", `Invalid "${definition.name}" record: ${result.issues.map((i) => `${i.path}: ${i.message}`).join("; ")}`, {
|
|
70
|
+
details: { collection: definition.name, issues: result.issues },
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function compare(a, b) {
|
|
75
|
+
if (a === b)
|
|
76
|
+
return 0;
|
|
77
|
+
if (a === undefined || a === null)
|
|
78
|
+
return -1;
|
|
79
|
+
if (b === undefined || b === null)
|
|
80
|
+
return 1;
|
|
81
|
+
if (typeof a === "number" && typeof b === "number")
|
|
82
|
+
return a - b;
|
|
83
|
+
return String(a) < String(b) ? -1 : 1;
|
|
84
|
+
}
|
|
85
|
+
function matchesWhere(record, where) {
|
|
86
|
+
return where.every((clause) => {
|
|
87
|
+
const value = record[clause.field];
|
|
88
|
+
if ("eq" in clause && clause.eq !== undefined && value !== clause.eq)
|
|
89
|
+
return false;
|
|
90
|
+
if ("neq" in clause && clause.neq !== undefined && value === clause.neq)
|
|
91
|
+
return false;
|
|
92
|
+
if (clause.lt !== undefined && !(compare(value, clause.lt) < 0))
|
|
93
|
+
return false;
|
|
94
|
+
if (clause.lte !== undefined && !(compare(value, clause.lte) <= 0))
|
|
95
|
+
return false;
|
|
96
|
+
if (clause.gt !== undefined && !(compare(value, clause.gt) > 0))
|
|
97
|
+
return false;
|
|
98
|
+
if (clause.gte !== undefined && !(compare(value, clause.gte) >= 0))
|
|
99
|
+
return false;
|
|
100
|
+
return true;
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
function sortRecords(records, orderBy = []) {
|
|
104
|
+
return [...records].sort((a, b) => {
|
|
105
|
+
for (const order of orderBy) {
|
|
106
|
+
const c = compare(a[order.field], b[order.field]);
|
|
107
|
+
if (c !== 0)
|
|
108
|
+
return order.direction === "desc" ? -c : c;
|
|
109
|
+
}
|
|
110
|
+
return compare(a.id, b.id);
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
function equalityWhere(filter) {
|
|
114
|
+
return Object.entries(filter ?? {})
|
|
115
|
+
.filter(([, value]) => value !== undefined)
|
|
116
|
+
.map(([field, value]) => ({ field, eq: value }));
|
|
117
|
+
}
|
|
118
|
+
function checkLimit(limit, name) {
|
|
119
|
+
if (limit !== undefined && (!Number.isInteger(limit) || limit < 1 || limit > MAX_FIND)) {
|
|
120
|
+
throw new FeltKitError("INVALID_ARGUMENT", `${name} limit must be an integer between 1 and ${MAX_FIND}.`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/** Read-only access. Every collection read is recorded so subscribers can be invalidated. */
|
|
124
|
+
export class Reader {
|
|
125
|
+
db;
|
|
126
|
+
resolver;
|
|
127
|
+
reads = new Set();
|
|
128
|
+
constructor(db, resolver) {
|
|
129
|
+
this.db = db;
|
|
130
|
+
this.resolver = resolver;
|
|
131
|
+
}
|
|
132
|
+
track(ref) {
|
|
133
|
+
const definition = this.resolver.resolve(ref);
|
|
134
|
+
this.reads.add(definition.name);
|
|
135
|
+
return definition;
|
|
136
|
+
}
|
|
137
|
+
async get(ref, id) {
|
|
138
|
+
const definition = this.track(ref);
|
|
139
|
+
if (typeof id !== "string" || !id)
|
|
140
|
+
throw new FeltKitError("INVALID_ARGUMENT", "get() needs a record id.");
|
|
141
|
+
return (await this.db.collection(definition.name).get(id)) ?? null;
|
|
142
|
+
}
|
|
143
|
+
async find(ref, filter, options = {}) {
|
|
144
|
+
const definition = this.track(ref);
|
|
145
|
+
checkLimit(options.limit, "find()");
|
|
146
|
+
const clean = Object.fromEntries(Object.entries(filter ?? {}).filter(([, v]) => v !== undefined));
|
|
147
|
+
const findOptions = {
|
|
148
|
+
...(options.orderBy ? { orderBy: options.orderBy } : {}),
|
|
149
|
+
...(options.limit ? { limit: options.limit } : {}),
|
|
150
|
+
};
|
|
151
|
+
return this.db.collection(definition.name).find(clean, findOptions);
|
|
152
|
+
}
|
|
153
|
+
async query(ref, request) {
|
|
154
|
+
const definition = this.track(ref);
|
|
155
|
+
checkLimit(request?.limit, "query()");
|
|
156
|
+
if (!request?.limit)
|
|
157
|
+
throw new FeltKitError("INVALID_ARGUMENT", "query() needs a limit; FeltDB queries are bounded.");
|
|
158
|
+
const page = await this.db.query({
|
|
159
|
+
collection: definition.name,
|
|
160
|
+
...(request.where ? { where: request.where } : {}),
|
|
161
|
+
...(request.orderBy ? { orderBy: request.orderBy } : {}),
|
|
162
|
+
limit: request.limit,
|
|
163
|
+
...(request.cursor ? { cursor: request.cursor } : {}),
|
|
164
|
+
});
|
|
165
|
+
return { records: page.records, exhausted: page.exhausted, ...(page.nextCursor ? { nextCursor: page.nextCursor } : {}) };
|
|
166
|
+
}
|
|
167
|
+
async count(ref, filter) {
|
|
168
|
+
return (await this.find(ref, filter)).length;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
/** Mutation access: reads see pending writes; writes commit together. */
|
|
172
|
+
export class Writer extends Reader {
|
|
173
|
+
#pending = new Map();
|
|
174
|
+
/** Committed versions observed by id (null = observed absent). */
|
|
175
|
+
#observed = new Map();
|
|
176
|
+
/** Read bases for predicate reads (find/query/count), keyed by predicate. The first capture wins. */
|
|
177
|
+
#fences = new Map();
|
|
178
|
+
#committed = false;
|
|
179
|
+
/**
|
|
180
|
+
* Fence a predicate read: capture FeltDB's read basis for "records of `collection`
|
|
181
|
+
* whose fields equal `where`" *before* reading, and carry it to commit. The commit
|
|
182
|
+
* re-evaluates the basis inside FeltDB's commit lock, so a record that started or
|
|
183
|
+
* stopped matching, or changed, since this read fails the whole transaction.
|
|
184
|
+
* Range clauses are dropped from the fenced predicate, which fences a superset:
|
|
185
|
+
* more conservative, never weaker.
|
|
186
|
+
*/
|
|
187
|
+
async #fence(collection, where) {
|
|
188
|
+
const equality = where
|
|
189
|
+
.filter((clause) => "eq" in clause && clause.eq !== undefined && [clause.neq, clause.lt, clause.lte, clause.gt, clause.gte].every((v) => v === undefined))
|
|
190
|
+
.map((clause) => ({ field: clause.field, eq: clause.eq }))
|
|
191
|
+
.sort((a, b) => a.field.localeCompare(b.field));
|
|
192
|
+
const key = JSON.stringify([collection, equality]);
|
|
193
|
+
if (this.#fences.has(key))
|
|
194
|
+
return;
|
|
195
|
+
const reads = equality.length
|
|
196
|
+
? { predicates: [{ collection, where: equality }] }
|
|
197
|
+
: { records: [{ collection: COLLECTION_MARKERS, id: collection }] };
|
|
198
|
+
try {
|
|
199
|
+
this.#fences.set(key, await this.db.readBasis(reads));
|
|
200
|
+
}
|
|
201
|
+
catch (error) {
|
|
202
|
+
throw new FeltKitError("UNAVAILABLE", `This FeltDB authority cannot fence reads, so the mutation cannot guarantee its reads are current: ${error.message}`, { cause: error });
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
#key(collection, id) {
|
|
206
|
+
return `${collection}\u0000${id}`;
|
|
207
|
+
}
|
|
208
|
+
get pendingCount() {
|
|
209
|
+
return this.#pending.size;
|
|
210
|
+
}
|
|
211
|
+
#pendingFor(collection) {
|
|
212
|
+
return [...this.#pending.values()].filter((p) => p.collection === collection);
|
|
213
|
+
}
|
|
214
|
+
async #committedGet(collection, id) {
|
|
215
|
+
const record = (await this.db.collection(collection).get(id)) ?? null;
|
|
216
|
+
const key = this.#key(collection, id);
|
|
217
|
+
if (!this.#observed.has(key))
|
|
218
|
+
this.#observed.set(key, { collection, id, version: record ? record.__version : null });
|
|
219
|
+
return record;
|
|
220
|
+
}
|
|
221
|
+
#overlayOne(pending, committed) {
|
|
222
|
+
if (!pending)
|
|
223
|
+
return committed;
|
|
224
|
+
if (pending.op === "delete")
|
|
225
|
+
return null;
|
|
226
|
+
return { ...pending.value, id: pending.id, __version: committed?.__version ?? 0 };
|
|
227
|
+
}
|
|
228
|
+
async get(ref, id) {
|
|
229
|
+
const definition = this.track(ref);
|
|
230
|
+
if (typeof id !== "string" || !id)
|
|
231
|
+
throw new FeltKitError("INVALID_ARGUMENT", "get() needs a record id.");
|
|
232
|
+
const pending = this.#pending.get(this.#key(definition.name, id));
|
|
233
|
+
if (pending?.op === "insert")
|
|
234
|
+
return this.#overlayOne(pending, null);
|
|
235
|
+
return this.#overlayOne(pending, await this.#committedGet(definition.name, id));
|
|
236
|
+
}
|
|
237
|
+
async #overlayAll(name) {
|
|
238
|
+
const committed = await this.db.collection(name).find({});
|
|
239
|
+
const byId = new Map(committed.map((record) => [record.id, record]));
|
|
240
|
+
for (const pending of this.#pendingFor(name)) {
|
|
241
|
+
const merged = this.#overlayOne(pending, byId.get(pending.id) ?? null);
|
|
242
|
+
if (merged)
|
|
243
|
+
byId.set(pending.id, merged);
|
|
244
|
+
else
|
|
245
|
+
byId.delete(pending.id);
|
|
246
|
+
}
|
|
247
|
+
return [...byId.values()];
|
|
248
|
+
}
|
|
249
|
+
async find(ref, filter, options = {}) {
|
|
250
|
+
const definition = this.resolver.resolve(ref);
|
|
251
|
+
this.#assertOpen();
|
|
252
|
+
await this.#fence(definition.name, equalityWhere(filter));
|
|
253
|
+
if (this.#pendingFor(definition.name).length === 0)
|
|
254
|
+
return super.find(ref, filter, options);
|
|
255
|
+
this.track(ref);
|
|
256
|
+
checkLimit(options.limit, "find()");
|
|
257
|
+
const matched = sortRecords((await this.#overlayAll(definition.name)).filter((r) => matchesWhere(r, equalityWhere(filter))), options.orderBy);
|
|
258
|
+
return options.limit ? matched.slice(0, options.limit) : matched;
|
|
259
|
+
}
|
|
260
|
+
async query(ref, request) {
|
|
261
|
+
const definition = this.resolver.resolve(ref);
|
|
262
|
+
this.#assertOpen();
|
|
263
|
+
await this.#fence(definition.name, request?.where ?? []);
|
|
264
|
+
if (this.#pendingFor(definition.name).length === 0)
|
|
265
|
+
return super.query(ref, request);
|
|
266
|
+
this.track(ref);
|
|
267
|
+
checkLimit(request?.limit, "query()");
|
|
268
|
+
if (request.cursor) {
|
|
269
|
+
throw new FeltKitError("INVALID_ARGUMENT", "query() cursors cannot be combined with this mutation's own pending writes to the same collection.", {
|
|
270
|
+
hint: "Page before writing, or read with find().",
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
const matched = sortRecords((await this.#overlayAll(definition.name)).filter((r) => matchesWhere(r, request.where ?? [])), request.orderBy);
|
|
274
|
+
return { records: matched.slice(0, request.limit), exhausted: matched.length <= request.limit };
|
|
275
|
+
}
|
|
276
|
+
#assertOpen() {
|
|
277
|
+
if (this.#committed)
|
|
278
|
+
throw new FeltKitError("INTERNAL", "This mutation has already committed; ctx.db cannot be used after the handler returns.");
|
|
279
|
+
}
|
|
280
|
+
async insert(ref, value) {
|
|
281
|
+
this.#assertOpen();
|
|
282
|
+
const definition = this.track(ref);
|
|
283
|
+
if (!value || typeof value !== "object")
|
|
284
|
+
throw new FeltKitError("INVALID_ARGUMENT", "insert() needs a record object.");
|
|
285
|
+
const fields = stripSystem(value);
|
|
286
|
+
validateRecord(definition, fields);
|
|
287
|
+
const id = newId();
|
|
288
|
+
this.#pending.set(this.#key(definition.name, id), { collection: definition.name, id, op: "insert", value: fields });
|
|
289
|
+
return id;
|
|
290
|
+
}
|
|
291
|
+
async #existing(definition, id) {
|
|
292
|
+
if (typeof id !== "string" || !id)
|
|
293
|
+
throw new FeltKitError("INVALID_ARGUMENT", "A record id is required.");
|
|
294
|
+
const key = this.#key(definition.name, id);
|
|
295
|
+
const pending = this.#pending.get(key);
|
|
296
|
+
if (pending?.op === "delete")
|
|
297
|
+
throw new FeltKitError("NOT_FOUND", `${definition.name}/${id} was deleted earlier in this mutation.`);
|
|
298
|
+
if (pending)
|
|
299
|
+
return { current: pending.value ?? {}, pending, ...(pending.baseVersion !== undefined ? { base: pending.baseVersion } : {}) };
|
|
300
|
+
const committed = await this.#committedGet(definition.name, id);
|
|
301
|
+
if (!committed)
|
|
302
|
+
throw new FeltKitError("NOT_FOUND", `${definition.name}/${id} does not exist.`, { details: { collection: definition.name, id } });
|
|
303
|
+
return { current: stripSystem(committed), base: committed.__version };
|
|
304
|
+
}
|
|
305
|
+
async patch(ref, id, fields) {
|
|
306
|
+
this.#assertOpen();
|
|
307
|
+
const definition = this.track(ref);
|
|
308
|
+
const { current, pending, base } = await this.#existing(definition, id);
|
|
309
|
+
const merged = { ...current, ...stripSystem(fields ?? {}) };
|
|
310
|
+
for (const [key, value] of Object.entries(fields ?? {}))
|
|
311
|
+
if (value === undefined)
|
|
312
|
+
delete merged[key];
|
|
313
|
+
validateRecord(definition, merged);
|
|
314
|
+
this.#pending.set(this.#key(definition.name, id), {
|
|
315
|
+
collection: definition.name,
|
|
316
|
+
id,
|
|
317
|
+
op: pending?.op === "insert" ? "insert" : "update",
|
|
318
|
+
value: merged,
|
|
319
|
+
...(base !== undefined ? { baseVersion: base } : {}),
|
|
320
|
+
});
|
|
321
|
+
return { ...merged, id, __version: pending?.op === "insert" ? 1 : (base ?? 0) + 1 };
|
|
322
|
+
}
|
|
323
|
+
async replace(ref, id, value) {
|
|
324
|
+
this.#assertOpen();
|
|
325
|
+
const definition = this.track(ref);
|
|
326
|
+
const { pending, base } = await this.#existing(definition, id);
|
|
327
|
+
const fields = stripSystem(value ?? {});
|
|
328
|
+
validateRecord(definition, fields);
|
|
329
|
+
this.#pending.set(this.#key(definition.name, id), {
|
|
330
|
+
collection: definition.name,
|
|
331
|
+
id,
|
|
332
|
+
op: pending?.op === "insert" ? "insert" : "update",
|
|
333
|
+
value: fields,
|
|
334
|
+
...(base !== undefined ? { baseVersion: base } : {}),
|
|
335
|
+
});
|
|
336
|
+
return { ...fields, id, __version: pending?.op === "insert" ? 1 : (base ?? 0) + 1 };
|
|
337
|
+
}
|
|
338
|
+
async delete(ref, id) {
|
|
339
|
+
this.#assertOpen();
|
|
340
|
+
const definition = this.track(ref);
|
|
341
|
+
const { pending, base } = await this.#existing(definition, id);
|
|
342
|
+
const key = this.#key(definition.name, id);
|
|
343
|
+
if (pending?.op === "insert") {
|
|
344
|
+
this.#pending.delete(key);
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
this.#pending.set(key, { collection: definition.name, id, op: "delete", ...(base !== undefined ? { baseVersion: base } : {}) });
|
|
348
|
+
}
|
|
349
|
+
/** Commit every pending write in one FeltDB transaction. */
|
|
350
|
+
/**
|
|
351
|
+
* `keyed`: the transaction id was derived from the caller's idempotency key, so FeltDB's
|
|
352
|
+
* IDEMPOTENCY_CONFLICT means an earlier attempt under that key already committed.
|
|
353
|
+
*/
|
|
354
|
+
async commit(transactionId, options = {}) {
|
|
355
|
+
this.#assertOpen();
|
|
356
|
+
this.#committed = true;
|
|
357
|
+
const writes = [];
|
|
358
|
+
const operations = [];
|
|
359
|
+
for (const pending of this.#pending.values()) {
|
|
360
|
+
if (pending.op === "insert") {
|
|
361
|
+
operations.push({ collection: pending.collection, id: pending.id, requireAbsent: true, value: { ...pending.value, id: pending.id } });
|
|
362
|
+
writes.push({ collection: pending.collection, id: pending.id, op: "insert" });
|
|
363
|
+
}
|
|
364
|
+
else if (pending.op === "update") {
|
|
365
|
+
operations.push({ collection: pending.collection, id: pending.id, ifVersion: pending.baseVersion, value: { ...pending.value, id: pending.id } });
|
|
366
|
+
writes.push({ collection: pending.collection, id: pending.id, op: "update", expectedVersion: pending.baseVersion });
|
|
367
|
+
}
|
|
368
|
+
else {
|
|
369
|
+
operations.push({ collection: pending.collection, id: pending.id, ifVersion: pending.baseVersion });
|
|
370
|
+
writes.push({ collection: pending.collection, id: pending.id, op: "delete", expectedVersion: pending.baseVersion });
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
if (operations.length === 0)
|
|
374
|
+
return { transactionId, writes: [], operationIds: [] };
|
|
375
|
+
for (const collection of new Set(writes.map((w) => w.collection))) {
|
|
376
|
+
operations.push({ collection: COLLECTION_MARKERS, id: collection, value: { id: collection, transactionId, changedAt: Date.now() } });
|
|
377
|
+
}
|
|
378
|
+
const preconditions = [];
|
|
379
|
+
for (const [key, observed] of this.#observed) {
|
|
380
|
+
if (this.#pending.has(key))
|
|
381
|
+
continue;
|
|
382
|
+
preconditions.push(observed.version === null
|
|
383
|
+
? { collection: observed.collection, id: observed.id, requireAbsent: true }
|
|
384
|
+
: { collection: observed.collection, id: observed.id, ifVersion: observed.version });
|
|
385
|
+
}
|
|
386
|
+
try {
|
|
387
|
+
const fences = [...this.#fences.values()];
|
|
388
|
+
const result = await this.db.transaction({ transactionId, operations, ...(preconditions.length ? { preconditions } : {}), ...(fences.length ? { fences } : {}) });
|
|
389
|
+
return {
|
|
390
|
+
transactionId: result.transactionId,
|
|
391
|
+
writes,
|
|
392
|
+
operationIds: result.operationIds ?? [],
|
|
393
|
+
...(result.commitRevision !== undefined ? { commitRevision: result.commitRevision } : {}),
|
|
394
|
+
...(result.revisionId ? { revisionId: result.revisionId } : {}),
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
catch (error) {
|
|
398
|
+
throw commitError(error, transactionId, options.keyed === true);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* A refused or uncertain commit, by FeltDB's typed code and its own classification
|
|
404
|
+
* (every runtime and transport raises a FeltDBServiceError with a stable `code`; a lost
|
|
405
|
+
* race carries its structured `failure`). Anything else passes through unchanged.
|
|
406
|
+
*/
|
|
407
|
+
function commitError(error, transactionId, keyed) {
|
|
408
|
+
if (!(error instanceof FeltDBServiceError))
|
|
409
|
+
return error;
|
|
410
|
+
const details = { transactionId, feltdbCode: error.code, ...(error.requestId ? { requestId: error.requestId } : {}) };
|
|
411
|
+
if (keyed && error.code === FeltDBErrorCode.IDEMPOTENCY_CONFLICT) {
|
|
412
|
+
// The transaction id is the caller's idempotency key: an earlier attempt under it
|
|
413
|
+
// committed (its answer was lost, so AppPort released its claim). Nothing was written now.
|
|
414
|
+
return new FeltKitError("OUTCOME_UNKNOWN", "An earlier attempt with this idempotency key already committed; its result was not returned. Nothing was written again.", {
|
|
415
|
+
details: { ...details, committed: true },
|
|
416
|
+
hint: "Read the records this mutation writes. Do not retry with a new idempotency key: that would apply it twice.",
|
|
417
|
+
cause: error,
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
if (error.code === FeltDBErrorCode.PRECONDITION_FAILED) {
|
|
421
|
+
return new FeltKitError("CONFLICT", "A record this mutation read changed before it could commit. Nothing was written; it is safe to retry.", {
|
|
422
|
+
details: { ...details, failure: error.failure ?? null },
|
|
423
|
+
cause: error,
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
if (error.code === FeltDBErrorCode.COMMIT_OUTCOME_UNKNOWN || error.code === FeltDBErrorCode.DURABILITY_FAILURE) {
|
|
427
|
+
// FeltDB classifies both as requires_reconciliation: the commit may have landed.
|
|
428
|
+
return new FeltKitError("OUTCOME_UNKNOWN", `FeltDB could not confirm the commit (${error.code}); the mutation may or may not have been written.`, {
|
|
429
|
+
details,
|
|
430
|
+
hint: "Read the records it writes before acting again. FeltKit does not retry it.",
|
|
431
|
+
cause: error,
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
if (classifyError(error.code) === "retryable") {
|
|
435
|
+
return new FeltKitError("UNAVAILABLE", `FeltDB refused the commit (${error.code}); nothing was written. It is safe to retry.`, { details, cause: error });
|
|
436
|
+
}
|
|
437
|
+
return error;
|
|
438
|
+
}
|
|
439
|
+
//# sourceMappingURL=data.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"data.js","sourceRoot":"","sources":["../../src/runtime/data.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,kBAAkB,EAAqB,MAAM,cAAc,CAAC;AACrG,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAA6D,MAAM,cAAc,CAAC;AA0EnG,MAAM,QAAQ,GAAG,MAAM,CAAC;AAExB;;;;;GAKG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,sBAAsB,CAAC;AAEzD,MAAM,OAAO,kBAAkB;IACA;IAA7B,YAA6B,WAAsD;QAAtD,gBAAW,GAAX,WAAW,CAA2C;IAAG,CAAC;IAEvF,OAAO,CAAC,GAAkB;QACxB,MAAM,IAAI,GAAG,OAAO,GAAG,KAAK,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,IAAI,CAAC;QACvD,MAAM,UAAU,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;QACjE,IAAI,CAAC,UAAU,EAAE,CAAC;YAChB,MAAM,IAAI,YAAY,CAAC,WAAW,EAAE,eAAe,MAAM,CAAC,IAAI,CAAC,wCAAwC,EAAE;gBACvG,IAAI,EAAE,mCAAmC,MAAM,CAAC,IAAI,CAAC,qEAAqE;aAC3H,CAAC,CAAC;QACL,CAAC;QACD,OAAO,UAAU,CAAC;IACpB,CAAC;CACF;AAED,MAAM,UAAU,KAAK;IACnB,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AAI,YAAY,CAAC,UAAU,EAAE,uGAAuG,EAAE;YAC3I,OAAO,EAAE,EAAE,GAAG,OAAO,EAAE,OAAO,EAAG,KAA+B,CAAC,OAAO,IAAI,IAAI,EAAE;YAClF,KAAK,EAAE,KAAK;SACb,CAAC,CAAC;IACL,CAAC;IACD,IAAI,KAAK,CAAC,IAAI,KAAK,eAAe,CAAC,sBAAsB,IAAI,KAAK,CAAC,IAAI,KAAK,eAAe,CAAC,kBAAkB,EAAE,CAAC;QAC/G,iFAAiF;QACjF,OAAO,IAAI,YAAY,CAAC,iBAAiB,EAAE,wCAAwC,KAAK,CAAC,IAAI,mDAAmD,EAAE;YAChJ,OAAO;YACP,IAAI,EAAE,4EAA4E;YAClF,KAAK,EAAE,KAAK;SACb,CAAC,CAAC;IACL,CAAC;IACD,IAAI,aAAa,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,WAAW,EAAE,CAAC;QAC9C,OAAO,IAAI,YAAY,CAAC,aAAa,EAAE,8BAA8B,KAAK,CAAC,IAAI,8CAA8C,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;IAC5J,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model decisions, on FeltDB's provider-neutral `DecisionRuntime` contract.
|
|
3
|
+
*
|
|
4
|
+
* FeltDB defines what a decision is (binary / choice / score over an authorized context),
|
|
5
|
+
* how a runtime reports what it can do (`capabilities()`), and who made it (`metadata()`).
|
|
6
|
+
* FeltDB never calls a model, and AppPort Services' Intelligence service only stores which
|
|
7
|
+
* provider, model, endpoint, and credential reference an environment uses. This module is
|
|
8
|
+
* the piece in between:
|
|
9
|
+
*
|
|
10
|
+
* - Any `DecisionRuntime` works: `feltkit({ agent: { decisions } })` takes one directly
|
|
11
|
+
* (for example `@feltdb/webllm`'s `WebLLMDecisionRuntime` with an engine factory).
|
|
12
|
+
* - Otherwise the environment's Intelligence selection picks a server-side runtime by
|
|
13
|
+
* protocol: OpenAI chat completions (`openai`, `openai-compatible`, which covers
|
|
14
|
+
* local servers such as Ollama, vLLM, and llama.cpp) or Anthropic Messages.
|
|
15
|
+
* - Credentials are resolved from FeltKit's custody (FELTKIT_CREDENTIAL_<ID>) inside the
|
|
16
|
+
* call and never leave the server: not in configuration views, errors, logs, or
|
|
17
|
+
* records.
|
|
18
|
+
* - Nothing falls back silently. A missing selection, missing credential, timeout,
|
|
19
|
+
* refused credential, provider error, malformed answer, or unsupported decision kind
|
|
20
|
+
* is an error that says what to fix.
|
|
21
|
+
*
|
|
22
|
+
* A decision is a model's *choice*, recorded with its runtime and context. It is never
|
|
23
|
+
* evidence that anything happened: see runtime/agent.ts, which turns a choice into a
|
|
24
|
+
* proposal that goes through the action lifecycle.
|
|
25
|
+
*/
|
|
26
|
+
import type { DecisionBatch, DecisionBatchEvaluation, DecisionRequest, DecisionResult, DecisionRuntime, DecisionRuntimeCapabilities, DecisionRuntimeMetadata } from "@feltdb/core";
|
|
27
|
+
import { FeltKitError } from "../errors.js";
|
|
28
|
+
import type { FeltKitRuntime } from "./runtime.js";
|
|
29
|
+
export type { DecisionRuntime, DecisionRequest, DecisionResult, DecisionRuntimeCapabilities, DecisionRuntimeMetadata } from "@feltdb/core";
|
|
30
|
+
export type ModelProblem = "not_configured" | "intelligence_disabled" | "unknown_protocol" | "credential_missing" | "timeout" | "unavailable" | "credential_refused" | "provider_error" | "malformed" | "unsupported";
|
|
31
|
+
export declare function modelError(problem: ModelProblem, message: string, hint: string, details?: Record<string, unknown>): FeltKitError;
|
|
32
|
+
export declare const DEFAULT_DECISION_TIMEOUT_MS = 20000;
|
|
33
|
+
/** Labels a decision may answer with. */
|
|
34
|
+
export declare function decisionLabels(request: DecisionRequest): string[];
|
|
35
|
+
/** The same shape of prompt FeltDB's WebLLM runtime uses: the question, the labels, and only the authorized context. */
|
|
36
|
+
export declare function decisionPrompt(request: DecisionRequest): string;
|
|
37
|
+
/** Build a FeltDB DecisionResult from one chosen label. Without a distribution, only the choice is reported. */
|
|
38
|
+
export declare function resultFor(request: DecisionRequest, selected: string, probabilities?: Record<string, number>): DecisionResult;
|
|
39
|
+
/** Match a model's answer to exactly one allowed label, or refuse it. */
|
|
40
|
+
export declare function matchLabel(answer: unknown, labels: string[], provider: string): string;
|
|
41
|
+
export interface ModelConnection {
|
|
42
|
+
provider: string;
|
|
43
|
+
model: string;
|
|
44
|
+
endpoint: string;
|
|
45
|
+
/** The resolved secret. Held only for the duration of a call. */
|
|
46
|
+
credential?: string;
|
|
47
|
+
timeoutMs?: number;
|
|
48
|
+
fetch?: typeof fetch;
|
|
49
|
+
}
|
|
50
|
+
declare abstract class HttpDecisionRuntime implements DecisionRuntime {
|
|
51
|
+
protected readonly connection: ModelConnection;
|
|
52
|
+
constructor(connection: ModelConnection);
|
|
53
|
+
abstract metadata(): DecisionRuntimeMetadata;
|
|
54
|
+
abstract capabilities(): DecisionRuntimeCapabilities;
|
|
55
|
+
abstract execute(request: DecisionRequest): Promise<DecisionResult>;
|
|
56
|
+
executeBatch(batch: DecisionBatch): Promise<DecisionBatchEvaluation[]>;
|
|
57
|
+
}
|
|
58
|
+
/** OpenAI chat completions: OpenAI itself and every OpenAI-compatible server. */
|
|
59
|
+
export declare class OpenAIChatDecisionRuntime extends HttpDecisionRuntime {
|
|
60
|
+
metadata(): DecisionRuntimeMetadata;
|
|
61
|
+
capabilities(): DecisionRuntimeCapabilities;
|
|
62
|
+
execute(request: DecisionRequest): Promise<DecisionResult>;
|
|
63
|
+
}
|
|
64
|
+
/** Anthropic Messages, with the answer forced through a tool whose input is one of the labels. */
|
|
65
|
+
export declare class AnthropicDecisionRuntime extends HttpDecisionRuntime {
|
|
66
|
+
metadata(): DecisionRuntimeMetadata;
|
|
67
|
+
capabilities(): DecisionRuntimeCapabilities;
|
|
68
|
+
execute(request: DecisionRequest): Promise<DecisionResult>;
|
|
69
|
+
}
|
|
70
|
+
/** Server-side runtimes by AppPort Services Intelligence protocol. */
|
|
71
|
+
export declare const PROTOCOL_RUNTIMES: Record<string, (connection: ModelConnection) => DecisionRuntime>;
|
|
72
|
+
/** What an environment will use to decide, with everything a person needs to fix it. Never a secret. */
|
|
73
|
+
export interface ModelStatus {
|
|
74
|
+
source: "application" | "intelligence" | "none";
|
|
75
|
+
ready: boolean;
|
|
76
|
+
provider: string | null;
|
|
77
|
+
protocol: string | null;
|
|
78
|
+
model: string | null;
|
|
79
|
+
endpoint: string | null;
|
|
80
|
+
credential: {
|
|
81
|
+
required: boolean;
|
|
82
|
+
ref: string | null;
|
|
83
|
+
variable: string | null;
|
|
84
|
+
present: boolean;
|
|
85
|
+
};
|
|
86
|
+
runtime: {
|
|
87
|
+
metadata: DecisionRuntimeMetadata;
|
|
88
|
+
capabilities: DecisionRuntimeCapabilities;
|
|
89
|
+
} | null;
|
|
90
|
+
problems: Array<{
|
|
91
|
+
problem: ModelProblem;
|
|
92
|
+
message: string;
|
|
93
|
+
fix: string;
|
|
94
|
+
}>;
|
|
95
|
+
}
|
|
96
|
+
export type DecisionRuntimeOption = DecisionRuntime | ((environment: {
|
|
97
|
+
name: string;
|
|
98
|
+
production: boolean;
|
|
99
|
+
}) => DecisionRuntime | null | undefined);
|
|
100
|
+
export interface DecisionOutcome {
|
|
101
|
+
result: DecisionResult;
|
|
102
|
+
metadata: DecisionRuntimeMetadata;
|
|
103
|
+
capabilities: DecisionRuntimeCapabilities;
|
|
104
|
+
provider: string;
|
|
105
|
+
source: "application" | "intelligence";
|
|
106
|
+
durationMs: number;
|
|
107
|
+
}
|
|
108
|
+
/** One environment's model decisions. */
|
|
109
|
+
export declare class ModelHost {
|
|
110
|
+
#private;
|
|
111
|
+
constructor(runtime: FeltKitRuntime, options?: {
|
|
112
|
+
fetch?: typeof fetch;
|
|
113
|
+
});
|
|
114
|
+
/**
|
|
115
|
+
* Select this environment's provider and model in AppPort Services Intelligence. Host
|
|
116
|
+
* code only (the CLI, tests, development Studio): production Studio does not expose it.
|
|
117
|
+
* The credential is a reference; its value lives in FELTKIT_CREDENTIAL_<ID>.
|
|
118
|
+
*/
|
|
119
|
+
select(input: {
|
|
120
|
+
provider: string;
|
|
121
|
+
model: string;
|
|
122
|
+
endpoint?: string | null;
|
|
123
|
+
credentialRef?: string;
|
|
124
|
+
}): Promise<ModelStatus>;
|
|
125
|
+
status(): Promise<ModelStatus>;
|
|
126
|
+
/**
|
|
127
|
+
* Ask the environment's model. Supported kinds only; the answer must be one of the
|
|
128
|
+
* request's labels. Errors carry the problem and the fix; nothing is retried.
|
|
129
|
+
*/
|
|
130
|
+
decide(request: DecisionRequest): Promise<DecisionOutcome>;
|
|
131
|
+
}
|