feat: relay harness.balance to watch + fix to harness.prompt/cancel

This commit is contained in:
2026-09-01 21:13:26 +08:00
parent 93267ae8cf
commit 6ec6067c33
@@ -8,6 +8,7 @@ import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
@@ -15,6 +16,7 @@ public class MainActivity extends AppCompatActivity {
private TextView status; private TextView status;
private ServerClient serverClient; private ServerClient serverClient;
private WatchBridge watchBridge; private WatchBridge watchBridge;
private boolean balancePending = false;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@@ -33,28 +35,49 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
public void onHello(JSONObject hello) { public void onHello(JSONObject hello) {
serverClient.sendRequest("harness.status", new JSONObject()); serverClient.sendRequest("harness.status", new JSONObject());
balancePending = true;
serverClient.sendRequest("harness.balance", new JSONObject());
} }
@Override @Override
public void onStatus(JSONObject s) { public void onStatus(JSONObject s) {
if (s != null) { if (s != null) {
runOnUiThread(() -> status.setText("状态: " + s.optString("model") + " / " + s.optString("cwd"))); runOnUiThread(() -> status.setText("状态: " + s.optString("model") + " / " + s.optString("cwd")));
} JSONObject payload = new JSONObject();
if (watchBridge != null) { payload.put("model", s.optString("model"));
watchBridge.sendToWatch(s.toString()); payload.put("running", s.optBoolean("running", false));
payload.put("activeSessionId", s.optString("activeSessionId"));
JSONObject msg = new JSONObject();
msg.put("type", "status");
msg.put("payload", payload);
if (watchBridge != null) watchBridge.sendToWatch(msg.toString());
} }
} }
@Override @Override
public void onEvent(JSONObject event) { public void onEvent(JSONObject event) {
if (watchBridge != null) { JSONObject msg = new JSONObject();
watchBridge.sendToWatch(event.toString()); try { msg.put("type", "event"); msg.put("payload", event); } catch (Exception e) {}
} if (watchBridge != null) watchBridge.sendToWatch(msg.toString());
} }
@Override @Override
public void onResponse(String requestId, boolean ok, JSONObject result, JSONObject error) { public void onResponse(String requestId, boolean ok, JSONObject result, JSONObject error) {
runOnUiThread(() -> status.setText(ok ? "响应 OK" : "错误: " + (error != null ? error.optString("message") : ""))); if (balancePending && ok && result != null && result.has("balance_infos")) {
balancePending = false;
JSONArray infos = result.optJSONArray("balance_infos");
double total = (infos != null && infos.length() > 0 && infos.optJSONObject(0) != null)
? infos.optJSONObject(0).optDouble("total_balance", 0.0) : 0.0;
String balance = String.format("%.2f", total);
runOnUiThread(() -> status.setText("余额 ¥ " + balance));
JSONObject payload = new JSONObject();
try { payload.put("balance", balance); } catch (Exception e) {}
JSONObject msg = new JSONObject();
try { msg.put("type", "status"); msg.put("payload", payload); } catch (Exception e) {}
if (watchBridge != null) watchBridge.sendToWatch(msg.toString());
} else {
runOnUiThread(() -> status.setText(ok ? "响应 OK" : "错误: " + (error != null ? error.optString("message") : "")));
}
} }
@Override @Override
@@ -89,10 +112,10 @@ public class MainActivity extends AppCompatActivity {
serverClient.sendRequest("harness.cancel", params); serverClient.sendRequest("harness.cancel", params);
} else { } else {
params.put("content", content); params.put("content", content);
serverClient.sendRequest("session.prompt", params); serverClient.sendRequest("harness.prompt", params);
} }
} else if ("cancel".equals(type)) { } else if ("cancel".equals(type)) {
serverClient.sendRequest("session.cancel", params); serverClient.sendRequest("harness.cancel", params);
} }
} catch (Exception e) { } catch (Exception e) {
// ignore malformed // ignore malformed