Skip to content

feat: Import OpenCode transcripts #1874

Closed

Description

@huang195

Part of #1856, which holds the design for all agents. Depends on #1871 (the shared import path). Start this issue after #1871 merges.

OpenCode source (this issue)

Add an OpenCode source, so that agentop import --agent opencode imports OpenCode history.

  • Store: ~/.local/share/opencode/opencode.db (honour XDG_DATA_HOME), SQLite in WAL mode. Tables include session_v2, session_message, project.
  • Read it through the sqlite3 -readonly -json helper from feat: Import Bob transcripts #1872. If feat: Import Bob transcripts #1872 has not merged, add the helper here in the same place. Do not add a SQLite driver.
  • The Cortex session id is the affinity id that OpenCode sends as X-Session-Id: the parent's id for a subagent, the source's id for a fork, else the session's own id (SessionIDHeader in core/session/idheader.go). Check this against the real tables first, and say in the PR body which columns give the parent and the fork source.
  • A subagent files in its parent with SessionEvent.Subagent set to its own id.

Acceptance criteria

  • OpenCode sessions list in agentop with project, model, tokens and estimated cost.
  • The Timeline opens a session with its tool calls and subagents.
  • Fixture tests from a scrubbed database (a plain session, tool calls, a subagent, one row that does not parse).

The shared part, from #1871

#1871 adds POST /v1/import, agentop import, the importer rules, the archive write, the ledger rows and the imported mark. This issue adds only a source and registers it. A source only parses: it never touches the archive, the store or the ledger. Implement the Source interface as #1871 merged it. Its design shape was:

type Source interface {
    Agent() string               // "claude-code", "bob", ...
    Sessions(since time.Time) iter.Seq2[Session, error]
}
type Session struct {
    ID       string    // the Cortex session id, see this issue
    Updated  time.Time // last activity, for --since and for the replace rule
    Project  string    // cwd
    Title    string    // when the store has one
    Events   []*model.SessionEvent
    Skipped  int       // lines or rows not recognized
}

Events a source builds

For each model turn the source builds a request event and a response event with the
same RequestID, both with Direction outbound and an Inference extension:

  • Request: Model, Messages (the whole conversation up to this turn, as the agent
    sent it), Tools when the store records them, ToolResults for the tool results that
    this turn sends.
  • Response: Completion, ToolCalls, ResponseThinking, FinishReason, and the token
    fields that the store records (InputTokens, CompletionTokens, CacheReadTokens,
    CacheWriteTokens, TotalTokens).
  • At is the store's timestamp for the turn.
  • A subagent's events go in its root session with SessionEvent.Subagent set to the
    subagent's own id, and AgentRole set too. The Timeline tells subagents apart by
    Subagent (timeline.go:412-416). AgentRole alone merges them into one row.

The archive interns on disk the same fields that the store interns, so the repeated
Messages prefix costs about what it costs for a live session.

Cost

Price each turn's tokens through the existing pricing table, at today's rates. The cost record gets Source: transcript. A turn with no usage gets no cost record. It is not priced as zero.

Bad input

  • A line or row that the source does not recognize is skipped and counted. It does not
    stop the import.
  • A session where nothing parsed is not written.
  • A file or database that cannot be opened fails that agent only, with its error in
    the response.

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions