Skip to content

Commit

Permalink
Merge pull request #3258 from JoaquinGimenez1/main
Browse files Browse the repository at this point in the history
Add http and internal codes to AI Binding
  • Loading branch information
danlapid authored Dec 20, 2024
2 parents 03401f0 + f499e73 commit ea52c9a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/cloudflare/internal/ai-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export class Ai {
private options: AiOptions = {};
public lastRequestId: string | null = null;
public aiGatewayLogId: string | null = null;
public lastRequestHttpStatusCode: number | null = null;
public lastRequestInternalStatusCode: number | null = null;

public constructor(fetcher: Fetcher) {
this.fetcher = fetcher;
Expand Down Expand Up @@ -101,6 +103,7 @@ export class Ai {

this.lastRequestId = res.headers.get('cf-ai-req-id');
this.aiGatewayLogId = res.headers.get('cf-aig-log-id');
this.lastRequestHttpStatusCode = res.status;

if (inputs['stream']) {
if (!res.ok) {
Expand Down Expand Up @@ -135,6 +138,7 @@ export class Ai {

try {
const parsedContent = JSON.parse(content) as AiError;
this.lastRequestInternalStatusCode = parsedContent.internalCode;
return new InferenceUpstreamError(
`${parsedContent.internalCode}: ${parsedContent.description}`,
parsedContent.name
Expand Down
5 changes: 5 additions & 0 deletions src/cloudflare/internal/test/ai/ai-api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export const tests = {
env.ai.lastRequestId,
'3a1983d7-1ddd-453a-ab75-c4358c91b582'
);
// Test request http status code is present
assert.deepStrictEqual(env.ai.lastRequestHttpStatusCode, 200);
}

{
Expand Down Expand Up @@ -55,6 +57,9 @@ export const tests = {
message: '1001: prompt and messages are mutually exclusive',
}
);
// Test request internal status code is present
assert.deepEqual;
assert.deepStrictEqual(env.ai.lastRequestInternalStatusCode, 1001);
}
}

Expand Down

0 comments on commit ea52c9a

Please sign in to comment.