Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions spec/v2/providers/ai.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,25 @@
});
});
});
describe("Typings", () => {
it("should allow regional webhooks to specify multiple locations", () => {
ai.beforeGenerateContent(
{ regionalWebhook: true, location: ["us-central1", "europe-west1"] },
() => { }

Check failure on line 262 in spec/v2/providers/ai.spec.ts

View workflow job for this annotation

GitHub Actions / lint (22.x)

Delete `·`
);
});

it("should allow global webhooks to specify a single location", () => {
ai.beforeGenerateContent({ region: "us-central1" }, () => { });

Check failure on line 267 in spec/v2/providers/ai.spec.ts

View workflow job for this annotation

GitHub Actions / lint (22.x)

Delete `·`
});

it("should allow regional webhooks to specify a single location", () => {
ai.beforeGenerateContent({ regionalWebhook: true, region: "us-central1" }, () => { });

Check failure on line 271 in spec/v2/providers/ai.spec.ts

View workflow job for this annotation

GitHub Actions / lint (22.x)

Delete `·`
});

// Compilation failure tests (commented out):
// it("should NOT allow global webhooks to specify multiple locations", () => {
// ai.beforeGenerateContent({ region: ["us-central1", "europe-west1"] }, () => {});
// });
});
Comment thread
inlined marked this conversation as resolved.
});
38 changes: 26 additions & 12 deletions src/v2/providers/ai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,12 @@
GeminiV1BetaGenerateContentRequest,
GeminiV1BetaGenerateContentResponse,
};
export interface WebhookOptions extends Omit<EventHandlerOptions, "location"> {
location?: string | string[] | Expression<string> | ResetValue;
type MultipleLocationsIf<Allowed extends boolean> = Allowed extends true ? string[] : never;

export interface WebhookOptions<Regional extends boolean = false>
extends Omit<EventHandlerOptions, "location"> {
location?: string | Expression<string> | MultipleLocationsIf<Regional> | ResetValue;
regionalWebhook?: Regional;
}

export interface PromptTemplateInfo {
Expand Down Expand Up @@ -153,8 +157,8 @@
) => MaybeAsync<void | Partial<AnyValidAIRequest>>
): BlockingFunction;

export function beforeGenerateContent(
options: WebhookOptions,
export function beforeGenerateContent<Regional extends boolean = false>(
options: WebhookOptions<Regional>,
callback: (
event: AIBlockingEvent<BeforeGenerateContentData>
) => MaybeAsync<void | Partial<AnyValidAIRequest>>
Expand All @@ -164,8 +168,8 @@
optsOrCb:
| WebhookOptions
| ((
event: AIBlockingEvent<BeforeGenerateContentData>
) => MaybeAsync<void | Partial<AnyValidAIRequest>>),
event: AIBlockingEvent<BeforeGenerateContentData>

Check failure on line 171 in src/v2/providers/ai/index.ts

View workflow job for this annotation

GitHub Actions / lint (22.x)

Insert `··`
) => MaybeAsync<void | Partial<AnyValidAIRequest>>),

Check failure on line 172 in src/v2/providers/ai/index.ts

View workflow job for this annotation

GitHub Actions / lint (22.x)

Insert `··`
cb?: (
event: AIBlockingEvent<BeforeGenerateContentData>
) => MaybeAsync<void | Partial<AnyValidAIRequest>>
Expand Down Expand Up @@ -243,7 +247,12 @@
...baseOpts?.labels,
...specificOpts?.labels,
},
httpsTrigger: {},
blockingTrigger: {
eventType: beforeGenerateEventType,
options: {
regionalWebhook: opts.regionalWebhook,
},
},
};

return func as BlockingFunction;
Expand All @@ -255,8 +264,8 @@
) => MaybeAsync<void | Partial<AnyValidAIResponse>>
): BlockingFunction;

export function afterGenerateContent(
options: WebhookOptions,
export function afterGenerateContent<Regional extends boolean = false>(
options: WebhookOptions<Regional>,
callback: (
event: AIBlockingEvent<AfterGenerateContentData>
) => MaybeAsync<void | Partial<AnyValidAIResponse>>
Expand All @@ -266,8 +275,8 @@
optsOrCb:
| WebhookOptions
| ((
event: AIBlockingEvent<AfterGenerateContentData>
) => MaybeAsync<void | Partial<AnyValidAIResponse>>),
event: AIBlockingEvent<AfterGenerateContentData>

Check failure on line 278 in src/v2/providers/ai/index.ts

View workflow job for this annotation

GitHub Actions / lint (22.x)

Insert `··`
) => MaybeAsync<void | Partial<AnyValidAIResponse>>),

Check failure on line 279 in src/v2/providers/ai/index.ts

View workflow job for this annotation

GitHub Actions / lint (22.x)

Insert `··`
cb?: (
event: AIBlockingEvent<AfterGenerateContentData>
) => MaybeAsync<void | Partial<AnyValidAIResponse>>
Expand Down Expand Up @@ -345,7 +354,12 @@
...baseOpts?.labels,
...specificOpts?.labels,
},
httpsTrigger: {},
blockingTrigger: {
eventType: afterGenerateEventType,
options: {
regionalWebhook: opts.regionalWebhook,
},
},
};

return func as BlockingFunction;
Expand Down
Loading