Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 1 addition & 42 deletions internal/parser/deepseek_tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,14 @@ import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"

"github.com/tidwall/gjson"
)

const deepSeekTUIPrefix = "deepseek-tui:"

// DiscoverDeepSeekTUISessions finds DeepSeek TUI / CodeWhale session
// JSON documents under a sessions directory.
func DiscoverDeepSeekTUISessions(root string) []DiscoveredFile {
entries, err := os.ReadDir(root)
if err != nil {
return nil
}

files := make([]DiscoveredFile, 0)
for _, entry := range entries {
if entry.IsDir() || !isDeepSeekTUISessionFile(entry.Name()) {
continue
}
files = append(files, DiscoveredFile{
Path: filepath.Join(root, entry.Name()),
Agent: AgentDeepSeekTUI,
})
}

sort.Slice(files, func(i, j int) bool {
return files[i].Path < files[j].Path
})
return files
}

// FindDeepSeekTUISourceFile locates a DeepSeek TUI / CodeWhale session
// JSON document by raw session ID.
func FindDeepSeekTUISourceFile(root, rawID string) string {
if !IsValidSessionID(rawID) {
return ""
}
path := filepath.Join(root, rawID+".json")
if info, err := os.Stat(path); err == nil && !info.IsDir() {
return path
}
return ""
}

// ParseDeepSeekTUISession parses a DeepSeek TUI / CodeWhale saved
// session JSON file.
func ParseDeepSeekTUISession(
func parseDeepSeekTUISession(
path, machine string,
) (*ParsedSession, []ParsedMessage, error) {
info, err := os.Stat(path)
Expand Down
66 changes: 66 additions & 0 deletions internal/parser/deepseek_tui_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package parser

import (
"context"
"path/filepath"
)

// DeepSeek TUI stores each session as a single JSON file in a directory. It is
// a directory-of-files provider: discovery, watching, change classification,
// lookup, and fingerprinting come from JSONLSourceSet, and the ParseFile option
// makes that source set a full SourceSet so it rides the generic factory.
func newDeepSeekTUIProviderFactory(def AgentDef) ProviderFactory {
return newSourceSetFactory(
def,
deepSeekTUIProviderCapabilities(),
func(cfg ProviderConfig) SourceSet { return newDeepSeekTUISourceSet(cfg.Roots) },
)
}

func newDeepSeekTUISourceSet(roots []string) JSONLSourceSet {
return newJSONLSourceSet(AgentDeepSeekTUI, roots,
withExtensions(".json"),
withFollowSymlinkFiles(),
withContentHashing(),
withIncludePath(isDeepSeekTUISourcePath),
withSessionIDFromPath(func(root, path string) string {
return deepSeekTUISessionIDFromPath(path)
}),
withParseFile(deepSeekTUIParseFile),
)
}

func deepSeekTUIParseFile(
_ context.Context, path string, req ParseRequest,
) ([]ParseResult, []string, error) {
sess, msgs, err := parseDeepSeekTUISession(path, req.Machine)
if err != nil {
return nil, nil, err
}
if sess == nil {
return nil, nil, nil
}
if req.Fingerprint.Hash != "" {
sess.File.Hash = req.Fingerprint.Hash
}
return []ParseResult{{Session: *sess, Messages: msgs}}, nil, nil
}

func isDeepSeekTUISourcePath(root, path string) bool {
return isDeepSeekTUISessionFile(filepath.Base(path))
}

func deepSeekTUIProviderCapabilities() Capabilities {
return Capabilities{
Source: jsonlFileProviderSourceCapabilities(),
Content: ContentCapabilities{
FirstMessage: CapabilitySupported,
SessionName: CapabilitySupported,
Cwd: CapabilitySupported,
Model: CapabilitySupported,
Thinking: CapabilitySupported,
ToolCalls: CapabilitySupported,
ToolResults: CapabilitySupported,
},
}
}
160 changes: 160 additions & 0 deletions internal/parser/deepseek_tui_provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package parser

import (
"context"
"crypto/sha256"
"fmt"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDeepSeekTUIProviderFactoryReplacesLegacyAdapter(t *testing.T) {
factory, ok := ProviderFactoryByType(AgentDeepSeekTUI)
require.True(t, ok)
require.NotNil(t, factory)

provider, ok := NewProvider(AgentDeepSeekTUI, ProviderConfig{
Roots: []string{t.TempDir()},
Machine: "devbox",
})
require.True(t, ok)
require.NotNil(t, provider)
}

func TestDeepSeekTUIProviderSourceMethods(t *testing.T) {
root := t.TempDir()
sourcePath := filepath.Join(root, "session_123.json")
writeSourceFile(t, sourcePath, deepSeekTUIProviderFixture())
writeSourceFile(t, filepath.Join(root, "latest.json"), "{}\n")
writeSourceFile(t, filepath.Join(root, "offline_queue.json"), "{}\n")
writeSourceFile(t, filepath.Join(root, "nested", "session_456.json"), "{}\n")

provider, ok := NewProvider(AgentDeepSeekTUI, ProviderConfig{
Roots: []string{root},
Machine: "devbox",
})
require.True(t, ok)

discovered, err := provider.Discover(context.Background())
require.NoError(t, err)
require.Len(t, discovered, 1)
assert.Equal(t, AgentDeepSeekTUI, discovered[0].Provider)
assert.Equal(t, sourcePath, discovered[0].DisplayPath)

found, ok, err := provider.FindSource(context.Background(), FindSourceRequest{
FullSessionID: "host~deepseek-tui:session_123",
})
require.NoError(t, err)
require.True(t, ok)
assert.Equal(t, sourcePath, found.DisplayPath)

found, ok, err = provider.FindSource(context.Background(), FindSourceRequest{
FingerprintKey: sourcePath,
})
require.NoError(t, err)
require.True(t, ok)
assert.Equal(t, sourcePath, found.DisplayPath)

require.NoError(t, os.Remove(sourcePath))
changed, err := provider.SourcesForChangedPath(
context.Background(),
ChangedPathRequest{Path: sourcePath, EventKind: "remove", WatchRoot: root},
)
require.NoError(t, err)
require.Len(t, changed, 1)
assert.Equal(t, sourcePath, changed[0].DisplayPath)
}

func TestDeepSeekTUIProviderSourceMethodsFollowSymlinkedSessionFile(t *testing.T) {
root := t.TempDir()
targetDir := t.TempDir()
targetPath := filepath.Join(targetDir, "session_123.json")
sourcePath := filepath.Join(root, "session_123.json")
writeSourceFile(t, targetPath, deepSeekTUIProviderFixture())
if err := os.Symlink(targetPath, sourcePath); err != nil {
t.Skipf("symlink not supported: %v", err)
}

provider, ok := NewProvider(AgentDeepSeekTUI, ProviderConfig{
Roots: []string{root},
Machine: "devbox",
})
require.True(t, ok)

discovered, err := provider.Discover(context.Background())
require.NoError(t, err)
require.Len(t, discovered, 1)
assert.Equal(t, sourcePath, discovered[0].DisplayPath)

found, ok, err := provider.FindSource(context.Background(), FindSourceRequest{
FullSessionID: "host~deepseek-tui:session_123",
})
require.NoError(t, err)
require.True(t, ok)
assert.Equal(t, sourcePath, found.DisplayPath)

changed, err := provider.SourcesForChangedPath(
context.Background(),
ChangedPathRequest{Path: sourcePath, EventKind: "write", WatchRoot: root},
)
require.NoError(t, err)
require.Len(t, changed, 1)
assert.Equal(t, sourcePath, changed[0].DisplayPath)
}

func TestDeepSeekTUIProviderParse(t *testing.T) {
root := t.TempDir()
sourcePath := filepath.Join(root, "session_123.json")
content := deepSeekTUIProviderFixture()
writeSourceFile(t, sourcePath, content)

provider, ok := NewProvider(AgentDeepSeekTUI, ProviderConfig{
Roots: []string{root},
Machine: "devbox",
})
require.True(t, ok)
sources, err := provider.Discover(context.Background())
require.NoError(t, err)
require.Len(t, sources, 1)

fingerprint, err := provider.Fingerprint(context.Background(), sources[0])
require.NoError(t, err)

outcome, err := provider.Parse(context.Background(), ParseRequest{
Source: sources[0],
Fingerprint: fingerprint,
})
require.NoError(t, err)
require.True(t, outcome.ResultSetComplete)
require.Len(t, outcome.Results, 1)
assert.Equal(t, DataVersionCurrent, outcome.Results[0].DataVersion)
assert.Equal(t, "deepseek-tui:session_123", outcome.Results[0].Result.Session.ID)
assert.Equal(t, "sample_project", outcome.Results[0].Result.Session.Project)
assert.Equal(t, "devbox", outcome.Results[0].Result.Session.Machine)
assert.Equal(t,
fmt.Sprintf("%x", sha256.Sum256([]byte(content))),
outcome.Results[0].Result.Session.File.Hash,
)
assert.Len(t, outcome.Results[0].Result.Messages, 2)
}

func deepSeekTUIProviderFixture() string {
return `{
"metadata": {
"id": "session_123",
"title": "Investigate DeepSeek TUI",
"created_at": "2026-06-01T10:00:00Z",
"updated_at": "2026-06-01T10:02:00Z",
"model": "deepseek-chat",
"workspace": "/Users/alice/code/sample-project"
},
"messages": [
{"role": "user", "content": "Inspect server logs", "timestamp": "2026-06-01T10:00:05Z"},
{"role": "assistant", "content": [{"type": "text", "text": "The server failed during startup."}], "timestamp": "2026-06-01T10:00:10Z"}
]
}`
}
Loading