( taskLoopParams: TaskLoopParams<TResponseFormat> )
| 19 | import { processAgentFinalResponse } from "./finalResponse"; |
| 20 | |
| 21 | export async function runTaskLoop< |
| 22 | TResponseFormat extends "text" | z.ZodType<any> = "text", |
| 23 | TResponse = InferResponseType<TResponseFormat>, |
| 24 | >( |
| 25 | taskLoopParams: TaskLoopParams<TResponseFormat> |
| 26 | ): Promise<AgentResponse<TResponse>> { |
| 27 | const { |
| 28 | actions, |
| 29 | model, |
| 30 | input, |
| 31 | externalCustomerId, |
| 32 | maxSteps = 10, |
| 33 | instructions, |
| 34 | debug, |
| 35 | sessionId = uuidv4(), |
| 36 | agentId, |
| 37 | spinApiKey, |
| 38 | state = {}, |
| 39 | responseFormat = "text", |
| 40 | customLoggingEndpoint, |
| 41 | messages: initialMessages = [], |
| 42 | fixedSteps, |
| 43 | } = taskLoopParams; |
| 44 | const modelId = model.modelId; |
| 45 | const modelProvider = model.provider; |
| 46 | const interactionId = uuidv4(); |
| 47 | |
| 48 | // Set debug logging based on parameter |
| 49 | setDebugEnabled(debug ?? "default"); |
| 50 | |
| 51 | // Track total cost |
| 52 | let totalCostCents = 0; |
| 53 | let totalPromptTokens = 0; |
| 54 | let totalCompletionTokens = 0; |
| 55 | |
| 56 | // Create system message |
| 57 | const systemMessage = await createSystemMessage(instructions, actions); |
| 58 | const userMessage = await createUserMessage(input); |
| 59 | // Initialize messages array with system message |
| 60 | const messages: Message[] = |
| 61 | initialMessages.length === 0 |
| 62 | ? [systemMessage, userMessage] |
| 63 | : [...initialMessages, userMessage]; |
| 64 | |
| 65 | log(`Starting interaction with ${debug ?? "default"} logging`, { |
| 66 | type: "summary", |
| 67 | }); |
| 68 | |
| 69 | // Initialize logging service |
| 70 | const taskStartTime = Date.now(); |
| 71 | |
| 72 | const logger = new LoggingService({ |
| 73 | agentId: agentId, |
| 74 | spinApiKey: spinApiKey, |
| 75 | sessionId, |
| 76 | interactionId, |
| 77 | modelId, |
| 78 | modelProvider, |
no test coverage detected