Circle V2 API Docs
    Preparing search index...
    • Writes a read subtree onto the eval branch as a sequence of SHORT statements (parent → child, so any partial write is a referentially-consistent prefix), then episode-stamps the cloned charts. Deliberately NOT one big transaction: the branch connects through the transaction-mode pooler, which is hostile to long transactions, and the insert-only semantics (on conflict do nothing) already make a re-run resume idempotently — it fills in whatever a failed run didn't reach. Out-of-subtree FKs are nulled and secrets scrubbed on the way in; jsonb columns are wrapped so postgres.js parses them as JSON rather than typing a bare scalar (see toJsonb). A cloned patient is a frozen snapshot — a re-clone never overwrites existing rows nor clobbers entity-scoped parents shared with other clones. Owns the branch connection (destroy in finally).

      Parameters

      • subtree: {
            admissions: {
                admitted_at: string;
                created_at: DbDate;
                discharged_at: string | null;
                external_admission_id: string | null;
                id: PatientAdmissionId;
                integration_id: string | null;
                last_sync: string | null;
                level_of_care_id: number | null;
                patient_id: string & { __brand: "PatientId" } | null;
                updated_at: DbDate;
            }[];
            auditQuestions: {
                additional_instructions: string
                | null;
                created_at: DbDate;
                created_by: string & { __brand: "UserId" } | null;
                id: AuditQuestionId;
                manual: boolean;
                org_id: number | null;
                question: string | null;
                question_order: number | null;
                question_type: "summary" | "pass_fail";
                share_with_org: boolean | null;
                shared_by: string & { __brand: "UserId" } | null;
                shared_with_entity_id: number & { __brand: "EntityId" } | null;
                template_id: string & { __brand: "AuditTemplateId" } | null;
                updated_at: string & { __brand: "DbDate" } | null;
            }[];
            auditTemplates: {
                created_at: DbDate;
                created_by: string & { __brand: "UserId" }
                | null;
                custom_review_criteria_prompt: string | null;
                custom_summary_prompt: string | null;
                description: string | null;
                id: AuditTemplateId;
                name: string | null;
                org_id: number & { __brand: "EntityId" } | null;
                share_with_org: boolean | null;
                shared_by: string & { __brand: "UserId" } | null;
                shared_with_entity_id: number & { __brand: "EntityId" } | null;
                updated_at: string & { __brand: "DbDate" } | null;
            }[];
            cases: {
                created_at: DbDate;
                created_by: string
                | null;
                id: CaseId;
                is_patient_case: boolean;
                patient_id: string & { __brand: "PatientId" } | null;
                status: string | null;
                summary: string | null;
                title: string | null;
                updated_at: DbDate | null;
            }[];
            charts: {
                circle_metadata: | {
                    admittedAt: string
                    | null;
                    capturedAt: string;
                    dischargedAt: string | null;
                    entityId: number;
                    episodeEnd?: string | null;
                    episodeStart?: string | null;
                    levelOfCareName?: string | null;
                    patientId: PatientId;
                    patientName: string;
                    programEnd?: string | null;
                    programName?: string | null;
                    programStart?: string | null;
                    urLevelOfCareName?: string | null;
                }
                | null;
                created_at: DbDate;
                created_by: string & { __brand: "UserId" }
                | null;
                embedding_count: string | null;
                embedding_err_msg: string | null;
                embedding_status:
                    | "pending"
                    | "processing"
                    | "failed"
                    | "parse"
                    | "extract"
                    | "completed"
                    | null;
                entity_id: number & { __brand: "EntityId" }
                | null;
                external_document_created_at: string & { __brand: "DbDate" } | null;
                external_document_id: string | null;
                external_document_type:
                    | "kipu_evaluation"
                    | "kipu_consent_form"
                    | "kipu_ciwa_ar"
                    | "kipu_ciwa_b"
                    | "kipu_group_session"
                    | "lightningstep_formdata"
                    | "lightningstep_txplan"
                    | "kipu_vital_signs"
                    | "kipu_orthostatic_vital_signs"
                    | "kipu_patient_orders"
                    | "kipu_glucose_logs"
                    | "kipu_program_history"
                    | "kipu_diagnosis_history"
                    | "kipu_appointment"
                    | "lightningstep_admissions"
                    | "kipu_episode_outcome"
                    | "sunwave_form"
                    | "sunwave_diagnosis"
                    | "sunwave_group_note"
                    | "sunwave_program"
                    | "sunwave_lab_result"
                    | "sunwave_assessment"
                    | "emr_insurance_information"
                    | "insync_episode_summary"
                    | "insync_condition"
                    | "insync_vital_signs"
                    | "insync_lab_result"
                    | "insync_observation"
                    | "insync_allergy"
                    | "insync_immunization"
                    | "insync_medication_request"
                    | "insync_diagnostic_report"
                    | "insync_procedure"
                    | "insync_rpa_locadtr"
                    | "insync_rpa_treatment_plan"
                    | "insync_rpa_biopsychosocial"
                    | "synthetic_biopsychosocial"
                    | "synthetic_asam"
                    | "synthetic_consent"
                    | "synthetic_nursing_intake"
                    | "synthetic_diagnosis"
                    | "synthetic_treatment_plan"
                    | "synthetic_group_note"
                    | "synthetic_progress_note"
                    | "synthetic_ur_review"
                    | "synthetic_discharge_summary"
                    | "insync_rpa_encounter_note"
                    | "insync_rpa_consent_form"
                    | "insync_rpa_lab_result"
                    | "insync_rpa_insurance"
                    | "insync_rpa_document"
                    | null;
                external_document_updated_at: string & { __brand: "DbDate" }
                | null;
                file_name: string | null;
                file_type: "patient_chart" | "generated_form" | "emr_document" | null;
                file_url: string | null;
                id: PatientChartId;
                integration_id: string & { __brand: "IntegrationId" } | null;
                is_ready: boolean | null;
                patient_id: string & { __brand: "PatientId" } | null;
                raw_document: unknown;
                run_id: string | null;
                sources: unknown;
                treatment_episode_id: string & { __brand: "TreatmentEpisodeId" } | null;
                updated_at: string & { __brand: "DbDate" } | null;
            }[];
            entities: {
                archived_at: string
                | null;
                company_name: string;
                created_at: string | null;
                created_by: string | null;
                entity_id: EntityId;
            }[];
            entityLevelsOfCare: {
                created_at: DbDate;
                entity_id: number & { __brand: "EntityId" };
                facility_id: string | null;
                id: EntityLevelOfCareId;
                name: string;
                parent_level_of_care_id: number | null;
                updated_at: DbDate;
            }[];
            episodes: {
                admission_date: string
                | null;
                created_at: DbDate;
                discharge_date: string | null;
                episode_start: string | null;
                external_created_at: string | null;
                external_id: string | null;
                external_updated_at: string | null;
                facility_id: string;
                facility_level_of_care_id:
                    | string & { __brand: "FacilityLevelOfCareId" }
                    | null;
                id: TreatmentEpisodeId;
                integration_id: string | null;
                last_sync: string | null;
                last_synced_documents: string | null;
                legacy_admission_id: number & { __brand: "PatientAdmissionId" } | null;
                mrn: string | null;
                next_facility_level_of_care_id:
                    | string & { __brand: "FacilityLevelOfCareId" }
                    | null;
                next_level_of_care_date: string | null;
                patient_id: string & { __brand: "PatientId" };
                projected_discharge_date: string | null;
                status: string | null;
                updated_at: DbDate;
            }[];
            facilities: {
                admission_start_source: | "episode_start_date"
                | "first_program_start"
                | null;
                created_at: DbDate;
                entity_id: number & { __brand: "EntityId" };
                id: FacilityId;
                name: string;
                timezone: string | null;
                updated_at: DbDate;
            }[];
            facilityLevelsOfCare: {
                created_at: DbDate;
                entity_id: number & { __brand: "EntityId" };
                external_id: string | null;
                facility_id: string;
                id: FacilityLevelOfCareId;
                integration_id: string | null;
                name: string;
                type: "clinical" | "ur";
                updated_at: DbDate;
            }[];
            facilityPrograms: {
                created_at: DbDate;
                entity_id: number & { __brand: "EntityId" };
                external_created_at: string | null;
                external_id: string | null;
                external_updated_at: string | null;
                facility_id: string;
                id: FacilityProgramId;
                integration_id: string;
                name: string;
                updated_at: DbDate;
            }[];
            integrations: {
                created_at: DbDate;
                created_by: string
                | null;
                credentials:
                    | {
                        access_id: string;
                        app_id: string;
                        location_id: number;
                        location_name?: string;
                        secret_key: string;
                    }
                    | {
                        bearer_token: string;
                        location_ids: number[];
                        location_name?: string;
                    }
                    | {
                        api_id: string;
                        api_secret: string;
                        location_name?: string;
                        realm_id: string;
                        service_facility_id: number;
                        sunwave_email: string;
                    }
                    | {
                        base_url: string;
                        client_id: string;
                        extend_workflow_id?: string;
                        rpa_vault_user_id: string;
                        rpa_workflow_id: string;
                        secret_key: string;
                    }
                    | {
                        as_of_date?: string;
                        location_id: "1"
                        | "2";
                        location_name?: string;
                        seed: string;
                    };
                entity_id: number & { __brand: "EntityId" };
                facility_id: FacilityId | null;
                id: IntegrationId;
                label: string | null;
                last_sync: string | null;
                status: "error" | "active" | "inactive";
                type:
                    | "kipu"
                    | "lightningstep"
                    | "sunwave"
                    | "qualifacts_insync"
                    | "synthetic";
                updated_at: DbDate;
            }[];
            patient: {
                address_city: string
                | null;
                address_state: string | null;
                address_street1: string | null;
                address_street2: string | null;
                address_zipcode: string | null;
                created_at: DbDate;
                created_by: UserId | null;
                dob: string | null;
                email: string | null;
                entity_id: number & { __brand: "EntityId" } | null;
                external_patient_id: string | null;
                external_source:
                    | "kipu"
                    | "lightningstep"
                    | "sunwave"
                    | "qualifacts_insync"
                    | "synthetic"
                    | null;
                facility_id: string & { __brand: "FacilityId" }
                | null;
                first_name: string | null;
                gender: string | null;
                intake_notes: string | null;
                last_name: string | null;
                last_updated_summary: string | null;
                patient_id: PatientId;
                phone_mobile: string | null;
                primary_provider_id: string & { __brand: "ProfileId" } | null;
                status: string | null;
                summary: LegacyPatientSummary | null;
            };
            programStays: {
                created_at: DbDate;
                end_date: string
                | null;
                external_id: string | null;
                facility_level_of_care_id:
                    | string & { __brand: "FacilityLevelOfCareId" }
                    | null;
                facility_program_id: string & { __brand: "FacilityProgramId" } | null;
                id: ProgramStayId;
                integration_id: string | null;
                legacy_admission_id: number & { __brand: "PatientAdmissionId" } | null;
                start_date: string;
                treatment_episode_id: string;
                updated_at: DbDate;
            }[];
            sessions: {
                ai_summarized_note: string
                | null;
                as_summarized_note_type: string | null;
                audit_run_id: string & { __brand: "AuditRunId" } | null;
                audit_status:
                    | "error"
                    | "pending"
                    | "fail"
                    | "in_progress"
                    | "pass"
                    | "needs_review"
                    | "not_started"
                    | null;
                case_id: string & { __brand: "CaseId" }
                | null;
                cpt_code: unknown[] | null;
                created_at: DbDate;
                created_by: string | null;
                dsm_code:
                    | {
                        code: string;
                        description: string;
                        status: "suggested"
                        | "accepted"
                        | "rejected";
                    }[]
                    | null;
                end_timestamp: DbDate;
                error_msg: string
                | null;
                file_url: string | null;
                icd_code:
                    | {
                        code: string;
                        description: string;
                        status: "suggested"
                        | "accepted"
                        | "rejected";
                    }[]
                    | null;
                id: SessionId;
                manual_entry: boolean
                | null;
                patient_id: string & { __brand: "PatientId" } | null;
                private_note: string | null;
                progress_status:
                    | "initialized"
                    | "processing"
                    | "transcribing"
                    | "complete"
                    | "failed"
                    | null;
                selected_template: | {
                    additional_ai_instructions?: string;
                    check_cpt?: boolean;
                    check_demo?: boolean;
                    check_dsm_5?: boolean;
                    check_icd_10?: boolean;
                    created_by?: UserId
                    | null;
                    created_by_circle?: boolean;
                    hidden_instructions?: string | null;
                    id: TemplateId;
                    is_allow_bullet_points?: boolean;
                    is_unstructured_narrative?: boolean;
                    last_updated?: string | null;
                    last_used?: string | null;
                    name?: string;
                    original_template_id?: string;
                    sections: {
                        body: string;
                        bullet_format?: boolean;
                        id: SectionId;
                        name: string;
                        optional: boolean;
                        order: number;
                    }[];
                    selected_audit_template_ids?: number[];
                    shared_with_org_id?: string;
                    title: string;
                    [key: string]: unknown;
                }
                | null;
                start_timestamp: DbDate;
                transcript: string
                | null;
                updated_by: number | null;
            }[];
        }

      Returns Promise<
          {
              counts: Record<string, number>;
              stampedCharts: number;
              unstampedCharts: number;
          },
      >