@veriforms/webapp-sdk 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +59 -12
- package/config.schema.json +15 -2
- package/dist/index.d.ts +107 -2
- package/dist/index.js +98 -1
- package/dist/serverClient.d.ts +20 -1
- package/dist/serverClient.js +23 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,20 +10,18 @@ npm i @veriforms/webapp-sdk @veriforms/webapp-react react@18.3.1 react-dom@18.3.
|
|
|
10
10
|
npm i -D @veriforms/webapp-vite vite
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
The packages are public on npm (owner decision 2026-12-03). Web apps built in code are open to
|
|
14
|
+
the Enterprise and Enterprise Plus plans and to other accounts VeriForms enabled (owner decision
|
|
15
|
+
2026-12-04).
|
|
16
|
+
The three packages are released together at one version
|
|
17
17
|
(`webapp-react` names the SDK exactly). Install React at the version the platform serves (18.3.1) —
|
|
18
18
|
the upload compares your `package-lock.json`.
|
|
19
19
|
|
|
20
|
-
**Status:**
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
It becomes useful to customers with phase 2, when the gateway can serve a web app
|
|
26
|
-
built in code. Until then, VeriForms developers can already use it and its types.
|
|
20
|
+
**Status:** published public (tag `webapp-v<version>`, `.github/workflows/webapp-release.yml`).
|
|
21
|
+
**Availability:** web apps built in code are included in Enterprise and Enterprise Plus and open
|
|
22
|
+
to other accounts a platform administrator enabled (owner decision 2026-12-04; the switch is
|
|
23
|
+
`CwaCode:Availability`, read by `CodeWebAppsGate`). Server functions (`src/server/`) stay
|
|
24
|
+
platform-administrator only.
|
|
27
25
|
|
|
28
26
|
## What it is
|
|
29
27
|
|
|
@@ -65,11 +63,12 @@ location.href = pageLink('admin'); // every area is its own page, gated by the
|
|
|
65
63
|
| `db.update(source, id, patch, version?, {clientToken}?)` | `customdb-write` | `VF.db.update` (version → 409 row_changed when someone changed the row) |
|
|
66
64
|
| `data.list(source, params?)` | `veriforms-data` | `VF.data.list` → `{Rows, Count, Truncated}` |
|
|
67
65
|
| `fetchSource(source, params?)` | `veriflow`, `http`, `demo` | `VF.fetch` |
|
|
66
|
+
| `knowledge.search(source, question)` (0.1.3) | `knowledge` | `VF.fetch` with `?q=` → `{status, message, passages, sourceLine}` |
|
|
68
67
|
| `send(source, body, {clientToken}?)` | submission, webhook, workflow, veriflow, customdb-write | `VF.send` (repeat-safe) |
|
|
69
68
|
| `runAction(source, opts?)` | `workflow`, `veriflow`, `webhook` | `VF.runAction` |
|
|
70
69
|
| `form.render(target, source, opts?)`, `form.submitPartial` | submission, customdb-write | `VF.form.*` |
|
|
71
70
|
| `pay.start / status / pendingId / receiptLink` | `veriforms-payment` | `VF.pay.*` |
|
|
72
|
-
| `user()`, `signIn()`, `signOut()` | — | `VF.user`, `VF.signIn`, `VF.signOut` |
|
|
71
|
+
| `user()`, `signIn()`, `signOut()`, `signInSecurity()` (0.1.3) | — | `VF.user`, `VF.signIn`, `VF.signOut`, `VF.signInSecurity` |
|
|
73
72
|
| `access.has(role)`, `access.can(source)`, `access.level()`, `access.signedIn()` | — | `VF.access` |
|
|
74
73
|
| `pageLink(name, '?…' \| '#…')`, `currentPage()`, `appLink()`, `file()` | — | `VF.pageLink`, `VF.page`, `VF.appLink`, `VF.file` |
|
|
75
74
|
| `t()`, `theme()`, `newClientToken()`, `onDataChanged(listener)` | — | `VF.t`, `VF.theme.current`, `VF.newClientToken`, `vf:data-changed` |
|
|
@@ -119,6 +118,37 @@ The app must offer sign-in: restricted, or open to anyone with optional sign-in
|
|
|
119
118
|
(`set_cwa_access` with `methods`, e.g. `["code"]`) — otherwise the save refuses
|
|
120
119
|
the rule (`sign_in_not_offered`).
|
|
121
120
|
|
|
121
|
+
### Knowledge bases: answers with their source
|
|
122
|
+
|
|
123
|
+
A `knowledge` source asks one of the space's knowledge bases a question
|
|
124
|
+
(`"base"`: the base's id, optional `"topK"` 1–8). `knowledge.search` resolves
|
|
125
|
+
with `status: "ok"` and the passages — each with the document, web page or
|
|
126
|
+
table row it comes from — and `sourceLine`, the citation the server built
|
|
127
|
+
("Quelle: …" / "Source: …"). Show that line under anything you build from the
|
|
128
|
+
passages; never write a citation yourself. `"no_match"` (nothing in the base
|
|
129
|
+
answers it) and `"empty"` (nothing indexed yet) come with a `message` and no
|
|
130
|
+
passages: show the message, never an invented answer.
|
|
131
|
+
|
|
132
|
+
A refusal is an error, never an empty answer: `knowledge.search` rejects with a
|
|
133
|
+
`KnowledgeSearchError` whose `code` says why — `knowledge_base_internal` (an
|
|
134
|
+
app whose visitors are not members of the space may only use a public base an
|
|
135
|
+
admin confirmed), `knowledge_base_unconfirmed_app`, `knowledge_base_missing`,
|
|
136
|
+
`knowledge_ai_unavailable` — and `isKnowledgeRefusal(e)` is true for those.
|
|
137
|
+
`kind: "unavailable"` means the search could not run just now. Every search is
|
|
138
|
+
metered to the space's AI budget (as "Knowledge base: search from a web app").
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
import { knowledge, isKnowledgeRefusal } from '@veriforms/webapp-sdk';
|
|
142
|
+
|
|
143
|
+
try {
|
|
144
|
+
const answer = await knowledge.search('faq', question);
|
|
145
|
+
if (answer.status === 'ok') show(answer.passages, answer.sourceLine);
|
|
146
|
+
else showNote(answer.message);
|
|
147
|
+
} catch (e) {
|
|
148
|
+
showNote(isKnowledgeRefusal(e) ? e.message : 'The search is not available just now.');
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
122
152
|
### The draft shows preview data only
|
|
123
153
|
|
|
124
154
|
`vf app dev` and the builder's draft preview never read or change real rows. A
|
|
@@ -199,6 +229,23 @@ try {
|
|
|
199
229
|
Every refusal rejects with `ServerFunctionError` (`code`, `status`, `message` / `messageDe` /
|
|
200
230
|
`messageEn`, `runId`; `kind === 'server_function_refused'` for `ctx.refuse`); no answer at all is
|
|
201
231
|
`code: 'network'` with `outcome: 'unknown'` — resend with the same `idempotencyKey`.
|
|
232
|
+
- **"No access" is not "failed" (since 0.1.2):** `e.accessRefused` (or `isServerFunctionAccessRefusal(e)`)
|
|
233
|
+
is true when the VISITOR may not do this — not signed in (`server_function_sign_in_required`), or a data
|
|
234
|
+
source refused them by access (`server_function_source_refused` with `access: true`: 401/403, e.g.
|
|
235
|
+
`sourceCode: 'row_rule_missing'` — the source has row rules only for roles the visitor does not hold).
|
|
236
|
+
Then `e.source`, `e.sourceStatus`, `e.sourceCode` and `e.sourceMessageDe` / `e.sourceMessageEn` say which
|
|
237
|
+
source and why; show "no access" (or a sign-in) there, never a calculation error. Every other code is a
|
|
238
|
+
fault of the function or a source. Every answer of the route — its own gates too (wrong host, JSON only,
|
|
239
|
+
link password, app misconfigured) — has the same shape: `code`, `message`, `messageDe`, `messageEn`.
|
|
240
|
+
|
|
241
|
+
```ts
|
|
242
|
+
try { setQuotes(await server.listMyQuotes()); }
|
|
243
|
+
catch (e) {
|
|
244
|
+
if (isServerFunctionAccessRefusal(e)) showNoAccess(lang === 'en' ? e.sourceMessageEn ?? e.messageEn : e.sourceMessageDe ?? e.messageDe);
|
|
245
|
+
else if (isServerFunctionError(e)) showFailure(e.message, e.runId);
|
|
246
|
+
else throw e;
|
|
247
|
+
}
|
|
248
|
+
```
|
|
202
249
|
- **A newer release (X9):** `409 server_function_version_changed` reloads the page (once per build) and
|
|
203
250
|
rejects with `ServerFunctionVersionChangedError`; the old code never runs.
|
|
204
251
|
- **Types:** the plugin writes `vf-server.d.ts` (input and answer of every function, from its
|
package/config.schema.json
CHANGED
|
@@ -36,7 +36,8 @@
|
|
|
36
36
|
"customdb",
|
|
37
37
|
"customdb-write",
|
|
38
38
|
"workflow",
|
|
39
|
-
"veriforms-roles"
|
|
39
|
+
"veriforms-roles",
|
|
40
|
+
"knowledge"
|
|
40
41
|
]
|
|
41
42
|
},
|
|
42
43
|
"veriflowId": {
|
|
@@ -322,7 +323,9 @@
|
|
|
322
323
|
},
|
|
323
324
|
"callableFrom": {
|
|
324
325
|
"type": "string",
|
|
325
|
-
"enum": [
|
|
326
|
+
"enum": [
|
|
327
|
+
"server"
|
|
328
|
+
],
|
|
326
329
|
"description": "\"server\": only the app's server functions may call this source, not the page directly (server functions are in a pilot: only platform administrators push and release them). Absent = page and server functions."
|
|
327
330
|
},
|
|
328
331
|
"anyoneMayRun": {
|
|
@@ -438,6 +441,16 @@
|
|
|
438
441
|
"type": "string",
|
|
439
442
|
"minLength": 10
|
|
440
443
|
}
|
|
444
|
+
},
|
|
445
|
+
"base": {
|
|
446
|
+
"type": "string",
|
|
447
|
+
"description": "knowledge: the knowledge base's id (from list_knowledge_bases). The page asks with ?q=…; the API looks the base up in the app's own space."
|
|
448
|
+
},
|
|
449
|
+
"topK": {
|
|
450
|
+
"type": "integer",
|
|
451
|
+
"minimum": 1,
|
|
452
|
+
"maximum": 8,
|
|
453
|
+
"description": "knowledge: how many passages to return (1–8)."
|
|
441
454
|
}
|
|
442
455
|
},
|
|
443
456
|
"patternProperties": {
|
package/dist/index.d.ts
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
*
|
|
16
16
|
* Concept: docs/webapp-builder/CWA-CODE-APPS-CONCEPT.md (§4.2).
|
|
17
17
|
*/
|
|
18
|
-
export declare const SDK_VERSION = "0.1.
|
|
18
|
+
export declare const SDK_VERSION = "0.1.3";
|
|
19
19
|
/**
|
|
20
20
|
* Augmented by the `vf-app.d.ts` that `vf app types` generates:
|
|
21
21
|
*
|
|
@@ -64,6 +64,8 @@ export type SourceShape = {
|
|
|
64
64
|
type: 'veriforms-payment';
|
|
65
65
|
} | {
|
|
66
66
|
type: 'veriforms-roles';
|
|
67
|
+
} | {
|
|
68
|
+
type: 'knowledge';
|
|
67
69
|
};
|
|
68
70
|
export interface AppShape {
|
|
69
71
|
sources: Record<string, SourceShape>;
|
|
@@ -117,6 +119,8 @@ export type ActionKey = SourceKey<'workflow' | 'veriflow' | 'webhook'>;
|
|
|
117
119
|
export type PaymentKey = SourceKey<'veriforms-payment'>;
|
|
118
120
|
/** A source {@link form.render} can draw a form for. */
|
|
119
121
|
export type FormKey = SourceKey<'veriforms-submission' | 'customdb-write'>;
|
|
122
|
+
/** A source a page asks questions with {@link knowledge.search}: one of the space's knowledge bases (since 0.1.3). */
|
|
123
|
+
export type KnowledgeKey = SourceKey<'knowledge'>;
|
|
120
124
|
/** A page of this web app ('main' or '' is the main page). */
|
|
121
125
|
export type PageName = (App extends {
|
|
122
126
|
pages: infer P;
|
|
@@ -219,8 +223,18 @@ export interface UserInfo {
|
|
|
219
223
|
name: string | null;
|
|
220
224
|
roles: RoleName[];
|
|
221
225
|
level: string | null;
|
|
222
|
-
|
|
226
|
+
/**
|
|
227
|
+
* How the visitor signed in: `'code'` (the e-mail code, or an owner-set list password), `'passkey'`,
|
|
228
|
+
* `'account'` (VeriForms account), `'sso'`, `'company'` — or `'preview'` in the builder preview. A passkey
|
|
229
|
+
* proves the e-mail address exactly like the code: never grant anything because via is `'passkey'`.
|
|
230
|
+
*/
|
|
231
|
+
via: 'code' | 'passkey' | 'account' | 'sso' | 'company' | 'preview' | (string & {}) | null;
|
|
223
232
|
signInOffered: boolean;
|
|
233
|
+
/**
|
|
234
|
+
* This visitor can save and manage passkeys here (since 0.1.3): anonymous, or signed in by the e-mail code or a
|
|
235
|
+
* passkey, on an app that offers the e-mail code. Then a link to {@link signInSecurity} is worth showing.
|
|
236
|
+
*/
|
|
237
|
+
passkeysOffered?: boolean;
|
|
224
238
|
preview?: boolean;
|
|
225
239
|
}
|
|
226
240
|
export interface ActionResult {
|
|
@@ -288,6 +302,7 @@ export interface VfRuntime {
|
|
|
288
302
|
user(): Promise<unknown>;
|
|
289
303
|
signIn(path?: string): void;
|
|
290
304
|
signOut(path?: string): void;
|
|
305
|
+
signInSecurity(path?: string): void;
|
|
291
306
|
appLink(target: string, path?: string): string;
|
|
292
307
|
pageLink(name?: string, suffix?: string): string;
|
|
293
308
|
page: string;
|
|
@@ -345,6 +360,90 @@ export declare const data: {
|
|
|
345
360
|
};
|
|
346
361
|
/** Reads a source: an Automation (veriflow), an http upstream or demo data. */
|
|
347
362
|
export declare function fetchSource<K extends ReadKey>(source: K, params?: Partial<ParamsOf<K>> & NoUndeclaredParams): Promise<ResultOf<K>>;
|
|
363
|
+
/**
|
|
364
|
+
* How a knowledge search ended, as the gateway answers it (KnowledgeSourceClient.SearchAsync):
|
|
365
|
+
* `ok` has passages; `no_match` (nothing in the base answers the question) and `empty` (the base
|
|
366
|
+
* holds no indexed document yet) have none and a message to show — never an invented answer.
|
|
367
|
+
*/
|
|
368
|
+
export declare const KNOWLEDGE_STATUSES: readonly ["ok", "no_match", "empty"];
|
|
369
|
+
export type KnowledgeStatus = typeof KNOWLEDGE_STATUSES[number];
|
|
370
|
+
/** One passage of a knowledge base, with what it was cited from. */
|
|
371
|
+
export interface KnowledgePassage {
|
|
372
|
+
/** The document it comes from (file name, page title, FAQ entry); null for a table row. */
|
|
373
|
+
document: string | null;
|
|
374
|
+
/** The web page it was read from, when the base indexed one. */
|
|
375
|
+
url: string | null;
|
|
376
|
+
/** The Custom Databases table, for a passage built from a table row. */
|
|
377
|
+
table: string | null;
|
|
378
|
+
rowId: number | null;
|
|
379
|
+
/** Its place in the document (0-based). */
|
|
380
|
+
position: number;
|
|
381
|
+
/** How well it answers the question (higher is better); compare within one answer only. */
|
|
382
|
+
score: number;
|
|
383
|
+
text: string;
|
|
384
|
+
}
|
|
385
|
+
/** The passage keys, in the gateway's order (pinned against KnowledgeSourceClient.cs by test/pins.test.ts). */
|
|
386
|
+
export declare const KNOWLEDGE_PASSAGE_FIELDS: readonly ["document", "url", "table", "rowId", "position", "score", "text"];
|
|
387
|
+
/**
|
|
388
|
+
* What {@link knowledge.search} resolves with. `sourceLine` is the server-built citation ("Quelle: …" /
|
|
389
|
+
* "Source: …"): show it under any answer built from the passages — never write a citation yourself.
|
|
390
|
+
*/
|
|
391
|
+
export type KnowledgeAnswer = {
|
|
392
|
+
status: 'ok';
|
|
393
|
+
message: string | null;
|
|
394
|
+
passages: KnowledgePassage[];
|
|
395
|
+
sourceLine: string | null;
|
|
396
|
+
} | {
|
|
397
|
+
status: 'no_match' | 'empty';
|
|
398
|
+
message: string | null;
|
|
399
|
+
passages: [];
|
|
400
|
+
sourceLine: null;
|
|
401
|
+
};
|
|
402
|
+
/** The answer's keys (pinned against KnowledgeSourceClient.cs by test/pins.test.ts). */
|
|
403
|
+
export declare const KNOWLEDGE_ANSWER_FIELDS: readonly ["status", "message", "passages", "sourceLine"];
|
|
404
|
+
/**
|
|
405
|
+
* The codes a knowledge source is REFUSED with — the base, not the question: an internal base on an
|
|
406
|
+
* app whose visitors are not workspace members (`knowledge_base_internal`), a public base no admin
|
|
407
|
+
* confirmed (`knowledge_base_unconfirmed_app`), AI not available on the plan or switched off
|
|
408
|
+
* (`knowledge_ai_unavailable`), a base that does not exist (`knowledge_base_missing`). Pinned against
|
|
409
|
+
* KnowledgeSourceClient.RuleCodes and KnowledgeWebApps by test/pins.test.ts.
|
|
410
|
+
*/
|
|
411
|
+
export declare const KNOWLEDGE_REFUSAL_CODES: readonly ["knowledge_base_internal", "knowledge_base_unconfirmed_app", "knowledge_ai_unavailable", "knowledge_base_missing"];
|
|
412
|
+
export type KnowledgeRefusalCode = typeof KNOWLEDGE_REFUSAL_CODES[number];
|
|
413
|
+
/**
|
|
414
|
+
* - `refused`: the server will not search this base for this app (one of {@link KNOWLEDGE_REFUSAL_CODES},
|
|
415
|
+
* or the visitor may not use the source) — asking again changes nothing; show `message`.
|
|
416
|
+
* - `question`: no question was asked (`query_required`).
|
|
417
|
+
* - `unavailable`: the search could not run just now, or its answer could not be read — try later.
|
|
418
|
+
*/
|
|
419
|
+
export type KnowledgeErrorKind = 'refused' | 'question' | 'unavailable';
|
|
420
|
+
/**
|
|
421
|
+
* Every way a knowledge search does not answer. A refusal is an ERROR, never an empty answer: an empty
|
|
422
|
+
* list would read as "the base knows nothing about it", which is a different, false, statement.
|
|
423
|
+
*/
|
|
424
|
+
export declare class KnowledgeSearchError extends Error {
|
|
425
|
+
readonly code: string;
|
|
426
|
+
readonly source: string;
|
|
427
|
+
readonly status?: number | undefined;
|
|
428
|
+
readonly kind: KnowledgeErrorKind;
|
|
429
|
+
constructor(code: string, source: string, message: string, status?: number | undefined, options?: {
|
|
430
|
+
cause?: unknown;
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
/** True for a knowledge search the server refused (the base's rule, a missing base, no access). */
|
|
434
|
+
export declare function isKnowledgeRefusal(e: unknown): e is KnowledgeSearchError & {
|
|
435
|
+
kind: 'refused';
|
|
436
|
+
};
|
|
437
|
+
/** Questions to the space's knowledge bases, through a `knowledge` source. */
|
|
438
|
+
export declare const knowledge: {
|
|
439
|
+
/**
|
|
440
|
+
* Asks the source's knowledge base one question. Resolves with the passages and the server-built
|
|
441
|
+
* source line (`ok`), or with `no_match` / `empty` and a message to show. Rejects with a
|
|
442
|
+
* {@link KnowledgeSearchError} when the search is refused or cannot run — see {@link isKnowledgeRefusal}.
|
|
443
|
+
* Each search is metered to the space's AI budget.
|
|
444
|
+
*/
|
|
445
|
+
search<K extends KnowledgeKey>(source: K, question: string): Promise<KnowledgeAnswer>;
|
|
446
|
+
};
|
|
348
447
|
/**
|
|
349
448
|
* Posts to a source with one clientToken for the visitor's one action, and asks
|
|
350
449
|
* again with the same token when the answer did not settle anything. A rejection
|
|
@@ -379,6 +478,12 @@ export declare function user(): Promise<UserInfo>;
|
|
|
379
478
|
/** Sends the visitor to the platform's sign-in, and back to `path` (default: here). */
|
|
380
479
|
export declare function signIn(path?: string): void;
|
|
381
480
|
export declare function signOut(path?: string): void;
|
|
481
|
+
/**
|
|
482
|
+
* Sends the visitor to the platform's "Sign-in & security" page — their passkeys: rename, remove, add, remove all —
|
|
483
|
+
* and its Back link returns to `path` (default: here). A page of the platform, never a passkey dialog in page code
|
|
484
|
+
* (the gateway runs every ceremony). Since 0.1.3; show it when `(await user()).passkeysOffered`.
|
|
485
|
+
*/
|
|
486
|
+
export declare function signInSecurity(path?: string): void;
|
|
382
487
|
/** What the visitor may do, as the server told the page. The server checks every call again. */
|
|
383
488
|
export declare const access: {
|
|
384
489
|
has(role: RoleName): boolean;
|
package/dist/index.js
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
*
|
|
16
16
|
* Concept: docs/webapp-builder/CWA-CODE-APPS-CONCEPT.md (§4.2).
|
|
17
17
|
*/
|
|
18
|
-
export const SDK_VERSION = '0.1.
|
|
18
|
+
export const SDK_VERSION = '0.1.3';
|
|
19
19
|
export function isVfRequestError(e) {
|
|
20
20
|
return e instanceof Error;
|
|
21
21
|
}
|
|
@@ -104,6 +104,97 @@ export const data = {
|
|
|
104
104
|
export function fetchSource(source, params) {
|
|
105
105
|
return fn('fetch')(source, params ? { params: params } : {});
|
|
106
106
|
}
|
|
107
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
108
|
+
// Knowledge bases (a `knowledge` source, since 0.1.3)
|
|
109
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
110
|
+
/**
|
|
111
|
+
* How a knowledge search ended, as the gateway answers it (KnowledgeSourceClient.SearchAsync):
|
|
112
|
+
* `ok` has passages; `no_match` (nothing in the base answers the question) and `empty` (the base
|
|
113
|
+
* holds no indexed document yet) have none and a message to show — never an invented answer.
|
|
114
|
+
*/
|
|
115
|
+
export const KNOWLEDGE_STATUSES = ['ok', 'no_match', 'empty'];
|
|
116
|
+
/** The passage keys, in the gateway's order (pinned against KnowledgeSourceClient.cs by test/pins.test.ts). */
|
|
117
|
+
export const KNOWLEDGE_PASSAGE_FIELDS = ['document', 'url', 'table', 'rowId', 'position', 'score', 'text'];
|
|
118
|
+
/** The answer's keys (pinned against KnowledgeSourceClient.cs by test/pins.test.ts). */
|
|
119
|
+
export const KNOWLEDGE_ANSWER_FIELDS = ['status', 'message', 'passages', 'sourceLine'];
|
|
120
|
+
/**
|
|
121
|
+
* The codes a knowledge source is REFUSED with — the base, not the question: an internal base on an
|
|
122
|
+
* app whose visitors are not workspace members (`knowledge_base_internal`), a public base no admin
|
|
123
|
+
* confirmed (`knowledge_base_unconfirmed_app`), AI not available on the plan or switched off
|
|
124
|
+
* (`knowledge_ai_unavailable`), a base that does not exist (`knowledge_base_missing`). Pinned against
|
|
125
|
+
* KnowledgeSourceClient.RuleCodes and KnowledgeWebApps by test/pins.test.ts.
|
|
126
|
+
*/
|
|
127
|
+
export const KNOWLEDGE_REFUSAL_CODES = [
|
|
128
|
+
'knowledge_base_internal', 'knowledge_base_unconfirmed_app', 'knowledge_ai_unavailable', 'knowledge_base_missing',
|
|
129
|
+
];
|
|
130
|
+
/**
|
|
131
|
+
* Every way a knowledge search does not answer. A refusal is an ERROR, never an empty answer: an empty
|
|
132
|
+
* list would read as "the base knows nothing about it", which is a different, false, statement.
|
|
133
|
+
*/
|
|
134
|
+
export class KnowledgeSearchError extends Error {
|
|
135
|
+
constructor(code, source, message, status, options) {
|
|
136
|
+
super(message);
|
|
137
|
+
this.code = code;
|
|
138
|
+
this.source = source;
|
|
139
|
+
this.status = status;
|
|
140
|
+
this.name = 'KnowledgeSearchError';
|
|
141
|
+
if (options && 'cause' in options)
|
|
142
|
+
this.cause = options.cause;
|
|
143
|
+
this.kind = KNOWLEDGE_REFUSAL_CODES.includes(code) || status === 401 || status === 403 || status === 404
|
|
144
|
+
? 'refused'
|
|
145
|
+
: code === 'query_required' || status === 400 ? 'question' : 'unavailable';
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/** True for a knowledge search the server refused (the base's rule, a missing base, no access). */
|
|
149
|
+
export function isKnowledgeRefusal(e) {
|
|
150
|
+
return e instanceof KnowledgeSearchError && e.kind === 'refused';
|
|
151
|
+
}
|
|
152
|
+
const isPassage = (p) => !!p && typeof p === 'object'
|
|
153
|
+
&& typeof p.text === 'string' && typeof p.position === 'number';
|
|
154
|
+
/** The gateway's answer, checked: anything else is a loud failure, never an empty answer. */
|
|
155
|
+
function readKnowledgeAnswer(source, raw) {
|
|
156
|
+
const a = raw;
|
|
157
|
+
const unreadable = () => new KnowledgeSearchError('knowledge_answer_unreadable', source, `@veriforms/webapp-sdk ${SDK_VERSION}: the answer of knowledge source "${source}" could not be read.`);
|
|
158
|
+
if (!a || typeof a !== 'object' || !KNOWLEDGE_STATUSES.includes(a.status)
|
|
159
|
+
|| !Array.isArray(a.passages) || !a.passages.every(isPassage))
|
|
160
|
+
throw unreadable();
|
|
161
|
+
const message = typeof a.message === 'string' ? a.message : null;
|
|
162
|
+
if (a.status === 'ok') {
|
|
163
|
+
if (a.passages.length === 0)
|
|
164
|
+
throw unreadable();
|
|
165
|
+
return { status: 'ok', message, passages: a.passages, sourceLine: typeof a.sourceLine === 'string' ? a.sourceLine : null };
|
|
166
|
+
}
|
|
167
|
+
if (a.passages.length > 0)
|
|
168
|
+
throw unreadable();
|
|
169
|
+
return { status: a.status, message, passages: [], sourceLine: null };
|
|
170
|
+
}
|
|
171
|
+
/** Questions to the space's knowledge bases, through a `knowledge` source. */
|
|
172
|
+
export const knowledge = {
|
|
173
|
+
/**
|
|
174
|
+
* Asks the source's knowledge base one question. Resolves with the passages and the server-built
|
|
175
|
+
* source line (`ok`), or with `no_match` / `empty` and a message to show. Rejects with a
|
|
176
|
+
* {@link KnowledgeSearchError} when the search is refused or cannot run — see {@link isKnowledgeRefusal}.
|
|
177
|
+
* Each search is metered to the space's AI budget.
|
|
178
|
+
*/
|
|
179
|
+
async search(source, question) {
|
|
180
|
+
const q = typeof question === 'string' ? question.trim() : '';
|
|
181
|
+
if (!q)
|
|
182
|
+
throw new KnowledgeSearchError('query_required', source, 'Ask a question: knowledge.search(source, "…").', 400);
|
|
183
|
+
const call = fn('fetch');
|
|
184
|
+
let raw;
|
|
185
|
+
try {
|
|
186
|
+
raw = await call(source, { params: { q } });
|
|
187
|
+
}
|
|
188
|
+
catch (e) {
|
|
189
|
+
const err = e;
|
|
190
|
+
const data = (err && typeof err === 'object' ? err.data : undefined);
|
|
191
|
+
const code = typeof err?.code === 'string' ? err.code : typeof data?.code === 'string' ? data.code : 'knowledge_unavailable';
|
|
192
|
+
const message = typeof data?.message === 'string' ? data.message : err instanceof Error ? err.message : String(e);
|
|
193
|
+
throw new KnowledgeSearchError(code, source, message, typeof err?.status === 'number' ? err.status : undefined, { cause: e });
|
|
194
|
+
}
|
|
195
|
+
return readKnowledgeAnswer(source, raw);
|
|
196
|
+
},
|
|
197
|
+
};
|
|
107
198
|
/**
|
|
108
199
|
* Posts to a source with one clientToken for the visitor's one action, and asks
|
|
109
200
|
* again with the same token when the answer did not settle anything. A rejection
|
|
@@ -154,6 +245,12 @@ export function user() {
|
|
|
154
245
|
/** Sends the visitor to the platform's sign-in, and back to `path` (default: here). */
|
|
155
246
|
export function signIn(path) { fn('signIn')(path); }
|
|
156
247
|
export function signOut(path) { fn('signOut')(path); }
|
|
248
|
+
/**
|
|
249
|
+
* Sends the visitor to the platform's "Sign-in & security" page — their passkeys: rename, remove, add, remove all —
|
|
250
|
+
* and its Back link returns to `path` (default: here). A page of the platform, never a passkey dialog in page code
|
|
251
|
+
* (the gateway runs every ceremony). Since 0.1.3; show it when `(await user()).passkeysOffered`.
|
|
252
|
+
*/
|
|
253
|
+
export function signInSecurity(path) { fn('signInSecurity')(path); }
|
|
157
254
|
/** What the visitor may do, as the server told the page. The server checks every call again. */
|
|
158
255
|
export const access = {
|
|
159
256
|
has(role) { return fn('access.has')(role); },
|
package/dist/serverClient.d.ts
CHANGED
|
@@ -33,7 +33,7 @@ export declare const INPUT_SCHEMA_TYPES: readonly ["object", "array", "string",
|
|
|
33
33
|
* the two lists equal). An author's own refusal (`ctx.refuse`) keeps the author's code and has
|
|
34
34
|
* `kind === 'server_function_refused'`.
|
|
35
35
|
*/
|
|
36
|
-
export declare const SERVER_FUNCTION_CODES: readonly ["server_functions_off", "server_functions_not_enabled", "server_function_unavailable", "server_function_not_found", "server_function_version_changed", "server_function_sign_in_required", "server_function_input_too_large", "server_function_input_invalid", "server_function_nesting_limit", "server_function_budget_exhausted", "server_function_busy", "server_function_breaker_open", "server_function_time_limit", "server_function_statement_limit", "server_function_allocation_limit", "server_function_stack_limit", "server_function_regex_limit", "server_function_forbidden", "server_function_too_many_data_calls", "server_function_source_undeclared", "server_function_source_unsupported", "server_function_source_timeout", "server_function_source_refused", "server_function_module_unknown", "server_function_output_too_large", "server_function_output_invalid", "server_function_failed", "server_function_refused", "server_function_partial", "server_manifest_mismatch"];
|
|
36
|
+
export declare const SERVER_FUNCTION_CODES: readonly ["server_functions_off", "server_functions_not_enabled", "server_function_unavailable", "server_function_not_found", "server_function_version_changed", "server_function_sign_in_required", "server_function_input_too_large", "server_function_input_invalid", "server_function_nesting_limit", "server_function_budget_exhausted", "server_function_busy", "server_function_breaker_open", "server_function_time_limit", "server_function_statement_limit", "server_function_allocation_limit", "server_function_stack_limit", "server_function_regex_limit", "server_function_forbidden", "server_function_too_many_data_calls", "server_function_source_undeclared", "server_function_source_unsupported", "server_function_source_timeout", "server_function_source_refused", "server_function_module_unknown", "server_function_output_too_large", "server_function_output_invalid", "server_function_failed", "server_function_refused", "server_function_partial", "server_manifest_mismatch", "server_function_cross_site", "server_function_json_only", "server_function_wrong_host", "server_function_instance_mismatch", "server_function_link_ended", "server_function_link_password_required", "server_function_app_misconfigured"];
|
|
37
37
|
export type ServerFunctionCode = typeof SERVER_FUNCTION_CODES[number];
|
|
38
38
|
/** The stub's own codes: the request never got an answer (`network`), or one that is not the gateway's JSON (`http_error`). */
|
|
39
39
|
export type ServerFunctionClientCode = 'network' | 'http_error';
|
|
@@ -57,6 +57,20 @@ export declare class ServerFunctionError extends Error {
|
|
|
57
57
|
readonly idempotencyKey: string;
|
|
58
58
|
/** The whole answer body (e.g. `completed` / `pending` of `server_function_partial`). */
|
|
59
59
|
readonly data: Record<string, unknown> | undefined;
|
|
60
|
+
/** `server_function_source_refused`: the data source that refused the function's call (its config key). */
|
|
61
|
+
readonly source: string | undefined;
|
|
62
|
+
/** `server_function_source_refused`: the source's own status (403) and code (`row_rule_missing`). */
|
|
63
|
+
readonly sourceStatus: number | undefined;
|
|
64
|
+
readonly sourceCode: string | undefined;
|
|
65
|
+
/** `server_function_source_refused` for an ACCESS refusal: what the source said, in both languages. */
|
|
66
|
+
readonly sourceMessageDe: string | undefined;
|
|
67
|
+
readonly sourceMessageEn: string | undefined;
|
|
68
|
+
/**
|
|
69
|
+
* True when the visitor may not do this — not signed in (`server_function_sign_in_required`) or a source
|
|
70
|
+
* refused them by access (401/403: no role with a row rule, too low a level). Show "no access" / offer
|
|
71
|
+
* sign-in, never "the calculation failed". False for a fault of the function or a source.
|
|
72
|
+
*/
|
|
73
|
+
readonly accessRefused: boolean;
|
|
60
74
|
constructor(init: {
|
|
61
75
|
code: string;
|
|
62
76
|
message: string;
|
|
@@ -77,6 +91,11 @@ export declare class ServerFunctionVersionChangedError extends ServerFunctionErr
|
|
|
77
91
|
readonly reloading: boolean;
|
|
78
92
|
constructor(init: ConstructorParameters<typeof ServerFunctionError>[0], reloading: boolean);
|
|
79
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* True for a refusal of the VISITOR (not signed in, or a source refused them by access — e.g. a row rule only for
|
|
96
|
+
* roles they do not hold): show "no access" or offer sign-in, not "failed". See {@link ServerFunctionError.accessRefused}.
|
|
97
|
+
*/
|
|
98
|
+
export declare function isServerFunctionAccessRefusal(e: unknown): e is ServerFunctionError;
|
|
80
99
|
/** True for a {@link ServerFunctionError}, optionally of one code. */
|
|
81
100
|
export declare function isServerFunctionError(e: unknown, code?: ServerFunctionCode | ServerFunctionClientCode | string): e is ServerFunctionError;
|
|
82
101
|
/**
|
package/dist/serverClient.js
CHANGED
|
@@ -65,6 +65,13 @@ export const SERVER_FUNCTION_CODES = [
|
|
|
65
65
|
'server_function_refused',
|
|
66
66
|
'server_function_partial',
|
|
67
67
|
'server_manifest_mismatch',
|
|
68
|
+
'server_function_cross_site',
|
|
69
|
+
'server_function_json_only',
|
|
70
|
+
'server_function_wrong_host',
|
|
71
|
+
'server_function_instance_mismatch',
|
|
72
|
+
'server_function_link_ended',
|
|
73
|
+
'server_function_link_password_required',
|
|
74
|
+
'server_function_app_misconfigured',
|
|
68
75
|
];
|
|
69
76
|
/**
|
|
70
77
|
* A refused or failed server-function call. `message` is the gateway's sentence in the visitor's language;
|
|
@@ -85,6 +92,15 @@ export class ServerFunctionError extends Error {
|
|
|
85
92
|
this.outcome = init.outcome;
|
|
86
93
|
this.idempotencyKey = init.idempotencyKey;
|
|
87
94
|
this.data = init.data;
|
|
95
|
+
const d = init.data ?? {};
|
|
96
|
+
const str = (k) => (typeof d[k] === 'string' ? d[k] : undefined);
|
|
97
|
+
this.source = str('source');
|
|
98
|
+
this.sourceStatus = typeof d.sourceStatus === 'number' ? d.sourceStatus : undefined;
|
|
99
|
+
this.sourceCode = str('sourceCode');
|
|
100
|
+
this.sourceMessageDe = str('sourceMessageDe');
|
|
101
|
+
this.sourceMessageEn = str('sourceMessageEn');
|
|
102
|
+
this.accessRefused = init.code === 'server_function_sign_in_required'
|
|
103
|
+
|| (init.code === 'server_function_source_refused' && d.access === true);
|
|
88
104
|
}
|
|
89
105
|
}
|
|
90
106
|
/** 409 `server_function_version_changed`: the page is out of date; the stub has started a reload (unless it already did for this build). */
|
|
@@ -95,6 +111,13 @@ export class ServerFunctionVersionChangedError extends ServerFunctionError {
|
|
|
95
111
|
this.reloading = reloading;
|
|
96
112
|
}
|
|
97
113
|
}
|
|
114
|
+
/**
|
|
115
|
+
* True for a refusal of the VISITOR (not signed in, or a source refused them by access — e.g. a row rule only for
|
|
116
|
+
* roles they do not hold): show "no access" or offer sign-in, not "failed". See {@link ServerFunctionError.accessRefused}.
|
|
117
|
+
*/
|
|
118
|
+
export function isServerFunctionAccessRefusal(e) {
|
|
119
|
+
return e instanceof ServerFunctionError && e.accessRefused;
|
|
120
|
+
}
|
|
98
121
|
/** True for a {@link ServerFunctionError}, optionally of one code. */
|
|
99
122
|
export function isServerFunctionError(e, code) {
|
|
100
123
|
return e instanceof ServerFunctionError && (code === undefined || e.code === code);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@veriforms/webapp-sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Typed access to the VeriForms web app runtime (window.VF) for web apps built in code: data sources, tables, submissions, actions, payments, sign-in and pages.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"veriforms",
|