Chat info: Session section with native id, resume command and export
The chat information sheet gains a Session section: the harness's native session id (copy), harness, directory, a verified resume command (copy) and Export conversation (Markdown), Export JSON and, for Claude Code, Export native session (the JSONL transcript). Server: chat_sessions rows carry resume_command and native_export; new chat_export command builds Markdown/JSON from the same paged history chat_messages serves (bounded at 8 MiB), refuses non-operator sessions. App: exports go through the artifact file bridge (web download, share sheet on phones).
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package claude
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"unicode/utf8"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// NativeExportLimit bounds the Claude Code session file chat_export returns.
|
||||
const NativeExportLimit = 8 << 20
|
||||
|
||||
// NativeExport returns Claude Code's own JSONL transcript for one session, the
|
||||
// file `claude --resume` reads. It is a plain read of the retained file; no CLI
|
||||
// process is started.
|
||||
func (c *Chat) NativeExport(ctx context.Context, id string) (protocol.ChatExportFile, error) {
|
||||
path := findTranscriptFile(id)
|
||||
if path == "" {
|
||||
return protocol.ChatExportFile{}, errors.New("Claude Code has not written a transcript for session " + id + " yet")
|
||||
}
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return protocol.ChatExportFile{}, err
|
||||
}
|
||||
defer file.Close()
|
||||
info, err := file.Stat()
|
||||
if err != nil {
|
||||
return protocol.ChatExportFile{}, err
|
||||
}
|
||||
if !info.Mode().IsRegular() {
|
||||
return protocol.ChatExportFile{}, errors.New("the Claude Code transcript is not a regular file")
|
||||
}
|
||||
if info.Size() > NativeExportLimit {
|
||||
return protocol.ChatExportFile{}, fmt.Errorf("the Claude Code transcript is %d bytes, over the %d byte export limit; it is at %s on the server", info.Size(), NativeExportLimit, path)
|
||||
}
|
||||
data, err := io.ReadAll(io.LimitReader(file, NativeExportLimit+1))
|
||||
if err != nil {
|
||||
return protocol.ChatExportFile{}, err
|
||||
}
|
||||
if len(data) > NativeExportLimit {
|
||||
return protocol.ChatExportFile{}, fmt.Errorf("the Claude Code transcript grew past the %d byte export limit", NativeExportLimit)
|
||||
}
|
||||
if !utf8.Valid(data) {
|
||||
return protocol.ChatExportFile{}, errors.New("the Claude Code transcript is not valid UTF-8")
|
||||
}
|
||||
return protocol.ChatExportFile{
|
||||
SessionID: id,
|
||||
Format: protocol.ChatExportNative,
|
||||
Name: id + ".jsonl",
|
||||
Mime: "application/x-ndjson",
|
||||
Content: string(data),
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package claude
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
func TestNativeExportReturnsTheSessionJSONL(t *testing.T) {
|
||||
config := t.TempDir()
|
||||
t.Setenv("CLAUDE_CONFIG_DIR", config)
|
||||
id := "0b6f1c2e-1111-4222-8333-444455556666"
|
||||
project := filepath.Join(config, "projects", "-work-a")
|
||||
if err := os.MkdirAll(project, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body := `{"type":"user","uuid":"u1"}` + "\n" + `{"type":"assistant","uuid":"a1"}` + "\n"
|
||||
if err := os.WriteFile(filepath.Join(project, id+".jsonl"), []byte(body), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
chat := New(context.Background())
|
||||
defer chat.Close()
|
||||
file, err := chat.NativeExport(context.Background(), id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if file.Name != id+".jsonl" || file.Mime != "application/x-ndjson" || file.Content != body || file.Format != protocol.ChatExportNative {
|
||||
t.Fatalf("native export = %+v", file)
|
||||
}
|
||||
if _, err := chat.NativeExport(context.Background(), "0b6f1c2e-1111-4222-8333-000000000000"); err == nil || !strings.Contains(err.Error(), "has not written a transcript") {
|
||||
t.Fatalf("missing transcript error = %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user