// Code generated by protoc-gen-ts_proto. DO NOT EDIT.
// versions:
//   protoc-gen-ts_proto  v2.11.8
//   protoc               v3.19.1
// source: common.proto

/* eslint-disable */
import { BinaryReader, BinaryWriter } from "@bufbuild/protobuf/wire";
import Long from "long";
import { Struct } from "./google/protobuf/struct";

export enum ChatRole {
  CHAT_ROLE_UNSPECIFIED = 0,
  CHAT_ROLE_SYSTEM = 1,
  CHAT_ROLE_USER = 2,
  CHAT_ROLE_ASSISTANT = 3,
  CHAT_ROLE_TOOL = 4,
  UNRECOGNIZED = -1,
}

export function chatRoleFromJSON(object: any): ChatRole {
  switch (object) {
    case 0:
    case "CHAT_ROLE_UNSPECIFIED":
      return ChatRole.CHAT_ROLE_UNSPECIFIED;
    case 1:
    case "CHAT_ROLE_SYSTEM":
      return ChatRole.CHAT_ROLE_SYSTEM;
    case 2:
    case "CHAT_ROLE_USER":
      return ChatRole.CHAT_ROLE_USER;
    case 3:
    case "CHAT_ROLE_ASSISTANT":
      return ChatRole.CHAT_ROLE_ASSISTANT;
    case 4:
    case "CHAT_ROLE_TOOL":
      return ChatRole.CHAT_ROLE_TOOL;
    case -1:
    case "UNRECOGNIZED":
    default:
      return ChatRole.UNRECOGNIZED;
  }
}

export function chatRoleToJSON(object: ChatRole): string {
  switch (object) {
    case ChatRole.CHAT_ROLE_UNSPECIFIED:
      return "CHAT_ROLE_UNSPECIFIED";
    case ChatRole.CHAT_ROLE_SYSTEM:
      return "CHAT_ROLE_SYSTEM";
    case ChatRole.CHAT_ROLE_USER:
      return "CHAT_ROLE_USER";
    case ChatRole.CHAT_ROLE_ASSISTANT:
      return "CHAT_ROLE_ASSISTANT";
    case ChatRole.CHAT_ROLE_TOOL:
      return "CHAT_ROLE_TOOL";
    case ChatRole.UNRECOGNIZED:
    default:
      return "UNRECOGNIZED";
  }
}

/**
 * Identity-bearing principal shipped with every request (carried in the JWT for
 * internal callers, or in the request body for API-key callers — same shape).
 */
export interface Principal {
  subject: string;
  roles: string[];
  attributes: { [key: string]: string };
}

export interface Principal_AttributesEntry {
  key: string;
  value: string;
}

/**
 * Default-deny ACL attached to every ingested chunk.
 * At least one of allowed_roles or allowed_subjects must be non-empty,
 * enforced at the controller, the DB CHECK constraint, and the post-filter.
 */
export interface AclSpec {
  allowedRoles: string[];
  allowedSubjects: string[];
  deniedRoles: string[];
  requiredAttributes: { [key: string]: string };
}

export interface AclSpec_RequiredAttributesEntry {
  key: string;
  value: string;
}

export interface ChatMessage {
  role: ChatRole;
  content: string;
  toolCallId: string;
  name: string;
}

/**
 * JSON-schema descriptor for caller-side tool execution. The AI service is
 * tool-blind: it emits ToolCall events; the caller executes and posts back.
 */
export interface ToolDescriptor {
  name: string;
  description: string;
  schema?: { [key: string]: any } | undefined;
}

function createBasePrincipal(): Principal {
  return { subject: "", roles: [], attributes: {} };
}

export const Principal: MessageFns<Principal> = {
  encode(message: Principal, writer: BinaryWriter = new BinaryWriter()): BinaryWriter {
    if (message.subject !== "") {
      writer.uint32(10).string(message.subject);
    }
    for (const v of message.roles) {
      writer.uint32(18).string(v!);
    }
    globalThis.Object.entries(message.attributes).forEach(([key, value]: [string, string]) => {
      Principal_AttributesEntry.encode({ key: key as any, value }, writer.uint32(26).fork()).join();
    });
    return writer;
  },

  decode(input: BinaryReader | Uint8Array, length?: number): Principal {
    const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
    const end = length === undefined ? reader.len : reader.pos + length;
    const message = createBasePrincipal();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1: {
          if (tag !== 10) {
            break;
          }

          message.subject = reader.string();
          continue;
        }
        case 2: {
          if (tag !== 18) {
            break;
          }

          message.roles.push(reader.string());
          continue;
        }
        case 3: {
          if (tag !== 26) {
            break;
          }

          const entry3 = Principal_AttributesEntry.decode(reader, reader.uint32());
          if (entry3.value !== undefined) {
            message.attributes[entry3.key] = entry3.value;
          }
          continue;
        }
      }
      if ((tag & 7) === 4 || tag === 0) {
        break;
      }
      reader.skip(tag & 7);
    }
    return message;
  },

  fromJSON(object: any): Principal {
    return {
      subject: isSet(object.subject) ? globalThis.String(object.subject) : "",
      roles: globalThis.Array.isArray(object?.roles) ? object.roles.map((e: any) => globalThis.String(e)) : [],
      attributes: isObject(object.attributes)
        ? (globalThis.Object.entries(object.attributes) as [string, any][]).reduce(
          (acc: { [key: string]: string }, [key, value]: [string, any]) => {
            acc[key] = globalThis.String(value);
            return acc;
          },
          {},
        )
        : {},
    };
  },

  toJSON(message: Principal): unknown {
    const obj: any = {};
    if (message.subject !== "") {
      obj.subject = message.subject;
    }
    if (message.roles?.length) {
      obj.roles = message.roles;
    }
    if (message.attributes) {
      const entries = globalThis.Object.entries(message.attributes) as [string, string][];
      if (entries.length > 0) {
        obj.attributes = {};
        entries.forEach(([k, v]) => {
          obj.attributes[k] = v;
        });
      }
    }
    return obj;
  },

  create<I extends Exact<DeepPartial<Principal>, I>>(base?: I): Principal {
    return Principal.fromPartial(base ?? ({} as any));
  },
  fromPartial<I extends Exact<DeepPartial<Principal>, I>>(object: I): Principal {
    const message = createBasePrincipal();
    message.subject = object.subject ?? "";
    message.roles = object.roles?.map((e) => e) || [];
    message.attributes = (globalThis.Object.entries(object.attributes ?? {}) as [string, string][]).reduce(
      (acc: { [key: string]: string }, [key, value]: [string, string]) => {
        if (value !== undefined) {
          acc[key] = globalThis.String(value);
        }
        return acc;
      },
      {},
    );
    return message;
  },
};

function createBasePrincipal_AttributesEntry(): Principal_AttributesEntry {
  return { key: "", value: "" };
}

export const Principal_AttributesEntry: MessageFns<Principal_AttributesEntry> = {
  encode(message: Principal_AttributesEntry, writer: BinaryWriter = new BinaryWriter()): BinaryWriter {
    if (message.key !== "") {
      writer.uint32(10).string(message.key);
    }
    if (message.value !== "") {
      writer.uint32(18).string(message.value);
    }
    return writer;
  },

  decode(input: BinaryReader | Uint8Array, length?: number): Principal_AttributesEntry {
    const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
    const end = length === undefined ? reader.len : reader.pos + length;
    const message = createBasePrincipal_AttributesEntry();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1: {
          if (tag !== 10) {
            break;
          }

          message.key = reader.string();
          continue;
        }
        case 2: {
          if (tag !== 18) {
            break;
          }

          message.value = reader.string();
          continue;
        }
      }
      if ((tag & 7) === 4 || tag === 0) {
        break;
      }
      reader.skip(tag & 7);
    }
    return message;
  },

  fromJSON(object: any): Principal_AttributesEntry {
    return {
      key: isSet(object.key) ? globalThis.String(object.key) : "",
      value: isSet(object.value) ? globalThis.String(object.value) : "",
    };
  },

  toJSON(message: Principal_AttributesEntry): unknown {
    const obj: any = {};
    if (message.key !== "") {
      obj.key = message.key;
    }
    if (message.value !== "") {
      obj.value = message.value;
    }
    return obj;
  },

  create<I extends Exact<DeepPartial<Principal_AttributesEntry>, I>>(base?: I): Principal_AttributesEntry {
    return Principal_AttributesEntry.fromPartial(base ?? ({} as any));
  },
  fromPartial<I extends Exact<DeepPartial<Principal_AttributesEntry>, I>>(object: I): Principal_AttributesEntry {
    const message = createBasePrincipal_AttributesEntry();
    message.key = object.key ?? "";
    message.value = object.value ?? "";
    return message;
  },
};

function createBaseAclSpec(): AclSpec {
  return { allowedRoles: [], allowedSubjects: [], deniedRoles: [], requiredAttributes: {} };
}

export const AclSpec: MessageFns<AclSpec> = {
  encode(message: AclSpec, writer: BinaryWriter = new BinaryWriter()): BinaryWriter {
    for (const v of message.allowedRoles) {
      writer.uint32(10).string(v!);
    }
    for (const v of message.allowedSubjects) {
      writer.uint32(18).string(v!);
    }
    for (const v of message.deniedRoles) {
      writer.uint32(26).string(v!);
    }
    globalThis.Object.entries(message.requiredAttributes).forEach(([key, value]: [string, string]) => {
      AclSpec_RequiredAttributesEntry.encode({ key: key as any, value }, writer.uint32(34).fork()).join();
    });
    return writer;
  },

  decode(input: BinaryReader | Uint8Array, length?: number): AclSpec {
    const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
    const end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseAclSpec();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1: {
          if (tag !== 10) {
            break;
          }

          message.allowedRoles.push(reader.string());
          continue;
        }
        case 2: {
          if (tag !== 18) {
            break;
          }

          message.allowedSubjects.push(reader.string());
          continue;
        }
        case 3: {
          if (tag !== 26) {
            break;
          }

          message.deniedRoles.push(reader.string());
          continue;
        }
        case 4: {
          if (tag !== 34) {
            break;
          }

          const entry4 = AclSpec_RequiredAttributesEntry.decode(reader, reader.uint32());
          if (entry4.value !== undefined) {
            message.requiredAttributes[entry4.key] = entry4.value;
          }
          continue;
        }
      }
      if ((tag & 7) === 4 || tag === 0) {
        break;
      }
      reader.skip(tag & 7);
    }
    return message;
  },

  fromJSON(object: any): AclSpec {
    return {
      allowedRoles: globalThis.Array.isArray(object?.allowedRoles)
        ? object.allowedRoles.map((e: any) => globalThis.String(e))
        : globalThis.Array.isArray(object?.allowed_roles)
        ? object.allowed_roles.map((e: any) => globalThis.String(e))
        : [],
      allowedSubjects: globalThis.Array.isArray(object?.allowedSubjects)
        ? object.allowedSubjects.map((e: any) => globalThis.String(e))
        : globalThis.Array.isArray(object?.allowed_subjects)
        ? object.allowed_subjects.map((e: any) => globalThis.String(e))
        : [],
      deniedRoles: globalThis.Array.isArray(object?.deniedRoles)
        ? object.deniedRoles.map((e: any) => globalThis.String(e))
        : globalThis.Array.isArray(object?.denied_roles)
        ? object.denied_roles.map((e: any) => globalThis.String(e))
        : [],
      requiredAttributes: isObject(object.requiredAttributes)
        ? (globalThis.Object.entries(object.requiredAttributes) as [string, any][]).reduce(
          (acc: { [key: string]: string }, [key, value]: [string, any]) => {
            acc[key] = globalThis.String(value);
            return acc;
          },
          {},
        )
        : isObject(object.required_attributes)
        ? (globalThis.Object.entries(object.required_attributes) as [string, any][]).reduce(
          (acc: { [key: string]: string }, [key, value]: [string, any]) => {
            acc[key] = globalThis.String(value);
            return acc;
          },
          {},
        )
        : {},
    };
  },

  toJSON(message: AclSpec): unknown {
    const obj: any = {};
    if (message.allowedRoles?.length) {
      obj.allowedRoles = message.allowedRoles;
    }
    if (message.allowedSubjects?.length) {
      obj.allowedSubjects = message.allowedSubjects;
    }
    if (message.deniedRoles?.length) {
      obj.deniedRoles = message.deniedRoles;
    }
    if (message.requiredAttributes) {
      const entries = globalThis.Object.entries(message.requiredAttributes) as [string, string][];
      if (entries.length > 0) {
        obj.requiredAttributes = {};
        entries.forEach(([k, v]) => {
          obj.requiredAttributes[k] = v;
        });
      }
    }
    return obj;
  },

  create<I extends Exact<DeepPartial<AclSpec>, I>>(base?: I): AclSpec {
    return AclSpec.fromPartial(base ?? ({} as any));
  },
  fromPartial<I extends Exact<DeepPartial<AclSpec>, I>>(object: I): AclSpec {
    const message = createBaseAclSpec();
    message.allowedRoles = object.allowedRoles?.map((e) => e) || [];
    message.allowedSubjects = object.allowedSubjects?.map((e) => e) || [];
    message.deniedRoles = object.deniedRoles?.map((e) => e) || [];
    message.requiredAttributes = (globalThis.Object.entries(object.requiredAttributes ?? {}) as [string, string][])
      .reduce((acc: { [key: string]: string }, [key, value]: [string, string]) => {
        if (value !== undefined) {
          acc[key] = globalThis.String(value);
        }
        return acc;
      }, {});
    return message;
  },
};

function createBaseAclSpec_RequiredAttributesEntry(): AclSpec_RequiredAttributesEntry {
  return { key: "", value: "" };
}

export const AclSpec_RequiredAttributesEntry: MessageFns<AclSpec_RequiredAttributesEntry> = {
  encode(message: AclSpec_RequiredAttributesEntry, writer: BinaryWriter = new BinaryWriter()): BinaryWriter {
    if (message.key !== "") {
      writer.uint32(10).string(message.key);
    }
    if (message.value !== "") {
      writer.uint32(18).string(message.value);
    }
    return writer;
  },

  decode(input: BinaryReader | Uint8Array, length?: number): AclSpec_RequiredAttributesEntry {
    const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
    const end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseAclSpec_RequiredAttributesEntry();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1: {
          if (tag !== 10) {
            break;
          }

          message.key = reader.string();
          continue;
        }
        case 2: {
          if (tag !== 18) {
            break;
          }

          message.value = reader.string();
          continue;
        }
      }
      if ((tag & 7) === 4 || tag === 0) {
        break;
      }
      reader.skip(tag & 7);
    }
    return message;
  },

  fromJSON(object: any): AclSpec_RequiredAttributesEntry {
    return {
      key: isSet(object.key) ? globalThis.String(object.key) : "",
      value: isSet(object.value) ? globalThis.String(object.value) : "",
    };
  },

  toJSON(message: AclSpec_RequiredAttributesEntry): unknown {
    const obj: any = {};
    if (message.key !== "") {
      obj.key = message.key;
    }
    if (message.value !== "") {
      obj.value = message.value;
    }
    return obj;
  },

  create<I extends Exact<DeepPartial<AclSpec_RequiredAttributesEntry>, I>>(base?: I): AclSpec_RequiredAttributesEntry {
    return AclSpec_RequiredAttributesEntry.fromPartial(base ?? ({} as any));
  },
  fromPartial<I extends Exact<DeepPartial<AclSpec_RequiredAttributesEntry>, I>>(
    object: I,
  ): AclSpec_RequiredAttributesEntry {
    const message = createBaseAclSpec_RequiredAttributesEntry();
    message.key = object.key ?? "";
    message.value = object.value ?? "";
    return message;
  },
};

function createBaseChatMessage(): ChatMessage {
  return { role: 0, content: "", toolCallId: "", name: "" };
}

export const ChatMessage: MessageFns<ChatMessage> = {
  encode(message: ChatMessage, writer: BinaryWriter = new BinaryWriter()): BinaryWriter {
    if (message.role !== 0) {
      writer.uint32(8).int32(message.role);
    }
    if (message.content !== "") {
      writer.uint32(18).string(message.content);
    }
    if (message.toolCallId !== "") {
      writer.uint32(26).string(message.toolCallId);
    }
    if (message.name !== "") {
      writer.uint32(34).string(message.name);
    }
    return writer;
  },

  decode(input: BinaryReader | Uint8Array, length?: number): ChatMessage {
    const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
    const end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseChatMessage();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1: {
          if (tag !== 8) {
            break;
          }

          message.role = reader.int32() as any;
          continue;
        }
        case 2: {
          if (tag !== 18) {
            break;
          }

          message.content = reader.string();
          continue;
        }
        case 3: {
          if (tag !== 26) {
            break;
          }

          message.toolCallId = reader.string();
          continue;
        }
        case 4: {
          if (tag !== 34) {
            break;
          }

          message.name = reader.string();
          continue;
        }
      }
      if ((tag & 7) === 4 || tag === 0) {
        break;
      }
      reader.skip(tag & 7);
    }
    return message;
  },

  fromJSON(object: any): ChatMessage {
    return {
      role: isSet(object.role) ? chatRoleFromJSON(object.role) : 0,
      content: isSet(object.content) ? globalThis.String(object.content) : "",
      toolCallId: isSet(object.toolCallId)
        ? globalThis.String(object.toolCallId)
        : isSet(object.tool_call_id)
        ? globalThis.String(object.tool_call_id)
        : "",
      name: isSet(object.name) ? globalThis.String(object.name) : "",
    };
  },

  toJSON(message: ChatMessage): unknown {
    const obj: any = {};
    if (message.role !== 0) {
      obj.role = chatRoleToJSON(message.role);
    }
    if (message.content !== "") {
      obj.content = message.content;
    }
    if (message.toolCallId !== "") {
      obj.toolCallId = message.toolCallId;
    }
    if (message.name !== "") {
      obj.name = message.name;
    }
    return obj;
  },

  create<I extends Exact<DeepPartial<ChatMessage>, I>>(base?: I): ChatMessage {
    return ChatMessage.fromPartial(base ?? ({} as any));
  },
  fromPartial<I extends Exact<DeepPartial<ChatMessage>, I>>(object: I): ChatMessage {
    const message = createBaseChatMessage();
    message.role = object.role ?? 0;
    message.content = object.content ?? "";
    message.toolCallId = object.toolCallId ?? "";
    message.name = object.name ?? "";
    return message;
  },
};

function createBaseToolDescriptor(): ToolDescriptor {
  return { name: "", description: "", schema: undefined };
}

export const ToolDescriptor: MessageFns<ToolDescriptor> = {
  encode(message: ToolDescriptor, writer: BinaryWriter = new BinaryWriter()): BinaryWriter {
    if (message.name !== "") {
      writer.uint32(10).string(message.name);
    }
    if (message.description !== "") {
      writer.uint32(18).string(message.description);
    }
    if (message.schema !== undefined) {
      Struct.encode(Struct.wrap(message.schema), writer.uint32(26).fork()).join();
    }
    return writer;
  },

  decode(input: BinaryReader | Uint8Array, length?: number): ToolDescriptor {
    const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
    const end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseToolDescriptor();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1: {
          if (tag !== 10) {
            break;
          }

          message.name = reader.string();
          continue;
        }
        case 2: {
          if (tag !== 18) {
            break;
          }

          message.description = reader.string();
          continue;
        }
        case 3: {
          if (tag !== 26) {
            break;
          }

          message.schema = Struct.unwrap(Struct.decode(reader, reader.uint32()));
          continue;
        }
      }
      if ((tag & 7) === 4 || tag === 0) {
        break;
      }
      reader.skip(tag & 7);
    }
    return message;
  },

  fromJSON(object: any): ToolDescriptor {
    return {
      name: isSet(object.name) ? globalThis.String(object.name) : "",
      description: isSet(object.description) ? globalThis.String(object.description) : "",
      schema: isObject(object.schema) ? object.schema : undefined,
    };
  },

  toJSON(message: ToolDescriptor): unknown {
    const obj: any = {};
    if (message.name !== "") {
      obj.name = message.name;
    }
    if (message.description !== "") {
      obj.description = message.description;
    }
    if (message.schema !== undefined) {
      obj.schema = message.schema;
    }
    return obj;
  },

  create<I extends Exact<DeepPartial<ToolDescriptor>, I>>(base?: I): ToolDescriptor {
    return ToolDescriptor.fromPartial(base ?? ({} as any));
  },
  fromPartial<I extends Exact<DeepPartial<ToolDescriptor>, I>>(object: I): ToolDescriptor {
    const message = createBaseToolDescriptor();
    message.name = object.name ?? "";
    message.description = object.description ?? "";
    message.schema = object.schema ?? undefined;
    return message;
  },
};

type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;

type DeepPartial<T> = T extends Builtin ? T
  : T extends Long ? string | number | Long : T extends globalThis.Array<infer U> ? globalThis.Array<DeepPartial<U>>
  : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>>
  : T extends {} ? { [K in keyof T]?: DeepPartial<T[K]> }
  : Partial<T>;

type KeysOfUnion<T> = T extends T ? keyof T : never;
type Exact<P, I extends P> = P extends Builtin ? P
  : P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: never };

function isObject(value: any): boolean {
  return typeof value === "object" && value !== null;
}

function isSet(value: any): boolean {
  return value !== null && value !== undefined;
}

interface MessageFns<T> {
  encode(message: T, writer?: BinaryWriter): BinaryWriter;
  decode(input: BinaryReader | Uint8Array, length?: number): T;
  fromJSON(object: any): T;
  toJSON(message: T): unknown;
  create<I extends Exact<DeepPartial<T>, I>>(base?: I): T;
  fromPartial<I extends Exact<DeepPartial<T>, I>>(object: I): T;
}
