@rudderhq/db 0.4.6-canary.7 → 0.4.6-canary.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/migrations/0101_bizarre_morlun.sql +111 -0
- package/dist/migrations/meta/0101_snapshot.json +18860 -0
- package/dist/migrations/meta/_journal.json +7 -0
- package/dist/schema/heartbeat_runs.d.ts +19 -0
- package/dist/schema/heartbeat_runs.d.ts.map +1 -1
- package/dist/schema/heartbeat_runs.js +2 -0
- package/dist/schema/heartbeat_runs.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
ALTER TABLE "heartbeat_runs" ADD COLUMN "result_summary_json" jsonb;--> statement-breakpoint
|
|
2
|
+
CREATE FUNCTION "rudder_backfill_run_result_summary"("input" jsonb)
|
|
3
|
+
RETURNS jsonb
|
|
4
|
+
LANGUAGE plpgsql
|
|
5
|
+
AS $$
|
|
6
|
+
DECLARE
|
|
7
|
+
"summary" jsonb;
|
|
8
|
+
"line" text;
|
|
9
|
+
"event" jsonb;
|
|
10
|
+
"event_type" text;
|
|
11
|
+
"candidate" text;
|
|
12
|
+
"message" jsonb;
|
|
13
|
+
BEGIN
|
|
14
|
+
IF "input" IS NULL OR jsonb_typeof("input") <> 'object' THEN
|
|
15
|
+
RETURN NULL;
|
|
16
|
+
END IF;
|
|
17
|
+
|
|
18
|
+
"summary" := jsonb_strip_nulls(jsonb_build_object(
|
|
19
|
+
'summary', CASE WHEN jsonb_typeof("input" -> 'summary') = 'string' THEN left("input" ->> 'summary', 500) END,
|
|
20
|
+
'result', CASE
|
|
21
|
+
WHEN jsonb_typeof("input" -> 'result') = 'string' THEN left("input" ->> 'result', 500)
|
|
22
|
+
WHEN jsonb_typeof("input" -> 'body') = 'string' THEN left("input" ->> 'body', 500)
|
|
23
|
+
END,
|
|
24
|
+
'message', CASE WHEN jsonb_typeof("input" -> 'message') = 'string' THEN left("input" ->> 'message', 500) END,
|
|
25
|
+
'error', CASE WHEN jsonb_typeof("input" -> 'error') = 'string' THEN left("input" ->> 'error', 500) END,
|
|
26
|
+
'userMessage', CASE WHEN jsonb_typeof("input" -> 'userMessage') = 'string' THEN left("input" ->> 'userMessage', 500) END,
|
|
27
|
+
'total_cost_usd', CASE
|
|
28
|
+
WHEN jsonb_typeof("input" -> 'total_cost_usd') = 'number' THEN "input" -> 'total_cost_usd'
|
|
29
|
+
END,
|
|
30
|
+
'cost_usd', CASE
|
|
31
|
+
WHEN jsonb_typeof("input" -> 'cost_usd') = 'number' THEN "input" -> 'cost_usd'
|
|
32
|
+
END,
|
|
33
|
+
'costUsd', CASE
|
|
34
|
+
WHEN jsonb_typeof("input" -> 'costUsd') = 'number' THEN "input" -> 'costUsd'
|
|
35
|
+
END
|
|
36
|
+
));
|
|
37
|
+
|
|
38
|
+
IF "summary" ?| array['summary', 'result', 'message', 'userMessage']
|
|
39
|
+
OR jsonb_typeof("input" -> 'stdout') <> 'string' THEN
|
|
40
|
+
RETURN NULLIF("summary", '{}'::jsonb);
|
|
41
|
+
END IF;
|
|
42
|
+
|
|
43
|
+
FOR "line" IN SELECT regexp_split_to_table("input" ->> 'stdout', E'\\r?\\n') LOOP
|
|
44
|
+
BEGIN
|
|
45
|
+
"event" := "line"::jsonb;
|
|
46
|
+
EXCEPTION WHEN OTHERS THEN
|
|
47
|
+
CONTINUE;
|
|
48
|
+
END;
|
|
49
|
+
IF jsonb_typeof("event") <> 'object' THEN
|
|
50
|
+
CONTINUE;
|
|
51
|
+
END IF;
|
|
52
|
+
|
|
53
|
+
"event_type" := "event" ->> 'type';
|
|
54
|
+
"candidate" := NULL;
|
|
55
|
+
IF "event_type" = 'result' THEN
|
|
56
|
+
"candidate" := coalesce("event" ->> 'result', "event" ->> 'text', "event" ->> 'response');
|
|
57
|
+
ELSIF "event_type" = 'item.completed' AND "event" #>> '{item,type}' = 'agent_message' THEN
|
|
58
|
+
"candidate" := "event" #>> '{item,text}';
|
|
59
|
+
ELSIF "event_type" IN ('assistant', 'turn_end') THEN
|
|
60
|
+
"message" := "event" -> 'message';
|
|
61
|
+
IF jsonb_typeof("message") = 'string' THEN
|
|
62
|
+
"candidate" := "message" #>> '{}';
|
|
63
|
+
ELSIF jsonb_typeof("message") = 'object' THEN
|
|
64
|
+
"candidate" := coalesce("message" ->> 'text',
|
|
65
|
+
CASE WHEN jsonb_typeof("message" -> 'content') = 'string' THEN "message" ->> 'content' END);
|
|
66
|
+
IF "candidate" IS NULL AND jsonb_typeof("message" -> 'content') = 'array' THEN
|
|
67
|
+
SELECT string_agg("piece", E'\n\n' ORDER BY "ordinality") INTO "candidate"
|
|
68
|
+
FROM (
|
|
69
|
+
SELECT "ordinality", CASE
|
|
70
|
+
WHEN jsonb_typeof("part") = 'string' THEN "part" #>> '{}'
|
|
71
|
+
WHEN jsonb_typeof("part") = 'object' THEN coalesce("part" ->> 'text', "part" ->> 'content')
|
|
72
|
+
END AS "piece"
|
|
73
|
+
FROM jsonb_array_elements("message" -> 'content') WITH ORDINALITY AS "parts"("part", "ordinality")
|
|
74
|
+
) AS "message_parts"
|
|
75
|
+
WHERE "piece" IS NOT NULL AND btrim("piece") <> '';
|
|
76
|
+
END IF;
|
|
77
|
+
END IF;
|
|
78
|
+
ELSIF "event_type" = 'agent_end' AND jsonb_typeof("event" -> 'messages') = 'array' THEN
|
|
79
|
+
SELECT "assistant_message" INTO "message"
|
|
80
|
+
FROM jsonb_array_elements("event" -> 'messages') WITH ORDINALITY AS "messages"("assistant_message", "ordinality")
|
|
81
|
+
WHERE "assistant_message" ->> 'role' = 'assistant'
|
|
82
|
+
ORDER BY "ordinality" DESC
|
|
83
|
+
LIMIT 1;
|
|
84
|
+
IF jsonb_typeof("message" -> 'content') = 'string' THEN
|
|
85
|
+
"candidate" := "message" ->> 'content';
|
|
86
|
+
ELSIF jsonb_typeof("message" -> 'content') = 'array' THEN
|
|
87
|
+
SELECT string_agg("piece", E'\n\n' ORDER BY "ordinality") INTO "candidate"
|
|
88
|
+
FROM (
|
|
89
|
+
SELECT "ordinality", CASE
|
|
90
|
+
WHEN jsonb_typeof("part") = 'string' THEN "part" #>> '{}'
|
|
91
|
+
WHEN jsonb_typeof("part") = 'object' THEN coalesce("part" ->> 'text', "part" ->> 'content')
|
|
92
|
+
END AS "piece"
|
|
93
|
+
FROM jsonb_array_elements("message" -> 'content') WITH ORDINALITY AS "parts"("part", "ordinality")
|
|
94
|
+
) AS "message_parts"
|
|
95
|
+
WHERE "piece" IS NOT NULL AND btrim("piece") <> '';
|
|
96
|
+
END IF;
|
|
97
|
+
END IF;
|
|
98
|
+
|
|
99
|
+
IF "candidate" IS NOT NULL AND btrim("candidate") <> '' THEN
|
|
100
|
+
"summary" := "summary" || jsonb_build_object('result', left(btrim("candidate"), 500));
|
|
101
|
+
END IF;
|
|
102
|
+
END LOOP;
|
|
103
|
+
|
|
104
|
+
RETURN NULLIF("summary", '{}'::jsonb);
|
|
105
|
+
END;
|
|
106
|
+
$$;--> statement-breakpoint
|
|
107
|
+
UPDATE "heartbeat_runs"
|
|
108
|
+
SET "result_summary_json" = "rudder_backfill_run_result_summary"("result_json")
|
|
109
|
+
WHERE "result_json" IS NOT NULL;--> statement-breakpoint
|
|
110
|
+
DROP FUNCTION "rudder_backfill_run_result_summary"(jsonb);--> statement-breakpoint
|
|
111
|
+
CREATE INDEX "heartbeat_runs_org_created_id_idx" ON "heartbeat_runs" USING btree ("org_id","created_at","id");
|