Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING CHANGE(js)!: Reconcile authPolicy. add firebase and api key helpers #1743

Merged
merged 52 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
24c8b08
Checkpoint. Need to remove firebase/functions
inlined Jan 31, 2025
b4f16f2
Revert removal of middleware.ts
inlined Feb 5, 2025
473468e
Merge remote-tracking branch 'origin/main' into inlined.auth
inlined Feb 5, 2025
496dede
firebaseAuth sans integration tests
inlined Feb 5, 2025
5a201d9
Merge remote-tracking branch 'origin' into inlined.auth
inlined Feb 5, 2025
0924ab9
API key module
inlined Feb 5, 2025
c6492ec
Update error type to match Callable protocol
inlined Feb 5, 2025
2e0fa7e
Checkpoint. Merging UserFacingError with GenkitError
inlined Feb 5, 2025
d67b9be
Checkpoint. Refactored policy & error. Updated express. Strange errors
inlined Feb 6, 2025
b68d638
Whoops. Missing files
inlined Feb 6, 2025
dd15fa0
Tests passing again! Onto Next.js
inlined Feb 6, 2025
73afde8
Next.js works! Theres' enough testing I think we can GA it
inlined Feb 6, 2025
118bf91
Fix esm stuff; update docs
inlined Feb 6, 2025
347aa88
Remove last reference to onCallFlow
inlined Feb 6, 2025
042bd45
a few fixes for #1743 (#1880)
pavelgj Feb 6, 2025
4be1e3a
Adding missing files; PR feedback
inlined Feb 6, 2025
dad4a0a
docs: vertexai updates (#1841)
ifielker Feb 5, 2025
4f71804
breaking(js/ai): drop ToolDefinition from ToolArgument (and generate …
pavelgj Feb 5, 2025
5e744c9
chore: bump @genkit-ai/tools-common version to @genkit-ai/tools-commo…
gh-action-bump-version Feb 5, 2025
8a36c92
chore: bump genkit-cli version to [email protected]
gh-action-bump-version Feb 5, 2025
413f82b
chore: bump @genkit-ai/telemetry-server version to @genkit-ai/telemet…
gh-action-bump-version Feb 5, 2025
26ca0d8
chore: bump @genkit-ai/core version to @genkit-ai/[email protected]
gh-action-bump-version Feb 5, 2025
f67f4e5
chore: bump @genkit-ai/ai version to @genkit-ai/[email protected]
gh-action-bump-version Feb 5, 2025
73f8610
chore: bump genkit version to [email protected]
gh-action-bump-version Feb 5, 2025
d289a7d
chore: bump genkitx-chromadb version to [email protected]
gh-action-bump-version Feb 5, 2025
4ac6af2
chore: bump @genkit-ai/dev-local-vectorstore version to @genkit-ai/de…
gh-action-bump-version Feb 5, 2025
306c9a3
chore: bump @genkit-ai/evaluator version to @genkit-ai/[email protected].…
gh-action-bump-version Feb 5, 2025
1631611
chore: bump @genkit-ai/firebase version to @genkit-ai/[email protected]
gh-action-bump-version Feb 5, 2025
74e6b29
chore: bump @genkit-ai/google-cloud version to @genkit-ai/google-clou…
gh-action-bump-version Feb 5, 2025
e08bc1f
chore: bump @genkit-ai/googleai version to @genkit-ai/[email protected]
gh-action-bump-version Feb 5, 2025
c02a8bc
chore: bump genkitx-langchain version to [email protected]
gh-action-bump-version Feb 5, 2025
79f233a
chore: bump genkitx-ollama version to [email protected]
gh-action-bump-version Feb 5, 2025
a75fe7c
chore: bump genkitx-pinecone version to [email protected]
gh-action-bump-version Feb 5, 2025
9298eee
chore: bump @genkit-ai/vertexai version to @genkit-ai/[email protected]
gh-action-bump-version Feb 5, 2025
11663a2
chore: bump @genkit-ai/checks version to @genkit-ai/[email protected]
gh-action-bump-version Feb 5, 2025
7286715
chore: bump genkitx-mcp version to [email protected]
gh-action-bump-version Feb 5, 2025
a2cb532
chore: bump @genkit-ai/express version to @genkit-ai/[email protected]
gh-action-bump-version Feb 5, 2025
d364a19
chore: added next plugin to release script
pavelgj Feb 5, 2025
163ca84
docs: Restructuring of codelab-chat-with-a-pdf.md (#1836)
thedmail Feb 5, 2025
5c7ba1f
docs(js): Adds doc for context. (#1843)
mbleigh Feb 5, 2025
398e1b3
feat: support model validation (#1868)
ssbushi Feb 5, 2025
9552866
docs: 2nd tranche of docs updates for 6 Feb GA release (#1865)
thedmail Feb 5, 2025
a08c69a
fix(js): stream tool message generated by interrupt resume (#1870)
mbleigh Feb 5, 2025
2dcf527
feature(go): add support for multiple model versions (#1575)
hugoaguirre Feb 5, 2025
c3b91ab
docs: Third tranche of docs updates (#1871)
thedmail Feb 6, 2025
39a17bb
feat(js/ai): allow explicit constrained gen option, allow simulation …
pavelgj Feb 6, 2025
961feae
fix: update-samples-to-v17 (#1867)
i2amsam Feb 6, 2025
55e10ab
fix: update js-chatbot to work in IDX preview (#1874)
i2amsam Feb 6, 2025
652a5a6
fix: use correct idx link in js api readme (#1877)
tonybaroneee Feb 6, 2025
72f3ad0
fix: minor fixes in ollama plugin doc (#1875)
shrutip90 Feb 6, 2025
0f2d3f7
PR feedback
inlined Feb 6, 2025
9a07808
PR feedback & merge conflicts. Testing async
inlined Feb 6, 2025
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
27 changes: 27 additions & 0 deletions js/core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,30 @@ export function getContext(
registry = registry as Registry;
return registry.asyncStore.getStore<ActionContext>(contextAlsKey);
}

/**
* A universal type that request handling extensions (e.g. express, next) can map their request to.
* This allows middleware to build consistent interfacese on any web framework.
* Headers must be lowercase to ensure portability.
*/
export interface RequestData<T = any> {
method: 'GET' | 'PUT' | 'POST' | 'DELETE' | 'OPTIONS' | 'QUERY';
headers: Record<string, string>;
input: T;
}

/**
* Middleware can read request data and add information to the context that will
* be passed to the Action. If middleware throws an error, that error will fail
* the request and the Action will not be invoked. Expected cases should return a
* UserFacingError, which allows the request handler to know what data is safe to
* return to end users.
*
* Middleware can provide validation in addition to parsing. For example, an auth
* middleware can have policies for validating auth in addition to passing auth context
* to the Action.
*/
export type ContextProvider<
C extends ActionContext = ActionContext,
T = any,
> = (request: RequestData<T>) => C | Promise<C>;
56 changes: 55 additions & 1 deletion js/core/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@
*/

import { Registry } from './registry.js';
import { StatusName } from './statusTypes.js';
import { httpStatusCode, StatusName } from './statusTypes.js';

interface HttpErrorWireFormat {
details?: unknown;
message: string;
status: StatusName;
}

/**
* Base error class for Genkit errors.
Expand All @@ -24,6 +30,7 @@ export class GenkitError extends Error {
source?: string;
status: StatusName;
detail?: any;
code: number;

constructor({
status,
Expand All @@ -37,10 +44,24 @@ export class GenkitError extends Error {
source?: string;
}) {
super(`${source ? `${source}: ` : ''}${status}: ${message}`);
this.code = httpStatusCode(status);
this.status = status;
this.detail = detail;
this.name = 'GenkitError';
}

/**
* Returns a JSON-serializable representation of this object.
*/
public toJSON(): HttpErrorWireFormat {
return {
// This error type is used by 3P authors with the field "detail",
// but the actual Callable protocol value is "details"
...(this.detail === undefined ? {} : { details: this.detail }),
status: this.status,
message: this.message,
};
}
}

export class UnstableApiError extends GenkitError {
Expand Down Expand Up @@ -69,6 +90,39 @@ export function assertUnstable(
}
}

/**
* Creates a new class of Error for issues to be returned to users.
* Using this error allows a web framework handler (e.g. express, next) to know it
* is safe to return the message in a request. Other kinds of errors will
* result in a generic 500 message to avoid the possibility of internal
* exceptions being leaked to attackers.
* In JSON requests, code will be an HTTP code and error will be a response body.
* In streaming requests, { code, message } will be passed as the error message.
*/
export class UserFacingError extends GenkitError {
constructor(status: StatusName, message: string, details?: any) {
super({ status, detail: details, message });
super.name = 'UserFacingError';
}
}

export function getHttpStatus(e: any): number {
if (e instanceof GenkitError) {
return e.code;
}
return 500;
}

export function getCallableJSON(e: any): HttpErrorWireFormat {
if (e instanceof GenkitError) {
return e.toJSON();
}
return {
message: 'Internal Error',
status: 'INTERNAL',
};
}

/**
* Extracts error message from the given error object, or if input is not an error then just turn the error into a string.
*/
Expand Down
17 changes: 15 additions & 2 deletions js/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,21 @@ export const GENKIT_REFLECTION_API_SPEC_VERSION = 1;

export { z } from 'zod';
export * from './action.js';
export { getContext, runWithContext, type ActionContext } from './context.js';
export { GenkitError, UnstableApiError, assertUnstable } from './error.js';
export {
getContext,
runWithContext,
type ActionContext,
type ContextProvider,
type RequestData,
} from './context.js';
export {
GenkitError,
UnstableApiError,
UserFacingError,
assertUnstable,
getCallableJSON,
getHttpStatus,
} from './error.js';
export {
defineFlow,
run,
Expand Down
28 changes: 28 additions & 0 deletions js/core/src/statusTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export enum StatusCodes {
}

export const StatusNameSchema = z.enum([
'OK',
'CANCELLED',
'UNKNOWN',
'INVALID_ARGUMENT',
Expand All @@ -195,6 +196,33 @@ export const StatusNameSchema = z.enum([
]);
export type StatusName = z.infer<typeof StatusNameSchema>;

const statusCodeMap: Record<StatusName, number> = {
OK: 200,
CANCELLED: 499,
UNKNOWN: 500,
INVALID_ARGUMENT: 400,
DEADLINE_EXCEEDED: 504,
NOT_FOUND: 404,
ALREADY_EXISTS: 409,
PERMISSION_DENIED: 403,
UNAUTHENTICATED: 401,
RESOURCE_EXHAUSTED: 429,
FAILED_PRECONDITION: 400,
ABORTED: 409,
OUT_OF_RANGE: 400,
UNIMPLEMENTED: 501,
INTERNAL: 500,
UNAVAILABLE: 503,
DATA_LOSS: 500,
};

export function httpStatusCode(status: StatusName): number {
if (!(status in statusCodeMap)) {
throw new Error(`Invalid status code ${status}`);
}
return statusCodeMap[status];
}

const StatusCodesSchema = z.nativeEnum(StatusCodes);

// If changing below
Expand Down
9 changes: 9 additions & 0 deletions js/genkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@
"require": "./lib/client/index.js",
"import": "./lib/client/index.mjs",
"default": "./lib/client/index.js"
},
"./authPolicy": {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We talked about this being context, right?

Suggested change
"./authPolicy": {
"./context": {

"types": "./lib/authPolicy.d.ts",
pavelgj marked this conversation as resolved.
Show resolved Hide resolved
"require": "./lib/authPolicy.js",
"import": "./lib/authPolicy.mjs",
"default": "./lib/authPolicy.js"
}
},
"typesVersions": {
Expand Down Expand Up @@ -216,6 +222,9 @@
],
"beta/client": [
"lib/client/index"
],
"authPolicy": [
inlined marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"authPolicy": [
"context": [

"lib/authPolicy"
]
}
}
Expand Down
2 changes: 2 additions & 0 deletions js/genkit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export {
ReflectionServer,
StatusCodes,
StatusSchema,
UserFacingError,
defineJsonSchema,
defineSchema,
getCurrentEnv,
Expand All @@ -130,6 +131,7 @@ export {
type Action,
type ActionContext,
type ActionMetadata,
type ContextProvider,
type Flow,
type FlowConfig,
type FlowFn,
Expand Down
1 change: 1 addition & 0 deletions js/plugins/api-key/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
Loading
Loading