chore: refactoring
This commit is contained in:
@@ -670,7 +670,7 @@ func (a *agent) CompactSession(ctx context.Context, sessionID string, force bool
|
|||||||
// Filter messages that were created after the last summarization
|
// Filter messages that were created after the last summarization
|
||||||
var newMessages []message.Message
|
var newMessages []message.Message
|
||||||
for _, msg := range sessionMessages {
|
for _, msg := range sessionMessages {
|
||||||
if msg.CreatedAt > session.SummarizedAt.UnixMilli() {
|
if msg.CreatedAt.After(session.SummarizedAt) {
|
||||||
newMessages = append(newMessages, msg)
|
newMessages = append(newMessages, msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -106,21 +106,11 @@ func (ToolResult) isPart() {}
|
|||||||
|
|
||||||
type Finish struct {
|
type Finish struct {
|
||||||
Reason FinishReason `json:"reason"`
|
Reason FinishReason `json:"reason"`
|
||||||
Time int64 `json:"time"`
|
Time time.Time `json:"time"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (Finish) isPart() {}
|
func (Finish) isPart() {}
|
||||||
|
|
||||||
type Message struct {
|
|
||||||
ID string
|
|
||||||
Role MessageRole
|
|
||||||
SessionID string
|
|
||||||
Parts []ContentPart
|
|
||||||
Model models.ModelID
|
|
||||||
CreatedAt int64
|
|
||||||
UpdatedAt int64
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Message) Content() *TextContent {
|
func (m *Message) Content() *TextContent {
|
||||||
for _, part := range m.Parts {
|
for _, part := range m.Parts {
|
||||||
if c, ok := part.(TextContent); ok {
|
if c, ok := part.(TextContent); ok {
|
||||||
@@ -318,7 +308,7 @@ func (m *Message) AddFinish(reason FinishReason) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m.Parts = append(m.Parts, Finish{Reason: reason, Time: time.Now().UnixMilli()})
|
m.Parts = append(m.Parts, Finish{Reason: reason, Time: time.Now()})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Message) AddImageURL(url, detail string) {
|
func (m *Message) AddImageURL(url, detail string) {
|
||||||
|
|||||||
@@ -16,6 +16,16 @@ import (
|
|||||||
"github.com/sst/opencode/internal/pubsub"
|
"github.com/sst/opencode/internal/pubsub"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Message struct {
|
||||||
|
ID string
|
||||||
|
Role MessageRole
|
||||||
|
SessionID string
|
||||||
|
Parts []ContentPart
|
||||||
|
Model models.ModelID
|
||||||
|
CreatedAt time.Time
|
||||||
|
UpdatedAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
EventMessageCreated pubsub.EventType = "message_created"
|
EventMessageCreated pubsub.EventType = "message_created"
|
||||||
EventMessageUpdated pubsub.EventType = "message_updated"
|
EventMessageUpdated pubsub.EventType = "message_updated"
|
||||||
@@ -81,7 +91,7 @@ func (s *service) Create(ctx context.Context, sessionID string, params CreateMes
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if params.Role == User && !isFinished {
|
if params.Role == User && !isFinished {
|
||||||
params.Parts = append(params.Parts, Finish{Reason: FinishReasonEndTurn, Time: time.Now().UnixMilli()})
|
params.Parts = append(params.Parts, Finish{Reason: FinishReasonEndTurn, Time: time.Now()})
|
||||||
}
|
}
|
||||||
|
|
||||||
partsJSON, err := marshallParts(params.Parts)
|
partsJSON, err := marshallParts(params.Parts)
|
||||||
@@ -126,9 +136,9 @@ func (s *service) Update(ctx context.Context, message Message) (Message, error)
|
|||||||
|
|
||||||
var dbFinishedAt sql.NullInt64
|
var dbFinishedAt sql.NullInt64
|
||||||
finishPart := message.FinishPart()
|
finishPart := message.FinishPart()
|
||||||
if finishPart != nil && finishPart.Time > 0 {
|
if finishPart != nil && !finishPart.Time.IsZero() {
|
||||||
dbFinishedAt = sql.NullInt64{
|
dbFinishedAt = sql.NullInt64{
|
||||||
Int64: finishPart.Time,
|
Int64: finishPart.Time.UnixMilli(),
|
||||||
Valid: true,
|
Valid: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -290,8 +300,8 @@ func (s *service) fromDBItem(item db.Message) (Message, error) {
|
|||||||
Role: MessageRole(item.Role),
|
Role: MessageRole(item.Role),
|
||||||
Parts: parts,
|
Parts: parts,
|
||||||
Model: models.ModelID(item.Model.String),
|
Model: models.ModelID(item.Model.String),
|
||||||
CreatedAt: item.CreatedAt * 1000,
|
CreatedAt: time.UnixMilli(item.CreatedAt),
|
||||||
UpdatedAt: item.UpdatedAt * 1000,
|
UpdatedAt: time.UnixMilli(item.UpdatedAt),
|
||||||
}
|
}
|
||||||
|
|
||||||
return msg, nil
|
return msg, nil
|
||||||
@@ -400,15 +410,8 @@ func marshallParts(parts []ContentPart) ([]byte, error) {
|
|||||||
func unmarshallParts(data []byte) ([]ContentPart, error) {
|
func unmarshallParts(data []byte) ([]ContentPart, error) {
|
||||||
var rawMessages []json.RawMessage
|
var rawMessages []json.RawMessage
|
||||||
if err := json.Unmarshal(data, &rawMessages); err != nil {
|
if err := json.Unmarshal(data, &rawMessages); err != nil {
|
||||||
// Handle case where 'parts' might be a single object if not an array initially
|
|
||||||
// This was a fallback, if your DB always stores an array, this might not be needed.
|
|
||||||
var singleRawMessage json.RawMessage
|
|
||||||
if errSingle := json.Unmarshal(data, &singleRawMessage); errSingle == nil {
|
|
||||||
rawMessages = []json.RawMessage{singleRawMessage}
|
|
||||||
} else {
|
|
||||||
return nil, fmt.Errorf("failed to unmarshal parts data as array: %w. Data: %s", err, string(data))
|
return nil, fmt.Errorf("failed to unmarshal parts data as array: %w. Data: %s", err, string(data))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
parts := make([]ContentPart, 0, len(rawMessages))
|
parts := make([]ContentPart, 0, len(rawMessages))
|
||||||
for _, rawPart := range rawMessages {
|
for _, rawPart := range rawMessages {
|
||||||
@@ -461,11 +464,15 @@ func unmarshallParts(data []byte) ([]ContentPart, error) {
|
|||||||
}
|
}
|
||||||
parts = append(parts, p)
|
parts = append(parts, p)
|
||||||
case finishType:
|
case finishType:
|
||||||
var p Finish
|
type dbFinish struct {
|
||||||
|
Reason FinishReason `json:"reason"`
|
||||||
|
Time int64 `json:"time"`
|
||||||
|
}
|
||||||
|
var p dbFinish
|
||||||
if err := json.Unmarshal(wrapper.Data, &p); err != nil {
|
if err := json.Unmarshal(wrapper.Data, &p); err != nil {
|
||||||
return nil, fmt.Errorf("unmarshal Finish: %w. Data: %s", err, string(wrapper.Data))
|
return nil, fmt.Errorf("unmarshal Finish: %w. Data: %s", err, string(wrapper.Data))
|
||||||
}
|
}
|
||||||
parts = append(parts, p)
|
parts = append(parts, Finish{Reason: FinishReason(p.Reason), Time: time.UnixMilli(p.Time)})
|
||||||
default:
|
default:
|
||||||
slog.Warn("Unknown part type during unmarshalling, attempting to parse as TextContent", "type", wrapper.Type, "data", string(wrapper.Data))
|
slog.Warn("Unknown part type during unmarshalling, attempting to parse as TextContent", "type", wrapper.Type, "data", string(wrapper.Data))
|
||||||
// Fallback: if type is unknown or empty, try to parse data as TextContent directly
|
// Fallback: if type is unknown or empty, try to parse data as TextContent directly
|
||||||
|
|||||||
@@ -219,8 +219,8 @@ func (s *service) fromDBItem(item db.Session) Session {
|
|||||||
Cost: item.Cost,
|
Cost: item.Cost,
|
||||||
Summary: item.Summary.String,
|
Summary: item.Summary.String,
|
||||||
SummarizedAt: summarizedAt,
|
SummarizedAt: summarizedAt,
|
||||||
CreatedAt: time.UnixMilli(item.CreatedAt * 1000),
|
CreatedAt: time.UnixMilli(item.CreatedAt),
|
||||||
UpdatedAt: time.UnixMilli(item.UpdatedAt * 1000),
|
UpdatedAt: time.UnixMilli(item.UpdatedAt),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -100,7 +100,7 @@ func renderUserMessage(msg message.Message, isFocused bool, width int, position
|
|||||||
|
|
||||||
// Add timestamp info
|
// Add timestamp info
|
||||||
info := []string{}
|
info := []string{}
|
||||||
timestamp := time.UnixMilli(msg.CreatedAt).Local().Format("02 Jan 2006 03:04 PM")
|
timestamp := msg.CreatedAt.Local().Format("02 Jan 2006 03:04 PM")
|
||||||
username, _ := config.GetUsername()
|
username, _ := config.GetUsername()
|
||||||
info = append(info, baseStyle.
|
info = append(info, baseStyle.
|
||||||
Width(width-1).
|
Width(width-1).
|
||||||
@@ -148,7 +148,7 @@ func renderAssistantMessage(
|
|||||||
baseStyle := styles.BaseStyle()
|
baseStyle := styles.BaseStyle()
|
||||||
|
|
||||||
// Always add timestamp info
|
// Always add timestamp info
|
||||||
timestamp := time.UnixMilli(msg.CreatedAt).Local().Format("02 Jan 2006 03:04 PM")
|
timestamp := msg.CreatedAt.Local().Format("02 Jan 2006 03:04 PM")
|
||||||
modelName := "Assistant"
|
modelName := "Assistant"
|
||||||
if msg.Model != "" {
|
if msg.Model != "" {
|
||||||
modelName = models.SupportedModels[msg.Model].Name
|
modelName = models.SupportedModels[msg.Model].Name
|
||||||
|
|||||||
Reference in New Issue
Block a user