syntax = "proto3";

package nominal.ai.v1;

// KnowledgeBaseService provides AI-powered knowledge base management
service KnowledgeBaseService {
  // CreateOrUpdateKnowledgeBase creates a knowledge base in the workspace
  rpc CreateOrUpdateKnowledgeBase(CreateOrUpdateKnowledgeBaseRequest) returns (CreateOrUpdateKnowledgeBaseResponse);

  // List returns all knowledge bases in the specified workspace
  rpc List(ListRequest) returns (ListResponse);

  // Delete removes a knowledge base by its RID
  rpc Delete(DeleteRequest) returns (DeleteResponse);

  // GetBatch retrieves multiple knowledge bases by their RIDs
  rpc GetBatch(GetBatchRequest) returns (GetBatchResponse);

  // GenerateSummaryDescription generates a summary description for an attachment rid
  rpc GenerateSummaryDescription(GenerateSummaryDescriptionRequest) returns (GenerateSummaryDescriptionResponse);
}

// KnowledgeBaseType defines the types of knowledge base
enum KnowledgeBaseType {
  KNOWLEDGE_BASE_TYPE_UNSPECIFIED = 0; // defaults to PROMPT
  KNOWLEDGE_BASE_TYPE_PROMPT = 1; // knowledge base gets added directly to prompt (needs to be small enough!)
  KNOWLEDGE_BASE_TYPE_EMBEDDING = 2; // knowledge base gets used via vector search on embeddings
}

// CreateOrUpdateKnowledgeBaseRequest is a request to create a knowledge from an attachment in the attachment's
// workspace. Subsequent calls for the same attachment will overwrite the existing knowledge base.
message CreateOrUpdateKnowledgeBaseRequest {
  string attachment_rid = 1;
  string summary_description = 2; // summary of the knowledge base, will be used by the LLM to decide when to use it
  optional KnowledgeBaseType type = 3;
}

// CreateOrUpdateKnowledgeBaseResponse is the response to creating/updating a knowledge base
message CreateOrUpdateKnowledgeBaseResponse {
  string knowledge_base_rid = 1;
}

// KnowledgeBase represents a knowledge base entry
message KnowledgeBase {
  string knowledge_base_rid = 1;
  string attachment_rid = 2;
  string workspace_rid = 3;
  string summary_description = 4;
  KnowledgeBaseType type = 5;
  int32 version = 6;
}

message ListRequest {
  string workspace_rid = 1;
}

message ListResponse {
  repeated KnowledgeBase knowledge_bases = 1;
}

message DeleteRequest {
  string knowledge_base_rid = 1;
}

message DeleteResponse {
  bool success = 1;
}

message GetBatchRequest {
  repeated string knowledge_base_rids = 1;
}

message GetBatchResponse {
  repeated KnowledgeBase knowledge_bases = 1;
}

// generate summary description is intentionally going to return the generated description to the frontend
// rather than storing it in the knowledge base directly because the description needs to be accepted by the user
message GenerateSummaryDescriptionRequest {
  string attachment_rid = 1;
}

message GenerateSummaryDescriptionResponse {
  string summary_description = 1;
}
