M0:神识 Go 网关骨架(IR 入站/SSE 出站/stub 适配器/服务令牌/健康检查)及测试与镜像
This commit is contained in:
1 parent
7ef3a15061
commit
f3058a64c3
10 files changed
+514
No files matched your search
@@ -0,0 +1,66 @@
|
||||
// Package ir 定义灵台 → 神识的内部中间格式(IR)。
|
||||
// 字段与灵台 app/shenshi_client/types.py 逐一对齐,是跨服务契约的 Go 侧表达;
|
||||
// 契约取值以 docs/contracts/contract-glossary.md 为单一事实源。
|
||||
package ir
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Message IR 消息。Tool 消息须携带 ToolCallID,与工具调用配对不拆散。
|
||||
type Message struct {
|
||||
Role string `json:"role"`
|
||||
Content string `json:"content"`
|
||||
ToolCallID string `json:"tool_call_id,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
}
|
||||
|
||||
// ChatRequest 版本化模型请求(数据面唯一入站形态)。
|
||||
type ChatRequest struct {
|
||||
IRVersion int `json:"ir_version"`
|
||||
Slot string `json:"slot"`
|
||||
Model string `json:"model,omitempty"`
|
||||
Messages []Message `json:"messages"`
|
||||
RunID string `json:"run_id"`
|
||||
TraceID string `json:"trace_id,omitempty"`
|
||||
CharacterID string `json:"character_id,omitempty"`
|
||||
Temperature *float64 `json:"temperature,omitempty"`
|
||||
MaxTokens *int `json:"max_tokens,omitempty"`
|
||||
Stream bool `json:"stream"`
|
||||
}
|
||||
|
||||
// Delta 神识标准化流增量(与 Provider 方言无关)。
|
||||
type Delta struct {
|
||||
Delta string `json:"delta,omitempty"`
|
||||
FinishReason string `json:"finish_reason,omitempty"`
|
||||
UsagePromptTokens int `json:"usage_prompt_tokens,omitempty"`
|
||||
UsageCompletionTokens int `json:"usage_completion_tokens,omitempty"`
|
||||
}
|
||||
|
||||
// SupportedIRVersion 当前支持的 IR 版本;不匹配时返回错误(HJ-2002)。
|
||||
const SupportedIRVersion = 1
|
||||
|
||||
// Validate 校验请求形态:版本、必填字段与流式开关。
|
||||
func (r *ChatRequest) Validate() error {
|
||||
if r.IRVersion != SupportedIRVersion {
|
||||
return fmt.Errorf("ir_version %d 不受支持(当前 %d)", r.IRVersion, SupportedIRVersion)
|
||||
}
|
||||
if r.RunID == "" {
|
||||
return fmt.Errorf("run_id 不能为空")
|
||||
}
|
||||
if len(r.Messages) == 0 {
|
||||
return fmt.Errorf("messages 不能为空")
|
||||
}
|
||||
if !r.Stream {
|
||||
return fmt.Errorf("数据面当前只接受流式请求(stream=true)")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// LastUserMessage 返回最后一条用户消息内容(stub 适配器回声源)。
|
||||
func (r *ChatRequest) LastUserMessage() string {
|
||||
for i := len(r.Messages) - 1; i >= 0; i-- {
|
||||
if r.Messages[i].Role == "user" {
|
||||
return r.Messages[i].Content
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
Reference in new issue
Block a user