Files
daluandou/.codely.packages/cn.tuanjie.codely.bridge@1.0.85-785dea6a/Editor/Bridge/Notifier/AgentInputNotifier.cs
T
huadexuan 53d95802e5 ```
feat(gameplay): 更新单位属性和材质配置

- 调整多个单位的基础生命值和攻击力数值平衡
- 修改CoreBattleGround材质的纹理缩放比例
- 更新地砖材质颜色为白色
- 优化纹理资源设置,调整最大纹理尺寸并添加PlayableAds平台支持
- 修复弓箭手技能目标查找逻辑,改用统一的敌人检测方法
- 为水晶矿添加中立阵营设置和光球视觉效果同步
- 调整预制体中的全局缩放参数
- 为狼人和石巨人添加NavMeshAgent组件支持
- 优化水晶矿血条显示位置计算方式
```
2026-09-20 21:52:31 +08:00

128 lines
5.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Codely.Newtonsoft.Json.Linq;
using UnityEditor;
using UnityTcp.Editor.Helpers;
#if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX
using Cn.Tuanjie.Codely.Editor;
#endif
namespace UnityTcp.Editor.Notifier
{
/// <summary>
/// Pushes messages from the Editor/engine into the input field of the
/// embedded AI agent (CodelyWindow) as an "input_from_editor" event:
/// user-typed messages, engine errors, stack traces, or any other info the
/// agent should react to. Distinguish the kind of message via the
/// <c>source</c>/<c>type</c> arguments.
///
/// If no CodelyWindow is open, one is opened and the message is queued
/// instead of broadcast; the frontend drains the queue via manage_editor
/// action "drain_agent_input" once its page is fully loaded, so cold-start
/// messages are not lost. With a window already open, the message is
/// broadcast directly.
///
/// Payload shape: <c>{ text, source, type, autoSubmit, timestamp, attachments? }</c>.
/// </summary>
public static class AgentInputNotifier
{
private const string EventType = "input_from_editor";
private const int MaxPendingMessages = 100;
private static readonly object _gate = new object();
private static readonly List<JObject> _pending = new List<JObject>();
/// <summary>
/// Sends <paramref name="text"/> to the embedded agent's input field.
/// Must be called from the main thread (may open an EditorWindow).
/// </summary>
/// <param name="text">Message to place into the agent input field.</param>
/// <param name="source">Origin of the message; defaults to "editor_window".</param>
/// <param name="type">Kind of message; defaults to "user_message".</param>
/// <param name="autoSubmit">True to have the frontend submit the message immediately instead of just appending it.</param>
/// <param name="timestamp">Time the message was created; defaults to now (UTC).</param>
/// <param name="attachments">Optional file paths the frontend attaches (@) to the message; may be null.</param>
/// <returns>
/// True if broadcast to at least one client; false if invalid or queued
/// for the frontend to drain (no CodelyWindow was open yet).
/// </returns>
public static bool Send(
string text,
string source = "editor_window",
string type = "user_message",
bool autoSubmit = false,
DateTime? timestamp = null,
IReadOnlyList<string> attachments = null)
{
if (string.IsNullOrWhiteSpace(text)) return false;
try
{
var payload = new JObject
{
["text"] = text,
["source"] = source,
["type"] = type,
["autoSubmit"] = autoSubmit,
["timestamp"] = (timestamp ?? DateTime.UtcNow).ToString("o"),
};
var attachmentPaths = attachments
?.Select(p => p?.Trim())
.Where(p => !string.IsNullOrWhiteSpace(p))
.ToList();
if (attachmentPaths != null && attachmentPaths.Count > 0)
payload["attachments"] = JArray.FromObject(attachmentPaths);
#if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX
// The CodelyWindow's presence is the one reliable signal here:
// connection state can lie (e.g. a standalone app client makes
// NotifyAll report delivered while the embedded page is still
// loading). No window → open one, queue the message, and let
// the frontend drain the queue once its page is fully loaded.
if (!EditorWindow.HasOpenInstances<CodelyWindow>())
{
CodelyWindow.ShowWindow();
QueuePending(payload);
return false;
}
#endif
return UnityTcpBridge.IsRunning
&& UnityTcpBridge.NotifyAll(EventType, payload);
}
catch (Exception e)
{
try { CodelyLogger.LogError($"[AgentInputNotifier] send failed: {e.Message}"); } catch { }
return false;
}
}
private static void QueuePending(JObject payload)
{
lock (_gate)
{
if (_pending.Count >= MaxPendingMessages)
_pending.RemoveAt(0); // drop the oldest
_pending.Add(payload);
}
}
/// <summary>
/// Returns all queued (undelivered) messages and clears the queue.
/// Called by manage_editor's "drain_agent_input" action on behalf of
/// the frontend once its page is fully loaded.
/// </summary>
public static List<JObject> DrainPending()
{
lock (_gate)
{
var items = new List<JObject>(_pending);
_pending.Clear();
return items;
}
}
}
}