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 org.json.JSONArray;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
@@ -15,6 +16,7 @@ public class MainActivity extends AppCompatActivity {
private TextView status;
private ServerClient serverClient;
private WatchBridge watchBridge;
private boolean balancePending = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -33,28 +35,49 @@ public class MainActivity extends AppCompatActivity {
@Override
public void onHello(JSONObject hello) {
serverClient.sendRequest("harness.status", new JSONObject());
balancePending = true;
serverClient.sendRequest("harness.balance", new JSONObject());
}
@Override
public void onStatus(JSONObject s) {
if (s != null) {
runOnUiThread(() -> status.setText("状态: " + s.optString("model") + " / " + s.optString("cwd")));
}
if (watchBridge != null) {
watchBridge.sendToWatch(s.toString());
JSONObject payload = new JSONObject();
payload.put("model", s.optString("model"));
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
public void onEvent(JSONObject event) {
if (watchBridge != null) {
watchBridge.sendToWatch(event.toString());
}
JSONObject msg = new JSONObject();
try { msg.put("type", "event"); msg.put("payload", event); } catch (Exception e) {}
if (watchBridge != null) watchBridge.sendToWatch(msg.toString());
}
@Override
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
@@ -89,10 +112,10 @@ public class MainActivity extends AppCompatActivity {
serverClient.sendRequest("harness.cancel", params);
} else {
params.put("content", content);
serverClient.sendRequest("session.prompt", params);
serverClient.sendRequest("harness.prompt", params);
}
} else if ("cancel".equals(type)) {
serverClient.sendRequest("session.cancel", params);
serverClient.sendRequest("harness.cancel", params);
}
} catch (Exception e) {
// ignore malformed