@cartanova/qgrid-cli 2.14.1 → 2.15.2
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/bundle/dist/application/qgrid/qgrid.frame.js +3 -2
- package/bundle/dist/application/request-log/request-log-cost.js +59 -0
- package/bundle/dist/application/request-log/request-log.model.js +55 -85
- package/bundle/dist/utils/providers/common/model-cost.js +10 -1
- package/bundle/dist/utils/providers/openai/openai-dispatcher.js +3 -2
- package/bundle/src/application/request-log/request-log-cost.test.ts +40 -0
- package/bundle/src/application/request-log/request-log-cost.ts +103 -0
- package/bundle/src/application/request-log/request-log.model.test.ts +121 -123
- package/bundle/src/application/request-log/request-log.model.ts +70 -167
- package/bundle/src/utils/providers/common/effort.test.ts +1 -0
- package/bundle/src/utils/providers/common/model-cost.test.ts +5 -0
- package/bundle/src/utils/providers/common/model-cost.ts +12 -0
- package/bundle/src/utils/providers/openai/openai-dispatcher.test.ts +7 -6
- package/bundle/src/utils/providers/openai/openai-dispatcher.ts +3 -1
- package/bundle/web-dist/client/assets/{index-jExDfi8j.js → index-VhUpqKIg.js} +3 -3
- package/bundle/web-dist/client/assets/logs-BwSzVf_A.js +1 -0
- package/bundle/web-dist/client/assets/{monit-CGTRk2U0.js → monit-B2CVdIml.js} +1 -1
- package/bundle/web-dist/client/assets/{routes-D9l20OWa.js → routes-BOOXpy3Q.js} +1 -1
- package/bundle/web-dist/client/assets/{settings-D2zIv0xR.js → settings-B_a3Nr0H.js} +1 -1
- package/bundle/web-dist/client/assets/{show-DefwCScC.js → show-D5Uxcb4a.js} +1 -1
- package/bundle/web-dist/client/assets/{tokens-CbmIxMZ9.js → tokens-zlrqnP63.js} +1 -1
- package/bundle/web-dist/client/assets/{use-oauth-login-flow-CEufhZcR.js → use-oauth-login-flow-qR-56G2w.js} +1 -1
- package/bundle/web-dist/client/assets/{use-update-token-mutation-E5ey27W6.js → use-update-token-mutation-B-vcLRx-.js} +1 -1
- package/bundle/web-dist/client/index.html +1 -1
- package/bundle/web-dist/server/assets/{logs-C0md5rmp.js → logs-6289J29J.js} +17 -4
- package/bundle/web-dist/server/entry-server.generated.js +3 -2
- package/package.json +1 -1
- package/skills/qgrid/references/ai-sdk-provider-contract.md +1 -1
- package/skills/qgrid/references/docs-workflow.md +10 -0
- package/skills/qgrid/references/openai-codex-runtime.md +22 -5
- package/skills/qgrid/references/sonamu-api-web-flow.md +1 -1
- package/bundle/web-dist/client/assets/logs-ol_IGjcT.js +0 -1
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* request log 비용 계산 규칙. Sonamu 에 의존하지 않아 모델과 운영 백필 스크립트가 같이 쓴다.
|
|
3
|
+
*/
|
|
4
|
+
import { calculateCostUsd } from "../../utils/providers/common/model-cost";
|
|
5
|
+
|
|
6
|
+
// cost_usd는 정수 micro-USD로 저장. 실제 USD = cost_usd / MICRO_USD.
|
|
7
|
+
export const MICRO_USD = 1_000_000;
|
|
8
|
+
|
|
9
|
+
export type RequestLogUsageRow = {
|
|
10
|
+
token_name?: string | null;
|
|
11
|
+
model_name?: string | null;
|
|
12
|
+
input_tokens: number;
|
|
13
|
+
output_tokens: number;
|
|
14
|
+
cache_read_tokens: number;
|
|
15
|
+
cache_creation_tokens: number;
|
|
16
|
+
cache_creation_5m_tokens?: number | null;
|
|
17
|
+
cache_creation_1h_tokens?: number | null;
|
|
18
|
+
cost_usd?: number | null;
|
|
19
|
+
cost_source?: string | null;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
function isAnthropicUsageRow(row: Pick<RequestLogUsageRow, "token_name" | "model_name">): boolean {
|
|
23
|
+
return (
|
|
24
|
+
row.token_name?.startsWith("anthropic/") === true ||
|
|
25
|
+
row.model_name?.startsWith("claude-") === true ||
|
|
26
|
+
row.model_name?.startsWith("anthropic/claude-") === true
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function canonicalModelName(modelName: string): string {
|
|
31
|
+
return modelName.includes("/") ? modelName.split("/").pop()! : modelName;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface UsageForCost {
|
|
35
|
+
model: string | null;
|
|
36
|
+
inputTokens: number;
|
|
37
|
+
outputTokens: number;
|
|
38
|
+
cachedInputTokens: number;
|
|
39
|
+
cacheCreationInputTokens: number;
|
|
40
|
+
cacheCreationInputTokens5m?: number;
|
|
41
|
+
cacheCreationInputTokens1h?: number;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function normalizedUsageForCost(row: RequestLogUsageRow): UsageForCost {
|
|
45
|
+
const cacheRead = row.cache_read_tokens;
|
|
46
|
+
const cacheCreation = row.cache_creation_tokens;
|
|
47
|
+
const storedInput = row.input_tokens;
|
|
48
|
+
|
|
49
|
+
const legacyAnthropicSplitInput =
|
|
50
|
+
isAnthropicUsageRow(row) && storedInput < cacheRead + cacheCreation;
|
|
51
|
+
|
|
52
|
+
const usage: UsageForCost = {
|
|
53
|
+
model: row.model_name ? canonicalModelName(row.model_name) : null,
|
|
54
|
+
inputTokens: legacyAnthropicSplitInput ? storedInput + cacheRead + cacheCreation : storedInput,
|
|
55
|
+
outputTokens: row.output_tokens,
|
|
56
|
+
cachedInputTokens: cacheRead,
|
|
57
|
+
cacheCreationInputTokens: cacheCreation,
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
if (row.cache_creation_5m_tokens !== null && row.cache_creation_5m_tokens !== undefined) {
|
|
61
|
+
usage.cacheCreationInputTokens5m = row.cache_creation_5m_tokens;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (row.cache_creation_1h_tokens !== null && row.cache_creation_1h_tokens !== undefined) {
|
|
65
|
+
usage.cacheCreationInputTokens1h = row.cache_creation_1h_tokens;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return usage;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** 현재 가격표로 계산한 driver 비용(micro-USD). 모델을 모르면 undefined. */
|
|
72
|
+
export function pricingTableCostMicroUsd(row: RequestLogUsageRow): number | undefined {
|
|
73
|
+
const usage = normalizedUsageForCost(row);
|
|
74
|
+
|
|
75
|
+
if (!usage.model) return undefined;
|
|
76
|
+
|
|
77
|
+
return Math.round(
|
|
78
|
+
calculateCostUsd(usage.model, {
|
|
79
|
+
inputTokens: usage.inputTokens,
|
|
80
|
+
outputTokens: usage.outputTokens,
|
|
81
|
+
cachedInputTokens: usage.cachedInputTokens,
|
|
82
|
+
cacheCreationInputTokens: usage.cacheCreationInputTokens,
|
|
83
|
+
cacheCreationInputTokens5m: usage.cacheCreationInputTokens5m,
|
|
84
|
+
cacheCreationInputTokens1h: usage.cacheCreationInputTokens1h,
|
|
85
|
+
}) * MICRO_USD,
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 출처 없는 비용을 쓰기 시점에 가격표로 확정한다. 읽기 경로는 저장값만 더하므로, 이 규칙을
|
|
91
|
+
* 거친 row 는 재계산 보정이 0 이다. 출처가 있으면 그대로 두고, 모델을 모르거나 저장값이 없는데
|
|
92
|
+
* 계산값도 0 이면(미확정 media 등) 바꾸지 않는다.
|
|
93
|
+
*/
|
|
94
|
+
export function resolvePricingCost(
|
|
95
|
+
row: RequestLogUsageRow,
|
|
96
|
+
): { cost_usd: number; cost_source: "pricing_table" } | undefined {
|
|
97
|
+
if (row.cost_source) return undefined;
|
|
98
|
+
const cost = pricingTableCostMicroUsd(row);
|
|
99
|
+
|
|
100
|
+
if (cost === undefined || (cost === 0 && (row.cost_usd ?? null) === null)) return undefined;
|
|
101
|
+
|
|
102
|
+
return { cost_usd: cost, cost_source: "pricing_table" };
|
|
103
|
+
}
|
|
@@ -596,17 +596,11 @@ describe("RequestLogModel cost provenance", () => {
|
|
|
596
596
|
});
|
|
597
597
|
|
|
598
598
|
/**
|
|
599
|
-
* totalCost 는
|
|
599
|
+
* totalCost 는 저장된 비용을 SQL SUM 한 번으로 합산한다.
|
|
600
600
|
* @param confirmedTotal SUM 쿼리가 돌려줄 micro-USD 합계
|
|
601
|
-
* @param legacyRows 재계산 대상 row 들
|
|
602
601
|
*/
|
|
603
|
-
function
|
|
604
|
-
|
|
605
|
-
legacyRows: unknown[],
|
|
606
|
-
unknownImageCostCount = 0,
|
|
607
|
-
unknownCostCount = 0,
|
|
608
|
-
) {
|
|
609
|
-
// subset qb 는 SELECT 를 비우고(clear) 집계/축소된 컬럼만 다시 얹는다 — 구현과 같은 체인.
|
|
602
|
+
function mockTotalCostQuery(confirmedTotal: number, unknownImageCostCount = 0, unknownCostCount = 0) {
|
|
603
|
+
// subset qb 는 SELECT 를 비우고(clear) 집계 컬럼만 다시 얹는다 — 구현과 같은 체인.
|
|
610
604
|
const sumChain = {
|
|
611
605
|
where: vi.fn(),
|
|
612
606
|
whereIn: vi.fn(),
|
|
@@ -621,31 +615,16 @@ describe("RequestLogModel cost provenance", () => {
|
|
|
621
615
|
sumChain.clear.mockReturnValue(sumChain);
|
|
622
616
|
sumChain.select.mockReturnValue(sumChain);
|
|
623
617
|
|
|
624
|
-
const
|
|
625
|
-
where: vi.fn(),
|
|
626
|
-
whereIn: vi.fn(),
|
|
627
|
-
whereRaw: vi.fn(),
|
|
628
|
-
clear: vi.fn(),
|
|
629
|
-
select: vi.fn(async () => legacyRows),
|
|
630
|
-
};
|
|
631
|
-
legacyChain.where.mockReturnValue(legacyChain);
|
|
632
|
-
legacyChain.whereIn.mockReturnValue(legacyChain);
|
|
633
|
-
legacyChain.whereRaw.mockReturnValue(legacyChain);
|
|
634
|
-
legacyChain.clear.mockReturnValue(legacyChain);
|
|
635
|
-
|
|
636
|
-
const getSubsetQueries = vi
|
|
637
|
-
.fn()
|
|
638
|
-
.mockReturnValueOnce({ qb: sumChain })
|
|
639
|
-
.mockReturnValueOnce({ qb: legacyChain });
|
|
618
|
+
const getSubsetQueries = vi.fn().mockReturnValue({ qb: sumChain });
|
|
640
619
|
vi.spyOn(
|
|
641
620
|
RequestLogModel as unknown as { getSubsetQueries: typeof getSubsetQueries },
|
|
642
621
|
"getSubsetQueries",
|
|
643
622
|
).mockImplementation(getSubsetQueries);
|
|
644
|
-
return { sumChain,
|
|
623
|
+
return { sumChain, getSubsetQueries };
|
|
645
624
|
}
|
|
646
625
|
|
|
647
626
|
it("uses exact stored cost for rows with a cost source", async () => {
|
|
648
|
-
|
|
627
|
+
mockTotalCostQuery(3_000);
|
|
649
628
|
|
|
650
629
|
await expect(RequestLogModel.totalCost()).resolves.toEqual({
|
|
651
630
|
usd: 0.003,
|
|
@@ -655,7 +634,7 @@ describe("RequestLogModel cost provenance", () => {
|
|
|
655
634
|
});
|
|
656
635
|
|
|
657
636
|
it("counts unpriced non-token media rows separately from unknown image costs", async () => {
|
|
658
|
-
const { sumChain } =
|
|
637
|
+
const { sumChain } = mockTotalCostQuery(3_000, 2, 3);
|
|
659
638
|
|
|
660
639
|
await expect(RequestLogModel.totalCost()).resolves.toEqual({
|
|
661
640
|
usd: 0.003,
|
|
@@ -679,77 +658,18 @@ describe("RequestLogModel cost provenance", () => {
|
|
|
679
658
|
}
|
|
680
659
|
});
|
|
681
660
|
|
|
682
|
-
it("sums
|
|
683
|
-
const { sumChain,
|
|
661
|
+
it("sums stored costs in one SQL query without re-pricing source-less rows", async () => {
|
|
662
|
+
const { sumChain, getSubsetQueries } = mockTotalCostQuery(3_000);
|
|
684
663
|
|
|
685
664
|
await RequestLogModel.totalCost();
|
|
686
665
|
|
|
666
|
+
expect(getSubsetQueries).toHaveBeenCalledTimes(1);
|
|
687
667
|
expect(sumChain.select).toHaveBeenCalledTimes(1);
|
|
688
|
-
// source 없는 행도 저장된 이미지 비용을 포함하고 driver 차액만 별도로 보정한다.
|
|
689
668
|
expect(sumChain.whereRaw).not.toHaveBeenCalled();
|
|
690
|
-
expect(legacyChain.whereRaw).toHaveBeenCalledWith(
|
|
691
|
-
"request_logs.cost_source IS NULL OR request_logs.cost_usd IS NULL",
|
|
692
|
-
);
|
|
693
669
|
});
|
|
694
670
|
|
|
695
|
-
it("
|
|
696
|
-
|
|
697
|
-
{
|
|
698
|
-
model_name: "claude-sonnet-4-6",
|
|
699
|
-
input_tokens: 100_000,
|
|
700
|
-
output_tokens: 0,
|
|
701
|
-
cache_read_tokens: 0,
|
|
702
|
-
cache_creation_tokens: 80_000,
|
|
703
|
-
cache_creation_5m_tokens: 30_000,
|
|
704
|
-
cache_creation_1h_tokens: 50_000,
|
|
705
|
-
cost_usd: null,
|
|
706
|
-
cost_source: null,
|
|
707
|
-
},
|
|
708
|
-
]);
|
|
709
|
-
|
|
710
|
-
await expect(RequestLogModel.totalCost().then((result) => result.usd)).resolves.toBeCloseTo(0.4725, 10);
|
|
711
|
-
});
|
|
712
|
-
|
|
713
|
-
it("adds only the delta for legacy rows already counted in the SQL sum", async () => {
|
|
714
|
-
// 전체 SUM 이 저장값 0.2 를 이미 포함하므로, 재계산값 0.4725 와의 차액만 더해야 한다.
|
|
715
|
-
mockTotalCostQueries(200_000, [
|
|
716
|
-
{
|
|
717
|
-
model_name: "claude-sonnet-4-6",
|
|
718
|
-
input_tokens: 100_000,
|
|
719
|
-
output_tokens: 0,
|
|
720
|
-
cache_read_tokens: 0,
|
|
721
|
-
cache_creation_tokens: 80_000,
|
|
722
|
-
cache_creation_5m_tokens: 30_000,
|
|
723
|
-
cache_creation_1h_tokens: 50_000,
|
|
724
|
-
cost_usd: 200_000,
|
|
725
|
-
cost_source: null,
|
|
726
|
-
},
|
|
727
|
-
]);
|
|
728
|
-
|
|
729
|
-
await expect(RequestLogModel.totalCost().then((result) => result.usd)).resolves.toBeCloseTo(0.4725, 10);
|
|
730
|
-
});
|
|
731
|
-
|
|
732
|
-
it("leaves the stored value untouched for legacy rows without a model name", async () => {
|
|
733
|
-
// 모델을 모르면 재계산할 수 없다. 저장값이 SUM 에 이미 들어갔으므로 보정하지 않는다.
|
|
734
|
-
mockTotalCostQueries(5_000, [
|
|
735
|
-
{
|
|
736
|
-
model_name: null,
|
|
737
|
-
input_tokens: 100,
|
|
738
|
-
output_tokens: 100,
|
|
739
|
-
cache_read_tokens: 0,
|
|
740
|
-
cache_creation_tokens: 0,
|
|
741
|
-
cache_creation_5m_tokens: null,
|
|
742
|
-
cache_creation_1h_tokens: null,
|
|
743
|
-
cost_usd: 5_000,
|
|
744
|
-
cost_source: null,
|
|
745
|
-
},
|
|
746
|
-
]);
|
|
747
|
-
|
|
748
|
-
await expect(RequestLogModel.totalCost().then((result) => result.usd)).resolves.toBeCloseTo(0.005, 10);
|
|
749
|
-
});
|
|
750
|
-
|
|
751
|
-
it("applies list filters to both cost queries so totals match the filtered list", async () => {
|
|
752
|
-
const { sumChain, legacyChain } = mockTotalCostQueries(0, []);
|
|
671
|
+
it("applies list filters so totals match the filtered list", async () => {
|
|
672
|
+
const { sumChain } = mockTotalCostQuery(0);
|
|
753
673
|
|
|
754
674
|
await RequestLogModel.totalCost({
|
|
755
675
|
num: 0,
|
|
@@ -759,39 +679,17 @@ describe("RequestLogModel cost provenance", () => {
|
|
|
759
679
|
model_name: "openai/gpt-5.5",
|
|
760
680
|
});
|
|
761
681
|
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
expect(chain.where).toHaveBeenCalledWith("request_logs.model_name", "openai/gpt-5.5");
|
|
766
|
-
}
|
|
682
|
+
expect(sumChain.where).toHaveBeenCalledWith("request_logs.project_name", "deti");
|
|
683
|
+
expect(sumChain.where).toHaveBeenCalledWith("request_logs.token_name", "openai/yds");
|
|
684
|
+
expect(sumChain.where).toHaveBeenCalledWith("request_logs.model_name", "openai/gpt-5.5");
|
|
767
685
|
});
|
|
768
686
|
|
|
769
|
-
it("applies the unassigned-project filter
|
|
770
|
-
const { sumChain
|
|
687
|
+
it("applies the unassigned-project filter", async () => {
|
|
688
|
+
const { sumChain } = mockTotalCostQuery(0);
|
|
771
689
|
|
|
772
690
|
await RequestLogModel.totalCost({ num: 0, page: 1, project_name_is_null: true });
|
|
773
691
|
|
|
774
|
-
|
|
775
|
-
expect(chain.where).toHaveBeenCalledWith("request_logs.project_name", null);
|
|
776
|
-
}
|
|
777
|
-
});
|
|
778
|
-
|
|
779
|
-
it("combines the SQL-summed total with the legacy recomputation delta", async () => {
|
|
780
|
-
mockTotalCostQueries(3_000, [
|
|
781
|
-
{
|
|
782
|
-
model_name: "claude-sonnet-4-6",
|
|
783
|
-
input_tokens: 100_000,
|
|
784
|
-
output_tokens: 0,
|
|
785
|
-
cache_read_tokens: 0,
|
|
786
|
-
cache_creation_tokens: 80_000,
|
|
787
|
-
cache_creation_5m_tokens: 30_000,
|
|
788
|
-
cache_creation_1h_tokens: 50_000,
|
|
789
|
-
cost_usd: null,
|
|
790
|
-
cost_source: null,
|
|
791
|
-
},
|
|
792
|
-
]);
|
|
793
|
-
|
|
794
|
-
await expect(RequestLogModel.totalCost().then((result) => result.usd)).resolves.toBeCloseTo(0.4755, 10);
|
|
692
|
+
expect(sumChain.where).toHaveBeenCalledWith("request_logs.project_name", null);
|
|
795
693
|
});
|
|
796
694
|
});
|
|
797
695
|
|
|
@@ -800,7 +698,7 @@ describe("RequestLogModel PostgreSQL", () => {
|
|
|
800
698
|
await Sonamu.initForTesting();
|
|
801
699
|
});
|
|
802
700
|
|
|
803
|
-
it("includes image costs once and filters
|
|
701
|
+
it("includes image costs once and filters stored totals", async () => {
|
|
804
702
|
const projectName = `cost-${randomUUID()}`;
|
|
805
703
|
const ids: number[] = [];
|
|
806
704
|
try {
|
|
@@ -829,9 +727,9 @@ describe("RequestLogModel PostgreSQL", () => {
|
|
|
829
727
|
const { rows } = await RequestLogModel.findMany("C", params);
|
|
830
728
|
expect(rows.map((row) => row.id)).toEqual(expect.arrayContaining([ids[0], ids[1], ids[3], ids[4]]));
|
|
831
729
|
expect(rows).toHaveLength(4);
|
|
832
|
-
// .
|
|
730
|
+
// .03 known images + .005/.001 stored drivers + .001 hosted driver + .002 text; other project excluded.
|
|
833
731
|
const total = await RequestLogModel.totalCost(params);
|
|
834
|
-
expect(total.usd).toBeCloseTo(0.
|
|
732
|
+
expect(total.usd).toBeCloseTo(0.039, 10);
|
|
835
733
|
expect(total.unknownImageCostCount).toBe(1);
|
|
836
734
|
} finally {
|
|
837
735
|
await RequestLogModel.del(ids);
|
|
@@ -875,6 +773,106 @@ describe("RequestLogModel PostgreSQL", () => {
|
|
|
875
773
|
}
|
|
876
774
|
});
|
|
877
775
|
|
|
776
|
+
it("prices unsourced finishRun and save costs at write time but keeps unknown media unpriced", async () => {
|
|
777
|
+
const projectName = `write-cost-${randomUUID()}`;
|
|
778
|
+
const ids: number[] = [];
|
|
779
|
+
const read = (id: number) =>
|
|
780
|
+
RequestLogModel.getPuri("r")
|
|
781
|
+
.from("request_logs")
|
|
782
|
+
.select({ cost_usd: "cost_usd", cost_source: "cost_source" })
|
|
783
|
+
.where("id", id)
|
|
784
|
+
.first();
|
|
785
|
+
try {
|
|
786
|
+
const priced = await RequestLogModel.createRun({ user_prompt: "priced", project_name: projectName });
|
|
787
|
+
const media = await RequestLogModel.createRun({ user_prompt: "media", project_name: projectName });
|
|
788
|
+
ids.push(priced, media);
|
|
789
|
+
// Sonnet 4.6: 1K input × $3 + 1K output × $15 = $0.018; a source-less caller cost is re-priced.
|
|
790
|
+
await RequestLogModel.finishRun(priced, {
|
|
791
|
+
status: "succeeded",
|
|
792
|
+
token_name: "anthropic/test",
|
|
793
|
+
model_name: "claude-sonnet-4-6",
|
|
794
|
+
input_tokens: 1_000,
|
|
795
|
+
output_tokens: 1_000,
|
|
796
|
+
cost_usd: 1,
|
|
797
|
+
});
|
|
798
|
+
await RequestLogModel.finishRun(media, {
|
|
799
|
+
status: "succeeded",
|
|
800
|
+
token_name: "grok/test",
|
|
801
|
+
model_name: "grok/tts",
|
|
802
|
+
input_tokens: 0,
|
|
803
|
+
output_tokens: 0,
|
|
804
|
+
cost_usd: null,
|
|
805
|
+
cost_source: null,
|
|
806
|
+
});
|
|
807
|
+
expect(await read(priced)).toEqual({ cost_usd: 18_000, cost_source: "pricing_table" });
|
|
808
|
+
expect(await read(media)).toEqual({ cost_usd: null, cost_source: null });
|
|
809
|
+
|
|
810
|
+
const saved = (
|
|
811
|
+
await RequestLogModel.save([
|
|
812
|
+
{
|
|
813
|
+
user_prompt: "saved",
|
|
814
|
+
project_name: projectName,
|
|
815
|
+
token_name: "anthropic/test",
|
|
816
|
+
model_name: "claude-sonnet-4-6",
|
|
817
|
+
input_tokens: 1_000,
|
|
818
|
+
output_tokens: 1_000,
|
|
819
|
+
cache_read_tokens: 0,
|
|
820
|
+
cache_creation_tokens: 0,
|
|
821
|
+
cost_usd: null,
|
|
822
|
+
cost_source: null,
|
|
823
|
+
} as Parameters<typeof RequestLogModel.save>[0][number],
|
|
824
|
+
])
|
|
825
|
+
)[0]!;
|
|
826
|
+
ids.push(saved);
|
|
827
|
+
expect(await read(saved)).toEqual({ cost_usd: 18_000, cost_source: "pricing_table" });
|
|
828
|
+
} finally {
|
|
829
|
+
await RequestLogModel.del(ids);
|
|
830
|
+
}
|
|
831
|
+
});
|
|
832
|
+
|
|
833
|
+
it("prices an expired run whose generate steps carried no cost source", async () => {
|
|
834
|
+
const id = await RequestLogModel.createRun({ user_prompt: "stale external run" });
|
|
835
|
+
try {
|
|
836
|
+
await RequestLogModel.recordRunProgress(id, { token_name: "anthropic/test", model_name: "claude-sonnet-4-6" });
|
|
837
|
+
await RequestLogModel.appendStep(id, { step_index: 0, type: "generate", input_tokens: 1_000, output_tokens: 1_000 });
|
|
838
|
+
await RequestLogModel.appendStep(id, { step_index: 1, type: "tool_call", tool_call_id: "call-1", tool_name: "t" });
|
|
839
|
+
|
|
840
|
+
await expect(
|
|
841
|
+
RequestLogModel.tryExpireStaleToolWaitingRun(id, new Date(Date.now() + 60_000), "stale"),
|
|
842
|
+
).resolves.toBe(true);
|
|
843
|
+
const row = await RequestLogModel.getPuri("r")
|
|
844
|
+
.from("request_logs")
|
|
845
|
+
.select({ cost_usd: "cost_usd", cost_source: "cost_source" })
|
|
846
|
+
.where("id", id)
|
|
847
|
+
.first();
|
|
848
|
+
expect(row).toEqual({ cost_usd: 18_000, cost_source: "pricing_table" });
|
|
849
|
+
} finally {
|
|
850
|
+
await RequestLogModel.del([id]);
|
|
851
|
+
}
|
|
852
|
+
});
|
|
853
|
+
|
|
854
|
+
it("lists distinct project and model names in ascending order without NULL", async () => {
|
|
855
|
+
const prefix = `distinct-${randomUUID()}`;
|
|
856
|
+
const ids: number[] = [];
|
|
857
|
+
try {
|
|
858
|
+
for (const project of [`${prefix}-b`, `${prefix}-a`, `${prefix}-b`, null]) {
|
|
859
|
+
const id = await RequestLogModel.createRun({ user_prompt: "distinct", project_name: project ?? undefined });
|
|
860
|
+
ids.push(id);
|
|
861
|
+
await RequestLogModel.recordRunProgress(id, { model_name: `${prefix}-model` });
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
const projects = (await RequestLogModel.distinctProjectNames()).filter((name) => name.startsWith(prefix));
|
|
865
|
+
expect(projects).toEqual([`${prefix}-a`, `${prefix}-b`]);
|
|
866
|
+
expect((await RequestLogModel.distinctModelNames()).filter((name) => name.startsWith(prefix))).toEqual([
|
|
867
|
+
`${prefix}-model`,
|
|
868
|
+
]);
|
|
869
|
+
const all = await RequestLogModel.distinctProjectNames();
|
|
870
|
+
expect(all).toEqual([...all].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0)));
|
|
871
|
+
} finally {
|
|
872
|
+
await RequestLogModel.del(ids);
|
|
873
|
+
}
|
|
874
|
+
});
|
|
875
|
+
|
|
878
876
|
it("does not expire a run while another connection holds its follow-up lock", async () => {
|
|
879
877
|
const wdb = RequestLogModel.getPuri("w");
|
|
880
878
|
const client = new Client(wdb.knex.client.config.connection as ClientConfig);
|