仰望星辰工作室

miaopanel

miaopanel/ internal/ai/types.go 3.4 KB · 129 行 原始文件
1// Package ai implements the panel AI assistant: OpenAI-compatible chat,
2// streaming output, tool calling and approval for mutating operations.
3package ai
4
5// Risk classifies a tool call.
6type Risk string
7
8const (
9 // RiskRead executes immediately without asking the operator.
10 RiskRead Risk = "read"
11 // RiskWrite changes panel state and must be confirmed.
12 RiskWrite Risk = "write"
13 // RiskDanger executes host commands and must be confirmed.
14 RiskDanger Risk = "danger"
15)
16
17// ToolCall is the OpenAI function call shape.
18type ToolCall struct {
19 ID string `json:"id"`
20 Type string `json:"type"`
21 Function FunctionCall `json:"function"`
22}
23
24// FunctionCall carries the tool name and raw JSON arguments.
25type FunctionCall struct {
26 Name string `json:"name"`
27 Arguments string `json:"arguments"`
28}
29
30// ChatMessage is one provider message.
31type ChatMessage struct {
32 Role string `json:"role"`
33 Content string `json:"content,omitempty"`
34 ToolCalls []ToolCall `json:"tool_calls,omitempty"`
35 ToolCallID string `json:"tool_call_id,omitempty"`
36 Name string `json:"name,omitempty"`
37}
38
39// ToolDef is the function schema sent to the provider.
40type ToolDef struct {
41 Type string `json:"type"`
42 Function FunctionDef `json:"function"`
43}
44
45// FunctionDef describes one callable tool.
46type FunctionDef struct {
47 Name string `json:"name"`
48 Description string `json:"description"`
49 Parameters map[string]any `json:"parameters"`
50}
51
52// ChatRequest is one completion request.
53type ChatRequest struct {
54 Model string
55 Messages []ChatMessage
56 Tools []ToolDef
57 Temperature float64
58 MaxTokens int
59}
60
61// Usage mirrors the provider token accounting.
62type Usage struct {
63 PromptTokens int `json:"prompt_tokens"`
64 CompletionTokens int `json:"completion_tokens"`
65 TotalTokens int `json:"total_tokens"`
66}
67
68// ChatResult is one completed assistant turn.
69type ChatResult struct {
70 Message ChatMessage
71 FinishReason string
72 Usage *Usage
73}
74
75// EmitFunc writes one downstream SSE event.
76type EmitFunc func(event string, data any) error
77
78// TerminalCommand is one command suggested for the smart terminal.
79type TerminalCommand struct {
80 Command string `json:"command"`
81 Note string `json:"note"`
82 Risk string `json:"risk"`
83}
84
85// TerminalSuggestion is the terminal assistant response.
86type TerminalSuggestion struct {
87 Reply string `json:"reply"`
88 Commands []TerminalCommand `json:"commands"`
89}
90
91// Meta identifies the conversation and model behind a stream.
92type Meta struct {
93 SessionID string `json:"session_id"`
94 Model string `json:"model"`
95}
96
97// TextDelta is one incremental assistant token.
98type TextDelta struct {
99 Delta string `json:"delta"`
100}
101
102// ToolEvent reports a tool call progress update.
103type ToolEvent struct {
104 ID string `json:"id"`
105 Name string `json:"name"`
106 Args string `json:"args,omitempty"`
107 Status string `json:"status,omitempty"`
108 Result string `json:"result,omitempty"`
109}
110
111// ActionEvent is a pending approval card.
112type ActionEvent struct {
113 ID string `json:"id"`
114 ToolCallID string `json:"tool_call_id"`
115 Tool string `json:"tool"`
116 Args string `json:"args"`
117 Summary string `json:"summary"`
118 Risk string `json:"risk"`
119}
120
121// DoneEvent ends one stream.
122type DoneEvent struct {
123 Status string `json:"status"`
124}
125
126// ErrorEvent reports a failure to the client.
127type ErrorEvent struct {
128 Message string `json:"message"`
129}