bb-plugin-goal 0.0.0-stage → 0.1.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 +95 -2
- package/app.tsx +805 -0
- package/components/ui/button.tsx +60 -0
- package/components/ui/coarse-pointer-sizing.ts +76 -0
- package/components/ui/input.tsx +31 -0
- package/components/ui/motion.ts +21 -0
- package/dist/app.css +2 -0
- package/dist/app.js +1 -0
- package/dist/app.meta.json +19 -0
- package/dist/package.json +1 -0
- package/dist/server.js +442 -0
- package/dist/server.js.map +6 -0
- package/dist/server.meta.json +11 -0
- package/lib/utils.ts +24 -0
- package/package.json +72 -4
- package/server.ts +1758 -0
- package/src/coordinator.ts +892 -0
- package/src/domain.ts +407 -0
- package/src/failure.ts +202 -0
- package/src/history-cursor.ts +46 -0
- package/src/progress.ts +167 -0
- package/src/repository.ts +1735 -0
- package/src/runtime.ts +189 -0
- package/src/usage-wait.ts +21 -0
package/src/runtime.ts
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import type BetterSqlite3 from "better-sqlite3";
|
|
2
|
+
import { Effect, Layer, ManagedRuntime, Result } from "effect";
|
|
3
|
+
import {
|
|
4
|
+
GoalClock,
|
|
5
|
+
GoalContinuationCoordinator,
|
|
6
|
+
GoalCoordinator,
|
|
7
|
+
GoalIdGenerator,
|
|
8
|
+
liveGoalClockLayer,
|
|
9
|
+
liveGoalIdGeneratorLayer,
|
|
10
|
+
makeGoalThreadGatewayLayer,
|
|
11
|
+
type GoalThreadGatewayAdapter,
|
|
12
|
+
} from "./coordinator";
|
|
13
|
+
import {
|
|
14
|
+
goalErrorToDto,
|
|
15
|
+
type GoalCommand,
|
|
16
|
+
type GoalCommandResult,
|
|
17
|
+
type GoalDto,
|
|
18
|
+
type GoalFailure,
|
|
19
|
+
type GoalFailureEventIdentity,
|
|
20
|
+
} from "./domain";
|
|
21
|
+
import { makeGoalRepositoryLayer } from "./repository";
|
|
22
|
+
|
|
23
|
+
export interface GoalRuntime {
|
|
24
|
+
readonly run: (command: GoalCommand) => Promise<GoalCommandResult>;
|
|
25
|
+
readonly enqueueIdle: (
|
|
26
|
+
threadId: string,
|
|
27
|
+
opportunityKey: string,
|
|
28
|
+
) => Promise<void>;
|
|
29
|
+
readonly recordFailure: (
|
|
30
|
+
threadId: string,
|
|
31
|
+
failure: GoalFailure,
|
|
32
|
+
event?: GoalFailureEventIdentity,
|
|
33
|
+
observedEvents?: readonly GoalFailureEventIdentity[],
|
|
34
|
+
) => Promise<GoalDto | null>;
|
|
35
|
+
readonly archiveThread: (threadId: string) => Promise<GoalDto | null>;
|
|
36
|
+
readonly deleteThread: (threadId: string) => Promise<void>;
|
|
37
|
+
readonly ownedThreadIds: () => Promise<readonly string[]>;
|
|
38
|
+
readonly recoverContinuations: () => Promise<void>;
|
|
39
|
+
readonly assessSettledContinuations: (
|
|
40
|
+
signal?: AbortSignal,
|
|
41
|
+
) => Promise<readonly GoalDto[]>;
|
|
42
|
+
readonly recoverUsageLimits: () => Promise<readonly GoalDto[]>;
|
|
43
|
+
readonly nextUsageLimitReset: () => Promise<string | null>;
|
|
44
|
+
/**
|
|
45
|
+
* Process one claimed Continuation. onGoalChanged receives a Goal the
|
|
46
|
+
* processing changed (a delivery failure pauses it) so it can be published.
|
|
47
|
+
*/
|
|
48
|
+
readonly processContinuation: (
|
|
49
|
+
signal: AbortSignal,
|
|
50
|
+
onGoalChanged?: (goal: GoalDto) => void,
|
|
51
|
+
) => Promise<boolean>;
|
|
52
|
+
readonly dispose: () => Promise<void>;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface GoalRuntimeOptions {
|
|
56
|
+
readonly nextGoalId?: () => string;
|
|
57
|
+
readonly nowIso?: () => string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function makeGoalRuntime(
|
|
61
|
+
database: BetterSqlite3.Database,
|
|
62
|
+
gateway: GoalThreadGatewayAdapter,
|
|
63
|
+
options: GoalRuntimeOptions = {},
|
|
64
|
+
): GoalRuntime {
|
|
65
|
+
const idLayer =
|
|
66
|
+
options.nextGoalId === undefined
|
|
67
|
+
? liveGoalIdGeneratorLayer
|
|
68
|
+
: Layer.succeed(
|
|
69
|
+
GoalIdGenerator,
|
|
70
|
+
GoalIdGenerator.of({
|
|
71
|
+
next: Effect.sync(options.nextGoalId),
|
|
72
|
+
}),
|
|
73
|
+
);
|
|
74
|
+
const clockLayer =
|
|
75
|
+
options.nowIso === undefined
|
|
76
|
+
? liveGoalClockLayer
|
|
77
|
+
: Layer.succeed(
|
|
78
|
+
GoalClock,
|
|
79
|
+
GoalClock.of({
|
|
80
|
+
nowIso: Effect.sync(options.nowIso),
|
|
81
|
+
}),
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
const dependencies = Layer.mergeAll(
|
|
85
|
+
makeGoalRepositoryLayer(database),
|
|
86
|
+
makeGoalThreadGatewayLayer(gateway),
|
|
87
|
+
idLayer,
|
|
88
|
+
clockLayer,
|
|
89
|
+
);
|
|
90
|
+
const coordinatorLayer = Layer.mergeAll(
|
|
91
|
+
GoalCoordinator.layer,
|
|
92
|
+
GoalContinuationCoordinator.layer,
|
|
93
|
+
).pipe(Layer.provide(dependencies));
|
|
94
|
+
const runtime = ManagedRuntime.make(coordinatorLayer);
|
|
95
|
+
let disposed = false;
|
|
96
|
+
|
|
97
|
+
return {
|
|
98
|
+
async run(command) {
|
|
99
|
+
const result = await runtime.runPromise(
|
|
100
|
+
GoalCoordinator.use((coordinator) => coordinator.execute(command)).pipe(
|
|
101
|
+
Effect.result,
|
|
102
|
+
),
|
|
103
|
+
);
|
|
104
|
+
return Result.isFailure(result)
|
|
105
|
+
? { ok: false, error: goalErrorToDto(result.failure) }
|
|
106
|
+
: { ok: true, ...result.success };
|
|
107
|
+
},
|
|
108
|
+
async enqueueIdle(threadId, opportunityKey) {
|
|
109
|
+
await runtime.runPromise(
|
|
110
|
+
GoalContinuationCoordinator.use((coordinator) =>
|
|
111
|
+
coordinator.enqueueIdle({ threadId, opportunityKey }),
|
|
112
|
+
),
|
|
113
|
+
);
|
|
114
|
+
},
|
|
115
|
+
async recordFailure(threadId, failure, event, observedEvents) {
|
|
116
|
+
const result = await runtime.runPromise(
|
|
117
|
+
GoalCoordinator.use((coordinator) =>
|
|
118
|
+
coordinator.execute({
|
|
119
|
+
type: "failure",
|
|
120
|
+
threadId,
|
|
121
|
+
failure,
|
|
122
|
+
event,
|
|
123
|
+
observedEvents,
|
|
124
|
+
}),
|
|
125
|
+
),
|
|
126
|
+
);
|
|
127
|
+
return "goal" in result ? result.goal : null;
|
|
128
|
+
},
|
|
129
|
+
archiveThread(threadId) {
|
|
130
|
+
return runtime.runPromise(
|
|
131
|
+
GoalCoordinator.use((coordinator) =>
|
|
132
|
+
coordinator.archiveThread(threadId),
|
|
133
|
+
),
|
|
134
|
+
);
|
|
135
|
+
},
|
|
136
|
+
deleteThread(threadId) {
|
|
137
|
+
return runtime.runPromise(
|
|
138
|
+
GoalCoordinator.use((coordinator) => coordinator.deleteThread(threadId)),
|
|
139
|
+
);
|
|
140
|
+
},
|
|
141
|
+
ownedThreadIds() {
|
|
142
|
+
return runtime.runPromise(
|
|
143
|
+
GoalCoordinator.use((coordinator) => coordinator.ownedThreadIds()),
|
|
144
|
+
);
|
|
145
|
+
},
|
|
146
|
+
async recoverContinuations() {
|
|
147
|
+
await runtime.runPromise(
|
|
148
|
+
GoalContinuationCoordinator.use((coordinator) => coordinator.recover()),
|
|
149
|
+
);
|
|
150
|
+
},
|
|
151
|
+
async assessSettledContinuations(signal) {
|
|
152
|
+
return runtime.runPromise(
|
|
153
|
+
GoalContinuationCoordinator.use((coordinator) =>
|
|
154
|
+
coordinator.assessSettled(signal),
|
|
155
|
+
),
|
|
156
|
+
);
|
|
157
|
+
},
|
|
158
|
+
async recoverUsageLimits() {
|
|
159
|
+
return runtime.runPromise(
|
|
160
|
+
GoalContinuationCoordinator.use((coordinator) =>
|
|
161
|
+
coordinator.recoverUsageLimits(),
|
|
162
|
+
),
|
|
163
|
+
);
|
|
164
|
+
},
|
|
165
|
+
async nextUsageLimitReset() {
|
|
166
|
+
return runtime.runPromise(
|
|
167
|
+
GoalContinuationCoordinator.use((coordinator) =>
|
|
168
|
+
coordinator.nextUsageLimitReset(),
|
|
169
|
+
),
|
|
170
|
+
);
|
|
171
|
+
},
|
|
172
|
+
async processContinuation(signal, onGoalChanged) {
|
|
173
|
+
const result = await runtime.runPromise(
|
|
174
|
+
GoalContinuationCoordinator.use((coordinator) =>
|
|
175
|
+
coordinator.process(signal),
|
|
176
|
+
),
|
|
177
|
+
);
|
|
178
|
+
if (result.kind === "released" && result.changedGoal !== undefined) {
|
|
179
|
+
onGoalChanged?.(result.changedGoal);
|
|
180
|
+
}
|
|
181
|
+
return result.kind !== "idle";
|
|
182
|
+
},
|
|
183
|
+
async dispose() {
|
|
184
|
+
if (disposed) return;
|
|
185
|
+
disposed = true;
|
|
186
|
+
await runtime.dispose();
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimum wait before re-checking a usage-limit reset that is already due.
|
|
3
|
+
*
|
|
4
|
+
* A reset in the past means recoverUsageLimits() left the Goal waiting because
|
|
5
|
+
* its thread is not eligible yet (Plan mode, awaiting input, busy). Without a
|
|
6
|
+
* floor the continuation worker would wait 0 ms and spin; the thread's next
|
|
7
|
+
* idle event still wakes the worker earlier.
|
|
8
|
+
*/
|
|
9
|
+
export const DUE_USAGE_RESET_POLL_MS = 5_000;
|
|
10
|
+
|
|
11
|
+
/** Milliseconds until the next usage-limit reset check, or null when none. */
|
|
12
|
+
export function usageResetWaitMs(
|
|
13
|
+
nextReset: string | null,
|
|
14
|
+
nowMs: number,
|
|
15
|
+
): number | null {
|
|
16
|
+
if (nextReset === null) return null;
|
|
17
|
+
const resetMs = Date.parse(nextReset);
|
|
18
|
+
if (!Number.isFinite(resetMs)) return null;
|
|
19
|
+
const untilReset = resetMs - nowMs;
|
|
20
|
+
return untilReset > 0 ? untilReset : DUE_USAGE_RESET_POLL_MS;
|
|
21
|
+
}
|