@unieai/rabi-api-session-controller 0.0.0-stage → 0.2.2-rc.5
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 +7 -2
- package/lib/client.js +4 -0
- package/lib/index.js +4300 -0
- package/lib/typert.host.d.ts +3 -0
- package/lib/typert.host.js +5013 -0
- package/lib/typert.remote-client.d.ts +115 -0
- package/lib/typert.remote-client.js +2714 -0
- package/lib/types/agent.d.ts +164 -0
- package/lib/types/agent.js +484 -0
- package/lib/types/archived-session-gate.d.ts +33 -0
- package/lib/types/archived-session-gate.js +54 -0
- package/lib/types/assistant-stream.d.ts +26 -0
- package/lib/types/assistant-stream.js +82 -0
- package/lib/types/catalog.d.ts +24 -0
- package/lib/types/catalog.js +107 -0
- package/lib/types/client/contract/events.d.ts +102 -0
- package/lib/types/client/contract/events.js +111 -0
- package/lib/types/client/contract/session.d.ts +178 -0
- package/lib/types/client/contract/session.js +1 -0
- package/lib/types/client/contract/sessions.d.ts +162 -0
- package/lib/types/client/contract/sessions.js +1 -0
- package/lib/types/client/contract/snapshot.d.ts +84 -0
- package/lib/types/client/contract/snapshot.js +1 -0
- package/lib/types/client/index.d.ts +39 -0
- package/lib/types/client/index.js +60 -0
- package/lib/types/client/ordered-baseline.d.ts +12 -0
- package/lib/types/client/ordered-baseline.js +40 -0
- package/lib/types/client/scope.d.ts +46 -0
- package/lib/types/client/scope.js +46 -0
- package/lib/types/client/session-wire-event.d.ts +11 -0
- package/lib/types/client/session-wire-event.js +42 -0
- package/lib/types/client/sessions/assistant-stream.d.ts +56 -0
- package/lib/types/client/sessions/assistant-stream.js +182 -0
- package/lib/types/client/sessions/history-records.d.ts +22 -0
- package/lib/types/client/sessions/history-records.js +25 -0
- package/lib/types/client/sessions/lineage.d.ts +48 -0
- package/lib/types/client/sessions/lineage.js +55 -0
- package/lib/types/client/sessions/manager.d.ts +272 -0
- package/lib/types/client/sessions/manager.js +774 -0
- package/lib/types/client/sessions/notifier.d.ts +39 -0
- package/lib/types/client/sessions/notifier.js +97 -0
- package/lib/types/client/sessions/projection-store.d.ts +128 -0
- package/lib/types/client/sessions/projection-store.js +161 -0
- package/lib/types/client/sessions/remotes.d.ts +29 -0
- package/lib/types/client/sessions/remotes.js +7 -0
- package/lib/types/client/sessions/service.d.ts +263 -0
- package/lib/types/client/sessions/service.js +597 -0
- package/lib/types/client/sessions/session.d.ts +303 -0
- package/lib/types/client/sessions/session.js +991 -0
- package/lib/types/client/time-zone.d.ts +8 -0
- package/lib/types/client/time-zone.js +13 -0
- package/lib/types/client/transport.d.ts +77 -0
- package/lib/types/client/transport.js +130 -0
- package/lib/types/commands.d.ts +92 -0
- package/lib/types/commands.js +575 -0
- package/lib/types/continuation.d.ts +35 -0
- package/lib/types/continuation.js +47 -0
- package/lib/types/control.d.ts +20 -0
- package/lib/types/control.js +106 -0
- package/lib/types/file-references.d.ts +27 -0
- package/lib/types/file-references.js +68 -0
- package/lib/types/history/assistant-stream.d.ts +43 -0
- package/lib/types/history/assistant-stream.js +79 -0
- package/lib/types/history/controller.d.ts +104 -0
- package/lib/types/history/controller.js +293 -0
- package/lib/types/history/listener.d.ts +105 -0
- package/lib/types/history/listener.js +296 -0
- package/lib/types/history.d.ts +58 -0
- package/lib/types/history.js +152 -0
- package/lib/types/index.d.ts +262 -0
- package/lib/types/index.js +693 -0
- package/lib/types/list.d.ts +62 -0
- package/lib/types/list.js +343 -0
- package/lib/types/media-references.d.ts +18 -0
- package/lib/types/media-references.js +84 -0
- package/lib/types/remote-events.d.ts +8 -0
- package/lib/types/remote-events.js +1 -0
- package/lib/types/skill-catalog.d.ts +28 -0
- package/lib/types/skill-catalog.js +203 -0
- package/lib/types/state/controller.d.ts +45 -0
- package/lib/types/state/controller.js +288 -0
- package/lib/types/state/listener.d.ts +40 -0
- package/lib/types/state/listener.js +111 -0
- package/lib/types/types.d.ts +687 -0
- package/lib/types/types.js +5 -0
- package/lib/types/web/controller.d.ts +25 -0
- package/lib/types/web/controller.js +138 -0
- package/locale/de.json +6 -0
- package/locale/en.json +5 -0
- package/locale/es.json +6 -0
- package/locale/fr.json +6 -0
- package/locale/ja.json +6 -0
- package/locale/pt.json +6 -0
- package/locale/ru.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +119 -4
- package/src/agent.ts +474 -0
- package/src/archived-session-gate.ts +59 -0
- package/src/assistant-stream.ts +102 -0
- package/src/catalog.ts +117 -0
- package/src/client/contract/events.ts +205 -0
- package/src/client/contract/session.ts +174 -0
- package/src/client/contract/sessions.ts +169 -0
- package/src/client/contract/snapshot.ts +94 -0
- package/src/client/index.ts +149 -0
- package/src/client/ordered-baseline.ts +43 -0
- package/src/client/scope.ts +76 -0
- package/src/client/session-wire-event.ts +46 -0
- package/src/client/sessions/assistant-stream.ts +229 -0
- package/src/client/sessions/history-records.ts +35 -0
- package/src/client/sessions/lineage.ts +96 -0
- package/src/client/sessions/manager.ts +857 -0
- package/src/client/sessions/notifier.ts +97 -0
- package/src/client/sessions/projection-store.ts +244 -0
- package/src/client/sessions/remotes.ts +44 -0
- package/src/client/sessions/service.ts +741 -0
- package/src/client/sessions/session.ts +1055 -0
- package/src/client/time-zone.ts +14 -0
- package/src/client/transport.ts +247 -0
- package/src/commands.ts +602 -0
- package/src/continuation.ts +58 -0
- package/src/control.ts +121 -0
- package/src/file-references.ts +42 -0
- package/src/history/assistant-stream.ts +102 -0
- package/src/history/controller.ts +256 -0
- package/src/history/listener.ts +302 -0
- package/src/history.ts +189 -0
- package/src/index.ts +651 -0
- package/src/list.ts +382 -0
- package/src/media-references.ts +84 -0
- package/src/remote-events.ts +14 -0
- package/src/skill-catalog.ts +108 -0
- package/src/state/controller.ts +221 -0
- package/src/state/listener.ts +116 -0
- package/src/types.ts +720 -0
- package/src/web/controller.ts +78 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The controller's admission gate for archived Sessions: an archived
|
|
3
|
+
* Session, or a subagent descendant of one, must not run a model step until
|
|
4
|
+
* it is restored. The work a Session still runs is reported and stopped by
|
|
5
|
+
* its owners — the Agent registry (`turn`), the job registry seam (`job`),
|
|
6
|
+
* the Subagent runtime (`subagent`), and the Schedule plugin (`schedule`) —
|
|
7
|
+
* through the Workspace registry's archive-admission events. The Subagent
|
|
8
|
+
* runtime also checks its retained ancestry before each step, so a released
|
|
9
|
+
* intermediate Session cannot hide an archived ancestor.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* The gate as a plugin for `ctx.plugin(...)`: it loads once the Agent
|
|
13
|
+
* registry, Session store, and Workspace registry are available and unwinds
|
|
14
|
+
* with the plugin's fiber. A late waking delivery to an archived Session — a
|
|
15
|
+
* subagent settlement, a queued follow-up — proposes a step the gate rejects,
|
|
16
|
+
* which the loop ends as `blocked` without a request; unarchiving lifts the
|
|
17
|
+
* gate for the whole lineage.
|
|
18
|
+
*/
|
|
19
|
+
export const ArchivedSessionGate = {
|
|
20
|
+
name: 'archived-session-gate',
|
|
21
|
+
inject: ['agents', 'sessions', 'workspaceRegistry'],
|
|
22
|
+
apply(ctx) {
|
|
23
|
+
ctx.on('agent/pre-step', (payload, next) => underArchivedSession(ctx, payload.agent)
|
|
24
|
+
? Promise.resolve({ kind: 'reject' })
|
|
25
|
+
: next());
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Whether the Agent's Session, or a Session above it in its subagent lineage,
|
|
30
|
+
* is archived. This header-only check follows the durable fields through
|
|
31
|
+
* subagent-origin Sessions only: a fork of an archived Session is an
|
|
32
|
+
* independent conversation. The Subagent runtime separately checks managed
|
|
33
|
+
* ancestry when an intermediate Session is unavailable here.
|
|
34
|
+
* @param ctx - Host context.
|
|
35
|
+
* @param agent - the Agent proposing a step.
|
|
36
|
+
* @returns whether an archived Session owns the step.
|
|
37
|
+
*/
|
|
38
|
+
export function underArchivedSession(ctx, agent) {
|
|
39
|
+
const archived = ctx.workspaceRegistry.archivedSessionIds;
|
|
40
|
+
let header = agent.session.header;
|
|
41
|
+
const visited = new Set();
|
|
42
|
+
while (!visited.has(header.id)) {
|
|
43
|
+
if (archived.includes(header.id))
|
|
44
|
+
return true;
|
|
45
|
+
visited.add(header.id);
|
|
46
|
+
if (header.origin !== 'subagent' || header.parentSession === undefined)
|
|
47
|
+
return false;
|
|
48
|
+
const parent = ctx.sessions.get(header.parentSession);
|
|
49
|
+
if (parent === undefined)
|
|
50
|
+
return archived.includes(header.parentSession);
|
|
51
|
+
header = parent.header;
|
|
52
|
+
}
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** Process-local assistant state retained for reconnecting Web followers. */
|
|
2
|
+
import type { AssistantStreamFrame } from '@unieai/rabi-agent';
|
|
3
|
+
import type { SessionSeqCursor } from '@unieai/rabi-session';
|
|
4
|
+
import type { SessionAssistantStreamBaseline } from './types.ts';
|
|
5
|
+
/**
|
|
6
|
+
* Folds dense Agent frames and materializes one shared immutable reconnect
|
|
7
|
+
* baseline per accepted revision.
|
|
8
|
+
*/
|
|
9
|
+
export declare class SessionAssistantStreamAccumulator {
|
|
10
|
+
private activeAttempt;
|
|
11
|
+
private revision;
|
|
12
|
+
private snapshotValue;
|
|
13
|
+
private dirty;
|
|
14
|
+
/**
|
|
15
|
+
* Fold one trusted frame from the current attached Agent lifecycle.
|
|
16
|
+
* @param frame - next dense process-local Assistant frame.
|
|
17
|
+
* @param durableCursor - last committed Session seq when this frame was observed.
|
|
18
|
+
*/
|
|
19
|
+
accept(frame: AssistantStreamFrame, durableCursor: SessionSeqCursor): void;
|
|
20
|
+
/**
|
|
21
|
+
* Read the cached reconnect baseline, materializing it after a state change.
|
|
22
|
+
* @returns the identity-stable baseline for the latest accepted revision.
|
|
23
|
+
*/
|
|
24
|
+
snapshot(): SessionAssistantStreamBaseline;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=assistant-stream.d.ts.map
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/** Process-local assistant state retained for reconnecting Web followers. */
|
|
2
|
+
import { AssistantStreamAccumulator } from '@unieai/rabi-llm';
|
|
3
|
+
const EMPTY_BASELINE = { revision: 0 };
|
|
4
|
+
/**
|
|
5
|
+
* Folds dense Agent frames and materializes one shared immutable reconnect
|
|
6
|
+
* baseline per accepted revision.
|
|
7
|
+
*/
|
|
8
|
+
export class SessionAssistantStreamAccumulator {
|
|
9
|
+
activeAttempt;
|
|
10
|
+
revision = 0;
|
|
11
|
+
snapshotValue = EMPTY_BASELINE;
|
|
12
|
+
dirty = false;
|
|
13
|
+
/**
|
|
14
|
+
* Fold one trusted frame from the current attached Agent lifecycle.
|
|
15
|
+
* @param frame - next dense process-local Assistant frame.
|
|
16
|
+
* @param durableCursor - last committed Session seq when this frame was observed.
|
|
17
|
+
*/
|
|
18
|
+
accept(frame, durableCursor) {
|
|
19
|
+
if (frame.type === 'start' && frame.revision === 1 && this.revision !== 0) {
|
|
20
|
+
this.activeAttempt = undefined;
|
|
21
|
+
this.revision = 0;
|
|
22
|
+
}
|
|
23
|
+
if (frame.revision !== this.revision + 1) {
|
|
24
|
+
this.activeAttempt = undefined;
|
|
25
|
+
this.revision = frame.revision;
|
|
26
|
+
this.dirty = true;
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
this.revision = frame.revision;
|
|
30
|
+
switch (frame.type) {
|
|
31
|
+
case 'start':
|
|
32
|
+
this.activeAttempt = {
|
|
33
|
+
attemptId: frame.attemptId,
|
|
34
|
+
startedAfterSeq: durableCursor,
|
|
35
|
+
turn: frame.turn,
|
|
36
|
+
step: frame.step,
|
|
37
|
+
stream: new AssistantStreamAccumulator(),
|
|
38
|
+
nextIndex: 0,
|
|
39
|
+
};
|
|
40
|
+
break;
|
|
41
|
+
case 'chunk': {
|
|
42
|
+
const attempt = this.activeAttempt;
|
|
43
|
+
if (attempt === undefined
|
|
44
|
+
|| attempt.attemptId !== frame.attemptId
|
|
45
|
+
|| frame.index !== attempt.nextIndex) {
|
|
46
|
+
this.activeAttempt = undefined;
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
attempt.stream.push({ time: frame.time, chunk: frame.chunk });
|
|
50
|
+
attempt.nextIndex += 1;
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
case 'end':
|
|
54
|
+
this.activeAttempt = undefined;
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
this.dirty = true;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Read the cached reconnect baseline, materializing it after a state change.
|
|
61
|
+
* @returns the identity-stable baseline for the latest accepted revision.
|
|
62
|
+
*/
|
|
63
|
+
snapshot() {
|
|
64
|
+
if (!this.dirty)
|
|
65
|
+
return this.snapshotValue;
|
|
66
|
+
this.snapshotValue = {
|
|
67
|
+
revision: this.revision,
|
|
68
|
+
...this.activeAttempt === undefined ? {} : {
|
|
69
|
+
activeAttempt: {
|
|
70
|
+
attemptId: this.activeAttempt.attemptId,
|
|
71
|
+
startedAfterSeq: this.activeAttempt.startedAfterSeq,
|
|
72
|
+
turn: this.activeAttempt.turn,
|
|
73
|
+
step: this.activeAttempt.step,
|
|
74
|
+
nextIndex: this.activeAttempt.nextIndex,
|
|
75
|
+
stream: this.activeAttempt.stream.snapshot(),
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
this.dirty = false;
|
|
80
|
+
return this.snapshotValue;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Shared projection of the live LLM registry into the browser model catalog. */
|
|
2
|
+
import type { Context } from '@unieai/cordis';
|
|
3
|
+
import type { ModelCatalog, ModelSelection } from './types.ts';
|
|
4
|
+
/**
|
|
5
|
+
* Build the browser model catalog without requiring a Session.
|
|
6
|
+
* @param ctx - Host context carrying the live LLM registry.
|
|
7
|
+
* @param defaultSelection - deployment default used before a Session selects a model.
|
|
8
|
+
* @returns successful non-empty provider groups and isolated provider failures.
|
|
9
|
+
*/
|
|
10
|
+
export declare function buildModelCatalog(ctx: Context, defaultSelection?: ModelSelection): Promise<ModelCatalog>;
|
|
11
|
+
/**
|
|
12
|
+
* Check a GUI selection against the current available provider catalog.
|
|
13
|
+
* @param ctx - Host LLM registry.
|
|
14
|
+
* @param selection - stored or explicitly requested selection.
|
|
15
|
+
* @returns whether the exact model is currently advertised as available.
|
|
16
|
+
*/
|
|
17
|
+
export declare function modelAvailable(ctx: Context, selection: ModelSelection): Promise<boolean>;
|
|
18
|
+
/**
|
|
19
|
+
* Check configured provider API-key references independently of model availability.
|
|
20
|
+
* @param ctx - Host registry, settings, and credential services.
|
|
21
|
+
* @returns whether any API-key provider has a configured credential.
|
|
22
|
+
*/
|
|
23
|
+
export declare function hasProviderApiKey(ctx: Context): Promise<boolean>;
|
|
24
|
+
//# sourceMappingURL=catalog.d.ts.map
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/** Shared projection of the live LLM registry into the browser model catalog. */
|
|
2
|
+
import { RemoteError } from '@unieai/rabi-typert-protocol';
|
|
3
|
+
import { credentialRef } from '@unieai/rabi-credentials';
|
|
4
|
+
/**
|
|
5
|
+
* Build the browser model catalog without requiring a Session.
|
|
6
|
+
* @param ctx - Host context carrying the live LLM registry.
|
|
7
|
+
* @param defaultSelection - deployment default used before a Session selects a model.
|
|
8
|
+
* @returns successful non-empty provider groups and isolated provider failures.
|
|
9
|
+
*/
|
|
10
|
+
export async function buildModelCatalog(ctx, defaultSelection = ctx.agentDefaultModel.currentSelection()) {
|
|
11
|
+
const providers = ctx.llm.listProviders();
|
|
12
|
+
const catalog = await Promise.all(providers.map(async (provider) => {
|
|
13
|
+
try {
|
|
14
|
+
const models = await ctx.llm.listModels(provider.id);
|
|
15
|
+
const entries = await Promise.all(models.map(async (model) => {
|
|
16
|
+
const resolved = await ctx.llm.resolveModelInfo(provider.id, model.id);
|
|
17
|
+
const reasoning = resolved.reasoning === undefined
|
|
18
|
+
? undefined
|
|
19
|
+
: {
|
|
20
|
+
efforts: resolved.reasoning.efforts.map(effort => ({
|
|
21
|
+
id: effort.id,
|
|
22
|
+
name: effort.name,
|
|
23
|
+
...(effort.description === undefined ? {} : { description: effort.description }),
|
|
24
|
+
})),
|
|
25
|
+
...(resolved.reasoning.defaultEffort === undefined
|
|
26
|
+
? {}
|
|
27
|
+
: { defaultEffort: resolved.reasoning.defaultEffort }),
|
|
28
|
+
};
|
|
29
|
+
return {
|
|
30
|
+
id: model.id,
|
|
31
|
+
name: model.name,
|
|
32
|
+
...(model.description === undefined ? {} : { description: model.description }),
|
|
33
|
+
...(reasoning === undefined ? {} : { reasoning }),
|
|
34
|
+
...(model.badge === undefined ? {} : { badge: model.badge }),
|
|
35
|
+
};
|
|
36
|
+
}));
|
|
37
|
+
return {
|
|
38
|
+
kind: 'group',
|
|
39
|
+
group: { id: provider.id, name: provider.name, models: entries },
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
return {
|
|
44
|
+
kind: 'failure',
|
|
45
|
+
failure: {
|
|
46
|
+
id: provider.id,
|
|
47
|
+
name: provider.name,
|
|
48
|
+
message: error instanceof Error ? error.message : String(error),
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
}));
|
|
53
|
+
const groups = catalog.flatMap(item => item.kind === 'group' ? [item.group] : [])
|
|
54
|
+
.filter(group => group.models.length > 0);
|
|
55
|
+
return {
|
|
56
|
+
default: { ...defaultSelection },
|
|
57
|
+
routableProviders: groups.map(group => group.id),
|
|
58
|
+
groups,
|
|
59
|
+
failures: catalog.flatMap(item => item.kind === 'failure' ? [item.failure] : []),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Check a GUI selection against the current available provider catalog.
|
|
64
|
+
* @param ctx - Host LLM registry.
|
|
65
|
+
* @param selection - stored or explicitly requested selection.
|
|
66
|
+
* @returns whether the exact model is currently advertised as available.
|
|
67
|
+
*/
|
|
68
|
+
export async function modelAvailable(ctx, selection) {
|
|
69
|
+
if (!ctx.llm.listProviders().some(provider => provider.id === selection.provider))
|
|
70
|
+
return false;
|
|
71
|
+
let models;
|
|
72
|
+
try {
|
|
73
|
+
models = await ctx.llm.listModels(selection.provider);
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
throw new RemoteError('session/model-unavailable', error instanceof Error ? error.message : String(error), { provider: selection.provider, model: selection.model });
|
|
77
|
+
}
|
|
78
|
+
return models.some(model => model.id === selection.model);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Check configured provider API-key references independently of model availability.
|
|
82
|
+
* @param ctx - Host registry, settings, and credential services.
|
|
83
|
+
* @returns whether any API-key provider has a configured credential.
|
|
84
|
+
*/
|
|
85
|
+
export async function hasProviderApiKey(ctx) {
|
|
86
|
+
const settings = ctx.get('settings');
|
|
87
|
+
const credentials = ctx.get('credentials');
|
|
88
|
+
if (settings === undefined || credentials === undefined) {
|
|
89
|
+
throw new RemoteError('session/provider-credentials-unavailable', 'provider credentials are unavailable', {});
|
|
90
|
+
}
|
|
91
|
+
const namespaces = settings.describe({ redactSecrets: true });
|
|
92
|
+
for (const provider of ctx.llm.listConfigurableProviders()) {
|
|
93
|
+
if (provider.provider === 'deepseek-account')
|
|
94
|
+
continue;
|
|
95
|
+
let profile = namespaces.find(namespace => namespace.ns === provider.settingsNs)?.value;
|
|
96
|
+
for (const key of provider.settingsPath) {
|
|
97
|
+
profile = typeof profile === 'object' && profile !== null ? Reflect.get(profile, key) : undefined;
|
|
98
|
+
}
|
|
99
|
+
if (typeof profile !== 'object' || profile === null)
|
|
100
|
+
continue;
|
|
101
|
+
const ref = Reflect.get(profile, 'apiKeyEnv');
|
|
102
|
+
if (typeof ref === 'string' && ref.length > 0
|
|
103
|
+
&& (await credentials.describe(credentialRef(ref))).configured)
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/** Observable contiguous Session event window consumed by domain assemblers. */
|
|
2
|
+
import { type ObservableSnapshot } from '@unieai/rabi-client-store';
|
|
3
|
+
import type { LlmAttemptId, StreamChunk } from '@unieai/rabi-llm';
|
|
4
|
+
import type { SessionEvent } from '@unieai/rabi-session/types';
|
|
5
|
+
/** Client-only live chunk presentation; `seq` orders the transient row between durable Session seqs. */
|
|
6
|
+
export interface AssistantLiveChunkEvent {
|
|
7
|
+
readonly type: 'assistant/live-chunk';
|
|
8
|
+
readonly seq: number;
|
|
9
|
+
readonly time: number;
|
|
10
|
+
readonly data: {
|
|
11
|
+
readonly attemptId: LlmAttemptId;
|
|
12
|
+
readonly turn: number;
|
|
13
|
+
readonly step: number;
|
|
14
|
+
readonly chunk: StreamChunk;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/** Current durable Session event or one client-only live chunk presentation. */
|
|
18
|
+
export type SessionEventLike = SessionEvent | AssistantLiveChunkEvent;
|
|
19
|
+
/** Client history entry retaining its coarse transport discriminator. */
|
|
20
|
+
export type SessionEventLikeEntry = {
|
|
21
|
+
readonly type: 'event';
|
|
22
|
+
readonly event: SessionEvent;
|
|
23
|
+
} | {
|
|
24
|
+
readonly type: 'transient';
|
|
25
|
+
readonly event: AssistantLiveChunkEvent;
|
|
26
|
+
};
|
|
27
|
+
/** Scalar live entry accepted by append-only Client paths. */
|
|
28
|
+
export type SessionLiveEventEntry = Extract<SessionEventLikeEntry, {
|
|
29
|
+
readonly type: 'event';
|
|
30
|
+
}>;
|
|
31
|
+
/** Durable Assistant event that atomically supersedes one attempt's transient rows. */
|
|
32
|
+
export interface SessionAssistantSettlementEntry {
|
|
33
|
+
readonly type: 'event';
|
|
34
|
+
readonly event: SessionEvent<'assistant/message'> | SessionEvent<'assistant/attempt'>;
|
|
35
|
+
}
|
|
36
|
+
/** Client-only Assistant frame admitted outside durable cursor algebra. */
|
|
37
|
+
export type SessionTransientEventEntry = Extract<SessionEventLikeEntry, {
|
|
38
|
+
readonly type: 'transient';
|
|
39
|
+
}>;
|
|
40
|
+
/** Exact delta that produced the latest event-window revision. */
|
|
41
|
+
export type SessionEventChange = {
|
|
42
|
+
readonly kind: 'replace';
|
|
43
|
+
readonly entries: readonly SessionEventLikeEntry[];
|
|
44
|
+
} | {
|
|
45
|
+
readonly kind: 'prepend';
|
|
46
|
+
readonly entries: readonly SessionEventLikeEntry[];
|
|
47
|
+
} | {
|
|
48
|
+
readonly kind: 'append';
|
|
49
|
+
readonly entries: readonly SessionEventLikeEntry[];
|
|
50
|
+
} | {
|
|
51
|
+
readonly kind: 'settle-assistant';
|
|
52
|
+
readonly attemptId: LlmAttemptId;
|
|
53
|
+
readonly entry?: SessionAssistantSettlementEntry;
|
|
54
|
+
};
|
|
55
|
+
/** Current contiguous event window and its latest synchronous delta. */
|
|
56
|
+
export interface SessionEventWindow {
|
|
57
|
+
readonly entries: readonly SessionEventLikeEntry[];
|
|
58
|
+
readonly hasMore: boolean;
|
|
59
|
+
readonly revision: number;
|
|
60
|
+
readonly change: SessionEventChange;
|
|
61
|
+
}
|
|
62
|
+
/** Conversation-facing event source exposed by one Session binding. */
|
|
63
|
+
export type SessionEventSource = ObservableSnapshot<SessionEventWindow>;
|
|
64
|
+
/** Session-owned event feed; every accepted window mutation publishes synchronously. */
|
|
65
|
+
export declare class MutableSessionEventSource implements SessionEventSource {
|
|
66
|
+
private readonly listeners;
|
|
67
|
+
private window;
|
|
68
|
+
private snapshot;
|
|
69
|
+
/** @returns the cached event-window snapshot. */
|
|
70
|
+
getSnapshot(): SessionEventWindow;
|
|
71
|
+
/**
|
|
72
|
+
* Subscribe to synchronous window publication.
|
|
73
|
+
* @param listener - invalidation callback.
|
|
74
|
+
* @returns unsubscribe function.
|
|
75
|
+
*/
|
|
76
|
+
subscribe(listener: () => void): () => void;
|
|
77
|
+
/**
|
|
78
|
+
* Replace the complete contiguous window.
|
|
79
|
+
* @param entries - complete window.
|
|
80
|
+
* @param hasMore - whether older history remains.
|
|
81
|
+
*/
|
|
82
|
+
replace(entries: readonly SessionEventLikeEntry[], hasMore: boolean): void;
|
|
83
|
+
/**
|
|
84
|
+
* Prepend one older contiguous page.
|
|
85
|
+
* @param entries - newly loaded older entries.
|
|
86
|
+
* @param hasMore - whether still older history remains.
|
|
87
|
+
*/
|
|
88
|
+
prepend(entries: readonly SessionEventLikeEntry[], hasMore: boolean): void;
|
|
89
|
+
/**
|
|
90
|
+
* Append one contiguous live entry.
|
|
91
|
+
* @param entry - live tail entry.
|
|
92
|
+
*/
|
|
93
|
+
append(entry: SessionEventLikeEntry): void;
|
|
94
|
+
/**
|
|
95
|
+
* Replace one attempt's transient rows with its committed durable settlement.
|
|
96
|
+
* @param attemptId - process-local attempt whose live rows are now redundant.
|
|
97
|
+
* @param entry - durable settlement committed for that attempt.
|
|
98
|
+
*/
|
|
99
|
+
settleAssistant(attemptId: LlmAttemptId, entry?: SessionAssistantSettlementEntry): void;
|
|
100
|
+
private publish;
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=events.d.ts.map
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/** Observable contiguous Session event window consumed by domain assemblers. */
|
|
2
|
+
import { notifySubscribers } from '@unieai/rabi-client-store';
|
|
3
|
+
function leaf(entries) {
|
|
4
|
+
return { kind: 'leaf', entries, length: entries.length };
|
|
5
|
+
}
|
|
6
|
+
function concat(left, right) {
|
|
7
|
+
return { kind: 'concat', left, right, length: left.length + right.length };
|
|
8
|
+
}
|
|
9
|
+
function materialize(node) {
|
|
10
|
+
if (node.kind === 'leaf')
|
|
11
|
+
return node.entries;
|
|
12
|
+
const entries = new Array(node.length);
|
|
13
|
+
const pending = [node];
|
|
14
|
+
let index = 0;
|
|
15
|
+
while (pending.length > 0) {
|
|
16
|
+
const current = pending.pop();
|
|
17
|
+
if (current.kind === 'concat') {
|
|
18
|
+
pending.push(current.right, current.left);
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
for (const entry of current.entries) {
|
|
22
|
+
entries[index] = entry;
|
|
23
|
+
index += 1;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return entries;
|
|
27
|
+
}
|
|
28
|
+
function windowSnapshot(node, hasMore, revision, change) {
|
|
29
|
+
let entries;
|
|
30
|
+
return {
|
|
31
|
+
get entries() {
|
|
32
|
+
entries ??= materialize(node);
|
|
33
|
+
return entries;
|
|
34
|
+
},
|
|
35
|
+
hasMore,
|
|
36
|
+
revision,
|
|
37
|
+
change,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/** Session-owned event feed; every accepted window mutation publishes synchronously. */
|
|
41
|
+
export class MutableSessionEventSource {
|
|
42
|
+
listeners = new Set();
|
|
43
|
+
window = leaf([]);
|
|
44
|
+
snapshot = windowSnapshot(this.window, false, 0, { kind: 'replace', entries: [] });
|
|
45
|
+
/** @returns the cached event-window snapshot. */
|
|
46
|
+
getSnapshot() { return this.snapshot; }
|
|
47
|
+
/**
|
|
48
|
+
* Subscribe to synchronous window publication.
|
|
49
|
+
* @param listener - invalidation callback.
|
|
50
|
+
* @returns unsubscribe function.
|
|
51
|
+
*/
|
|
52
|
+
subscribe(listener) {
|
|
53
|
+
this.listeners.add(listener);
|
|
54
|
+
return () => { this.listeners.delete(listener); };
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Replace the complete contiguous window.
|
|
58
|
+
* @param entries - complete window.
|
|
59
|
+
* @param hasMore - whether older history remains.
|
|
60
|
+
*/
|
|
61
|
+
replace(entries, hasMore) {
|
|
62
|
+
this.window = leaf(entries);
|
|
63
|
+
this.publish(hasMore, { kind: 'replace', entries });
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Prepend one older contiguous page.
|
|
67
|
+
* @param entries - newly loaded older entries.
|
|
68
|
+
* @param hasMore - whether still older history remains.
|
|
69
|
+
*/
|
|
70
|
+
prepend(entries, hasMore) {
|
|
71
|
+
this.window = concat(leaf(entries), this.window);
|
|
72
|
+
this.publish(hasMore, { kind: 'prepend', entries });
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Append one contiguous live entry.
|
|
76
|
+
* @param entry - live tail entry.
|
|
77
|
+
*/
|
|
78
|
+
append(entry) {
|
|
79
|
+
const entries = [entry];
|
|
80
|
+
this.window = concat(this.window, leaf(entries));
|
|
81
|
+
this.publish(this.snapshot.hasMore, {
|
|
82
|
+
kind: 'append',
|
|
83
|
+
entries,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Replace one attempt's transient rows with its committed durable settlement.
|
|
88
|
+
* @param attemptId - process-local attempt whose live rows are now redundant.
|
|
89
|
+
* @param entry - durable settlement committed for that attempt.
|
|
90
|
+
*/
|
|
91
|
+
settleAssistant(attemptId, entry) {
|
|
92
|
+
const entries = materialize(this.window).filter(candidate => (candidate.type !== 'transient' || candidate.event.data.attemptId !== attemptId));
|
|
93
|
+
if (entry !== undefined) {
|
|
94
|
+
const index = entries.findIndex(candidate => candidate.event.seq > entry.event.seq);
|
|
95
|
+
if (index < 0)
|
|
96
|
+
entries.push(entry);
|
|
97
|
+
else
|
|
98
|
+
entries.splice(index, 0, entry);
|
|
99
|
+
}
|
|
100
|
+
this.window = leaf(entries);
|
|
101
|
+
this.publish(this.snapshot.hasMore, {
|
|
102
|
+
kind: 'settle-assistant',
|
|
103
|
+
attemptId,
|
|
104
|
+
...(entry === undefined ? {} : { entry }),
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
publish(hasMore, change) {
|
|
108
|
+
this.snapshot = windowSnapshot(this.window, hasMore, this.snapshot.revision + 1, change);
|
|
109
|
+
notifySubscribers(this.listeners, '[session-controller] event feed');
|
|
110
|
+
}
|
|
111
|
+
}
|