@gemies/sdk 7.0.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.
Files changed (35) hide show
  1. package/CHANGELOG.md +44 -0
  2. package/README.md +9 -6
  3. package/package.json +1 -1
  4. package/src/accounts.ts +11 -0
  5. package/src/components/GemieBetweenGemies.tsx +115 -0
  6. package/src/components/GemieChatContent.tsx +38 -44
  7. package/src/components/GemieParticipants.tsx +12 -64
  8. package/src/components/GemieSharedConversation.tsx +1 -9
  9. package/src/components/thread/GemieConversationCard.tsx +120 -0
  10. package/src/components/thread/GemieThread.tsx +30 -80
  11. package/src/components/thread/ThreadRow.tsx +24 -5
  12. package/src/hooks/useThreadRows.ts +0 -12
  13. package/src/index.ts +5 -6
  14. package/src/lib/copy-en.ts +8 -5
  15. package/src/lib/copy-es.ts +8 -5
  16. package/src/lib/gemie-conversation-opener.ts +14 -0
  17. package/src/lib/participants.ts +16 -22
  18. package/src/realtime/cache.ts +5 -0
  19. package/src/realtime/contract.ts +4 -0
  20. package/src/runtime/channel-tasks.ts +78 -0
  21. package/src/runtime/connected-accounts.ts +18 -16
  22. package/src/runtime/conversations.ts +18 -8
  23. package/src/runtime/gemie-conversations.ts +122 -0
  24. package/src/runtime/index.ts +6 -2
  25. package/src/runtime/message-history.ts +9 -12
  26. package/src/runtime/query-keys.ts +8 -1
  27. package/src/runtime/room-events.ts +23 -17
  28. package/src/runtime/routes.ts +5 -0
  29. package/src/runtime/stream-events.ts +10 -7
  30. package/src/runtime/types.ts +10 -3
  31. package/src/runtime/use-conversation-stream.ts +31 -26
  32. package/src/runtime/use-conversation-thread.ts +1 -1
  33. package/src/components/thread/AgentExchangeView.tsx +0 -112
  34. package/src/runtime/thread/agent-exchange.ts +0 -94
  35. package/src/runtime/thread/speaker-changes.ts +0 -66
@@ -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
- }
@@ -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
- }