@gemies/sdk 7.1.0 → 9.0.0
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/CHANGELOG.md +43 -0
- package/README.md +9 -6
- package/package.json +1 -1
- package/src/accounts.ts +0 -11
- package/src/components/GemieBetweenGemies.tsx +115 -0
- package/src/components/GemieChatContent.tsx +38 -44
- package/src/components/GemieParticipants.tsx +12 -64
- package/src/components/GemieSharedConversation.tsx +1 -9
- package/src/components/thread/GemieConversationCard.tsx +120 -0
- package/src/components/thread/GemieThread.tsx +30 -80
- package/src/components/thread/ThreadRow.tsx +24 -5
- package/src/hooks/useThreadRows.ts +0 -12
- package/src/index.ts +5 -6
- package/src/lib/copy-en.ts +8 -5
- package/src/lib/copy-es.ts +8 -5
- package/src/lib/gemie-conversation-opener.ts +14 -0
- package/src/lib/participants.ts +16 -22
- package/src/realtime/cache.ts +5 -0
- package/src/realtime/contract.ts +4 -0
- package/src/runtime/connected-accounts.ts +2 -24
- package/src/runtime/conversations.ts +18 -8
- package/src/runtime/gemie-conversations.ts +122 -0
- package/src/runtime/index.ts +6 -2
- package/src/runtime/message-history.ts +9 -12
- package/src/runtime/query-keys.ts +6 -3
- package/src/runtime/room-events.ts +23 -17
- package/src/runtime/routes.ts +0 -5
- package/src/runtime/stream-events.ts +10 -7
- package/src/runtime/types.ts +10 -3
- package/src/runtime/use-conversation-stream.ts +31 -26
- package/src/runtime/use-conversation-thread.ts +1 -1
- package/src/components/thread/AgentExchangeView.tsx +0 -112
- package/src/runtime/channel-tasks.ts +0 -78
- package/src/runtime/thread/agent-exchange.ts +0 -94
- package/src/runtime/thread/speaker-changes.ts +0 -66
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Conversations between gemies (ADR 0042), as a chat with the person sees them.
|
|
3
|
+
*
|
|
4
|
+
* A gemie writes to another with `sendMessage`: the message goes into the conversation the two of
|
|
5
|
+
* them have for this person, and the call's result names it (`conversationId`, both gemies, the
|
|
6
|
+
* chain it started). The person's chat draws that call as a card — "Pepe is talking to Lumen…" —
|
|
7
|
+
* that follows the chain live over the conversation's `room` frames on the realtime socket and opens the
|
|
8
|
+
* whole conversation, read-only. Pure: the card and its hook are `GemieConversationCard`.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { RoomEvent } from './room-events';
|
|
12
|
+
import type { Message, ToolInvocation } from './types';
|
|
13
|
+
|
|
14
|
+
/** The messaging primitive a gemie writes to another with (`lib/tools/send-message.ts`). */
|
|
15
|
+
export const SEND_MESSAGE_TOOL = 'sendMessage';
|
|
16
|
+
|
|
17
|
+
type Agent = NonNullable<Message['agentInfo']>;
|
|
18
|
+
|
|
19
|
+
/** What a `sendMessage` call says about the conversation it wrote in. */
|
|
20
|
+
export interface GemieConversationCall {
|
|
21
|
+
/** The conversation between the two gemies; `null` until the call has a result, or if it failed. */
|
|
22
|
+
readonly conversationId: string | null;
|
|
23
|
+
/** The chain the message started, when the server named it: its events carry it. */
|
|
24
|
+
readonly chainId: string | null;
|
|
25
|
+
readonly from: Agent | null;
|
|
26
|
+
readonly to: Agent | null;
|
|
27
|
+
/** The call is still running: the message is being written. */
|
|
28
|
+
readonly writing: boolean;
|
|
29
|
+
/** Why it could not be sent, when it could not. */
|
|
30
|
+
readonly error: string | null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const isObject = (value: unknown): value is Record<string, unknown> =>
|
|
34
|
+
value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
35
|
+
|
|
36
|
+
function agentOf(value: unknown): Agent | null {
|
|
37
|
+
if (!isObject(value) || typeof value.id !== 'string' || typeof value.name !== 'string') {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
id: value.id,
|
|
42
|
+
name: value.name,
|
|
43
|
+
...(typeof value.handle === 'string' ? { handle: value.handle } : {}),
|
|
44
|
+
...(typeof value.color === 'string' ? { color: value.color } : {}),
|
|
45
|
+
...(isObject(value.character) ? { character: value.character } : {}),
|
|
46
|
+
} as Agent;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The conversation a `sendMessage` call wrote in, or `null` for any other call. */
|
|
50
|
+
export function gemieConversationOf(call: ToolInvocation): GemieConversationCall | null {
|
|
51
|
+
if (call.toolName !== SEND_MESSAGE_TOOL) return null;
|
|
52
|
+
const result = isObject(call.result) ? call.result : null;
|
|
53
|
+
const error = typeof result?.error === 'string' ? result.error : null;
|
|
54
|
+
const conversationId =
|
|
55
|
+
error === null && typeof result?.conversationId === 'string' ? result.conversationId : null;
|
|
56
|
+
return {
|
|
57
|
+
conversationId,
|
|
58
|
+
chainId: typeof result?.chainId === 'string' ? result.chainId : null,
|
|
59
|
+
from: agentOf(result?.from),
|
|
60
|
+
to: agentOf(result?.to),
|
|
61
|
+
writing: call.state !== 'result',
|
|
62
|
+
error,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The conversation's row moved (the realtime `conversation` frame every stored message is): its
|
|
68
|
+
* last line is the last thing either of them said — the first message included, which no turn
|
|
69
|
+
* streams.
|
|
70
|
+
*/
|
|
71
|
+
export function foldGemieConversationRow(
|
|
72
|
+
live: GemieConversationLive,
|
|
73
|
+
row: { readonly id: string; readonly lastMessage: string | null },
|
|
74
|
+
conversationId: string,
|
|
75
|
+
): GemieConversationLive {
|
|
76
|
+
const line = row.lastMessage?.trim() ?? '';
|
|
77
|
+
return row.id !== conversationId || line === '' || line === live.lastLine
|
|
78
|
+
? live
|
|
79
|
+
: { ...live, lastLine: line };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** What the card knows of the conversation while it watches it. */
|
|
83
|
+
export interface GemieConversationLive {
|
|
84
|
+
/** The chain is running: one of them is writing, or the other is about to answer. */
|
|
85
|
+
readonly talking: boolean;
|
|
86
|
+
/** Who is writing right now, if anybody. */
|
|
87
|
+
readonly speaking: Agent | null;
|
|
88
|
+
/** The last thing either of them said while the card watched. */
|
|
89
|
+
readonly lastLine: string | null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* What one event of the conversation does to what the card shows. Events of another conversation,
|
|
94
|
+
* or of another chain when the card knows its own, change nothing (the same object comes back).
|
|
95
|
+
*/
|
|
96
|
+
export function foldGemieConversationEvent(
|
|
97
|
+
live: GemieConversationLive,
|
|
98
|
+
event: RoomEvent,
|
|
99
|
+
conversationId: string,
|
|
100
|
+
chainId: string | null,
|
|
101
|
+
): GemieConversationLive {
|
|
102
|
+
if (event.conversationId !== conversationId || !isObject(event.data)) return live;
|
|
103
|
+
if (chainId !== null && event.chainId !== undefined && event.chainId !== chainId) return live;
|
|
104
|
+
const data = event.data;
|
|
105
|
+
switch (event.event) {
|
|
106
|
+
case 'alia.agent_start':
|
|
107
|
+
return { ...live, talking: true, speaking: agentOf(data.agent) };
|
|
108
|
+
case 'alia.agent_end': {
|
|
109
|
+
const content =
|
|
110
|
+
data.status === 'completed' && typeof data.content === 'string' ? data.content.trim() : '';
|
|
111
|
+
return content === ''
|
|
112
|
+
? live.speaking === null
|
|
113
|
+
? live
|
|
114
|
+
: { ...live, speaking: null }
|
|
115
|
+
: { ...live, speaking: null, lastLine: content };
|
|
116
|
+
}
|
|
117
|
+
case 'group.idle':
|
|
118
|
+
return { ...live, talking: false, speaking: null };
|
|
119
|
+
default:
|
|
120
|
+
return live;
|
|
121
|
+
}
|
|
122
|
+
}
|
package/src/runtime/index.ts
CHANGED
|
@@ -66,8 +66,12 @@ export type {
|
|
|
66
66
|
GemieHostTools,
|
|
67
67
|
HostResultRecord as GemieHostResultRecord,
|
|
68
68
|
} from './host-tools';
|
|
69
|
-
export {
|
|
70
|
-
|
|
69
|
+
export {
|
|
70
|
+
FEATURE_ENTITLEMENT_REQUIRED,
|
|
71
|
+
parseMemberTurnEvent,
|
|
72
|
+
parseHostAction,
|
|
73
|
+
} from './stream-events';
|
|
74
|
+
export type { MemberTurnAgent, MemberTurnEvent } from './stream-events';
|
|
71
75
|
export {
|
|
72
76
|
DEFAULT_SHARE_TTL_HOURS,
|
|
73
77
|
readSharedConversation,
|
|
@@ -41,23 +41,20 @@ function formatOutboundMessage(message: Message): OutboundMessage {
|
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
/**
|
|
44
|
-
* The id
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
* Derived, not received: the frames name a consultation (`consultId`), not a
|
|
48
|
-
* message. The server stores the same answer under the same derivation
|
|
49
|
-
* (`conversation-saver.ts` `agentMessageId`).
|
|
44
|
+
* The id a group member's reply is drawn under in the turn whose request is
|
|
45
|
+
* `assistantMessageId` — the `index`-th `alia.agent_start` of that request — until
|
|
46
|
+
* its end frame names the stored reply (`messageId`), which then replaces it.
|
|
50
47
|
*/
|
|
51
|
-
export function
|
|
52
|
-
return `${
|
|
48
|
+
export function memberDraftId(assistantMessageId: string, index: number): string {
|
|
49
|
+
return `${memberDraftPrefix(assistantMessageId)}${index}`;
|
|
53
50
|
}
|
|
54
51
|
|
|
55
|
-
/** Whether `id` is one of the
|
|
56
|
-
export function
|
|
57
|
-
return id.startsWith(
|
|
52
|
+
/** Whether `id` is one of the drafts {@link memberDraftId} names for this request. */
|
|
53
|
+
export function isMemberDraftOf(id: string, assistantMessageId: string): boolean {
|
|
54
|
+
return id.startsWith(memberDraftPrefix(assistantMessageId));
|
|
58
55
|
}
|
|
59
56
|
|
|
60
|
-
function
|
|
57
|
+
function memberDraftPrefix(assistantMessageId: string): string {
|
|
61
58
|
return `${assistantMessageId}-agent-`;
|
|
62
59
|
}
|
|
63
60
|
|
|
@@ -8,6 +8,11 @@ export interface ConversationsFilter {
|
|
|
8
8
|
agentId?: string;
|
|
9
9
|
/** Only the conversations started in this app, by its slug (`docs/agent-embedding.md` §6). */
|
|
10
10
|
app?: string;
|
|
11
|
+
/**
|
|
12
|
+
* The conversations the person's gemies have with each other (`gemies`, ADR 0042) instead of the
|
|
13
|
+
* person's own, which are the default and never include them.
|
|
14
|
+
*/
|
|
15
|
+
kind?: 'gemies';
|
|
11
16
|
}
|
|
12
17
|
|
|
13
18
|
export const gemieQueryKeys = {
|
|
@@ -21,7 +26,7 @@ export const gemieQueryKeys = {
|
|
|
21
26
|
* write to the list reaches it.
|
|
22
27
|
*/
|
|
23
28
|
filtered: (filter: ConversationsFilter) =>
|
|
24
|
-
['conversations', { agentId: filter.agentId, app: filter.app }] as const,
|
|
29
|
+
['conversations', { agentId: filter.agentId, app: filter.app, kind: filter.kind }] as const,
|
|
25
30
|
},
|
|
26
31
|
/**
|
|
27
32
|
* The person's connected accounts (ADR 0035 as amended): the list with its grants, the code of
|
|
@@ -32,8 +37,6 @@ export const gemieQueryKeys = {
|
|
|
32
37
|
all: ['connected-accounts'] as const,
|
|
33
38
|
qr: (accountId: string) => ['connected-account-qr', accountId] as const,
|
|
34
39
|
chats: (accountId: string) => ['connected-account-chats', accountId] as const,
|
|
35
|
-
/** The signed-in account's silent channel-task renewal, once a session (`channel-tasks.ts`). */
|
|
36
|
-
channelTaskRenewal: (userId: string) => ['channel-task-renewal', userId] as const,
|
|
37
40
|
},
|
|
38
41
|
/** An agent the chat is opened on: its profile and the state of its feature gates. */
|
|
39
42
|
agents: {
|
|
@@ -32,20 +32,20 @@ export function roomFailureReason(value: unknown): RoomFailureReason | null {
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
/**
|
|
35
|
-
* Which
|
|
35
|
+
* Which member's speaking turn is drawn in which message, and what the request already owns.
|
|
36
36
|
*
|
|
37
37
|
* Mutable on purpose: one per open conversation, held by the thread.
|
|
38
38
|
*/
|
|
39
39
|
export interface RoomLedger {
|
|
40
40
|
/** `consultId` to the id of the message that draws it. */
|
|
41
41
|
messages: Map<string, string>;
|
|
42
|
-
/** Socket-observed chain for a
|
|
42
|
+
/** Socket-observed chain for a turn, including one initially drawn by SSE. */
|
|
43
43
|
chains: Map<string, string>;
|
|
44
|
-
/**
|
|
44
|
+
/** Turns the chat request's SSE frames drew: the socket must not draw them again. */
|
|
45
45
|
claimed: Set<string>;
|
|
46
|
-
/** Claimed
|
|
46
|
+
/** Claimed turns whose request ended with `room_continues`: the socket carries them now. */
|
|
47
47
|
released: Set<string>;
|
|
48
|
-
/**
|
|
48
|
+
/** Turns whose end was applied. */
|
|
49
49
|
ended: Set<string>;
|
|
50
50
|
/** Provisional replies explicitly discarded by the room, never restored by late frames. */
|
|
51
51
|
discarded: Set<string>;
|
|
@@ -62,7 +62,7 @@ export function createRoomLedger(): RoomLedger {
|
|
|
62
62
|
};
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
/** The id a
|
|
65
|
+
/** The id a turn drawn from the socket gets until its stored reply names it. */
|
|
66
66
|
const roomDraftId = (consultId: string): string => `room-${consultId}`;
|
|
67
67
|
|
|
68
68
|
/**
|
|
@@ -71,7 +71,7 @@ const roomDraftId = (consultId: string): string => `room-${consultId}`;
|
|
|
71
71
|
* the caller empties that message and lets the request say it all (what the socket added before
|
|
72
72
|
* would otherwise be said twice).
|
|
73
73
|
*/
|
|
74
|
-
export function
|
|
74
|
+
export function claimMemberTurn(ledger: RoomLedger, consultId: string, id: string): string | null {
|
|
75
75
|
const existing = ledger.messages.get(consultId);
|
|
76
76
|
ledger.claimed.add(consultId);
|
|
77
77
|
if (existing !== undefined) return existing;
|
|
@@ -80,7 +80,7 @@ export function claimConsult(ledger: RoomLedger, consultId: string, id: string):
|
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
/** The request closed `consultId` with `room_continues`: the room goes on, the socket takes over. */
|
|
83
|
-
export function
|
|
83
|
+
export function releaseMemberTurn(ledger: RoomLedger, consultId: string): void {
|
|
84
84
|
if (ledger.claimed.has(consultId)) ledger.released.add(consultId);
|
|
85
85
|
}
|
|
86
86
|
|
|
@@ -98,7 +98,7 @@ const isObject = (value: unknown): value is Raw =>
|
|
|
98
98
|
const text = (value: unknown): string | undefined =>
|
|
99
99
|
typeof value === 'string' && value !== '' ? value : undefined;
|
|
100
100
|
|
|
101
|
-
/** A
|
|
101
|
+
/** A turn the socket drives is live; one the request drew is, once it let go. */
|
|
102
102
|
const isLive = (ledger: RoomLedger, consultId: string): boolean =>
|
|
103
103
|
!ledger.claimed.has(consultId) || ledger.released.has(consultId);
|
|
104
104
|
|
|
@@ -166,7 +166,7 @@ export function applyRoomEvent(
|
|
|
166
166
|
incoming.event === 'alia.agent_tool' ||
|
|
167
167
|
incoming.event === 'alia.agent_end')
|
|
168
168
|
) {
|
|
169
|
-
// SSE may already own this
|
|
169
|
+
// SSE may already own this turn, but only the socket carries its chain.
|
|
170
170
|
// Record it before the duplicate/ownership guards can discard the frame.
|
|
171
171
|
const knownChain = ledger.chains.get(consultId);
|
|
172
172
|
if (knownChain !== undefined && knownChain !== chainId) return messages;
|
|
@@ -180,16 +180,22 @@ export function applyRoomEvent(
|
|
|
180
180
|
if (!isObject(agent) || !text(agent.id) || !text(agent.name)) return messages;
|
|
181
181
|
const id = roomDraftId(consultId);
|
|
182
182
|
ledger.messages.set(consultId, id);
|
|
183
|
+
const draft: Message = {
|
|
184
|
+
id,
|
|
185
|
+
unsaved: true,
|
|
186
|
+
role: 'assistant',
|
|
187
|
+
content: '',
|
|
188
|
+
toolInvocations: [],
|
|
189
|
+
createdAt: new Date().toISOString(),
|
|
190
|
+
isStreaming: true,
|
|
191
|
+
};
|
|
192
|
+
// The conversation's own lead speaking in its chat (a gemie telling the person what another
|
|
193
|
+
// gemie answered, ADR 0042): its ordinary reply, drawn as every other one of its replies.
|
|
194
|
+
if (data.lead === true) return [...messages, draft];
|
|
183
195
|
return [
|
|
184
196
|
...messages,
|
|
185
197
|
{
|
|
186
|
-
|
|
187
|
-
unsaved: true,
|
|
188
|
-
role: 'assistant',
|
|
189
|
-
content: '',
|
|
190
|
-
toolInvocations: [],
|
|
191
|
-
createdAt: new Date().toISOString(),
|
|
192
|
-
isStreaming: true,
|
|
198
|
+
...draft,
|
|
193
199
|
agentInfo: {
|
|
194
200
|
id: agent.id as string,
|
|
195
201
|
name: agent.name as string,
|
package/src/runtime/routes.ts
CHANGED
|
@@ -105,9 +105,4 @@ export const ACCOUNT_ROUTES = {
|
|
|
105
105
|
/** A gemie's grant of one account: `PUT` grants it, `DELETE` revokes it. */
|
|
106
106
|
grant: (accountId: string, gemieId: string) =>
|
|
107
107
|
`/me/accounts/${encodeURIComponent(accountId)}/gemies/${encodeURIComponent(gemieId)}`,
|
|
108
|
-
/**
|
|
109
|
-
* `POST` renews every channel of the caller whose Oxy task needs it — a connected account, a
|
|
110
|
-
* system bot link, a gemie bot they linked their Telegram to — with their present session.
|
|
111
|
-
*/
|
|
112
|
-
renewChannelTasks: '/me/channel-tasks/renew',
|
|
113
108
|
} as const;
|
|
@@ -6,8 +6,11 @@
|
|
|
6
6
|
* `use-conversation-stream.ts` decides what each one does to the thread.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
/**
|
|
10
|
-
|
|
9
|
+
/**
|
|
10
|
+
* A group member that speaks in a room's turn, as `alia.agent_start` names it (ADR 0025). The
|
|
11
|
+
* frames' `consultId` is the id of that speaking turn: the wire's name for it.
|
|
12
|
+
*/
|
|
13
|
+
export interface MemberTurnAgent {
|
|
11
14
|
id: string;
|
|
12
15
|
name: string;
|
|
13
16
|
handle?: string;
|
|
@@ -15,8 +18,8 @@ export interface ConsultAgent {
|
|
|
15
18
|
character?: unknown;
|
|
16
19
|
}
|
|
17
20
|
|
|
18
|
-
export type
|
|
19
|
-
| { type: 'start'; consultId: string; agent:
|
|
21
|
+
export type MemberTurnEvent =
|
|
22
|
+
| { type: 'start'; consultId: string; agent: MemberTurnAgent; task?: string }
|
|
20
23
|
| { type: 'delta'; consultId: string; content: string }
|
|
21
24
|
| {
|
|
22
25
|
type: 'tool';
|
|
@@ -43,14 +46,14 @@ const isText = (value: unknown): value is string => typeof value === 'string' &&
|
|
|
43
46
|
const optionalText = (value: unknown): string | undefined =>
|
|
44
47
|
typeof value === 'string' && value !== '' ? value : undefined;
|
|
45
48
|
|
|
46
|
-
/** A frame of this contract: version 1 and
|
|
49
|
+
/** A frame of this contract: version 1 and the speaking turn it belongs to. */
|
|
47
50
|
function versioned(data: unknown): (Raw & { consultId: string }) | null {
|
|
48
51
|
if (!isObject(data) || data.eventVersion !== 1 || !isText(data.consultId)) return null;
|
|
49
52
|
return data as Raw & { consultId: string };
|
|
50
53
|
}
|
|
51
54
|
|
|
52
|
-
/** The
|
|
53
|
-
export function
|
|
55
|
+
/** The member frame `event` carries, or `null` when it is not one or not well-formed. */
|
|
56
|
+
export function parseMemberTurnEvent(event: string, data: unknown): MemberTurnEvent | null {
|
|
54
57
|
const frame = versioned(data);
|
|
55
58
|
if (frame === null) return null;
|
|
56
59
|
const { consultId } = frame;
|
package/src/runtime/types.ts
CHANGED
|
@@ -60,9 +60,9 @@ export interface Message {
|
|
|
60
60
|
*/
|
|
61
61
|
unsaved?: true;
|
|
62
62
|
/**
|
|
63
|
-
* The
|
|
64
|
-
*
|
|
65
|
-
*
|
|
63
|
+
* The gemie that wrote this message: a group member, either gemie of a conversation between
|
|
64
|
+
* gemies (ADR 0042), or the direct agent's proactive/scheduled reply. The conversation's members
|
|
65
|
+
* speak directly to whoever reads it.
|
|
66
66
|
*
|
|
67
67
|
* `character` is the agent's look as the server stores it (JSON), parsed
|
|
68
68
|
* where it is drawn; absent, the default look.
|
|
@@ -122,6 +122,13 @@ export interface Conversation {
|
|
|
122
122
|
* own, a group member answering in the background). The sidebar's badge; zeroed by reading it.
|
|
123
123
|
*/
|
|
124
124
|
unreadCount?: number;
|
|
125
|
+
/**
|
|
126
|
+
* `gemies` for a conversation the person's gemies have with each other (ADR 0042): the person
|
|
127
|
+
* reads it and never writes in it. `person` (or absent) for the person's own.
|
|
128
|
+
*/
|
|
129
|
+
kind?: 'person' | 'gemies';
|
|
130
|
+
/** The two gemies of a conversation between gemies, as their messages carry them. */
|
|
131
|
+
members?: NonNullable<Message['agentInfo']>[];
|
|
125
132
|
createdAt: Date;
|
|
126
133
|
updatedAt: Date;
|
|
127
134
|
messages: Message[];
|
|
@@ -15,14 +15,14 @@ import {
|
|
|
15
15
|
errorStatus,
|
|
16
16
|
readErrorResponseBody,
|
|
17
17
|
} from './error-utils';
|
|
18
|
-
import {
|
|
18
|
+
import { memberDraftId, buildOutboundMessages, isMemberDraftOf } from './message-history';
|
|
19
19
|
import { gemieQueryKeys } from './query-keys';
|
|
20
20
|
import {
|
|
21
21
|
applyRoomEvent,
|
|
22
|
-
|
|
22
|
+
claimMemberTurn,
|
|
23
23
|
createRoomLedger,
|
|
24
24
|
isRoomOwned,
|
|
25
|
-
|
|
25
|
+
releaseMemberTurn,
|
|
26
26
|
type RoomLedger,
|
|
27
27
|
} from './room-events';
|
|
28
28
|
import { createRandomUuid } from './random-uuid';
|
|
@@ -32,9 +32,9 @@ import { type GemieHostTools, HostOutcomeQueue, hostRequest } from './host-tools
|
|
|
32
32
|
import { API_ROUTES } from './routes';
|
|
33
33
|
import { createSseFrameReader } from './sse-frame-reader';
|
|
34
34
|
import {
|
|
35
|
-
type
|
|
35
|
+
type MemberTurnEvent,
|
|
36
36
|
lockedFeatureOf,
|
|
37
|
-
|
|
37
|
+
parseMemberTurnEvent,
|
|
38
38
|
parseHostAction,
|
|
39
39
|
} from './stream-events';
|
|
40
40
|
import { hasUsableStreamOutput, type StreamOutputEvidence } from './stream-outcome';
|
|
@@ -177,7 +177,7 @@ export interface UseConversationStreamOptions {
|
|
|
177
177
|
*/
|
|
178
178
|
clientContext?: string;
|
|
179
179
|
/**
|
|
180
|
-
* Which
|
|
180
|
+
* Which speaking turn each message draws, shared with the room's socket events (`use-room-events.ts`)
|
|
181
181
|
* so a member's reply that both the request and the socket carry is drawn once.
|
|
182
182
|
*/
|
|
183
183
|
room?: RoomLedger;
|
|
@@ -186,7 +186,7 @@ export interface UseConversationStreamOptions {
|
|
|
186
186
|
/** The typed end the chat request gives a member still speaking when it closes (ADR 0025). */
|
|
187
187
|
const ROOM_CONTINUES = 'room_continues';
|
|
188
188
|
|
|
189
|
-
/** The words a message holds, as text: a
|
|
189
|
+
/** The words a message holds, as text: a member's reply is plain text as it streams. */
|
|
190
190
|
function messageWords(message: Message): string {
|
|
191
191
|
return typeof message.content === 'string' ? message.content : '';
|
|
192
192
|
}
|
|
@@ -276,7 +276,7 @@ export function useConversationStream({
|
|
|
276
276
|
// Batching refs: accumulate streaming text and flush at ~20fps instead of per-chunk
|
|
277
277
|
const pendingContentRef = useRef('');
|
|
278
278
|
const pendingReasoningRef = useRef('');
|
|
279
|
-
/** A
|
|
279
|
+
/** A member's streamed words, by the id of the message they belong to. */
|
|
280
280
|
const pendingAgentContentRef = useRef(new Map<string, string>());
|
|
281
281
|
const flushTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
282
282
|
|
|
@@ -574,17 +574,17 @@ export function useConversationStream({
|
|
|
574
574
|
unsaved: true,
|
|
575
575
|
};
|
|
576
576
|
setMessages((prev) => [...prev, assistantMessage]);
|
|
577
|
-
/** How many
|
|
577
|
+
/** How many members have started speaking in this turn: the next one's `memberDraftId` index. */
|
|
578
578
|
let agentAnswers = 0;
|
|
579
|
-
const
|
|
579
|
+
const nextMemberDraftId = (): string => memberDraftId(assistantMessage.id, agentAnswers++);
|
|
580
580
|
/** Each consultation of this turn, by `consultId`: the message its agent writes into. */
|
|
581
|
-
const
|
|
581
|
+
const memberTurns = new Map<string, string>();
|
|
582
582
|
/** Whether `id` names a message this turn wrote: the question, the reply, an agent's answer. */
|
|
583
583
|
const isThisTurn = (id: string): boolean =>
|
|
584
584
|
id === userMessage.id ||
|
|
585
585
|
id === assistantMessage.id ||
|
|
586
|
-
|
|
587
|
-
(isGroupTurn && [...
|
|
586
|
+
isMemberDraftOf(id, assistantMessage.id) ||
|
|
587
|
+
(isGroupTurn && [...memberTurns.values()].includes(id));
|
|
588
588
|
|
|
589
589
|
/**
|
|
590
590
|
* End the assistant message's turn, once.
|
|
@@ -608,7 +608,7 @@ export function useConversationStream({
|
|
|
608
608
|
// A turn that completed is one the server has stored, under these ids.
|
|
609
609
|
const stored = outcome === 'completed';
|
|
610
610
|
return prev.map((m) => {
|
|
611
|
-
// A
|
|
611
|
+
// A member's turn still open when the request ends ends with it.
|
|
612
612
|
const settles =
|
|
613
613
|
m.id === assistantMessage.id ||
|
|
614
614
|
(m.isStreaming === true && isThisTurn(m.id) && !isRoomOwned(roomRef.current, m.id));
|
|
@@ -622,17 +622,22 @@ export function useConversationStream({
|
|
|
622
622
|
});
|
|
623
623
|
};
|
|
624
624
|
|
|
625
|
-
/**
|
|
626
|
-
|
|
625
|
+
/**
|
|
626
|
+
* What a member frame does to the thread: the member's own message, from start to end. Only a
|
|
627
|
+
* group turn's request carries them (ADR 0025); a gemie that wants something from another
|
|
628
|
+
* writes to it in their own conversation (ADR 0042), never in this one.
|
|
629
|
+
*/
|
|
630
|
+
const applyMemberTurn = (event: MemberTurnEvent): void => {
|
|
631
|
+
if (!isGroupTurn) return;
|
|
627
632
|
if (event.type === 'start') {
|
|
628
|
-
if (
|
|
633
|
+
if (memberTurns.has(event.consultId) || roomRef.current.discarded.has(event.consultId))
|
|
629
634
|
return;
|
|
630
|
-
const id =
|
|
631
|
-
|
|
635
|
+
const id = nextMemberDraftId();
|
|
636
|
+
memberTurns.set(event.consultId, id);
|
|
632
637
|
// The room's socket may have drawn this member typing already: that is its message.
|
|
633
|
-
const adopted =
|
|
638
|
+
const adopted = claimMemberTurn(roomRef.current, event.consultId, id);
|
|
634
639
|
if (adopted !== null) {
|
|
635
|
-
|
|
640
|
+
memberTurns.set(event.consultId, adopted);
|
|
636
641
|
if (!roomRef.current.ended.has(event.consultId)) {
|
|
637
642
|
setMessages((prev) =>
|
|
638
643
|
prev.map((m) => (m.id === adopted ? { ...m, content: '' } : m)),
|
|
@@ -659,7 +664,7 @@ export function useConversationStream({
|
|
|
659
664
|
});
|
|
660
665
|
return;
|
|
661
666
|
}
|
|
662
|
-
const id =
|
|
667
|
+
const id = memberTurns.get(event.consultId);
|
|
663
668
|
if (id === undefined || roomRef.current.discarded.has(event.consultId)) return;
|
|
664
669
|
if (
|
|
665
670
|
roomRef.current.ended.has(event.consultId) &&
|
|
@@ -701,7 +706,7 @@ export function useConversationStream({
|
|
|
701
706
|
if (event.errorCode === ROOM_CONTINUES) {
|
|
702
707
|
// The request is over but the member is still speaking: the room's socket carries it on.
|
|
703
708
|
// With no socket to carry it (signed out), it stops here, unfinished but not failed.
|
|
704
|
-
if (realtimeRef.current)
|
|
709
|
+
if (realtimeRef.current) releaseMemberTurn(roomRef.current, event.consultId);
|
|
705
710
|
else {
|
|
706
711
|
setMessages((prev) =>
|
|
707
712
|
prev.map((m) => (m.id === id ? { ...m, isStreaming: false } : m)),
|
|
@@ -720,7 +725,7 @@ export function useConversationStream({
|
|
|
720
725
|
if (event.content !== undefined) outputEvidence.agentOutputChars += event.content.length;
|
|
721
726
|
if (isGroupTurn && event.status === 'completed' && event.messageId !== undefined) {
|
|
722
727
|
roomRef.current.ended.add(event.consultId);
|
|
723
|
-
|
|
728
|
+
memberTurns.set(event.consultId, event.messageId);
|
|
724
729
|
setMessages((prev) =>
|
|
725
730
|
applyRoomEvent(prev, roomRef.current, conversationId ?? '', {
|
|
726
731
|
conversationId: conversationId ?? '',
|
|
@@ -1073,8 +1078,8 @@ export function useConversationStream({
|
|
|
1073
1078
|
case 'alia.agent_delta':
|
|
1074
1079
|
case 'alia.agent_tool':
|
|
1075
1080
|
case 'alia.agent_end': {
|
|
1076
|
-
const
|
|
1077
|
-
if (
|
|
1081
|
+
const memberTurn = parseMemberTurnEvent(currentEventType, parsed);
|
|
1082
|
+
if (memberTurn !== null) applyMemberTurn(memberTurn);
|
|
1078
1083
|
continue;
|
|
1079
1084
|
}
|
|
1080
1085
|
case 'alia.host_action': {
|
|
@@ -136,7 +136,7 @@ export function useConversationThread({
|
|
|
136
136
|
host.closeConversation?.();
|
|
137
137
|
}, [conversationQueryError, queryClient, host]);
|
|
138
138
|
|
|
139
|
-
// One per open conversation: which
|
|
139
|
+
// One per open conversation: which speaking turn each message draws (the request's and the room's).
|
|
140
140
|
// biome-ignore lint/correctness/useExhaustiveDependencies: a new conversation is a new ledger
|
|
141
141
|
const room = useMemo(() => createRoomLedger(), [conversationId]);
|
|
142
142
|
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import type { AiChatThreadHandle } from '@oxy.so/bloom/ai-chat';
|
|
2
|
-
import { ButtonGroup, ButtonGroupItem } from '@oxy.so/bloom/button-group';
|
|
3
|
-
import { RiArrowLeftLine } from '@oxy.so/bloom/icons/RiArrowLeftLine';
|
|
4
|
-
import { PageHeader } from '@oxy.so/bloom/page-header';
|
|
5
|
-
import { Text } from '@oxy.so/bloom/typography';
|
|
6
|
-
import { useMemo, useRef } from 'react';
|
|
7
|
-
import { View } from 'react-native';
|
|
8
|
-
import { useLabels } from '../../lib/labels';
|
|
9
|
-
import { type GemieLead, type GemieParticipant, participantOf } from '../../lib/participants';
|
|
10
|
-
import type { AgentExchange } from '../../runtime/thread/agent-exchange';
|
|
11
|
-
import type { Message } from '../../runtime/types';
|
|
12
|
-
import { GemieParticipants } from '../GemieParticipants';
|
|
13
|
-
import { GemieThread } from './GemieThread';
|
|
14
|
-
|
|
15
|
-
export interface AgentExchangeViewProps {
|
|
16
|
-
exchange: AgentExchange;
|
|
17
|
-
/** The conversation's lead: the asker of every consultation that names none. */
|
|
18
|
-
lead: GemieLead;
|
|
19
|
-
onBack: () => void;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* The conversation two agents had with each other, read-only: what the asker put to the consulted
|
|
24
|
-
* agent and what it answered, drawn by the ordinary thread. The asker speaks as itself (an
|
|
25
|
-
* assistant reply under its own name and avatar, never as the person) and so does the answerer.
|
|
26
|
-
* There is no composer here and nothing to act on: it replaces the chat while it is open.
|
|
27
|
-
*/
|
|
28
|
-
export function AgentExchangeView({ exchange, lead, onBack }: AgentExchangeViewProps) {
|
|
29
|
-
const { t, back } = useLabels();
|
|
30
|
-
const threadRef = useRef<AiChatThreadHandle | null>(null);
|
|
31
|
-
|
|
32
|
-
const { messages, asker, members } = useMemo(() => {
|
|
33
|
-
const first = exchange.pairs.find((pair) => pair.asker !== null)?.asker ?? null;
|
|
34
|
-
const askerIdentity: GemieParticipant = first ? participantOf(first) : lead;
|
|
35
|
-
const rows: Message[] = [];
|
|
36
|
-
exchange.pairs.forEach((pair, index) => {
|
|
37
|
-
const pairAsker = pair.asker ?? (lead.isAlia ? null : lead);
|
|
38
|
-
if (pair.task !== null) {
|
|
39
|
-
rows.push({
|
|
40
|
-
id: `exchange-${index}-ask`,
|
|
41
|
-
role: 'assistant',
|
|
42
|
-
content: pair.task,
|
|
43
|
-
...(pairAsker
|
|
44
|
-
? {
|
|
45
|
-
agentInfo: {
|
|
46
|
-
id: pairAsker.id,
|
|
47
|
-
name: pairAsker.name,
|
|
48
|
-
handle: pairAsker.handle,
|
|
49
|
-
color: pairAsker.color,
|
|
50
|
-
character: 'character' in pairAsker ? pairAsker.character : undefined,
|
|
51
|
-
},
|
|
52
|
-
}
|
|
53
|
-
: {}),
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
if (pair.answer !== '') {
|
|
57
|
-
rows.push({
|
|
58
|
-
id: `exchange-${index}-answer`,
|
|
59
|
-
role: 'assistant',
|
|
60
|
-
content: pair.answer,
|
|
61
|
-
agentInfo: exchange.agent,
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
return {
|
|
66
|
-
messages: rows,
|
|
67
|
-
asker: askerIdentity,
|
|
68
|
-
// Everyone here is already in the conversation: no "Chatted with" line between them.
|
|
69
|
-
members: new Set([askerIdentity.id, exchange.agent.id, lead.id]),
|
|
70
|
-
};
|
|
71
|
-
}, [exchange, lead]);
|
|
72
|
-
|
|
73
|
-
const participants = useMemo(
|
|
74
|
-
() => ({
|
|
75
|
-
lead: { ...asker, isAlia: asker.id === lead.id && lead.isAlia, starters: [] },
|
|
76
|
-
joined: [participantOf(exchange.agent)],
|
|
77
|
-
}),
|
|
78
|
-
[asker, exchange.agent, lead],
|
|
79
|
-
);
|
|
80
|
-
|
|
81
|
-
return (
|
|
82
|
-
<View className="absolute inset-0 z-20 bg-background" testID="agent-exchange">
|
|
83
|
-
<PageHeader
|
|
84
|
-
leading={
|
|
85
|
-
<ButtonGroup accessibilityLabel={back}>
|
|
86
|
-
<ButtonGroupItem
|
|
87
|
-
iconOnly
|
|
88
|
-
leadingIcon={RiArrowLeftLine}
|
|
89
|
-
accessibilityLabel={back}
|
|
90
|
-
onPress={onBack}
|
|
91
|
-
/>
|
|
92
|
-
</ButtonGroup>
|
|
93
|
-
}
|
|
94
|
-
title={<GemieParticipants participants={participants} />}
|
|
95
|
-
titleAlign="center"
|
|
96
|
-
scrim="none"
|
|
97
|
-
sticky={false}
|
|
98
|
-
safeArea={false}
|
|
99
|
-
/>
|
|
100
|
-
<Text className="px-4 pb-2 text-center text-xs text-muted-foreground">
|
|
101
|
-
{t('chat.agentChatNotice')}
|
|
102
|
-
</Text>
|
|
103
|
-
<GemieThread
|
|
104
|
-
messages={messages}
|
|
105
|
-
isLoading={false}
|
|
106
|
-
memberIds={members}
|
|
107
|
-
threadRef={threadRef}
|
|
108
|
-
readOnly
|
|
109
|
-
/>
|
|
110
|
-
</View>
|
|
111
|
-
);
|
|
112
|
-
}
|