Skip to content

Commit b617205

Browse files
committed
Consolidate interface and refactor AILogic checks
1 parent b91d42d commit b617205

2 files changed

Lines changed: 37 additions & 15 deletions

File tree

src/deploy/functions/prepare.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,17 @@ import * as prompt from "../../prompt";
6666
export const EVENTARC_SOURCE_ENV = "EVENTARC_CLOUD_EVENT_SOURCE";
6767
export const DEFAULT_FUNCTION_REGION = "us-central1";
6868

69-
interface EventTriggerResolutionTarget {
69+
/**
70+
* A subset of the backend.Endpoint interface for the purposes of resolving regions.
71+
*/
72+
interface TriggerResolutionTarget {
7073
project: string;
71-
eventTrigger: {
74+
eventTrigger?: {
7275
eventType: string;
7376
eventFilters?: Record<string, string | build.Expression<string>>;
7477
region?: string | build.Expression<string>;
7578
};
76-
}
77-
78-
interface BlockingTriggerResolutionTarget {
79-
blockingTrigger: {
79+
blockingTrigger?: {
8080
eventType: string;
8181
options?: Record<string, unknown>;
8282
};
@@ -415,22 +415,26 @@ export async function resolveDefaultRegionsForBuild(
415415
}
416416
}
417417

418-
function resolveRegionForBlockingTrigger(endpoint: BlockingTriggerResolutionTarget): string {
418+
function resolveRegionForBlockingTrigger(endpoint: TriggerResolutionTarget): string {
419+
if (!endpoint.blockingTrigger) {
420+
return DEFAULT_FUNCTION_REGION;
421+
}
419422
const eventType = endpoint.blockingTrigger.eventType;
420423
if ((events.AUTH_BLOCKING_EVENTS as readonly string[]).includes(eventType)) {
421424
return "us-east1";
422425
}
423426

424-
if (isGlobalAILogicEndpoint(endpoint as backend.Endpoint)) {
427+
if (isGlobalAILogicEndpoint(endpoint)) {
425428
return "us-east1";
426429
}
427430

428431
return DEFAULT_FUNCTION_REGION;
429432
}
430433

431-
async function resolveRegionForEventTrigger(
432-
endpoint: EventTriggerResolutionTarget,
433-
): Promise<string> {
434+
async function resolveRegionForEventTrigger(endpoint: TriggerResolutionTarget): Promise<string> {
435+
if (!endpoint.eventTrigger) {
436+
return DEFAULT_FUNCTION_REGION;
437+
}
434438
const eventTrigger = endpoint.eventTrigger;
435439
const eventType = eventTrigger.eventType;
436440

src/deploy/functions/services/ailogic.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@ export const AI_LOGIC_EVENTS = [
1515
AI_LOGIC_AFTER_GENERATE_CONTENT,
1616
] as const;
1717

18-
export type AILogicEndpoint = backend.Endpoint & {
18+
export interface AILogicTriggerTarget {
19+
blockingTrigger?: {
20+
eventType: string;
21+
options?: Record<string, unknown>;
22+
};
23+
project?: string;
24+
}
25+
26+
export type AILogicTrigger = {
1927
blockingTrigger: {
2028
eventType: (typeof AI_LOGIC_EVENTS)[number];
2129
options?: {
@@ -24,16 +32,26 @@ export type AILogicEndpoint = backend.Endpoint & {
2432
};
2533
};
2634

27-
export function isAILogicEvent(endpoint: backend.Endpoint): endpoint is AILogicEndpoint {
28-
if (!backend.isBlockingTriggered(endpoint)) {
35+
export type AILogicEndpoint = backend.Endpoint & AILogicTrigger;
36+
37+
/**
38+
* Type guard to check if a trigger target is an AI Logic event trigger.
39+
*/
40+
export function isAILogicEvent<T extends AILogicTriggerTarget>(
41+
endpoint: T,
42+
): endpoint is T & AILogicTrigger {
43+
if (!endpoint.blockingTrigger) {
2944
return false;
3045
}
3146
return AI_LOGIC_EVENTS.includes(
3247
endpoint.blockingTrigger.eventType as (typeof AI_LOGIC_EVENTS)[number],
3348
);
3449
}
3550

36-
export function isGlobalAILogicEndpoint(endpoint: backend.Endpoint): boolean {
51+
/**
52+
* Check if an AI Logic trigger target is global (i.e. not a regional webhook).
53+
*/
54+
export function isGlobalAILogicEndpoint(endpoint: AILogicTriggerTarget): boolean {
3755
if (!isAILogicEvent(endpoint)) {
3856
return false;
3957
}

0 commit comments

Comments
 (0)