| 78 | } |
| 79 | |
| 80 | async execute(context: RequestContext, eventQueue: EventQueue): Promise<void> { |
| 81 | const task = context.currentTask; |
| 82 | |
| 83 | if (!task) { |
| 84 | throw new Error('No task found in context'); |
| 85 | } |
| 86 | console.log('✅ Payment verified, processing request...'); |
| 87 | |
| 88 | // Extract user message from the context |
| 89 | const userMessage = context.message?.parts |
| 90 | ?.filter((part: any) => part.kind === 'text') |
| 91 | .map((part: any) => part.text) |
| 92 | .join(' ') || 'Hello'; |
| 93 | |
| 94 | console.log(`📝 User request: ${userMessage}`); |
| 95 | |
| 96 | try { |
| 97 | // Call OpenAI API to process the request |
| 98 | // REPLACE THIS with your own service logic |
| 99 | const completion = await this.openai.chat.completions.create({ |
| 100 | model: this.model, |
| 101 | messages: [ |
| 102 | { |
| 103 | role: 'system', |
| 104 | content: 'You are a helpful AI assistant. Provide concise and accurate responses.', |
| 105 | }, |
| 106 | { |
| 107 | role: 'user', |
| 108 | content: userMessage, |
| 109 | }, |
| 110 | ], |
| 111 | temperature: this.temperature, |
| 112 | max_tokens: this.maxTokens, |
| 113 | ...(this.provider === 'eigenai' && this.seed !== undefined |
| 114 | ? { seed: this.seed } |
| 115 | : {}), |
| 116 | }); |
| 117 | |
| 118 | const response = completion.choices[0]?.message?.content || 'No response generated'; |
| 119 | |
| 120 | console.log(`🤖 Service response: ${response}`); |
| 121 | |
| 122 | // Update task with the response |
| 123 | task.status.state = TaskState.COMPLETED; |
| 124 | task.status.message = { |
| 125 | messageId: `msg-${Date.now()}`, |
| 126 | role: 'agent', |
| 127 | parts: [ |
| 128 | { |
| 129 | kind: 'text', |
| 130 | text: response, |
| 131 | }, |
| 132 | ], |
| 133 | }; |
| 134 | |
| 135 | // Enqueue the completed task |
| 136 | await eventQueue.enqueueEvent(task); |
| 137 | |