factoryaidroid

package
v0.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 19, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package factoryaidroid implements the Agent interface for Factory AI Droid.

Index

Constants

View Source
const (
	HookNameSessionStart     = "session-start"
	HookNameSessionEnd       = "session-end"
	HookNameStop             = "stop"
	HookNameUserPromptSubmit = "user-prompt-submit"
	HookNamePreToolUse       = "pre-tool-use"
	HookNamePostToolUse      = "post-tool-use"
	HookNameSubagentStop     = "subagent-stop"
	HookNamePreCompact       = "pre-compact"
	HookNameNotification     = "notification"
)

Factory AI Droid hook names - these become subcommands under `entire hooks factoryai-droid`

View Source
const (
	ToolCreate       = "Create"
	ToolWrite        = "Write"
	ToolEdit         = "Edit"
	ToolMultiEdit    = "MultiEdit"
	ToolNotebookEdit = "NotebookEdit"
)

Tool names used in Factory Droid transcripts.

View Source
const FactorySettingsFileName = "settings.json"

FactorySettingsFileName is the settings file used by Factory AI Droid. This is Factory-specific and not shared with other agents.

Variables

FileModificationTools lists tools that create or modify files.

Functions

func CalculateTokenUsage

func CalculateTokenUsage(transcriptLines []TranscriptLine) *agent.TokenUsage

CalculateTokenUsage calculates token usage from a Factory AI Droid transcript. Due to streaming, multiple transcript rows may share the same message.id. We deduplicate by taking the row with the highest output_tokens for each message.id.

func CalculateTokenUsageFromFile

func CalculateTokenUsageFromFile(path string, startLine int) (*agent.TokenUsage, error)

CalculateTokenUsageFromFile calculates token usage from a transcript file. If startLine > 0, only considers lines from startLine onwards.

func CalculateTotalTokenUsageFromBytes

func CalculateTotalTokenUsageFromBytes(data []byte, startLine int, subagentsDir string) (*agent.TokenUsage, error)

CalculateTotalTokenUsageFromBytes calculates token usage from pre-loaded transcript bytes, including subagents. It parses the main transcript bytes from startLine, extracts spawned agent IDs, and calculates their token usage from transcript files in subagentsDir.

func CalculateTotalTokenUsageFromTranscript

func CalculateTotalTokenUsageFromTranscript(transcriptPath string, startLine int, subagentsDir string) (*agent.TokenUsage, error)

CalculateTotalTokenUsageFromTranscript calculates token usage for a turn, including subagents. It parses the main transcript from startLine, extracts spawned agent IDs, and calculates their token usage from transcripts in subagentsDir.

func ExtractAllModifiedFilesFromBytes

func ExtractAllModifiedFilesFromBytes(data []byte, startLine int, subagentsDir string) ([]string, error)

ExtractAllModifiedFilesFromBytes extracts files modified by both the main agent and any subagents from pre-loaded transcript bytes. It parses the main transcript bytes from startLine, collects modified files, then reads each subagent's transcript from subagentsDir to collect their modified files too.

func ExtractAllModifiedFilesFromTranscript

func ExtractAllModifiedFilesFromTranscript(transcriptPath string, startLine int, subagentsDir string) ([]string, error)

ExtractAllModifiedFilesFromTranscript extracts files modified by both the main agent and any subagents spawned via the Task tool. It parses the main transcript from startLine, collects modified files from the main agent, then reads each subagent's transcript from subagentsDir to collect their modified files too. The result is a deduplicated list of all modified file paths.

func ExtractModelFromTranscript added in v0.5.0

func ExtractModelFromTranscript(transcriptPath string) string

ExtractModelFromTranscript extracts the LLM model name from the Factory AI Droid session settings file. Factory AI Droid stores a .settings.json file alongside each transcript JSONL (e.g., <session-id>.settings.json next to <session-id>.jsonl). The settings file contains the actual model being used, unlike the transcript's system-reminder text which can be stale after provider switches. Returns empty string if the settings file cannot be read or has no model.

func ExtractModifiedFiles

func ExtractModifiedFiles(lines []TranscriptLine) []string

ExtractModifiedFiles extracts files modified by tool calls from transcript.

func ExtractSpawnedAgentIDs

func ExtractSpawnedAgentIDs(transcriptLines []TranscriptLine) map[string]string

ExtractSpawnedAgentIDs extracts agent IDs from Task tool results in a transcript. When a Task tool completes, the tool_result contains "agentId: <id>" in its content. Returns a map of agentID -> toolUseID for all spawned agents.

func NewFactoryAIDroidAgent

func NewFactoryAIDroidAgent() agent.Agent

NewFactoryAIDroidAgent creates a new Factory AI Droid agent instance.

func ParseDroidTranscript

func ParseDroidTranscript(path string, startLine int) ([]transcript.Line, int, error)

ParseDroidTranscript parses a Droid JSONL file into normalized transcript.Line entries. It transforms the Droid envelope format (type="message", role inside message) into the shared transcript.Line format (type="assistant"/"user", message=inner content). Non-message entries (session_start, etc.) are skipped.

func ParseDroidTranscriptFromBytes

func ParseDroidTranscriptFromBytes(content []byte, startLine int) ([]transcript.Line, int, error)

ParseDroidTranscriptFromBytes parses Droid JSONL content from a byte slice. startLine skips the first N raw JSONL lines before parsing (0 = parse all). Returns parsed lines, total raw line count, and any error. This mirrors ParseDroidTranscript's startLine parameter, applying the offset at the raw line level before filtering out non-message entries.

Types

type FactoryAIDroidAgent

type FactoryAIDroidAgent struct{}

FactoryAIDroidAgent implements the agent.Agent interface for Factory AI Droid.

func (*FactoryAIDroidAgent) AreHooksInstalled

func (f *FactoryAIDroidAgent) AreHooksInstalled(ctx context.Context) bool

AreHooksInstalled checks if Entire hooks are installed.

func (*FactoryAIDroidAgent) CalculateTokenUsage

func (f *FactoryAIDroidAgent) CalculateTokenUsage(transcriptData []byte, fromOffset int) (*agent.TokenUsage, error)

CalculateTokenUsage computes token usage from pre-loaded transcript bytes starting at the given line offset.

func (*FactoryAIDroidAgent) CalculateTotalTokenUsage

func (f *FactoryAIDroidAgent) CalculateTotalTokenUsage(transcriptData []byte, fromOffset int, subagentsDir string) (*agent.TokenUsage, error)

CalculateTotalTokenUsage computes token usage including all spawned subagents.

func (*FactoryAIDroidAgent) ChunkTranscript

func (f *FactoryAIDroidAgent) ChunkTranscript(_ context.Context, content []byte, maxSize int) ([][]byte, error)

ChunkTranscript splits a JSONL transcript at line boundaries.

func (*FactoryAIDroidAgent) Description

func (f *FactoryAIDroidAgent) Description() string

Description returns a human-readable description.

func (*FactoryAIDroidAgent) DetectPresence

func (f *FactoryAIDroidAgent) DetectPresence(ctx context.Context) (bool, error)

DetectPresence checks if Factory AI Droid is configured in the repository.

func (*FactoryAIDroidAgent) ExtractAllModifiedFiles

func (f *FactoryAIDroidAgent) ExtractAllModifiedFiles(transcriptData []byte, fromOffset int, subagentsDir string) ([]string, error)

ExtractAllModifiedFiles extracts files modified by both the main agent and any spawned subagents.

func (*FactoryAIDroidAgent) ExtractModifiedFilesFromOffset

func (f *FactoryAIDroidAgent) ExtractModifiedFilesFromOffset(path string, startOffset int) ([]string, int, error)

ExtractModifiedFilesFromOffset extracts files modified since a given line offset.

func (*FactoryAIDroidAgent) ExtractPrompts

func (f *FactoryAIDroidAgent) ExtractPrompts(sessionRef string, fromOffset int) ([]string, error)

ExtractPrompts extracts user prompts from the transcript starting at the given line offset.

func (*FactoryAIDroidAgent) ExtractSummary

func (f *FactoryAIDroidAgent) ExtractSummary(sessionRef string) (string, error)

ExtractSummary extracts the last assistant message as a session summary.

func (*FactoryAIDroidAgent) FormatResumeCommand

func (f *FactoryAIDroidAgent) FormatResumeCommand(sessionID string) string

FormatResumeCommand returns the command to resume a Factory AI Droid session.

func (*FactoryAIDroidAgent) GetSessionDir

func (f *FactoryAIDroidAgent) GetSessionDir(repoPath string) (string, error)

GetSessionDir returns the directory where Factory AI Droid stores session transcripts. Path: ~/.factory/sessions/<sanitized-repo-path>/

func (*FactoryAIDroidAgent) GetSessionID

func (f *FactoryAIDroidAgent) GetSessionID(input *agent.HookInput) string

GetSessionID extracts the session ID from hook input.

func (*FactoryAIDroidAgent) GetTranscriptPosition

func (f *FactoryAIDroidAgent) GetTranscriptPosition(path string) (int, error)

GetTranscriptPosition returns the current line count of the JSONL transcript.

func (*FactoryAIDroidAgent) HookNames

func (f *FactoryAIDroidAgent) HookNames() []string

HookNames returns the hook verbs Factory AI Droid supports. These become subcommands: entire hooks factoryai-droid <verb>

func (*FactoryAIDroidAgent) InstallHooks

func (f *FactoryAIDroidAgent) InstallHooks(ctx context.Context, localDev bool, force bool) (int, error)

InstallHooks installs Factory AI Droid hooks in .factory/settings.json. If force is true, removes existing Entire hooks before installing. Returns the number of hooks installed.

func (*FactoryAIDroidAgent) IsPreview

func (f *FactoryAIDroidAgent) IsPreview() bool

IsPreview returns true as Factory AI Droid integration is in preview.

func (*FactoryAIDroidAgent) Name

Name returns the agent registry key.

func (*FactoryAIDroidAgent) ParseHookEvent

func (f *FactoryAIDroidAgent) ParseHookEvent(_ context.Context, hookName string, stdin io.Reader) (*agent.Event, error)

ParseHookEvent translates a Factory AI Droid hook into a normalized lifecycle Event. Returns nil if the hook has no lifecycle significance.

func (*FactoryAIDroidAgent) ProtectedDirs

func (f *FactoryAIDroidAgent) ProtectedDirs() []string

ProtectedDirs returns directories that Factory AI Droid uses for config/state.

func (*FactoryAIDroidAgent) ReadSession

func (f *FactoryAIDroidAgent) ReadSession(input *agent.HookInput) (*agent.AgentSession, error)

ReadSession reads a session from Factory AI Droid's storage (JSONL transcript file). The session data is stored in NativeData as raw JSONL bytes. ModifiedFiles is computed by parsing the transcript.

func (*FactoryAIDroidAgent) ReadTranscript

func (f *FactoryAIDroidAgent) ReadTranscript(sessionRef string) ([]byte, error)

ReadTranscript reads the raw JSONL transcript bytes for a session.

func (*FactoryAIDroidAgent) ReassembleTranscript

func (f *FactoryAIDroidAgent) ReassembleTranscript(chunks [][]byte) ([]byte, error)

ReassembleTranscript concatenates JSONL chunks with newlines.

func (*FactoryAIDroidAgent) ResolveSessionFile

func (f *FactoryAIDroidAgent) ResolveSessionFile(sessionDir, agentSessionID string) string

ResolveSessionFile returns the path to a Factory AI Droid session file.

func (*FactoryAIDroidAgent) Type

Type returns the agent type identifier.

func (*FactoryAIDroidAgent) UninstallHooks

func (f *FactoryAIDroidAgent) UninstallHooks(ctx context.Context) error

UninstallHooks removes Entire hooks from Factory AI Droid settings.

func (*FactoryAIDroidAgent) WriteHookResponse

func (f *FactoryAIDroidAgent) WriteHookResponse(message string) error

WriteHookResponse outputs the hook response as plain text to stdout. Factory AI Droid does not parse the JSON systemMessage protocol, so we write plain text that it displays directly in the terminal.

func (*FactoryAIDroidAgent) WriteSession

func (f *FactoryAIDroidAgent) WriteSession(_ context.Context, session *agent.AgentSession) error

WriteSession writes a session to Factory AI Droid's storage (JSONL transcript file). Uses the NativeData field which contains raw JSONL bytes.

type FactoryHookEntry

type FactoryHookEntry struct {
	Type    string `json:"type"`
	Command string `json:"command"`
}

FactoryHookEntry represents a single hook command.

type FactoryHookMatcher

type FactoryHookMatcher struct {
	Matcher string             `json:"matcher"`
	Hooks   []FactoryHookEntry `json:"hooks"`
}

FactoryHookMatcher matches hooks to specific patterns.

type FactoryHooks

type FactoryHooks struct {
	SessionStart     []FactoryHookMatcher `json:"SessionStart,omitempty"`
	SessionEnd       []FactoryHookMatcher `json:"SessionEnd,omitempty"`
	UserPromptSubmit []FactoryHookMatcher `json:"UserPromptSubmit,omitempty"`
	Stop             []FactoryHookMatcher `json:"Stop,omitempty"`
	PreToolUse       []FactoryHookMatcher `json:"PreToolUse,omitempty"`
	PostToolUse      []FactoryHookMatcher `json:"PostToolUse,omitempty"`
	PreCompact       []FactoryHookMatcher `json:"PreCompact,omitempty"`
}

FactoryHooks contains the hook configurations.

type FactorySettings

type FactorySettings struct {
	Hooks FactoryHooks `json:"hooks"`
}

FactorySettings represents the .factory/settings.json structure.

type TranscriptLine

type TranscriptLine = transcript.Line

TranscriptLine is an alias to the shared transcript.Line type.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL