Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/voice_agents/crew_assistant/app/agent/crewTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,17 @@ export const DISPLAY_TITLES: Record<string, string> = {

/** What the person is asked before a call runs, by tool. */
export const APPROVALS: Record<string, ToolApproval> = {
book_check_in: { allowTitle: 'Book it', title: 'Book this check-in?' },
book_check_in: { allowTitle: 'Book it', reasonArgument: 'topic', title: 'Book this check-in?' },
create_support_ticket: {
allowTitle: 'Send for review',
message: 'The owner approves it before it goes to repairs, and you hear back here.',
reasonArgument: 'summary',
title: 'Draft this repair ticket?',
},
request_shift_swap: {
allowTitle: 'Send request',
message: 'Your teammate and the shift leader see it.',
reasonArgument: 'note',
title: 'Ask a teammate to take your shift?',
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ export const createDeviceTools = ({ clientId, userId }: { clientId: string; user
...(APPROVALS[name] ? { timeoutMs: APPROVAL_TIMEOUT_MS } : {}),
description,
displayTitle: DISPLAY_TITLES[name],
executor: 'client',
// A tool that asks first is declared as the server's, so the router asks on the reply's
// step, where an app reopened while the call waits finds the question. It asks a client
// tool's question there only once it can address this phone, which the SDK can't name yet.
executor: APPROVALS[name] ? 'server' : 'client',
name,
parameters,
run: async (input, call) => {
Expand Down
58 changes: 5 additions & 53 deletions examples/voice_agents/crew_assistant/app/screens/AgentNotices.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
import { useSyncExternalStore } from 'react';
import { Pressable, StyleSheet, Text, View } from 'react-native';
import { Pressable, StyleSheet, Text } from 'react-native';

import type { Session, ToolApprovalRequest } from '@stream-io/stream-agents';
import type { Session } from '@stream-io/stream-agents';
import { useStateStore } from 'stream-chat-expo';
import { AIToolApprovalCard } from 'stream-chat-expo/ai-components';

import { type Chat, type ChatState, chatStore } from '../agent/conversation';
import { useAppContext } from '../contexts/AppContext';

const NONE: readonly ToolApprovalRequest[] = [];

const sessionSelector = ({ session }: ChatState) => ({ session });

// What the call would do, in a line, from the arguments the model wrote.
const detailsOf = ({ arguments: written }: ToolApprovalRequest) => {
try {
const parsed = JSON.parse(written) as Record<string, unknown>;
return Object.entries(parsed)
.filter(([, value]) => typeof value === 'string' || typeof value === 'number')
.map(([key, value]) => `${key.replace(/_/g, ' ')}: ${String(value)}`)
.join(' · ');
} catch {
return undefined;
}
};

const Failure = ({ session }: { session: Session }) => {
const { retry } = useAppContext();
const failure = useSyncExternalStore(
Expand All @@ -40,48 +24,16 @@ const Failure = ({ session }: { session: Session }) => {
);
};

const Requests = ({ session }: { session: Session }) => {
const approvals = useSyncExternalStore(
session.conversation.subscribe,
() => session.conversation.get().approvals ?? NONE,
);
if (approvals.length === 0) return null;
return (
<View style={styles.container}>
{approvals.map((request) => (
<AIToolApprovalCard
approval={{
allowTitle: request.approval.allowTitle,
message: request.approval.message,
reason: request.reason || detailsOf(request),
title: request.approval.title,
}}
key={request.id}
onDecide={(allowed) => session.decide(request.id, { allowed })}
/>
))}
</View>
);
};

/**
* What the open conversation's session needs the person for, above the composer: why the agent
* couldn't answer, with a way to ask again, and the calls of the phone's tools that ask first. The
* phone asks through `responses.create`, so those wait on its session rather than on the reply's
* step, and the person answers them here.
* Why the open conversation's agent couldn't answer, above the composer, with a way to ask again.
* A call that asks first is asked on its step in the conversation, with the approver.
*/
export const AgentNotices = ({ chat }: { chat: Chat }) => {
const { session } = useStateStore(chatStore(chat), sessionSelector);
return session ? (
<>
<Failure session={session} />
<Requests session={session} />
</>
) : null;
return session ? <Failure session={session} /> : null;
};

const styles = StyleSheet.create({
container: { gap: 8, paddingBottom: 8, paddingHorizontal: 16 },
failure: { alignItems: 'center', paddingBottom: 8 },
failureText: { color: '#B00020', fontSize: 14 },
retryText: { color: '#C8102E', fontSize: 15, fontWeight: '700', paddingTop: 2 },
Expand Down
Loading