Skip to content

Commit 886d471

Browse files
committed
Make it work
1 parent 0b82ac4 commit 886d471

File tree

3 files changed

+164
-13
lines changed

3 files changed

+164
-13
lines changed

dist/index.js

Lines changed: 74 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export async function run(): Promise<void> {
7070
const enableMcp = core.getBooleanInput('enable-mcp') || false
7171

7272
let azureTools: any[] = []
73+
let mcp: Client | null = null
7374

7475
// Connect to MCP server if enabled
7576
if (enableMcp || true) {
@@ -86,7 +87,7 @@ export async function run(): Promise<void> {
8687
}
8788
)
8889

89-
const mcp = new Client({
90+
mcp = new Client({
9091
name: 'ai-inference-action',
9192
version: '1.0.0',
9293
transport
@@ -153,21 +154,103 @@ export async function run(): Promise<void> {
153154
)
154155
}
155156

156-
const modelResponse: string | null =
157+
let modelResponse: string | null =
157158
response.body.choices[0].message.content
158159

159160
core.info(`Model response: ${response || 'No response content'}`)
160161

161162
// Handle tool calls if present
162163
const toolCalls = response.body.choices[0].message.tool_calls
163-
if (toolCalls && toolCalls.length > 0) {
164+
if (toolCalls && toolCalls.length > 0 && mcp) {
164165
core.info(`Model requested ${toolCalls.length} tool calls`)
165-
// Note: For now, we'll just log the tool calls
166-
// In a full implementation, you'd execute them via MCP and continue the conversation
166+
167+
// Execute tool calls via MCP and continue the conversation
168+
const toolResults: any[] = []
169+
167170
for (const toolCall of toolCalls) {
168171
core.info(
169-
`Tool call: ${toolCall.function.name} with args: ${toolCall.function.arguments}`
172+
`Executing tool: ${toolCall.function.name} with args: ${toolCall.function.arguments}`
170173
)
174+
175+
try {
176+
// Parse the arguments from JSON string
177+
const args = JSON.parse(toolCall.function.arguments)
178+
179+
// Call the tool via MCP
180+
const result = await mcp.callTool({
181+
name: toolCall.function.name,
182+
arguments: args
183+
})
184+
185+
core.info(`Tool ${toolCall.function.name} executed successfully`)
186+
187+
// Store the result for the follow-up conversation
188+
toolResults.push({
189+
tool_call_id: toolCall.id,
190+
role: 'tool',
191+
name: toolCall.function.name,
192+
content: JSON.stringify(result.content)
193+
})
194+
195+
} catch (toolError) {
196+
core.warning(`Failed to execute tool ${toolCall.function.name}: ${toolError}`)
197+
198+
// Add error result to continue conversation
199+
toolResults.push({
200+
tool_call_id: toolCall.id,
201+
role: 'tool',
202+
name: toolCall.function.name,
203+
content: `Error: ${toolError}`
204+
})
205+
}
206+
}
207+
208+
// If we have tool results, continue the conversation
209+
if (toolResults.length > 0) {
210+
core.info('Continuing conversation with tool results...')
211+
212+
// Build the follow-up request with the original conversation + tool results
213+
const followUpMessages = [
214+
{
215+
role: 'system',
216+
content: systemPrompt
217+
},
218+
{ role: 'user', content: prompt },
219+
{
220+
role: 'assistant',
221+
content: modelResponse,
222+
tool_calls: toolCalls
223+
},
224+
...toolResults
225+
]
226+
227+
const followUpRequest: any = {
228+
messages: followUpMessages,
229+
max_tokens: maxTokens,
230+
model: modelName
231+
}
232+
233+
// Add tools again for potential follow-up tool calls
234+
if (azureTools.length > 0) {
235+
followUpRequest.tools = azureTools
236+
}
237+
238+
const followUpResponse = await client.path('/chat/completions').post({
239+
body: followUpRequest
240+
})
241+
242+
if (isUnexpected(followUpResponse)) {
243+
core.warning(
244+
'Failed to get follow-up response after tool execution: ' +
245+
followUpResponse.status + ': ' + followUpResponse.body
246+
)
247+
} else {
248+
const finalResponse = followUpResponse.body.choices[0].message.content
249+
core.info(`Final response after tool execution: ${finalResponse}`)
250+
251+
// Update the model response to the final one
252+
modelResponse = finalResponse || modelResponse
253+
}
171254
}
172255
}
173256

0 commit comments

Comments
 (0)