Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

6.3 KB
Raw Blame
import type {
  AkeruMemoryId,
  AkeruMemoryMutation,
  AkeruMemoryRevision,
  AkeruMemoryRootId,
  AkeruMemoryTargetScope,
  AkeruMemoryThreadAccess,
  AkeruMemoryImportPreview,
  AkeruMemoryImportApplyResult,
  MessageId,
} from "@akeru/contracts";
import * as Context from "effect/Context";
import type * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";

import type { ProjectionRepositoryError } from "../../persistence/Errors.ts";
import type { AkeruMemoryAccessDenied } from "../EntityMemoryAccess.ts";
import type { AuthorizedMemoryPartition } from "../EntityMemoryAccess.ts";

export class EntityMemoryConflictError extends Schema.TaggedErrorClass<EntityMemoryConflictError>()(
  "EntityMemoryConflictError",
  {
    rootId: Schema.String,
    expectedRevision: Schema.Number,
    actualRevision: Schema.NullOr(Schema.Number),
  },
) {
  override get message(): string {
    return `Memory revision conflict for ${this.rootId}. Expected ${this.expectedRevision}, found ${this.actualRevision ?? "none"}.`;
  }
}

export class EntityMemoryNotFoundError extends Schema.TaggedErrorClass<EntityMemoryNotFoundError>()(
  "EntityMemoryNotFoundError",
  { rootId: Schema.String },
) {
  override get message(): string {
    return `Memory ${this.rootId} does not exist.`;
  }
}

export interface SearchEntityMemoryInput {
  readonly access: AkeruMemoryThreadAccess;
  readonly query: string;
  readonly limit: number;
}

export interface GetEntityMemoryInput {
  readonly access: AkeruMemoryThreadAccess;
  readonly rootId: AkeruMemoryRootId;
}

export interface ListEntityMemoryInput {
  readonly access: AkeruMemoryThreadAccess;
}

export interface ListEntityMemoryPartitionsInput {
  readonly access: AkeruMemoryThreadAccess;
  readonly partitions: ReadonlyArray<AuthorizedMemoryPartition>;
  readonly complete: boolean;
}

export interface DeleteEntityMemoryInput extends GetEntityMemoryInput {}

export interface InsertEntityMemoryInput {
  readonly access: AkeruMemoryThreadAccess;
  readonly revision: AkeruMemoryRevision;
}

export interface ReviseEntityMemoryInput extends InsertEntityMemoryInput {
  readonly expectedRevision: number;
}

export interface ApplyEntityMemoryMutationInput {
  readonly access: AkeruMemoryThreadAccess;
  readonly mutation: Extract<AkeruMemoryMutation, { readonly operation: `fact.${string}` }>;
  readonly memoryId: AkeruMemoryId;
  readonly updatedAt: string;
  /** Shared-scope policy for a mutation that lands a fact on a shared scope. */
  readonly sharedProjectApproval: "approved" | "pending";
}

// Writes a new approved fact to the partition that backs a target scope for
// the given thread access. Used when a person approves a memory candidate.
export interface InsertScopedEntityMemoryInput {
  readonly access: AkeruMemoryThreadAccess;
  readonly scope: AkeruMemoryTargetScope;
  readonly fact: string;
  readonly sensitive: boolean;
  readonly confidence: number;
  readonly sourceMessageId: MessageId | null;
  readonly memoryId: AkeruMemoryId;
  readonly createdAt: string;
}

export interface TombstoneEntityMemoryInput {
  readonly access: AkeruMemoryThreadAccess;
  readonly rootId: AkeruMemoryRootId;
  readonly expectedRevision: number;
  readonly memoryId: AkeruMemoryId;
  readonly updatedAt: string;
}

export interface ImportEntityMemoryInput {
  readonly access: AkeruMemoryThreadAccess;
  readonly partitions: ReadonlyArray<AuthorizedMemoryPartition>;
  readonly revisions: ReadonlyArray<AkeruMemoryRevision>;
}

export interface ApplyEntityMemoryImportInput extends ImportEntityMemoryInput {
  readonly previewHash: string;
  readonly resolutions?: ReadonlyArray<{
    readonly rootId: AkeruMemoryRootId;
    readonly decision: "keep-local" | "use-archive";
  }>;
}

export class EntityMemoryImportError extends Schema.TaggedErrorClass<EntityMemoryImportError>()(
  "EntityMemoryImportError",
  { detail: Schema.String },
) {
  override get message(): string {
    return this.detail;
  }
}

export type EntityMemoryRepositoryError =
  | ProjectionRepositoryError
  | AkeruMemoryAccessDenied
  | EntityMemoryConflictError
  | EntityMemoryNotFoundError
  | EntityMemoryImportError;

export interface EntityMemoryRepositoryShape {
  readonly recordDerivedCopies?: (input: {
    readonly tenantId: string;
    readonly threadId: string;
    readonly revisions: ReadonlyArray<AkeruMemoryRevision>;
  }) => Effect.Effect<void, EntityMemoryRepositoryError>;
  readonly insert: (
    input: InsertEntityMemoryInput,
  ) => Effect.Effect<AkeruMemoryRevision, EntityMemoryRepositoryError>;
  readonly revise: (
    input: ReviseEntityMemoryInput,
  ) => Effect.Effect<AkeruMemoryRevision, EntityMemoryRepositoryError>;
  readonly tombstone: (
    input: TombstoneEntityMemoryInput,
  ) => Effect.Effect<AkeruMemoryRevision, EntityMemoryRepositoryError>;
  readonly getCurrent: (
    input: GetEntityMemoryInput,
  ) => Effect.Effect<AkeruMemoryRevision, EntityMemoryRepositoryError>;
  readonly search: (
    input: SearchEntityMemoryInput,
  ) => Effect.Effect<ReadonlyArray<AkeruMemoryRevision>, EntityMemoryRepositoryError>;
  readonly listCurrent: (
    input: ListEntityMemoryInput,
  ) => Effect.Effect<ReadonlyArray<AkeruMemoryRevision>, EntityMemoryRepositoryError>;
  readonly listHistory: (
    input: GetEntityMemoryInput,
  ) => Effect.Effect<ReadonlyArray<AkeruMemoryRevision>, EntityMemoryRepositoryError>;
  readonly listByPartitions: (
    input: ListEntityMemoryPartitionsInput,
  ) => Effect.Effect<ReadonlyArray<AkeruMemoryRevision>, EntityMemoryRepositoryError>;
  readonly previewImport: (
    input: ImportEntityMemoryInput,
  ) => Effect.Effect<AkeruMemoryImportPreview, EntityMemoryRepositoryError>;
  readonly applyImport: (
    input: ApplyEntityMemoryImportInput,
  ) => Effect.Effect<AkeruMemoryImportApplyResult, EntityMemoryRepositoryError>;
  readonly deleteRoot: (
    input: DeleteEntityMemoryInput,
  ) => Effect.Effect<void, EntityMemoryRepositoryError>;
  readonly insertScopedFact: (
    input: InsertScopedEntityMemoryInput,
  ) => Effect.Effect<AkeruMemoryRevision, EntityMemoryRepositoryError>;
  readonly applyMutation: (
    input: ApplyEntityMemoryMutationInput,
  ) => Effect.Effect<AkeruMemoryRevision | null, EntityMemoryRepositoryError>;
}

export class EntityMemoryRepository extends Context.Service<
  EntityMemoryRepository,
  EntityMemoryRepositoryShape
>()("akeru-bot/memory/Services/EntityMemoryRepository") {}