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
37 changes: 19 additions & 18 deletions src/deploy/functions/services/ailogic.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { AILogicService } from "./ailogic";
import {
AILogicService,
AI_LOGIC_BEFORE_GENERATE_CONTENT,
AI_LOGIC_AFTER_GENERATE_CONTENT,
} from "./ailogic";
import * as backend from "../backend";
import { expect } from "chai";
import * as ailogicApi from "../../../gcp/ailogic";
Expand All @@ -12,9 +16,6 @@ const BASE_EP = {
platform: "gcfv2" as const,
};

const BEFORE_GENERATE = "firebase.vertexai.v1beta.beforeGenerateContent";
const AFTER_GENERATE = "firebase.vertexai.v1beta.afterGenerateContent";

describe("AILogicService", () => {
const service = new AILogicService();
let upsertStub: sinon.SinonStub;
Expand All @@ -35,21 +36,21 @@ describe("AILogicService", () => {
...BASE_EP,
id: "func1",
blockingTrigger: {
eventType: BEFORE_GENERATE,
eventType: AI_LOGIC_BEFORE_GENERATE_CONTENT,
options: { regionalWebhook: true },
},
};
const ep2: backend.Endpoint = {
...BASE_EP,
id: "func2",
blockingTrigger: {
eventType: BEFORE_GENERATE,
eventType: AI_LOGIC_BEFORE_GENERATE_CONTENT,
options: { regionalWebhook: true },
},
};

expect(() => service.validateTrigger(ep1, backend.of(ep1, ep2))).to.throw(
`Can only create at most one regional AI Logic Trigger for ${BEFORE_GENERATE} in region us-central1`,
`Can only create at most one regional AI Logic Trigger for ${AI_LOGIC_BEFORE_GENERATE_CONTENT} in region us-central1`,
);
});

Expand All @@ -59,7 +60,7 @@ describe("AILogicService", () => {
id: "func1",
region: "us-central1",
blockingTrigger: {
eventType: BEFORE_GENERATE,
eventType: AI_LOGIC_BEFORE_GENERATE_CONTENT,
options: { regionalWebhook: true },
},
};
Expand All @@ -68,7 +69,7 @@ describe("AILogicService", () => {
id: "func2",
region: "us-east1",
blockingTrigger: {
eventType: BEFORE_GENERATE,
eventType: AI_LOGIC_BEFORE_GENERATE_CONTENT,
options: { regionalWebhook: true },
},
};
Expand All @@ -81,19 +82,19 @@ describe("AILogicService", () => {
...BASE_EP,
id: "func1",
blockingTrigger: {
eventType: BEFORE_GENERATE,
eventType: AI_LOGIC_BEFORE_GENERATE_CONTENT,
},
};
const ep2: backend.Endpoint = {
...BASE_EP,
id: "func2",
blockingTrigger: {
eventType: BEFORE_GENERATE,
eventType: AI_LOGIC_BEFORE_GENERATE_CONTENT,
},
};

expect(() => service.validateTrigger(ep1, backend.of(ep1, ep2))).to.throw(
`Can only create at most one global AI Logic Trigger for ${BEFORE_GENERATE}`,
`Can only create at most one global AI Logic Trigger for ${AI_LOGIC_BEFORE_GENERATE_CONTENT}`,
);
});

Expand All @@ -102,15 +103,15 @@ describe("AILogicService", () => {
...BASE_EP,
id: "func1",
blockingTrigger: {
eventType: BEFORE_GENERATE,
eventType: AI_LOGIC_BEFORE_GENERATE_CONTENT,
options: { regionalWebhook: true },
},
};
const ep2: backend.Endpoint = {
...BASE_EP,
id: "func2",
blockingTrigger: {
eventType: BEFORE_GENERATE,
eventType: AI_LOGIC_BEFORE_GENERATE_CONTENT,
},
};

Expand All @@ -122,14 +123,14 @@ describe("AILogicService", () => {
...BASE_EP,
id: "func1",
blockingTrigger: {
eventType: BEFORE_GENERATE,
eventType: AI_LOGIC_BEFORE_GENERATE_CONTENT,
},
};
const ep2: backend.Endpoint = {
...BASE_EP,
id: "func2",
blockingTrigger: {
eventType: AFTER_GENERATE,
eventType: AI_LOGIC_AFTER_GENERATE_CONTENT,
},
};

Expand All @@ -141,7 +142,7 @@ describe("AILogicService", () => {
it("should call upsertBlockingFunction", async () => {
const ep: backend.Endpoint = {
...BASE_EP,
blockingTrigger: { eventType: BEFORE_GENERATE },
blockingTrigger: { eventType: AI_LOGIC_BEFORE_GENERATE_CONTENT },
};

await service.registerTrigger(ep);
Expand All @@ -154,7 +155,7 @@ describe("AILogicService", () => {
it("should call deleteBlockingFunction", async () => {
const ep: backend.Endpoint = {
...BASE_EP,
blockingTrigger: { eventType: BEFORE_GENERATE },
blockingTrigger: { eventType: AI_LOGIC_BEFORE_GENERATE_CONTENT },
};

await service.unregisterTrigger(ep);
Expand Down
10 changes: 5 additions & 5 deletions src/deploy/functions/services/ailogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import { FirebaseError } from "../../../error";
import { Name, Service } from "./index";
import * as ailogicApi from "../../../gcp/ailogic";

export const AI_LOGIC_BEFORE_GENERATE_CONTENT =
"firebase.vertexai.v1beta.beforeGenerateContent" as const;
export const AI_LOGIC_AFTER_GENERATE_CONTENT =
"firebase.vertexai.v1beta.afterGenerateContent" as const;
import {
AI_LOGIC_BEFORE_GENERATE_CONTENT,
AI_LOGIC_AFTER_GENERATE_CONTENT,
} from "../../../gcp/ailogic";
export { AI_LOGIC_BEFORE_GENERATE_CONTENT, AI_LOGIC_AFTER_GENERATE_CONTENT };

export const AI_LOGIC_EVENTS = [
AI_LOGIC_BEFORE_GENERATE_CONTENT,
Expand All @@ -22,7 +22,7 @@
};
};

export function isAILogicEvent(endpoint: backend.Endpoint): endpoint is AILogicEndpoint {

Check warning on line 25 in src/deploy/functions/services/ailogic.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
if (!backend.isBlockingTriggered(endpoint)) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/deploy/functions/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
name: "noop",
api: "",
ensureTriggerRegion: noop,
validateTrigger: noop,

Check warning on line 52 in src/deploy/functions/services/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Promise-returning function provided to property where a void return was expected
registerTrigger: noop,
unregisterTrigger: noop,
};
Expand All @@ -60,7 +60,7 @@
api: "pubsub.googleapis.com",
requiredProjectBindings: noopProjectBindings,
ensureTriggerRegion: noop,
validateTrigger: noop,

Check warning on line 63 in src/deploy/functions/services/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Promise-returning function provided to property where a void return was expected
registerTrigger: noop,
unregisterTrigger: noop,
};
Expand All @@ -71,7 +71,7 @@
api: "storage.googleapis.com",
requiredProjectBindings: obtainStorageBindings,
ensureTriggerRegion: ensureStorageTriggerRegion,
validateTrigger: noop,

Check warning on line 74 in src/deploy/functions/services/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Promise-returning function provided to property where a void return was expected
registerTrigger: noop,
unregisterTrigger: noop,
};
Expand All @@ -82,7 +82,7 @@
api: "firebasealerts.googleapis.com",
requiredProjectBindings: noopProjectBindings,
ensureTriggerRegion: ensureFirebaseAlertsTriggerRegion,
validateTrigger: noop,

Check warning on line 85 in src/deploy/functions/services/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Promise-returning function provided to property where a void return was expected
registerTrigger: noop,
unregisterTrigger: noop,
};
Expand All @@ -97,7 +97,7 @@
api: "firebasedatabase.googleapis.com",
requiredProjectBindings: noopProjectBindings,
ensureTriggerRegion: ensureDatabaseTriggerRegion,
validateTrigger: noop,

Check warning on line 100 in src/deploy/functions/services/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Promise-returning function provided to property where a void return was expected
registerTrigger: noop,
unregisterTrigger: noop,
};
Expand All @@ -108,7 +108,7 @@
api: "firebaseremoteconfig.googleapis.com",
requiredProjectBindings: noopProjectBindings,
ensureTriggerRegion: ensureRemoteConfigTriggerRegion,
validateTrigger: noop,

Check warning on line 111 in src/deploy/functions/services/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Promise-returning function provided to property where a void return was expected
registerTrigger: noop,
unregisterTrigger: noop,
};
Expand All @@ -119,7 +119,7 @@
api: "testing.googleapis.com",
requiredProjectBindings: noopProjectBindings,
ensureTriggerRegion: ensureTestLabTriggerRegion,
validateTrigger: noop,

Check warning on line 122 in src/deploy/functions/services/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Promise-returning function provided to property where a void return was expected
registerTrigger: noop,
unregisterTrigger: noop,
};
Expand All @@ -130,7 +130,7 @@
api: "firestore.googleapis.com",
requiredProjectBindings: noopProjectBindings,
ensureTriggerRegion: ensureFirestoreTriggerRegion,
validateTrigger: noop,

Check warning on line 133 in src/deploy/functions/services/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Promise-returning function provided to property where a void return was expected
registerTrigger: noop,
unregisterTrigger: noop,
};
Expand All @@ -141,7 +141,7 @@
api: "firebasedataconnect.googleapis.com",
requiredProjectBindings: noopProjectBindings,
ensureTriggerRegion: ensureDataConnectTriggerRegion,
validateTrigger: noop,

Check warning on line 144 in src/deploy/functions/services/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Promise-returning function provided to property where a void return was expected
registerTrigger: noop,
unregisterTrigger: noop,
};
Expand Down Expand Up @@ -174,8 +174,8 @@
"google.cloud.firestore.document.v1.updated.withAuthContext": firestoreService,
"google.cloud.firestore.document.v1.deleted.withAuthContext": firestoreService,
"google.firebase.dataconnect.connector.v1.mutationExecuted": dataconnectService,
"firebase.vertexai.v1beta.beforeGenerateContent": aiLogicService,
"firebase.vertexai.v1beta.afterGenerateContent": aiLogicService,
"google.firebase.ailogic.v1.beforeGenerate": aiLogicService,
"google.firebase.ailogic.v1.afterGenerate": aiLogicService,
};

/**
Expand Down
18 changes: 18 additions & 0 deletions src/deploy/functions/validate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,24 @@ describe("validate", () => {
expect(() => validate.endpointsAreValid(want)).to.not.throw();
});

it("disallows unrecognized blocking trigger types", () => {
const ep: backend.Endpoint = {
platform: "gcfv2",
id: "id",
region: "us-east1",
project: "project",
entryPoint: "func",
runtime: "nodejs16",
blockingTrigger: {
eventType: "google.firebase.ailogic.v1.invalidEvent",
},
};

expect(() => validate.endpointsAreValid(backend.of(ep))).to.throw(
/Unrecognized blocking trigger type: google.firebase.ailogic.v1.invalidEvent. Please update your CLI/,
);
});

it("errors for scheduled functions with timeout > 1800s", () => {
const ep: backend.Endpoint = {
...ENDPOINT_BASE,
Expand Down
11 changes: 10 additions & 1 deletion src/deploy/functions/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,16 @@ export function endpointsAreValid(wantBackend: backend.Backend): void {
validateTimeoutConfig(endpoints);
for (const ep of endpoints) {
validateScheduledTimeout(ep);
serviceForEndpoint(ep).validateTrigger(ep, wantBackend);
const service = serviceForEndpoint(ep);
if (backend.isBlockingTriggered(ep)) {
if (service.name === "noop") {
throw new FirebaseError(
`Unrecognized blocking trigger type: ${ep.blockingTrigger.eventType}. Please update your CLI with ${clc.bold("npm install -g firebase-tools@latest")}.`,
{ exit: 1 },
);
}
}
service.validateTrigger(ep, wantBackend);
}

// Our SDK doesn't let people articulate this, but it's theoretically possible in the manifest syntax.
Expand Down
16 changes: 15 additions & 1 deletion src/functions/events/v2.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { AI_LOGIC_EVENTS } from "../../deploy/functions/services/ailogic";
import {
AI_LOGIC_EVENTS,
AI_LOGIC_BEFORE_GENERATE_CONTENT,
AI_LOGIC_AFTER_GENERATE_CONTENT,
} from "../../deploy/functions/services/ailogic";

export const PUBSUB_PUBLISH_EVENT = "google.cloud.pubsub.topic.v1.messagePublished";

Expand Down Expand Up @@ -62,3 +66,13 @@ export const CONVERTABLE_EVENTS: Partial<Record<Event, Event>> = {
"google.cloud.firestore.document.v1.written":
"google.cloud.firestore.document.v1.written.withAuthContext",
};

export const AI_LOGIC_EVENTS_TO_TRIGGER = {
[AI_LOGIC_BEFORE_GENERATE_CONTENT]: "before-generate-content",
[AI_LOGIC_AFTER_GENERATE_CONTENT]: "after-generate-content",
} as const;

export const AI_LOGIC_TRIGGERS_TO_EVENTS = {
"before-generate-content": AI_LOGIC_BEFORE_GENERATE_CONTENT,
"after-generate-content": AI_LOGIC_AFTER_GENERATE_CONTENT,
} as const;
10 changes: 5 additions & 5 deletions src/gcp/ailogic.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Client } from "../apiv2";
import { aiLogicProxyOrigin } from "../api";
import { DeepOmit } from "../metaprogramming";
import {
AI_LOGIC_BEFORE_GENERATE_CONTENT,
AI_LOGIC_AFTER_GENERATE_CONTENT,
type AILogicEndpoint,
} from "../deploy/functions/services/ailogic";
import type { AILogicEndpoint } from "../deploy/functions/services/ailogic";

export const API_VERSION = "v1beta";

export const AI_LOGIC_BEFORE_GENERATE_CONTENT =
"google.firebase.ailogic.v1.beforeGenerate" as const;
export const AI_LOGIC_AFTER_GENERATE_CONTENT = "google.firebase.ailogic.v1.afterGenerate" as const;

export const AI_LOGIC_EVENTS_TO_TRIGGER = {
[AI_LOGIC_BEFORE_GENERATE_CONTENT]: "before-generate-content",
[AI_LOGIC_AFTER_GENERATE_CONTENT]: "after-generate-content",
Expand Down
Loading