Repository navigation
refactor(ai): unify requests, errors, and prompt context #1073
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2ca56ec
refactor(ai): one agent request path and typed error contract
izadoesdev 18929c4
refactor(ai): one source for prompt rules and untrusted context
izadoesdev a83bd09
fix(ai): authorize session organizations before billing
izadoesdev 4272be4
fix(ai): preserve request identity and answer content
izadoesdev c112938
fix(api): isolate selected agent credentials
izadoesdev 0630a8e
fix(api): type native agent test fixtures
izadoesdev b756ffa
fix(ai): check live membership and avoid fixed pricing
izadoesdev 6102fb0
test(ai): consolidate and type native streaming fixtures
izadoesdev 3a7f001
fix(ai): reject inactive session organizations before billing
izadoesdev 1f291d7
fix(ai): keep multiline values inside markdown table cells
izadoesdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| const state = vi.hoisted(() => ({ | ||
| getSession: vi.fn(), | ||
| resolveApiKey: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@databuddy/auth", () => ({ | ||
| auth: { api: { getSession: state.getSession } }, | ||
| })); | ||
| vi.mock("@databuddy/api-keys/resolve", () => ({ | ||
| isApiKeyPresent: (headers: Headers) => Boolean(headers.get("x-api-key")), | ||
| resolveApiKey: state.resolveApiKey, | ||
| })); | ||
| vi.mock("@databuddy/ai/lib/tracing", () => ({ mergeWideEvent: () => {} })); | ||
|
|
||
| const { applyAuthWideEvent, resolveRequestAuth } = await import( | ||
| "./auth-wide-event" | ||
| ); | ||
| const key = { | ||
| id: "key-synthetic", | ||
| organizationId: "org-key-synthetic", | ||
| prefix: "test", | ||
| type: "user", | ||
| scopes: ["read:data"], | ||
| }; | ||
| const session = { | ||
| user: { id: "user-synthetic", email: "synthetic@example.invalid" }, | ||
| session: { activeOrganizationId: "org-session-synthetic" }, | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| state.getSession.mockReset().mockResolvedValue(session); | ||
| state.resolveApiKey.mockReset().mockResolvedValue({ key, outcome: "ok" }); | ||
| }); | ||
|
|
||
| describe("independent auth resolution", () => { | ||
| it("keeps a valid API key when uncached session resolution fails", async () => { | ||
| state.getSession.mockRejectedValueOnce( | ||
| new Error("Synthetic session-store outage") | ||
| ); | ||
| const headers = new Headers({ "x-api-key": "dbdy_inert_valid" }); | ||
| await expect(resolveRequestAuth(headers)).resolves.toEqual({ | ||
| apiKey: key, | ||
| session: null, | ||
| }); | ||
| expect(state.resolveApiKey).toHaveBeenCalledExactlyOnceWith(headers); | ||
| }); | ||
|
|
||
| it("returns no identity when session resolution fails without a key", async () => { | ||
| state.getSession.mockRejectedValueOnce( | ||
| new Error("Synthetic session-store outage") | ||
| ); | ||
| await expect(resolveRequestAuth(new Headers())).resolves.toEqual({ | ||
| apiKey: null, | ||
| session: null, | ||
| }); | ||
| expect(state.resolveApiKey).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("preserves both successful identities for caller selection", async () => { | ||
| await expect( | ||
| resolveRequestAuth(new Headers({ "x-api-key": "dbdy_inert_valid" })) | ||
| ).resolves.toEqual({ apiKey: key, session }); | ||
| }); | ||
|
|
||
| it("propagates an API-key resolver failure", async () => { | ||
| const failure = new Error("Synthetic key-store outage"); | ||
| state.resolveApiKey.mockRejectedValueOnce(failure); | ||
| await expect( | ||
| resolveRequestAuth(new Headers({ "x-api-key": "dbdy_inert_valid" })) | ||
| ).rejects.toBe(failure); | ||
| }); | ||
|
|
||
| it("reuses telemetry-resolved key auth without another session lookup", async () => { | ||
| state.getSession.mockRejectedValueOnce( | ||
| new Error("Synthetic session-store outage") | ||
| ); | ||
| const headers = new Headers({ "x-api-key": "dbdy_inert_valid" }); | ||
| await applyAuthWideEvent(headers); | ||
| await expect(resolveRequestAuth(headers)).resolves.toEqual({ | ||
| apiKey: key, | ||
| session: null, | ||
| }); | ||
| expect(state.getSession).toHaveBeenCalledTimes(1); | ||
| expect(state.resolveApiKey).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.