-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPromptBlock.java
More file actions
173 lines (149 loc) · 7.36 KB
/
Copy pathPromptBlock.java
File metadata and controls
173 lines (149 loc) · 7.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package rsp.compositions.agentui;
import rsp.component.ComponentStateSupplier;
import rsp.component.ComponentView;
import rsp.component.EventKey;
import rsp.component.StateUpdater;
import rsp.compositions.agent.ActionDispatcher;
import rsp.compositions.agent.AgentFeedback;
import rsp.compositions.agent.AgentRuntime;
import rsp.compositions.agent.AgentService;
import rsp.compositions.agent.AgentSpawner;
import rsp.compositions.agent.LoopPolicy;
import rsp.compositions.authorization.Authorization;
import rsp.compositions.composition.StructureNode;
import rsp.compositions.block.Block;
import rsp.compositions.block.EventKeys;
import rsp.compositions.block.Scene;
import rsp.page.QualifiedSessionId;
import java.util.Objects;
/**
* Thin IO/UI shell over {@link AgentRuntime}: wires the chat surface
* ({@link PromptService}) to the runtime and forwards lifecycle events.
* <p>
* All orchestration (LLM invocation, authorization, dispatch, plan execution)
* lives in the runtime. This block owns:
* <ul>
* <li>chat IO: SEND_PROMPT and PromptService update wiring</li>
* <li>scope key derivation from {@link QualifiedSessionId}</li>
* <li>active-category state updates from the mounted context watch</li>
* <li>scene push to the runtime through a mounted context watch</li>
* <li>delegation-approval forwarding to the runtime</li>
* </ul>
*/
public class PromptBlock extends Block<PromptView.PromptViewState, PromptView.PromptIntent> {
private final System.Logger logger = System.getLogger(getClass().getName());
public record Message(long id, String text, boolean fromUser) {}
public static final EventKey.SimpleKey<String> SEND_PROMPT =
new EventKey.SimpleKey<>("prompt.send", String.class);
private Runnable serviceUnsubscribe;
private String scopeKey;
private final PromptService promptService;
private AgentRuntime runtime;
private volatile String activeCategory = "";
public PromptBlock(PromptService promptService,
AgentService agentService, ActionDispatcher dispatcher,
Authorization authorization, AgentSpawner spawner,
StructureNode structure) {
this.promptService = Objects.requireNonNull(promptService);
this.agentService = Objects.requireNonNull(agentService);
this.dispatcher = Objects.requireNonNull(dispatcher);
this.authorization = Objects.requireNonNull(authorization);
this.spawner = Objects.requireNonNull(spawner);
this.structure = Objects.requireNonNull(structure);
}
private final AgentService agentService;
private final ActionDispatcher dispatcher;
private final Authorization authorization;
private final AgentSpawner spawner;
private final StructureNode structure;
@Override
public ComponentStateSupplier<PromptView.PromptViewState> initStateSupplier() {
return (_, context) -> {
QualifiedSessionId sessionId = context.get(QualifiedSessionId.class);
String key = sessionId != null ? sessionId.sessionId() : "unknown-session";
String category = normalizeCategory(context.get(rsp.compositions.block.ContextKeys.PRIMARY_CATEGORY_KEY));
return new PromptView.PromptViewState(promptService.getMessageHistory(key).stream()
.map(message -> new Message(message.id(), message.text(), message.fromUser()))
.toList(), category);
};
}
@Override
public ComponentView<PromptView.PromptViewState, PromptView.PromptIntent> componentView() {
return new PromptView();
}
@Override
protected void onBlockMounted(PromptView.PromptViewState state,
StateUpdater<PromptView.PromptViewState> stateUpdate) {
QualifiedSessionId sessionId = lookup().get(QualifiedSessionId.class);
scopeKey = sessionId != null ? sessionId.sessionId() : "unknown-session";
activeCategory = state.activeCategory();
AgentFeedback feedback = new AgentFeedback() {
@Override public void send(String message) {
promptService.sendReply(scopeKey, message);
}
@Override public void updateLast(String message) {
promptService.updateLastReply(scopeKey, message);
}
};
this.runtime = new AgentRuntime(agentService, dispatcher, spawner,
authorization, structure, lookup(), feedback,
DelegationApprovalBlock.class, LoopPolicy.DEFAULT, scopeKey);
subscribe(SEND_PROMPT, (_, text) -> submit(text, stateUpdate));
subscribe(DelegationApprovalBlock.APPROVAL_DECIDED, (eventName, approved) ->
runtime.onApprovalDecided(approved));
subscribe(EventKeys.PRIMARY_BLOCK_MOUNTED, (_, mounted) ->
runtime.onPrimaryBlockMounted(mounted));
watch(rsp.compositions.block.ContextKeys.PRIMARY_CATEGORY_KEY, category -> {
activeCategory = normalizeCategory(category);
stateUpdate.applyStateTransformation(current -> current.withActiveCategory(activeCategory));
});
watch(rsp.compositions.block.ContextKeys.SCENE, (_, scene) -> runtime.onScene(scene));
runtime.onScene(lookup().get(rsp.compositions.block.ContextKeys.SCENE));
serviceUnsubscribe = promptService.subscribe(scopeKey, message -> {
Message msg = new Message(message.id(), message.text(), message.fromUser());
logger.log(System.Logger.Level.DEBUG,
() -> String.format("Prompt message bridged [update=%s, messageId=%d, fromUser=%s]",
message.update(), msg.id(), msg.fromUser()));
if (message.update()) {
stateUpdate.applyStateTransformation(current -> current.withLastSystemMessageUpdated(msg.text()));
} else {
stateUpdate.applyStateTransformation(current -> current.withMessage(msg));
}
});
logger.log(System.Logger.Level.DEBUG, "Prompt block created");
}
@Override
public String title() {
return "Prompt";
}
@Override
protected void onIntent(PromptView.PromptIntent intent,
PromptView.PromptViewState state,
StateUpdater<PromptView.PromptViewState> stateUpdate) {
lookup().publish(SEND_PROMPT, intent.text());
}
private static String normalizeCategory(String category) {
return category != null ? category : "";
}
@Override
public void onUnmounted(rsp.component.ComponentCompositeKey componentId, PromptView.PromptViewState state) {
super.onUnmounted(componentId, state);
boolean hadBridge = serviceUnsubscribe != null;
if (serviceUnsubscribe != null) {
serviceUnsubscribe.run();
serviceUnsubscribe = null;
}
logger.log(System.Logger.Level.DEBUG,
() -> String.format("Prompt block destroyed [bridgeUnsubscribed=%s]", hadBridge));
runtime = null;
scopeKey = null;
}
private void submit(String text, StateUpdater<PromptView.PromptViewState> stateUpdate) {
if (text == null || text.isBlank() || runtime == null) {
return;
}
stateUpdate.applyStateTransformation(current -> current.withOptimisticMessage(text));
promptService.sendPrompt(scopeKey, text);
runtime.submit(text);
}
}