1
1
import fs from 'fs' ;
2
2
import { AIProjectsClient , AgentThreadOutput , DoneEvent , ErrorEvent , MessageStreamEvent , RunStreamEvent , isOutputOfType } from '@azure/ai-projects' ;
3
- import type { AgentOutput , MessageDeltaChunk , MessageDeltaTextContent , MessageImageFileContentOutput , MessageTextContentOutput , OpenAIPageableListOfThreadMessageOutput , ThreadRunOutput } from '@azure/ai-projects' ;
3
+ import type { AgentOutput , FunctionToolDefinitionOutput , MessageDeltaChunk , MessageDeltaTextContent , MessageImageFileContentOutput , MessageTextContentOutput , OpenAIPageableListOfThreadMessageOutput , SubmitToolOutputsActionOutput , ThreadRunOutput , ToolOutput } from '@azure/ai-projects' ;
4
4
import { PromptConfig } from '../types.js' ;
5
5
6
6
export async function addMessageToThread ( client : AIProjectsClient , threadId : string , message : string ) {
@@ -18,10 +18,18 @@ export async function printThreadMessages(selectedPromptConfig: PromptConfig, cl
18
18
const messagesArray = messages . data ;
19
19
for ( let i = messagesArray . length - 1 ; i >= 0 ; i -- ) {
20
20
const m = messagesArray [ i ] ;
21
+ const content = m . content [ 0 ] ;
22
+
23
+ if ( ! content ) {
24
+ // Skip if no content
25
+ continue ;
26
+ }
27
+
21
28
console . log ( `Type: ${ m . content [ 0 ] . type } ` ) ;
22
29
if ( isOutputOfType < MessageTextContentOutput > ( m . content [ 0 ] , 'text' ) ) {
23
30
const textContent = m . content [ 0 ] as MessageTextContentOutput ;
24
- console . log ( `Text: ${ textContent . text . value } ` ) ;
31
+ const role = m . role === 'user' ? 'User' : 'Agent' ;
32
+ console . log ( `${ role } : ${ textContent . text . value } ` ) ;
25
33
}
26
34
}
27
35
@@ -77,7 +85,7 @@ export async function getRunStats(runId: string, client: AIProjectsClient, threa
77
85
}
78
86
}
79
87
80
- export async function runAgent ( client : AIProjectsClient , thread : AgentThreadOutput , agent : AgentOutput ) : Promise < string > {
88
+ export async function runAgent ( client : AIProjectsClient , thread : AgentThreadOutput , agent : AgentOutput , promptConfig : PromptConfig ) : Promise < string > {
81
89
const run = client . agents . createRun ( thread . id , agent . id ) ;
82
90
const streamEventMessages = await run . stream ( ) ;
83
91
let runId = '' ;
@@ -104,8 +112,16 @@ export async function runAgent(client: AIProjectsClient, thread: AgentThreadOutp
104
112
}
105
113
break ;
106
114
115
+ case RunStreamEvent . ThreadRunRequiresAction :
116
+ console . log ( "\nThread run requires action." ) ;
117
+ const run = eventMessage . data as ThreadRunOutput ;
118
+ if ( run . requiredAction ) {
119
+ await processRequiredAction ( client , thread , run , promptConfig ) ;
120
+ }
121
+ break ;
122
+
107
123
case RunStreamEvent . ThreadRunCompleted :
108
- console . log ( ' \nThread run completed.' ) ;
124
+ console . log ( " \nThread run completed." ) ;
109
125
break ;
110
126
111
127
case ErrorEvent . Error :
@@ -119,4 +135,42 @@ export async function runAgent(client: AIProjectsClient, thread: AgentThreadOutp
119
135
}
120
136
121
137
return runId ;
122
- }
138
+ }
139
+
140
+ async function processRequiredAction (
141
+ client : AIProjectsClient ,
142
+ thread : AgentThreadOutput ,
143
+ run : ThreadRunOutput ,
144
+ promptConfig : PromptConfig
145
+ ) {
146
+ if (
147
+ run . requiredAction &&
148
+ isOutputOfType < SubmitToolOutputsActionOutput > (
149
+ run . requiredAction ,
150
+ "submit_tool_outputs"
151
+ )
152
+ ) {
153
+ const submitToolOutputsActionOutput =
154
+ run . requiredAction as SubmitToolOutputsActionOutput ;
155
+ const toolCalls = submitToolOutputsActionOutput . submitToolOutputs . toolCalls ;
156
+ const toolResponses : ToolOutput [ ] = [ ] ;
157
+ for ( const toolCall of toolCalls ) {
158
+ if ( isOutputOfType < FunctionToolDefinitionOutput > ( toolCall , "function" ) ) {
159
+ const toolResponse = promptConfig . executor ?. invokeTool ( toolCall ) as ToolOutput ;
160
+ console . log (
161
+ `💡 Function tool ${ toolCall . id } - ${ toolResponse . output } `
162
+ ) ;
163
+ if ( toolResponse ) {
164
+ toolResponses . push ( toolResponse ) ;
165
+ }
166
+ }
167
+ }
168
+ if ( toolResponses . length > 0 ) {
169
+ run = await client . agents . submitToolOutputsToRun (
170
+ thread . id ,
171
+ run . id ,
172
+ toolResponses
173
+ ) ;
174
+ }
175
+ }
176
+ }
0 commit comments