@collective-intelligence/effect-xstate 0.0.0-placeholder.0 → 0.7.1

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 CHANGED
@@ -1 +1,35 @@
1
- Placeholder for @collective-intelligence/effect-xstate. Real versions are published by GitHub Actions.
1
+ # `@collective-intelligence/effect-xstate`
2
+
3
+ The Effect and XState bridge of Collective Intelligence. Install the required peers `effect` (`^4.1.0`) and `xstate` (`^5.33.3`), and the optional peer `react` (`>=18.2 <20`) for `./react`. The package has no dependencies, so the application holds one copy of each peer. The React layers of `@collective-intelligence/ai` run on it; install it of the same release as that package.
4
+
5
+ ## `./actors`
6
+
7
+ - `fromEffect(run)`: XState actor logic that runs `run(input)` on `input.runtime`, an `EffectRunner<R>` such as a `ManagedRuntime` that provides `R`. Its output is the effect's value; a typed failure or a defect fails the actor, so the parent machine gets it as the error event of the invoke. A stop of the actor, as when its parent stops, interrupts the running effect, and no result of a stopped run reaches the actor.
8
+ - `effectTask(run)`: the machine of one single action of a view. `RUN` runs `run(input)` (`idle`, `running`, then `done` with `output` or `failed` with `error`), `CANCEL` interrupts the run, `RESET` puts a settled task back to `idle`. It emits `done` or `failed` with the same result. The error of an interrupted run is a `Cause.Interrupt` reason.
9
+
10
+ ```ts
11
+ import { effectTask } from "@collective-intelligence/effect-xstate/actors"
12
+ import { createActor } from "xstate"
13
+
14
+ const share = effectTask((page: string) => sharePage(page))
15
+ const task = createActor(share, { input: { runtime } })
16
+ task.start()
17
+ task.send({ input: "/accueil", type: "RUN" })
18
+ ```
19
+
20
+ ## `./react`
21
+
22
+ `makeEffectRuntimeContext<R>()` makes the context of the runtimes that provide `R`. `EffectRuntimeProvider` gives its tree a runtime through that context, and `useEffectRuntime(context)` reads it, so a view passes it to the machines it starts. The owner of the runtime makes it and disposes it.
23
+
24
+ ```tsx
25
+ const AppRuntime = makeEffectRuntimeContext<Shares>()
26
+
27
+ <EffectRuntimeProvider context={AppRuntime} runtime={runtime}>
28
+ <ShareButton />
29
+ </EffectRuntimeProvider>
30
+
31
+ const ShareButton = () => {
32
+ const task = useActorRef(share, { input: { runtime: useEffectRuntime(AppRuntime) } })
33
+ // ...
34
+ }
35
+ ```
@@ -0,0 +1,73 @@
1
+ import type { ActorRefFromLogic, MetaObject, PromiseActorLogic, StateMachine } from "xstate";
2
+ import * as Effect from "effect/Effect";
3
+ import type { EffectRunner } from "./from-effect.js";
4
+ /** The input of an `effectTask` machine: the runtime its effect runs on. */
5
+ export type EffectTaskInput<R> = Readonly<{
6
+ runtime: EffectRunner<R>;
7
+ }>;
8
+ /**
9
+ * The commands of an `effectTask` machine: `RUN` runs the effect with
10
+ * `input`, `CANCEL` interrupts the run, `RESET` puts a settled task back to
11
+ * `idle`.
12
+ */
13
+ export type EffectTaskEvent<Input> = Readonly<{
14
+ input: Input;
15
+ type: "RUN";
16
+ }> | Readonly<{
17
+ type: "CANCEL";
18
+ }> | Readonly<{
19
+ type: "RESET";
20
+ }>;
21
+ /** What an `effectTask` machine emits when its run ends. */
22
+ export type EffectTaskEmitted<A> = Readonly<{
23
+ output: A;
24
+ type: "done";
25
+ }> | Readonly<{
26
+ error: unknown;
27
+ type: "failed";
28
+ }>;
29
+ /** The context of an `effectTask` machine: its runtime and its last result. */
30
+ export type EffectTaskContext<A, R> = Readonly<{
31
+ /** The typed error or the defect of the last run, while the task is `failed`. */
32
+ error: unknown;
33
+ /** The value of the last run, while the task is `done`. */
34
+ output: A | undefined;
35
+ runtime: EffectRunner<R>;
36
+ }>;
37
+ /**
38
+ * The machine of one single action of a view, such as a download, a share or
39
+ * a sign-out: `RUN` runs `run(input)` in a `fromEffect` actor on the runtime
40
+ * of the machine input. The machine is `idle`, then `running` until the run
41
+ * settles, then `done` (with `output`) or `failed` (with `error`) until the
42
+ * next `RUN` or a `RESET`, which puts it back to `idle` without the result;
43
+ * it emits `done` or `failed` with the same result. A `RUN` or a `RESET`
44
+ * while it runs is ignored. `CANCEL` interrupts the run, and the task is
45
+ * `failed`. The error of an interrupted run, from `CANCEL` or from its effect,
46
+ * is a `Cause.Interrupt` reason (`Cause.isReason(error) &&
47
+ * Cause.isInterruptReason(error)`). A stop of the machine, as when its view
48
+ * unmounts, interrupts the run.
49
+ *
50
+ * A flow with steps of its own, such as a form that saves and then confirms,
51
+ * is its own machine with `fromEffect` actors, not a task.
52
+ */
53
+ export declare const effectTask: <Input, A, E, R = never>(run: (input: Input) => Effect.Effect<A, E, R>) => StateMachine<EffectTaskContext<A, R>, EffectTaskEvent<Input>, Record<string, ActorRefFromLogic<PromiseActorLogic<A, Readonly<{
54
+ runtime: EffectRunner<R>;
55
+ task: Input;
56
+ }>>> | undefined>, {
57
+ id: string | undefined;
58
+ logic: PromiseActorLogic<A, Readonly<{
59
+ runtime: EffectRunner<R>;
60
+ task: Input;
61
+ }>>;
62
+ src: "run";
63
+ }, never, never, never, "done" | "failed" | "idle" | "running", string, EffectTaskInput<R>, undefined, EffectTaskEmitted<A>, MetaObject, {
64
+ id: "effectTask";
65
+ states: {
66
+ readonly done: Record<never, never>;
67
+ readonly failed: Record<never, never>;
68
+ readonly idle: Record<never, never>;
69
+ readonly running: Record<never, never>;
70
+ };
71
+ }>;
72
+ /** The machine `effectTask(run)` makes. */
73
+ export type EffectTask<Input, A, E = never, R = never> = ReturnType<typeof effectTask<Input, A, E, R>>;
@@ -0,0 +1,15 @@
1
+ import type * as Effect from "effect/Effect";
2
+ import type * as ManagedRuntime from "effect/ManagedRuntime";
3
+ import type { PromiseActorLogic } from "xstate";
4
+ /** What runs an effect for an actor: a `ManagedRuntime` that provides `R`. */
5
+ export type EffectRunner<R> = Pick<ManagedRuntime.ManagedRuntime<R, never>, "runPromise">;
6
+ /**
7
+ * XState actor logic that runs the effect of `run` on the runtime of the actor
8
+ * input. The actor's output is the effect's value; a failure or a defect fails
9
+ * the actor, and its parent machine gets the typed error or the defect as the
10
+ * error event. A stop of the actor interrupts the fiber, so its run settles.
11
+ * After the stop, no result of that run gets to the actor.
12
+ */
13
+ export declare const fromEffect: <A, E, R, I extends {
14
+ readonly runtime: EffectRunner<R>;
15
+ }>(run: (input: I) => Effect.Effect<A, E, R>) => PromiseActorLogic<A, I>;
@@ -0,0 +1,4 @@
1
+ export { effectTask } from "./effect-task.js";
2
+ export { fromEffect } from "./from-effect.js";
3
+ export type { EffectTask, EffectTaskContext, EffectTaskEmitted, EffectTaskEvent, EffectTaskInput } from "./effect-task.js";
4
+ export type { EffectRunner } from "./from-effect.js";
@@ -0,0 +1,4 @@
1
+ export { effectTask } from "./actors/effect-task.js";
2
+ export { fromEffect } from "./actors/from-effect.js";
3
+ export type { EffectTask, EffectTaskContext, EffectTaskEmitted, EffectTaskEvent, EffectTaskInput } from "./actors/effect-task.js";
4
+ export type { EffectRunner } from "./actors/from-effect.js";
package/dist/actors.js ADDED
@@ -0,0 +1 @@
1
+ import*as Cause from"effect/Cause";import*as Effect from"effect/Effect";import{assertEvent,assign,emit,setup}from"xstate";import{fromPromise}from"xstate";var unsettled=()=>Promise.race([]);var fromEffect=(run)=>fromPromise(async({input,signal})=>{try{const value=await input.runtime.runPromise(run(input),{signal});return signal.aborted?unsettled():value}catch(error){if(signal.aborted){return unsettled()}throw error}});var interruptAsFailure=(effect)=>Effect.catchCauseIf(effect,Cause.hasInterruptsOnly,(cause)=>Effect.fail(cause.reasons.find(Cause.isInterruptReason)??Cause.makeInterruptReason()));var effectTask=(run)=>{const actors={run:fromEffect(({task})=>interruptAsFailure(run(task)))};return setup({actors}).createMachine({context:({input})=>({error:undefined,output:undefined,runtime:input.runtime}),id:"effectTask",initial:"idle",on:{RESET:{actions:assign({error:undefined,output:undefined}),target:".idle"},RUN:{target:".running"}},states:{done:{},failed:{},idle:{},running:{entry:assign({error:undefined,output:undefined}),invoke:{input:({context,event})=>{assertEvent(event,"RUN");return{runtime:context.runtime,task:event.input}},onDone:{actions:[assign({output:({event})=>event.output}),emit(({event})=>({output:event.output,type:"done"}))],target:"done"},onError:{actions:[assign({error:({event})=>event.error}),emit(({event})=>({error:event.error,type:"failed"}))],target:"failed"},src:"run"},on:{CANCEL:{actions:[assign({error:()=>Cause.makeInterruptReason()}),emit(({context})=>({error:context.error,type:"failed"}))],target:"failed"},RESET:{},RUN:{}}}}})};export{effectTask,fromEffect};
@@ -0,0 +1,2 @@
1
+ export { EffectRuntimeProvider, makeEffectRuntimeContext, useEffectRuntime } from "./runtime-provider.js";
2
+ export type { EffectRuntimeContext, EffectRuntimeProviderProps } from "./runtime-provider.js";
@@ -0,0 +1,24 @@
1
+ import type { Context, ReactNode } from "react";
2
+ import type { EffectRunner } from "../actors.js";
3
+ /**
4
+ * The React context that carries an Effect runtime providing `R`. Each owner
5
+ * of a runtime, such as an SDK with its own services, makes one, so the
6
+ * runtimes of two owners in one tree stay apart and each keeps its type.
7
+ */
8
+ export type EffectRuntimeContext<R> = Context<EffectRunner<R> | null>;
9
+ /** Makes the context of the Effect runtimes that provide `R`. */
10
+ export declare const makeEffectRuntimeContext: <R>() => EffectRuntimeContext<R>;
11
+ export type EffectRuntimeProviderProps<R> = Readonly<{
12
+ children: ReactNode;
13
+ context: EffectRuntimeContext<R>;
14
+ /**
15
+ * The runtime of the tree. Its owner makes it and disposes it: a view
16
+ * passes it to the machines it starts, in their input, and a machine can
17
+ * outlive one render of the provider.
18
+ */
19
+ runtime: EffectRunner<R>;
20
+ }>;
21
+ /** Gives its tree `runtime` through `context`. */
22
+ export declare const EffectRuntimeProvider: <R>({ children, context: RuntimeContext, runtime }: EffectRuntimeProviderProps<R>) => import("react").JSX.Element;
23
+ /** The runtime of the nearest `EffectRuntimeProvider` of `context`. */
24
+ export declare const useEffectRuntime: <R>(context: EffectRuntimeContext<R>) => EffectRunner<R>;
@@ -0,0 +1,2 @@
1
+ export { EffectRuntimeProvider, makeEffectRuntimeContext, useEffectRuntime } from "./react/runtime-provider.js";
2
+ export type { EffectRuntimeContext, EffectRuntimeProviderProps } from "./react/runtime-provider.js";
package/dist/react.js ADDED
@@ -0,0 +1,2 @@
1
+ "use client";
2
+ import{createContext,useContext}from"react";import{jsx}from"react/jsx-runtime";var makeEffectRuntimeContext=()=>createContext(null);var EffectRuntimeProvider=({children,context:RuntimeContext,runtime})=>jsx(RuntimeContext.Provider,{value:runtime,children});var useEffectRuntime=(context)=>{const runtime=useContext(context);if(runtime===null){throw new Error("useEffectRuntime requires an EffectRuntimeProvider")}return runtime};export{EffectRuntimeProvider,makeEffectRuntimeContext,useEffectRuntime};
package/package.json CHANGED
@@ -1,9 +1,74 @@
1
1
  {
2
2
  "name": "@collective-intelligence/effect-xstate",
3
- "version": "0.0.0-placeholder.0",
4
- "description": "Placeholder. Released from collective-intelligence-platform/ci-app by GitHub Actions.",
5
- "license": "UNLICENSED",
3
+ "version": "0.7.1",
4
+ "description": "The Effect and XState bridge of Collective Intelligence: XState actors that run Effects, and the React provider of the Effect runtime that they run on.",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "files": [
8
+ "dist",
9
+ "README.md"
10
+ ],
11
+ "exports": {
12
+ "./actors": {
13
+ "ci-source": "./src/actors/index.ts",
14
+ "types": "./dist/actors.d.ts",
15
+ "import": "./dist/actors.js",
16
+ "default": "./dist/actors.js"
17
+ },
18
+ "./react": {
19
+ "ci-source": "./src/react/index.ts",
20
+ "types": "./dist/react.d.ts",
21
+ "import": "./dist/react.js",
22
+ "default": "./dist/react.js"
23
+ }
24
+ },
25
+ "scripts": {
26
+ "build": "bun tools/build.ts",
27
+ "test": "bun run test:unit && bun run test:pack",
28
+ "test:pack": "bun tools/test-pack.ts",
29
+ "test:unit": "vitest run src",
30
+ "format": "oxfmt --write .",
31
+ "format:check": "oxfmt --check .",
32
+ "format:check:staged": "oxfmt --check",
33
+ "lint": "node \"../../../.agents/scripts/managed-lint-runner.mjs\" --scope \"packages/public/effect-xstate\"",
34
+ "lint:staged": "oxlint --no-error-on-unmatched-pattern",
35
+ "prepack": "bun run build",
36
+ "type-check": "effect-tsgo tsrs --noEmit -p tsconfig.json"
37
+ },
38
+ "peerDependencies": {
39
+ "effect": "^4.1.0",
40
+ "react": ">=18.2 <20",
41
+ "xstate": "^5.33.3"
42
+ },
43
+ "peerDependenciesMeta": {
44
+ "react": {
45
+ "optional": true
46
+ }
47
+ },
6
48
  "publishConfig": {
7
49
  "access": "public"
8
- }
50
+ },
51
+ "devDependencies": {
52
+ "@effect/eslint-plugin": "0.3.2",
53
+ "@oxlint/plugins": "1.94.0",
54
+ "@punks/lints": "0.2.0",
55
+ "@types/bun": "1.4.2",
56
+ "@types/react": "^19.2.14",
57
+ "@types/react-dom": "^19.2.3",
58
+ "@typescript-eslint/utils": "8.22.0",
59
+ "@xstate/react": "6.1.0",
60
+ "effect": "4.1.4",
61
+ "eslint": "9.39.5",
62
+ "eslint-plugin-react-you-might-not-need-an-effect": "1.0.2",
63
+ "jsdom": "28.1.0",
64
+ "oxfmt": "0.66.0",
65
+ "oxlint": "1.94.0",
66
+ "react": "^19.2.8",
67
+ "react-dom": "^19.2.8",
68
+ "typescript": "5.9.3",
69
+ "ultracite": "7.10.7",
70
+ "vitest": "4.1.10",
71
+ "xstate": "5.33.3"
72
+ },
73
+ "gitHead": "822e4e2a259aa39e865d40504f6ac1e85d9c182a"
9
74
  }