Skip to content

Commit

Permalink
fix: tools calls string crashing app
Browse files Browse the repository at this point in the history
  • Loading branch information
vincelwt committed Mar 31, 2024
1 parent 43ca09c commit 51e8c26
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 181 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"bcrypt": "^5.1.1",
"jose": "^5.2.0",
"js-tiktoken": "1.0.7",
"jsonrepair": "^3.6.0",
"koa": "^2.15.0",
"koa-bodyparser": "^4.4.1",
"koa-logger": "^3.2.1",
Expand Down
18 changes: 17 additions & 1 deletion packages/backend/src/api/v1/runs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { fileExport } from "./export"
import { deserializeLogic } from "shared"
import { convertChecksToSQL } from "@/src/utils/checks"
import { checkAccess } from "@/src/utils/authorization"
import { jsonrepair } from "jsonrepair"

const runs = new Router({
prefix: "/runs",
Expand Down Expand Up @@ -55,6 +56,20 @@ function processOutput(output: unknown) {
return output
}

function processParams(params: any) {
if (!params) return {}
try {
// handles tools received as string (eg. litellm)
if (params.tools && typeof params.tools === "string") {
params.tools = JSON.parse(jsonrepair(params.tools))
}
} catch (e) {
console.error(e)
console.error("Error parsing tools")
}
return params
}

const formatRun = (run: any) => ({
id: run.id,
isPublic: run.isPublic,
Expand All @@ -79,7 +94,8 @@ const formatRun = (run: any) => ({
error: run.error,
status: run.status,
siblingRunId: run.siblingRunId,
params: run.params,
params: processParams(run.params),

metadata: run.metadata,
user: {
id: run.externalUserId,
Expand Down
5 changes: 3 additions & 2 deletions packages/backend/src/api/v1/runs/ingest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const registerRunEvent = async (
name,
tokensUsage,
extra,
params,
error,
feedback,
metadata,
Expand Down Expand Up @@ -121,8 +122,8 @@ const registerRunEvent = async (
tags,
name,
status: "started",
params: extra,
metadata: metadata,
params: params || extra,
metadata,
templateVersionId: templateId,
parentRunId: parentRunIdToUse,
input,
Expand Down
18 changes: 5 additions & 13 deletions packages/backend/src/utils/ingest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export const ensureIsUUID = async (id: string): Promise<string | undefined> => {
}

// Converts snake_case to camelCase
// I found some (probably unintended) camelCase props in the tracer events, so normalize everything
// I found some (probably unintended) snake_case props in the tracer events, so normalize everything
const recursiveToCamel = (item: any): any => {
if (Array.isArray(item)) {
return item.map((el: unknown) => recursiveToCamel(el))
Expand All @@ -102,25 +102,17 @@ const recursiveToCamel = (item: any): any => {
function cleanMetadata(object: any) {
if (!object) return undefined

const validTypes = ["string", "number", "boolean"]

return Object.fromEntries(
Object.entries(object).map(([key, value]) => {
if (
typeof value === "string" ||
typeof value === "number" ||
typeof value === "boolean"
) {
if (validTypes.includes(typeof value)) {
return [key, value]
}
if (Array.isArray(value)) {
return [
key,
value.map((v) =>
typeof v === "string" ||
typeof v === "number" ||
typeof v === "boolean"
? v
: null,
),
value.map((v) => (validTypes.includes(typeof value) ? v : null)),
]
}
return [key, null]
Expand Down
Loading

0 comments on commit 51e8c26

Please sign in to comment.