import { IllaSDK } from '@illalabs/sdk'
import {
IllaToolError,
IllaToolOutcome,
type IllaToolOutcomeJSON,
type PendingToolCallType,
} from '@illalabs/interfaces'
const sdk = new IllaSDK({ apiKey: process.env.ILLA_API_KEY! })
async function executePendingTool(tool: PendingToolCallType): Promise<IllaToolOutcomeJSON> {
try {
// Your executor should handle:
// SingleTransaction / BatchTransactions / SignatureRequests / ActionSequence
const executionResult = await walletExecutor.execute(tool.input)
return IllaToolOutcome
.success(tool.toolCallId, tool.toolName, executionResult)
.toJSON()
} catch (error) {
return IllaToolOutcome
.error(
tool.toolCallId,
tool.toolName,
IllaToolError.execution(
tool.toolCallId,
tool.toolName,
error instanceof Error ? error.message : 'Unknown execution error',
),
)
.toJSON()
}
}
async function runToolLoop(
chatId: string,
initialTools: ReadonlyArray<PendingToolCallType>,
): Promise<void> {
let pendingTools = [...initialTools]
while (pendingTools.length > 0) {
const outcomes = await Promise.all(pendingTools.map(executePendingTool))
const followUp = await sdk.sendToolResults(chatId, outcomes)
if (followUp.response.isError) {
throw new Error(followUp.response.error.message)
}
pendingTools = [...(followUp.response.data.pendingTools ?? [])]
}
}