Skip to content

Commit 89c6a56

Browse files
committed
feat(genie): default spaces config from DATABRICKS_GENIE_SPACE_ID env var
Make the `spaces` config optional. When omitted, fall back to { default: DATABRICKS_GENIE_SPACE_ID }. If the env var is also unset, routes gracefully 404 instead of crashing at startup. Signed-off-by: Jorge Calvar <jorge.calvar@databricks.com>
1 parent 26b9ec3 commit 89c6a56

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

packages/appkit/src/plugins/genie/genie.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,15 @@ export class GeniePlugin extends Plugin {
7171

7272
constructor(config: IGenieConfig) {
7373
super(config);
74-
this.config = config;
74+
this.config = {
75+
...config,
76+
spaces: config.spaces ?? this.defaultSpaces(),
77+
};
78+
}
79+
80+
private defaultSpaces(): Record<string, string> {
81+
const spaceId = process.env.DATABRICKS_GENIE_SPACE_ID;
82+
return spaceId ? { default: spaceId } : {};
7583
}
7684

7785
private resolveSpaceId(alias: string): string | null {

packages/appkit/src/plugins/genie/tests/genie.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,67 @@ describe("Genie Plugin", () => {
921921
});
922922
});
923923

924+
describe("default spaces from DATABRICKS_GENIE_SPACE_ID", () => {
925+
test("should use env var as default space when spaces is omitted", async () => {
926+
process.env.DATABRICKS_GENIE_SPACE_ID = "env-space-id";
927+
928+
const plugin = new GeniePlugin({ timeout: 5000 });
929+
const { router, getHandler } = createMockRouter();
930+
931+
plugin.injectRoutes(router);
932+
933+
const handler = getHandler("POST", "/:alias/messages");
934+
const mockReq = createMockRequest({
935+
params: { alias: "default" },
936+
body: { content: "test question" },
937+
headers: {
938+
"x-forwarded-access-token": "user-token",
939+
"x-forwarded-user": "user-1",
940+
},
941+
});
942+
const mockRes = createMockResponse();
943+
944+
await handler(mockReq, mockRes);
945+
946+
expect(mockRes.status).not.toHaveBeenCalledWith(404);
947+
expect(mockGenieService.startConversation).toHaveBeenCalledWith(
948+
expect.objectContaining({
949+
space_id: "env-space-id",
950+
content: "test question",
951+
}),
952+
);
953+
954+
delete process.env.DATABRICKS_GENIE_SPACE_ID;
955+
});
956+
957+
test("should 404 for any alias when spaces is omitted and env var is unset", async () => {
958+
delete process.env.DATABRICKS_GENIE_SPACE_ID;
959+
960+
const plugin = new GeniePlugin({ timeout: 5000 });
961+
const { router, getHandler } = createMockRouter();
962+
963+
plugin.injectRoutes(router);
964+
965+
const handler = getHandler("POST", "/:alias/messages");
966+
const mockReq = createMockRequest({
967+
params: { alias: "default" },
968+
body: { content: "test question" },
969+
headers: {
970+
"x-forwarded-access-token": "user-token",
971+
"x-forwarded-user": "user-1",
972+
},
973+
});
974+
const mockRes = createMockResponse();
975+
976+
await handler(mockReq, mockRes);
977+
978+
expect(mockRes.status).toHaveBeenCalledWith(404);
979+
expect(mockRes.json).toHaveBeenCalledWith({
980+
error: "Unknown space alias: default",
981+
});
982+
});
983+
});
984+
924985
describe("SSE reconnection streamId", () => {
925986
let executeStreamSpy: ReturnType<typeof vi.spyOn>;
926987

packages/appkit/src/plugins/genie/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { BasePluginConfig } from "shared";
22

33
export interface IGenieConfig extends BasePluginConfig {
4-
/** Map of alias → Genie Space ID */
5-
spaces: Record<string, string>;
4+
/** Map of alias → Genie Space ID. Defaults to { default: DATABRICKS_GENIE_SPACE_ID } if omitted. */
5+
spaces?: Record<string, string>;
66
/** Genie polling timeout in ms. Set to 0 for indefinite. Default: 120000 (2 min) */
77
timeout?: number;
88
}

0 commit comments

Comments
 (0)