sailfork / t3code-github / .repos/ alchemy-effect/ packages/ gcp/ src/ ContactCenterInsights/ ConversationsFeedbackLabel.ts

import * as cci from "@distilled.cloud/gcp/contactcenterinsights_v1";
import { unownedUnlessPrevious } from "alchemy/AdoptPolicy";
import { isResolved } from "alchemy/Diff";
import * as Provider from "alchemy/Provider";
import { Resource } from "alchemy/Resource";
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as Stream from "effect/Stream";
import { GcpEnvironment } from "../Environment.ts";
import { createInternalLabels } from "../Labels.ts";
import type { Providers } from "../Providers.ts";
import {
  encodeOwnershipLine,
  hasOwnershipMarker,
  lastSegment,
  locationOf,
  locationParent,
  MAX_LABEL_LENGTH,
  ownedByAlchemy,
  parentOf,
  parseOwnership,
  sameJson,
  toResourceId,
} from "./ownership.ts";

type QaAnswerLabel = {
  /** Skip this question. */
  skipValue?: boolean;
  /** Not-applicable value. */
  naValue?: boolean;
  /** Answer-choice key. */
  key?: string;
  /** Numeric value. */
  numValue?: number;
  /** Boolean value. */
  boolValue?: boolean;
  /** String value. */
  strValue?: string;
};

export type ConversationsFeedbackLabelProps = {
  /**
   * Parent Conversation resource name
   * (`projects/{project}/locations/{location}/conversations/{conversation}`).
   * Immutable — changing it replaces the feedback label.
   */
  parent: string;
  /**
   * Feedback label id (the `{feedback_label}` segment). If omitted, a
   * unique id is generated. Immutable — changing it replaces the label.
   */
  feedbackLabelId?: string;
  /**
   * Topic-modeling string label. Feedback labels have no labels field, so
   * Alchemy ownership is stored in a `[alchemy …]` prefix and stripped
   * from attributes.
   */
  label?: string;
  /**
   * Resource being labeled (scorecard question, issue model, or generator).
   */
  labeledResource?: string;
  /**
   * Quality-AI answer label.
   */
  qaAnswerLabel?: QaAnswerLabel;
};

export type ConversationsFeedbackLabel = Resource<
  "GCP.ContactCenterInsights.ConversationsFeedbackLabel",
  ConversationsFeedbackLabelProps,
  {
    /** Full resource name. */
    name: string;
    /** Feedback label id (last path segment). */
    feedbackLabelId: string;
    /** Parent conversation resource name. */
    parent: string;
    /** Region id. */
    location: string;
    /** Project id. */
    project: string;
    /** User label with the Alchemy ownership prefix stripped. */
    label: string | undefined;
    /** Resource being labeled. */
    labeledResource: string | undefined;
    /** Quality-AI answer label. */
    qaAnswerLabel: QaAnswerLabel | undefined;
    /** RFC3339 creation timestamp. */
    createTime: string | undefined;
    /** RFC3339 last-update timestamp. */
    updateTime: string | undefined;
  },
  never,
  Providers
>;

/**
 * A feedback label on a Contact Center Insights conversation.
 *
 * Parent conversation and feedback label id are immutable. Feedback
 * labels have no labels map — Alchemy stamps ownership into the `label`
 * string. The label text, labeled resource, and QA answer update in
 * place.
 *
 * ### Creating a Feedback Label
 * **Example:** Topic-modeling label
 * ```typescript
 * const label = yield* GCP.ContactCenterInsights.ConversationsFeedbackLabel(
 *   "Topic",
 *   {
 *     parent: conversation.name,
 *     label: "billing",
 *   },
 * );
 * ```
 *
 * @resource
 * @category ContactCenterInsights
 */
export const ConversationsFeedbackLabel = Resource<ConversationsFeedbackLabel>(
  "GCP.ContactCenterInsights.ConversationsFeedbackLabel",
);

export class ConversationsFeedbackLabelNotResolved extends Data.TaggedError(
  "GCP.ContactCenterInsights.ConversationsFeedbackLabelNotResolved",
)<{
  resourceName: string;
}> {}

const resourceName = (parent: string, feedbackLabelId: string) =>
  `${parent}/feedbackLabels/${feedbackLabelId}`;

const toQa = (
  value: cci.GoogleCloudContactcenterinsightsV1QaAnswerAnswerValue | QaAnswerLabel | undefined,
): QaAnswerLabel | undefined => {
  if (value === undefined) return undefined;
  return {
    skipValue: value.skipValue,
    naValue: value.naValue,
    key: value.key,
    numValue: value.numValue,
    boolValue: value.boolValue,
    strValue: value.strValue,
  };
};

const toAttrs = (label: cci.GoogleCloudContactcenterinsightsV1FeedbackLabel, project: string) => {
  const name = label.name ?? "";
  const parsed = parseOwnership(label.label);
  return {
    name,
    feedbackLabelId: lastSegment(name),
    parent: parentOf(name),
    location: locationOf(name),
    project,
    label: parsed.text,
    labeledResource: label.labeledResource,
    qaAnswerLabel: toQa(label.qaAnswerLabel),
    createTime: label.createTime,
    updateTime: label.updateTime,
  };
};

const getByName = (name: string) =>
  name.length === 0
    ? Effect.undefined
    : cci
        .getProjectsLocationsConversationsFeedbackLabels({ name })
        .pipe(Effect.catchTag("NotFound", () => Effect.undefined));

const listAll = (parent: string, project: string) =>
  cci.listAllFeedbackLabelsProjectsLocations.pages({ parent, pageSize: 100 }).pipe(
    Stream.flatMap((page) => Stream.fromIterable(page.feedbackLabels ?? [])),
    Stream.filter((label) => hasOwnershipMarker(label.label)),
    Stream.filter((label) => !label.name?.includes("/datasets/")),
    Stream.map((label) => toAttrs(label, project)),
    Stream.runCollect,
    Effect.map((chunk) => Array.from(chunk)),
    Effect.catchTag("NotFound", () => Effect.succeed([])),
  );

export const ConversationsFeedbackLabelProvider = () =>
  Provider.succeed(ConversationsFeedbackLabel, {
    stables: ["name", "feedbackLabelId", "parent", "location", "project", "createTime"],

    diff: Effect.fn(function* ({ news, olds, output }) {
      if (!isResolved(news)) return undefined;
      const previousParent = olds?.parent ?? output?.parent;
      if (previousParent !== undefined && news.parent !== previousParent) {
        return { action: "replace" as const, deleteFirst: false };
      }
      const previousId = olds?.feedbackLabelId ?? output?.feedbackLabelId;
      if (
        previousId !== undefined &&
        news.feedbackLabelId !== undefined &&
        news.feedbackLabelId !== previousId
      ) {
        return { action: "replace" as const, deleteFirst: true };
      }
      return undefined;
    }),

    read: Effect.fn(function* ({ id, olds, output, previous }) {
      const env = yield* GcpEnvironment.current;
      const feedbackLabelId = yield* toResourceId(
        id,
        olds?.feedbackLabelId,
        output?.feedbackLabelId,
      );
      const name =
        output?.name ??
        (olds?.parent !== undefined ? resourceName(olds.parent, feedbackLabelId) : "");
      const existing = yield* getByName(name);
      if (existing === undefined) return undefined;
      const attrs = toAttrs(existing, env.project);
      return (yield* ownedByAlchemy(id, existing.label))
        ? attrs
        : unownedUnlessPrevious(attrs, previous, "name");
    }),

    list: () =>
      Effect.gen(function* () {
        const env = yield* GcpEnvironment.current;
        return yield* listAll(locationParent(env.project, env.region), env.project);
      }),

    reconcile: Effect.fn(function* ({ id, news, output }) {
      const env = yield* GcpEnvironment.current;
      const feedbackLabelId = yield* toResourceId(
        id,
        news.feedbackLabelId,
        output?.feedbackLabelId,
      );
      const name = resourceName(news.parent, feedbackLabelId);
      const ownership = yield* createInternalLabels(id);
      const label = encodeOwnershipLine(ownership, news.label, MAX_LABEL_LENGTH);

      let current = yield* getByName(output?.name ?? name);

      if (current === undefined) {
        const created = yield* cci
          .createProjectsLocationsConversationsFeedbackLabels({
            parent: news.parent,
            feedbackLabelId,
            body: {
              label,
              labeledResource: news.labeledResource,
              qaAnswerLabel: news.qaAnswerLabel,
            },
          })
          .pipe(Effect.catchTag("Conflict", () => getByName(name)));
        current = created ?? undefined;
      }

      if (current === undefined) {
        return yield* new ConversationsFeedbackLabelNotResolved({ resourceName: name });
      }

      const labelChanged = (current.label ?? "") !== label;
      const resourceChanged = (current.labeledResource ?? "") !== (news.labeledResource ?? "");
      const qaChanged = !sameJson(toQa(current.qaAnswerLabel), news.qaAnswerLabel);

      if (labelChanged || resourceChanged || qaChanged) {
        current = yield* cci.patchProjectsLocationsConversationsFeedbackLabels({
          name: current.name ?? name,
          updateMask: [
            labelChanged ? "label" : undefined,
            resourceChanged ? "labeled_resource" : undefined,
            qaChanged ? "qa_answer_label" : undefined,
          ]
            .filter((field): field is string => field !== undefined)
            .join(","),
          body: {
            name: current.name ?? name,
            label,
            labeledResource: news.labeledResource,
            qaAnswerLabel: news.qaAnswerLabel,
          },
        });
      }

      return toAttrs(current, env.project);
    }),

    delete: Effect.fn(function* ({ output }) {
      yield* cci
        .deleteProjectsLocationsConversationsFeedbackLabels({
          name: output.name,
        })
        .pipe(Effect.catchTag("NotFound", () => Effect.void));
    }),
  });