@gemies/sdk 7.1.0 → 8.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 +28 -0
- package/README.md +9 -6
- package/package.json +1 -1
- 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/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 -1
- package/src/runtime/room-events.ts +23 -17
- 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/thread/agent-exchange.ts +0 -94
- package/src/runtime/thread/speaker-changes.ts +0 -66
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* What an agent was asked and what it answered, rebuilt from the thread alone.
|
|
3
|
-
*
|
|
4
|
-
* A consultation (`consultAgent`, ADR 0020 §5) is never a conversation of its own in storage. It
|
|
5
|
-
* leaves two traces in the turn it happened in: the asker's reply carries the tool invocation (its
|
|
6
|
-
* `args.task` is what was asked, its `result` the answer with the agent's identity), and the
|
|
7
|
-
* consulted agent's own message (`agentInfo`) holds the answer. Both are already loaded with the
|
|
8
|
-
* thread, so the internal exchange needs no request.
|
|
9
|
-
*
|
|
10
|
-
* Several consultations of one agent in a turn come out in the order they were made. A turn whose
|
|
11
|
-
* task was not kept (an older message, a trimmed invocation) still yields the answers, with no
|
|
12
|
-
* question.
|
|
13
|
-
*/
|
|
14
|
-
import { messageText } from '../../lib/content-utils';
|
|
15
|
-
import type { Message } from '../types';
|
|
16
|
-
|
|
17
|
-
/** The tool the lead consults with (`lib/tools/consult-agent.ts`). */
|
|
18
|
-
export const CONSULT_AGENT_TOOL = 'consultAgent';
|
|
19
|
-
|
|
20
|
-
/** An agent as a message carries it (`Message.agentInfo`). */
|
|
21
|
-
type ExchangeAgent = NonNullable<Message['agentInfo']>;
|
|
22
|
-
|
|
23
|
-
interface AgentExchangePair {
|
|
24
|
-
/** Who asked: the agent whose reply held the call, or `null` for the conversation's own lead. */
|
|
25
|
-
asker: ExchangeAgent | null;
|
|
26
|
-
/** What was asked; `null` when the stored invocation has no task. */
|
|
27
|
-
task: string | null;
|
|
28
|
-
/** What the consulted agent answered. */
|
|
29
|
-
answer: string;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export interface AgentExchange {
|
|
33
|
-
/** The consulted agent. */
|
|
34
|
-
agent: ExchangeAgent;
|
|
35
|
-
pairs: AgentExchangePair[];
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
type ExchangeRow = Pick<Message, 'id' | 'role' | 'content' | 'toolInvocations' | 'agentInfo'>;
|
|
39
|
-
|
|
40
|
-
interface Consultation {
|
|
41
|
-
asker: ExchangeAgent | null;
|
|
42
|
-
task: string | null;
|
|
43
|
-
response: string | null;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* The exchange between the conversation and the agent that speaks at `anchorId`, over the turn
|
|
48
|
-
* (the rows between the questions of the person) that message is in. `null` when `anchorId` is
|
|
49
|
-
* not a message of a consulted agent.
|
|
50
|
-
*/
|
|
51
|
-
export function agentExchange(
|
|
52
|
-
rows: readonly ExchangeRow[],
|
|
53
|
-
anchorId: string,
|
|
54
|
-
/** The direct agent or group members speak to the person, not in an internal exchange. */
|
|
55
|
-
members?: ReadonlySet<string>,
|
|
56
|
-
): AgentExchange | null {
|
|
57
|
-
const anchorIndex = rows.findIndex((row) => row.id === anchorId);
|
|
58
|
-
const agent = rows[anchorIndex]?.agentInfo;
|
|
59
|
-
if (anchorIndex < 0 || !agent || members?.has(agent.id)) return null;
|
|
60
|
-
|
|
61
|
-
let start = anchorIndex;
|
|
62
|
-
while (start > 0 && rows[start - 1]?.role !== 'user') start -= 1;
|
|
63
|
-
let end = anchorIndex + 1;
|
|
64
|
-
while (end < rows.length && rows[end]?.role !== 'user') end += 1;
|
|
65
|
-
const turn = rows.slice(start, end);
|
|
66
|
-
|
|
67
|
-
const consultations: Consultation[] = [];
|
|
68
|
-
for (const row of turn) {
|
|
69
|
-
for (const call of row.toolInvocations ?? []) {
|
|
70
|
-
if (call.toolName !== CONSULT_AGENT_TOOL || call.state !== 'result') continue;
|
|
71
|
-
const result = call.result;
|
|
72
|
-
if (!result || typeof result !== 'object' || result.error || result.agentId !== agent.id) {
|
|
73
|
-
continue;
|
|
74
|
-
}
|
|
75
|
-
const task = call.args?.task;
|
|
76
|
-
consultations.push({
|
|
77
|
-
asker: row.agentInfo ?? null,
|
|
78
|
-
task: typeof task === 'string' && task.trim() !== '' ? task : null,
|
|
79
|
-
response: typeof result.response === 'string' ? result.response : null,
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
const answers = turn.filter((row) => row.role === 'assistant' && row.agentInfo?.id === agent.id);
|
|
84
|
-
|
|
85
|
-
const pairs: AgentExchangePair[] = [];
|
|
86
|
-
for (let i = 0; i < Math.max(consultations.length, answers.length); i += 1) {
|
|
87
|
-
const consultation = consultations[i];
|
|
88
|
-
const stored = answers[i];
|
|
89
|
-
const answer = stored ? messageText(stored) : (consultation?.response ?? '');
|
|
90
|
-
if (answer === '' && !consultation?.task) continue;
|
|
91
|
-
pairs.push({ asker: consultation?.asker ?? null, task: consultation?.task ?? null, answer });
|
|
92
|
-
}
|
|
93
|
-
return { agent, pairs };
|
|
94
|
-
}
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The turns where another agent comes into the conversation.
|
|
3
|
-
*
|
|
4
|
-
* Agent-authored replies carry `agentInfo`, including the lead's scheduled or proactive replies.
|
|
5
|
-
* The conversation's members (the direct agent or the group's roster) distinguish those from
|
|
6
|
-
* consultations with an outside agent. The thread marks the turn where a consulted agent starts
|
|
7
|
-
* speaking, or starts again after the lead has spoken in between, with a line saying whom the person is now
|
|
8
|
-
* chatting with. Consecutive replies of one agent get the line once; the lead resuming gets none
|
|
9
|
-
* (it is the conversation's own voice) but does reset the run, so the same agent coming back is
|
|
10
|
-
* announced again.
|
|
11
|
-
*
|
|
12
|
-
* Only an agent from OUTSIDE the conversation is announced. The members of a group chat are in
|
|
13
|
-
* it: each speaks as itself, with its own name on its message, as in any group chat, and a line
|
|
14
|
-
* saying whom the person is "now chatting with" at each change of speaker would be wrong.
|
|
15
|
-
*
|
|
16
|
-
* What a consulted agent said is not part of the person's conversation: the lead already answers
|
|
17
|
-
* with it. The thread shows the line and nothing under it, and the line opens the exchange.
|
|
18
|
-
*/
|
|
19
|
-
export interface SpeakerChange {
|
|
20
|
-
/** The agent that starts speaking here. */
|
|
21
|
-
readonly agentName: string;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function speakerChanges(
|
|
25
|
-
rows: readonly {
|
|
26
|
-
readonly id: string;
|
|
27
|
-
readonly role: string;
|
|
28
|
-
readonly agentInfo?: { readonly id: string; readonly name: string } | null;
|
|
29
|
-
}[],
|
|
30
|
-
/** The direct agent or group members: never announced. */
|
|
31
|
-
members?: ReadonlySet<string>,
|
|
32
|
-
): Map<string, SpeakerChange> {
|
|
33
|
-
const changes = new Map<string, SpeakerChange>();
|
|
34
|
-
let current: string | null = null;
|
|
35
|
-
for (const row of rows) {
|
|
36
|
-
if (row.role !== 'assistant') continue;
|
|
37
|
-
const speaker = row.agentInfo?.id ?? null;
|
|
38
|
-
if (speaker !== null && speaker !== current && row.agentInfo && !members?.has(speaker)) {
|
|
39
|
-
changes.set(row.id, { agentName: row.agentInfo.name });
|
|
40
|
-
}
|
|
41
|
-
current = speaker;
|
|
42
|
-
}
|
|
43
|
-
return changes;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* The replies of agents from OUTSIDE the conversation, every one of them: what a consulted agent
|
|
48
|
-
* said, which the thread leaves out under the line that announces it (the exchange it opens has
|
|
49
|
-
* it). A group's members and the lead are never in it.
|
|
50
|
-
*/
|
|
51
|
-
export function consultedReplies(
|
|
52
|
-
rows: readonly {
|
|
53
|
-
readonly id: string;
|
|
54
|
-
readonly role: string;
|
|
55
|
-
readonly agentInfo?: { readonly id: string; readonly name: string } | null;
|
|
56
|
-
}[],
|
|
57
|
-
members?: ReadonlySet<string>,
|
|
58
|
-
): Set<string> {
|
|
59
|
-
const ids = new Set<string>();
|
|
60
|
-
for (const row of rows) {
|
|
61
|
-
if (row.role === 'assistant' && row.agentInfo && !members?.has(row.agentInfo.id)) {
|
|
62
|
-
ids.add(row.id);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
return ids;
|
|
66
|
-
}
|