第103章 开发一个 Web SAI
第102章建立了设备控制 SAI:
Individual
→ Decision
→ Behavior
→ Action
→ Command
→ Adapter
→ Device
→ Device Response
→ Feedback
第103章把 SAI 放到 Web 环境中。
本章解决的问题是:
浏览器如何把信息交给 SAI,SAI 如何完成认知与决策,再把结构化结果通过 Web 返回给浏览器。
本章的 Web 只是外部交互环境,不是 SAI 的认知本身。
核心结构:
Browser
↓
Web Controller
↓
Information
↓
Central
↓
Individual
↓
Cognition
↓
Decision
↓
SATE
↓
WebRenderer
↓
Response
↓
Browser
仍然采用 PHP OOP、对象、属性、方法、规则和结构化数据,不使用任何大模型机制。
1. Web Controller
1.1 Web Controller 定义
Web Controller 是 Web 请求进入 SAI 的第一层程序组件。
浏览器发送:
GET /device?id=Motor_A
Web Controller 接收:
HTTP Request
然后完成:
Request
↓
验证
↓
提取参数
↓
形成 Information
↓
交给 Central
1.2 Controller 不负责 Cognition
错误结构:
Browser
↓
Controller
↓
Controller 自己判断
↓
Controller 自己决定
正确结构:
Browser
↓
Web Controller
↓
Information
↓
Central
↓
Individual
↓
Cognition
↓
Decision
因此:
Controller 是 Web 入口,不是 SAI 的认知中心。
1.3 简单 Controller
class WebController
{
protected $central;
public function __construct($central)
{
$this->central = $central;
}
public function handle($request)
{
$information = new Information(
'WEB',
$request
);
return $this->central->process($information);
}
}
Controller 的职责保持简单:
接收
验证
转换
转交
2. Information
Web 请求本身不是完整的 SAI Information。
需要经过 Controller 整理。
例如浏览器请求:
GET /device?id=Motor_A
Controller 提取:
id = Motor_A
形成:
$information = array(
'source' => 'WEB',
'type' => 'DEVICE_QUERY',
'data' => array(
'device_id' => 'Motor_A'
),
'timestamp' => time()
);
2.1 Web Request 与 Information
必须区分:
Web Request
=
HTTP 通信结构
而:
Information
=
Individual 可以处理的内部信息结构
关系:
HTTP Request
↓
Web Controller
↓
Information
因此:
Web 是信息进入 SAI 的一个来源。
3. Individual
3.1 Individual 在 Web SAI 中的作用
Individual 是真正执行 SAI 内部认知过程的主体。
Controller 不直接执行:
Cognition
Reasoning
Decision
Behavior
Action
而是把 Information 交给 Individual。
Information
↓
Individual
3.2 Individual 内部结构
Individual_A
│
├── InformationReceiver
├── Perception
├── Cognition
├── Memory
├── Reasoning
├── Decision
├── Behavior
├── Action
└── Expression
Web 只是入口和出口。
3.3 Web Individual
例如:
class Individual
{
protected $cognition;
protected $decision;
public function receive($information)
{
$cognition = $this->cognition->process($information);
return $this->decision->process($cognition);
}
}
实际完整系统还可以包含:
Perception
Memory
Reasoning
Risk
Priority
Behavior
Action
Expression
这里为了说明 Web 结构,只保留核心路径。
4. Central
4.1 Central 定义
Central 是 Web Controller 与 Individual 之间的协调中心。
核心作用:
接收 Information
↓
确定 Individual
↓
调用 Individual
↓
获取 Result
↓
返回给 Controller
因此:
Central = 协调
Individual = 运行认知
4.2 Central 不等于 Cognition
这是 Web SAI 中必须保持的边界。
Central
不负责:
理解
推理
决策
它负责:
组件协调
运行调度
状态检查
结果传递
例如:
class Central
{
protected $individual;
public function __construct($individual)
{
$this->individual = $individual;
}
public function process($information)
{
return $this->individual->receive($information);
}
}
5. Cognition
Web 用户输入进入 Individual 后,首先需要形成认知。
例如:
GET /device?id=Motor_A
Information:
type = DEVICE_QUERY
device_id = Motor_A
Cognition 可以形成:
target = Motor_A
object_type = DEVICE
request_type = QUERY
然后进一步读取:
Motor_A
state = RUNNING
speed = 800
temperature = 84
形成:
$cognition = array(
'state' => 'UNDERSTOOD',
'object' => array(
'id' => 'Motor_A',
'type' => 'DEVICE'
),
'properties' => array(
'state' => 'RUNNING',
'speed' => 800,
'temperature' => 84
)
);
5.1 Cognition 的作用
Web SAI 中:
Information
↓
Cognition
不是简单地把请求原样返回。
例如用户请求:
device_id = Motor_A
Individual 需要理解:
这是一个设备对象查询
目标对象是 Motor_A
需要读取当前设备状态
6. Decision
Cognition 得到:
Motor_A
state = RUNNING
speed = 800
temperature = 84
然后 Decision 判断应该执行什么。
例如用户请求:
GET /device?id=Motor_A
候选:
RETURN_DEVICE
QUERY_DEVICE
REJECT
UNKNOWN
系统检查:
Motor_A exists TRUE
Device state RUNNING
Request valid TRUE
于是:
Decision = QUERY_DEVICE
6.1 如果是控制请求
例如:
POST /device/action
数据:
{
"device_id": "Motor_A",
"action": "STOP"
}
Cognition:
target = Motor_A
requested_action = STOP
Decision 则检查:
Device exists
Device state
Ability
Condition
Risk
Conflict
之后才能形成:
Decision = STOP
所以:
Web 请求中的 Action 不是自动执行命令。
它仍然必须经过 Individual 的认知与决策过程。
7. SATE
7.1 SATE 在 Web SAI 中的作用
Individual 完成处理以后,得到的是结构化结果。
例如:
$result = array(
'device' => 'Motor_A',
'state' => 'RUNNING',
'speed' => 800,
'temperature' => 84
);
这个结果不是 HTML。
因此需要:
Result
↓
SATE
↓
Template
↓
Structured Presentation
7.2 SATE Template
例如:
<html>
<head>
<title>Device Status</title>
</head>
<body>
<h1>{{device}}</h1>
<p>State: {{state}}</p>
<p>Speed: {{speed}}</p>
<p>Temperature: {{temperature}}</p>
</body>
</html>
SATE 将:
{{device}}
{{state}}
{{speed}}
{{temperature}}
映射到实际数据。
7.3 SATE 不负责认知
SATE 不应该:
判断设备危险
决定是否停止
推理温度
修改 Device
它只负责:
结构化数据
↓
模板
↓
表现结构
因此:
SATE 是表现层,不是认知层。
8. WebRenderer
8.1 WebRenderer 定义
WebRenderer 把 SATE 处理后的模板结构转换成 Web 可返回的表现结果。
核心:
Expression
↓
SATE
↓
Template
↓
WebRenderer
↓
HTML
8.2 WebRenderer 示例
输入:
$data = array(
'device' => 'Motor_A',
'state' => 'RUNNING',
'speed' => 800,
'temperature' => 84
);
WebRenderer 最终得到:
<html>
<body>
<h1>Motor_A</h1>
<p>State: RUNNING</p>
<p>Speed: 800</p>
<p>Temperature: 84</p>
</body>
</html>
8.3 WebRenderer 不做 Decision
错误:
WebRenderer
↓
发现 temperature > 85
↓
决定 STOP
正确:
Cognition
↓
Reasoning
↓
Decision
↓
Expression
↓
SATE
↓
WebRenderer
WebRenderer 只负责:
把已经确定的结构化结果表现成 Web 内容。
9. Response
9.1 HTTP Response
WebRenderer 完成后,需要形成 HTTP Response。
基本结构:
HTTP Response
├── Status Code
├── Headers
├── Content-Type
└── Body
例如:
Status:
200
Content-Type:
text/html
Body:
<html>
...
</html>
9.2 Response 与 Expression
两者不同:
Expression
=
SAI 内部准备对外表达的结构化结果
而:
Response
=
Web 环境中的 HTTP 返回结构
关系:
Individual
↓
Expression
↓
SATE
↓
WebRenderer
↓
HTTP Response
10. 完整 Web 应用
现在建立一个最简单的 Web SAI。
10.1 Web 应用结构
web-sai/
│
├── index.php
│
├── src/
│ ├── WebController.php
│ ├── Information.php
│ ├── Central.php
│ ├── Individual.php
│ ├── Cognition.php
│ ├── Decision.php
│ ├── SATE.php
│ └── WebRenderer.php
│
└── templates/
└── device.php
10.2 浏览器请求
用户访问:
/device?id=Motor_A
浏览器发送:
GET /device?id=Motor_A
10.3 Web Controller
Controller 接收:
Request
读取:
id = Motor_A
形成:
Information
source = WEB
type = DEVICE_QUERY
device_id = Motor_A
10.4 Central
Central 接收:
Information
然后交给:
Individual_A
10.5 Individual
Individual 开始运行:
Information
↓
Cognition
↓
Decision
10.6 Cognition
形成:
Object:
Motor_A
State:
RUNNING
Properties:
speed = 800
temperature = 84
10.7 Decision
系统判断:
Request = DEVICE_QUERY
选择:
Decision = RETURN_DEVICE
10.8 Expression
Individual 形成:
$expression = array(
'type' => 'DEVICE_STATUS',
'device' => array(
'id' => 'Motor_A',
'state' => 'RUNNING',
'speed' => 800,
'temperature' => 84
),
'state' => 'SUCCESS'
);
10.9 SATE
模板:
device.php
结构:
<h1>{{device.id}}</h1>
<p>State: {{device.state}}</p>
<p>Speed: {{device.speed}}</p>
<p>Temperature: {{device.temperature}}</p>
SATE 进行:
Data Mapping
↓
Variable Replacement
↓
Template Result
10.10 WebRenderer
WebRenderer 得到:
<h1>Motor_A</h1>
<p>State: RUNNING</p>
<p>Speed: 800</p>
<p>Temperature: 84</p>
10.11 Response
最终:
HTTP/1.1 200 OK
Content-Type: text/html
Body:
<h1>Motor_A</h1>
<p>State: RUNNING</p>
<p>Speed: 800</p>
<p>Temperature: 84</p>
浏览器显示:
Motor_A
State: RUNNING
Speed: 800
Temperature: 84
11. Web SAI 完整闭环
最终形成:
Browser
│
HTTP Request
│
↓
Web Controller
│
↓
Information
│
↓
Central
│
↓
Individual
│
┌────────┴────────┐
↓ ↓
Perception Cognition
│
↓
Reasoning
│
↓
Decision
│
↓
Behavior
│
↓
Action
│
↓
Expression
│
↓
SATE
│
↓
WebRenderer
│
↓
Response
│
↓
Browser
如果涉及设备控制,则继续:
Action
↓
Command
↓
Adapter
↓
Device
↓
Device Response
↓
Feedback
↓
Information
↓
Individual
于是 Web SAI 与第102章设备控制 SAI 可以连接起来:
Browser
↓
Web Controller
↓
Information
↓
Central
↓
Individual
↓
Cognition
↓
Reasoning
↓
Decision
↓
Behavior
↓
Action
↓
Command
↓
Adapter
↓
Device
↓
Device Response
↓
Feedback
↓
Information
↓
Individual
↓
Expression
↓
SATE
↓
WebRenderer
↓
Response
↓
Browser
12. Web SAI 的核心边界
这一章最重要的是把 Web 层和 SAI 内部层彻底分开。
Web Controller
↓
负责 Web 请求进入
Information
↓
负责内部信息结构
Central
↓
负责组件协调
Individual
↓
负责 SAI 个体运行
Cognition
↓
负责理解
Reasoning
↓
负责推理
Decision
↓
负责选择
Behavior
↓
负责组织执行
Action
↓
负责具体操作
SATE
↓
负责模板组织
WebRenderer
↓
负责 Web 表现
Response
↓
负责 HTTP 返回
可以浓缩成:
Web Controller = Web入口
Information = 信息
Central = 协调
Individual = SAI主体
Cognition = 理解
Decision = 选择
SATE = 模板组织
WebRenderer = Web表现
Response = HTTP返回
本章核心模型
第103章建立的是:
Browser
↓
Web Controller
↓
Information
↓
Central
↓
Individual
↓
Cognition
↓
Decision
↓
SATE
↓
WebRenderer
↓
Response
↓
Browser
如果加入完整 SAI 行为链:
Browser
↓
Web Controller
↓
Information
↓
Central
↓
Individual
↓
Perception
↓
Cognition
↓
Memory
↓
Reasoning
↓
Decision
↓
Behavior
↓
Action
↓
Expression
↓
SATE
↓
WebRenderer
↓
Response
↓
Browser
因此:
Web 不是 SAI 本身,Web 是 SAI 与外部用户进行信息交换的环境。
而第103章真正建立的是:
一个可以通过浏览器接收 Information、由 Individual 完成 Cognition 与 Decision,并通过 SATE 和 WebRenderer 把结构化结果返回浏览器的 Web SAI。