|
| 1 | +import { app, HttpRequest, HttpResponseInit, InvocationContext } from "@azure/functions"; |
| 2 | +import { TableClient } from "@azure/data-tables"; |
| 3 | +import { AzureCliCredential, ManagedIdentityCredential } from "@azure/identity"; |
| 4 | +import { logRequestIdentity } from "../requestIdentity"; |
| 5 | + |
| 6 | +const STORAGE_ACCOUNT_NAME = process.env.STORAGE_ACCOUNT_NAME; |
| 7 | +const TOOL_USAGE_TABLE_NAME = process.env.TOOL_USAGE_TABLE_NAME; |
| 8 | + |
| 9 | +function getToolUsageTableClient(): TableClient { |
| 10 | + if (!STORAGE_ACCOUNT_NAME) { |
| 11 | + throw new Error("STORAGE_ACCOUNT_NAME environment variable is not set"); |
| 12 | + } |
| 13 | + if (!TOOL_USAGE_TABLE_NAME) { |
| 14 | + throw new Error("TOOL_USAGE_TABLE_NAME environment variable is not set"); |
| 15 | + } |
| 16 | + const clientId = process.env.AZURE_CLIENT_ID; |
| 17 | + const isDevEnvironment = process.env.AZURE_FUNCTIONS_ENVIRONMENT === "Development"; |
| 18 | + const credential = isDevEnvironment ? new AzureCliCredential() : new ManagedIdentityCredential(clientId!); |
| 19 | + return new TableClient( |
| 20 | + `https://${STORAGE_ACCOUNT_NAME}.table.core.windows.net`, |
| 21 | + TOOL_USAGE_TABLE_NAME, |
| 22 | + credential |
| 23 | + ); |
| 24 | +} |
| 25 | + |
| 26 | +/** Escape a value for use inside an OData string literal (single quotes are doubled). */ |
| 27 | +function odataLiteral(value: string): string { |
| 28 | + return value.replace(/'/g, "''"); |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Build the OData filter for tool-usage queries from optional equality filters. |
| 33 | + * Returns undefined when no filters are provided. |
| 34 | + */ |
| 35 | +export function buildToolUsageFilter(filters: { |
| 36 | + skill?: string; |
| 37 | + test?: string; |
| 38 | + branch?: string; |
| 39 | + runId?: string; |
| 40 | + runToken?: string; |
| 41 | + runDate?: string; |
| 42 | +}): string | undefined { |
| 43 | + const clauses: string[] = []; |
| 44 | + if (filters.skill) clauses.push(`skill eq '${odataLiteral(filters.skill)}'`); |
| 45 | + if (filters.test) clauses.push(`testName eq '${odataLiteral(filters.test)}'`); |
| 46 | + if (filters.branch) clauses.push(`branch eq '${odataLiteral(filters.branch)}'`); |
| 47 | + if (filters.runId) clauses.push(`runId eq '${odataLiteral(filters.runId)}'`); |
| 48 | + if (filters.runToken) clauses.push(`runToken eq '${odataLiteral(filters.runToken)}'`); |
| 49 | + if (filters.runDate) clauses.push(`runDate eq '${odataLiteral(filters.runDate)}'`); |
| 50 | + return clauses.length > 0 ? clauses.join(" and ") : undefined; |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Returns integration-test tool usage rows from the table. |
| 55 | + * GET /api/tool-usage |
| 56 | + * Query params: skill (optional), test (optional), branch (optional), |
| 57 | + * runId (optional), runToken (optional), runDate (optional) |
| 58 | + * |
| 59 | + * Each row represents a single tool call in one run. Full tool arguments are not |
| 60 | + * stored here — they live in the per-run blob and are fetched on demand. |
| 61 | + */ |
| 62 | +async function getToolUsage(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> { |
| 63 | + logRequestIdentity(request, context, "getToolUsage"); |
| 64 | + |
| 65 | + const filter = buildToolUsageFilter({ |
| 66 | + skill: request.query.get("skill") || undefined, |
| 67 | + test: request.query.get("test") || undefined, |
| 68 | + branch: request.query.get("branch") || undefined, |
| 69 | + runId: request.query.get("runId") || undefined, |
| 70 | + runToken: request.query.get("runToken") || undefined, |
| 71 | + runDate: request.query.get("runDate") || undefined, |
| 72 | + }); |
| 73 | + |
| 74 | + // Require at least one filter. An unfiltered scan of the one-row-per-tool-call |
| 75 | + // table can be very large and risks timeouts / excessive storage reads. |
| 76 | + if (!filter) { |
| 77 | + return { |
| 78 | + status: 400, |
| 79 | + headers: { "Content-Type": "application/json" }, |
| 80 | + body: JSON.stringify({ |
| 81 | + error: "At least one filter is required: skill, test, branch, runId, runToken, or runDate.", |
| 82 | + }), |
| 83 | + }; |
| 84 | + } |
| 85 | + |
| 86 | + try { |
| 87 | + const tableClient = getToolUsageTableClient(); |
| 88 | + const listOptions = { queryOptions: { filter } }; |
| 89 | + const entities: Record<string, unknown>[] = []; |
| 90 | + |
| 91 | + for await (const entity of tableClient.listEntities(listOptions)) { |
| 92 | + entities.push({ |
| 93 | + skill: entity.skill, |
| 94 | + testName: entity.testName, |
| 95 | + branch: entity.branch, |
| 96 | + runId: entity.runId, |
| 97 | + runDate: entity.runDate, |
| 98 | + runTimestamp: entity.runTimestamp, |
| 99 | + runToken: entity.runToken, |
| 100 | + reportFile: entity.reportFile, |
| 101 | + sessionId: entity.sessionId, |
| 102 | + model: entity.model, |
| 103 | + order: entity.order, |
| 104 | + toolName: entity.toolName, |
| 105 | + toolCallId: entity.toolCallId, |
| 106 | + successState: entity.successState, |
| 107 | + durationMs: entity.durationMs, |
| 108 | + outputBytes: entity.outputBytes, |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + return { |
| 113 | + status: 200, |
| 114 | + headers: { "Content-Type": "application/json" }, |
| 115 | + body: JSON.stringify(entities), |
| 116 | + }; |
| 117 | + } catch (err: any) { |
| 118 | + context.error("Error querying tool usage:", err?.message ?? err); |
| 119 | + return { |
| 120 | + status: 500, |
| 121 | + headers: { "Content-Type": "application/json" }, |
| 122 | + body: JSON.stringify({ error: "Failed to query tool usage" }), |
| 123 | + }; |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +app.http("getToolUsage", { |
| 128 | + methods: ["GET"], |
| 129 | + authLevel: "anonymous", |
| 130 | + route: "tool-usage", |
| 131 | + handler: getToolUsage, |
| 132 | +}); |
0 commit comments