@sess.si/sdk 0.0.0-stage → 0.1.0-rc.10
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/CHANGELOG.md +185 -0
- package/LICENSE +201 -0
- package/NOTICE +27 -0
- package/README.md +327 -2
- package/dist/ask.d.ts +161 -0
- package/dist/ask.js +499 -0
- package/dist/cli/main.d.ts +2 -0
- package/dist/cli/main.js +7 -0
- package/dist/cli/verify.d.ts +13 -0
- package/dist/cli/verify.js +322 -0
- package/dist/client.d.ts +19 -0
- package/dist/client.js +43 -0
- package/dist/commerce/builders.d.ts +99 -0
- package/dist/commerce/builders.js +208 -0
- package/dist/commerce/currency.d.ts +4 -0
- package/dist/commerce/currency.js +25 -0
- package/dist/commerce/items.d.ts +119 -0
- package/dist/commerce/items.js +280 -0
- package/dist/commerce/lint.d.ts +16 -0
- package/dist/commerce/lint.js +56 -0
- package/dist/errors.d.ts +61 -0
- package/dist/errors.js +63 -0
- package/dist/generated/openapi.d.ts +8360 -0
- package/dist/generated/openapi.js +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +6 -0
- package/dist/internal/decode.d.ts +20 -0
- package/dist/internal/decode.js +380 -0
- package/dist/internal/json.d.ts +3 -0
- package/dist/internal/json.js +74 -0
- package/dist/internal/time.d.ts +10 -0
- package/dist/internal/time.js +70 -0
- package/dist/internal/version.d.ts +2 -0
- package/dist/internal/version.js +2 -0
- package/dist/polling.d.ts +11 -0
- package/dist/polling.js +137 -0
- package/dist/qr/encoder.d.ts +10 -0
- package/dist/qr/encoder.js +343 -0
- package/dist/qr/index.d.ts +55 -0
- package/dist/qr/index.js +150 -0
- package/dist/receipts/bundle.d.ts +123 -0
- package/dist/receipts/bundle.js +423 -0
- package/dist/receipts/index.d.ts +8 -0
- package/dist/receipts/index.js +3 -0
- package/dist/receipts/json.d.ts +21 -0
- package/dist/receipts/json.js +151 -0
- package/dist/receipts/key-cache.d.ts +27 -0
- package/dist/receipts/key-cache.js +109 -0
- package/dist/receipts/renderers.d.ts +57 -0
- package/dist/receipts/renderers.js +153 -0
- package/dist/receipts/resource.d.ts +38 -0
- package/dist/receipts/resource.js +145 -0
- package/dist/receipts/verify.d.ts +112 -0
- package/dist/receipts/verify.js +339 -0
- package/dist/sessions.d.ts +18 -0
- package/dist/sessions.js +151 -0
- package/dist/transport.d.ts +30 -0
- package/dist/transport.js +511 -0
- package/dist/types.d.ts +283 -0
- package/dist/types.js +8 -0
- package/dist/webhook-resource.d.ts +12 -0
- package/dist/webhook-resource.js +51 -0
- package/dist/webhooks/index.d.ts +21 -0
- package/dist/webhooks/index.js +183 -0
- package/package.json +82 -4
package/README.md
CHANGED
|
@@ -1,3 +1,328 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Sess SDK
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Ask a person a question from your server and continue with their verified answer.
|
|
4
|
+
|
|
5
|
+
**Create and run** a small agent:
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm create @sess.si/agent@next my-agent
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
**What you'll see:** a Sess link. Answer two questions on your phone and your plan prints in the
|
|
12
|
+
terminal with a verified receipt.
|
|
13
|
+
|
|
14
|
+
**Install** the SDK in your own server project:
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
npm install --save-exact @sess.si/sdk@next
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Ask a person
|
|
21
|
+
|
|
22
|
+
Use a developer API key from [the Sess developer portal](https://app.sess.si). Keep it on the server.
|
|
23
|
+
The [hosted no-signup demo](https://sess.si) is a separate trial flow; a Sess link or trial credential
|
|
24
|
+
is not an owner API key. You need no LLM, agent framework, Docker or Sess source checkout.
|
|
25
|
+
|
|
26
|
+
<!-- example:ask -->
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { Sess } from '@sess.si/sdk';
|
|
30
|
+
const sess = new Sess({ apiKey: process.env.SESS_API_KEY });
|
|
31
|
+
const question = await sess.ask({
|
|
32
|
+
question: 'Approve the synthetic refund?',
|
|
33
|
+
fields: [
|
|
34
|
+
{ name: 'approved', label: 'Approve it', type: 'boolean' },
|
|
35
|
+
{ name: 'amount', label: 'Refund amount', type: 'number', minimum: 0 },
|
|
36
|
+
],
|
|
37
|
+
});
|
|
38
|
+
console.log(`Send this Sess link to the person: ${question.link}`);
|
|
39
|
+
const { approved, amount } = await question.answer(); // verified; the Session is now complete
|
|
40
|
+
if (approved) console.log(`Refund of ${amount.toFixed(2)} approved.`);
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
<!-- /example:ask -->
|
|
44
|
+
|
|
45
|
+
`sess.ask()` checks the question locally, creates the Session once and returns its Sess link
|
|
46
|
+
before anything waits. The link is a bearer capability: deliver it only to the person, never to
|
|
47
|
+
logs or analytics. `answer()` waits for the person (ten minutes by default), verifies the signed
|
|
48
|
+
receipt against the exact question, checks that the answer is still the current step, completes the
|
|
49
|
+
Session and returns the values typed from your fields. Completion closes the Session: the link no
|
|
50
|
+
longer accepts answers. Field types are `text`, `number`, `integer`, `boolean`, `choice` and
|
|
51
|
+
`multi_choice` ([API reference](docs/api.md#ask-one-question)).
|
|
52
|
+
|
|
53
|
+
If the wait times out (`wait_ended`, code `WAIT_TIMEOUT`) or you abort it, nothing changes remotely:
|
|
54
|
+
call `answer()` again on the same handle. An error with `outcome: "unknown"` means completion is not
|
|
55
|
+
confirmed; calling `answer()` again replays the same completion. The handle lives in memory only. To
|
|
56
|
+
resume after a restart, persist `sessionId` and `revision` and continue with the API below
|
|
57
|
+
([Integration](docs/integration.md#one-question-with-sessask)).
|
|
58
|
+
|
|
59
|
+
If Sess refuses a call, the error says what happened and what to do. A key Sess does not accept
|
|
60
|
+
prints: "Sess did not accept your API key. Check SESS_API_KEY and the portal you made it on." The
|
|
61
|
+
message comes first; the code and request ID follow it:
|
|
62
|
+
|
|
63
|
+
<!-- example:ask-error -->
|
|
64
|
+
|
|
65
|
+
```js
|
|
66
|
+
try {
|
|
67
|
+
const question = await sess.ask({
|
|
68
|
+
question: 'Approve the synthetic refund?',
|
|
69
|
+
fields: [{ name: 'approved', label: 'Approve it', type: 'boolean' }],
|
|
70
|
+
});
|
|
71
|
+
console.log(`Send this Sess link to the person: ${question.link}`);
|
|
72
|
+
} catch (error) {
|
|
73
|
+
console.error(error.message); // what happened and what to do
|
|
74
|
+
const request = error.requestId ? `, request ${error.requestId}` : '';
|
|
75
|
+
console.error(`Details: ${error.code ?? error.kind}${request}`);
|
|
76
|
+
process.exitCode = 1;
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
<!-- /example:ask-error -->
|
|
81
|
+
|
|
82
|
+
## Keep it short
|
|
83
|
+
|
|
84
|
+
A person reads the page on a phone, so Sess keeps every piece of it short. Lengths count
|
|
85
|
+
characters (Unicode code points), and Sess never truncates:
|
|
86
|
+
|
|
87
|
+
| What | Style | At most |
|
|
88
|
+
| --------------------------- | ------------------------------ | -------- |
|
|
89
|
+
| Title (`question`) | A question or a task, about 40 | 80 |
|
|
90
|
+
| Button (`submit_label`) | A verb, 1–3 words | 24 |
|
|
91
|
+
| Field label, option | Short | 60 |
|
|
92
|
+
| Help (field `description`) | One hint | 160 |
|
|
93
|
+
| Description under the title | One or two sentences | 280 |
|
|
94
|
+
| Heading | Short | 60 |
|
|
95
|
+
| Record caption | A few words | 200 |
|
|
96
|
+
| Record key / string value | As written | 40 / 200 |
|
|
97
|
+
| Text (`details`) | Plain text | 4,000 |
|
|
98
|
+
|
|
99
|
+
There are at most 5 buttons. A value over its limit fails with a `SessError` that names it:
|
|
100
|
+
`field` is a JSON pointer such as `/question`, `limit` is the most it may have and `length` is
|
|
101
|
+
what it has. The message says how to fix it: "The title can be at most 80 characters. Keep it
|
|
102
|
+
to a short heading, about 40." for the title, and for anything else, for example
|
|
103
|
+
"/fields/0/label is 72 characters; the limit is 60. Shorten it to at most 60 characters." Nothing
|
|
104
|
+
was created (`outcome` is `not_sent` or `rejected`), so shorten the value and ask again:
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
import { Sess, SessError } from '@sess.si/sdk';
|
|
108
|
+
declare function shorten(text: string, max: number): Promise<string>; // e.g. your model rewrites it
|
|
109
|
+
|
|
110
|
+
const sess = new Sess({ apiKey: process.env.SESS_API_KEY });
|
|
111
|
+
const fields = [{ name: 'approved', label: 'Approve it', type: 'boolean' }] as const;
|
|
112
|
+
async function askShort(question: string) {
|
|
113
|
+
try {
|
|
114
|
+
return await sess.ask({ question, fields });
|
|
115
|
+
} catch (error) {
|
|
116
|
+
if (!(error instanceof SessError) || error.field !== '/question' || error.limit === undefined)
|
|
117
|
+
throw error;
|
|
118
|
+
return sess.ask({ question: await shorten(question, error.limit), fields });
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
**Release status:** every SDK release so far is a `0.1.0` release candidate, served under the `next`
|
|
124
|
+
dist-tag. There is no stable release yet: do not install the bare name, whose `latest` tag points at a
|
|
125
|
+
deprecated `0.0.0-stage` placeholder. The [changelog](CHANGELOG.md) describes each version;
|
|
126
|
+
`npm view @sess.si/sdk dist-tags` shows what the registry serves now.
|
|
127
|
+
|
|
128
|
+
## Commerce
|
|
129
|
+
|
|
130
|
+
Sess takes no payment: the person approves a bill on a Sess page, and moving the money stays with
|
|
131
|
+
you and your payment processor.
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
import { Sess, invoice } from '@sess.si/sdk';
|
|
135
|
+
const sess = new Sess({ apiKey: process.env.SESS_API_KEY });
|
|
136
|
+
const created = await sess.sessions.create({
|
|
137
|
+
interaction: invoice({
|
|
138
|
+
currency: 'CAD', // whole cents: 420000 is $4,200.00
|
|
139
|
+
reference: 'INV-2041',
|
|
140
|
+
rows: [{ name: 'Design retainer, October', amount: 420000 }],
|
|
141
|
+
lines: [{ label: 'GST 5%', amount: 21000, role: 'tax' }],
|
|
142
|
+
}),
|
|
143
|
+
});
|
|
144
|
+
console.log(`Send this Sess link to the person: ${created.data.human_url}`);
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Three things you will meet first:
|
|
148
|
+
|
|
149
|
+
- **Short rows.** A row name has at most 80 characters and its detail at most 160. A longer value
|
|
150
|
+
is refused, never trimmed: the `SessError` names it by its pointer, such as
|
|
151
|
+
`/interaction/source/components/1/rows/0/name`, with `limit` and `length`.
|
|
152
|
+
- **Whole minor units.** Amounts are integers in the currency's smallest unit: cents for USD, yen
|
|
153
|
+
for JPY, fils for BHD. `money(1250, 'USD')` is $12.50; `money(12.5, 'USD')` throws, because Sess
|
|
154
|
+
never rounds or converts.
|
|
155
|
+
- **Exact totals.** The builders compute the subtotal, total and amount due from your rows and
|
|
156
|
+
lines. An items component you write by hand whose subtotal is not the sum of its rows is refused
|
|
157
|
+
with HTTP 400:
|
|
158
|
+
|
|
159
|
+
```json
|
|
160
|
+
{
|
|
161
|
+
"error": {
|
|
162
|
+
"code": "INVALID_SCHEMA",
|
|
163
|
+
"field": "/interaction/source/components/0/totals/0/amount",
|
|
164
|
+
"expected": 441250,
|
|
165
|
+
"actual": 451248
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
`invoice()`, `receipt()`, `quote()`, `refund()` and `purchaseOrder()` each return an ordinary
|
|
171
|
+
interactive Surface for `sess.sessions.create({ interaction })`: the bill as an `items` card, the
|
|
172
|
+
document's `reference` under a plain name, and the buttons the person needs (Approve and Decline;
|
|
173
|
+
Continue on a receipt; Accept on a quote). `refund()` states positive amounts under "Refund total".
|
|
174
|
+
A row `quantity` lets the person change it and adds a Request changes button; your agent replies
|
|
175
|
+
with a new revision and updated amounts. `items()` builds just the card, and `lintText()` finds a
|
|
176
|
+
Markdown table in text, which the person would see as pipes. See the
|
|
177
|
+
[API reference](docs/api.md#commerce).
|
|
178
|
+
|
|
179
|
+
What Sess does not do: take or move payment, convert currencies, compute tax or prices, show your
|
|
180
|
+
logo or branding, or let another site frame the page (`frame-ancestors 'none'`).
|
|
181
|
+
|
|
182
|
+
## Advanced: the full Surface API
|
|
183
|
+
|
|
184
|
+
<!-- example:quickstart -->
|
|
185
|
+
|
|
186
|
+
```ts
|
|
187
|
+
import { Sess } from '@sess.si/sdk';
|
|
188
|
+
|
|
189
|
+
const sess = new Sess({ apiKey: process.env.SESS_API_KEY });
|
|
190
|
+
const created = await sess.sessions.create({
|
|
191
|
+
interaction: {
|
|
192
|
+
mode: 'interactive',
|
|
193
|
+
title: 'Review a synthetic invoice',
|
|
194
|
+
source: {
|
|
195
|
+
kind: 'sess_surface',
|
|
196
|
+
components: [
|
|
197
|
+
{ type: 'record', label: 'INV-001', fields: { amount: 120 } },
|
|
198
|
+
{
|
|
199
|
+
type: 'field',
|
|
200
|
+
name: 'amount',
|
|
201
|
+
label: 'Correct amount',
|
|
202
|
+
required: true,
|
|
203
|
+
schema: { type: 'number', minimum: 0 },
|
|
204
|
+
value: 120,
|
|
205
|
+
},
|
|
206
|
+
],
|
|
207
|
+
},
|
|
208
|
+
actions: [{ id: 'submit', label: 'Submit review', intent: 'submit' }],
|
|
209
|
+
},
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
// Privately deliver created.data.human_url through your application's handoff.
|
|
213
|
+
// It is a bearer capability: do not put it in ordinary logs or analytics.
|
|
214
|
+
const answer = await sess.sessions.waitForResponse(created.data.id, {
|
|
215
|
+
revision: created.data.revision,
|
|
216
|
+
});
|
|
217
|
+
const verified = await sess.receipts.verifyResponse({
|
|
218
|
+
sessionId: created.data.id,
|
|
219
|
+
response: answer.response,
|
|
220
|
+
});
|
|
221
|
+
const amount = verified.response.data.amount;
|
|
222
|
+
if (typeof amount !== 'number' || !Number.isFinite(amount)) {
|
|
223
|
+
throw new Error('Expected an accepted numeric amount');
|
|
224
|
+
}
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
<!-- /example:quickstart -->
|
|
228
|
+
|
|
229
|
+
Verification checks that the receipt was issued for your workspace. Sess names your workspace on
|
|
230
|
+
authenticated responses (the `Sess-Workspace-Id` header), so you set nothing. If a runtime does not
|
|
231
|
+
send it, set `expectedWorkspaceId` from trusted deployment settings; otherwise verification fails
|
|
232
|
+
with `VERIFICATION_CONTEXT_MISSING`. The SDK never takes the workspace from the unverified receipt.
|
|
233
|
+
|
|
234
|
+
The [quickstart function](examples/quickstart.ts) includes a private delivery callback.
|
|
235
|
+
[Three-round continuation](examples/multi-round.ts) persists mutation intent and checks that an
|
|
236
|
+
answer is still the current expected business step before publishing or completing. Its store
|
|
237
|
+
and worker lease are application-owned. See [Integration](docs/integration.md).
|
|
238
|
+
|
|
239
|
+
## Check a decision bundle
|
|
240
|
+
|
|
241
|
+
A decision bundle is one accepted decision that its owner chose to share: the signed receipt, the
|
|
242
|
+
exact Surface and input, and the issuer's public keys. Check it on your own computer, with keys you
|
|
243
|
+
trust (never the keys inside the bundle):
|
|
244
|
+
|
|
245
|
+
```sh
|
|
246
|
+
npx -p @sess.si/sdk@0.1.0-rc.5 sess verify bundle.json --issuer https://api.sess.si --keys sess-receipt-keys.json
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
The `sess` command first ships in `0.1.0-rc.5`; name that version (or a later one) exactly, because
|
|
250
|
+
the bare package name resolves to the `latest` placeholder, which has no command.
|
|
251
|
+
|
|
252
|
+
You'll see `Valid decision bundle.` and what was accepted, when, on which revision; or `Invalid`
|
|
253
|
+
and the first reason. Exit status is 0 valid, 1 invalid, 2 usage error; add `--json` for the full
|
|
254
|
+
result. Nothing is sent anywhere. In code, use `verifyDecisionBundle` from `@sess.si/sdk/receipts`
|
|
255
|
+
([Security](docs/security.md#decision-bundles)). Formats: [receipt-v1](docs/spec/receipt-v1.md) and
|
|
256
|
+
[bundle-v1](docs/spec/bundle-v1.md).
|
|
257
|
+
|
|
258
|
+
## Show the link as a QR code
|
|
259
|
+
|
|
260
|
+
When the person is at a laptop and would answer on their phone, draw the link as a QR code, locally,
|
|
261
|
+
with no network call:
|
|
262
|
+
|
|
263
|
+
```ts
|
|
264
|
+
import { printQr, qr } from '@sess.si/sdk/qr';
|
|
265
|
+
|
|
266
|
+
const link = 'https://sess.si/s/synthetic-link'; // the link sess.ask() returned
|
|
267
|
+
console.log(link);
|
|
268
|
+
printQr(link); // prints "Scan to answer on your phone" and the code, on an interactive terminal only
|
|
269
|
+
const svg = qr(link, { format: 'svg' }); // also 'png' (bytes) and 'terminal' (text)
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
Use it where a person sees a screen but not their phone: a desktop chat window, a dashboard, a kiosk,
|
|
273
|
+
print. Skip it in SMS, or when the person is already on their phone. Sess never shows or sends the
|
|
274
|
+
code; your agent chooses. `printQr` prints nothing without a TTY, in CI (`CI` set), in production
|
|
275
|
+
(`NODE_ENV=production`), with `--no-qr` or with `{ qr: false }`, and draws without colour under
|
|
276
|
+
`NO_COLOR`. The code is the private link in another form: keep it wherever you keep the link, and
|
|
277
|
+
never in logs, analytics or a URL.
|
|
278
|
+
|
|
279
|
+
## Runtime and imports
|
|
280
|
+
|
|
281
|
+
Supported: maintained Node 22 releases at or above 22.14, and Node 24 (recommended).
|
|
282
|
+
TypeScript consumer declarations target 5.9 and current stable. JavaScript consumers need no compiler.
|
|
283
|
+
The package contains one ESM build. CommonJS applications use `await import('@sess.si/sdk')`, as in the
|
|
284
|
+
[CommonJS example](examples/commonjs.cjs); synchronous `require` is not a supported contract.
|
|
285
|
+
|
|
286
|
+
Entry points are `@sess.si/sdk`, `@sess.si/sdk/receipts`, `@sess.si/sdk/webhooks` and `@sess.si/sdk/qr`. Importing or constructing
|
|
287
|
+
the SDK does not contact a service, start a server, read credential files or log data.
|
|
288
|
+
|
|
289
|
+
## Errors and retries
|
|
290
|
+
|
|
291
|
+
Errors are `SessError` with stable `kind`, API `code`, `status`, `requestId`, `attempts` and write
|
|
292
|
+
`outcome` fields when applicable. Server bodies and request secrets are omitted from default errors.
|
|
293
|
+
|
|
294
|
+
Session creation is attempted once. An unknown outcome may mean a Session exists whose private
|
|
295
|
+
URL was lost. Never automatically create again. If a valid creation body arrived with defective
|
|
296
|
+
headers, explicitly inspect `error.getRecoveryResult()` and protect that capability.
|
|
297
|
+
|
|
298
|
+
Publish and complete require a strong quoted `ifMatch`. Persist the body, original ETag and explicit
|
|
299
|
+
`idempotencyKey` before a durable write. Internal retries retain those values and the same resolved
|
|
300
|
+
credential. A 409 or 412 is returned to the caller; the SDK never refreshes an ETag to force a write.
|
|
301
|
+
|
|
302
|
+
## Direct HTTP remains supported
|
|
303
|
+
|
|
304
|
+
The same create operation is available without the SDK:
|
|
305
|
+
|
|
306
|
+
```sh
|
|
307
|
+
curl --request POST https://api.sess.si/v1/sessions \
|
|
308
|
+
--header "Authorization: Bearer $SESS_API_KEY" \
|
|
309
|
+
--header 'Content-Type: application/json' \
|
|
310
|
+
--data-binary @interaction.json
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
Here `interaction.json` contains the same `{ "interaction": ... }` request body. The response
|
|
314
|
+
contains a private human URL; protect terminal output and disable request tracing. The complete
|
|
315
|
+
contract is served at [OpenAPI](https://api.sess.si/openapi.json).
|
|
316
|
+
|
|
317
|
+
## Guides
|
|
318
|
+
|
|
319
|
+
- [API reference](docs/api.md): supported operations, validated responses, options and errors.
|
|
320
|
+
- [Integration](docs/integration.md): authoring, lifecycle, validation, durability and future boundaries.
|
|
321
|
+
- [Security](docs/security.md) and [receipt example](examples/receipt-verification.ts).
|
|
322
|
+
- [Webhooks](docs/webhooks.md) and [Node receiver with a durable-inbox boundary](examples/webhook-receiver.ts).
|
|
323
|
+
- [Compatibility](docs/compatibility.md), [migrating to the registry package](docs/migration.md) and [release runbook](docs/releasing.md).
|
|
324
|
+
- [Plain-Node quickstart](examples/quickstart-node/README.md): a complete program, from the first call to the verified answer.
|
|
325
|
+
|
|
326
|
+
Sess renders the page the person sees; this SDK has no appearance settings.
|
|
327
|
+
|
|
328
|
+
[Contribute](CONTRIBUTING.md) · [Security reporting](SECURITY.md) · [Apache-2.0](LICENSE)
|
package/dist/ask.d.ts
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import type { ReceiptResource } from './receipts/resource.js';
|
|
2
|
+
import type { Sessions } from './sessions.js';
|
|
3
|
+
import type { InteractiveInteraction, RequestOptions } from './types.js';
|
|
4
|
+
interface AskFieldBase {
|
|
5
|
+
/** Key of this field in the answer: `^[a-z][a-z0-9_]{0,63}$`. */
|
|
6
|
+
readonly name: string;
|
|
7
|
+
/** What the field asks for: short, a few words; at most 60. Example: `Refund amount` */
|
|
8
|
+
readonly label: string;
|
|
9
|
+
/** Default `true`, except boolean fields (default `false`). */
|
|
10
|
+
readonly required?: boolean;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* One simple field of a single question. Each kind compiles to the ordinary interactive field
|
|
14
|
+
* with the runtime names, limits and schema semantics. `default` is a proposal the person can
|
|
15
|
+
* correct; it must itself be a valid value.
|
|
16
|
+
*/
|
|
17
|
+
export type AskField = (AskFieldBase & {
|
|
18
|
+
readonly type: 'text';
|
|
19
|
+
readonly multiline?: boolean;
|
|
20
|
+
readonly default?: string;
|
|
21
|
+
}) | (AskFieldBase & {
|
|
22
|
+
readonly type: 'number' | 'integer';
|
|
23
|
+
readonly minimum?: number;
|
|
24
|
+
readonly maximum?: number;
|
|
25
|
+
readonly default?: number;
|
|
26
|
+
}) | (AskFieldBase & {
|
|
27
|
+
readonly type: 'boolean';
|
|
28
|
+
readonly default?: boolean;
|
|
29
|
+
}) | (AskFieldBase & {
|
|
30
|
+
readonly type: 'choice';
|
|
31
|
+
/** 1–100 choices. The words shown for one choice: short; at most 60. Example: `Next Monday` */
|
|
32
|
+
readonly options: readonly [string, ...string[]];
|
|
33
|
+
readonly default?: string;
|
|
34
|
+
}) | (AskFieldBase & {
|
|
35
|
+
readonly type: 'multi_choice';
|
|
36
|
+
/** 1–100 choices. The words shown for one choice: short; at most 60. Example: `Next Monday` */
|
|
37
|
+
readonly options: readonly [string, ...string[]];
|
|
38
|
+
readonly default?: readonly string[];
|
|
39
|
+
});
|
|
40
|
+
/**
|
|
41
|
+
* A question plus typed fields: the same shape as the MCP `sess_ask` tool input. Lengths are in
|
|
42
|
+
* Unicode code points and are never truncated: a value over its limit throws a `SessError` whose
|
|
43
|
+
* `field`, `limit` and `length` say which value to shorten and to what.
|
|
44
|
+
*/
|
|
45
|
+
export interface AskInput<F extends readonly AskField[] = readonly AskField[]> {
|
|
46
|
+
/**
|
|
47
|
+
* The page's heading: a question or a task, about 40 characters; at most 80.
|
|
48
|
+
* Examples: `Approve the refund?`, `Pick a delivery date`
|
|
49
|
+
*/
|
|
50
|
+
readonly question: string;
|
|
51
|
+
/** Optional plain-text context shown above the fields, at most 4,000 characters. */
|
|
52
|
+
readonly details?: string;
|
|
53
|
+
/** A button: a verb, 1–3 words; at most 24. Default `Send`. Examples: `Approve`, `Send refund` */
|
|
54
|
+
readonly submit_label?: string;
|
|
55
|
+
/** One to twelve fields with unique names. */
|
|
56
|
+
readonly fields: F;
|
|
57
|
+
/**
|
|
58
|
+
* Advanced: how long the link stays open, in whole seconds from creation: from 30 up to your
|
|
59
|
+
* plan's link lifetime (Sess checks the ceiling). Absent: the plan's lifetime.
|
|
60
|
+
*/
|
|
61
|
+
readonly expires_in?: number;
|
|
62
|
+
}
|
|
63
|
+
type AskValue<T> = T extends {
|
|
64
|
+
type: 'text';
|
|
65
|
+
} ? string : T extends {
|
|
66
|
+
type: 'number' | 'integer';
|
|
67
|
+
} ? number : T extends {
|
|
68
|
+
type: 'boolean';
|
|
69
|
+
} ? boolean : T extends {
|
|
70
|
+
type: 'choice';
|
|
71
|
+
options: readonly (infer O extends string)[];
|
|
72
|
+
} ? O : T extends {
|
|
73
|
+
type: 'multi_choice';
|
|
74
|
+
options: readonly (infer O extends string)[];
|
|
75
|
+
} ? readonly O[] : never;
|
|
76
|
+
/** A checkbox and a multiple choice always have a value; other fields only when required. */
|
|
77
|
+
type AlwaysPresent<T> = T extends {
|
|
78
|
+
type: 'boolean' | 'multi_choice';
|
|
79
|
+
} ? true : T extends {
|
|
80
|
+
required: false;
|
|
81
|
+
} ? false : T extends {
|
|
82
|
+
required: true;
|
|
83
|
+
} ? true : T extends {
|
|
84
|
+
required: boolean;
|
|
85
|
+
} ? false : true;
|
|
86
|
+
type Simplify<T> = {
|
|
87
|
+
[K in keyof T]: T[K];
|
|
88
|
+
} & {};
|
|
89
|
+
/** The verified answer type inferred from the field definitions (and checked at runtime). */
|
|
90
|
+
export type AskAnswer<F extends readonly AskField[]> = Simplify<{
|
|
91
|
+
readonly [P in F[number] as AlwaysPresent<P> extends true ? P['name'] : never]: AskValue<P>;
|
|
92
|
+
} & {
|
|
93
|
+
readonly [P in F[number] as AlwaysPresent<P> extends true ? never : P['name']]?: AskValue<P>;
|
|
94
|
+
}>;
|
|
95
|
+
export interface AskOptions extends RequestOptions {
|
|
96
|
+
/** Trusted expected receipt audience for this question; defaults to the client configuration. */
|
|
97
|
+
expectedWorkspaceId?: string;
|
|
98
|
+
}
|
|
99
|
+
export interface AskAnswerOptions {
|
|
100
|
+
/** Total local wait for the person, default ten minutes. Ending it never closes the Session. */
|
|
101
|
+
timeoutMs?: number;
|
|
102
|
+
/** Polling interval, default 1,500 ms plus jitter, minimum 1,000 ms. */
|
|
103
|
+
intervalMs?: number;
|
|
104
|
+
/** Ends the local wait of this call only. */
|
|
105
|
+
signal?: AbortSignal;
|
|
106
|
+
}
|
|
107
|
+
/** One question: its private link now, its verified answer later. */
|
|
108
|
+
export interface AskHandle<T> {
|
|
109
|
+
/** Private bearer link for the person. Deliver it only to them; keep it out of logs. */
|
|
110
|
+
readonly link: string;
|
|
111
|
+
readonly sessionId: string;
|
|
112
|
+
/** The Surface revision of the question, for recovery through the advanced API. */
|
|
113
|
+
readonly revision: number;
|
|
114
|
+
/**
|
|
115
|
+
* When the link stops taking answers (`action_expires_at`, ISO 8601), as created. On a link of
|
|
116
|
+
* 20 hours or less the person may ask for more time, which moves it forward up to a fixed
|
|
117
|
+
* horizon; while the link is lapsed (past it, still extendable) `answer()` keeps waiting. An
|
|
118
|
+
* answer accepted before the end is still returned after it; with none, `answer()` ends with
|
|
119
|
+
* `wait_ended` code `LINK_EXPIRED` once the link has expired. Null only from a runtime that
|
|
120
|
+
* does not report it.
|
|
121
|
+
*/
|
|
122
|
+
readonly expiresAt: string | null;
|
|
123
|
+
/**
|
|
124
|
+
* Waits for the answer, verifies its signed receipt and exact bindings, checks that it is still
|
|
125
|
+
* the current step, completes (closes) the Session, then returns the typed answer.
|
|
126
|
+
*/
|
|
127
|
+
answer(options?: AskAnswerOptions): Promise<T>;
|
|
128
|
+
}
|
|
129
|
+
declare const TYPES: readonly ["text", "number", "integer", "boolean", "choice", "multi_choice"];
|
|
130
|
+
type AskType = (typeof TYPES)[number];
|
|
131
|
+
/** Internal compiled field: what an accepted answer is checked against. */
|
|
132
|
+
interface CompiledField {
|
|
133
|
+
name: string;
|
|
134
|
+
type: AskType;
|
|
135
|
+
required: boolean;
|
|
136
|
+
options?: readonly string[];
|
|
137
|
+
minimum?: number;
|
|
138
|
+
maximum?: number;
|
|
139
|
+
}
|
|
140
|
+
export interface CompiledAsk {
|
|
141
|
+
interaction: InteractiveInteraction;
|
|
142
|
+
fields: readonly CompiledField[];
|
|
143
|
+
/** The link lifetime in seconds, when the question set one. */
|
|
144
|
+
expiresIn?: number;
|
|
145
|
+
}
|
|
146
|
+
/** The shortest link lifetime Sess accepts, in seconds (the ceiling is the plan's). */
|
|
147
|
+
export declare const LINK_LIFETIME_FLOOR_SECONDS = 30;
|
|
148
|
+
/**
|
|
149
|
+
* The one deterministic simple-field compiler: validates a question locally, without network,
|
|
150
|
+
* credential or clock access, and expands it into the ordinary interactive Surface.
|
|
151
|
+
*/
|
|
152
|
+
export declare function compileAsk(input: unknown): CompiledAsk;
|
|
153
|
+
/**
|
|
154
|
+
* Checks verified answer data against the compiled fields and returns a frozen copy in field
|
|
155
|
+
* order. It follows the runtime decoding: a checkbox is always present, a multiple choice is
|
|
156
|
+
* always a list, and an empty optional value is absent.
|
|
157
|
+
*/
|
|
158
|
+
export declare function checkAnswer(fields: readonly CompiledField[], data: unknown): Readonly<Record<string, unknown>>;
|
|
159
|
+
/** Validates locally, creates once (the link is available before any wait) and returns the handle. */
|
|
160
|
+
export declare function ask<T>(sessions: Sessions, receipts: ReceiptResource, input: unknown, options?: AskOptions): Promise<AskHandle<T>>;
|
|
161
|
+
export {};
|