// Copyright 2026 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

edition = "2023";

package optimization_guide.proto;

import "components/optimization_guide/proto/features/common_quality_data.proto";

import "components/optimization_guide/proto/model_quality_metadata.proto";

option optimize_for = LITE_RUNTIME;
option java_package = "org.chromium.components.optimization_guide.features.proto";

option java_outer_classname = "ContextHubProto";

// DO NOT EDIT THIS FILE DIRECTLY!
//
// This file is generated in g3 and then synced to Chrome. Instead, please refer to
// http://go/chrome-mqls-onboarding (Google-internal link), and then changes will
// be synced with Chrome automatically.

message ContextHubLoggingData {
  ContextHubRequest request = 1 [features = { field_presence: EXPLICIT }];

  ContextHubResponse response = 2 [features = { field_presence: EXPLICIT }];

  // Internal logging information from the model execution.
  ModelExecutionInfo model_execution_info = 3 [features = { field_presence: EXPLICIT }];
}

// Holds the input data for the context hub feature.
message ContextHubRequest {
  // The type of request for the context hub feature.
  ContextHubRequestType request_type = 1 [features = { field_presence: EXPLICIT }];

  // The entry items for the request.
  repeated EntryItem entry_items = 2;

  // All pre-existing tab groups
  repeated TabGroupMinimal pre_existing_tab_groups = 3;

  // User instruction.
  string user_command = 4 [features = { field_presence: EXPLICIT }];

  // The conversation history context representing prior user queries and model
  // responses.
  repeated ChatHistoryTurn chat_history = 5;

  // The request for the auto todos deduplication feature.
  AutoTodosDeduplicationRequest auto_todos_deduplication_request = 6 [features = { field_presence: EXPLICIT }];
}

message EntryItem {
  oneof item {
    Tab tab = 1;

    MemoryBankEntry memory_bank_entry = 2;
  }
}

// Holds a single entry saved in the memory bank.
message MemoryBankEntry {
  // Unique identifier for the entry.
  int64 id = 1 [features = { field_presence: EXPLICIT }];

  // The type of the memory bank entry.
  MemoryBankType type = 2 [features = { field_presence: EXPLICIT }];

  // Timestamp of when the entry was saved (in milliseconds since epoch).
  int64 timestamp_ms = 3 [features = { field_presence: EXPLICIT }];

  // The URL of the page associated with the entry.
  string url = 4 [features = { field_presence: EXPLICIT }];

  // Title of the tab associated with the entry.
  string tab_title = 5 [features = { field_presence: EXPLICIT }];

  // Text selected by the user for the entry.
  string selected_text = 6 [features = { field_presence: EXPLICIT }];

  // Tags associated with the entry for grouping.
  repeated string tags = 7;
}

// A single turn in the conversation history.
message ChatHistoryTurn {
  Role role = 1 [features = { field_presence: EXPLICIT }];

  // The textual message content of the conversation turn.
  string message_content = 2 [features = { field_presence: EXPLICIT }];

  // Timestamp of when the message was created (in milliseconds since epoch).
  int64 timestamp_ms = 3 [features = { field_presence: EXPLICIT }];

  enum Role {
    ROLE_UNSPECIFIED = 0;

    ROLE_USER = 1;

    ROLE_ASSISTANT = 2;
  }
}

// Holds the input data for the auto todos deduplication feature.
message AutoTodosDeduplicationRequest {
  // The existing todos already stored for the user, each populated with a
  // unique identifier in `id` (e.g. "todo_1", "todo_2").
  repeated ContextHubAutoTodoItem existing_todos = 1;

  // The newly extracted candidate todos to deduplicate against existing todos,
  // each populated with a temporary candidate identifier in `id` (e.g.
  // "new_todo_1").
  repeated ContextHubAutoTodoItem new_todos = 2;
}

message ContextHubAutoTodoItem {
  string title = 1 [features = { field_presence: EXPLICIT }];

  string description = 2 [features = { field_presence: EXPLICIT }];

  string actionable_url = 3 [features = { field_presence: EXPLICIT }];

  float importance_score = 4 [features = { field_presence: EXPLICIT }];

  Status status = 5 [features = { field_presence: EXPLICIT }];

  // In request:
  //   - For existing todos: the cached ID of the todo (e.g. "todo_1").
  //   - For new candidate todos: a temporary candidate ID (e.g. "new_todo_1").
  // In response:
  //   - For modified existing todos: populated with the existing todo's ID.
  //   - For newly created todos: omitted or empty string.
  string id = 6 [features = { field_presence: EXPLICIT }];

  // Provenance tracking of which input todos contributed to this item.
  //
  // In response:
  //   Lists all input `id`s (from `existing_todos` and/or `new_todos`) that
  //   were merged into this item (e.g. ["todo_1", "new_todo_1"] or
  //   ["new_todo_2"]).
  //
  //   Purpose & Usage:
  //   To minimize LLM prompt token overhead and prevent URL hallucination,
  //   full source reference metadata (such as Gmail email URLs, subjects, and
  //   Photos URLs) is kept in the caller service CMS and mapped to these IDs.
  //   The caller uses `todo_ids_merged` to retrieve and combine the underlying
  //   source references of all merged input todos onto the final output item.
  repeated string todo_ids_mergeds = 7;

  enum Status {
    STATUS_UNSPECIFIED = 0;

    STATUS_ACTIVE = 1;

    STATUS_COMPLETED = 2;

    STATUS_DISMISSED = 3;
  }
}

// Holds the output data for the context hub feature.
message ContextHubResponse {
  oneof feature_response {
    // The response for a request of type CONTEXT_HUB_REQUEST_TYPE_GROUPING.
    GroupResponse group_response = 1;

    // The response for a request of type
    // CONTEXT_HUB_REQUEST_TYPE_MEMORY_BANK_CHAT.
    MemoryBankChatResponse memory_bank_chat_response = 2;

    // The response for a request of type
    // CONTEXT_HUB_REQUEST_TYPE_BROWSER_BASED_TODOS.
    BrowserBasedTodosResponse browser_based_todos_response = 3;

    // The response for a request of type
    // CONTEXT_HUB_REQUEST_TYPE_AUTO_TODOS_DEDUPLICATION.
    AutoTodosDeduplicationResponse auto_todos_deduplication_response = 4;
  }
}

// Holds the output data for a request of type
// CONTEXT_HUB_REQUEST_TYPE_GROUPING.
message GroupResponse {
  // TODO(crbug.com/534358600): Deprecate this field in favor of minimal_tab_groups.
  repeated TabGroup tab_groups = 1;

  // The tab groups.
  repeated TabGroupMinimal minimal_tab_groups = 2;

  // The tab ids that are not in any tab group.
  repeated int64 ungrouped_tab_ids = 3;

  // The textual response or follow-up questions generated by the LLM.
  string text_response = 4 [features = { field_presence: EXPLICIT }];
}

// Holds the output data for a request of type
// CONTEXT_HUB_REQUEST_TYPE_MEMORY_BANK_CHAT.
message MemoryBankChatResponse {
  // The textual response generated by the model.
  string text_response = 1 [features = { field_presence: EXPLICIT }];
}

// Holds the output data for a request of type
// CONTEXT_HUB_REQUEST_TYPE_BROWSER_BASED_TODOS.
message BrowserBasedTodosResponse {
  // The tab id of the tab that the todo is for.
  int64 tab_id = 1 [features = { field_presence: EXPLICIT }];

  GroupType group_type = 2 [features = { field_presence: EXPLICIT }];

  // The title of the todo.
  string todo_title = 3 [features = { field_presence: EXPLICIT }];

  // The description of the todo.
  string todo_description = 4 [features = { field_presence: EXPLICIT }];

  // The type of specialty group the todo belongs to.
  enum GroupType {
    GROUP_TYPE_UNSPECIFIED = 0;

    GROUP_TYPE_READING_LIST = 1;

    GROUP_TYPE_NUDGE_TO_CLOSE = 2;

    GROUP_TYPE_UNFINISHED = 3;

    GROUP_TYPE_SHOPPING_CART = 4;
  }
}

// Holds the output data for a request of type
// CONTEXT_HUB_REQUEST_TYPE_AUTO_TODOS_DEDUPLICATION.
message AutoTodosDeduplicationResponse {
  // The delta list of deduplicated todos containing ONLY:
  // 1. Modified existing todos (with `id` set to the existing todo ID and
  //    `matched_todo_ids` listing all merged existing and new IDs).
  // 2. Newly created todos (with `id` omitted/empty and `matched_todo_ids`
  //    listing the contributing new IDs).
  //
  // Unchanged existing todos are omitted from this response.
  repeated ContextHubAutoTodoItem deduplicated_todos = 1;
}

enum ContextHubRequestType {
  CONTEXT_HUB_REQUEST_TYPE_UNSPECIFIED = 0;

  // For the generation of grouping of tab items.
  CONTEXT_HUB_REQUEST_TYPE_GROUPING = 1;

  // For the memory bank chat feature.
  CONTEXT_HUB_REQUEST_TYPE_MEMORY_BANK_CHAT = 2;

  // For the generation of browser-based todos.
  CONTEXT_HUB_REQUEST_TYPE_BROWSER_BASED_TODOS = 3;

  // For the deduplication of auto todos.
  CONTEXT_HUB_REQUEST_TYPE_AUTO_TODOS_DEDUPLICATION = 4;
}

// Type of entry in the memory bank.
enum MemoryBankType {
  MEMORY_BANK_TYPE_UNSPECIFIED = 0;

  // Entry created from a tab.
  MEMORY_BANK_TYPE_TAB = 1;

  // Entry created from text selection.
  MEMORY_BANK_TYPE_TEXT_SELECTION = 2;
}
