@maiaos/client-core 0.1.0-canary.63 → 0.1.0-canary.65

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 (42) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +18 -16
  3. package/dist/cockpit/creation.d.ts +6 -2
  4. package/dist/cockpit/creation.js +11 -3
  5. package/dist/cockpit/index.d.ts +1 -1
  6. package/dist/cockpit/index.js +1 -1
  7. package/dist/cockpit/launcher.js +2 -8
  8. package/dist/cockpit/onboarding.js +7 -7
  9. package/dist/cockpit/routing.d.ts +1 -1
  10. package/dist/cockpit/routing.js +1 -0
  11. package/dist/interaction/recovery.d.ts +2 -0
  12. package/dist/portfolio/portfolio.d.ts +28 -2
  13. package/dist/presentation/collection-view.d.ts +14 -1
  14. package/dist/presentation/collection-view.js +67 -22
  15. package/dist/presentation/index.d.ts +1 -0
  16. package/dist/presentation/index.js +1 -0
  17. package/dist/presentation/live.d.ts +11 -2
  18. package/dist/presentation/session-view.d.ts +11 -2
  19. package/dist/records/agents.d.ts +36 -0
  20. package/dist/records/agents.js +20 -0
  21. package/dist/records/held.d.ts +82 -0
  22. package/dist/records/held.js +95 -0
  23. package/dist/records/index.d.ts +15 -0
  24. package/dist/records/index.js +7 -0
  25. package/dist/records/interactions.d.ts +62 -0
  26. package/dist/records/interactions.js +33 -0
  27. package/dist/records/kind.d.ts +44 -0
  28. package/dist/records/kind.js +36 -0
  29. package/dist/records/records.d.ts +41 -0
  30. package/dist/records/records.js +103 -0
  31. package/dist/records/routines.d.ts +28 -0
  32. package/dist/records/routines.js +35 -0
  33. package/dist/records/service.d.ts +42 -0
  34. package/dist/records/service.js +46 -0
  35. package/dist/records/state.d.ts +34 -0
  36. package/dist/records/state.js +26 -0
  37. package/dist/records/surface.d.ts +19 -0
  38. package/dist/records/surface.js +23 -0
  39. package/dist/records/watch.d.ts +24 -0
  40. package/dist/records/watch.js +68 -0
  41. package/dist/session-view/conversation.js +2 -5
  42. package/package.json +23 -3
@@ -0,0 +1,20 @@
1
+ import { AGENT_AGGREGATE_TYPE } from "@maiaos/sdk";
2
+ import { Effect, Option } from "effect";
3
+ import { pageOf } from "./kind.js";
4
+ import { makeRecordsView } from "./records.js";
5
+ const agentsKind = ({ projectId }) => ({
6
+ aggregateType: AGENT_AGGREGATE_TYPE,
7
+ id: (agent) => agent.id,
8
+ inScope: (agent) => agent.projectId === projectId,
9
+ list: (api, page) => api
10
+ .listAgents({ ...page, projectId })
11
+ .pipe(Effect.map((result) => pageOf(result.items, result.nextCursor))),
12
+ revision: (agent) => agent.revision,
13
+ show: (api, id) => api.showAgent(id).pipe(Effect.map(Option.map((result) => result.agent))),
14
+ });
15
+ /**
16
+ * The Agents of one Project, live: `agents.list` fills the snapshot and
17
+ * `agents.show` reads each Agent a committed change names.
18
+ */
19
+ export const makeAgentsView = (scope, emit) => makeRecordsView(agentsKind(scope), emit);
20
+ //# sourceMappingURL=agents.js.map
@@ -0,0 +1,82 @@
1
+ /** Most records one view holds. A longer list loses its tail and sets `truncated`. */
2
+ export declare const MAX_HELD_RECORDS = 500;
3
+ /** Why a view read its whole list again instead of applying one change. */
4
+ export type ReloadReason = "instance-changed" | "resync" | "subscription-lost";
5
+ /**
6
+ * One record as a read of the Node returned it. `seq` counts the reads in the
7
+ * order the view issued them, so a late answer differs from a newer one even
8
+ * when both name the same revision.
9
+ */
10
+ export type Held<TRecord> = {
11
+ readonly id: string;
12
+ readonly record: TRecord;
13
+ /** The Node's revision, a decimal string. */
14
+ readonly revision: string;
15
+ readonly seq: number;
16
+ };
17
+ /**
18
+ * The records one load of the list produced, and what the later changes did
19
+ * to them. A view's collection holds it as its page.
20
+ */
21
+ export type Snapshot<TRecord> = {
22
+ /** The Node process instance that answered the list read. */
23
+ readonly instanceId: string;
24
+ /**
25
+ * Set on the snapshot a reload produced. A read of one record keeps it; the
26
+ * next read of the whole list replaces it.
27
+ */
28
+ readonly reload?: ReloadReason;
29
+ readonly rows: ReadonlyMap<string, Held<TRecord>>;
30
+ /** The sequence number of the list read. */
31
+ readonly seq: number;
32
+ /** A limit cut the list, so the rows omit some records the Node holds. */
33
+ readonly truncated: boolean;
34
+ };
35
+ /** Keeps the fire history or other detail a newer list row lacks. */
36
+ export type Carry<TRecord> = (held: TRecord, read: TRecord) => TRecord;
37
+ type Options<TRecord> = {
38
+ readonly cap?: number;
39
+ readonly carry?: Carry<TRecord>;
40
+ };
41
+ /**
42
+ * Whether a read may replace the copy the view holds: it names a newer
43
+ * revision, or the same revision and the view issued it later. An older
44
+ * revision never replaces a newer one, whatever order the answers arrive in.
45
+ */
46
+ export declare const admits: <TRecord>(held: Held<TRecord> | undefined, read: Held<TRecord>) => boolean;
47
+ /** Whether the snapshot already holds `revision`, or a newer one, of a record. */
48
+ export declare const holdsRevision: <TRecord>(snapshot: Snapshot<TRecord>, id: string, revision: string) => boolean;
49
+ /**
50
+ * The snapshot of one list read, from the records its pages returned in the
51
+ * order the Node listed them. It holds at most `cap` records and sets
52
+ * `truncated` when the Node had more, whether the cap or the page limit
53
+ * stopped the read.
54
+ */
55
+ export declare const listed: <TRecord>(input: {
56
+ readonly held: readonly Held<TRecord>[];
57
+ readonly instanceId: string;
58
+ readonly more: boolean;
59
+ readonly seq: number;
60
+ }, cap?: number) => Snapshot<TRecord>;
61
+ /**
62
+ * Applies one read the Node answered. A record the snapshot does not hold
63
+ * joins it unless the cap stops it, which sets `truncated`. It returns the
64
+ * same snapshot when the read changes nothing. A read of one record is not a
65
+ * new snapshot, so the reload marker stays.
66
+ */
67
+ export declare const applyRead: <TRecord>(snapshot: Snapshot<TRecord>, read: Held<TRecord>, options?: Options<TRecord>) => Snapshot<TRecord>;
68
+ /**
69
+ * Removes a record the Node reports it no longer holds, unless a later read
70
+ * saw the record since. The reload marker stays, as it does for `applyRead`.
71
+ */
72
+ export declare const removeRead: <TRecord>(snapshot: Snapshot<TRecord>, id: string, seq: number) => Snapshot<TRecord>;
73
+ /**
74
+ * Settles a fresh list against the snapshot on screen. Another Node instance
75
+ * answers for itself, and the snapshot names the change of instance. On the
76
+ * same instance no row rolls back: each id keeps the copy `admits` prefers,
77
+ * and a record that the older snapshot alone holds stays when a read issued
78
+ * after the fresh list saw it, since that list cannot have seen the record.
79
+ */
80
+ export declare const settle: <TRecord>(current: Snapshot<TRecord> | undefined, fresh: Snapshot<TRecord>, options?: Options<TRecord>) => Snapshot<TRecord>;
81
+ export {};
82
+ //# sourceMappingURL=held.d.ts.map
@@ -0,0 +1,95 @@
1
+ /** Most records one view holds. A longer list loses its tail and sets `truncated`. */
2
+ export const MAX_HELD_RECORDS = 500;
3
+ /**
4
+ * Whether a read may replace the copy the view holds: it names a newer
5
+ * revision, or the same revision and the view issued it later. An older
6
+ * revision never replaces a newer one, whatever order the answers arrive in.
7
+ */
8
+ export const admits = (held, read) => {
9
+ if (held === undefined)
10
+ return true;
11
+ const revision = BigInt(read.revision);
12
+ const known = BigInt(held.revision);
13
+ return revision > known || (revision === known && read.seq > held.seq);
14
+ };
15
+ /** Whether the snapshot already holds `revision`, or a newer one, of a record. */
16
+ export const holdsRevision = (snapshot, id, revision) => {
17
+ const held = snapshot.rows.get(id);
18
+ return held !== undefined && BigInt(held.revision) >= BigInt(revision);
19
+ };
20
+ const replaced = (held, read, carry) => held === undefined || carry === undefined
21
+ ? read
22
+ : { ...read, record: carry(held.record, read.record) };
23
+ /**
24
+ * The snapshot of one list read, from the records its pages returned in the
25
+ * order the Node listed them. It holds at most `cap` records and sets
26
+ * `truncated` when the Node had more, whether the cap or the page limit
27
+ * stopped the read.
28
+ */
29
+ export const listed = (input, cap = MAX_HELD_RECORDS) => {
30
+ const unique = input.held.reduce((rows, read) => admits(rows.get(read.id), read) ? rows.set(read.id, read) : rows, new Map());
31
+ return {
32
+ instanceId: input.instanceId,
33
+ rows: new Map([...unique].slice(0, cap)),
34
+ seq: input.seq,
35
+ truncated: input.more || unique.size > cap,
36
+ };
37
+ };
38
+ /**
39
+ * Applies one read the Node answered. A record the snapshot does not hold
40
+ * joins it unless the cap stops it, which sets `truncated`. It returns the
41
+ * same snapshot when the read changes nothing. A read of one record is not a
42
+ * new snapshot, so the reload marker stays.
43
+ */
44
+ export const applyRead = (snapshot, read, options = {}) => {
45
+ const held = snapshot.rows.get(read.id);
46
+ if (!admits(held, read))
47
+ return snapshot;
48
+ if (held === undefined &&
49
+ snapshot.rows.size >= (options.cap ?? MAX_HELD_RECORDS))
50
+ return snapshot.truncated ? snapshot : { ...snapshot, truncated: true };
51
+ return {
52
+ ...snapshot,
53
+ rows: new Map(snapshot.rows).set(read.id, replaced(held, read, options.carry)),
54
+ };
55
+ };
56
+ /**
57
+ * Removes a record the Node reports it no longer holds, unless a later read
58
+ * saw the record since. The reload marker stays, as it does for `applyRead`.
59
+ */
60
+ export const removeRead = (snapshot, id, seq) => {
61
+ const held = snapshot.rows.get(id);
62
+ if (held === undefined || held.seq >= seq)
63
+ return snapshot;
64
+ const rows = new Map(snapshot.rows);
65
+ rows.delete(id);
66
+ return { ...snapshot, rows };
67
+ };
68
+ /**
69
+ * Settles a fresh list against the snapshot on screen. Another Node instance
70
+ * answers for itself, and the snapshot names the change of instance. On the
71
+ * same instance no row rolls back: each id keeps the copy `admits` prefers,
72
+ * and a record that the older snapshot alone holds stays when a read issued
73
+ * after the fresh list saw it, since that list cannot have seen the record.
74
+ */
75
+ export const settle = (current, fresh, options = {}) => {
76
+ if (current === undefined)
77
+ return fresh;
78
+ if (current.instanceId !== fresh.instanceId)
79
+ return { ...fresh, reload: "instance-changed" };
80
+ const merged = new Map([...fresh.rows].map(([id, read]) => {
81
+ const held = current.rows.get(id);
82
+ return [
83
+ id,
84
+ held !== undefined && !admits(held, read)
85
+ ? held
86
+ : replaced(held, read, options.carry),
87
+ ];
88
+ }));
89
+ const newer = [...current.rows.values()].filter((held) => !merged.has(held.id) && held.seq > fresh.seq);
90
+ const settled = newer.reduce((snapshot, held) => applyRead(snapshot, held, options), { ...fresh, rows: merged });
91
+ return fresh.reload === undefined
92
+ ? settled
93
+ : { ...settled, reload: fresh.reload };
94
+ };
95
+ //# sourceMappingURL=held.js.map
@@ -0,0 +1,15 @@
1
+ export type { AgentsScope, AgentsState } from "./agents.js";
2
+ export { makeAgentsView } from "./agents.js";
3
+ export type { ReloadReason } from "./held.js";
4
+ export { MAX_HELD_RECORDS } from "./held.js";
5
+ export type { InteractionsScope, InteractionsState } from "./interactions.js";
6
+ export { makeInteractionsView } from "./interactions.js";
7
+ export type { RecordsView } from "./records.js";
8
+ export type { RoutineRecord, RoutinesScope, RoutinesState, } from "./routines.js";
9
+ export { makeRoutinesView } from "./routines.js";
10
+ export type { ReadOptions, RecordsClient, RecordsService } from "./service.js";
11
+ export { attemptRead, makeRecordsLayer, RecordsApi } from "./service.js";
12
+ export type { RecordsCapability, RecordsState } from "./state.js";
13
+ export type { RecordsSurface, RuntimeRequest } from "./surface.js";
14
+ export { runtimeRequest } from "./surface.js";
15
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,7 @@
1
+ export { makeAgentsView } from "./agents.js";
2
+ export { MAX_HELD_RECORDS } from "./held.js";
3
+ export { makeInteractionsView } from "./interactions.js";
4
+ export { makeRoutinesView } from "./routines.js";
5
+ export { attemptRead, makeRecordsLayer, RecordsApi } from "./service.js";
6
+ export { runtimeRequest } from "./surface.js";
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,62 @@
1
+ import type { Interaction } from "@maiaos/sdk";
2
+ import { Effect } from "effect";
3
+ import type { RecordsState } from "./state.js";
4
+ /** The Session whose Interactions a view holds. */
5
+ export type InteractionsScope = {
6
+ readonly projectId: string;
7
+ readonly sessionId: string;
8
+ };
9
+ /** The state of an Interactions view. */
10
+ export type InteractionsState = RecordsState<Interaction>;
11
+ /**
12
+ * The Interactions of one Session, live: `interaction.list` fills the
13
+ * snapshot and `interaction.show` reads each Interaction a committed change
14
+ * names. An Interaction expires by the clock with no change, so one that reads
15
+ * `pending` here may read `expired` at the next `reread`.
16
+ */
17
+ export declare const makeInteractionsView: (scope: InteractionsScope, emit: (state: InteractionsState) => void) => Effect.Effect<import("./records.js").RecordsView<{
18
+ readonly activationId: null;
19
+ readonly answeredAt: string | null;
20
+ readonly answerSchemaDigest: string;
21
+ readonly body: string;
22
+ readonly createdAt: string;
23
+ readonly expiresAt: string | null;
24
+ readonly interactionId: string;
25
+ readonly lifecycleStatus: "answered" | "cancelled" | "expired" | "pending";
26
+ readonly origin: {
27
+ readonly kind: "external-cli";
28
+ };
29
+ readonly projectId: string;
30
+ readonly requestingActorId: string;
31
+ readonly revision: string;
32
+ readonly schemaVersion: "maia.interaction/v1";
33
+ readonly sessionId: string;
34
+ readonly subject: string;
35
+ readonly wakeDisposition: "record-only";
36
+ readonly kind: "question";
37
+ } | {
38
+ readonly activationId: null;
39
+ readonly answeredAt: string | null;
40
+ readonly answerSchemaDigest: string;
41
+ readonly body: string;
42
+ readonly createdAt: string;
43
+ readonly expiresAt: string | null;
44
+ readonly interactionId: string;
45
+ readonly lifecycleStatus: "answered" | "cancelled" | "expired" | "pending";
46
+ readonly origin: {
47
+ readonly kind: "external-cli";
48
+ };
49
+ readonly projectId: string;
50
+ readonly requestingActorId: string;
51
+ readonly revision: string;
52
+ readonly schemaVersion: "maia.interaction/v1";
53
+ readonly sessionId: string;
54
+ readonly subject: string;
55
+ readonly wakeDisposition: "record-only";
56
+ readonly kind: "selection";
57
+ readonly options: readonly {
58
+ readonly label: string;
59
+ readonly optionId: string;
60
+ }[];
61
+ }>, never, import("./service.js").RecordsApi | import("effect/Scope").Scope>;
62
+ //# sourceMappingURL=interactions.d.ts.map
@@ -0,0 +1,33 @@
1
+ import { Effect, Option } from "effect";
2
+ import { pageOf } from "./kind.js";
3
+ import { makeRecordsView } from "./records.js";
4
+ const INTERACTION_AGGREGATE_TYPE = "interaction";
5
+ /**
6
+ * A list row never carries the answer, but `interaction.show` does for the
7
+ * asking principal. A held record drops it: a renderer reads an answer when a
8
+ * person opens it and never keeps it in the collection.
9
+ */
10
+ const withoutAnswer = (detail) => {
11
+ const { answer, respondingActorId, responseDigest, ...projection } = detail;
12
+ return projection;
13
+ };
14
+ const interactionsKind = ({ projectId, sessionId, }) => ({
15
+ aggregateType: INTERACTION_AGGREGATE_TYPE,
16
+ id: (interaction) => interaction.interactionId,
17
+ inScope: (interaction) => interaction.projectId === projectId && interaction.sessionId === sessionId,
18
+ list: (api, page) => api
19
+ .listInteractions({ ...page, projectId, sessionId })
20
+ .pipe(Effect.map((result) => pageOf(result.items, result.nextCursor))),
21
+ revision: (interaction) => interaction.revision,
22
+ show: (api, id) => api
23
+ .showInteraction(id)
24
+ .pipe(Effect.map(Option.map((result) => withoutAnswer(result.interaction)))),
25
+ });
26
+ /**
27
+ * The Interactions of one Session, live: `interaction.list` fills the
28
+ * snapshot and `interaction.show` reads each Interaction a committed change
29
+ * names. An Interaction expires by the clock with no change, so one that reads
30
+ * `pending` here may read `expired` at the next `reread`.
31
+ */
32
+ export const makeInteractionsView = (scope, emit) => makeRecordsView(interactionsKind(scope), emit);
33
+ //# sourceMappingURL=interactions.js.map
@@ -0,0 +1,44 @@
1
+ import { Effect, type Option } from "effect";
2
+ import type { PresentationError } from "../presentation/index.js";
3
+ import { type Carry, type Snapshot } from "./held.js";
4
+ import type { RecordsService } from "./service.js";
5
+ /** One page of a records list, in the order the Node listed it. */
6
+ export type RecordsPage<TRecord> = {
7
+ readonly nextCursor?: string;
8
+ readonly records: readonly TRecord[];
9
+ };
10
+ /** The page of a list result, with no `nextCursor` key on the last page. */
11
+ export declare const pageOf: <TRecord>(records: readonly TRecord[], nextCursor: string | undefined) => RecordsPage<TRecord>;
12
+ /** What one record type adds to the generic view: how to list, show and identify it. */
13
+ export type RecordsKind<TRecord> = {
14
+ /** The name the Node's change feed gives records of this type. */
15
+ readonly aggregateType: string;
16
+ /** Keeps detail a newer list row lacks, such as a routine's fire history. */
17
+ readonly carry?: Carry<TRecord>;
18
+ readonly id: (record: TRecord) => string;
19
+ /**
20
+ * Whether the view's scope holds the record. The change feed names every
21
+ * record of the type, so the view ignores a read of one outside the scope.
22
+ */
23
+ readonly inScope: (record: TRecord) => boolean;
24
+ readonly list: (api: RecordsService, page: {
25
+ readonly afterCursor?: string;
26
+ readonly limit: number;
27
+ }) => Effect.Effect<RecordsPage<TRecord>, PresentationError>;
28
+ /** The Node's revision of the record, a decimal string. */
29
+ readonly revision: (record: TRecord) => string;
30
+ /** `None` when the Node holds no such record. */
31
+ readonly show: (api: RecordsService, id: string) => Effect.Effect<Option.Option<TRecord>, PresentationError>;
32
+ };
33
+ /** The list read number `seq` of one view. */
34
+ export type ListRead<TRecord> = {
35
+ readonly api: RecordsService;
36
+ readonly kind: RecordsKind<TRecord>;
37
+ readonly seq: number;
38
+ };
39
+ /**
40
+ * Reads the list page by page, up to the cap, as the snapshot of the Node
41
+ * instance that answers.
42
+ */
43
+ export declare const readSnapshot: <TRecord>(read: ListRead<TRecord>) => Effect.Effect<Snapshot<TRecord>, PresentationError>;
44
+ //# sourceMappingURL=kind.d.ts.map
@@ -0,0 +1,36 @@
1
+ import { Effect } from "effect";
2
+ import { listed, MAX_HELD_RECORDS, } from "./held.js";
3
+ /** Most records the Node returns in one page of any records list. */
4
+ const PAGE_SIZE = 100;
5
+ /** The page of a list result, with no `nextCursor` key on the last page. */
6
+ export const pageOf = (records, nextCursor) => nextCursor === undefined ? { records } : { nextCursor, records };
7
+ const readPages = (read, held, cursor) => {
8
+ const { api, kind, seq } = read;
9
+ const request = cursor === undefined
10
+ ? { limit: PAGE_SIZE }
11
+ : { afterCursor: cursor, limit: PAGE_SIZE };
12
+ return kind.list(api, request).pipe(Effect.flatMap((page) => {
13
+ const all = [
14
+ ...held,
15
+ ...page.records.map((record) => ({
16
+ id: kind.id(record),
17
+ record,
18
+ revision: kind.revision(record),
19
+ seq,
20
+ })),
21
+ ];
22
+ return page.nextCursor === undefined || all.length >= MAX_HELD_RECORDS
23
+ ? Effect.succeed({ held: all, more: page.nextCursor !== undefined })
24
+ : readPages(read, all, page.nextCursor);
25
+ }));
26
+ };
27
+ /**
28
+ * Reads the list page by page, up to the cap, as the snapshot of the Node
29
+ * instance that answers.
30
+ */
31
+ export const readSnapshot = (read) => Effect.gen(function* () {
32
+ const instanceId = yield* read.api.instance;
33
+ const { held, more } = yield* readPages(read, []);
34
+ return listed({ held, instanceId, more, seq: read.seq });
35
+ });
36
+ //# sourceMappingURL=kind.js.map
@@ -0,0 +1,41 @@
1
+ import { Effect } from "effect";
2
+ import { type NodeChanges } from "../presentation/index.js";
3
+ import { type ReloadReason } from "./held.js";
4
+ import { type RecordsKind } from "./kind.js";
5
+ import { RecordsApi } from "./service.js";
6
+ import { type RecordsState } from "./state.js";
7
+ /** One records view: its state, explicit reads and the live follow of the Node. */
8
+ export type RecordsView<TRecord> = {
9
+ /** Reads the list once, for the first state. */
10
+ readonly load: Effect.Effect<void>;
11
+ readonly read: Effect.Effect<RecordsState<TRecord>>;
12
+ /** Reads the whole list again under a new generation. */
13
+ readonly refresh: Effect.Effect<void>;
14
+ /**
15
+ * Reads the whole list again once any read in flight settles, and recovers
16
+ * from a failed read. A `reason` marks the new snapshot `reloaded` with it;
17
+ * a reload with no reason is a quiet reread and sets no notice.
18
+ */
19
+ readonly reload: (reason?: ReloadReason) => Effect.Effect<void>;
20
+ /**
21
+ * Reads these records by id with `show` and applies each read when its
22
+ * revision exceeds the held one; a record the Node no longer holds leaves
23
+ * the view. Yields `false` when a read failed.
24
+ */
25
+ readonly reread: (ids: readonly string[]) => Effect.Effect<boolean>;
26
+ /**
27
+ * Keeps the view current from the Node's committed changes until
28
+ * interruption: it rereads the records a change names and reloads the list
29
+ * on a resync, a change that names no record, a lost subscription or a new
30
+ * Node instance.
31
+ */
32
+ readonly watch: Effect.Effect<void, never, NodeChanges>;
33
+ };
34
+ /**
35
+ * One view of a record type, held as a bounded snapshot that follows the
36
+ * Node's committed changes. It reuses the collection view's generation, so the
37
+ * view drops a list read that a newer read replaced, and it never lets an
38
+ * older revision of a record replace a newer one.
39
+ */
40
+ export declare const makeRecordsView: <TRecord>(kind: RecordsKind<TRecord>, emit: (state: RecordsState<TRecord>) => void) => Effect.Effect<RecordsView<TRecord>, never, RecordsApi | import("effect/Scope").Scope>;
41
+ //# sourceMappingURL=records.d.ts.map
@@ -0,0 +1,103 @@
1
+ import { Duration, Effect, Exit, Option, Ref, Schedule } from "effect";
2
+ import { makeCollectionView, } from "../presentation/index.js";
3
+ import { applyRead, removeRead, settle, } from "./held.js";
4
+ import { readSnapshot } from "./kind.js";
5
+ import { RecordsApi } from "./service.js";
6
+ import { toState } from "./state.js";
7
+ import { followChanges } from "./watch.js";
8
+ const READ_CONCURRENCY = 4;
9
+ const SETTLE_POLL = Duration.millis(100);
10
+ const nextSeq = (order) => Ref.updateAndGet(order, (value) => value + 1);
11
+ /**
12
+ * The list read of the collection. A read that fails or loses its place hands
13
+ * its reload reason to the next read.
14
+ */
15
+ const readList = ({ api, kind, order, pending }) => Effect.gen(function* () {
16
+ const reload = yield* Ref.getAndSet(pending, Option.none());
17
+ const seq = yield* nextSeq(order);
18
+ const snapshot = yield* readSnapshot({ api, kind, seq }).pipe(Effect.onExit((exit) => Exit.isSuccess(exit)
19
+ ? Effect.void
20
+ : Ref.update(pending, (now) => (Option.isSome(now) ? now : reload))));
21
+ return Option.isSome(reload)
22
+ ? { ...snapshot, reload: reload.value }
23
+ : snapshot;
24
+ });
25
+ /** The page after a `show`: the record joins or replaces its row, or leaves. */
26
+ const withShown = (page, kind, { id, seq, shown }) => {
27
+ if (Option.isNone(shown))
28
+ return removeRead(page, id, seq);
29
+ const record = shown.value;
30
+ return kind.inScope(record)
31
+ ? applyRead(page, { id, record, revision: kind.revision(record), seq }, { carry: kind.carry })
32
+ : page;
33
+ };
34
+ const rereadOne = ({ api, kind, order }, collection, id) => Effect.gen(function* () {
35
+ const seq = yield* nextSeq(order);
36
+ const shown = yield* kind.show(api, id);
37
+ yield* collection.patch((page) => withShown(page, kind, { id, seq, shown }));
38
+ return true;
39
+ }).pipe(Effect.catch(() => Effect.succeed(false)));
40
+ /** The collection's state once no read runs; it polls while a read runs. */
41
+ const settled = (collection) => collection.read.pipe(Effect.repeat({
42
+ schedule: Schedule.spaced(SETTLE_POLL),
43
+ until: (state) => state.status !== "loading",
44
+ }));
45
+ /**
46
+ * Reads the whole list again, once any read in flight settles. A list that
47
+ * failed to read keeps its fault state on screen until the new read settles,
48
+ * so a timed reload never flashes `loading`.
49
+ */
50
+ const reloader = ({ pending }, collection) => (reason) => Effect.gen(function* () {
51
+ if (reason !== undefined)
52
+ yield* Ref.set(pending, Option.some(reason));
53
+ const state = yield* settled(collection);
54
+ yield* state.status === "ready"
55
+ ? collection.refresh
56
+ : collection.loadQuietly;
57
+ yield* settled(collection);
58
+ });
59
+ const viewOf = (parts, collection) => {
60
+ const reread = (ids) => Effect.forEach(ids, (id) => rereadOne(parts, collection, id), {
61
+ concurrency: READ_CONCURRENCY,
62
+ }).pipe(Effect.map((results) => results.every(Boolean)));
63
+ const reload = reloader(parts, collection);
64
+ return {
65
+ load: collection.load(),
66
+ read: Effect.map(collection.read, toState),
67
+ refresh: collection.refresh,
68
+ reload,
69
+ reread,
70
+ watch: followChanges({
71
+ aggregateType: parts.kind.aggregateType,
72
+ instance: parts.api.instance,
73
+ known: Effect.map(settled(collection), ({ page }) => Option.fromUndefinedOr(page)),
74
+ reload,
75
+ reread,
76
+ }),
77
+ };
78
+ };
79
+ /**
80
+ * One view of a record type, held as a bounded snapshot that follows the
81
+ * Node's committed changes. It reuses the collection view's generation, so the
82
+ * view drops a list read that a newer read replaced, and it never lets an
83
+ * older revision of a record replace a newer one.
84
+ */
85
+ export const makeRecordsView = (kind, emit) => Effect.gen(function* () {
86
+ const parts = {
87
+ api: yield* RecordsApi,
88
+ kind,
89
+ order: yield* Ref.make(0),
90
+ pending: yield* Ref.make(Option.none()),
91
+ };
92
+ // The identifier is constant, so the collection view's error type cannot occur.
93
+ const collection = yield* makeCollectionView({
94
+ identifier: Effect.succeed("records"),
95
+ merge: (fresh, current) => settle(current, fresh, { carry: kind.carry }),
96
+ read: () => readList(parts),
97
+ selected: () => Option.none(),
98
+ }, (state) => {
99
+ emit(toState(state));
100
+ }).pipe(Effect.orDie);
101
+ return viewOf(parts, collection);
102
+ });
103
+ //# sourceMappingURL=records.js.map
@@ -0,0 +1,28 @@
1
+ import type { Routine, RoutineFire } from "@maiaos/sdk";
2
+ import { Effect } from "effect";
3
+ import type { RecordsState } from "./state.js";
4
+ /**
5
+ * A Routine with its fire history, newest first, as of the last
6
+ * `routine.show` of it. The list carries no history, so a Routine that a view
7
+ * merely listed has none, and a later list row keeps the history the Routine
8
+ * had.
9
+ */
10
+ export type RoutineRecord = {
11
+ readonly fires?: readonly RoutineFire[];
12
+ readonly routine: Routine;
13
+ };
14
+ /** The Project whose Routines a view holds; without one, the Node's every Routine. */
15
+ export type RoutinesScope = {
16
+ readonly projectId?: string;
17
+ };
18
+ /** The state of a Routines view. */
19
+ export type RoutinesState = RecordsState<RoutineRecord>;
20
+ /**
21
+ * The Routines of one Project, or of the whole Node, live: `routine.list`
22
+ * fills the snapshot and `routine.show`, which carries the fire history, reads
23
+ * each Routine a committed change names. The Node reports no committed change
24
+ * for a scheduled fire or for the outcome of a fire, so the history moves when
25
+ * a `show` reads it: `reread` the Routine a person opens.
26
+ */
27
+ export declare const makeRoutinesView: (scope: RoutinesScope, emit: (state: RoutinesState) => void) => Effect.Effect<import("./records.js").RecordsView<RoutineRecord>, never, import("./service.js").RecordsApi | import("effect/Scope").Scope>;
28
+ //# sourceMappingURL=routines.d.ts.map
@@ -0,0 +1,35 @@
1
+ import { Effect, Option } from "effect";
2
+ import { pageOf } from "./kind.js";
3
+ import { makeRecordsView } from "./records.js";
4
+ const ROUTINE_AGGREGATE_TYPE = "routine";
5
+ /** A list row has no history, so the row keeps the history it replaces. */
6
+ const carry = (held, read) => read.fires === undefined && held.fires !== undefined
7
+ ? { ...read, fires: held.fires }
8
+ : read;
9
+ const list = (projectId) => (api, page) => api
10
+ .listRoutines({
11
+ ...page,
12
+ ...(projectId === undefined ? {} : { projectId }),
13
+ })
14
+ .pipe(Effect.map((result) => pageOf(result.items.map((routine) => ({ routine })), result.nextCursor)));
15
+ const show = (api, id) => api
16
+ .showRoutine(id)
17
+ .pipe(Effect.map(Option.map(({ fires, routine }) => ({ fires, routine }))));
18
+ const routinesKind = ({ projectId }) => ({
19
+ aggregateType: ROUTINE_AGGREGATE_TYPE,
20
+ carry,
21
+ id: ({ routine }) => routine.id,
22
+ inScope: ({ routine }) => projectId === undefined || routine.template.projectId === projectId,
23
+ list: list(projectId),
24
+ revision: ({ routine }) => routine.revision,
25
+ show,
26
+ });
27
+ /**
28
+ * The Routines of one Project, or of the whole Node, live: `routine.list`
29
+ * fills the snapshot and `routine.show`, which carries the fire history, reads
30
+ * each Routine a committed change names. The Node reports no committed change
31
+ * for a scheduled fire or for the outcome of a fire, so the history moves when
32
+ * a `show` reads it: `reread` the Routine a person opens.
33
+ */
34
+ export const makeRoutinesView = (scope, emit) => makeRecordsView(routinesKind(scope), emit);
35
+ //# sourceMappingURL=routines.js.map
@@ -0,0 +1,42 @@
1
+ import { type AgentsListParameters, type AgentsListResult, type AgentsShowResult, type InteractionListParameters, type InteractionListResult, type InteractionShowResult, type LocalNodeClient, type RoutineListParameters, type RoutineListResult, type RoutineShowResult } from "@maiaos/sdk";
2
+ import { Context, Effect, Layer, Option } from "effect";
3
+ import { PresentationError } from "../presentation/index.js";
4
+ /** The bounded reads of agents, routines and interactions, and the Node instance. */
5
+ export type RecordsService = {
6
+ /** The answering Node process instance, from `node.status`. */
7
+ readonly instance: Effect.Effect<string, PresentationError>;
8
+ readonly listAgents: (input: AgentsListParameters) => Effect.Effect<AgentsListResult, PresentationError>;
9
+ readonly listInteractions: (input: InteractionListParameters) => Effect.Effect<InteractionListResult, PresentationError>;
10
+ readonly listRoutines: (input: RoutineListParameters) => Effect.Effect<RoutineListResult, PresentationError>;
11
+ /** `None` when the Node holds no such agent. */
12
+ readonly showAgent: (agentId: string) => Effect.Effect<Option.Option<AgentsShowResult>, PresentationError>;
13
+ /** `None` when the Node holds no such interaction. */
14
+ readonly showInteraction: (interactionId: string) => Effect.Effect<Option.Option<InteractionShowResult>, PresentationError>;
15
+ /** `None` when the Node holds no such routine. */
16
+ readonly showRoutine: (routineId: string) => Effect.Effect<Option.Option<RoutineShowResult>, PresentationError>;
17
+ };
18
+ declare const RecordsApi_base: Context.ServiceClass<RecordsApi, "@maiaos/client-core/RecordsApi", RecordsService>;
19
+ /** The service the records views read through. */
20
+ export declare class RecordsApi extends RecordsApi_base {
21
+ }
22
+ /** The SDK operations the records views use. */
23
+ export type RecordsClient = Pick<LocalNodeClient, "listAgents" | "listInteractions" | "listRoutines" | "showAgent" | "showInteraction" | "showRoutine">;
24
+ type Dependencies = {
25
+ readonly client: RecordsClient;
26
+ readonly instance: RecordsService["instance"];
27
+ };
28
+ /** What an SDK read needs of its caller: the request identifier and the cancellation signal. */
29
+ export type ReadOptions = {
30
+ readonly messageId: string;
31
+ readonly signal: AbortSignal;
32
+ };
33
+ /**
34
+ * One SDK read as an Effect: a fresh request identifier, a 5.5 s deadline
35
+ * that fails as `offline`, and the failure the request raised. Interrupting
36
+ * the Effect aborts the request's signal.
37
+ */
38
+ export declare const attemptRead: <TResult>(request: (options: ReadOptions) => Promise<TResult>) => Effect.Effect<TResult, unknown>;
39
+ /** The records reads over an SDK client; the platform supplies the Node instance. */
40
+ export declare const makeRecordsLayer: ({ client, instance }: Dependencies) => Layer.Layer<RecordsApi, never, never>;
41
+ export {};
42
+ //# sourceMappingURL=service.d.ts.map