@familiar-ai/cloudflare 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -12
- package/dist/durable-workspace.d.ts +47 -0
- package/dist/{durable-familiar.js → durable-workspace.js} +29 -32
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +3 -3
- package/dist/durable-familiar.d.ts +0 -42
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @familiar-ai/cloudflare
|
|
2
2
|
|
|
3
|
-
Runs one Familiar in one SQLite-backed Cloudflare Durable Object. It owns storage, alarms and recovery after eviction. The [Cloudflare Worker example](../../examples/cloudflare-worker/README.md) hosts it with a scripted model. See [Host on Cloudflare](https://aidanzealley.github.io/familiar/build/cloudflare/) (publishes once GitHub Pages deployment is enabled).
|
|
3
|
+
Runs one Familiar workspace in one SQLite-backed Cloudflare Durable Object. It owns storage, alarms and recovery after eviction. The [Cloudflare Worker example](../../examples/cloudflare-worker/README.md) hosts it with a scripted model. See [Host on Cloudflare](https://aidanzealley.github.io/familiar/build/cloudflare/) (publishes once GitHub Pages deployment is enabled).
|
|
4
4
|
|
|
5
5
|
```sh
|
|
6
6
|
npm install @familiar-ai/core @familiar-ai/cloudflare
|
|
@@ -10,39 +10,49 @@ Use this package only in Workers. Inside your Durable Object, construct the adap
|
|
|
10
10
|
|
|
11
11
|
```ts
|
|
12
12
|
import { DurableObject } from 'cloudflare:workers'
|
|
13
|
-
import {
|
|
13
|
+
import { DurableWorkspace } from '@familiar-ai/cloudflare'
|
|
14
14
|
import { fauxAssistantMessage } from '@familiar-ai/core/models/faux'
|
|
15
15
|
import { scriptedModels } from '@familiar-ai/core/models/scripted'
|
|
16
16
|
|
|
17
17
|
export class Bot extends DurableObject {
|
|
18
|
-
readonly
|
|
19
|
-
|
|
18
|
+
readonly workspace = new DurableWorkspace(this.ctx, {
|
|
19
|
+
workspace: () => ({
|
|
20
20
|
models: scriptedModels(() =>
|
|
21
21
|
fauxAssistantMessage('Hello from a Worker.'),
|
|
22
22
|
),
|
|
23
|
-
|
|
23
|
+
chatModel: { provider: 'faux', modelId: 'faux-1' },
|
|
24
24
|
timeZone: 'Europe/London',
|
|
25
25
|
}),
|
|
26
|
+
// After each open: make its Familiar the first time.
|
|
27
|
+
setup: async (workspace) => {
|
|
28
|
+
if (!(await workspace.familiars.list()).length) {
|
|
29
|
+
await workspace.familiars.create({ name: 'Bot' })
|
|
30
|
+
}
|
|
31
|
+
},
|
|
26
32
|
})
|
|
27
33
|
|
|
28
34
|
override alarm() {
|
|
29
|
-
return this.
|
|
35
|
+
return this.workspace.alarm()
|
|
30
36
|
}
|
|
31
37
|
|
|
32
38
|
async greet() {
|
|
33
|
-
return this.
|
|
34
|
-
familiar.
|
|
39
|
+
return this.workspace.use(async (workspace) => {
|
|
40
|
+
const [familiar] = await workspace.familiars.list()
|
|
41
|
+
const dm = await workspace.rooms.get(
|
|
42
|
+
await workspace.rooms.dm(familiar.id, 'owner'),
|
|
43
|
+
)
|
|
44
|
+
return dm!.post({
|
|
35
45
|
author: { id: 'owner', name: 'Sam' },
|
|
36
46
|
text: 'Hello',
|
|
37
47
|
requestId: crypto.randomUUID(),
|
|
38
|
-
})
|
|
39
|
-
)
|
|
48
|
+
})
|
|
49
|
+
})
|
|
40
50
|
}
|
|
41
51
|
}
|
|
42
52
|
```
|
|
43
53
|
|
|
44
|
-
This scripted model answers every request alike. A lasting host configures its own provider, credentials, notifications, network policy and tools in the factory
|
|
54
|
+
This scripted model answers every request alike. A lasting host configures its own provider, credentials, notifications, network policy and tools in the factory, and its Familiars and rooms in `setup`. Configure a SQLite Durable Object binding and migration in Wrangler; see the [example's configuration](../../examples/cloudflare-worker/wrangler.jsonc).
|
|
45
55
|
|
|
46
|
-
Reach the
|
|
56
|
+
Reach the workspace through `use()` so recovery is armed before work starts, and don't call its `dismiss()`. `terminate()` permanently refuses that object ID while keeping history.
|
|
47
57
|
|
|
48
58
|
Local workerd tests cover reminder delivery, whether the object was evicted or stayed in memory, interrupted-reply recovery and persistent termination. ChatGPT sign-in and real-model operation on Workers have not been verified.
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { type Workspace, type WorkspaceOptions } from '@familiar-ai/core';
|
|
2
|
+
/**
|
|
3
|
+
* Everything a host chooses about its workspace: models, zone, plugins,
|
|
4
|
+
* notifications, network and secrets. The adapter owns storage and waking.
|
|
5
|
+
*/
|
|
6
|
+
export type HostWorkspaceOptions = Omit<WorkspaceOptions, 'storage' | 'now' | 'setTimer' | 'onWake'>;
|
|
7
|
+
export type DurableWorkspaceOptions = {
|
|
8
|
+
/** Called each time the object opens its workspace, after a restart or eviction too. */
|
|
9
|
+
workspace: () => HostWorkspaceOptions | Promise<HostWorkspaceOptions>;
|
|
10
|
+
/**
|
|
11
|
+
* Runs after each open, before any request or alarm reaches the
|
|
12
|
+
* workspace, such as to create its Familiars and rooms the first time.
|
|
13
|
+
*/
|
|
14
|
+
setup?: (workspace: Workspace) => Promise<void>;
|
|
15
|
+
/**
|
|
16
|
+
* How soon work a lost process left is resumed: the alarm while anything
|
|
17
|
+
* runs, and the fallback while opening. Default one minute.
|
|
18
|
+
*/
|
|
19
|
+
heartbeatMs?: number;
|
|
20
|
+
};
|
|
21
|
+
/** Refused because the workspace was terminated. Its object ID never opens again. */
|
|
22
|
+
export declare class WorkspaceTerminated extends Error {
|
|
23
|
+
constructor();
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* One workspace in one SQLite-backed Durable Object. Construct it in the
|
|
27
|
+
* object's constructor or a field, route `alarm()` to it, and reach the
|
|
28
|
+
* workspace only through `use()`, which arms recovery before work can start.
|
|
29
|
+
*/
|
|
30
|
+
export declare class DurableWorkspace {
|
|
31
|
+
#private;
|
|
32
|
+
constructor(ctx: DurableObjectState, options: DurableWorkspaceOptions);
|
|
33
|
+
/**
|
|
34
|
+
* Runs `work` with the workspace, after arming a recovery alarm, because
|
|
35
|
+
* any call can start work. Rejects with WorkspaceTerminated once
|
|
36
|
+
* terminated. Leave closing to `terminate()`: don't call `dismiss()`.
|
|
37
|
+
*/
|
|
38
|
+
use<T>(work: (workspace: Workspace) => T | Promise<T>): Promise<T>;
|
|
39
|
+
/** The object's `alarm()`: runs whatever is due or was cut off. */
|
|
40
|
+
alarm(): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Stops the workspace for good, keeping its history: records that it's
|
|
43
|
+
* terminated and deletes the alarm in one transaction, then closes it,
|
|
44
|
+
* cutting off any reply. Later requests, alarms and restarts are refused.
|
|
45
|
+
*/
|
|
46
|
+
terminate(): Promise<void>;
|
|
47
|
+
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { DurableObjectSqliteDatabase, openDurableObjectSqliteStorage, } from '@earendil-works/pi-durable/storage/sqlite/cloudflare';
|
|
2
|
-
import {
|
|
2
|
+
import { openWorkspace, } from '@familiar-ai/core';
|
|
3
3
|
import { alarmTime, alarmWriter } from "./alarm.js";
|
|
4
|
-
/** Refused because the
|
|
5
|
-
export class
|
|
4
|
+
/** Refused because the workspace was terminated. Its object ID never opens again. */
|
|
5
|
+
export class WorkspaceTerminated extends Error {
|
|
6
6
|
constructor() {
|
|
7
|
-
super('This
|
|
8
|
-
this.name = '
|
|
7
|
+
super('This workspace was terminated.');
|
|
8
|
+
this.name = 'WorkspaceTerminated';
|
|
9
9
|
}
|
|
10
10
|
}
|
|
11
11
|
const HEARTBEAT_MS = 60_000;
|
|
@@ -14,11 +14,11 @@ const PI_PREFIX = 'pi_';
|
|
|
14
14
|
/** The object's key-value record that it was terminated, and when. */
|
|
15
15
|
const TERMINATED = 'familiar.terminated';
|
|
16
16
|
/**
|
|
17
|
-
* One
|
|
17
|
+
* One workspace in one SQLite-backed Durable Object. Construct it in the
|
|
18
18
|
* object's constructor or a field, route `alarm()` to it, and reach the
|
|
19
|
-
*
|
|
19
|
+
* workspace only through `use()`, which arms recovery before work can start.
|
|
20
20
|
*/
|
|
21
|
-
export class
|
|
21
|
+
export class DurableWorkspace {
|
|
22
22
|
#ctx;
|
|
23
23
|
#heartbeatMs;
|
|
24
24
|
/**
|
|
@@ -38,17 +38,17 @@ export class DurableFamiliar {
|
|
|
38
38
|
void ctx.blockConcurrencyWhile(() => this.#open(options));
|
|
39
39
|
}
|
|
40
40
|
/**
|
|
41
|
-
* Runs `work` with the
|
|
42
|
-
* any call can start work. Rejects with
|
|
43
|
-
* Leave closing to `terminate()`: don't call `dismiss()`.
|
|
41
|
+
* Runs `work` with the workspace, after arming a recovery alarm, because
|
|
42
|
+
* any call can start work. Rejects with WorkspaceTerminated once
|
|
43
|
+
* terminated. Leave closing to `terminate()`: don't call `dismiss()`.
|
|
44
44
|
*/
|
|
45
45
|
async use(work) {
|
|
46
46
|
const runtime = this.#runtime;
|
|
47
47
|
if (runtime.status !== 'open') {
|
|
48
|
-
throw new
|
|
48
|
+
throw new WorkspaceTerminated();
|
|
49
49
|
}
|
|
50
|
-
await this.#arm({ ...runtime.
|
|
51
|
-
return work(runtime.
|
|
50
|
+
await this.#arm({ ...runtime.workspace.wake(), active: true });
|
|
51
|
+
return work(runtime.workspace);
|
|
52
52
|
}
|
|
53
53
|
/** The object's `alarm()`: runs whatever is due or was cut off. */
|
|
54
54
|
async alarm() {
|
|
@@ -56,16 +56,16 @@ export class DurableFamiliar {
|
|
|
56
56
|
if (runtime.status !== 'open') {
|
|
57
57
|
return this.#writeAlarm(null);
|
|
58
58
|
}
|
|
59
|
-
await this.#arm({ ...runtime.
|
|
60
|
-
runtime.
|
|
59
|
+
await this.#arm({ ...runtime.workspace.wake(), active: true });
|
|
60
|
+
runtime.workspace.resume();
|
|
61
61
|
// Pi tells onWake only of changes, and the heartbeat above wasn't one.
|
|
62
62
|
// Due work already counts as active here.
|
|
63
|
-
await this.#arm(runtime.
|
|
63
|
+
await this.#arm(runtime.workspace.wake());
|
|
64
64
|
}
|
|
65
65
|
/**
|
|
66
|
-
* Stops the
|
|
67
|
-
* terminated and deletes the alarm in one transaction, then
|
|
68
|
-
*
|
|
66
|
+
* Stops the workspace for good, keeping its history: records that it's
|
|
67
|
+
* terminated and deletes the alarm in one transaction, then closes it,
|
|
68
|
+
* cutting off any reply. Later requests, alarms and restarts are refused.
|
|
69
69
|
*/
|
|
70
70
|
async terminate() {
|
|
71
71
|
const runtime = this.#runtime;
|
|
@@ -84,12 +84,7 @@ export class DurableFamiliar {
|
|
|
84
84
|
throw error;
|
|
85
85
|
}
|
|
86
86
|
if (runtime.status === 'open') {
|
|
87
|
-
|
|
88
|
-
await runtime.familiar.abort();
|
|
89
|
-
}
|
|
90
|
-
finally {
|
|
91
|
-
await runtime.familiar.dismiss();
|
|
92
|
-
}
|
|
87
|
+
await runtime.workspace.dismiss();
|
|
93
88
|
}
|
|
94
89
|
}
|
|
95
90
|
async #open(options) {
|
|
@@ -104,21 +99,23 @@ export class DurableFamiliar {
|
|
|
104
99
|
if (current === null || current > fallback) {
|
|
105
100
|
await this.#writeAlarm(fallback);
|
|
106
101
|
}
|
|
107
|
-
const
|
|
108
|
-
...(await options.
|
|
102
|
+
const workspace = await openWorkspace({
|
|
103
|
+
...(await options.workspace()),
|
|
109
104
|
storage: await openDurableObjectSqliteStorage(this.#ctx.storage, {
|
|
110
105
|
prefix: PI_PREFIX,
|
|
111
106
|
}),
|
|
112
107
|
onWake: (wake) => void this.#arm(wake).catch(() => { }),
|
|
113
108
|
});
|
|
114
|
-
void
|
|
109
|
+
void workspace.closed.then((end) => {
|
|
115
110
|
if (end.reason === 'failed' && this.#runtime.status !== 'terminated') {
|
|
116
111
|
// Keep the recovery alarm: the next instance reopens persisted work.
|
|
117
|
-
this.#ctx.abort('
|
|
112
|
+
this.#ctx.abort('Workspace storage failed');
|
|
118
113
|
}
|
|
119
114
|
});
|
|
120
|
-
|
|
121
|
-
await
|
|
115
|
+
// Still under the fallback alarm: if setup throws, the object resets.
|
|
116
|
+
await options.setup?.(workspace);
|
|
117
|
+
this.#runtime = { status: 'open', workspace };
|
|
118
|
+
await this.#arm(workspace.wake());
|
|
122
119
|
}
|
|
123
120
|
/** Writes the alarm `wake` asks for, as the runtime allows. */
|
|
124
121
|
#arm(wake) {
|
package/dist/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { DurableWorkspace, WorkspaceTerminated, type DurableWorkspaceOptions, type HostWorkspaceOptions, } from './durable-workspace.ts';
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { DurableWorkspace, WorkspaceTerminated, } from "./durable-workspace.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@familiar-ai/cloudflare",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -18,13 +18,13 @@
|
|
|
18
18
|
"access": "public"
|
|
19
19
|
},
|
|
20
20
|
"peerDependencies": {
|
|
21
|
-
"@familiar-ai/core": "0.
|
|
21
|
+
"@familiar-ai/core": "0.3.0"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@cloudflare/workers-types": "5.20261023.1",
|
|
25
25
|
"vitest": "^5.0.5",
|
|
26
26
|
"wrangler": "4.156.0",
|
|
27
|
-
"@familiar-ai/core": "0.
|
|
27
|
+
"@familiar-ai/core": "0.3.0"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"@earendil-works/pi-durable": "1.4.0"
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { type Familiar, type FamiliarOptions } from '@familiar-ai/core';
|
|
2
|
-
/**
|
|
3
|
-
* Everything a host chooses about its Familiar: models, zone, plugins,
|
|
4
|
-
* notifications, network and secrets. The adapter owns storage and waking.
|
|
5
|
-
*/
|
|
6
|
-
export type HostFamiliarOptions = Omit<FamiliarOptions, 'storage' | 'now' | 'setTimer' | 'onWake'>;
|
|
7
|
-
export type DurableFamiliarOptions = {
|
|
8
|
-
/** Called each time the object opens its Familiar, after a restart or eviction too. */
|
|
9
|
-
familiar: () => HostFamiliarOptions | Promise<HostFamiliarOptions>;
|
|
10
|
-
/**
|
|
11
|
-
* How soon work a lost process left is resumed: the alarm while anything
|
|
12
|
-
* runs, and the fallback while opening. Default one minute.
|
|
13
|
-
*/
|
|
14
|
-
heartbeatMs?: number;
|
|
15
|
-
};
|
|
16
|
-
/** Refused because the Familiar was terminated. Its ID never opens again. */
|
|
17
|
-
export declare class FamiliarTerminated extends Error {
|
|
18
|
-
constructor();
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* One Familiar in one SQLite-backed Durable Object. Construct it in the
|
|
22
|
-
* object's constructor or a field, route `alarm()` to it, and reach the
|
|
23
|
-
* Familiar only through `use()`, which arms recovery before work can start.
|
|
24
|
-
*/
|
|
25
|
-
export declare class DurableFamiliar {
|
|
26
|
-
#private;
|
|
27
|
-
constructor(ctx: DurableObjectState, options: DurableFamiliarOptions);
|
|
28
|
-
/**
|
|
29
|
-
* Runs `work` with the Familiar, after arming a recovery alarm, because
|
|
30
|
-
* any call can start work. Rejects with FamiliarTerminated once terminated.
|
|
31
|
-
* Leave closing to `terminate()`: don't call `dismiss()`.
|
|
32
|
-
*/
|
|
33
|
-
use<T>(work: (familiar: Familiar) => T | Promise<T>): Promise<T>;
|
|
34
|
-
/** The object's `alarm()`: runs whatever is due or was cut off. */
|
|
35
|
-
alarm(): Promise<void>;
|
|
36
|
-
/**
|
|
37
|
-
* Stops the Familiar for good, keeping its history: records that it's
|
|
38
|
-
* terminated and deletes the alarm in one transaction, then stops the
|
|
39
|
-
* current run and closes. Later requests, alarms and restarts are refused.
|
|
40
|
-
*/
|
|
41
|
-
terminate(): Promise<void>;
|
|
42
|
-
}
|