mirror of
https://github.com/sst/opencode.git
synced 2025-08-31 02:07:24 +00:00
wip: refactoring tui
This commit is contained in:
parent
a9799136fe
commit
6759674c0f
12 changed files with 345 additions and 1524 deletions
|
@ -1,109 +0,0 @@
|
|||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/sst/opencode/internal/config"
|
||||
"github.com/sst/opencode/internal/llm/tools"
|
||||
"github.com/sst/opencode/internal/lsp"
|
||||
"github.com/sst/opencode/internal/message"
|
||||
"github.com/sst/opencode/internal/session"
|
||||
)
|
||||
|
||||
type agentTool struct {
|
||||
sessions session.Service
|
||||
messages message.Service
|
||||
lspClients map[string]*lsp.Client
|
||||
}
|
||||
|
||||
const (
|
||||
AgentToolName = "agent"
|
||||
)
|
||||
|
||||
type AgentParams struct {
|
||||
Prompt string `json:"prompt"`
|
||||
}
|
||||
|
||||
func (b *agentTool) Info() tools.ToolInfo {
|
||||
return tools.ToolInfo{
|
||||
Name: AgentToolName,
|
||||
Description: "Launch a new agent that has access to the following tools: GlobTool, GrepTool, LS, View. When you are searching for a keyword or file and are not confident that you will find the right match on the first try, use the Agent tool to perform the search for you. For example:\n\n- If you are searching for a keyword like \"config\" or \"logger\", or for questions like \"which file does X?\", the Agent tool is strongly recommended\n- If you want to read a specific file path, use the View or GlobTool tool instead of the Agent tool, to find the match more quickly\n- If you are searching for a specific class definition like \"class Foo\", use the GlobTool tool instead, to find the match more quickly\n\nUsage notes:\n1. Launch multiple agents concurrently whenever possible, to maximize performance; to do that, use a single message with multiple tool uses\n2. When the agent is done, it will return a single message back to you. The result returned by the agent is not visible to the user. To show the user the result, you should send a text message back to the user with a concise summary of the result.\n3. Each agent invocation is stateless. You will not be able to send additional messages to the agent, nor will the agent be able to communicate with you outside of its final report. Therefore, your prompt should contain a highly detailed task description for the agent to perform autonomously and you should specify exactly what information the agent should return back to you in its final and only message to you.\n4. The agent's outputs should generally be trusted\n5. IMPORTANT: The agent can not use Bash, Replace, Edit, so can not modify files. If you want to use these tools, use them directly instead of going through the agent.",
|
||||
Parameters: map[string]any{
|
||||
"prompt": map[string]any{
|
||||
"type": "string",
|
||||
"description": "The task for the agent to perform",
|
||||
},
|
||||
},
|
||||
Required: []string{"prompt"},
|
||||
}
|
||||
}
|
||||
|
||||
func (b *agentTool) Run(ctx context.Context, call tools.ToolCall) (tools.ToolResponse, error) {
|
||||
var params AgentParams
|
||||
if err := json.Unmarshal([]byte(call.Input), ¶ms); err != nil {
|
||||
return tools.NewTextErrorResponse(fmt.Sprintf("error parsing parameters: %s", err)), nil
|
||||
}
|
||||
if params.Prompt == "" {
|
||||
return tools.NewTextErrorResponse("prompt is required"), nil
|
||||
}
|
||||
|
||||
sessionID, messageID := tools.GetContextValues(ctx)
|
||||
if sessionID == "" || messageID == "" {
|
||||
return tools.ToolResponse{}, fmt.Errorf("session_id and message_id are required")
|
||||
}
|
||||
|
||||
agent, err := NewAgent(config.AgentTask, b.sessions, b.messages, TaskAgentTools(b.lspClients))
|
||||
if err != nil {
|
||||
return tools.ToolResponse{}, fmt.Errorf("error creating agent: %s", err)
|
||||
}
|
||||
|
||||
session, err := b.sessions.CreateTaskSession(ctx, call.ID, sessionID, "New Agent Session")
|
||||
if err != nil {
|
||||
return tools.ToolResponse{}, fmt.Errorf("error creating session: %s", err)
|
||||
}
|
||||
|
||||
done, err := agent.Run(ctx, session.ID, params.Prompt)
|
||||
if err != nil {
|
||||
return tools.ToolResponse{}, fmt.Errorf("error generating agent: %s", err)
|
||||
}
|
||||
result := <-done
|
||||
if result.Err() != nil {
|
||||
return tools.ToolResponse{}, fmt.Errorf("error generating agent: %s", result.Err())
|
||||
}
|
||||
|
||||
response := result.Response()
|
||||
if response.Role != message.Assistant {
|
||||
return tools.NewTextErrorResponse("no response"), nil
|
||||
}
|
||||
|
||||
updatedSession, err := b.sessions.Get(ctx, session.ID)
|
||||
if err != nil {
|
||||
return tools.ToolResponse{}, fmt.Errorf("error getting session: %s", err)
|
||||
}
|
||||
parentSession, err := b.sessions.Get(ctx, sessionID)
|
||||
if err != nil {
|
||||
return tools.ToolResponse{}, fmt.Errorf("error getting parent session: %s", err)
|
||||
}
|
||||
|
||||
parentSession.Cost += updatedSession.Cost
|
||||
|
||||
_, err = b.sessions.Update(ctx, parentSession)
|
||||
if err != nil {
|
||||
return tools.ToolResponse{}, fmt.Errorf("error saving parent session: %s", err)
|
||||
}
|
||||
return tools.NewTextResponse(response.Content().String()), nil
|
||||
}
|
||||
|
||||
func NewAgentTool(
|
||||
Sessions session.Service,
|
||||
Messages message.Service,
|
||||
LspClients map[string]*lsp.Client,
|
||||
) tools.BaseTool {
|
||||
return &agentTool{
|
||||
sessions: Sessions,
|
||||
messages: Messages,
|
||||
lspClients: LspClients,
|
||||
}
|
||||
}
|
|
@ -1,804 +0,0 @@
|
|||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/sst/opencode/internal/config"
|
||||
"github.com/sst/opencode/internal/llm/models"
|
||||
"github.com/sst/opencode/internal/llm/prompt"
|
||||
"github.com/sst/opencode/internal/llm/provider"
|
||||
"github.com/sst/opencode/internal/llm/tools"
|
||||
"github.com/sst/opencode/internal/logging"
|
||||
"github.com/sst/opencode/internal/message"
|
||||
"github.com/sst/opencode/internal/permission"
|
||||
"github.com/sst/opencode/internal/session"
|
||||
"github.com/sst/opencode/internal/status"
|
||||
)
|
||||
|
||||
// Common errors
|
||||
var (
|
||||
ErrRequestCancelled = errors.New("request cancelled by user")
|
||||
ErrSessionBusy = errors.New("session is currently processing another request")
|
||||
)
|
||||
|
||||
type AgentEvent struct {
|
||||
message message.Message
|
||||
err error
|
||||
}
|
||||
|
||||
func (e *AgentEvent) Err() error {
|
||||
return e.err
|
||||
}
|
||||
|
||||
func (e *AgentEvent) Response() message.Message {
|
||||
return e.message
|
||||
}
|
||||
|
||||
type Service interface {
|
||||
Run(ctx context.Context, sessionID string, content string, attachments ...message.Attachment) (<-chan AgentEvent, error)
|
||||
Cancel(sessionID string)
|
||||
IsSessionBusy(sessionID string) bool
|
||||
IsBusy() bool
|
||||
Update(agentName config.AgentName, modelID models.ModelID) (models.Model, error)
|
||||
CompactSession(ctx context.Context, sessionID string, force bool) error
|
||||
GetUsage(ctx context.Context, sessionID string) (*int64, error)
|
||||
EstimateContextWindowUsage(ctx context.Context, sessionID string) (float64, bool, error)
|
||||
}
|
||||
|
||||
type agent struct {
|
||||
sessions session.Service
|
||||
messages message.Service
|
||||
|
||||
tools []tools.BaseTool
|
||||
provider provider.Provider
|
||||
|
||||
titleProvider provider.Provider
|
||||
|
||||
activeRequests sync.Map
|
||||
}
|
||||
|
||||
func NewAgent(
|
||||
agentName config.AgentName,
|
||||
sessions session.Service,
|
||||
messages message.Service,
|
||||
agentTools []tools.BaseTool,
|
||||
) (Service, error) {
|
||||
agentProvider, err := createAgentProvider(agentName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var titleProvider provider.Provider
|
||||
// Only generate titles for the primary agent
|
||||
if agentName == config.AgentPrimary {
|
||||
titleProvider, err = createAgentProvider(config.AgentTitle)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
agent := &agent{
|
||||
provider: agentProvider,
|
||||
messages: messages,
|
||||
sessions: sessions,
|
||||
tools: agentTools,
|
||||
titleProvider: titleProvider,
|
||||
activeRequests: sync.Map{},
|
||||
}
|
||||
|
||||
return agent, nil
|
||||
}
|
||||
|
||||
func (a *agent) Cancel(sessionID string) {
|
||||
if cancelFunc, exists := a.activeRequests.LoadAndDelete(sessionID); exists {
|
||||
if cancel, ok := cancelFunc.(context.CancelFunc); ok {
|
||||
status.Info(fmt.Sprintf("Request cancellation initiated for session: %s", sessionID))
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (a *agent) IsBusy() bool {
|
||||
busy := false
|
||||
a.activeRequests.Range(func(key, value interface{}) bool {
|
||||
if cancelFunc, ok := value.(context.CancelFunc); ok {
|
||||
if cancelFunc != nil {
|
||||
busy = true
|
||||
return false // Stop iterating
|
||||
}
|
||||
}
|
||||
return true // Continue iterating
|
||||
})
|
||||
return busy
|
||||
}
|
||||
|
||||
func (a *agent) IsSessionBusy(sessionID string) bool {
|
||||
_, busy := a.activeRequests.Load(sessionID)
|
||||
return busy
|
||||
}
|
||||
|
||||
func (a *agent) generateTitle(ctx context.Context, sessionID string, content string) error {
|
||||
if content == "" {
|
||||
return nil
|
||||
}
|
||||
if a.titleProvider == nil {
|
||||
return nil
|
||||
}
|
||||
session, err := a.sessions.Get(ctx, sessionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
parts := []message.ContentPart{message.TextContent{Text: content}}
|
||||
response, err := a.titleProvider.SendMessages(
|
||||
ctx,
|
||||
[]message.Message{
|
||||
{
|
||||
Role: message.User,
|
||||
Parts: parts,
|
||||
},
|
||||
},
|
||||
make([]tools.BaseTool, 0),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
title := strings.TrimSpace(strings.ReplaceAll(response.Content, "\n", " "))
|
||||
if title == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
session.Title = title
|
||||
_, err = a.sessions.Update(ctx, session)
|
||||
return err
|
||||
}
|
||||
|
||||
func (a *agent) err(err error) AgentEvent {
|
||||
return AgentEvent{
|
||||
err: err,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *agent) Run(ctx context.Context, sessionID string, content string, attachments ...message.Attachment) (<-chan AgentEvent, error) {
|
||||
if !a.provider.Model().SupportsAttachments && attachments != nil {
|
||||
attachments = nil
|
||||
}
|
||||
events := make(chan AgentEvent)
|
||||
if a.IsSessionBusy(sessionID) {
|
||||
return nil, ErrSessionBusy
|
||||
}
|
||||
|
||||
genCtx, cancel := context.WithCancel(ctx)
|
||||
|
||||
a.activeRequests.Store(sessionID, cancel)
|
||||
go func() {
|
||||
slog.Debug("Request started", "sessionID", sessionID)
|
||||
defer logging.RecoverPanic("agent.Run", func() {
|
||||
events <- a.err(fmt.Errorf("panic while running the agent"))
|
||||
})
|
||||
var attachmentParts []message.ContentPart
|
||||
for _, attachment := range attachments {
|
||||
attachmentParts = append(attachmentParts, message.BinaryContent{Path: attachment.FilePath, MIMEType: attachment.MimeType, Data: attachment.Content})
|
||||
}
|
||||
result := a.processGeneration(genCtx, sessionID, content, attachmentParts)
|
||||
if result.Err() != nil && !errors.Is(result.Err(), ErrRequestCancelled) && !errors.Is(result.Err(), context.Canceled) {
|
||||
status.Error(result.Err().Error())
|
||||
}
|
||||
slog.Debug("Request completed", "sessionID", sessionID)
|
||||
a.activeRequests.Delete(sessionID)
|
||||
cancel()
|
||||
events <- result
|
||||
close(events)
|
||||
}()
|
||||
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func (a *agent) prepareMessageHistory(ctx context.Context, sessionID string) (session.Session, []message.Message, error) {
|
||||
currentSession, err := a.sessions.Get(ctx, sessionID)
|
||||
if err != nil {
|
||||
return currentSession, nil, fmt.Errorf("failed to get session: %w", err)
|
||||
}
|
||||
|
||||
var sessionMessages []message.Message
|
||||
if currentSession.Summary != "" && !currentSession.SummarizedAt.IsZero() {
|
||||
// If summary exists, only fetch messages after the summarization timestamp
|
||||
sessionMessages, err = a.messages.ListAfter(ctx, sessionID, currentSession.SummarizedAt)
|
||||
if err != nil {
|
||||
return currentSession, nil, fmt.Errorf("failed to list messages after summary: %w", err)
|
||||
}
|
||||
} else {
|
||||
// If no summary, fetch all messages
|
||||
sessionMessages, err = a.messages.List(ctx, sessionID)
|
||||
if err != nil {
|
||||
return currentSession, nil, fmt.Errorf("failed to list messages: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
var messages []message.Message
|
||||
if currentSession.Summary != "" && !currentSession.SummarizedAt.IsZero() {
|
||||
// If summary exists, create a temporary message for the summary
|
||||
summaryMessage := message.Message{
|
||||
Role: message.Assistant,
|
||||
Parts: []message.ContentPart{
|
||||
message.TextContent{Text: currentSession.Summary},
|
||||
},
|
||||
}
|
||||
// Start with the summary, then add messages after the summary timestamp
|
||||
messages = append([]message.Message{summaryMessage}, sessionMessages...)
|
||||
} else {
|
||||
// If no summary, just use all messages
|
||||
messages = sessionMessages
|
||||
}
|
||||
|
||||
return currentSession, messages, nil
|
||||
}
|
||||
|
||||
func (a *agent) triggerTitleGeneration(sessionID string, content string) {
|
||||
go func() {
|
||||
defer logging.RecoverPanic("agent.Run", func() {
|
||||
status.Error("panic while generating title")
|
||||
})
|
||||
titleErr := a.generateTitle(context.Background(), sessionID, content)
|
||||
if titleErr != nil {
|
||||
status.Error(fmt.Sprintf("failed to generate title: %v", titleErr))
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (a *agent) processGeneration(ctx context.Context, sessionID, content string, attachmentParts []message.ContentPart) AgentEvent {
|
||||
currentSession, sessionMessages, err := a.prepareMessageHistory(ctx, sessionID)
|
||||
if err != nil {
|
||||
return a.err(err)
|
||||
}
|
||||
|
||||
// If this is a new session, start title generation asynchronously
|
||||
if len(sessionMessages) == 0 && currentSession.Summary == "" {
|
||||
a.triggerTitleGeneration(sessionID, content)
|
||||
}
|
||||
|
||||
userMsg, err := a.createUserMessage(ctx, sessionID, content, attachmentParts)
|
||||
if err != nil {
|
||||
return a.err(fmt.Errorf("failed to create user message: %w", err))
|
||||
}
|
||||
|
||||
messages := append(sessionMessages, userMsg)
|
||||
|
||||
for {
|
||||
// Check for cancellation before each iteration
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return a.err(ctx.Err())
|
||||
default:
|
||||
// Continue processing
|
||||
}
|
||||
|
||||
// Check if auto-compaction is needed before calling the provider
|
||||
usagePercentage, needsCompaction, errEstimate := a.EstimateContextWindowUsage(ctx, sessionID)
|
||||
if errEstimate != nil {
|
||||
slog.Warn("Failed to estimate context window usage for auto-compaction", "error", errEstimate, "sessionID", sessionID)
|
||||
} else if needsCompaction {
|
||||
status.Info(fmt.Sprintf("Context window usage is at %.2f%%. Auto-compacting conversation...", usagePercentage))
|
||||
|
||||
// Run compaction synchronously
|
||||
compactCtx, cancelCompact := context.WithTimeout(ctx, 30*time.Second) // Use appropriate context
|
||||
errCompact := a.CompactSession(compactCtx, sessionID, true)
|
||||
cancelCompact()
|
||||
|
||||
if errCompact != nil {
|
||||
status.Warn(fmt.Sprintf("Auto-compaction failed: %v. Context window usage may continue to grow.", errCompact))
|
||||
} else {
|
||||
status.Info("Auto-compaction completed successfully.")
|
||||
// After compaction, message history needs to be re-prepared.
|
||||
// The 'messages' slice needs to be updated with the new summary and subsequent messages,
|
||||
// ensuring the latest user message is correctly appended.
|
||||
_, sessionMessagesFromCompact, errPrepare := a.prepareMessageHistory(ctx, sessionID)
|
||||
if errPrepare != nil {
|
||||
return a.err(fmt.Errorf("failed to re-prepare message history after compaction: %w", errPrepare))
|
||||
}
|
||||
messages = sessionMessagesFromCompact
|
||||
|
||||
// Ensure the user message that triggered this cycle is the last one.
|
||||
// 'userMsg' was created before this loop using a.createUserMessage.
|
||||
// It should be appended to the 'messages' slice if it's not already the last element.
|
||||
if len(messages) == 0 || (len(messages) > 0 && messages[len(messages)-1].ID != userMsg.ID) {
|
||||
messages = append(messages, userMsg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
agentMessage, toolResults, err := a.streamAndHandleEvents(ctx, sessionID, messages)
|
||||
if err != nil {
|
||||
if errors.Is(err, context.Canceled) {
|
||||
agentMessage.AddFinish(message.FinishReasonCanceled)
|
||||
a.messages.Update(context.Background(), agentMessage)
|
||||
return a.err(ErrRequestCancelled)
|
||||
}
|
||||
return a.err(fmt.Errorf("failed to process events: %w", err))
|
||||
}
|
||||
slog.Info("Result", "message", agentMessage.FinishReason(), "toolResults", toolResults)
|
||||
if (agentMessage.FinishReason() == message.FinishReasonToolUse) && toolResults != nil {
|
||||
// We are not done, we need to respond with the tool response
|
||||
messages = append(messages, agentMessage, *toolResults)
|
||||
continue
|
||||
}
|
||||
return AgentEvent{
|
||||
message: agentMessage,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (a *agent) createUserMessage(ctx context.Context, sessionID, content string, attachmentParts []message.ContentPart) (message.Message, error) {
|
||||
parts := []message.ContentPart{message.TextContent{Text: content}}
|
||||
parts = append(parts, attachmentParts...)
|
||||
return a.messages.Create(ctx, sessionID, message.CreateMessageParams{
|
||||
Role: message.User,
|
||||
Parts: parts,
|
||||
})
|
||||
}
|
||||
|
||||
func (a *agent) createToolResponseMessage(ctx context.Context, sessionID string, toolResults []message.ToolResult) (*message.Message, error) {
|
||||
if len(toolResults) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
parts := make([]message.ContentPart, 0, len(toolResults))
|
||||
for _, tr := range toolResults {
|
||||
parts = append(parts, tr)
|
||||
}
|
||||
|
||||
msg, err := a.messages.Create(ctx, sessionID, message.CreateMessageParams{
|
||||
Role: message.Tool,
|
||||
Parts: parts,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create tool response message: %w", err)
|
||||
}
|
||||
|
||||
return &msg, nil
|
||||
}
|
||||
|
||||
func (a *agent) streamAndHandleEvents(ctx context.Context, sessionID string, msgHistory []message.Message) (message.Message, *message.Message, error) {
|
||||
eventChan := a.provider.StreamResponse(ctx, msgHistory, a.tools)
|
||||
|
||||
assistantMsg, err := a.messages.Create(ctx, sessionID, message.CreateMessageParams{
|
||||
Role: message.Assistant,
|
||||
Parts: []message.ContentPart{},
|
||||
Model: a.provider.Model().ID,
|
||||
})
|
||||
if err != nil {
|
||||
return assistantMsg, nil, fmt.Errorf("failed to create assistant message: %w", err)
|
||||
}
|
||||
|
||||
// Add the session and message ID into the context if needed by tools.
|
||||
ctx = context.WithValue(ctx, tools.MessageIDContextKey, assistantMsg.ID)
|
||||
ctx = context.WithValue(ctx, tools.SessionIDContextKey, sessionID)
|
||||
|
||||
// Process each event in the stream.
|
||||
for event := range eventChan {
|
||||
if processErr := a.processEvent(ctx, sessionID, &assistantMsg, event); processErr != nil {
|
||||
a.finishMessage(ctx, &assistantMsg, message.FinishReasonCanceled)
|
||||
return assistantMsg, nil, processErr
|
||||
}
|
||||
if ctx.Err() != nil {
|
||||
a.finishMessage(context.Background(), &assistantMsg, message.FinishReasonCanceled)
|
||||
return assistantMsg, nil, ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
// If the assistant wants to use tools, execute them
|
||||
if assistantMsg.FinishReason() == message.FinishReasonToolUse {
|
||||
toolCalls := assistantMsg.ToolCalls()
|
||||
if len(toolCalls) > 0 {
|
||||
toolResults, err := a.executeToolCalls(ctx, toolCalls)
|
||||
if err != nil {
|
||||
if errors.Is(err, context.Canceled) {
|
||||
a.finishMessage(context.Background(), &assistantMsg, message.FinishReasonCanceled)
|
||||
}
|
||||
return assistantMsg, nil, err
|
||||
}
|
||||
|
||||
// Create a message with the tool results
|
||||
toolResponseMsg, err := a.createToolResponseMessage(ctx, sessionID, toolResults)
|
||||
if err != nil {
|
||||
return assistantMsg, nil, err
|
||||
}
|
||||
|
||||
return assistantMsg, toolResponseMsg, nil
|
||||
}
|
||||
}
|
||||
|
||||
return assistantMsg, nil, nil
|
||||
}
|
||||
|
||||
func (a *agent) executeToolCalls(ctx context.Context, toolCalls []message.ToolCall) ([]message.ToolResult, error) {
|
||||
toolResults := make([]message.ToolResult, len(toolCalls))
|
||||
|
||||
for i, toolCall := range toolCalls {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
// Make all future tool calls cancelled
|
||||
for j := i; j < len(toolCalls); j++ {
|
||||
toolResults[j] = message.ToolResult{
|
||||
ToolCallID: toolCalls[j].ID,
|
||||
Content: "Tool execution canceled by user",
|
||||
IsError: true,
|
||||
}
|
||||
}
|
||||
return toolResults, ctx.Err()
|
||||
default:
|
||||
// Continue processing
|
||||
var tool tools.BaseTool
|
||||
for _, availableTools := range a.tools {
|
||||
if availableTools.Info().Name == toolCall.Name {
|
||||
tool = availableTools
|
||||
}
|
||||
}
|
||||
|
||||
// Tool not found
|
||||
if tool == nil {
|
||||
toolResults[i] = message.ToolResult{
|
||||
ToolCallID: toolCall.ID,
|
||||
Content: fmt.Sprintf("Tool not found: %s", toolCall.Name),
|
||||
IsError: true,
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
toolResult, toolErr := tool.Run(ctx, tools.ToolCall{
|
||||
ID: toolCall.ID,
|
||||
Name: toolCall.Name,
|
||||
Input: toolCall.Input,
|
||||
})
|
||||
|
||||
if toolErr != nil {
|
||||
if errors.Is(toolErr, permission.ErrorPermissionDenied) {
|
||||
toolResults[i] = message.ToolResult{
|
||||
ToolCallID: toolCall.ID,
|
||||
Content: "Permission denied",
|
||||
IsError: true,
|
||||
}
|
||||
// Cancel all remaining tool calls if permission is denied
|
||||
for j := i + 1; j < len(toolCalls); j++ {
|
||||
toolResults[j] = message.ToolResult{
|
||||
ToolCallID: toolCalls[j].ID,
|
||||
Content: "Tool execution canceled by user",
|
||||
IsError: true,
|
||||
}
|
||||
}
|
||||
return toolResults, nil
|
||||
}
|
||||
|
||||
// Handle other errors
|
||||
toolResults[i] = message.ToolResult{
|
||||
ToolCallID: toolCall.ID,
|
||||
Content: toolErr.Error(),
|
||||
IsError: true,
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
toolResults[i] = message.ToolResult{
|
||||
ToolCallID: toolCall.ID,
|
||||
Content: toolResult.Content,
|
||||
Metadata: toolResult.Metadata,
|
||||
IsError: toolResult.IsError,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return toolResults, nil
|
||||
}
|
||||
|
||||
func (a *agent) finishMessage(ctx context.Context, msg *message.Message, finishReson message.FinishReason) {
|
||||
msg.AddFinish(finishReson)
|
||||
_, _ = a.messages.Update(ctx, *msg)
|
||||
}
|
||||
|
||||
func (a *agent) processEvent(ctx context.Context, sessionID string, assistantMsg *message.Message, event provider.ProviderEvent) error {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
default:
|
||||
// Continue processing
|
||||
}
|
||||
|
||||
switch event.Type {
|
||||
case provider.EventThinkingDelta:
|
||||
assistantMsg.AppendReasoningContent(event.Content)
|
||||
_, err := a.messages.Update(ctx, *assistantMsg)
|
||||
return err
|
||||
case provider.EventContentDelta:
|
||||
assistantMsg.AppendContent(event.Content)
|
||||
_, err := a.messages.Update(ctx, *assistantMsg)
|
||||
return err
|
||||
case provider.EventToolUseStart:
|
||||
assistantMsg.AddToolCall(*event.ToolCall)
|
||||
_, err := a.messages.Update(ctx, *assistantMsg)
|
||||
return err
|
||||
case provider.EventToolUseStop:
|
||||
assistantMsg.FinishToolCall(event.ToolCall.ID)
|
||||
_, err := a.messages.Update(ctx, *assistantMsg)
|
||||
return err
|
||||
case provider.EventError:
|
||||
if errors.Is(event.Error, context.Canceled) {
|
||||
status.Info(fmt.Sprintf("Event processing canceled for session: %s", sessionID))
|
||||
return context.Canceled
|
||||
}
|
||||
status.Error(event.Error.Error())
|
||||
return event.Error
|
||||
case provider.EventComplete:
|
||||
assistantMsg.SetToolCalls(event.Response.ToolCalls)
|
||||
assistantMsg.AddFinish(event.Response.FinishReason)
|
||||
if _, err := a.messages.Update(ctx, *assistantMsg); err != nil {
|
||||
return fmt.Errorf("failed to update message: %w", err)
|
||||
}
|
||||
return a.TrackUsage(ctx, sessionID, a.provider.Model(), event.Response.Usage)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *agent) GetUsage(ctx context.Context, sessionID string) (*int64, error) {
|
||||
session, err := a.sessions.Get(ctx, sessionID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get session: %w", err)
|
||||
}
|
||||
|
||||
usage := session.PromptTokens + session.CompletionTokens
|
||||
return &usage, nil
|
||||
}
|
||||
|
||||
func (a *agent) EstimateContextWindowUsage(ctx context.Context, sessionID string) (float64, bool, error) {
|
||||
session, err := a.sessions.Get(ctx, sessionID)
|
||||
if err != nil {
|
||||
return 0, false, fmt.Errorf("failed to get session: %w", err)
|
||||
}
|
||||
|
||||
// Get the model's context window size
|
||||
model := a.provider.Model()
|
||||
contextWindow := model.ContextWindow
|
||||
if contextWindow <= 0 {
|
||||
// Default to a reasonable size if not specified
|
||||
contextWindow = 100000
|
||||
}
|
||||
|
||||
// Calculate current token usage
|
||||
currentTokens := session.PromptTokens + session.CompletionTokens
|
||||
|
||||
// Get the max tokens setting for the agent
|
||||
maxTokens := a.provider.MaxTokens()
|
||||
|
||||
// Calculate percentage of context window used
|
||||
usagePercentage := float64(currentTokens) / float64(contextWindow)
|
||||
|
||||
// Check if we need to auto-compact
|
||||
// Auto-compact when:
|
||||
// 1. Usage exceeds 90% of context window, OR
|
||||
// 2. Current usage + maxTokens would exceed 100% of context window
|
||||
needsCompaction := usagePercentage >= 0.9 ||
|
||||
float64(currentTokens+maxTokens) > float64(contextWindow)
|
||||
|
||||
return usagePercentage * 100, needsCompaction, nil
|
||||
}
|
||||
|
||||
func (a *agent) TrackUsage(ctx context.Context, sessionID string, model models.Model, usage provider.TokenUsage) error {
|
||||
sess, err := a.sessions.Get(ctx, sessionID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get session: %w", err)
|
||||
}
|
||||
|
||||
cost := model.CostPer1MInCached/1e6*float64(usage.CacheCreationTokens) +
|
||||
model.CostPer1MOutCached/1e6*float64(usage.CacheReadTokens) +
|
||||
model.CostPer1MIn/1e6*float64(usage.InputTokens) +
|
||||
model.CostPer1MOut/1e6*float64(usage.OutputTokens)
|
||||
|
||||
sess.Cost += cost
|
||||
sess.CompletionTokens = usage.OutputTokens + usage.CacheReadTokens
|
||||
sess.PromptTokens = usage.InputTokens + usage.CacheCreationTokens
|
||||
|
||||
_, err = a.sessions.Update(ctx, sess)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to save session: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *agent) Update(agentName config.AgentName, modelID models.ModelID) (models.Model, error) {
|
||||
if a.IsBusy() {
|
||||
return models.Model{}, fmt.Errorf("cannot change model while processing requests")
|
||||
}
|
||||
|
||||
if err := config.UpdateAgentModel(agentName, modelID); err != nil {
|
||||
return models.Model{}, fmt.Errorf("failed to update config: %w", err)
|
||||
}
|
||||
|
||||
provider, err := createAgentProvider(agentName)
|
||||
if err != nil {
|
||||
return models.Model{}, fmt.Errorf("failed to create provider for model %s: %w", modelID, err)
|
||||
}
|
||||
|
||||
a.provider = provider
|
||||
|
||||
return a.provider.Model(), nil
|
||||
}
|
||||
|
||||
func (a *agent) CompactSession(ctx context.Context, sessionID string, force bool) error {
|
||||
// Check if the session is busy
|
||||
if a.IsSessionBusy(sessionID) && !force {
|
||||
return ErrSessionBusy
|
||||
}
|
||||
|
||||
// Create a cancellable context
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
|
||||
// Mark the session as busy during compaction
|
||||
compactionCancelFunc := func() {}
|
||||
a.activeRequests.Store(sessionID+"-compact", compactionCancelFunc)
|
||||
defer a.activeRequests.Delete(sessionID + "-compact")
|
||||
|
||||
// Fetch the session
|
||||
session, err := a.sessions.Get(ctx, sessionID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get session: %w", err)
|
||||
}
|
||||
|
||||
// Fetch all messages for the session
|
||||
sessionMessages, err := a.messages.List(ctx, sessionID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to list messages: %w", err)
|
||||
}
|
||||
|
||||
var existingSummary string
|
||||
if session.Summary != "" && !session.SummarizedAt.IsZero() {
|
||||
// Filter messages that were created after the last summarization
|
||||
var newMessages []message.Message
|
||||
for _, msg := range sessionMessages {
|
||||
if msg.CreatedAt.After(session.SummarizedAt) {
|
||||
newMessages = append(newMessages, msg)
|
||||
}
|
||||
}
|
||||
sessionMessages = newMessages
|
||||
existingSummary = session.Summary
|
||||
}
|
||||
|
||||
// If there are no messages to summarize and no existing summary, return early
|
||||
if len(sessionMessages) == 0 && existingSummary == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
messages := []message.Message{
|
||||
message.Message{
|
||||
Role: message.System,
|
||||
Parts: []message.ContentPart{
|
||||
message.TextContent{
|
||||
Text: `You are a helpful AI assistant tasked with summarizing conversations.
|
||||
|
||||
When asked to summarize, provide a detailed but concise summary of the conversation.
|
||||
Focus on information that would be helpful for continuing the conversation, including:
|
||||
- What was done
|
||||
- What is currently being worked on
|
||||
- Which files are being modified
|
||||
- What needs to be done next
|
||||
|
||||
Your summary should be comprehensive enough to provide context but concise enough to be quickly understood.`,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// If there's an existing summary, include it
|
||||
if existingSummary != "" {
|
||||
messages = append(messages, message.Message{
|
||||
Role: message.Assistant,
|
||||
Parts: []message.ContentPart{
|
||||
message.TextContent{
|
||||
Text: existingSummary,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// Add all messages since the last summarized message
|
||||
messages = append(messages, sessionMessages...)
|
||||
|
||||
// Add a final user message requesting the summary
|
||||
messages = append(messages, message.Message{
|
||||
Role: message.User,
|
||||
Parts: []message.ContentPart{
|
||||
message.TextContent{
|
||||
Text: "Provide a detailed but concise summary of our conversation above. Focus on information that would be helpful for continuing the conversation, including what we did, what we're doing, which files we're working on, and what we're going to do next.",
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
// Call provider to get the summary
|
||||
response, err := a.provider.SendMessages(ctx, messages, a.tools)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get summary from the assistant: %w", err)
|
||||
}
|
||||
|
||||
// Extract the summary text
|
||||
summaryText := strings.TrimSpace(response.Content)
|
||||
if summaryText == "" {
|
||||
return fmt.Errorf("received empty summary from the assistant")
|
||||
}
|
||||
|
||||
// Update the session with the new summary
|
||||
session.Summary = summaryText
|
||||
session.SummarizedAt = time.Now()
|
||||
|
||||
// Save the updated session
|
||||
_, err = a.sessions.Update(ctx, session)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to save session with summary: %w", err)
|
||||
}
|
||||
|
||||
// Track token usage
|
||||
err = a.TrackUsage(ctx, sessionID, a.provider.Model(), response.Usage)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to track usage: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func createAgentProvider(agentName config.AgentName) (provider.Provider, error) {
|
||||
cfg := config.Get()
|
||||
agentConfig, ok := cfg.Agents[agentName]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("agent %s not found", agentName)
|
||||
}
|
||||
model, ok := models.SupportedModels[agentConfig.Model]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("model %s not supported", agentConfig.Model)
|
||||
}
|
||||
|
||||
providerCfg, ok := cfg.Providers[model.Provider]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("provider %s not supported", model.Provider)
|
||||
}
|
||||
if providerCfg.Disabled {
|
||||
return nil, fmt.Errorf("provider %s is not enabled", model.Provider)
|
||||
}
|
||||
maxTokens := model.DefaultMaxTokens
|
||||
if agentConfig.MaxTokens > 0 {
|
||||
maxTokens = agentConfig.MaxTokens
|
||||
}
|
||||
opts := []provider.ProviderClientOption{
|
||||
provider.WithAPIKey(providerCfg.APIKey),
|
||||
provider.WithModel(model),
|
||||
provider.WithSystemMessage(prompt.GetAgentPrompt(agentName, model.Provider)),
|
||||
provider.WithMaxTokens(maxTokens),
|
||||
}
|
||||
if model.Provider == models.ProviderOpenAI && model.CanReason {
|
||||
opts = append(
|
||||
opts,
|
||||
provider.WithOpenAIOptions(
|
||||
provider.WithReasoningEffort(agentConfig.ReasoningEffort),
|
||||
),
|
||||
)
|
||||
} else if model.Provider == models.ProviderAnthropic && model.CanReason && agentName == config.AgentPrimary {
|
||||
opts = append(
|
||||
opts,
|
||||
provider.WithAnthropicOptions(
|
||||
provider.WithAnthropicShouldThinkFn(provider.DefaultShouldThinkFn),
|
||||
),
|
||||
)
|
||||
}
|
||||
agentProvider, err := provider.NewProvider(
|
||||
model.Provider,
|
||||
opts...,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not create provider: %v", err)
|
||||
}
|
||||
|
||||
return agentProvider, nil
|
||||
}
|
|
@ -1,198 +0,0 @@
|
|||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/sst/opencode/internal/config"
|
||||
"github.com/sst/opencode/internal/llm/tools"
|
||||
"github.com/sst/opencode/internal/permission"
|
||||
"github.com/sst/opencode/internal/version"
|
||||
"log/slog"
|
||||
|
||||
"github.com/mark3labs/mcp-go/client"
|
||||
"github.com/mark3labs/mcp-go/mcp"
|
||||
)
|
||||
|
||||
type mcpTool struct {
|
||||
mcpName string
|
||||
tool mcp.Tool
|
||||
mcpConfig config.MCPServer
|
||||
permissions permission.Service
|
||||
}
|
||||
|
||||
type MCPClient interface {
|
||||
Initialize(
|
||||
ctx context.Context,
|
||||
request mcp.InitializeRequest,
|
||||
) (*mcp.InitializeResult, error)
|
||||
ListTools(ctx context.Context, request mcp.ListToolsRequest) (*mcp.ListToolsResult, error)
|
||||
CallTool(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error)
|
||||
Close() error
|
||||
}
|
||||
|
||||
func (b *mcpTool) Info() tools.ToolInfo {
|
||||
return tools.ToolInfo{
|
||||
Name: fmt.Sprintf("%s_%s", b.mcpName, b.tool.Name),
|
||||
Description: b.tool.Description,
|
||||
Parameters: b.tool.InputSchema.Properties,
|
||||
Required: b.tool.InputSchema.Required,
|
||||
}
|
||||
}
|
||||
|
||||
func runTool(ctx context.Context, c MCPClient, toolName string, input string) (tools.ToolResponse, error) {
|
||||
defer c.Close()
|
||||
initRequest := mcp.InitializeRequest{}
|
||||
initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION
|
||||
initRequest.Params.ClientInfo = mcp.Implementation{
|
||||
Name: "OpenCode",
|
||||
Version: version.Version,
|
||||
}
|
||||
|
||||
_, err := c.Initialize(ctx, initRequest)
|
||||
if err != nil {
|
||||
return tools.NewTextErrorResponse(err.Error()), nil
|
||||
}
|
||||
|
||||
toolRequest := mcp.CallToolRequest{}
|
||||
toolRequest.Params.Name = toolName
|
||||
var args map[string]any
|
||||
if err = json.Unmarshal([]byte(input), &args); err != nil {
|
||||
return tools.NewTextErrorResponse(fmt.Sprintf("error parsing parameters: %s", err)), nil
|
||||
}
|
||||
toolRequest.Params.Arguments = args
|
||||
result, err := c.CallTool(ctx, toolRequest)
|
||||
if err != nil {
|
||||
return tools.NewTextErrorResponse(err.Error()), nil
|
||||
}
|
||||
|
||||
output := ""
|
||||
for _, v := range result.Content {
|
||||
if v, ok := v.(mcp.TextContent); ok {
|
||||
output = v.Text
|
||||
} else {
|
||||
output = fmt.Sprintf("%v", v)
|
||||
}
|
||||
}
|
||||
|
||||
return tools.NewTextResponse(output), nil
|
||||
}
|
||||
|
||||
func (b *mcpTool) Run(ctx context.Context, params tools.ToolCall) (tools.ToolResponse, error) {
|
||||
sessionID, messageID := tools.GetContextValues(ctx)
|
||||
if sessionID == "" || messageID == "" {
|
||||
return tools.ToolResponse{}, fmt.Errorf("session ID and message ID are required for creating a new file")
|
||||
}
|
||||
permissionDescription := fmt.Sprintf("execute %s with the following parameters: %s", b.Info().Name, params.Input)
|
||||
p := b.permissions.Request(
|
||||
ctx,
|
||||
permission.CreatePermissionRequest{
|
||||
SessionID: sessionID,
|
||||
Path: config.WorkingDirectory(),
|
||||
ToolName: b.Info().Name,
|
||||
Action: "execute",
|
||||
Description: permissionDescription,
|
||||
Params: params.Input,
|
||||
},
|
||||
)
|
||||
if !p {
|
||||
return tools.NewTextErrorResponse("permission denied"), nil
|
||||
}
|
||||
|
||||
switch b.mcpConfig.Type {
|
||||
case config.MCPStdio:
|
||||
c, err := client.NewStdioMCPClient(
|
||||
b.mcpConfig.Command,
|
||||
b.mcpConfig.Env,
|
||||
b.mcpConfig.Args...,
|
||||
)
|
||||
if err != nil {
|
||||
return tools.NewTextErrorResponse(err.Error()), nil
|
||||
}
|
||||
return runTool(ctx, c, b.tool.Name, params.Input)
|
||||
case config.MCPSse:
|
||||
c, err := client.NewSSEMCPClient(
|
||||
b.mcpConfig.URL,
|
||||
client.WithHeaders(b.mcpConfig.Headers),
|
||||
)
|
||||
if err != nil {
|
||||
return tools.NewTextErrorResponse(err.Error()), nil
|
||||
}
|
||||
return runTool(ctx, c, b.tool.Name, params.Input)
|
||||
}
|
||||
|
||||
return tools.NewTextErrorResponse("invalid mcp type"), nil
|
||||
}
|
||||
|
||||
func NewMcpTool(name string, tool mcp.Tool, permissions permission.Service, mcpConfig config.MCPServer) tools.BaseTool {
|
||||
return &mcpTool{
|
||||
mcpName: name,
|
||||
tool: tool,
|
||||
mcpConfig: mcpConfig,
|
||||
permissions: permissions,
|
||||
}
|
||||
}
|
||||
|
||||
var mcpTools []tools.BaseTool
|
||||
|
||||
func getTools(ctx context.Context, name string, m config.MCPServer, permissions permission.Service, c MCPClient) []tools.BaseTool {
|
||||
var stdioTools []tools.BaseTool
|
||||
initRequest := mcp.InitializeRequest{}
|
||||
initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION
|
||||
initRequest.Params.ClientInfo = mcp.Implementation{
|
||||
Name: "OpenCode",
|
||||
Version: version.Version,
|
||||
}
|
||||
|
||||
_, err := c.Initialize(ctx, initRequest)
|
||||
if err != nil {
|
||||
slog.Error("error initializing mcp client", "error", err)
|
||||
return stdioTools
|
||||
}
|
||||
toolsRequest := mcp.ListToolsRequest{}
|
||||
tools, err := c.ListTools(ctx, toolsRequest)
|
||||
if err != nil {
|
||||
slog.Error("error listing tools", "error", err)
|
||||
return stdioTools
|
||||
}
|
||||
for _, t := range tools.Tools {
|
||||
stdioTools = append(stdioTools, NewMcpTool(name, t, permissions, m))
|
||||
}
|
||||
defer c.Close()
|
||||
return stdioTools
|
||||
}
|
||||
|
||||
func GetMcpTools(ctx context.Context, permissions permission.Service) []tools.BaseTool {
|
||||
if len(mcpTools) > 0 {
|
||||
return mcpTools
|
||||
}
|
||||
for name, m := range config.Get().MCPServers {
|
||||
switch m.Type {
|
||||
case config.MCPStdio:
|
||||
c, err := client.NewStdioMCPClient(
|
||||
m.Command,
|
||||
m.Env,
|
||||
m.Args...,
|
||||
)
|
||||
if err != nil {
|
||||
slog.Error("error creating mcp client", "error", err)
|
||||
continue
|
||||
}
|
||||
|
||||
mcpTools = append(mcpTools, getTools(ctx, name, m, permissions, c)...)
|
||||
case config.MCPSse:
|
||||
c, err := client.NewSSEMCPClient(
|
||||
m.URL,
|
||||
client.WithHeaders(m.Headers),
|
||||
)
|
||||
if err != nil {
|
||||
slog.Error("error creating mcp client", "error", err)
|
||||
continue
|
||||
}
|
||||
mcpTools = append(mcpTools, getTools(ctx, name, m, permissions, c)...)
|
||||
}
|
||||
}
|
||||
|
||||
return mcpTools
|
||||
}
|
|
@ -1,79 +0,0 @@
|
|||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/sst/opencode/internal/history"
|
||||
"github.com/sst/opencode/internal/llm/tools"
|
||||
"github.com/sst/opencode/internal/lsp"
|
||||
"github.com/sst/opencode/internal/message"
|
||||
"github.com/sst/opencode/internal/permission"
|
||||
"github.com/sst/opencode/internal/session"
|
||||
)
|
||||
|
||||
func PrimaryAgentTools(
|
||||
permissions permission.Service,
|
||||
sessions session.Service,
|
||||
messages message.Service,
|
||||
history history.Service,
|
||||
lspClients map[string]*lsp.Client,
|
||||
) []tools.BaseTool {
|
||||
ctx := context.Background()
|
||||
mcpTools := GetMcpTools(ctx, permissions)
|
||||
|
||||
// Create the list of tools
|
||||
toolsList := []tools.BaseTool{
|
||||
tools.NewBashTool(permissions),
|
||||
tools.NewEditTool(lspClients, permissions, history),
|
||||
tools.NewFetchTool(permissions),
|
||||
tools.NewGlobTool(),
|
||||
tools.NewGrepTool(),
|
||||
tools.NewLsTool(),
|
||||
tools.NewViewTool(lspClients),
|
||||
tools.NewPatchTool(lspClients, permissions, history),
|
||||
tools.NewWriteTool(lspClients, permissions, history),
|
||||
tools.NewDiagnosticsTool(lspClients),
|
||||
tools.NewDefinitionTool(lspClients),
|
||||
tools.NewReferencesTool(lspClients),
|
||||
tools.NewDocSymbolsTool(lspClients),
|
||||
tools.NewWorkspaceSymbolsTool(lspClients),
|
||||
tools.NewCodeActionTool(lspClients),
|
||||
NewAgentTool(sessions, messages, lspClients),
|
||||
}
|
||||
|
||||
// Create a map of tools for the batch tool
|
||||
toolsMap := make(map[string]tools.BaseTool)
|
||||
for _, tool := range toolsList {
|
||||
toolsMap[tool.Info().Name] = tool
|
||||
}
|
||||
|
||||
// Add the batch tool with access to all other tools
|
||||
toolsList = append(toolsList, tools.NewBatchTool(toolsMap))
|
||||
|
||||
return append(toolsList, mcpTools...)
|
||||
}
|
||||
|
||||
func TaskAgentTools(lspClients map[string]*lsp.Client) []tools.BaseTool {
|
||||
// Create the list of tools
|
||||
toolsList := []tools.BaseTool{
|
||||
tools.NewGlobTool(),
|
||||
tools.NewGrepTool(),
|
||||
tools.NewLsTool(),
|
||||
tools.NewViewTool(lspClients),
|
||||
tools.NewDefinitionTool(lspClients),
|
||||
tools.NewReferencesTool(lspClients),
|
||||
tools.NewDocSymbolsTool(lspClients),
|
||||
tools.NewWorkspaceSymbolsTool(lspClients),
|
||||
}
|
||||
|
||||
// Create a map of tools for the batch tool
|
||||
toolsMap := make(map[string]tools.BaseTool)
|
||||
for _, tool := range toolsList {
|
||||
toolsMap[tool.Info().Name] = tool
|
||||
}
|
||||
|
||||
// Add the batch tool with access to all other tools
|
||||
toolsList = append(toolsList, tools.NewBatchTool(toolsMap))
|
||||
|
||||
return toolsList
|
||||
}
|
|
@ -3,7 +3,6 @@ package app
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"maps"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
|
@ -12,7 +11,6 @@ import (
|
|||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/sst/opencode/internal/config"
|
||||
"github.com/sst/opencode/internal/fileutil"
|
||||
"github.com/sst/opencode/internal/lsp"
|
||||
"github.com/sst/opencode/internal/message"
|
||||
"github.com/sst/opencode/internal/session"
|
||||
"github.com/sst/opencode/internal/status"
|
||||
|
@ -23,23 +21,21 @@ import (
|
|||
)
|
||||
|
||||
type App struct {
|
||||
State map[string]any
|
||||
Client *client.ClientWithResponses
|
||||
Events *client.Client
|
||||
State map[string]any
|
||||
Session *client.SessionInfo
|
||||
Messages []client.SessionMessage
|
||||
|
||||
CurrentSession *session.Session
|
||||
Logs any // TODO: Define LogService interface when needed
|
||||
Sessions SessionService
|
||||
Messages MessageService
|
||||
History any // TODO: Define HistoryService interface when needed
|
||||
Permissions any // TODO: Define PermissionService interface when needed
|
||||
Status status.Service
|
||||
Client *client.ClientWithResponses
|
||||
Events *client.Client
|
||||
CurrentSessionOLD *session.Session
|
||||
SessionsOLD SessionService
|
||||
MessagesOLD MessageService
|
||||
LogsOLD any // TODO: Define LogService interface when needed
|
||||
HistoryOLD any // TODO: Define HistoryService interface when needed
|
||||
PermissionsOLD any // TODO: Define PermissionService interface when needed
|
||||
Status status.Service
|
||||
|
||||
PrimaryAgent AgentService
|
||||
|
||||
LSPClients map[string]*lsp.Client
|
||||
|
||||
clientsMutex sync.RWMutex
|
||||
PrimaryAgentOLD AgentService
|
||||
|
||||
watcherCancelFuncs []context.CancelFunc
|
||||
cancelFuncsMutex sync.Mutex
|
||||
|
@ -80,20 +76,20 @@ func New(ctx context.Context) (*App, error) {
|
|||
agentBridge := NewAgentServiceBridge(httpClient)
|
||||
|
||||
app := &App{
|
||||
State: make(map[string]any),
|
||||
Client: httpClient,
|
||||
Events: eventClient,
|
||||
CurrentSession: &session.Session{},
|
||||
Sessions: sessionBridge,
|
||||
Messages: messageBridge,
|
||||
PrimaryAgent: agentBridge,
|
||||
Status: status.GetService(),
|
||||
LSPClients: make(map[string]*lsp.Client),
|
||||
State: make(map[string]any),
|
||||
Client: httpClient,
|
||||
Events: eventClient,
|
||||
Session: &client.SessionInfo{},
|
||||
CurrentSessionOLD: &session.Session{},
|
||||
SessionsOLD: sessionBridge,
|
||||
MessagesOLD: messageBridge,
|
||||
PrimaryAgentOLD: agentBridge,
|
||||
Status: status.GetService(),
|
||||
|
||||
// TODO: These services need API endpoints:
|
||||
Logs: nil, // logging.GetService(),
|
||||
History: nil, // history.GetService(),
|
||||
Permissions: nil, // permission.GetService(),
|
||||
LogsOLD: nil, // logging.GetService(),
|
||||
HistoryOLD: nil, // history.GetService(),
|
||||
PermissionsOLD: nil, // permission.GetService(),
|
||||
}
|
||||
|
||||
// Initialize theme based on configuration
|
||||
|
@ -105,30 +101,28 @@ func New(ctx context.Context) (*App, error) {
|
|||
// Create creates a new session
|
||||
func (a *App) SendChatMessage(ctx context.Context, text string, attachments []message.Attachment) tea.Cmd {
|
||||
var cmds []tea.Cmd
|
||||
if a.CurrentSession.ID == "" {
|
||||
if a.Session.Id == "" {
|
||||
resp, err := a.Client.PostSessionCreateWithResponse(ctx)
|
||||
if err != nil {
|
||||
// return session.Session{}, err
|
||||
status.Error(err.Error())
|
||||
return nil
|
||||
}
|
||||
if resp.StatusCode() != 200 {
|
||||
// return session.Session{}, fmt.Errorf("failed to create session: %d", resp.StatusCode())
|
||||
status.Error(fmt.Sprintf("failed to create session: %d", resp.StatusCode()))
|
||||
return nil
|
||||
}
|
||||
info := resp.JSON200
|
||||
|
||||
// Convert to old session type
|
||||
info := resp.JSON200
|
||||
a.Session = info
|
||||
|
||||
// Convert to old session type for backwards compatibility
|
||||
newSession := session.Session{
|
||||
ID: info.Id,
|
||||
Title: info.Title,
|
||||
CreatedAt: time.Now(), // API doesn't provide this yet
|
||||
UpdatedAt: time.Now(), // API doesn't provide this yet
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
status.Error(err.Error())
|
||||
return nil
|
||||
}
|
||||
|
||||
a.CurrentSession = &newSession
|
||||
a.CurrentSessionOLD = &newSession
|
||||
|
||||
cmds = append(cmds, util.CmdHandler(state.SessionSelectedMsg(&newSession)))
|
||||
}
|
||||
|
@ -147,7 +141,7 @@ func (a *App) SendChatMessage(ctx context.Context, text string, attachments []me
|
|||
parts := []client.SessionMessagePart{part}
|
||||
|
||||
go a.Client.PostSessionChatWithResponse(ctx, client.PostSessionChatJSONRequestBody{
|
||||
SessionID: a.CurrentSession.ID,
|
||||
SessionID: a.Session.Id,
|
||||
Parts: parts,
|
||||
ProviderID: "anthropic",
|
||||
ModelID: "claude-sonnet-4-20250514",
|
||||
|
@ -234,18 +228,4 @@ func (app *App) Shutdown() {
|
|||
}
|
||||
app.cancelFuncsMutex.Unlock()
|
||||
app.watcherWG.Wait()
|
||||
|
||||
// Perform additional cleanup for LSP clients
|
||||
app.clientsMutex.RLock()
|
||||
clients := make(map[string]*lsp.Client, len(app.LSPClients))
|
||||
maps.Copy(clients, app.LSPClients)
|
||||
app.clientsMutex.RUnlock()
|
||||
|
||||
for name, client := range clients {
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
if err := client.Shutdown(shutdownCtx); err != nil {
|
||||
slog.Error("Failed to shutdown LSP client", "name", name, "error", err)
|
||||
}
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -143,7 +143,7 @@ func (m *editorCmp) Init() tea.Cmd {
|
|||
}
|
||||
|
||||
func (m *editorCmp) send() tea.Cmd {
|
||||
if m.app.PrimaryAgent.IsSessionBusy(m.app.CurrentSession.ID) {
|
||||
if m.app.PrimaryAgentOLD.IsSessionBusy(m.app.CurrentSessionOLD.ID) {
|
||||
status.Warn("Agent is working, please wait...")
|
||||
return nil
|
||||
}
|
||||
|
@ -217,7 +217,7 @@ func (m *editorCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||
return m, nil
|
||||
}
|
||||
if key.Matches(msg, editorMaps.OpenEditor) {
|
||||
if m.app.PrimaryAgent.IsSessionBusy(m.app.CurrentSession.ID) {
|
||||
if m.app.PrimaryAgentOLD.IsSessionBusy(m.app.CurrentSessionOLD.ID) {
|
||||
status.Warn("Agent is working, please wait...")
|
||||
return m, nil
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package chat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
@ -12,12 +11,12 @@ import (
|
|||
"github.com/charmbracelet/x/ansi"
|
||||
"github.com/sst/opencode/internal/config"
|
||||
"github.com/sst/opencode/internal/diff"
|
||||
"github.com/sst/opencode/internal/llm/agent"
|
||||
"github.com/sst/opencode/internal/llm/models"
|
||||
"github.com/sst/opencode/internal/llm/tools"
|
||||
"github.com/sst/opencode/internal/message"
|
||||
"github.com/sst/opencode/internal/tui/styles"
|
||||
"github.com/sst/opencode/internal/tui/theme"
|
||||
"github.com/sst/opencode/pkg/client"
|
||||
)
|
||||
|
||||
type uiMessageType int
|
||||
|
@ -33,8 +32,6 @@ const (
|
|||
type uiMessage struct {
|
||||
ID string
|
||||
messageType uiMessageType
|
||||
position int
|
||||
height int
|
||||
content string
|
||||
}
|
||||
|
||||
|
@ -48,7 +45,7 @@ func renderMessage(msg string, isUser bool, isFocused bool, width int, info ...s
|
|||
t := theme.CurrentTheme()
|
||||
|
||||
style := styles.BaseStyle().
|
||||
Width(width - 1).
|
||||
// Width(width - 1).
|
||||
BorderLeft(true).
|
||||
Foreground(t.TextMuted()).
|
||||
BorderForeground(t.Primary()).
|
||||
|
@ -79,28 +76,29 @@ func renderMessage(msg string, isUser bool, isFocused bool, width int, info ...s
|
|||
return rendered
|
||||
}
|
||||
|
||||
func renderUserMessage(msg message.Message, isFocused bool, width int, position int) uiMessage {
|
||||
var styledAttachments []string
|
||||
func renderUserMessage(msg client.SessionMessage, isFocused bool, width int, position int) uiMessage {
|
||||
// var styledAttachments []string
|
||||
t := theme.CurrentTheme()
|
||||
baseStyle := styles.BaseStyle()
|
||||
attachmentStyles := baseStyle.
|
||||
MarginLeft(1).
|
||||
Background(t.TextMuted()).
|
||||
Foreground(t.Text())
|
||||
for _, attachment := range msg.BinaryContent() {
|
||||
file := filepath.Base(attachment.Path)
|
||||
var filename string
|
||||
if len(file) > 10 {
|
||||
filename = fmt.Sprintf(" %s %s...", styles.DocumentIcon, file[0:7])
|
||||
} else {
|
||||
filename = fmt.Sprintf(" %s %s", styles.DocumentIcon, file)
|
||||
}
|
||||
styledAttachments = append(styledAttachments, attachmentStyles.Render(filename))
|
||||
}
|
||||
// attachmentStyles := baseStyle.
|
||||
// MarginLeft(1).
|
||||
// Background(t.TextMuted()).
|
||||
// Foreground(t.Text())
|
||||
// for _, attachment := range msg.BinaryContent() {
|
||||
// file := filepath.Base(attachment.Path)
|
||||
// var filename string
|
||||
// if len(file) > 10 {
|
||||
// filename = fmt.Sprintf(" %s %s...", styles.DocumentIcon, file[0:7])
|
||||
// } else {
|
||||
// filename = fmt.Sprintf(" %s %s", styles.DocumentIcon, file)
|
||||
// }
|
||||
// styledAttachments = append(styledAttachments, attachmentStyles.Render(filename))
|
||||
// }
|
||||
|
||||
info := []string{}
|
||||
|
||||
// Add timestamp info
|
||||
info := []string{}
|
||||
timestamp := msg.CreatedAt.Local().Format("02 Jan 2006 03:04 PM")
|
||||
timestamp := time.UnixMilli(int64(msg.Metadata.Time.Created)).Local().Format("02 Jan 2006 03:04 PM")
|
||||
username, _ := config.GetUsername()
|
||||
info = append(info, baseStyle.
|
||||
Width(width-1).
|
||||
|
@ -109,17 +107,27 @@ func renderUserMessage(msg message.Message, isFocused bool, width int, position
|
|||
)
|
||||
|
||||
content := ""
|
||||
if len(styledAttachments) > 0 {
|
||||
attachmentContent := baseStyle.Width(width).Render(lipgloss.JoinHorizontal(lipgloss.Left, styledAttachments...))
|
||||
content = renderMessage(msg.Content().String(), true, isFocused, width, append(info, attachmentContent)...)
|
||||
} else {
|
||||
content = renderMessage(msg.Content().String(), true, isFocused, width, info...)
|
||||
// if len(styledAttachments) > 0 {
|
||||
// attachmentContent := baseStyle.Width(width).Render(lipgloss.JoinHorizontal(lipgloss.Left, styledAttachments...))
|
||||
// content = renderMessage(msg.Content().String(), true, isFocused, width, append(info, attachmentContent)...)
|
||||
// } else {
|
||||
for _, p := range msg.Parts {
|
||||
part, err := p.ValueByDiscriminator()
|
||||
if err != nil {
|
||||
continue //TODO: handle error?
|
||||
}
|
||||
|
||||
switch part.(type) {
|
||||
case client.SessionMessagePartText:
|
||||
textPart := part.(client.SessionMessagePartText)
|
||||
content = renderMessage(textPart.Text, true, isFocused, width, info...)
|
||||
}
|
||||
}
|
||||
// content = renderMessage(msg.Parts, true, isFocused, width, info...)
|
||||
|
||||
userMsg := uiMessage{
|
||||
ID: msg.ID,
|
||||
ID: msg.Id,
|
||||
messageType: userMessageType,
|
||||
position: position,
|
||||
height: lipgloss.Height(content),
|
||||
content: content,
|
||||
}
|
||||
return userMsg
|
||||
|
@ -193,11 +201,11 @@ func renderAssistantMessage(
|
|||
messages = append(messages, uiMessage{
|
||||
ID: msg.ID,
|
||||
messageType: assistantMessageType,
|
||||
position: position,
|
||||
height: lipgloss.Height(content),
|
||||
content: content,
|
||||
// position: position,
|
||||
// height: lipgloss.Height(content),
|
||||
content: content,
|
||||
})
|
||||
position += messages[0].height
|
||||
// position += messages[0].height
|
||||
position++ // for the space
|
||||
} else if thinking && thinkingContent != "" {
|
||||
// Render the thinking content with timestamp
|
||||
|
@ -205,9 +213,9 @@ func renderAssistantMessage(
|
|||
messages = append(messages, uiMessage{
|
||||
ID: msg.ID,
|
||||
messageType: assistantMessageType,
|
||||
position: position,
|
||||
height: lipgloss.Height(content),
|
||||
content: content,
|
||||
// position: position,
|
||||
// height: lipgloss.Height(content),
|
||||
content: content,
|
||||
})
|
||||
position += lipgloss.Height(content)
|
||||
position++ // for the space
|
||||
|
@ -226,7 +234,7 @@ func renderAssistantMessage(
|
|||
i+1,
|
||||
)
|
||||
messages = append(messages, toolCallContent)
|
||||
position += toolCallContent.height
|
||||
// position += toolCallContent.height
|
||||
position++ // for the space
|
||||
}
|
||||
}
|
||||
|
@ -246,8 +254,8 @@ func findToolResponse(toolCallID string, futureMessages []message.Message) *mess
|
|||
|
||||
func toolName(name string) string {
|
||||
switch name {
|
||||
case agent.AgentToolName:
|
||||
return "Task"
|
||||
// case agent.AgentToolName:
|
||||
// return "Task"
|
||||
case tools.BashToolName:
|
||||
return "Bash"
|
||||
case tools.EditToolName:
|
||||
|
@ -274,8 +282,8 @@ func toolName(name string) string {
|
|||
|
||||
func getToolAction(name string) string {
|
||||
switch name {
|
||||
case agent.AgentToolName:
|
||||
return "Preparing prompt..."
|
||||
// case agent.AgentToolName:
|
||||
// return "Preparing prompt..."
|
||||
case tools.BashToolName:
|
||||
return "Building command..."
|
||||
case tools.EditToolName:
|
||||
|
@ -363,11 +371,11 @@ func removeWorkingDirPrefix(path string) string {
|
|||
func renderToolParams(paramWidth int, toolCall message.ToolCall) string {
|
||||
params := ""
|
||||
switch toolCall.Name {
|
||||
case agent.AgentToolName:
|
||||
var params agent.AgentParams
|
||||
json.Unmarshal([]byte(toolCall.Input), ¶ms)
|
||||
prompt := strings.ReplaceAll(params.Prompt, "\n", " ")
|
||||
return renderParams(paramWidth, prompt)
|
||||
// case agent.AgentToolName:
|
||||
// var params agent.AgentParams
|
||||
// json.Unmarshal([]byte(toolCall.Input), ¶ms)
|
||||
// prompt := strings.ReplaceAll(params.Prompt, "\n", " ")
|
||||
// return renderParams(paramWidth, prompt)
|
||||
case tools.BashToolName:
|
||||
var params tools.BashParams
|
||||
json.Unmarshal([]byte(toolCall.Input), ¶ms)
|
||||
|
@ -481,11 +489,11 @@ func renderToolResponse(toolCall message.ToolCall, response message.ToolResult,
|
|||
|
||||
resultContent := truncateHeight(response.Content, maxResultHeight)
|
||||
switch toolCall.Name {
|
||||
case agent.AgentToolName:
|
||||
return styles.ForceReplaceBackgroundWithLipgloss(
|
||||
toMarkdown(resultContent, false, width),
|
||||
t.Background(),
|
||||
)
|
||||
// case agent.AgentToolName:
|
||||
// return styles.ForceReplaceBackgroundWithLipgloss(
|
||||
// toMarkdown(resultContent, false, width),
|
||||
// t.Background(),
|
||||
// )
|
||||
case tools.BashToolName:
|
||||
resultContent = fmt.Sprintf("```bash\n%s\n```", resultContent)
|
||||
return styles.ForceReplaceBackgroundWithLipgloss(
|
||||
|
@ -628,9 +636,9 @@ func renderToolMessage(
|
|||
content := style.Render(lipgloss.JoinHorizontal(lipgloss.Left, toolNameText, progressText))
|
||||
toolMsg := uiMessage{
|
||||
messageType: toolMessageType,
|
||||
position: position,
|
||||
height: lipgloss.Height(content),
|
||||
content: content,
|
||||
// position: position,
|
||||
// height: lipgloss.Height(content),
|
||||
content: content,
|
||||
}
|
||||
return toolMsg
|
||||
}
|
||||
|
@ -667,17 +675,17 @@ func renderToolMessage(
|
|||
parts = append(parts, lipgloss.JoinHorizontal(lipgloss.Left, prefix, toolNameText, formattedParams))
|
||||
}
|
||||
|
||||
if toolCall.Name == agent.AgentToolName {
|
||||
taskMessages, _ := messagesService.List(context.Background(), toolCall.ID)
|
||||
toolCalls := []message.ToolCall{}
|
||||
for _, v := range taskMessages {
|
||||
toolCalls = append(toolCalls, v.ToolCalls()...)
|
||||
}
|
||||
for _, call := range toolCalls {
|
||||
rendered := renderToolMessage(call, []message.Message{}, messagesService, focusedUIMessageId, true, width, 0)
|
||||
parts = append(parts, rendered.content)
|
||||
}
|
||||
}
|
||||
// if toolCall.Name == agent.AgentToolName {
|
||||
// taskMessages, _ := messagesService.List(context.Background(), toolCall.ID)
|
||||
// toolCalls := []message.ToolCall{}
|
||||
// for _, v := range taskMessages {
|
||||
// toolCalls = append(toolCalls, v.ToolCalls()...)
|
||||
// }
|
||||
// for _, call := range toolCalls {
|
||||
// rendered := renderToolMessage(call, []message.Message{}, messagesService, focusedUIMessageId, true, width, 0)
|
||||
// parts = append(parts, rendered.content)
|
||||
// }
|
||||
// }
|
||||
if responseContent != "" && !nested {
|
||||
parts = append(parts, responseContent)
|
||||
}
|
||||
|
@ -696,9 +704,9 @@ func renderToolMessage(
|
|||
}
|
||||
toolMsg := uiMessage{
|
||||
messageType: toolMessageType,
|
||||
position: position,
|
||||
height: lipgloss.Height(content),
|
||||
content: content,
|
||||
// position: position,
|
||||
// height: lipgloss.Height(content),
|
||||
content: content,
|
||||
}
|
||||
return toolMsg
|
||||
}
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
package chat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
|
@ -13,14 +11,13 @@ import (
|
|||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/sst/opencode/internal/message"
|
||||
"github.com/sst/opencode/internal/pubsub"
|
||||
"github.com/sst/opencode/internal/session"
|
||||
"github.com/sst/opencode/internal/status"
|
||||
"github.com/sst/opencode/internal/tui/app"
|
||||
"github.com/sst/opencode/internal/tui/components/dialog"
|
||||
"github.com/sst/opencode/internal/tui/state"
|
||||
"github.com/sst/opencode/internal/tui/styles"
|
||||
"github.com/sst/opencode/internal/tui/theme"
|
||||
"github.com/sst/opencode/pkg/client"
|
||||
)
|
||||
|
||||
type cacheItem struct {
|
||||
|
@ -32,7 +29,6 @@ type messagesCmp struct {
|
|||
app *app.App
|
||||
width, height int
|
||||
viewport viewport.Model
|
||||
messages []message.Message
|
||||
uiMessages []uiMessage
|
||||
currentMsgID string
|
||||
cachedContent map[string]cacheItem
|
||||
|
@ -75,6 +71,8 @@ func (m *messagesCmp) Init() tea.Cmd {
|
|||
}
|
||||
|
||||
func (m *messagesCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
m.renderView()
|
||||
|
||||
var cmds []tea.Cmd
|
||||
switch msg := msg.(type) {
|
||||
case dialog.ThemeChangedMsg:
|
||||
|
@ -90,7 +88,7 @@ func (m *messagesCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||
cmd := m.Reload(msg)
|
||||
return m, cmd
|
||||
case state.SessionClearedMsg:
|
||||
m.messages = make([]message.Message, 0)
|
||||
// m.messages = make([]message.Message, 0)
|
||||
m.currentMsgID = ""
|
||||
m.rendering = false
|
||||
return m, nil
|
||||
|
@ -104,62 +102,63 @@ func (m *messagesCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||
case renderFinishedMsg:
|
||||
m.rendering = false
|
||||
m.viewport.GotoBottom()
|
||||
|
||||
case state.StateUpdatedMsg:
|
||||
m.renderView()
|
||||
m.viewport.GotoBottom()
|
||||
|
||||
case pubsub.Event[message.Message]:
|
||||
needsRerender := false
|
||||
if msg.Type == message.EventMessageCreated {
|
||||
if msg.Payload.SessionID == m.app.CurrentSession.ID {
|
||||
messageExists := false
|
||||
for _, v := range m.messages {
|
||||
if v.ID == msg.Payload.ID {
|
||||
messageExists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !messageExists {
|
||||
if len(m.messages) > 0 {
|
||||
lastMsgID := m.messages[len(m.messages)-1].ID
|
||||
delete(m.cachedContent, lastMsgID)
|
||||
}
|
||||
|
||||
m.messages = append(m.messages, msg.Payload)
|
||||
delete(m.cachedContent, m.currentMsgID)
|
||||
m.currentMsgID = msg.Payload.ID
|
||||
needsRerender = true
|
||||
}
|
||||
}
|
||||
// There are tool calls from the child task
|
||||
for _, v := range m.messages {
|
||||
for _, c := range v.ToolCalls() {
|
||||
if c.ID == msg.Payload.SessionID {
|
||||
delete(m.cachedContent, v.ID)
|
||||
needsRerender = true
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if msg.Type == message.EventMessageUpdated && msg.Payload.SessionID == m.app.CurrentSession.ID {
|
||||
for i, v := range m.messages {
|
||||
if v.ID == msg.Payload.ID {
|
||||
m.messages[i] = msg.Payload
|
||||
delete(m.cachedContent, msg.Payload.ID)
|
||||
needsRerender = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if needsRerender {
|
||||
m.renderView()
|
||||
if len(m.messages) > 0 {
|
||||
if (msg.Type == message.EventMessageCreated) ||
|
||||
(msg.Type == message.EventMessageUpdated && msg.Payload.ID == m.messages[len(m.messages)-1].ID) {
|
||||
m.viewport.GotoBottom()
|
||||
}
|
||||
}
|
||||
}
|
||||
// case pubsub.Event[message.Message]:
|
||||
// needsRerender := false
|
||||
// if msg.Type == message.EventMessageCreated {
|
||||
// if msg.Payload.SessionID == m.app.CurrentSessionOLD.ID {
|
||||
// messageExists := false
|
||||
// for _, v := range m.messages {
|
||||
// if v.ID == msg.Payload.ID {
|
||||
// messageExists = true
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if !messageExists {
|
||||
// if len(m.messages) > 0 {
|
||||
// lastMsgID := m.messages[len(m.messages)-1].ID
|
||||
// delete(m.cachedContent, lastMsgID)
|
||||
// }
|
||||
//
|
||||
// m.messages = append(m.messages, msg.Payload)
|
||||
// delete(m.cachedContent, m.currentMsgID)
|
||||
// m.currentMsgID = msg.Payload.ID
|
||||
// needsRerender = true
|
||||
// }
|
||||
// }
|
||||
// // There are tool calls from the child task
|
||||
// for _, v := range m.messages {
|
||||
// for _, c := range v.ToolCalls() {
|
||||
// if c.ID == msg.Payload.SessionID {
|
||||
// delete(m.cachedContent, v.ID)
|
||||
// needsRerender = true
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// } else if msg.Type == message.EventMessageUpdated && msg.Payload.SessionID == m.app.CurrentSessionOLD.ID {
|
||||
// for i, v := range m.messages {
|
||||
// if v.ID == msg.Payload.ID {
|
||||
// m.messages[i] = msg.Payload
|
||||
// delete(m.cachedContent, msg.Payload.ID)
|
||||
// needsRerender = true
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// if needsRerender {
|
||||
// m.renderView()
|
||||
// if len(m.messages) > 0 {
|
||||
// if (msg.Type == message.EventMessageCreated) ||
|
||||
// (msg.Type == message.EventMessageUpdated && msg.Payload.ID == m.messages[len(m.messages)-1].ID) {
|
||||
// m.viewport.GotoBottom()
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
spinner, cmd := m.spinner.Update(msg)
|
||||
|
@ -169,7 +168,7 @@ func (m *messagesCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||
}
|
||||
|
||||
func (m *messagesCmp) IsAgentWorking() bool {
|
||||
return m.app.PrimaryAgent.IsSessionBusy(m.app.CurrentSession.ID)
|
||||
return m.app.PrimaryAgentOLD.IsSessionBusy(m.app.CurrentSessionOLD.ID)
|
||||
}
|
||||
|
||||
func formatTimeDifference(unixTime1, unixTime2 int64) string {
|
||||
|
@ -192,48 +191,48 @@ func (m *messagesCmp) renderView() {
|
|||
if m.width == 0 {
|
||||
return
|
||||
}
|
||||
for inx, msg := range m.messages {
|
||||
for _, msg := range m.app.Messages {
|
||||
switch msg.Role {
|
||||
case message.User:
|
||||
if cache, ok := m.cachedContent[msg.ID]; ok && cache.width == m.width {
|
||||
case client.User:
|
||||
if cache, ok := m.cachedContent[msg.Id]; ok && cache.width == m.width {
|
||||
m.uiMessages = append(m.uiMessages, cache.content...)
|
||||
continue
|
||||
}
|
||||
userMsg := renderUserMessage(
|
||||
msg,
|
||||
msg.ID == m.currentMsgID,
|
||||
msg.Id == m.currentMsgID,
|
||||
m.width,
|
||||
pos,
|
||||
)
|
||||
m.uiMessages = append(m.uiMessages, userMsg)
|
||||
m.cachedContent[msg.ID] = cacheItem{
|
||||
m.cachedContent[msg.Id] = cacheItem{
|
||||
width: m.width,
|
||||
content: []uiMessage{userMsg},
|
||||
}
|
||||
pos += userMsg.height + 1 // + 1 for spacing
|
||||
case message.Assistant:
|
||||
if cache, ok := m.cachedContent[msg.ID]; ok && cache.width == m.width {
|
||||
// pos += userMsg.height + 1 // + 1 for spacing
|
||||
case client.Assistant:
|
||||
if cache, ok := m.cachedContent[msg.Id]; ok && cache.width == m.width {
|
||||
m.uiMessages = append(m.uiMessages, cache.content...)
|
||||
continue
|
||||
}
|
||||
assistantMessages := renderAssistantMessage(
|
||||
msg,
|
||||
inx,
|
||||
m.messages,
|
||||
m.app.Messages,
|
||||
m.currentMsgID,
|
||||
m.width,
|
||||
pos,
|
||||
m.showToolMessages,
|
||||
)
|
||||
for _, msg := range assistantMessages {
|
||||
m.uiMessages = append(m.uiMessages, msg)
|
||||
pos += msg.height + 1 // + 1 for spacing
|
||||
}
|
||||
m.cachedContent[msg.ID] = cacheItem{
|
||||
width: m.width,
|
||||
content: assistantMessages,
|
||||
}
|
||||
// assistantMessages := renderAssistantMessage(
|
||||
// msg,
|
||||
// inx,
|
||||
// m.app.Messages,
|
||||
// m.app.MessagesOLD,
|
||||
// m.currentMsgID,
|
||||
// m.width,
|
||||
// pos,
|
||||
// m.showToolMessages,
|
||||
// )
|
||||
// for _, msg := range assistantMessages {
|
||||
// m.uiMessages = append(m.uiMessages, msg)
|
||||
// // pos += msg.height + 1 // + 1 for spacing
|
||||
// }
|
||||
// m.cachedContent[msg.Id] = cacheItem{
|
||||
// width: m.width,
|
||||
// content: assistantMessages,
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -248,33 +247,23 @@ func (m *messagesCmp) renderView() {
|
|||
)
|
||||
}
|
||||
|
||||
temp, _ := json.MarshalIndent(m.app.State, "", " ")
|
||||
// temp, _ := json.MarshalIndent(m.app.State, "", " ")
|
||||
|
||||
m.viewport.SetContent(
|
||||
baseStyle.
|
||||
Width(m.width).
|
||||
Render(
|
||||
string(temp),
|
||||
// lipgloss.JoinVertical(
|
||||
// lipgloss.Top,
|
||||
// messages...,
|
||||
// ),
|
||||
// string(temp),
|
||||
lipgloss.JoinVertical(
|
||||
lipgloss.Top,
|
||||
messages...,
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
func (m *messagesCmp) View() string {
|
||||
baseStyle := styles.BaseStyle()
|
||||
return baseStyle.
|
||||
Width(m.width).
|
||||
Render(
|
||||
lipgloss.JoinVertical(
|
||||
lipgloss.Top,
|
||||
m.viewport.View(),
|
||||
m.working(),
|
||||
m.help(),
|
||||
),
|
||||
)
|
||||
|
||||
if m.rendering {
|
||||
return baseStyle.
|
||||
|
@ -283,12 +272,12 @@ func (m *messagesCmp) View() string {
|
|||
lipgloss.JoinVertical(
|
||||
lipgloss.Top,
|
||||
"Loading...",
|
||||
m.working(),
|
||||
// m.working(),
|
||||
m.help(),
|
||||
),
|
||||
)
|
||||
}
|
||||
if len(m.messages) == 0 {
|
||||
if len(m.app.Messages) == 0 {
|
||||
content := baseStyle.
|
||||
Width(m.width).
|
||||
Height(m.height - 1).
|
||||
|
@ -314,7 +303,7 @@ func (m *messagesCmp) View() string {
|
|||
lipgloss.JoinVertical(
|
||||
lipgloss.Top,
|
||||
m.viewport.View(),
|
||||
m.working(),
|
||||
// m.working(),
|
||||
m.help(),
|
||||
),
|
||||
)
|
||||
|
@ -356,31 +345,31 @@ func hasUnfinishedToolCalls(messages []message.Message) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func (m *messagesCmp) working() string {
|
||||
text := ""
|
||||
if m.IsAgentWorking() && len(m.messages) > 0 {
|
||||
t := theme.CurrentTheme()
|
||||
baseStyle := styles.BaseStyle()
|
||||
|
||||
task := "Thinking..."
|
||||
lastMessage := m.messages[len(m.messages)-1]
|
||||
if hasToolsWithoutResponse(m.messages) {
|
||||
task = "Waiting for tool response..."
|
||||
} else if hasUnfinishedToolCalls(m.messages) {
|
||||
task = "Building tool call..."
|
||||
} else if !lastMessage.IsFinished() {
|
||||
task = "Generating..."
|
||||
}
|
||||
if task != "" {
|
||||
text += baseStyle.
|
||||
Width(m.width).
|
||||
Foreground(t.Primary()).
|
||||
Bold(true).
|
||||
Render(fmt.Sprintf("%s %s ", m.spinner.View(), task))
|
||||
}
|
||||
}
|
||||
return text
|
||||
}
|
||||
// func (m *messagesCmp) working() string {
|
||||
// text := ""
|
||||
// if m.IsAgentWorking() && len(m.app.Messages) > 0 {
|
||||
// t := theme.CurrentTheme()
|
||||
// baseStyle := styles.BaseStyle()
|
||||
//
|
||||
// task := "Thinking..."
|
||||
// lastMessage := m.app.Messages[len(m.app.Messages)-1]
|
||||
// if hasToolsWithoutResponse(m.app.Messages) {
|
||||
// task = "Waiting for tool response..."
|
||||
// } else if hasUnfinishedToolCalls(m.app.Messages) {
|
||||
// task = "Building tool call..."
|
||||
// } else if !lastMessage.IsFinished() {
|
||||
// task = "Generating..."
|
||||
// }
|
||||
// if task != "" {
|
||||
// text += baseStyle.
|
||||
// Width(m.width).
|
||||
// Foreground(t.Primary()).
|
||||
// Bold(true).
|
||||
// Render(fmt.Sprintf("%s %s ", m.spinner.View(), task))
|
||||
// }
|
||||
// }
|
||||
// return text
|
||||
// }
|
||||
|
||||
func (m *messagesCmp) help() string {
|
||||
t := theme.CurrentTheme()
|
||||
|
@ -388,7 +377,7 @@ func (m *messagesCmp) help() string {
|
|||
|
||||
text := ""
|
||||
|
||||
if m.app.PrimaryAgent.IsBusy() {
|
||||
if m.app.PrimaryAgentOLD.IsBusy() {
|
||||
text += lipgloss.JoinHorizontal(
|
||||
lipgloss.Left,
|
||||
baseStyle.Foreground(t.TextMuted()).Bold(true).Render("press "),
|
||||
|
@ -429,8 +418,8 @@ func (m *messagesCmp) initialScreen() string {
|
|||
}
|
||||
|
||||
func (m *messagesCmp) rerender() {
|
||||
for _, msg := range m.messages {
|
||||
delete(m.cachedContent, msg.ID)
|
||||
for _, msg := range m.app.Messages {
|
||||
delete(m.cachedContent, msg.Id)
|
||||
}
|
||||
m.renderView()
|
||||
}
|
||||
|
@ -454,14 +443,16 @@ func (m *messagesCmp) GetSize() (int, int) {
|
|||
}
|
||||
|
||||
func (m *messagesCmp) Reload(session *session.Session) tea.Cmd {
|
||||
messages, err := m.app.Messages.List(context.Background(), session.ID)
|
||||
if err != nil {
|
||||
status.Error(err.Error())
|
||||
return nil
|
||||
}
|
||||
m.messages = messages
|
||||
if len(m.messages) > 0 {
|
||||
m.currentMsgID = m.messages[len(m.messages)-1].ID
|
||||
// messages := m.app.Messages
|
||||
// messages, err := m.app.MessagesOLD.List(context.Background(), session.ID)
|
||||
// if err != nil {
|
||||
// status.Error(err.Error())
|
||||
// return nil
|
||||
// }
|
||||
// m.messages = messages
|
||||
|
||||
if len(m.app.Messages) > 0 {
|
||||
m.currentMsgID = m.app.Messages[len(m.app.Messages)-1].Id
|
||||
}
|
||||
delete(m.cachedContent, m.currentMsgID)
|
||||
m.rendering = true
|
||||
|
|
|
@ -86,7 +86,7 @@ func (m *sidebarCmp) sessionSection() string {
|
|||
|
||||
sessionValue := baseStyle.
|
||||
Foreground(t.Text()).
|
||||
Render(fmt.Sprintf(": %s", m.app.CurrentSession.Title))
|
||||
Render(fmt.Sprintf(": %s", m.app.CurrentSessionOLD.Title))
|
||||
|
||||
return sessionKey + sessionValue
|
||||
}
|
||||
|
@ -209,7 +209,7 @@ func NewSidebarCmp(app *app.App) tea.Model {
|
|||
}
|
||||
|
||||
func (m *sidebarCmp) loadModifiedFiles(ctx context.Context) {
|
||||
if m.app.CurrentSession.ID == "" {
|
||||
if m.app.CurrentSessionOLD.ID == "" {
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -9,7 +9,6 @@ import (
|
|||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/sst/opencode/internal/config"
|
||||
"github.com/sst/opencode/internal/llm/models"
|
||||
"github.com/sst/opencode/internal/lsp"
|
||||
"github.com/sst/opencode/internal/lsp/protocol"
|
||||
"github.com/sst/opencode/internal/pubsub"
|
||||
"github.com/sst/opencode/internal/status"
|
||||
|
@ -155,8 +154,8 @@ func (m statusCmp) View() string {
|
|||
// Initialize the help widget
|
||||
status := getHelpWidget("")
|
||||
|
||||
if m.app.CurrentSession.ID != "" {
|
||||
tokens := formatTokensAndCost(m.app.CurrentSession.PromptTokens+m.app.CurrentSession.CompletionTokens, model.ContextWindow, m.app.CurrentSession.Cost)
|
||||
if m.app.CurrentSessionOLD.ID != "" {
|
||||
tokens := formatTokensAndCost(m.app.CurrentSessionOLD.PromptTokens+m.app.CurrentSessionOLD.CompletionTokens, model.ContextWindow, m.app.CurrentSessionOLD.Cost)
|
||||
tokensStyle := styles.Padded().
|
||||
Background(t.Text()).
|
||||
Foreground(t.BackgroundSecondary()).
|
||||
|
@ -245,12 +244,12 @@ func (m *statusCmp) projectDiagnostics() string {
|
|||
|
||||
// Check if any LSP server is still initializing
|
||||
initializing := false
|
||||
for _, client := range m.app.LSPClients {
|
||||
if client.GetServerState() == lsp.StateStarting {
|
||||
initializing = true
|
||||
break
|
||||
}
|
||||
}
|
||||
// for _, client := range m.app.LSPClients {
|
||||
// if client.GetServerState() == lsp.StateStarting {
|
||||
// initializing = true
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
|
||||
// If any server is initializing, show that status
|
||||
if initializing {
|
||||
|
@ -263,22 +262,22 @@ func (m *statusCmp) projectDiagnostics() string {
|
|||
warnDiagnostics := []protocol.Diagnostic{}
|
||||
hintDiagnostics := []protocol.Diagnostic{}
|
||||
infoDiagnostics := []protocol.Diagnostic{}
|
||||
for _, client := range m.app.LSPClients {
|
||||
for _, d := range client.GetDiagnostics() {
|
||||
for _, diag := range d {
|
||||
switch diag.Severity {
|
||||
case protocol.SeverityError:
|
||||
errorDiagnostics = append(errorDiagnostics, diag)
|
||||
case protocol.SeverityWarning:
|
||||
warnDiagnostics = append(warnDiagnostics, diag)
|
||||
case protocol.SeverityHint:
|
||||
hintDiagnostics = append(hintDiagnostics, diag)
|
||||
case protocol.SeverityInformation:
|
||||
infoDiagnostics = append(infoDiagnostics, diag)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// for _, client := range m.app.LSPClients {
|
||||
// for _, d := range client.GetDiagnostics() {
|
||||
// for _, diag := range d {
|
||||
// switch diag.Severity {
|
||||
// case protocol.SeverityError:
|
||||
// errorDiagnostics = append(errorDiagnostics, diag)
|
||||
// case protocol.SeverityWarning:
|
||||
// warnDiagnostics = append(warnDiagnostics, diag)
|
||||
// case protocol.SeverityHint:
|
||||
// hintDiagnostics = append(hintDiagnostics, diag)
|
||||
// case protocol.SeverityInformation:
|
||||
// infoDiagnostics = append(infoDiagnostics, diag)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
if len(errorDiagnostics) == 0 &&
|
||||
len(warnDiagnostics) == 0 &&
|
||||
|
|
|
@ -78,7 +78,7 @@ func (p *chatPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||
}
|
||||
case dialog.CommandRunCustomMsg:
|
||||
// Check if the agent is busy before executing custom commands
|
||||
if p.app.PrimaryAgent.IsBusy() {
|
||||
if p.app.PrimaryAgentOLD.IsBusy() {
|
||||
status.Warn("Agent is busy, please wait before executing a command...")
|
||||
return p, nil
|
||||
}
|
||||
|
@ -105,20 +105,20 @@ func (p *chatPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||
cmd := p.setSidebar()
|
||||
cmds = append(cmds, cmd)
|
||||
case state.CompactSessionMsg:
|
||||
if p.app.CurrentSession.ID == "" {
|
||||
if p.app.CurrentSessionOLD.ID == "" {
|
||||
status.Warn("No active session to compact.")
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// Run compaction in background
|
||||
go func(sessionID string) {
|
||||
err := p.app.PrimaryAgent.CompactSession(context.Background(), sessionID, false)
|
||||
err := p.app.PrimaryAgentOLD.CompactSession(context.Background(), sessionID, false)
|
||||
if err != nil {
|
||||
status.Error(fmt.Sprintf("Compaction failed: %v", err))
|
||||
} else {
|
||||
status.Info("Conversation compacted successfully.")
|
||||
}
|
||||
}(p.app.CurrentSession.ID)
|
||||
}(p.app.CurrentSessionOLD.ID)
|
||||
|
||||
return p, nil
|
||||
case dialog.CompletionDialogCloseMsg:
|
||||
|
@ -131,16 +131,16 @@ func (p *chatPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||
p.app.SetCompletionDialogOpen(true)
|
||||
// Continue sending keys to layout->chat
|
||||
case key.Matches(msg, keyMap.NewSession):
|
||||
p.app.CurrentSession = &session.Session{}
|
||||
p.app.CurrentSessionOLD = &session.Session{}
|
||||
return p, tea.Batch(
|
||||
p.clearSidebar(),
|
||||
util.CmdHandler(state.SessionClearedMsg{}),
|
||||
)
|
||||
case key.Matches(msg, keyMap.Cancel):
|
||||
if p.app.CurrentSession.ID != "" {
|
||||
if p.app.CurrentSessionOLD.ID != "" {
|
||||
// Cancel the current session's generation process
|
||||
// This allows users to interrupt long-running operations
|
||||
p.app.PrimaryAgent.Cancel(p.app.CurrentSession.ID)
|
||||
p.app.PrimaryAgentOLD.Cancel(p.app.CurrentSessionOLD.ID)
|
||||
return p, nil
|
||||
}
|
||||
case key.Matches(msg, keyMap.ToggleTools):
|
||||
|
|
|
@ -2,6 +2,7 @@ package tui
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"strings"
|
||||
|
||||
|
@ -178,7 +179,7 @@ func (a appModel) Init() tea.Cmd {
|
|||
func (a appModel) updateAllPages(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
var cmds []tea.Cmd
|
||||
var cmd tea.Cmd
|
||||
for id, _ := range a.pages {
|
||||
for id := range a.pages {
|
||||
a.pages[id], cmd = a.pages[id].Update(msg)
|
||||
cmds = append(cmds, cmd)
|
||||
}
|
||||
|
@ -256,26 +257,76 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||
return a, a.moveToPage(msg.ID)
|
||||
|
||||
case state.SessionSelectedMsg:
|
||||
a.app.CurrentSession = msg
|
||||
a.app.CurrentSessionOLD = msg
|
||||
return a.updateAllPages(msg)
|
||||
|
||||
case pubsub.Event[session.Session]:
|
||||
if msg.Type == session.EventSessionUpdated {
|
||||
if a.app.CurrentSession.ID == msg.Payload.ID {
|
||||
a.app.CurrentSession = &msg.Payload
|
||||
if a.app.CurrentSessionOLD.ID == msg.Payload.ID {
|
||||
a.app.CurrentSessionOLD = &msg.Payload
|
||||
}
|
||||
}
|
||||
|
||||
// Handle SSE events from the TypeScript backend
|
||||
case client.EventStorageWrite:
|
||||
slog.Debug("Received SSE event", "key", msg.Properties.Key)
|
||||
parts := strings.Split(msg.Key, "/")
|
||||
if len(parts) < 3 {
|
||||
return a, nil
|
||||
}
|
||||
|
||||
if parts[0] == "session" && parts[1] == "info" {
|
||||
sessionId := parts[2]
|
||||
if sessionId == a.app.Session.Id {
|
||||
var sessionInfo client.SessionInfo
|
||||
bytes, _ := json.Marshal(msg.Content)
|
||||
if err := json.Unmarshal(bytes, &sessionInfo); err != nil {
|
||||
status.Error(err.Error())
|
||||
return a, nil
|
||||
}
|
||||
|
||||
a.app.Session = &sessionInfo
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
if parts[0] == "session" && parts[1] == "message" {
|
||||
sessionId := parts[2]
|
||||
if sessionId == a.app.Session.Id {
|
||||
messageId := parts[3]
|
||||
var message client.SessionMessage
|
||||
bytes, _ := json.Marshal(msg.Content)
|
||||
if err := json.Unmarshal(bytes, &message); err != nil {
|
||||
status.Error(err.Error())
|
||||
return a, nil
|
||||
}
|
||||
|
||||
for i, m := range a.app.Messages {
|
||||
if m.Id == messageId {
|
||||
a.app.Messages[i] = message
|
||||
slog.Debug("Updated message", "message", message)
|
||||
return a, nil
|
||||
}
|
||||
}
|
||||
|
||||
a.app.Messages = append(a.app.Messages, message)
|
||||
slog.Debug("Appended message", "message", message)
|
||||
|
||||
// a.app.CurrentSession.MessageCount++
|
||||
// a.app.CurrentSession.PromptTokens += message.PromptTokens
|
||||
// a.app.CurrentSession.CompletionTokens += message.CompletionTokens
|
||||
// a.app.CurrentSession.Cost += message.Cost
|
||||
// a.app.CurrentSession.UpdatedAt = message.CreatedAt
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
// log key and content
|
||||
slog.Debug("Received SSE event", "key", msg.Key, "content", msg.Content)
|
||||
|
||||
splits := strings.Split(msg.Properties.Key, "/")
|
||||
current := a.app.State
|
||||
|
||||
for i, part := range splits {
|
||||
if i == len(splits)-1 {
|
||||
current[part] = msg.Properties.Content
|
||||
for i, part := range parts {
|
||||
if i == len(parts)-1 {
|
||||
current[part] = msg.Content
|
||||
} else {
|
||||
if _, exists := current[part]; !exists {
|
||||
current[part] = make(map[string]any)
|
||||
|
@ -566,7 +617,7 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||
}
|
||||
return a, nil
|
||||
case key.Matches(msg, helpEsc):
|
||||
if a.app.PrimaryAgent.IsBusy() {
|
||||
if a.app.PrimaryAgentOLD.IsBusy() {
|
||||
if a.showQuit {
|
||||
return a, nil
|
||||
}
|
||||
|
@ -705,27 +756,9 @@ func (a *appModel) RegisterCommand(cmd dialog.Command) {
|
|||
}
|
||||
|
||||
// getAvailableToolNames returns a list of all available tool names
|
||||
func getAvailableToolNames(app *app.App) []string {
|
||||
func getAvailableToolNames(_ *app.App) []string {
|
||||
// TODO: Tools not implemented in API yet
|
||||
return []string{"Tools not available in API mode"}
|
||||
/*
|
||||
// Get primary agent tools (which already include MCP tools)
|
||||
allTools := agent.PrimaryAgentTools(
|
||||
app.Permissions,
|
||||
app.Sessions,
|
||||
app.Messages,
|
||||
app.History,
|
||||
app.LSPClients,
|
||||
)
|
||||
|
||||
// Extract tool names
|
||||
var toolNames []string
|
||||
for _, tool := range allTools {
|
||||
toolNames = append(toolNames, tool.Info().Name)
|
||||
}
|
||||
|
||||
return toolNames
|
||||
*/
|
||||
}
|
||||
|
||||
func (a *appModel) moveToPage(pageID page.PageID) tea.Cmd {
|
||||
|
@ -785,7 +818,7 @@ func (a appModel) View() string {
|
|||
|
||||
}
|
||||
|
||||
if !a.app.PrimaryAgent.IsBusy() {
|
||||
if !a.app.PrimaryAgentOLD.IsBusy() {
|
||||
a.status.SetHelpWidgetMsg("ctrl+? help")
|
||||
} else {
|
||||
a.status.SetHelpWidgetMsg("? help")
|
||||
|
@ -799,7 +832,7 @@ func (a appModel) View() string {
|
|||
if a.showPermissions {
|
||||
bindings = append(bindings, a.permissions.BindingKeys()...)
|
||||
}
|
||||
if !a.app.PrimaryAgent.IsBusy() {
|
||||
if !a.app.PrimaryAgentOLD.IsBusy() {
|
||||
bindings = append(bindings, helpEsc)
|
||||
}
|
||||
a.help.SetBindings(bindings)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue