|
| 1 | +import { Agent, RunContext, handoff, run, withTrace } from '@openai/agents'; |
| 2 | +import { createInterface } from 'node:readline/promises'; |
| 3 | + |
| 4 | +const introduction = |
| 5 | + `Handoffs with Conditional Enabling\n\n` + |
| 6 | + 'This routes conversations to language specialists based on user preferences.\n'; |
| 7 | + |
| 8 | +type LanguagePreference = 'spanish_only' | 'french_spanish' | 'european'; |
| 9 | + |
| 10 | +type AppContext = { |
| 11 | + languagePreference: LanguagePreference; |
| 12 | +}; |
| 13 | + |
| 14 | +function frenchSpanishEnabled({ |
| 15 | + runContext, |
| 16 | +}: { |
| 17 | + runContext: RunContext<AppContext>; |
| 18 | +}) { |
| 19 | + return ( |
| 20 | + runContext.context.languagePreference === 'french_spanish' || |
| 21 | + runContext.context.languagePreference === 'european' |
| 22 | + ); |
| 23 | +} |
| 24 | + |
| 25 | +function europeanEnabled({ |
| 26 | + runContext, |
| 27 | +}: { |
| 28 | + runContext: RunContext<AppContext>; |
| 29 | +}) { |
| 30 | + return runContext.context.languagePreference === 'european'; |
| 31 | +} |
| 32 | + |
| 33 | +const spanishAgent = new Agent<AppContext>({ |
| 34 | + name: 'spanish_agent', |
| 35 | + instructions: |
| 36 | + "You respond in Spanish. Always reply to the user's question in Spanish.", |
| 37 | + handoffDescription: 'Spanish speaking specialist.', |
| 38 | +}); |
| 39 | + |
| 40 | +const frenchAgent = new Agent<AppContext>({ |
| 41 | + name: 'french_agent', |
| 42 | + instructions: |
| 43 | + "You respond in French. Always reply to the user's question in French.", |
| 44 | + handoffDescription: 'French speaking specialist.', |
| 45 | +}); |
| 46 | + |
| 47 | +const italianAgent = new Agent<AppContext>({ |
| 48 | + name: 'italian_agent', |
| 49 | + instructions: |
| 50 | + "You respond in Italian. Always reply to the user's question in Italian.", |
| 51 | + handoffDescription: 'Italian speaking specialist.', |
| 52 | +}); |
| 53 | + |
| 54 | +const spanishHandoff = handoff(spanishAgent, { |
| 55 | + toolDescriptionOverride: |
| 56 | + 'Transfer the conversation to the Spanish specialist.', |
| 57 | +}); |
| 58 | + |
| 59 | +const frenchHandoff = handoff(frenchAgent, { |
| 60 | + isEnabled: frenchSpanishEnabled, |
| 61 | + toolDescriptionOverride: |
| 62 | + 'Transfer the conversation to the French specialist.', |
| 63 | +}); |
| 64 | + |
| 65 | +const italianHandoff = handoff(italianAgent, { |
| 66 | + isEnabled: europeanEnabled, |
| 67 | + toolDescriptionOverride: |
| 68 | + 'Transfer the conversation to the Italian specialist.', |
| 69 | +}); |
| 70 | + |
| 71 | +const triageAgent = new Agent<AppContext>({ |
| 72 | + name: 'triage_agent', |
| 73 | + instructions: [ |
| 74 | + 'You triage multilingual conversations.', |
| 75 | + 'When a specialist handoff is available you immediately transfer the conversation.', |
| 76 | + 'If no specialist is available you answer in English and explain the limitation.', |
| 77 | + ].join(' '), |
| 78 | + handoffs: [spanishHandoff, frenchHandoff, italianHandoff], |
| 79 | +}); |
| 80 | + |
| 81 | +async function main() { |
| 82 | + const rl = createInterface({ input: process.stdin, output: process.stdout }); |
| 83 | + |
| 84 | + try { |
| 85 | + console.log(introduction); |
| 86 | + console.log('Choose language preference:'); |
| 87 | + console.log('1. Spanish only (handoff to Spanish agent)'); |
| 88 | + console.log( |
| 89 | + '2. French and Spanish (handoffs to Spanish and French agents)', |
| 90 | + ); |
| 91 | + console.log('3. European languages (handoffs to all three agents)'); |
| 92 | + |
| 93 | + const choice = (await rl.question('\nSelect option (1-3): ')).trim(); |
| 94 | + const preferenceMap: Record<string, LanguagePreference> = { |
| 95 | + '1': 'spanish_only', |
| 96 | + '2': 'french_spanish', |
| 97 | + '3': 'european', |
| 98 | + }; |
| 99 | + const languagePreference = preferenceMap[choice] ?? 'spanish_only'; |
| 100 | + |
| 101 | + const runContext = new RunContext<AppContext>({ languagePreference }); |
| 102 | + const enabledHandoffs = await triageAgent.getEnabledHandoffs(runContext); |
| 103 | + const handoffNames = enabledHandoffs |
| 104 | + .map((handoffItem) => handoffItem.toolName) |
| 105 | + .join(', '); |
| 106 | + |
| 107 | + console.log(`\nLanguage preference: ${languagePreference}`); |
| 108 | + console.log( |
| 109 | + handoffNames |
| 110 | + ? `Available handoffs: ${handoffNames} (the model sees ${enabledHandoffs.length} options).` |
| 111 | + : 'Available handoffs: none.', |
| 112 | + ); |
| 113 | + |
| 114 | + const userRequest = await rl.question( |
| 115 | + '\nAsk a question and see which specialist handles the conversation:\n', |
| 116 | + ); |
| 117 | + |
| 118 | + if (!userRequest.trim()) { |
| 119 | + console.log('No question provided, exiting.'); |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + console.log('\nProcessing request...'); |
| 124 | + await withTrace('Conditional handoff access', async () => { |
| 125 | + const result = await run(triageAgent, userRequest, { |
| 126 | + context: runContext, |
| 127 | + }); |
| 128 | + const finalAgentName = result.lastAgent?.name ?? triageAgent.name; |
| 129 | + console.log(`\nFinal agent: ${finalAgentName}`); |
| 130 | + console.log(`\nResponse:\n${result.finalOutput}`); |
| 131 | + }); |
| 132 | + } finally { |
| 133 | + await rl.close(); |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +if (require.main === module) { |
| 138 | + main().catch((error) => { |
| 139 | + console.error('Error:', error); |
| 140 | + process.exitCode = 1; |
| 141 | + }); |
| 142 | +} |
0 commit comments