|
21 | 21 | @ApplicationScoped |
22 | 22 | public final class CurrencyAgentExecutorProducer { |
23 | 23 |
|
24 | | - private final CurrencyAgent currencyAgent; |
| 24 | + private final CurrencyAgent currencyAgent; |
25 | 25 |
|
26 | | - public CurrencyAgentExecutorProducer(CurrencyAgent currencyAgent) { |
27 | | - this.currencyAgent = currencyAgent; |
28 | | - } |
29 | | - |
30 | | - public CurrencyAgent getCurrencyAgent() { |
31 | | - return currencyAgent; |
32 | | - } |
33 | | - |
34 | | - @Produces |
35 | | - public AgentExecutor agentExecutor() { |
36 | | - return new CurrencyAgentExecutor(getCurrencyAgent()); |
37 | | - } |
38 | | - |
39 | | - private class CurrencyAgentExecutor implements AgentExecutor { |
40 | | - private final CurrencyAgent agent; |
41 | | - |
42 | | - public CurrencyAgentExecutor(CurrencyAgent currencyAgent) { |
43 | | - this.agent = currencyAgent; |
44 | | - } |
45 | | - |
46 | | - @Override |
47 | | - public void execute(RequestContext context, EventQueue eventQueue) throws JSONRPCError { |
48 | | -// executeSimple(context, eventQueue); |
49 | | - executeLoop(context, eventQueue); |
50 | | - } |
| 26 | + public CurrencyAgentExecutorProducer(CurrencyAgent currencyAgent) { |
| 27 | + this.currencyAgent = currencyAgent; |
| 28 | + } |
51 | 29 |
|
52 | | - void executeLoop(RequestContext context, EventQueue eventQueue) { |
53 | | - var updater = new TaskUpdater(context, eventQueue); |
54 | | - if (context.getTask() == null) { |
55 | | - // Initial message - create task in SUBMITTED → WORKING state |
56 | | - updater.submit(); |
57 | | - updater.startWork(); |
58 | | - |
59 | | - getResponse(context, updater); |
60 | | - } else { |
61 | | - // Subsequent messages - add artifacts |
62 | | - getResponse(context, updater); |
63 | | - } |
64 | | - } |
65 | | - |
66 | | - private void getResponse(RequestContext context, TaskUpdater updater) { |
67 | | - // extract the text from the message |
68 | | - var message = extractTextFromMessage(context.getMessage()); |
69 | | - |
70 | | - // call the currency agent with the message |
71 | | - ResponseFormat response = agent.handleRequest(message); |
72 | | - System.out.printf("Response: %s %n", response); |
73 | | - |
74 | | - // create the response part |
75 | | - TextPart responsePart = new TextPart(response.message(), null); |
76 | | - List<Part<?>> parts = List.of(responsePart); |
77 | | - |
78 | | - // add the response as an artifact |
79 | | - updater.addArtifact(parts); |
80 | | - switch (response.status()) { |
81 | | - case INPUT_REQUIRED -> updater.requiresInput(true); |
82 | | - case COMPLETED -> updater.complete(); |
83 | | - case ERROR -> updater.fail(); |
84 | | - } |
85 | | - } |
| 30 | + public CurrencyAgent getCurrencyAgent() { |
| 31 | + return currencyAgent; |
| 32 | + } |
86 | 33 |
|
87 | | - void executeSimple(RequestContext context, EventQueue eventQueue) throws JSONRPCError { |
88 | | - var updater = new TaskUpdater(context, eventQueue); |
| 34 | + @Produces |
| 35 | + public AgentExecutor agentExecutor() { |
| 36 | + return new CurrencyAgentExecutor(getCurrencyAgent()); |
| 37 | + } |
89 | 38 |
|
90 | | - // mark the task as submitted and start working on it |
91 | | - if (context.getTask() == null) { |
92 | | - updater.submit(); |
93 | | - } |
94 | | - updater.startWork(); |
| 39 | + private class CurrencyAgentExecutor implements AgentExecutor { |
| 40 | + private final CurrencyAgent agent; |
95 | 41 |
|
96 | | - // extract the text from the message |
97 | | - var message = extractTextFromMessage(context.getMessage()); |
| 42 | + public CurrencyAgentExecutor(CurrencyAgent currencyAgent) { |
| 43 | + this.agent = currencyAgent; |
| 44 | + } |
98 | 45 |
|
99 | | - // call the currency agent with the message |
100 | | - ResponseFormat response = agent.handleRequest(message); |
101 | | - System.out.printf("Response: %s %n", response); |
| 46 | + @Override |
| 47 | + public void execute(RequestContext context, EventQueue eventQueue) throws JSONRPCError { |
| 48 | + executeLoop(context, eventQueue); |
| 49 | + } |
102 | 50 |
|
103 | | - // create the response part |
104 | | - TextPart responsePart = new TextPart(response.message(), null); |
105 | | - List<Part<?>> parts = List.of(responsePart); |
| 51 | + void executeLoop(RequestContext context, EventQueue eventQueue) { |
| 52 | + var updater = new TaskUpdater(context, eventQueue); |
| 53 | + if (context.getTask() == null) { |
| 54 | + // Initial message - create task in SUBMITTED → WORKING state |
| 55 | + updater.submit(); |
| 56 | + updater.startWork(); |
| 57 | + |
| 58 | + getResponse(context, updater); |
| 59 | + } else { |
| 60 | + // Subsequent messages - add artifacts |
| 61 | + getResponse(context, updater); |
| 62 | + } |
| 63 | + } |
106 | 64 |
|
107 | | - // add the response as an artifact and complete the task |
108 | | - updater.addArtifact(parts, null, null, null); |
109 | | - updater.complete(); |
110 | | - } |
| 65 | + private void getResponse(RequestContext context, TaskUpdater updater) { |
| 66 | + // extract the text from the message |
| 67 | + var message = extractTextFromMessage(context.getMessage()); |
| 68 | + |
| 69 | + // call the currency agent with the message |
| 70 | + ResponseFormat response = agent.handleRequest(message); |
| 71 | + System.out.printf("Response: %s %n", response); |
| 72 | + |
| 73 | + // create the response part |
| 74 | + TextPart responsePart = new TextPart(response.message(), null); |
| 75 | + List<Part<?>> parts = List.of(responsePart); |
| 76 | + |
| 77 | + // add the response as an artifact |
| 78 | + updater.addArtifact(parts); |
| 79 | + switch (response.status()) { |
| 80 | + case INPUT_REQUIRED -> updater.requiresInput(true); |
| 81 | + case COMPLETED -> updater.complete(); |
| 82 | + case ERROR -> updater.fail(); |
| 83 | + } |
| 84 | + } |
111 | 85 |
|
112 | | - private String extractTextFromMessage(final Message message) { |
113 | | - final StringBuilder builder = new StringBuilder(); |
114 | | - if (message.getParts() != null) { |
115 | | - for (final Part<?> part : message.getParts()) { |
116 | | - if (part instanceof TextPart textPart) { |
117 | | - builder.append(textPart.getText()); |
118 | | - } |
119 | | - } |
120 | | - } |
121 | | - return builder.toString(); |
| 86 | + private String extractTextFromMessage(final Message message) { |
| 87 | + final StringBuilder builder = new StringBuilder(); |
| 88 | + if (message.getParts() != null) { |
| 89 | + for (final Part<?> part : message.getParts()) { |
| 90 | + if (part instanceof TextPart textPart) { |
| 91 | + builder.append(textPart.getText()); |
| 92 | + } |
122 | 93 | } |
| 94 | + } |
| 95 | + return builder.toString(); |
| 96 | + } |
123 | 97 |
|
124 | | - @Override |
125 | | - public void cancel(RequestContext context, EventQueue eventQueue) throws JSONRPCError { |
126 | | - var task = context.getTask(); |
| 98 | + @Override |
| 99 | + public void cancel(RequestContext context, EventQueue eventQueue) throws JSONRPCError { |
| 100 | + var task = context.getTask(); |
127 | 101 |
|
128 | | - if (task.getStatus().state() == TaskState.CANCELED || task.getStatus().state() == TaskState.COMPLETED) { |
129 | | - // task already canceled or completed |
130 | | - throw new TaskNotCancelableError(); |
131 | | - } |
| 102 | + if (task.getStatus().state() == TaskState.CANCELED || task.getStatus().state() == TaskState.COMPLETED) { |
| 103 | + // task already canceled or completed |
| 104 | + throw new TaskNotCancelableError(); |
| 105 | + } |
132 | 106 |
|
133 | | - var updater = new TaskUpdater(context, eventQueue); |
134 | | - updater.cancel(); |
135 | | - } |
| 107 | + var updater = new TaskUpdater(context, eventQueue); |
| 108 | + updater.cancel(); |
136 | 109 | } |
| 110 | + } |
137 | 111 | } |
0 commit comments