import {z} from 'zod' export type AnalyzeEntitiesRequest = {alternativeOutputFormat?: ('ALTERNATIVE_OUTPUT_FORMAT_UNSPECIFIED' | 'FHIR_BUNDLE') | undefined, documentContent?: string | undefined, licensedVocabularies?: (Array<'LICENSED_VOCABULARY_UNSPECIFIED' | 'ICD10CM' | 'SNOMEDCT_US'>) | undefined}; export type CreateApiV1ProjectsProjectsIdLocationsLocationsIdServicesNlpAnalyzeEntitiesArgs = {projectsId: string, locationsId: string, body: AnalyzeEntitiesRequest}; export type CreateApiV1ProjectsProjectsIdLocationsLocationsIdServicesNlpAnalyzeEntitiesOptions = {baseUrl?: string | undefined, Oauth2?: string | undefined, Oauth2c?: string | undefined, fetch?: typeof fetch | undefined}; export const entity = z.object({entityId: z.string().optional(), preferredTerm: z.string().optional(), vocabularyCodes: z.array(z.string()).optional()}); export const feature = z.object({confidence: z.number().optional(), value: z.string().optional()}); export const linkedEntity = z.object({entityId: z.string().optional()}); export const textSpan = z.object({beginOffset: z.number().int().optional(), content: z.string().optional()}); export const entityMention = z.object({certaintyAssessment: feature.optional(), confidence: z.number().optional(), linkedEntities: z.array(linkedEntity).optional(), mentionId: z.string().optional(), subject: feature.optional(), temporalAssessment: feature.optional(), text: textSpan.optional(), type: z.string().optional()}); export const entityMentionRelationship = z.object({confidence: z.number().optional(), objectId: z.string().optional(), subjectId: z.string().optional()}); export const analyzeEntitiesResponse = z.object({entities: z.array(entity).optional(), entityMentions: z.array(entityMention).optional(), fhirBundle: z.string().optional(), relationships: z.array(entityMentionRelationship).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Analyze heathcare entity in a document. Its response includes the recognized entity mentions and the relationships between them. AnalyzeEntities uses context aware models to detect entities. * * POST /v1/projects/{projectsId}/locations/{locationsId}/services/nlp:analyzeEntities * * Also accepts auth: Oauth2 + Oauth2c */ export const createApiV1ProjectsProjectsIdLocationsLocationsIdServicesNlpAnalyzeEntities = async (args: CreateApiV1ProjectsProjectsIdLocationsLocationsIdServicesNlpAnalyzeEntitiesArgs, options?: CreateApiV1ProjectsProjectsIdLocationsLocationsIdServicesNlpAnalyzeEntitiesOptions) => { const {baseUrl = "https://healthcare.googleapis.com", fetch: fetchFn = fetch, Oauth2, Oauth2c} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Oauth2 !== undefined) headers.set('authorization', `Bearer ${Oauth2}`) if (Oauth2c !== undefined) headers.set('authorization', `Bearer ${Oauth2c}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/projects/${encodeURIComponent(String(args.projectsId))}/locations/${encodeURIComponent(String(args.locationsId))}/services/nlp:analyzeEntities${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(analyzeEntitiesResponse, await res.json()) };