@maiaos/client-core 0.1.0-canary.7 → 0.1.0-canary.71
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 +26 -23
- package/dist/cockpit/creation.d.ts +6 -2
- package/dist/cockpit/creation.js +11 -3
- package/dist/cockpit/index.d.ts +1 -1
- package/dist/cockpit/index.js +1 -1
- package/dist/cockpit/launcher.js +2 -8
- package/dist/cockpit/onboarding.js +10 -8
- package/dist/cockpit/routing.d.ts +1 -1
- package/dist/cockpit/routing.js +1 -0
- package/dist/interaction/recovery.d.ts +2 -0
- package/dist/portfolio/portfolio.d.ts +76 -2
- package/dist/presentation/collection-view.d.ts +14 -1
- package/dist/presentation/collection-view.js +67 -22
- package/dist/presentation/index.d.ts +1 -0
- package/dist/presentation/index.js +1 -0
- package/dist/presentation/live.d.ts +35 -2
- package/dist/presentation/session-view.d.ts +35 -2
- package/dist/records/agents.d.ts +36 -0
- package/dist/records/agents.js +20 -0
- package/dist/records/held.d.ts +82 -0
- package/dist/records/held.js +95 -0
- package/dist/records/index.d.ts +15 -0
- package/dist/records/index.js +7 -0
- package/dist/records/interactions.d.ts +62 -0
- package/dist/records/interactions.js +33 -0
- package/dist/records/kind.d.ts +44 -0
- package/dist/records/kind.js +36 -0
- package/dist/records/records.d.ts +41 -0
- package/dist/records/records.js +103 -0
- package/dist/records/routines.d.ts +28 -0
- package/dist/records/routines.js +35 -0
- package/dist/records/service.d.ts +42 -0
- package/dist/records/service.js +46 -0
- package/dist/records/state.d.ts +34 -0
- package/dist/records/state.js +26 -0
- package/dist/records/surface.d.ts +19 -0
- package/dist/records/surface.js +23 -0
- package/dist/records/watch.d.ts +24 -0
- package/dist/records/watch.js +68 -0
- package/dist/session-view/conversation.js +2 -5
- package/dist/session-view/session-view.d.ts +2 -0
- package/dist/session-view/session-view.js +9 -6
- package/dist/session-view/timeline.d.ts +10 -1
- package/dist/session-view/timeline.js +12 -4
- package/package.json +23 -3
|
@@ -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
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { classifyClientError, } from "@maiaos/sdk";
|
|
2
|
+
import { Context, Effect, Layer, Option } from "effect";
|
|
3
|
+
import { nextCompactId } from "../id-generator/index.js";
|
|
4
|
+
import { PresentationError } from "../presentation/index.js";
|
|
5
|
+
/** The service the records views read through. */
|
|
6
|
+
export class RecordsApi extends Context.Service()("@maiaos/client-core/RecordsApi") {
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* The SDK's classification in the views' words: a Node without the operation
|
|
10
|
+
* is `unavailable`, a refusal by the credential, role or platform bridge is
|
|
11
|
+
* `denied`, and a record the Node does not hold is `scope_mismatch`.
|
|
12
|
+
*/
|
|
13
|
+
const reasonOf = (error) => {
|
|
14
|
+
if (error instanceof PresentationError)
|
|
15
|
+
return error;
|
|
16
|
+
const { kind } = classifyClientError(error, { mutation: false });
|
|
17
|
+
if (kind === "unavailable" || kind === "denied" || kind === "invalid")
|
|
18
|
+
return new PresentationError(kind);
|
|
19
|
+
return new PresentationError(kind === "conflict" || kind === "not_found" ? "scope_mismatch" : "offline");
|
|
20
|
+
};
|
|
21
|
+
const isAbsent = (error) => classifyClientError(error, { mutation: false }).kind === "not_found";
|
|
22
|
+
/**
|
|
23
|
+
* One SDK read as an Effect: a fresh request identifier, a 5.5 s deadline
|
|
24
|
+
* that fails as `offline`, and the failure the request raised. Interrupting
|
|
25
|
+
* the Effect aborts the request's signal.
|
|
26
|
+
*/
|
|
27
|
+
export const attemptRead = (request) => Effect.flatMap(nextCompactId("records"), (messageId) => Effect.tryPromise({
|
|
28
|
+
catch: (error) => error,
|
|
29
|
+
try: (signal) => request({ messageId, signal }),
|
|
30
|
+
})).pipe(Effect.timeoutOrElse({
|
|
31
|
+
duration: "5500 millis",
|
|
32
|
+
orElse: () => Effect.fail(new PresentationError("offline")),
|
|
33
|
+
}));
|
|
34
|
+
const read = (request) => attemptRead(request).pipe(Effect.mapError(reasonOf));
|
|
35
|
+
const readOrAbsent = (request) => attemptRead(request).pipe(Effect.map(Option.some), Effect.catchIf(isAbsent, () => Effect.succeed(Option.none())), Effect.mapError(reasonOf));
|
|
36
|
+
/** The records reads over an SDK client; the platform supplies the Node instance. */
|
|
37
|
+
export const makeRecordsLayer = ({ client, instance }) => Layer.succeed(RecordsApi, {
|
|
38
|
+
instance,
|
|
39
|
+
listAgents: (input) => read((options) => client.listAgents({ ...input }, options)),
|
|
40
|
+
listInteractions: (input) => read((options) => client.listInteractions({ ...input }, options)),
|
|
41
|
+
listRoutines: (input) => read((options) => client.listRoutines({ ...input }, options)),
|
|
42
|
+
showAgent: (agentId) => readOrAbsent((options) => client.showAgent({ agentId }, options)),
|
|
43
|
+
showInteraction: (interactionId) => readOrAbsent((options) => client.showInteraction({ interactionId }, options)),
|
|
44
|
+
showRoutine: (routineId) => readOrAbsent((options) => client.showRoutine({ routineId }, options)),
|
|
45
|
+
});
|
|
46
|
+
//# sourceMappingURL=service.js.map
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { CollectionState, PresentationErrorReason } from "../presentation/index.js";
|
|
2
|
+
import type { ReloadReason, Snapshot } from "./held.js";
|
|
3
|
+
/** What a view can do for its renderer; `available` carries the records. */
|
|
4
|
+
export type RecordsCapability = "available" | "denied" | "offline" | "unavailable";
|
|
5
|
+
/**
|
|
6
|
+
* What a renderer shows. Until the first answer the capability is `loading`.
|
|
7
|
+
* A Node that lacks the list (`unavailable`), a transport that refuses it
|
|
8
|
+
* (`denied`) and a Node that does not answer (`offline`) carry no records, so
|
|
9
|
+
* a renderer cannot show stale or sample data in them.
|
|
10
|
+
*/
|
|
11
|
+
export type RecordsState<TRecord> = {
|
|
12
|
+
readonly capability: "available";
|
|
13
|
+
readonly generation: number;
|
|
14
|
+
/** In the order the Node listed them; a record that a change added comes last. */
|
|
15
|
+
readonly records: readonly TRecord[];
|
|
16
|
+
/** A read of the whole list runs, and `records` stay as held. */
|
|
17
|
+
readonly refreshing: boolean;
|
|
18
|
+
/** Set on the state a reload produced; a read of one record keeps it, the next read of the list replaces it. */
|
|
19
|
+
readonly reloaded?: true;
|
|
20
|
+
readonly reloadReason?: ReloadReason;
|
|
21
|
+
/** The Node holds more records than the cap, so `records` omits some. */
|
|
22
|
+
readonly truncated: boolean;
|
|
23
|
+
} | {
|
|
24
|
+
readonly capability: "loading";
|
|
25
|
+
readonly generation: number;
|
|
26
|
+
} | {
|
|
27
|
+
readonly capability: Exclude<RecordsCapability, "available">;
|
|
28
|
+
readonly generation: number;
|
|
29
|
+
/** Why the read failed, finer than the capability. */
|
|
30
|
+
readonly reason: PresentationErrorReason;
|
|
31
|
+
};
|
|
32
|
+
/** The renderer's state for one state of the view's collection. */
|
|
33
|
+
export declare const toState: <TRecord>({ generation, page, reason, status, }: CollectionState<Snapshot<TRecord>>) => RecordsState<TRecord>;
|
|
34
|
+
//# sourceMappingURL=state.d.ts.map
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** A refused, unsupported or unanswered read names the matching capability. */
|
|
2
|
+
const capabilityOf = (reason) => reason === "denied" || reason === "unavailable" ? reason : "offline";
|
|
3
|
+
/** The renderer's state for one state of the view's collection. */
|
|
4
|
+
export const toState = ({ generation, page, reason, status, }) => {
|
|
5
|
+
if (status === "error") {
|
|
6
|
+
const failure = reason ?? "offline";
|
|
7
|
+
return {
|
|
8
|
+
capability: capabilityOf(failure),
|
|
9
|
+
generation,
|
|
10
|
+
reason: failure,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
if (page === undefined)
|
|
14
|
+
return { capability: "loading", generation };
|
|
15
|
+
return {
|
|
16
|
+
capability: "available",
|
|
17
|
+
generation,
|
|
18
|
+
records: [...page.rows.values()].map((held) => held.record),
|
|
19
|
+
refreshing: status === "loading",
|
|
20
|
+
...(page.reload === undefined
|
|
21
|
+
? {}
|
|
22
|
+
: { reloaded: true, reloadReason: page.reload }),
|
|
23
|
+
truncated: page.truncated,
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=state.js.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** The record surfaces the records views serve. */
|
|
2
|
+
export type RecordsSurface = "agents" | "interactions" | "routines";
|
|
3
|
+
/** What a surface needs from the Node, to show where its data is unavailable. */
|
|
4
|
+
export type RuntimeRequest = {
|
|
5
|
+
/** The Node operations that serve the surface: its list, then its show. */
|
|
6
|
+
readonly operations: readonly [string, string];
|
|
7
|
+
/** The issue references the caller tracks the request under. */
|
|
8
|
+
readonly references: readonly string[];
|
|
9
|
+
readonly surface: RecordsSurface;
|
|
10
|
+
/** One sentence naming the operations and the references. */
|
|
11
|
+
readonly text: string;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Names the runtime request behind a surface whose view is `unavailable`. The
|
|
15
|
+
* caller passes the issue references it tracks the request under, so the core
|
|
16
|
+
* holds no issue numbers.
|
|
17
|
+
*/
|
|
18
|
+
export declare const runtimeRequest: (surface: RecordsSurface, references?: readonly string[]) => RuntimeRequest;
|
|
19
|
+
//# sourceMappingURL=surface.d.ts.map
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const OPERATIONS = {
|
|
2
|
+
agents: ["agents.list", "agents.show"],
|
|
3
|
+
interactions: ["interaction.list", "interaction.show"],
|
|
4
|
+
routines: ["routine.list", "routine.show"],
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Names the runtime request behind a surface whose view is `unavailable`. The
|
|
8
|
+
* caller passes the issue references it tracks the request under, so the core
|
|
9
|
+
* holds no issue numbers.
|
|
10
|
+
*/
|
|
11
|
+
export const runtimeRequest = (surface, references = []) => {
|
|
12
|
+
const operations = OPERATIONS[surface];
|
|
13
|
+
const needs = `The Node must serve ${operations[0]} and ${operations[1]}.`;
|
|
14
|
+
return {
|
|
15
|
+
operations,
|
|
16
|
+
references,
|
|
17
|
+
surface,
|
|
18
|
+
text: references.length === 0
|
|
19
|
+
? needs
|
|
20
|
+
: `${needs} Tracked in ${references.join(", ")}.`,
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=surface.js.map
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Effect, Option } from "effect";
|
|
2
|
+
import { NodeChanges, type PresentationError } from "../presentation/index.js";
|
|
3
|
+
import { type ReloadReason, type Snapshot } from "./held.js";
|
|
4
|
+
/** What the change feed needs of one records view. */
|
|
5
|
+
export type Follow<TRecord> = {
|
|
6
|
+
/** The name the change feed gives records of the view's type. */
|
|
7
|
+
readonly aggregateType: string;
|
|
8
|
+
readonly instance: Effect.Effect<string, PresentationError>;
|
|
9
|
+
/** The snapshot the view holds, once a list read in flight settles. */
|
|
10
|
+
readonly known: Effect.Effect<Option.Option<Snapshot<TRecord>>>;
|
|
11
|
+
/** Reads the whole list again and waits for the answer; `reason` names why. */
|
|
12
|
+
readonly reload: (reason?: ReloadReason) => Effect.Effect<void>;
|
|
13
|
+
/** Reads these records by id; yields `false` when any read failed. */
|
|
14
|
+
readonly reread: (ids: readonly string[]) => Effect.Effect<boolean>;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Keeps a records view current from the Node's committed changes until
|
|
18
|
+
* interruption. Bursts coalesce into one batch. The follow retries a lost
|
|
19
|
+
* subscription with backoff and resubscribes after two seconds when the stream
|
|
20
|
+
* ends; each subscription reads the Node instance again, so the follow tracks
|
|
21
|
+
* a restarted Node.
|
|
22
|
+
*/
|
|
23
|
+
export declare const followChanges: <TRecord>(follow: Follow<TRecord>) => Effect.Effect<void, never, NodeChanges>;
|
|
24
|
+
//# sourceMappingURL=watch.d.ts.map
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Duration, Effect, Option, Schedule, Stream } from "effect";
|
|
2
|
+
import { NodeChanges } from "../presentation/index.js";
|
|
3
|
+
import { holdsRevision } from "./held.js";
|
|
4
|
+
const COALESCE_WITHIN = Duration.millis(150);
|
|
5
|
+
const MAX_TARGETED_READS = 64;
|
|
6
|
+
const RESUBSCRIBE_AFTER = Duration.seconds(2);
|
|
7
|
+
/** A failed subscription retries after 2 s, doubling to 30 s, as the cockpit's feed does. */
|
|
8
|
+
const RECONNECT_BACKOFF = Schedule.min([
|
|
9
|
+
Schedule.exponential(Duration.seconds(2)),
|
|
10
|
+
Schedule.spaced(Duration.seconds(30)),
|
|
11
|
+
]);
|
|
12
|
+
const touches = (type, batch) => batch.some((change) => change.kind === "records" && change.aggregateTypes.includes(type));
|
|
13
|
+
/** A resync, or a change of this type that names no record, says to read the list again. */
|
|
14
|
+
const needsReload = (type, batch) => batch.some((change) => change.kind === "resync" ||
|
|
15
|
+
(change.changed === undefined && change.aggregateTypes.includes(type)));
|
|
16
|
+
/** The records of this type that a batch changed past the revisions the snapshot holds. */
|
|
17
|
+
const changedIds = (type, snapshot, batch) => [
|
|
18
|
+
...new Set(batch.flatMap((change) => change.kind === "records"
|
|
19
|
+
? (change.changed ?? [])
|
|
20
|
+
.filter((aggregate) => aggregate.type === type &&
|
|
21
|
+
!holdsRevision(snapshot, aggregate.id, aggregate.revision))
|
|
22
|
+
.map((aggregate) => aggregate.id)
|
|
23
|
+
: [])),
|
|
24
|
+
];
|
|
25
|
+
/**
|
|
26
|
+
* One coalesced batch. A resync, or a change that names no record, reloads the
|
|
27
|
+
* list. Otherwise the view reads each changed record by id. A batch that names
|
|
28
|
+
* more records than worth reading one by one, or a read that failed, reloads
|
|
29
|
+
* the list, which either converges or says the Node cannot serve it. A view
|
|
30
|
+
* that holds nothing reloads on the first change of its type.
|
|
31
|
+
*/
|
|
32
|
+
const applyBatch = (follow, batch) => Effect.gen(function* () {
|
|
33
|
+
const { aggregateType } = follow;
|
|
34
|
+
if (needsReload(aggregateType, batch))
|
|
35
|
+
return yield* follow.reload("resync");
|
|
36
|
+
const known = yield* follow.known;
|
|
37
|
+
if (Option.isNone(known))
|
|
38
|
+
return yield* touches(aggregateType, batch)
|
|
39
|
+
? follow.reload("resync")
|
|
40
|
+
: Effect.void;
|
|
41
|
+
const ids = changedIds(aggregateType, known.value, batch);
|
|
42
|
+
if (ids.length > MAX_TARGETED_READS)
|
|
43
|
+
return yield* follow.reload("resync");
|
|
44
|
+
if (ids.length > 0 && !(yield* follow.reread(ids)))
|
|
45
|
+
yield* follow.reload("resync");
|
|
46
|
+
});
|
|
47
|
+
/**
|
|
48
|
+
* One subscription to the Node instance now answering. A view that holds
|
|
49
|
+
* nothing, or holds another instance's snapshot, reads its list first. A
|
|
50
|
+
* subscription that fails reads the list again before the next attempt.
|
|
51
|
+
*/
|
|
52
|
+
const subscription = (follow) => Effect.gen(function* () {
|
|
53
|
+
const changes = yield* NodeChanges;
|
|
54
|
+
const instanceId = yield* follow.instance;
|
|
55
|
+
const known = yield* follow.known;
|
|
56
|
+
if (Option.isNone(known) || known.value.instanceId !== instanceId)
|
|
57
|
+
yield* follow.reload();
|
|
58
|
+
yield* changes.watch(instanceId).pipe(Stream.groupedWithin(512, COALESCE_WITHIN), Stream.runForEach((batch) => applyBatch(follow, batch)));
|
|
59
|
+
}).pipe(Effect.tapError(() => follow.reload("subscription-lost")));
|
|
60
|
+
/**
|
|
61
|
+
* Keeps a records view current from the Node's committed changes until
|
|
62
|
+
* interruption. Bursts coalesce into one batch. The follow retries a lost
|
|
63
|
+
* subscription with backoff and resubscribes after two seconds when the stream
|
|
64
|
+
* ends; each subscription reads the Node instance again, so the follow tracks
|
|
65
|
+
* a restarted Node.
|
|
66
|
+
*/
|
|
67
|
+
export const followChanges = (follow) => subscription(follow).pipe(Effect.retry(RECONNECT_BACKOFF), Effect.repeat(Schedule.spaced(RESUBSCRIBE_AFTER)), Effect.ignore);
|
|
68
|
+
//# sourceMappingURL=watch.js.map
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { executorLabel } from "../cockpit/index.js";
|
|
1
2
|
const PENDING = new Set(["dispatching", "queued"]);
|
|
2
3
|
const fromTimeline = (turn) => ({
|
|
3
4
|
...(turn.failureReason === undefined
|
|
@@ -67,10 +68,6 @@ export const durationLabel = (milliseconds) => {
|
|
|
67
68
|
};
|
|
68
69
|
/** A token count as `950` or `4.2k`. */
|
|
69
70
|
export const tokenLabel = (tokens) => tokens < 1000 ? String(tokens) : `${(tokens / 1000).toFixed(1)}k`;
|
|
70
|
-
const EXECUTOR_LABELS = new Map([
|
|
71
|
-
["claude-code", "Claude Code"],
|
|
72
|
-
["codex-cli", "Codex"],
|
|
73
|
-
]);
|
|
74
71
|
/** The first line of the first prompt, as a title; at most 80 characters. */
|
|
75
72
|
const titleOf = (conversation) => {
|
|
76
73
|
const first = conversation
|
|
@@ -107,7 +104,7 @@ export const projectSessionSummary = (input) => {
|
|
|
107
104
|
? {}
|
|
108
105
|
: {
|
|
109
106
|
connection: connectionName ?? executor.connectionId,
|
|
110
|
-
executor:
|
|
107
|
+
executor: executorLabel(executor.kind),
|
|
111
108
|
}),
|
|
112
109
|
...(elapsed === undefined ? {} : { elapsed }),
|
|
113
110
|
...(executor?.model === undefined ? {} : { model: executor.model }),
|
|
@@ -43,6 +43,8 @@ export type SessionViewState = {
|
|
|
43
43
|
readonly observedAt?: number;
|
|
44
44
|
/** An interrupt prepared for confirmation, bound to one turn revision. */
|
|
45
45
|
readonly pendingInterrupt?: TurnInterruptParameters;
|
|
46
|
+
/** The oldest cursor the Node still holds, when it pruned earlier history. */
|
|
47
|
+
readonly prunedBefore?: string;
|
|
46
48
|
readonly session?: Session;
|
|
47
49
|
readonly source: TimelineSource["kind"];
|
|
48
50
|
readonly turns: readonly TimelineTurn[];
|
|
@@ -38,14 +38,17 @@ const readDiff = (context, session) => session.worktree === undefined
|
|
|
38
38
|
}),
|
|
39
39
|
onSuccess: (diff) => ({ diffError: _error, ...state }) => ({ ...state, diff }),
|
|
40
40
|
}), Effect.flatMap((change) => SubscriptionRef.update(context.ref, change)));
|
|
41
|
+
/** A fresh read replaces the turns and the pruned marker, and clears an error. */
|
|
42
|
+
const withTimeline = (session, { prunedBefore, turns }) => ({ error: _error, prunedBefore: _pruned, ...state }) => ({
|
|
43
|
+
...state,
|
|
44
|
+
...(prunedBefore === undefined ? {} : { prunedBefore }),
|
|
45
|
+
session,
|
|
46
|
+
turns,
|
|
47
|
+
});
|
|
41
48
|
const refresh = (context) => Effect.gen(function* () {
|
|
42
49
|
const shown = yield* read("session.show", (options) => context.client.showSession({ sessionId: context.sessionId }, options));
|
|
43
|
-
const
|
|
44
|
-
yield* SubscriptionRef.update(context.ref, (
|
|
45
|
-
...state,
|
|
46
|
-
session: shown.session,
|
|
47
|
-
turns,
|
|
48
|
-
}));
|
|
50
|
+
const loaded = yield* context.timeline.load(context.sessionId);
|
|
51
|
+
yield* SubscriptionRef.update(context.ref, withTimeline(shown.session, loaded));
|
|
49
52
|
yield* observed(context);
|
|
50
53
|
yield* Effect.all([
|
|
51
54
|
readDiff(context, shown.session),
|
|
@@ -20,7 +20,16 @@ export type TimelineTurn = {
|
|
|
20
20
|
*/
|
|
21
21
|
export type TimelineSource = {
|
|
22
22
|
readonly kind: "final-text";
|
|
23
|
-
readonly load: (sessionId: string) => Effect.Effect<
|
|
23
|
+
readonly load: (sessionId: string) => Effect.Effect<TimelineLoad, CockpitError>;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* The recent turns and, when the Node pruned older events, the oldest cursor
|
|
27
|
+
* it still holds. The detail has no older-page load, so the read from cursor 0
|
|
28
|
+
* is the one place a hole can show.
|
|
29
|
+
*/
|
|
30
|
+
export type TimelineLoad = {
|
|
31
|
+
readonly prunedBefore?: string;
|
|
32
|
+
readonly turns: readonly TimelineTurn[];
|
|
24
33
|
};
|
|
25
34
|
/** The most recent turns a detail shows; older ones stay on the Node. */
|
|
26
35
|
export declare const MAX_TIMELINE_TURNS = 20;
|
|
@@ -14,6 +14,9 @@ const eventPage = (client, sessionId, after) => read("session.events.list", (opt
|
|
|
14
14
|
...(result.nextSessionCursor === undefined
|
|
15
15
|
? {}
|
|
16
16
|
: { next: result.nextSessionCursor }),
|
|
17
|
+
...(result.oldestRetainedCursor === undefined
|
|
18
|
+
? {}
|
|
19
|
+
: { oldest: result.oldestRetainedCursor }),
|
|
17
20
|
})));
|
|
18
21
|
/** Reads the last `EVENT_WINDOW` events, never the whole history. */
|
|
19
22
|
const recentEvents = (client, sessionId) => Effect.gen(function* () {
|
|
@@ -22,9 +25,11 @@ const recentEvents = (client, sessionId) => Effect.gen(function* () {
|
|
|
22
25
|
const collect = (after, seen) => Effect.flatMap(eventPage(client, sessionId, after), (page) => page.next === undefined || page.items.length === 0
|
|
23
26
|
? Effect.succeed([...seen, ...page.items])
|
|
24
27
|
: collect(page.next, [...seen, ...page.items]));
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
+
const events = from <= 0n ? yield* collect("0", []) : yield* collect(String(from), []);
|
|
29
|
+
return {
|
|
30
|
+
events,
|
|
31
|
+
...(probe.oldest === undefined ? {} : { oldest: probe.oldest }),
|
|
32
|
+
};
|
|
28
33
|
});
|
|
29
34
|
const toTimeline = (turn) => ({
|
|
30
35
|
...("failureReason" in turn ? { failureReason: turn.failureReason } : {}),
|
|
@@ -38,6 +43,9 @@ const toTimeline = (turn) => ({
|
|
|
38
43
|
/** The final-text source over `session.events.list` and `turn.show`. */
|
|
39
44
|
export const finalTextTimeline = (client) => ({
|
|
40
45
|
kind: "final-text",
|
|
41
|
-
load: (sessionId) => recentEvents(client, sessionId).pipe(Effect.
|
|
46
|
+
load: (sessionId) => recentEvents(client, sessionId).pipe(Effect.flatMap(({ events, oldest }) => Effect.forEach([...new Set(events.flatMap((event) => turnIdOf(event) ?? []))].slice(-MAX_TIMELINE_TURNS), (turnId) => read("turn.show", (options) => client.showTurn({ turnId }, options)).pipe(Effect.map((result) => toTimeline(result.turn))), { concurrency: 4 }).pipe(Effect.map((turns) => ({
|
|
47
|
+
...(oldest === undefined ? {} : { prunedBefore: oldest }),
|
|
48
|
+
turns,
|
|
49
|
+
}))))),
|
|
42
50
|
});
|
|
43
51
|
//# sourceMappingURL=timeline.js.map
|
package/package.json
CHANGED
|
@@ -1,7 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@maiaos/client-core",
|
|
3
|
-
"version": "0.1.0-canary.
|
|
3
|
+
"version": "0.1.0-canary.71",
|
|
4
4
|
"description": "Platform-neutral client use cases for Maia OS: Effect services between a client's renderer and the SDK, so every client reads Maia Node state and sends requests the same way.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"maia",
|
|
7
|
+
"maiaos",
|
|
8
|
+
"ai-agents",
|
|
9
|
+
"coding-agents",
|
|
10
|
+
"multi-agent",
|
|
11
|
+
"agent-orchestration",
|
|
12
|
+
"client",
|
|
13
|
+
"effect",
|
|
14
|
+
"typescript"
|
|
15
|
+
],
|
|
5
16
|
"homepage": "https://github.com/vllnt/maia/tree/main/packages/client-core#readme",
|
|
6
17
|
"bugs": "https://github.com/vllnt/maia/issues",
|
|
7
18
|
"repository": {
|
|
@@ -9,6 +20,7 @@
|
|
|
9
20
|
"url": "git+https://github.com/vllnt/maia.git",
|
|
10
21
|
"directory": "packages/client-core"
|
|
11
22
|
},
|
|
23
|
+
"license": "MIT",
|
|
12
24
|
"type": "module",
|
|
13
25
|
"sideEffects": false,
|
|
14
26
|
"exports": {
|
|
@@ -32,6 +44,10 @@
|
|
|
32
44
|
"types": "./dist/dictation/index.d.ts",
|
|
33
45
|
"import": "./dist/dictation/index.js"
|
|
34
46
|
},
|
|
47
|
+
"./id-generator": {
|
|
48
|
+
"types": "./dist/id-generator/index.d.ts",
|
|
49
|
+
"import": "./dist/id-generator/index.js"
|
|
50
|
+
},
|
|
35
51
|
"./interaction-view": {
|
|
36
52
|
"types": "./dist/interaction/view.d.ts",
|
|
37
53
|
"import": "./dist/interaction/view.js"
|
|
@@ -52,6 +68,10 @@
|
|
|
52
68
|
"types": "./dist/presentation/index.d.ts",
|
|
53
69
|
"import": "./dist/presentation/index.js"
|
|
54
70
|
},
|
|
71
|
+
"./records": {
|
|
72
|
+
"types": "./dist/records/index.d.ts",
|
|
73
|
+
"import": "./dist/records/index.js"
|
|
74
|
+
},
|
|
55
75
|
"./node-status": {
|
|
56
76
|
"types": "./dist/node-status/index.d.ts",
|
|
57
77
|
"import": "./dist/node-status/index.js"
|
|
@@ -94,13 +114,13 @@
|
|
|
94
114
|
"node": "24.22.1"
|
|
95
115
|
},
|
|
96
116
|
"dependencies": {
|
|
97
|
-
"@maiaos/sdk": "0.1.0-canary.
|
|
117
|
+
"@maiaos/sdk": "0.1.0-canary.71",
|
|
98
118
|
"effect": "4.0.0"
|
|
99
119
|
},
|
|
100
120
|
"devDependencies": {
|
|
101
121
|
"@types/node": "24.13.3",
|
|
102
122
|
"@typescript/native": "npm:typescript@7.0.2",
|
|
103
|
-
"@vllnt/typescript": "
|
|
123
|
+
"@vllnt/typescript": "3.0.1",
|
|
104
124
|
"eslint": "9.39.5",
|
|
105
125
|
"typescript": "npm:@typescript/typescript6@6.0.2"
|
|
106
126
|
},
|