+// WhatsAppImportOptions selects the target, exports, and plan-only mode.
+type WhatsAppImportOptions struct {
+ planOnly bool
+ agent string
+ paths []string
+}
+
+// whatsappImportParse accepts GNU-style flags, including --agent=NAME and --
+// before paths that start with a dash.
+func whatsappImportParse(args []string) (WhatsAppImportOptions, error) {
+ var options WhatsAppImportOptions
+
+ flags := true
+ for i := 0; i < len(args); i++ {
+ arg := args[i]
+ switch {
+ case flags && arg == "--":
+ flags = false
+ case flags && arg == "-n":
+ options.planOnly = true
+ case flags && (arg == "--agent" || strings.HasPrefix(arg, "--agent=")):
+ _, name, found := strings.Cut(arg, "=")
+ if !found {
+ i++
+ if i == len(args) {
+ return options, errors.New("--agent needs a child name")
+ }
+
+ name = args[i]
+ }
+
+ if name == "" {
+ return options, errors.New("--agent needs a child name")
+ }
+
+ options.agent = name
+ case flags && strings.HasPrefix(arg, "-"):
+ return options, fmt.Errorf("unknown import flag %q", arg)
+ default:
+ options.paths = append(options.paths, arg)
+ }
+ }
+
+ if len(options.paths) == 0 {
+ return options, errors.New("usage: onechat import [-n] [--agent NAME] ZIP [ZIP...]")
+ }
+
+ return options, nil
+}
+
+// whatsappImportAgent selects the root or an existing direct child without
+// creating a directory or config. A root without a config can have a plan,
+// using the default name.
+func whatsappImportAgent(root, name string) (*Agent, error) {
+ _, err := os.Stat(filepath.Join(root, "config.json"))
+ if errors.Is(err, os.ErrNotExist) && name == "" {
+ var config AgentConfig
+
+ config, err = defaultAgentConfig()
+ if err != nil {
+ return nil, fmt.Errorf("read default import agent: %w", err)
+ }
+
+ return &Agent{
+ dir: root,
+ config: config,
+ }, nil
+ }
+
+ if err != nil {
+ return nil, fmt.Errorf("read root agent config: %w", err)
+ }
+
+ agent, err := openAgent(root)
+ if err != nil {
+ return nil, fmt.Errorf("open root agent: %w", err)
+ }
+
+ if name == "" {
+ return agent, nil
+ }
+
+ if agent.config.Parent != "" {
+ return nil, errors.New("import root cannot have a parent")
+ }
+
+ children, err := agent.Children()
+ if err != nil {
+ return nil, fmt.Errorf("read import children: %w", err)
+ }
+
+ for _, child := range children {
+ if child.config.Name == name {
+ return child, nil
+ }
+ }
+
+ return nil, fmt.Errorf("child %q does not exist", name)
+}
+
+// whatsappImportIdle refuses unfinished subagents and pending deliveries to the
+// target. Rebuilding renumbers its log, so saved delivery IDs must not refer to
+// it until their turns finish. The caller holds the token lock.
+func whatsappImportIdle(root string, agent *Agent) error {
+ err := rebuildIdle(agent.dir)
+ if err != nil {
+ return fmt.Errorf("check target work: %w", err)
+ }
+
+ data, err := os.ReadFile(filepath.Join(root, "messages.json"))
+ if errors.Is(err, os.ErrNotExist) {
+ return nil
+ }
+
+ if err != nil {
+ return fmt.Errorf("read agent deliveries: %w", err)
+ }
+
+ var queue DaemonMessageFile
+
+ err = json.Unmarshal(data, &queue)
+ if err != nil {
+ return fmt.Errorf("decode agent deliveries: %w", err)
+ }
+
+ for _, message := range queue.Pending {
+ if message.Target == agent.config.Name {
+ return fmt.Errorf("import refused: agent %q has pending deliveries", agent.config.Name)
+ }
+ }
+
+ return nil
+}
+