@elizaos/plugin-form 2.0.0-beta.1 → 2.0.3-beta.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/LICENSE +21 -0
- package/README.md +134 -0
- package/package.json +22 -4
- package/registry-entry.json +26 -0
- package/dist/actions/restore.d.ts +0 -25
- package/dist/actions/restore.d.ts.map +0 -1
- package/dist/actions/restore.js +0 -176
- package/dist/actions/restore.js.map +0 -1
- package/dist/builder.d.ts +0 -320
- package/dist/builder.d.ts.map +0 -1
- package/dist/builder.js +0 -458
- package/dist/builder.js.map +0 -1
- package/dist/builtins.d.ts +0 -128
- package/dist/builtins.d.ts.map +0 -1
- package/dist/builtins.js +0 -233
- package/dist/builtins.js.map +0 -1
- package/dist/defaults.d.ts +0 -95
- package/dist/defaults.d.ts.map +0 -1
- package/dist/defaults.js +0 -79
- package/dist/defaults.js.map +0 -1
- package/dist/evaluators/extractor.d.ts +0 -28
- package/dist/evaluators/extractor.d.ts.map +0 -1
- package/dist/evaluators/extractor.js +0 -247
- package/dist/evaluators/extractor.js.map +0 -1
- package/dist/extraction.d.ts +0 -55
- package/dist/extraction.d.ts.map +0 -1
- package/dist/extraction.js +0 -331
- package/dist/extraction.js.map +0 -1
- package/dist/index.d.ts +0 -31
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -144
- package/dist/index.js.map +0 -1
- package/dist/providers/context.d.ts +0 -56
- package/dist/providers/context.d.ts.map +0 -1
- package/dist/providers/context.js +0 -206
- package/dist/providers/context.js.map +0 -1
- package/dist/service.d.ts +0 -402
- package/dist/service.d.ts.map +0 -1
- package/dist/service.js +0 -1158
- package/dist/service.js.map +0 -1
- package/dist/storage.d.ts +0 -228
- package/dist/storage.d.ts.map +0 -1
- package/dist/storage.js +0 -218
- package/dist/storage.js.map +0 -1
- package/dist/template.d.ts +0 -10
- package/dist/template.d.ts.map +0 -1
- package/dist/template.js +0 -60
- package/dist/template.js.map +0 -1
- package/dist/ttl.d.ts +0 -144
- package/dist/ttl.d.ts.map +0 -1
- package/dist/ttl.js +0 -85
- package/dist/ttl.js.map +0 -1
- package/dist/types.d.ts +0 -1213
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -39
- package/dist/types.js.map +0 -1
- package/dist/validation.d.ts +0 -156
- package/dist/validation.d.ts.map +0 -1
- package/dist/validation.js +0 -289
- package/dist/validation.js.map +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Shaw Walters and elizaOS Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# @elizaos/plugin-form
|
|
2
|
+
|
|
3
|
+
Conversational forms for Eliza agents. Define structured data-collection workflows; the agent extracts values from natural conversation, tracks completion, and delivers a typed `FormSubmission` when all required fields are filled.
|
|
4
|
+
|
|
5
|
+
## What it does
|
|
6
|
+
|
|
7
|
+
- **Defines forms** — a `FormDefinition` describes the fields to collect (`FormControl[]`), lifecycle hooks, TTL settings, and UX rules (undo, skip, autofill).
|
|
8
|
+
- **Tracks sessions** — one `FormSession` per user per room. Sessions survive topic changes and can be stashed and resumed later.
|
|
9
|
+
- **Extracts field values via LLM** — the post-turn evaluator detects user intent and pulls values from conversational messages, handling uncertain extractions with a confirm step.
|
|
10
|
+
- **Supports composite and external control types** — composite types (e.g. address) have subfields that must all fill before the parent is complete; external types (e.g. payment) await asynchronous confirmation from another service before marking the field filled.
|
|
11
|
+
- **Effort-aware retention** — session TTL scales with how long the user has invested (default 14–90 days).
|
|
12
|
+
|
|
13
|
+
## Capabilities added to an Eliza agent
|
|
14
|
+
|
|
15
|
+
| Capability | How |
|
|
16
|
+
|---|---|
|
|
17
|
+
| FORM\_CONTEXT provider | Injects current form progress into every agent turn — filled/missing fields, uncertain extractions, pending external fields, and a single action directive |
|
|
18
|
+
| FORM action | `action=restore` rehydrates the most recent stashed form before the agent responds. **Not auto-registered** — import `formAction` from `@elizaos/plugin-form` and add it to your consuming plugin's `actions` array to activate it. |
|
|
19
|
+
| Form evaluator | Post-turn: detects submit / stash / cancel / undo / skip / autofill / fill\_form intents and updates session state |
|
|
20
|
+
| FormService | Singleton service: register forms and custom control types, start/manage sessions, submit, stash, restore |
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
Add the plugin to your agent character config:
|
|
25
|
+
|
|
26
|
+
```json
|
|
27
|
+
{
|
|
28
|
+
"plugins": ["@elizaos/plugin-form"],
|
|
29
|
+
"features": {
|
|
30
|
+
"form": true
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
The plugin auto-enables when `config.features.form` is truthy (`true`, or an object whose `enabled` is not `false`). No environment variables are required.
|
|
36
|
+
|
|
37
|
+
## Built-in field types
|
|
38
|
+
|
|
39
|
+
`text`, `number`, `email`, `boolean`, `select`, `date`, `file`
|
|
40
|
+
|
|
41
|
+
Custom types are registered at runtime via `FormService.registerControlType()`.
|
|
42
|
+
|
|
43
|
+
## Registering a form (consuming plugin example)
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import type { FormDefinition } from '@elizaos/plugin-form';
|
|
47
|
+
|
|
48
|
+
const onboardingForm: FormDefinition = {
|
|
49
|
+
id: 'onboard',
|
|
50
|
+
name: 'Onboarding',
|
|
51
|
+
controls: [
|
|
52
|
+
{ key: 'name', label: 'Full Name', type: 'text', required: true },
|
|
53
|
+
{ key: 'email', label: 'Email', type: 'email', required: true },
|
|
54
|
+
{ key: 'role', label: 'Role', type: 'select', required: false,
|
|
55
|
+
options: [{ value: 'dev', label: 'Developer' }, { value: 'pm', label: 'Product' }] },
|
|
56
|
+
],
|
|
57
|
+
hooks: { onSubmit: 'handle_onboarding_submission' },
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// In your plugin's start() or action handler:
|
|
61
|
+
const formService = runtime.getService('FORM');
|
|
62
|
+
formService.registerForm(onboardingForm);
|
|
63
|
+
await formService.startSession('onboard', entityId, roomId);
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Register a task worker to handle the submission:
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
runtime.registerTaskWorker({
|
|
70
|
+
name: 'handle_onboarding_submission',
|
|
71
|
+
execute: async (runtime, { submission }) => {
|
|
72
|
+
// submission.values, submission.mappedValues, submission.entityId, etc.
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Registering a custom control type
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
// Simple type
|
|
81
|
+
formService.registerControlType({
|
|
82
|
+
id: 'phone',
|
|
83
|
+
validate: (v) => ({ valid: /^\+?[\d\s-]{10,}$/.test(String(v)) }),
|
|
84
|
+
extractionPrompt: 'a phone number with country code',
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// External type (async confirmation required)
|
|
88
|
+
formService.registerControlType({
|
|
89
|
+
id: 'payment',
|
|
90
|
+
getSubControls: () => [
|
|
91
|
+
{ key: 'amount', type: 'number', label: 'Amount', required: true },
|
|
92
|
+
{ key: 'currency', type: 'select', label: 'Currency', required: true,
|
|
93
|
+
options: [{ value: 'SOL', label: 'SOL' }, { value: 'USDC', label: 'USDC' }] },
|
|
94
|
+
],
|
|
95
|
+
activate: async (ctx) => {
|
|
96
|
+
// Return instructions and a reference for matching the external event
|
|
97
|
+
return { instructions: 'Send funds to ...', reference: 'memo-xyz', address: '...' };
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
When all subfields are filled the evaluator automatically calls `activateExternalField()`. Your service calls `formService.confirmExternalField(sessionId, entityId, field, value, externalData)` when confirmation arrives.
|
|
103
|
+
|
|
104
|
+
## FormDefinition reference (key fields)
|
|
105
|
+
|
|
106
|
+
| Field | Type | Default | Description |
|
|
107
|
+
|---|---|---|---|
|
|
108
|
+
| `id` | string | required | Unique form identifier |
|
|
109
|
+
| `name` | string | required | Human-readable name |
|
|
110
|
+
| `controls` | FormControl[] | required | Fields to collect |
|
|
111
|
+
| `status` | `draft\|active\|deprecated` | `active` | Draft forms cannot be started |
|
|
112
|
+
| `allowMultiple` | boolean | `false` | Allow multiple submissions per user |
|
|
113
|
+
| `hooks.onStart/onSubmit/onCancel/onReady/onFieldChange/onExpire` | string | — | Task worker names |
|
|
114
|
+
| `ttl.minDays` / `ttl.maxDays` | number | 14 / 90 | Retention bounds |
|
|
115
|
+
| `ttl.effortMultiplier` | number | 0.5 | Extra days per minute of interaction |
|
|
116
|
+
| `ux.allowUndo` / `allowSkip` / `allowAutofill` | boolean | all `true` | UX feature flags |
|
|
117
|
+
| `nudge.afterInactiveHours` / `maxNudges` | number | 48 / 3 | Stale-session nudge config |
|
|
118
|
+
|
|
119
|
+
## FormControl reference (key fields)
|
|
120
|
+
|
|
121
|
+
| Field | Type | Description |
|
|
122
|
+
|---|---|---|
|
|
123
|
+
| `key` | string | Unique within form; used in `values` map |
|
|
124
|
+
| `label` | string | Human-readable name |
|
|
125
|
+
| `type` | string | `text`, `number`, `email`, `boolean`, `select`, `date`, `file`, or custom |
|
|
126
|
+
| `required` | boolean | Form cannot submit without this field |
|
|
127
|
+
| `sensitive` | boolean | Value masked in agent context |
|
|
128
|
+
| `hidden` | boolean | Extract silently, never ask directly |
|
|
129
|
+
| `askPrompt` | string | Custom agent prompt for this field |
|
|
130
|
+
| `extractHints` | string[] | Keywords to improve LLM extraction accuracy |
|
|
131
|
+
| `confirmThreshold` | number | Confidence below this triggers confirmation (default 0.8) |
|
|
132
|
+
| `dependsOn` | FormControlDependency | Conditional display based on another field's value |
|
|
133
|
+
| `dbbind` | string | Column name for `mappedValues` in submission |
|
|
134
|
+
| `options` | FormControlOption[] | For `select` type |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elizaos/plugin-form",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.3-beta.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Eliza plugin-form: conversational forms as guardrails for guided user journeys.",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -8,12 +8,28 @@
|
|
|
8
8
|
"./package.json": "./package.json",
|
|
9
9
|
".": {
|
|
10
10
|
"types": "./dist/index.d.ts",
|
|
11
|
+
"eliza-source": {
|
|
12
|
+
"types": "./src/index.ts",
|
|
13
|
+
"import": "./src/index.ts",
|
|
14
|
+
"default": "./src/index.ts"
|
|
15
|
+
},
|
|
11
16
|
"import": "./dist/index.js",
|
|
12
17
|
"default": "./dist/index.js"
|
|
18
|
+
},
|
|
19
|
+
"./*.css": "./dist/*.css",
|
|
20
|
+
"./*": {
|
|
21
|
+
"types": "./dist/*.d.ts",
|
|
22
|
+
"eliza-source": {
|
|
23
|
+
"types": "./src/*.ts",
|
|
24
|
+
"import": "./src/*.ts",
|
|
25
|
+
"default": "./src/*.ts"
|
|
26
|
+
},
|
|
27
|
+
"import": "./dist/*.js",
|
|
28
|
+
"default": "./dist/*.js"
|
|
13
29
|
}
|
|
14
30
|
},
|
|
15
31
|
"dependencies": {
|
|
16
|
-
"@elizaos/core": "2.0.
|
|
32
|
+
"@elizaos/core": "2.0.3-beta.3",
|
|
17
33
|
"uuid": "^14.0.0"
|
|
18
34
|
},
|
|
19
35
|
"agentConfig": {
|
|
@@ -40,11 +56,12 @@
|
|
|
40
56
|
"build": "bun run build:js && bun run build:types",
|
|
41
57
|
"clean": "rm -rf dist",
|
|
42
58
|
"test": "vitest run --config vitest.config.ts",
|
|
43
|
-
"typecheck": "
|
|
59
|
+
"typecheck": "tsgo --noEmit -p tsconfig.json",
|
|
44
60
|
"build:js": "tsup --config ../tsup.plugin-packages.shared.ts",
|
|
45
61
|
"build:types": "tsc --noCheck -p tsconfig.build.json"
|
|
46
62
|
},
|
|
47
63
|
"files": [
|
|
64
|
+
"registry-entry.json",
|
|
48
65
|
"dist",
|
|
49
66
|
"auto-enable.ts"
|
|
50
67
|
],
|
|
@@ -53,5 +70,6 @@
|
|
|
53
70
|
"tsup": "^8.5.1",
|
|
54
71
|
"typescript": "^6.0.3",
|
|
55
72
|
"vitest": "^4.0.18"
|
|
56
|
-
}
|
|
73
|
+
},
|
|
74
|
+
"gitHead": "f54b0f4eaed317d59fa7dbcdce20f4cdb0734420"
|
|
57
75
|
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "form",
|
|
3
|
+
"name": "Form",
|
|
4
|
+
"description": "Structured conversational forms — FORM service, form context provider, form evaluator, and restore action provided by @elizaos/plugin-form.",
|
|
5
|
+
"npmName": "@elizaos/plugin-form",
|
|
6
|
+
"version": "2.0.0-beta.0",
|
|
7
|
+
"source": "bundled",
|
|
8
|
+
"tags": ["form", "conversational", "data-collection"],
|
|
9
|
+
"config": {},
|
|
10
|
+
"render": {
|
|
11
|
+
"visible": true,
|
|
12
|
+
"pinTo": [],
|
|
13
|
+
"style": "card",
|
|
14
|
+
"icon": "ClipboardList",
|
|
15
|
+
"group": "media",
|
|
16
|
+
"groupOrder": 8,
|
|
17
|
+
"actions": ["enable", "configure"]
|
|
18
|
+
},
|
|
19
|
+
"resources": {
|
|
20
|
+
"homepage": "https://elizaos.ai",
|
|
21
|
+
"repository": "https://github.com/elizaos/elizaos"
|
|
22
|
+
},
|
|
23
|
+
"dependsOn": [],
|
|
24
|
+
"kind": "plugin",
|
|
25
|
+
"subtype": "media"
|
|
26
|
+
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module actions/restore
|
|
3
|
-
* @description Planner action for restoring stashed form sessions.
|
|
4
|
-
*
|
|
5
|
-
* Restore is a planner-driven Action (not part of the post-message form
|
|
6
|
-
* evaluator) because the restored form context must reach the provider
|
|
7
|
-
* BEFORE the agent generates its response. If the user has an active form
|
|
8
|
-
* in the current room, the action asks them to continue or stash the
|
|
9
|
-
* current one. Multiple stashed forms restore the most recent.
|
|
10
|
-
*/
|
|
11
|
-
import { type Action } from "@elizaos/core";
|
|
12
|
-
/**
|
|
13
|
-
* Form Restore Action
|
|
14
|
-
*
|
|
15
|
-
* Fast-path action for restoring stashed forms.
|
|
16
|
-
* Preempts REPLY to provide immediate restoration with summary.
|
|
17
|
-
*
|
|
18
|
-
* WHY action:
|
|
19
|
-
* - Needs to run BEFORE provider
|
|
20
|
-
* - Must generate immediate response
|
|
21
|
-
* - Context needed for next message
|
|
22
|
-
*/
|
|
23
|
-
export declare const formRestoreAction: Action;
|
|
24
|
-
export default formRestoreAction;
|
|
25
|
-
//# sourceMappingURL=restore.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"restore.d.ts","sourceRoot":"","sources":["../../src/actions/restore.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EACL,KAAK,MAAM,EASZ,MAAM,eAAe,CAAC;AAYvB;;;;;;;;;;GAUG;AACH,eAAO,MAAM,iBAAiB,EAAE,MA4L/B,CAAC;AAEF,eAAe,iBAAiB,CAAC"}
|
package/dist/actions/restore.js
DELETED
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
logger
|
|
3
|
-
} from "@elizaos/core";
|
|
4
|
-
const RESTORE_FIELD_LIMIT = 12;
|
|
5
|
-
const RESTORE_RESPONSE_MAX_CHARS = 4e3;
|
|
6
|
-
function truncateRestoreResponse(text) {
|
|
7
|
-
return text.length <= RESTORE_RESPONSE_MAX_CHARS ? text : `${text.slice(0, RESTORE_RESPONSE_MAX_CHARS)}
|
|
8
|
-
|
|
9
|
-
[truncated restored form summary]`;
|
|
10
|
-
}
|
|
11
|
-
const formRestoreAction = {
|
|
12
|
-
name: "FORM_RESTORE",
|
|
13
|
-
contexts: ["tasks", "automation", "memory"],
|
|
14
|
-
contextGate: { anyOf: ["tasks", "automation", "memory"] },
|
|
15
|
-
roleGate: { minRole: "USER" },
|
|
16
|
-
similes: ["RESUME_FORM", "CONTINUE_FORM"],
|
|
17
|
-
description: "Restore a previously stashed form session",
|
|
18
|
-
descriptionCompressed: "Restore stashed form session.",
|
|
19
|
-
parameters: [
|
|
20
|
-
{
|
|
21
|
-
name: "sessionId",
|
|
22
|
-
description: "Optional stashed form session id to restore.",
|
|
23
|
-
required: false,
|
|
24
|
-
schema: { type: "string" }
|
|
25
|
-
}
|
|
26
|
-
],
|
|
27
|
-
/**
|
|
28
|
-
* Validate: action is selectable whenever the user has stashed sessions
|
|
29
|
-
* and no active form in the current room. The planner picks it via the
|
|
30
|
-
* action description/similes when the user actually wants to resume.
|
|
31
|
-
*/
|
|
32
|
-
validate: async (runtime, message, _state) => {
|
|
33
|
-
const formService = runtime.getService("FORM");
|
|
34
|
-
if (!formService) return false;
|
|
35
|
-
const entityId = message.entityId;
|
|
36
|
-
const roomId = message.roomId;
|
|
37
|
-
if (!entityId || !roomId) return false;
|
|
38
|
-
const stashed = await formService.getStashedSessions(entityId);
|
|
39
|
-
if (stashed.length === 0) return false;
|
|
40
|
-
const active = await formService.getActiveSession(entityId, roomId);
|
|
41
|
-
return active === null;
|
|
42
|
-
},
|
|
43
|
-
/**
|
|
44
|
-
* Handler: Restore the most recent stashed session.
|
|
45
|
-
*
|
|
46
|
-
* 1. Check for conflicts (active session in room)
|
|
47
|
-
* 2. Restore the session
|
|
48
|
-
* 3. Generate summary response
|
|
49
|
-
*
|
|
50
|
-
* @returns ActionResult with success status and session data
|
|
51
|
-
*/
|
|
52
|
-
handler: async (runtime, message, _state, _options, callback) => {
|
|
53
|
-
try {
|
|
54
|
-
const formService = runtime.getService("FORM");
|
|
55
|
-
if (!formService) {
|
|
56
|
-
await callback?.({
|
|
57
|
-
text: "Sorry, I couldn't find the form service."
|
|
58
|
-
});
|
|
59
|
-
return { success: false };
|
|
60
|
-
}
|
|
61
|
-
const entityId = message.entityId;
|
|
62
|
-
const roomId = message.roomId;
|
|
63
|
-
if (!entityId || !roomId) {
|
|
64
|
-
await callback?.({
|
|
65
|
-
text: "Sorry, I couldn't identify you."
|
|
66
|
-
});
|
|
67
|
-
return { success: false };
|
|
68
|
-
}
|
|
69
|
-
const existing = await formService.getActiveSession(entityId, roomId);
|
|
70
|
-
if (existing) {
|
|
71
|
-
const form2 = formService.getForm(existing.formId);
|
|
72
|
-
await callback?.({
|
|
73
|
-
text: `You already have an active form: "${form2?.name || existing.formId}". Would you like to continue with that one, or should I save it and restore your other form?`
|
|
74
|
-
});
|
|
75
|
-
return { success: false };
|
|
76
|
-
}
|
|
77
|
-
const stashed = await formService.getStashedSessions(entityId);
|
|
78
|
-
if (stashed.length === 0) {
|
|
79
|
-
await callback?.({
|
|
80
|
-
text: "You don't have any saved forms to resume."
|
|
81
|
-
});
|
|
82
|
-
return { success: false };
|
|
83
|
-
}
|
|
84
|
-
const sessionToRestore = stashed.sort(
|
|
85
|
-
(a, b) => b.updatedAt - a.updatedAt
|
|
86
|
-
)[0];
|
|
87
|
-
const session = await formService.restore(sessionToRestore.id, entityId);
|
|
88
|
-
const form = formService.getForm(session.formId);
|
|
89
|
-
const context = formService.getSessionContext(session);
|
|
90
|
-
let responseText = `I've restored your "${form?.name || session.formId}" form. `;
|
|
91
|
-
responseText += `You're ${context.progress}% complete. `;
|
|
92
|
-
if (context.filledFields.length > 0) {
|
|
93
|
-
responseText += `
|
|
94
|
-
|
|
95
|
-
Here's what I have so far:
|
|
96
|
-
`;
|
|
97
|
-
for (const field of context.filledFields.slice(0, RESTORE_FIELD_LIMIT)) {
|
|
98
|
-
responseText += `\u2022 ${field.label}: ${field.displayValue}
|
|
99
|
-
`;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
if (context.nextField) {
|
|
103
|
-
responseText += `
|
|
104
|
-
Let's continue with ${context.nextField.label}.`;
|
|
105
|
-
if (context.nextField.askPrompt) {
|
|
106
|
-
responseText += ` ${context.nextField.askPrompt}`;
|
|
107
|
-
}
|
|
108
|
-
} else if (context.status === "ready") {
|
|
109
|
-
responseText += `
|
|
110
|
-
Everything looks complete! Ready to submit?`;
|
|
111
|
-
}
|
|
112
|
-
await callback?.({
|
|
113
|
-
text: truncateRestoreResponse(responseText)
|
|
114
|
-
});
|
|
115
|
-
return {
|
|
116
|
-
success: true,
|
|
117
|
-
data: {
|
|
118
|
-
sessionId: session.id,
|
|
119
|
-
formId: session.formId,
|
|
120
|
-
progress: context.progress
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
} catch (error) {
|
|
124
|
-
logger.error("[FormRestoreAction] Handler error:", String(error));
|
|
125
|
-
await callback?.({
|
|
126
|
-
text: "Sorry, I couldn't restore your form. Please try again."
|
|
127
|
-
});
|
|
128
|
-
return { success: false };
|
|
129
|
-
}
|
|
130
|
-
},
|
|
131
|
-
// Example conversations for training/documentation
|
|
132
|
-
examples: [
|
|
133
|
-
[
|
|
134
|
-
{
|
|
135
|
-
name: "{{user1}}",
|
|
136
|
-
content: { text: "Resume my form" }
|
|
137
|
-
},
|
|
138
|
-
{
|
|
139
|
-
name: "{{agentName}}",
|
|
140
|
-
content: {
|
|
141
|
-
text: "I've restored your form. Let's continue where you left off."
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
],
|
|
145
|
-
[
|
|
146
|
-
{
|
|
147
|
-
name: "{{user1}}",
|
|
148
|
-
content: { text: "Continue with my registration" }
|
|
149
|
-
},
|
|
150
|
-
{
|
|
151
|
-
name: "{{agentName}}",
|
|
152
|
-
content: {
|
|
153
|
-
text: "I've restored your Registration form. You're 60% complete."
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
],
|
|
157
|
-
[
|
|
158
|
-
{
|
|
159
|
-
name: "{{user1}}",
|
|
160
|
-
content: { text: "Pick up where I left off" }
|
|
161
|
-
},
|
|
162
|
-
{
|
|
163
|
-
name: "{{agentName}}",
|
|
164
|
-
content: {
|
|
165
|
-
text: "I've restored your form. Here's what you have so far..."
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
]
|
|
169
|
-
]
|
|
170
|
-
};
|
|
171
|
-
var restore_default = formRestoreAction;
|
|
172
|
-
export {
|
|
173
|
-
restore_default as default,
|
|
174
|
-
formRestoreAction
|
|
175
|
-
};
|
|
176
|
-
//# sourceMappingURL=restore.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/actions/restore.ts"],"sourcesContent":["/**\n * @module actions/restore\n * @description Planner action for restoring stashed form sessions.\n *\n * Restore is a planner-driven Action (not part of the post-message form\n * evaluator) because the restored form context must reach the provider\n * BEFORE the agent generates its response. If the user has an active form\n * in the current room, the action asks them to continue or stash the\n * current one. Multiple stashed forms restore the most recent.\n */\n\nimport {\n type Action,\n type ActionResult,\n type HandlerCallback,\n type HandlerOptions,\n type IAgentRuntime,\n logger,\n type Memory,\n type State,\n type UUID,\n} from \"@elizaos/core\";\nimport type { FormService } from \"../service\";\n\nconst RESTORE_FIELD_LIMIT = 12;\nconst RESTORE_RESPONSE_MAX_CHARS = 4_000;\n\nfunction truncateRestoreResponse(text: string): string {\n return text.length <= RESTORE_RESPONSE_MAX_CHARS\n ? text\n : `${text.slice(0, RESTORE_RESPONSE_MAX_CHARS)}\\n\\n[truncated restored form summary]`;\n}\n\n/**\n * Form Restore Action\n *\n * Fast-path action for restoring stashed forms.\n * Preempts REPLY to provide immediate restoration with summary.\n *\n * WHY action:\n * - Needs to run BEFORE provider\n * - Must generate immediate response\n * - Context needed for next message\n */\nexport const formRestoreAction: Action = {\n name: \"FORM_RESTORE\",\n contexts: [\"tasks\", \"automation\", \"memory\"],\n contextGate: { anyOf: [\"tasks\", \"automation\", \"memory\"] },\n roleGate: { minRole: \"USER\" },\n similes: [\"RESUME_FORM\", \"CONTINUE_FORM\"],\n description: \"Restore a previously stashed form session\",\n descriptionCompressed: \"Restore stashed form session.\",\n parameters: [\n {\n name: \"sessionId\",\n description: \"Optional stashed form session id to restore.\",\n required: false,\n schema: { type: \"string\" },\n },\n ],\n\n /**\n * Validate: action is selectable whenever the user has stashed sessions\n * and no active form in the current room. The planner picks it via the\n * action description/similes when the user actually wants to resume.\n */\n validate: async (\n runtime: IAgentRuntime,\n message: Memory,\n _state?: State,\n ): Promise<boolean> => {\n const formService = runtime.getService(\"FORM\") as FormService;\n if (!formService) return false;\n\n const entityId = message.entityId as UUID;\n const roomId = message.roomId as UUID;\n if (!entityId || !roomId) return false;\n\n const stashed = await formService.getStashedSessions(entityId);\n if (stashed.length === 0) return false;\n\n const active = await formService.getActiveSession(entityId, roomId);\n return active === null;\n },\n\n /**\n * Handler: Restore the most recent stashed session.\n *\n * 1. Check for conflicts (active session in room)\n * 2. Restore the session\n * 3. Generate summary response\n *\n * @returns ActionResult with success status and session data\n */\n handler: async (\n runtime: IAgentRuntime,\n message: Memory,\n _state?: State,\n _options?: HandlerOptions,\n callback?: HandlerCallback,\n ): Promise<ActionResult> => {\n try {\n const formService = runtime.getService(\"FORM\") as FormService;\n if (!formService) {\n await callback?.({\n text: \"Sorry, I couldn't find the form service.\",\n });\n return { success: false };\n }\n\n const entityId = message.entityId as UUID;\n const roomId = message.roomId as UUID;\n\n if (!entityId || !roomId) {\n await callback?.({\n text: \"Sorry, I couldn't identify you.\",\n });\n return { success: false };\n }\n\n // Check for existing active session in this room\n // WHY check: Can't have two active sessions in same room\n const existing = await formService.getActiveSession(entityId, roomId);\n if (existing) {\n const form = formService.getForm(existing.formId);\n await callback?.({\n text: `You already have an active form: \"${form?.name || existing.formId}\". Would you like to continue with that one, or should I save it and restore your other form?`,\n });\n return { success: false };\n }\n\n // Get stashed sessions\n const stashed = await formService.getStashedSessions(entityId);\n\n if (stashed.length === 0) {\n await callback?.({\n text: \"You don't have any saved forms to resume.\",\n });\n return { success: false };\n }\n\n // Restore the most recent stashed session — the user likely wants what\n // they just stashed.\n const sessionToRestore = stashed.sort(\n (a, b) => b.updatedAt - a.updatedAt,\n )[0];\n const session = await formService.restore(sessionToRestore.id, entityId);\n\n const form = formService.getForm(session.formId);\n const context = formService.getSessionContext(session);\n\n // Generate response with restored context\n // WHY immediate response: User knows what happened\n let responseText = `I've restored your \"${form?.name || session.formId}\" form. `;\n responseText += `You're ${context.progress}% complete. `;\n\n if (context.filledFields.length > 0) {\n responseText += `\\n\\nHere's what I have so far:\\n`;\n for (const field of context.filledFields.slice(0, RESTORE_FIELD_LIMIT)) {\n responseText += `• ${field.label}: ${field.displayValue}\\n`;\n }\n }\n\n if (context.nextField) {\n responseText += `\\nLet's continue with ${context.nextField.label}.`;\n if (context.nextField.askPrompt) {\n responseText += ` ${context.nextField.askPrompt}`;\n }\n } else if (context.status === \"ready\") {\n responseText += `\\nEverything looks complete! Ready to submit?`;\n }\n\n await callback?.({\n text: truncateRestoreResponse(responseText),\n });\n\n return {\n success: true,\n data: {\n sessionId: session.id,\n formId: session.formId,\n progress: context.progress,\n },\n };\n } catch (error) {\n logger.error(\"[FormRestoreAction] Handler error:\", String(error));\n await callback?.({\n text: \"Sorry, I couldn't restore your form. Please try again.\",\n });\n return { success: false };\n }\n },\n\n // Example conversations for training/documentation\n examples: [\n [\n {\n name: \"{{user1}}\",\n content: { text: \"Resume my form\" },\n },\n {\n name: \"{{agentName}}\",\n content: {\n text: \"I've restored your form. Let's continue where you left off.\",\n },\n },\n ],\n [\n {\n name: \"{{user1}}\",\n content: { text: \"Continue with my registration\" },\n },\n {\n name: \"{{agentName}}\",\n content: {\n text: \"I've restored your Registration form. You're 60% complete.\",\n },\n },\n ],\n [\n {\n name: \"{{user1}}\",\n content: { text: \"Pick up where I left off\" },\n },\n {\n name: \"{{agentName}}\",\n content: {\n text: \"I've restored your form. Here's what you have so far...\",\n },\n },\n ],\n ],\n};\n\nexport default formRestoreAction;\n"],"mappings":"AAWA;AAAA,EAME;AAAA,OAIK;AAGP,MAAM,sBAAsB;AAC5B,MAAM,6BAA6B;AAEnC,SAAS,wBAAwB,MAAsB;AACrD,SAAO,KAAK,UAAU,6BAClB,OACA,GAAG,KAAK,MAAM,GAAG,0BAA0B,CAAC;AAAA;AAAA;AAClD;AAaO,MAAM,oBAA4B;AAAA,EACvC,MAAM;AAAA,EACN,UAAU,CAAC,SAAS,cAAc,QAAQ;AAAA,EAC1C,aAAa,EAAE,OAAO,CAAC,SAAS,cAAc,QAAQ,EAAE;AAAA,EACxD,UAAU,EAAE,SAAS,OAAO;AAAA,EAC5B,SAAS,CAAC,eAAe,eAAe;AAAA,EACxC,aAAa;AAAA,EACb,uBAAuB;AAAA,EACvB,YAAY;AAAA,IACV;AAAA,MACE,MAAM;AAAA,MACN,aAAa;AAAA,MACb,UAAU;AAAA,MACV,QAAQ,EAAE,MAAM,SAAS;AAAA,IAC3B;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,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