@rebasepro/plugin-ai 0.12.1-canary.gf5f1d39 → 0.13.1-canary.g06dbe5b

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.
Files changed (36) hide show
  1. package/README.md +66 -18
  2. package/dist/api.d.ts +77 -30
  3. package/dist/components/AutofillReviewDialog.d.ts +16 -0
  4. package/dist/components/DataEnhancementControllerProvider.d.ts +10 -4
  5. package/dist/components/FormEnhanceAction.d.ts +1 -1
  6. package/dist/editor/useEditorAIController.d.ts +11 -2
  7. package/dist/index.es.js +695 -388
  8. package/dist/index.es.js.map +1 -1
  9. package/dist/types/data_enhancement_controller.d.ts +84 -28
  10. package/dist/useDataEnhancementPlugin.d.ts +10 -5
  11. package/dist/utils/values.d.ts +33 -1
  12. package/package.json +24 -19
  13. package/src/api.ts +296 -174
  14. package/src/components/AutofillReviewDialog.tsx +209 -0
  15. package/src/components/DataEnhancementControllerProvider.tsx +238 -261
  16. package/src/components/FormEnhanceAction.tsx +158 -129
  17. package/src/editor/useEditorAIController.tsx +20 -33
  18. package/src/tests/AutofillReviewDialog.test.tsx +340 -0
  19. package/src/tests/api.test.ts +382 -0
  20. package/src/tests/properties.test.ts +420 -0
  21. package/src/tests/request_agreement.test.ts +240 -0
  22. package/src/tests/review.test.tsx +596 -0
  23. package/src/tests/useDataEnhancementPlugin.test.tsx +36 -15
  24. package/src/tests/useEditorAIController.test.ts +98 -0
  25. package/src/types/data_enhancement_controller.tsx +98 -31
  26. package/src/useDataEnhancementPlugin.tsx +13 -12
  27. package/src/utils/values.ts +63 -3
  28. package/dist/utils/diffStrings.d.ts +0 -7
  29. package/dist/utils/strings_counter.d.ts +0 -2
  30. package/dist/utils/suggestions.d.ts +0 -1
  31. package/src/tests/diffStrings.test.ts +0 -128
  32. package/src/tests/strings_counter.test.ts +0 -117
  33. package/src/tests/suggestions.test.ts +0 -53
  34. package/src/utils/diffStrings.ts +0 -70
  35. package/src/utils/strings_counter.ts +0 -22
  36. package/src/utils/suggestions.ts +0 -6
package/README.md CHANGED
@@ -8,17 +8,56 @@ AI-powered data autofill and text autocomplete plugin for Rebase.
8
8
  pnpm add @rebasepro/plugin-ai
9
9
  ```
10
10
 
11
- **Peer dependencies:** `react >= 19.0.0`, `react-dom >= 19.0.0`, `react-router >= 6.28.0`, `react-router-dom >= 6.28.0`
11
+ **Peer dependencies:** `react >= 19.2.7`, `react-dom >= 19.2.7`, `react-router ^8`
12
12
 
13
13
  ## What This Package Does
14
14
 
15
15
  This plugin adds AI-powered capabilities to the Rebase admin panel:
16
16
 
17
- - **Form autofill** — An "Enhance" action button injected into snapshot forms that uses AI to suggest and fill field values based on collection schema and existing data.
18
- - **Editor autocomplete** — A streaming text autocomplete controller for rich text editors, powered by an AI backend.
17
+ - **Form autofill** — An "Autofill" action in the entity form footer that proposes values from the collection schema, whatever is already in the record, and an optional instruction.
18
+ - **Editor autocomplete** — A streaming inline continuation for the rich text editor.
19
19
 
20
20
  It registers as a standard `RebasePlugin`, injecting UI slots and providers automatically.
21
21
 
22
+ ### Autofill proposes; it does not edit
23
+
24
+ Clicking **Autofill** opens a review. Fields stream into that list as the model
25
+ writes them, so a long run shows progress — but the record is not touched. Each row
26
+ shows the proposed value, and the current value struck through when the proposal
27
+ would replace one. Untick anything you don't want, then **Apply** writes the rest in
28
+ a single step. **Discard** leaves the record exactly as it was.
29
+
30
+ This is a deliberate change from the FireCMS-era behaviour, where generated text was
31
+ streamed directly into the live form fields. That approach mutated the record before
32
+ anyone had agreed to it, showed half-written sentences that read as bugs, and relied
33
+ on heuristics to guess whether each token should append to or replace what you had
34
+ already typed — with no way back but retyping.
35
+
36
+ ## How it reaches a model
37
+
38
+ The plugin talks to a small hosted service that Rebase runs and pays for (Gemini 3.6 Flash, behind Rebase’s own credits). There is
39
+ **nothing to configure and no API key to obtain** — install the plugin, mount it, done.
40
+
41
+ Two consequences worth knowing:
42
+
43
+ - **Requests are anonymous.** No auth token of any kind leaves your app. The service
44
+ cannot verify a self-hosted backend's JWT (you sign it with your own secret), so
45
+ asking for one would only hand a live credential to a third party that has no use
46
+ for it. Cost is bounded by rate limits and a daily ceiling instead.
47
+ - **Your collection schema and the record's current values are sent** with each
48
+ autofill request, because the service has no other way to know the shape of what
49
+ it is filling. Values of properties marked `admin: { readOnly: true }` or
50
+ `admin: { disabled: true }` are the exception — they are neither filled nor sent.
51
+ If the rest is not acceptable for your data, set `endpoint` and run the
52
+ service yourself — the reference implementation is `saas/backend/functions/ai.ts`
53
+ in the Rebase repository, and the wire format is documented in `src/api.ts`.
54
+
55
+ The plugin renders nothing until the service's `GET /status` reports itself
56
+ available, so an unreachable host or an exhausted daily quota means no Autofill
57
+ button — never a button that fails when clicked. That check is one request per
58
+ session, not one per record opened; return `false` from `getConfigForPath` to
59
+ keep a collection from being asked about at all.
60
+
22
61
  ## Key Exports
23
62
 
24
63
  | Export | Type | Description |
@@ -31,38 +70,39 @@ It registers as a standard `RebasePlugin`, injecting UI slots and providers auto
31
70
 
32
71
  | Prop | Type | Default | Description |
33
72
  |---|---|---|---|
34
- | `apiKey` | `string` | Built-in default key | API key for the data enhancement service |
35
- | `getConfigForPath` | `(props: { path, collection, user }) => boolean` | — | Return `false` to disable enhancement for specific paths |
36
- | `host` | `string` | — | Custom API host (development only) |
73
+ | `getConfigForPath` | `(props: { path, collection, user }) => boolean` | — | Return `false` to disable autofill for specific paths |
74
+ | `endpoint` | `string` | Rebase's hosted service | Base URL of the AI service. Point it at your own deployment to keep generation inside your infrastructure |
37
75
 
38
76
  ## Quick Start
39
77
 
40
78
  ```tsx
41
79
  import { useDataEnhancementPlugin } from "@rebasepro/plugin-ai";
42
80
 
43
- // In your app setup:
44
81
  const dataEnhancementPlugin = useDataEnhancementPlugin({
45
- getConfigForPath: ({ path, collection }) => {
46
- // Disable for certain collections
47
- return collection.name !== "system_logs";
48
- }
82
+ getConfigForPath: ({ collection }) => collection.name !== "system_logs"
49
83
  });
50
84
 
51
85
  // Pass to your Rebase app:
52
- <RebaseFirebaseApp
53
- plugins={[dataEnhancementPlugin]}
54
- // ...other props
55
- />
86
+ // <Rebase plugins={[dataEnhancementPlugin]} ... />
87
+ ```
88
+
89
+ ### Self-hosting the service
90
+
91
+ ```tsx
92
+ const dataEnhancementPlugin = useDataEnhancementPlugin({
93
+ endpoint: "https://ai.internal.example.com"
94
+ });
56
95
  ```
57
96
 
97
+ Your endpoint needs to answer `GET /status`, `POST /autofill` (SSE), `POST /autocomplete`
98
+ (SSE) and `POST /prompts`.
99
+
58
100
  ### Editor AI Autocomplete
59
101
 
60
102
  ```tsx
61
103
  import { useEditorAIController } from "@rebasepro/plugin-ai";
62
104
 
63
- const aiController = useEditorAIController({
64
- getAuthToken: () => firebaseUser.getIdToken()
65
- });
105
+ const aiController = useEditorAIController();
66
106
 
67
107
  // Use in a rich text editor:
68
108
  await aiController.autocomplete(
@@ -74,6 +114,14 @@ await aiController.autocomplete(
74
114
  );
75
115
  ```
76
116
 
117
+ ## Migrating from the FireCMS-era plugin
118
+
119
+ The `apiKey` and `host` props are gone. `apiKey` shipped a hardcoded key in the
120
+ published package and pointed at a service that no longer exists; `host` is now
121
+ `endpoint` and is a supported production option rather than a development-only
122
+ escape hatch. `useEditorAIController` no longer takes `getAuthToken` — it needs no
123
+ token.
124
+
77
125
  ## Related Packages
78
126
 
79
127
  - `@rebasepro/admin` — The admin panel this plugin extends
package/dist/api.d.ts CHANGED
@@ -1,34 +1,81 @@
1
- import { EnhancedDataResult, InputProperty, SamplePromptsResult } from "./types/data_enhancement_controller";
2
- import { EntityValues } from "@rebasepro/types";
3
- export declare function enhanceDataAPIStream<M extends Record<string, unknown>>(props: {
4
- apiKey: string;
5
- entityId?: string | number;
1
+ import { AutofillRequest, AutofillResult, AiStatus, SamplePromptsResult } from "./types/data_enhancement_controller";
2
+ /**
3
+ * The hosted service Rebase runs for this plugin.
4
+ *
5
+ * The previous value here was `https://api.rebase.pro`, a FireCMS-era host that
6
+ * resolves but serves nothing — every path 404s — so Autofill had never worked
7
+ * in a Rebase install. This one is served by the control plane
8
+ * (`saas/backend/functions/ai.ts`). Point `endpoint` somewhere else to run your
9
+ * own; the wire format below is the whole contract.
10
+ */
11
+ export declare const DEFAULT_AI_ENDPOINT = "https://app.rebase.pro/api/functions/ai";
12
+ /**
13
+ * Ask the service whether it can serve a request at all.
14
+ *
15
+ * The plugin gates every affordance on this. A missing provider key, an
16
+ * exhausted daily quota or an unreachable host all resolve to `available:
17
+ * false`, and the Autofill button is simply not rendered — rather than
18
+ * rendered, clicked, and failed.
19
+ */
20
+ export declare function fetchAiStatus(props: {
21
+ endpoint?: string;
22
+ signal?: AbortSignal;
23
+ }): Promise<AiStatus>;
24
+ /**
25
+ * {@link fetchAiStatus}, asked once per endpoint per session.
26
+ *
27
+ * The provider is form-scoped, so the uncached call meant one request to the
28
+ * host every time any record was opened — a beacon on an install that may never
29
+ * click Autofill, and enough traffic from one NAT'd office to spend the host's
30
+ * per-IP rate limit on nothing, which reads back as `available: false` and makes
31
+ * the button flicker in and out for everyone behind it.
32
+ *
33
+ * Availability changes on the order of a deploy or a daily quota reset, not of a
34
+ * form open, so a session-long answer is the right resolution. Failures resolve
35
+ * to `available: false` and are cached like any other answer — retrying per form
36
+ * open is the behaviour this replaces.
37
+ */
38
+ export declare function fetchAiStatusCached(props: {
39
+ endpoint?: string;
40
+ }): Promise<AiStatus>;
41
+ /** Forget every cached probe, so the next caller asks again. */
42
+ export declare function clearAiStatusCache(): void;
43
+ /**
44
+ * Fill a record, streaming each field as the service writes it.
45
+ *
46
+ * `onDelta` fires with more text for a field still being written; `onValue`
47
+ * fires once a field is complete and carries its final, correctly typed value.
48
+ * A caller that implements only `onValue` still ends up with the right record —
49
+ * the deltas exist so a long text field fills in visibly instead of appearing
50
+ * all at once.
51
+ */
52
+ export declare function autofillStream(props: {
53
+ request: AutofillRequest;
54
+ endpoint?: string;
55
+ signal?: AbortSignal;
56
+ onDelta: (key: string, text: string) => void;
57
+ onValue: (key: string, value: unknown) => void;
58
+ }): Promise<AutofillResult>;
59
+ /** Inline continuation for the rich-text editor. Streams plain text. */
60
+ export declare function autocompleteStream(props: {
61
+ textBefore?: string;
62
+ textAfter?: string;
63
+ endpoint?: string;
64
+ signal?: AbortSignal;
65
+ onDelta: (text: string) => void;
66
+ }): Promise<string>;
67
+ /**
68
+ * Sample prompts for the Autofill menu.
69
+ *
70
+ * Failure is deliberately not thrown: the menu has built-in prompts to fall
71
+ * back on, and an empty suggestion list is a far better outcome than an error
72
+ * toast for something nobody asked for.
73
+ */
74
+ export declare function fetchPromptSuggestions(props: {
6
75
  entityName: string;
76
+ /** Ties suggestions to the domain — see the note on the service's side. */
7
77
  entityDescription?: string;
8
- propertyKey?: string;
9
- propertyInstructions?: string;
10
- values: EntityValues<M>;
11
- path: string;
12
- properties: Record<string, InputProperty>;
13
- instructions?: string;
14
- firebaseToken: string;
15
- onUpdate: (suggestions: Record<string, string | number>) => void;
16
- onUpdateDelta: (propertyKey: string, partialValue: string) => void;
17
- onError: (error: Error) => void;
18
- onEnd: (result: EnhancedDataResult) => void;
19
- host?: string;
20
- }): Promise<void>;
21
- export declare function fetchEntityPromptSuggestion<M extends object>(props: {
22
78
  input?: string;
23
- entityName: string;
24
- firebaseToken: string;
25
- apiKey: string;
26
- host?: string;
79
+ endpoint?: string;
80
+ signal?: AbortSignal;
27
81
  }): Promise<SamplePromptsResult>;
28
- export declare function autocompleteStream(props: {
29
- firebaseToken: string;
30
- textBefore?: string;
31
- textAfter: string;
32
- host?: string;
33
- onUpdate: (delta: string) => void;
34
- }): Promise<string>;
@@ -0,0 +1,16 @@
1
+ import React from "react";
2
+ /**
3
+ * The review step.
4
+ *
5
+ * Autofill used to write generated text into the live form as it streamed —
6
+ * fields mutating under the cursor, half-written sentences that looked like
7
+ * bugs, and a pile of heuristics deciding whether each token should append to
8
+ * or replace what the operator had already typed. Getting the old value back
9
+ * meant retyping it.
10
+ *
11
+ * So the generated values land here instead. Streaming still happens, and is
12
+ * still worth having — rows appear and fill in as the model works, so a long
13
+ * run shows progress — but it happens in a surface that owns nothing. The
14
+ * record changes on **Apply**, once, for the rows still ticked.
15
+ */
16
+ export declare function AutofillReviewDialog(): React.JSX.Element | null;
@@ -1,15 +1,21 @@
1
1
  import React, { PropsWithChildren } from "react";
2
2
  import { DataEnhancementController } from "../types/data_enhancement_controller";
3
- import { CollectionConfig } from "@rebasepro/types";
3
+ import { CollectionConfig, User } from "@rebasepro/types";
4
4
  import { PluginFormActionProps } from "@rebasepro/admin-types";
5
5
  type DataEnhancementControllerProviderProps = {
6
- apiKey: string;
6
+ /**
7
+ * Kept in step with `DataEnhancementPluginProps.getConfigForPath`, which is
8
+ * the signature the host app actually writes against: the plugin hands this
9
+ * component through as `ComponentType<any>`, so nothing but agreement here
10
+ * makes the two match.
11
+ */
7
12
  getConfigForPath?: (props: {
8
13
  path: string;
9
14
  collection: CollectionConfig;
15
+ user: User | null;
10
16
  }) => boolean;
11
- host?: string;
17
+ endpoint?: string;
12
18
  };
13
19
  export declare const useDataEnhancementController: () => DataEnhancementController;
14
- export declare function DataEnhancementControllerProvider({ apiKey, getConfigForPath, children, host, path, collection, formContext }: PropsWithChildren<DataEnhancementControllerProviderProps & PluginFormActionProps>): React.JSX.Element;
20
+ export declare function DataEnhancementControllerProvider({ getConfigForPath, children, endpoint, path, collection, formContext }: PropsWithChildren<DataEnhancementControllerProviderProps & PluginFormActionProps>): React.JSX.Element;
15
21
  export {};
@@ -1,3 +1,3 @@
1
1
  import React from "react";
2
2
  import { PluginFormActionProps } from "@rebasepro/admin-types";
3
- export declare function FormEnhanceAction({ entityId, path, status, collection, formContext, openEntityMode }: PluginFormActionProps): React.JSX.Element | null;
3
+ export declare function FormEnhanceAction({ path, status, collection, formContext }: PluginFormActionProps): React.JSX.Element | null;
@@ -1,4 +1,13 @@
1
1
  import { EditorAIController } from "@rebasepro/admin";
2
- export declare function useEditorAIController({ getAuthToken }: {
3
- getAuthToken?: () => Promise<string>;
2
+ /**
3
+ * Inline continuation for the rich-text editor's slash command.
4
+ *
5
+ * No token is threaded through any more. The previous version demanded a
6
+ * Firebase ID token and threw `"Firebase token is required"` when it could not
7
+ * get one — in a Rebase app there is no such thing, and the token it actually
8
+ * sent was a Rebase JWT the receiving service had no way to verify. The hosted
9
+ * service authenticates nobody; see `src/api.ts`.
10
+ */
11
+ export declare function useEditorAIController({ endpoint }?: {
12
+ endpoint?: string;
4
13
  }): EditorAIController;