@things-factory/ai-client-base 10.1.277 → 10.1.279
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-server/service/agent/ai-client-for.js +2 -0
- package/dist-server/service/agent/ai-model-price.d.ts +22 -0
- package/dist-server/service/agent/ai-model-price.js +81 -0
- package/dist-server/service/agent/ai-usage-summary.d.ts +8 -1
- package/dist-server/service/agent/ai-usage-summary.js +17 -4
- package/dist-server/service/agent/ai-usage.d.ts +2 -0
- package/dist-server/service/agent/ai-usage.js +12 -2
- package/dist-server/service/agent/index.d.ts +2 -0
- package/dist-server/service/agent/index.js +4 -1
- package/dist-server/service/agent/model-price-boot.d.ts +1 -0
- package/dist-server/service/agent/model-price-boot.js +14 -0
- package/dist-server/service/agent/model-price.d.ts +77 -0
- package/dist-server/service/agent/model-price.js +214 -0
- package/dist-server/service/index.d.ts +3 -1
- package/dist-server/service/index.js +3 -1
- package/dist-server/service/providers/anthropic.js +2 -5
- package/dist-server/service/providers/gemini.js +2 -6
- package/dist-server/service/providers/openai.js +2 -5
- package/dist-server/service/providers/usage.d.ts +18 -0
- package/dist-server/service/providers/usage.js +50 -0
- package/dist-server/service/types.d.ts +6 -0
- package/package.json +20 -6
- package/translations/en.json +5 -1
- package/translations/es.json +5 -1
- package/translations/ja.json +5 -1
- package/translations/ko.json +5 -1
- package/translations/ms.json +5 -1
- package/translations/zh.json +5 -1
- package/dist-server/index.js.map +0 -1
- package/dist-server/service/agent/agent-directory.js.map +0 -1
- package/dist-server/service/agent/agent-settings-save.js.map +0 -1
- package/dist-server/service/agent/agent-spec.js.map +0 -1
- package/dist-server/service/agent/ai-client-for.js.map +0 -1
- package/dist-server/service/agent/ai-usage-summary.js.map +0 -1
- package/dist-server/service/agent/ai-usage.js.map +0 -1
- package/dist-server/service/agent/index.js.map +0 -1
- package/dist-server/service/agent/provider-models.js.map +0 -1
- package/dist-server/service/agentic-loop/agentic-loop.js.map +0 -1
- package/dist-server/service/agentic-loop/index.js.map +0 -1
- package/dist-server/service/analysis-contract.js.map +0 -1
- package/dist-server/service/assistant/abort-notice.js.map +0 -1
- package/dist-server/service/assistant/assistant-chat.js.map +0 -1
- package/dist-server/service/assistant/index.js.map +0 -1
- package/dist-server/service/assistant/open-subject.js.map +0 -1
- package/dist-server/service/assistant/registry-assistant.js.map +0 -1
- package/dist-server/service/assistant/registry-gate.js.map +0 -1
- package/dist-server/service/assistant/summarize.js.map +0 -1
- package/dist-server/service/assistant/tool-budget.js.map +0 -1
- package/dist-server/service/assistant/tool-scope.js.map +0 -1
- package/dist-server/service/capability.js.map +0 -1
- package/dist-server/service/decision/config.js.map +0 -1
- package/dist-server/service/decision/decision.js.map +0 -1
- package/dist-server/service/decision/index.js.map +0 -1
- package/dist-server/service/decision/jev.js.map +0 -1
- package/dist-server/service/factory.js.map +0 -1
- package/dist-server/service/index.js.map +0 -1
- package/dist-server/service/providers/anthropic.js.map +0 -1
- package/dist-server/service/providers/gemini.js.map +0 -1
- package/dist-server/service/providers/index.js.map +0 -1
- package/dist-server/service/providers/openai.js.map +0 -1
- package/dist-server/service/providers/request-options.js.map +0 -1
- package/dist-server/service/testing/tool-call-context.js.map +0 -1
- package/dist-server/service/tool-registry/call-recorder.js.map +0 -1
- package/dist-server/service/tool-registry/index.js.map +0 -1
- package/dist-server/service/tool-registry/registry.js.map +0 -1
- package/dist-server/service/tool-registry/translate.js.map +0 -1
- package/dist-server/service/tool-registry/types.js.map +0 -1
- package/dist-server/service/types.js.map +0 -1
- package/dist-server/service/utils/grounding.js.map +0 -1
- package/dist-server/service/utils/index.js.map +0 -1
- package/dist-server/service/utils/parse-json.js.map +0 -1
- package/dist-server/service/utils/transient.js.map +0 -1
- package/dist-server/tsconfig.tsbuildinfo +0 -1
- package/docs/grounded-analysis-contract.md +0 -54
- package/experiments/entity360/README.md +0 -224
- package/experiments/entity360/apps.cjs +0 -103
- package/experiments/entity360/apps.test.cjs +0 -24
- package/experiments/entity360/baseline-apps-results.json +0 -3357
- package/experiments/entity360/baseline-expanded-results.json +0 -5952
- package/experiments/entity360/baseline-results.json +0 -1507
- package/experiments/entity360/expanded.cjs +0 -65
- package/experiments/entity360/fixtures.json +0 -100
- package/experiments/entity360/index.html +0 -44
- package/experiments/entity360/jev-apps-results.json +0 -3440
- package/experiments/entity360/jev-expanded-results.json +0 -6095
- package/experiments/entity360/jev-results.json +0 -1542
- package/experiments/entity360/run.cjs +0 -100
- package/server/index.ts +0 -19
- package/server/service/agent/agent-directory.test.ts +0 -206
- package/server/service/agent/agent-directory.ts +0 -198
- package/server/service/agent/agent-settings-save.test.ts +0 -151
- package/server/service/agent/agent-settings-save.ts +0 -205
- package/server/service/agent/agent-spec.ts +0 -103
- package/server/service/agent/ai-client-for.test.ts +0 -170
- package/server/service/agent/ai-client-for.ts +0 -215
- package/server/service/agent/ai-usage-summary.test.ts +0 -65
- package/server/service/agent/ai-usage-summary.ts +0 -146
- package/server/service/agent/ai-usage.ts +0 -97
- package/server/service/agent/index.ts +0 -7
- package/server/service/agent/one-door.test.ts +0 -57
- package/server/service/agent/provider-models.ts +0 -43
- package/server/service/agentic-loop/agentic-loop.test.ts +0 -1118
- package/server/service/agentic-loop/agentic-loop.ts +0 -745
- package/server/service/agentic-loop/index.ts +0 -1
- package/server/service/agentic-loop/proposal-key.test.ts +0 -83
- package/server/service/analysis-contract.test.ts +0 -20
- package/server/service/analysis-contract.ts +0 -77
- package/server/service/assistant/abort-notice.ts +0 -67
- package/server/service/assistant/assistant-chat.test.ts +0 -240
- package/server/service/assistant/assistant-chat.ts +0 -104
- package/server/service/assistant/index.ts +0 -13
- package/server/service/assistant/open-subject.test.ts +0 -91
- package/server/service/assistant/open-subject.ts +0 -140
- package/server/service/assistant/registry-assistant.test.ts +0 -627
- package/server/service/assistant/registry-assistant.ts +0 -240
- package/server/service/assistant/registry-gate.ts +0 -76
- package/server/service/assistant/summarize.ts +0 -47
- package/server/service/assistant/tool-budget.test.ts +0 -246
- package/server/service/assistant/tool-budget.ts +0 -186
- package/server/service/assistant/tool-scope.test.ts +0 -123
- package/server/service/assistant/tool-scope.ts +0 -103
- package/server/service/capability.test.ts +0 -40
- package/server/service/capability.ts +0 -49
- package/server/service/decision/config.test.ts +0 -55
- package/server/service/decision/config.ts +0 -49
- package/server/service/decision/decision.test.ts +0 -94
- package/server/service/decision/decision.ts +0 -112
- package/server/service/decision/index.ts +0 -3
- package/server/service/decision/jev.ts +0 -40
- package/server/service/factory.ts +0 -28
- package/server/service/index.ts +0 -17
- package/server/service/providers/anthropic.ts +0 -274
- package/server/service/providers/gemini.ts +0 -396
- package/server/service/providers/index.ts +0 -8
- package/server/service/providers/json-answer.test.ts +0 -53
- package/server/service/providers/openai-models.test.ts +0 -30
- package/server/service/providers/openai.ts +0 -326
- package/server/service/providers/request-options.ts +0 -12
- package/server/service/providers/request-signal.test.ts +0 -77
- package/server/service/providers/transcribe.test.ts +0 -18
- package/server/service/testing/tool-call-context.ts +0 -38
- package/server/service/tool-registry/call-recorder.ts +0 -116
- package/server/service/tool-registry/index.ts +0 -50
- package/server/service/tool-registry/registry.test.ts +0 -319
- package/server/service/tool-registry/registry.ts +0 -248
- package/server/service/tool-registry/translate.ts +0 -12
- package/server/service/tool-registry/types.ts +0 -305
- package/server/service/types.ts +0 -221
- package/server/service/utils/grounding.test.ts +0 -119
- package/server/service/utils/grounding.ts +0 -131
- package/server/service/utils/index.ts +0 -3
- package/server/service/utils/parse-json.ts +0 -116
- package/server/service/utils/transient.test.ts +0 -92
- package/server/service/utils/transient.ts +0 -92
- package/tsconfig.json +0 -9
|
@@ -106,6 +106,8 @@ function meter(client, entry, request) {
|
|
|
106
106
|
sessionId: request.sessionId ?? undefined,
|
|
107
107
|
inputTokens: usage.inputTokens,
|
|
108
108
|
outputTokens: usage.outputTokens,
|
|
109
|
+
cacheReadTokens: usage.cacheReadTokens,
|
|
110
|
+
cacheWriteTokens: usage.cacheWriteTokens,
|
|
109
111
|
/* The whole call's time goes on its last request; the rows of one call still add up to it. */
|
|
110
112
|
durationMs: index === all.length - 1 ? durationMs : 0,
|
|
111
113
|
outcome: index === all.length - 1 ? outcome : 'ok',
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { User } from '@things-factory/auth-base';
|
|
2
|
+
/**
|
|
3
|
+
* What one model costs per million tokens, in USD (ADR-0061 decision 6 amendment 1-1).
|
|
4
|
+
*
|
|
5
|
+
* One table for every domain: a provider sets its prices, and what differs between domains is only which models they
|
|
6
|
+
* use. A row is made when a model in use has none - read from the LiteLLM price list - or by the system administrator.
|
|
7
|
+
*/
|
|
8
|
+
export declare class AiModelPrice {
|
|
9
|
+
readonly id: string;
|
|
10
|
+
provider: string;
|
|
11
|
+
model: string;
|
|
12
|
+
inputPerMTok: number;
|
|
13
|
+
outputPerMTok: number;
|
|
14
|
+
cacheReadPerMTok?: number;
|
|
15
|
+
cacheWritePerMTok?: number;
|
|
16
|
+
/** `litellm` or `manual`. */
|
|
17
|
+
source: string;
|
|
18
|
+
listedAs?: string;
|
|
19
|
+
updater?: User;
|
|
20
|
+
updaterId?: string;
|
|
21
|
+
updatedAt: Date;
|
|
22
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AiModelPrice = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const typeorm_1 = require("typeorm");
|
|
6
|
+
const type_graphql_1 = require("type-graphql");
|
|
7
|
+
const auth_base_1 = require("@things-factory/auth-base");
|
|
8
|
+
/**
|
|
9
|
+
* What one model costs per million tokens, in USD (ADR-0061 decision 6 amendment 1-1).
|
|
10
|
+
*
|
|
11
|
+
* One table for every domain: a provider sets its prices, and what differs between domains is only which models they
|
|
12
|
+
* use. A row is made when a model in use has none - read from the LiteLLM price list - or by the system administrator.
|
|
13
|
+
*/
|
|
14
|
+
let AiModelPrice = class AiModelPrice {
|
|
15
|
+
};
|
|
16
|
+
exports.AiModelPrice = AiModelPrice;
|
|
17
|
+
tslib_1.__decorate([
|
|
18
|
+
(0, typeorm_1.PrimaryGeneratedColumn)('uuid'),
|
|
19
|
+
(0, type_graphql_1.Field)(type => type_graphql_1.ID, { description: 'Row id.' }),
|
|
20
|
+
tslib_1.__metadata("design:type", String)
|
|
21
|
+
], AiModelPrice.prototype, "id", void 0);
|
|
22
|
+
tslib_1.__decorate([
|
|
23
|
+
(0, typeorm_1.Column)(),
|
|
24
|
+
(0, type_graphql_1.Field)(type => String, { description: 'openai, anthropic or gemini.' }),
|
|
25
|
+
tslib_1.__metadata("design:type", String)
|
|
26
|
+
], AiModelPrice.prototype, "provider", void 0);
|
|
27
|
+
tslib_1.__decorate([
|
|
28
|
+
(0, typeorm_1.Column)(),
|
|
29
|
+
(0, type_graphql_1.Field)(type => String, { description: 'The model, as the provider names it in its answers.' }),
|
|
30
|
+
tslib_1.__metadata("design:type", String)
|
|
31
|
+
], AiModelPrice.prototype, "model", void 0);
|
|
32
|
+
tslib_1.__decorate([
|
|
33
|
+
(0, typeorm_1.Column)({ type: 'float' }),
|
|
34
|
+
(0, type_graphql_1.Field)(type => type_graphql_1.Float, { description: 'USD per million input tokens.' }),
|
|
35
|
+
tslib_1.__metadata("design:type", Number)
|
|
36
|
+
], AiModelPrice.prototype, "inputPerMTok", void 0);
|
|
37
|
+
tslib_1.__decorate([
|
|
38
|
+
(0, typeorm_1.Column)({ type: 'float' }),
|
|
39
|
+
(0, type_graphql_1.Field)(type => type_graphql_1.Float, { description: 'USD per million output tokens.' }),
|
|
40
|
+
tslib_1.__metadata("design:type", Number)
|
|
41
|
+
], AiModelPrice.prototype, "outputPerMTok", void 0);
|
|
42
|
+
tslib_1.__decorate([
|
|
43
|
+
(0, typeorm_1.Column)({ type: 'float', nullable: true }),
|
|
44
|
+
(0, type_graphql_1.Field)(type => type_graphql_1.Float, { nullable: true, description: 'USD per million input tokens read from the cache. Empty uses the input price.' }),
|
|
45
|
+
tslib_1.__metadata("design:type", Number)
|
|
46
|
+
], AiModelPrice.prototype, "cacheReadPerMTok", void 0);
|
|
47
|
+
tslib_1.__decorate([
|
|
48
|
+
(0, typeorm_1.Column)({ type: 'float', nullable: true }),
|
|
49
|
+
(0, type_graphql_1.Field)(type => type_graphql_1.Float, { nullable: true, description: 'USD per million input tokens written to the cache. Empty uses the input price.' }),
|
|
50
|
+
tslib_1.__metadata("design:type", Number)
|
|
51
|
+
], AiModelPrice.prototype, "cacheWritePerMTok", void 0);
|
|
52
|
+
tslib_1.__decorate([
|
|
53
|
+
(0, typeorm_1.Column)(),
|
|
54
|
+
(0, type_graphql_1.Field)(type => String, { description: 'Where the prices came from: litellm or manual.' }),
|
|
55
|
+
tslib_1.__metadata("design:type", String)
|
|
56
|
+
], AiModelPrice.prototype, "source", void 0);
|
|
57
|
+
tslib_1.__decorate([
|
|
58
|
+
(0, typeorm_1.Column)({ nullable: true }),
|
|
59
|
+
(0, type_graphql_1.Field)(type => String, { nullable: true, description: 'The name the price list had the model under, when it came from LiteLLM.' }),
|
|
60
|
+
tslib_1.__metadata("design:type", String)
|
|
61
|
+
], AiModelPrice.prototype, "listedAs", void 0);
|
|
62
|
+
tslib_1.__decorate([
|
|
63
|
+
(0, typeorm_1.ManyToOne)(type => auth_base_1.User, { nullable: true }),
|
|
64
|
+
(0, type_graphql_1.Field)(type => auth_base_1.User, { nullable: true, description: 'Who last changed it. Empty when it was read from LiteLLM on its own.' }),
|
|
65
|
+
tslib_1.__metadata("design:type", auth_base_1.User)
|
|
66
|
+
], AiModelPrice.prototype, "updater", void 0);
|
|
67
|
+
tslib_1.__decorate([
|
|
68
|
+
(0, typeorm_1.RelationId)((price) => price.updater),
|
|
69
|
+
tslib_1.__metadata("design:type", String)
|
|
70
|
+
], AiModelPrice.prototype, "updaterId", void 0);
|
|
71
|
+
tslib_1.__decorate([
|
|
72
|
+
(0, typeorm_1.UpdateDateColumn)(),
|
|
73
|
+
(0, type_graphql_1.Field)(type => Date, { description: 'When it was read or last changed.' }),
|
|
74
|
+
tslib_1.__metadata("design:type", Date)
|
|
75
|
+
], AiModelPrice.prototype, "updatedAt", void 0);
|
|
76
|
+
exports.AiModelPrice = AiModelPrice = tslib_1.__decorate([
|
|
77
|
+
(0, typeorm_1.Entity)(),
|
|
78
|
+
(0, typeorm_1.Index)('ix_ai_model_price_0', (price) => [price.provider, price.model], { unique: true }),
|
|
79
|
+
(0, type_graphql_1.ObjectType)({ description: 'What one AI model costs per million tokens, in USD.' })
|
|
80
|
+
], AiModelPrice);
|
|
81
|
+
//# sourceMappingURL=ai-model-price.js.map
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Domain } from '@things-factory/shell';
|
|
2
|
+
import { type ModelPriceValues } from './model-price.js';
|
|
2
3
|
export type AiUsageBucketSize = 'hour' | 'day' | 'month';
|
|
3
4
|
export interface AiUsageFilter {
|
|
4
5
|
from: Date;
|
|
@@ -8,6 +9,8 @@ export interface AiUsageFilter {
|
|
|
8
9
|
userIds?: string[];
|
|
9
10
|
/** Also count what domains below did with this domain's agents - per domain, without their people. */
|
|
10
11
|
includeDescendants?: boolean;
|
|
12
|
+
/** False adds everyone's calls together: no row names a person (ADR-0061 decision 6 amendment 1-7). */
|
|
13
|
+
byPerson?: boolean;
|
|
11
14
|
}
|
|
12
15
|
export interface AiUsageBucketRow {
|
|
13
16
|
/** The bucket in the domain's time zone: `2026-10-12 14:00`, `2026-10-12` or `2026-10`. */
|
|
@@ -25,7 +28,11 @@ export interface AiUsageBucketRow {
|
|
|
25
28
|
calls: number;
|
|
26
29
|
inputTokens: number;
|
|
27
30
|
outputTokens: number;
|
|
31
|
+
cacheReadTokens: number;
|
|
32
|
+
cacheWriteTokens: number;
|
|
28
33
|
failures: number;
|
|
34
|
+
/** USD at the model's price. Empty when the model has no price - never 0 (ADR-0061 decision 6 amendment 1-4). */
|
|
35
|
+
cost?: number;
|
|
29
36
|
}
|
|
30
37
|
/**
|
|
31
38
|
* Usage per bucket × agent × person (design/ai/agent-settings.md §6-2).
|
|
@@ -34,4 +41,4 @@ export interface AiUsageBucketRow {
|
|
|
34
41
|
* domain's days or months here. People of a domain below are never named to the domain above.
|
|
35
42
|
*/
|
|
36
43
|
export declare function summarizeAiUsage(given: Domain, filter: AiUsageFilter): Promise<AiUsageBucketRow[]>;
|
|
37
|
-
export declare function rollUp(hours: any[], bucket: AiUsageBucketSize, timezone: string | undefined, hereId: string, agentNameOf: Map<string, string
|
|
44
|
+
export declare function rollUp(hours: any[], bucket: AiUsageBucketSize, timezone: string | undefined, hereId: string, agentNameOf: Map<string, string>, prices?: Map<string, ModelPriceValues>, byPerson?: boolean): AiUsageBucketRow[];
|
|
@@ -4,6 +4,7 @@ exports.summarizeAiUsage = summarizeAiUsage;
|
|
|
4
4
|
exports.rollUp = rollUp;
|
|
5
5
|
const shell_1 = require("@things-factory/shell");
|
|
6
6
|
const agent_directory_js_1 = require("./agent-directory.js");
|
|
7
|
+
const model_price_js_1 = require("./model-price.js");
|
|
7
8
|
/**
|
|
8
9
|
* Usage per bucket × agent × person (design/ai/agent-settings.md §6-2).
|
|
9
10
|
*
|
|
@@ -29,6 +30,8 @@ async function summarizeAiUsage(given, filter) {
|
|
|
29
30
|
.addSelect('COUNT(*)', 'calls')
|
|
30
31
|
.addSelect('SUM(COALESCE(usage.inputTokens, 0))', 'inputTokens')
|
|
31
32
|
.addSelect('SUM(COALESCE(usage.outputTokens, 0))', 'outputTokens')
|
|
33
|
+
.addSelect('SUM(COALESCE(usage.cacheReadTokens, 0))', 'cacheReadTokens')
|
|
34
|
+
.addSelect('SUM(COALESCE(usage.cacheWriteTokens, 0))', 'cacheWriteTokens')
|
|
32
35
|
.addSelect(`SUM(CASE WHEN usage.outcome = 'ok' THEN 0 ELSE 1 END)`, 'failures')
|
|
33
36
|
.where('usage.hourStart >= :from AND usage.hourStart < :to', { from: filter.from, to: filter.to })
|
|
34
37
|
.groupBy('usage.hourStart')
|
|
@@ -52,16 +55,16 @@ async function summarizeAiUsage(given, filter) {
|
|
|
52
55
|
query.andWhere('usage.agentId IN (:...agentIds)', { agentIds: filter.agentIds });
|
|
53
56
|
if (filter.userIds?.length)
|
|
54
57
|
query.andWhere('creator.id IN (:...userIds)', { userIds: filter.userIds });
|
|
55
|
-
const [hours, directory] = await Promise.all([query.getRawMany(), (0, agent_directory_js_1.readAiAgentDirectory)(domain)]);
|
|
58
|
+
const [hours, directory, prices] = await Promise.all([query.getRawMany(), (0, agent_directory_js_1.readAiAgentDirectory)(domain), (0, model_price_js_1.readModelPrices)()]);
|
|
56
59
|
const agentNameOf = new Map(directory.entries.map(one => [one.spec.id, one.spec.name]));
|
|
57
|
-
return rollUp(hours, filter.bucket, domain.timezone, domain.id, agentNameOf);
|
|
60
|
+
return rollUp(hours, filter.bucket, domain.timezone, domain.id, agentNameOf, prices, filter.byPerson ?? true);
|
|
58
61
|
}
|
|
59
|
-
function rollUp(hours, bucket, timezone, hereId, agentNameOf) {
|
|
62
|
+
function rollUp(hours, bucket, timezone, hereId, agentNameOf, prices = new Map(), byPerson = true) {
|
|
60
63
|
const label = bucketLabeller(bucket, timezone);
|
|
61
64
|
const rows = new Map();
|
|
62
65
|
for (const hour of hours) {
|
|
63
66
|
const below = hour.domainId !== hereId;
|
|
64
|
-
const userId = below ? undefined : hour.userId ?? undefined;
|
|
67
|
+
const userId = below || !byPerson ? undefined : hour.userId ?? undefined;
|
|
65
68
|
const key = [label(new Date(hour.hourStart)), hour.agentId, hour.provider, hour.model, hour.domainId, userId].join('\u0000');
|
|
66
69
|
const row = rows.get(key) ??
|
|
67
70
|
rows
|
|
@@ -76,14 +79,24 @@ function rollUp(hours, bucket, timezone, hereId, agentNameOf) {
|
|
|
76
79
|
calls: 0,
|
|
77
80
|
inputTokens: 0,
|
|
78
81
|
outputTokens: 0,
|
|
82
|
+
cacheReadTokens: 0,
|
|
83
|
+
cacheWriteTokens: 0,
|
|
79
84
|
failures: 0
|
|
80
85
|
})
|
|
81
86
|
.get(key);
|
|
82
87
|
row.calls += Number(hour.calls) || 0;
|
|
83
88
|
row.inputTokens += Number(hour.inputTokens) || 0;
|
|
84
89
|
row.outputTokens += Number(hour.outputTokens) || 0;
|
|
90
|
+
row.cacheReadTokens += Number(hour.cacheReadTokens) || 0;
|
|
91
|
+
row.cacheWriteTokens += Number(hour.cacheWriteTokens) || 0;
|
|
85
92
|
row.failures += Number(hour.failures) || 0;
|
|
86
93
|
}
|
|
94
|
+
/* Cost is linear in the tokens, so the rolled-up row is priced once. */
|
|
95
|
+
for (const row of rows.values()) {
|
|
96
|
+
const price = prices.get((0, model_price_js_1.modelKey)(row));
|
|
97
|
+
if (price)
|
|
98
|
+
row.cost = (0, model_price_js_1.costOf)(row, price);
|
|
99
|
+
}
|
|
87
100
|
return [...rows.values()].sort((a, b) => b.bucket.localeCompare(a.bucket) || b.calls - a.calls);
|
|
88
101
|
}
|
|
89
102
|
/** Names the bucket a UTC moment falls in, in the domain's time zone (the server's when the domain has none). */
|
|
@@ -20,6 +20,8 @@ export declare class AiUsage {
|
|
|
20
20
|
operation: string;
|
|
21
21
|
sessionId?: string;
|
|
22
22
|
inputTokens?: number;
|
|
23
|
+
cacheReadTokens?: number;
|
|
24
|
+
cacheWriteTokens?: number;
|
|
23
25
|
outputTokens?: number;
|
|
24
26
|
durationMs: number;
|
|
25
27
|
/** `ok`, or why it failed: key-rejected · rate-limited · provider-error. */
|
|
@@ -72,12 +72,22 @@ tslib_1.__decorate([
|
|
|
72
72
|
], AiUsage.prototype, "sessionId", void 0);
|
|
73
73
|
tslib_1.__decorate([
|
|
74
74
|
(0, typeorm_1.Column)({ type: 'int', nullable: true }),
|
|
75
|
-
(0, type_graphql_1.Field)(type => type_graphql_1.Int, { nullable: true, description: '
|
|
75
|
+
(0, type_graphql_1.Field)(type => type_graphql_1.Int, { nullable: true, description: 'Every input token, cached ones included.' }),
|
|
76
76
|
tslib_1.__metadata("design:type", Number)
|
|
77
77
|
], AiUsage.prototype, "inputTokens", void 0);
|
|
78
78
|
tslib_1.__decorate([
|
|
79
79
|
(0, typeorm_1.Column)({ type: 'int', nullable: true }),
|
|
80
|
-
(0, type_graphql_1.Field)(type => type_graphql_1.Int, { nullable: true, description: '
|
|
80
|
+
(0, type_graphql_1.Field)(type => type_graphql_1.Int, { nullable: true, description: 'The part of the input read from the provider cache.' }),
|
|
81
|
+
tslib_1.__metadata("design:type", Number)
|
|
82
|
+
], AiUsage.prototype, "cacheReadTokens", void 0);
|
|
83
|
+
tslib_1.__decorate([
|
|
84
|
+
(0, typeorm_1.Column)({ type: 'int', nullable: true }),
|
|
85
|
+
(0, type_graphql_1.Field)(type => type_graphql_1.Int, { nullable: true, description: 'The part of the input written to the provider cache.' }),
|
|
86
|
+
tslib_1.__metadata("design:type", Number)
|
|
87
|
+
], AiUsage.prototype, "cacheWriteTokens", void 0);
|
|
88
|
+
tslib_1.__decorate([
|
|
89
|
+
(0, typeorm_1.Column)({ type: 'int', nullable: true }),
|
|
90
|
+
(0, type_graphql_1.Field)(type => type_graphql_1.Int, { nullable: true, description: 'Every output token, reasoning included.' }),
|
|
81
91
|
tslib_1.__metadata("design:type", Number)
|
|
82
92
|
], AiUsage.prototype, "outputTokens", void 0);
|
|
83
93
|
tslib_1.__decorate([
|
|
@@ -4,4 +4,6 @@ export * from './ai-client-for.js';
|
|
|
4
4
|
export * from './provider-models.js';
|
|
5
5
|
export * from './agent-settings-save.js';
|
|
6
6
|
export * from './ai-usage-summary.js';
|
|
7
|
+
export * from './model-price.js';
|
|
7
8
|
export { AiUsage } from './ai-usage.js';
|
|
9
|
+
export { AiModelPrice } from './ai-model-price.js';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AiUsage = void 0;
|
|
3
|
+
exports.AiModelPrice = exports.AiUsage = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
tslib_1.__exportStar(require("./agent-spec.js"), exports);
|
|
6
6
|
tslib_1.__exportStar(require("./agent-directory.js"), exports);
|
|
@@ -8,6 +8,9 @@ tslib_1.__exportStar(require("./ai-client-for.js"), exports);
|
|
|
8
8
|
tslib_1.__exportStar(require("./provider-models.js"), exports);
|
|
9
9
|
tslib_1.__exportStar(require("./agent-settings-save.js"), exports);
|
|
10
10
|
tslib_1.__exportStar(require("./ai-usage-summary.js"), exports);
|
|
11
|
+
tslib_1.__exportStar(require("./model-price.js"), exports);
|
|
11
12
|
var ai_usage_js_1 = require("./ai-usage.js");
|
|
12
13
|
Object.defineProperty(exports, "AiUsage", { enumerable: true, get: function () { return ai_usage_js_1.AiUsage; } });
|
|
14
|
+
var ai_model_price_js_1 = require("./ai-model-price.js");
|
|
15
|
+
Object.defineProperty(exports, "AiModelPrice", { enumerable: true, get: function () { return ai_model_price_js_1.AiModelPrice; } });
|
|
13
16
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const model_price_js_1 = require("./model-price.js");
|
|
4
|
+
/*
|
|
5
|
+
* When the server starts, the models of every domain's agents and of every usage row that have no price get one from
|
|
6
|
+
* the price list (ADR-0061 decision 6 amendment 1-2). An agent changed since still leaves its old rows priced. Never
|
|
7
|
+
* on a schedule; a failure is logged and the models stay unpriced.
|
|
8
|
+
*/
|
|
9
|
+
process.on('bootstrap-module-start', () => {
|
|
10
|
+
(0, model_price_js_1.readModelsInUse)()
|
|
11
|
+
.then(models => (0, model_price_js_1.fillMissingModelPrices)(models))
|
|
12
|
+
.catch(error => console.error('[ai-client-base] could not read the models in use:', error?.message ?? error));
|
|
13
|
+
});
|
|
14
|
+
//# sourceMappingURL=model-price-boot.js.map
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { AiModelPrice } from './ai-model-price.js';
|
|
2
|
+
/**
|
|
3
|
+
* The price of the models in use, and what a usage row cost (ADR-0061 decision 6 amendment 1).
|
|
4
|
+
*
|
|
5
|
+
* No provider answers its prices through an API, so prices are read from the one list LiteLLM publishes - only for a
|
|
6
|
+
* model in use, and only when it has none. A row already in the table is never changed on its own.
|
|
7
|
+
*/
|
|
8
|
+
/** The one price list prices are read from. Prices in it are USD per token. */
|
|
9
|
+
export declare const MODEL_PRICE_LIST_URL = "https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json";
|
|
10
|
+
export interface ModelRef {
|
|
11
|
+
provider: string;
|
|
12
|
+
model: string;
|
|
13
|
+
}
|
|
14
|
+
export interface ModelPriceValues {
|
|
15
|
+
inputPerMTok: number;
|
|
16
|
+
outputPerMTok: number;
|
|
17
|
+
cacheReadPerMTok?: number;
|
|
18
|
+
cacheWritePerMTok?: number;
|
|
19
|
+
}
|
|
20
|
+
export interface ListedPrice extends ModelPriceValues {
|
|
21
|
+
/** The name the list had the model under. */
|
|
22
|
+
listedAs: string;
|
|
23
|
+
}
|
|
24
|
+
export interface UsageTokens {
|
|
25
|
+
inputTokens: number;
|
|
26
|
+
outputTokens: number;
|
|
27
|
+
cacheReadTokens?: number;
|
|
28
|
+
cacheWriteTokens?: number;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The names a model is looked for under, in order (amendment 1-3): `<provider>/<model>`, `<model>`, then both without
|
|
32
|
+
* a date at the end - a provider answers with a dated name (`gpt-5-2025-08-07`) the list may only have undated.
|
|
33
|
+
*/
|
|
34
|
+
export declare function listNamesOf({ provider, model }: ModelRef): string[];
|
|
35
|
+
/** The model's prices per million tokens, from the list - or nothing when it is not there with both prices. */
|
|
36
|
+
export declare function priceInList(list: Record<string, any>, ref: ModelRef): ListedPrice | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* What tokens cost at a price, in USD (amendment 1-6). The cached parts of the input are charged at the cache prices,
|
|
39
|
+
* the input price when the model has none; the rest of the input at the input price.
|
|
40
|
+
*/
|
|
41
|
+
export declare function costOf(tokens: UsageTokens, price: ModelPriceValues): number;
|
|
42
|
+
export declare const modelKey: ({ provider, model }: ModelRef) => string;
|
|
43
|
+
/** Every price in the table, by model. */
|
|
44
|
+
export declare function readModelPrices(): Promise<Map<string, AiModelPrice>>;
|
|
45
|
+
/** Reads the LiteLLM list. A list that cannot be read is a refusal that names it. */
|
|
46
|
+
export declare function fetchModelPriceList(): Promise<Record<string, any>>;
|
|
47
|
+
/**
|
|
48
|
+
* Gives the models that have no price one from the list (amendment 1-2). A model already priced is left as it is; a
|
|
49
|
+
* model the list does not have stays unpriced. Never throws: a failure is logged and the models stay unpriced.
|
|
50
|
+
*/
|
|
51
|
+
export declare function fillMissingModelPrices(models: ModelRef[]): Promise<ModelRef[]>;
|
|
52
|
+
/** Reads one model's prices from the list again, over what the table has - the administrator's own act. */
|
|
53
|
+
export declare function refreshModelPriceFromList(ref: ModelRef, user: {
|
|
54
|
+
id: string;
|
|
55
|
+
}): Promise<AiModelPrice>;
|
|
56
|
+
export interface ModelPriceRefresh {
|
|
57
|
+
/** Read from the list - new, or changed to the list's prices. */
|
|
58
|
+
updated: ModelRef[];
|
|
59
|
+
/** Entered by hand, so left as they are. */
|
|
60
|
+
kept: ModelRef[];
|
|
61
|
+
/** Not in the list; a row they had stays. */
|
|
62
|
+
unlisted: ModelRef[];
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Reads the prices of these models from the list again, in one fetch (amendment 1-8). A price entered by hand is left
|
|
66
|
+
* as it is; every other row takes the list's prices. Anyone who may change the domain's settings may ask for it, since
|
|
67
|
+
* the values can only be the list's. A list that cannot be read is a refusal.
|
|
68
|
+
*/
|
|
69
|
+
export declare function refreshModelPricesFromList(models: ModelRef[], user: {
|
|
70
|
+
id: string;
|
|
71
|
+
}): Promise<ModelPriceRefresh>;
|
|
72
|
+
/** The administrator's own prices for a model. */
|
|
73
|
+
export declare function saveModelPrice(ref: ModelRef & ModelPriceValues, user: {
|
|
74
|
+
id: string;
|
|
75
|
+
}): Promise<AiModelPrice>;
|
|
76
|
+
/** The models of every domain's agents and of every usage row - what the server prices when it starts. */
|
|
77
|
+
export declare function readModelsInUse(domainIds?: string[]): Promise<ModelRef[]>;
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.modelKey = exports.MODEL_PRICE_LIST_URL = void 0;
|
|
4
|
+
exports.listNamesOf = listNamesOf;
|
|
5
|
+
exports.priceInList = priceInList;
|
|
6
|
+
exports.costOf = costOf;
|
|
7
|
+
exports.readModelPrices = readModelPrices;
|
|
8
|
+
exports.fetchModelPriceList = fetchModelPriceList;
|
|
9
|
+
exports.fillMissingModelPrices = fillMissingModelPrices;
|
|
10
|
+
exports.refreshModelPriceFromList = refreshModelPriceFromList;
|
|
11
|
+
exports.refreshModelPricesFromList = refreshModelPricesFromList;
|
|
12
|
+
exports.saveModelPrice = saveModelPrice;
|
|
13
|
+
exports.readModelsInUse = readModelsInUse;
|
|
14
|
+
const typeorm_1 = require("typeorm");
|
|
15
|
+
const shell_1 = require("@things-factory/shell");
|
|
16
|
+
const setting_base_1 = require("@things-factory/setting-base");
|
|
17
|
+
const agent_spec_js_1 = require("./agent-spec.js");
|
|
18
|
+
/**
|
|
19
|
+
* The price of the models in use, and what a usage row cost (ADR-0061 decision 6 amendment 1).
|
|
20
|
+
*
|
|
21
|
+
* No provider answers its prices through an API, so prices are read from the one list LiteLLM publishes - only for a
|
|
22
|
+
* model in use, and only when it has none. A row already in the table is never changed on its own.
|
|
23
|
+
*/
|
|
24
|
+
/** The one price list prices are read from. Prices in it are USD per token. */
|
|
25
|
+
exports.MODEL_PRICE_LIST_URL = 'https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json';
|
|
26
|
+
const PRICE_LIST_TIMEOUT_MS = 15_000;
|
|
27
|
+
/**
|
|
28
|
+
* The names a model is looked for under, in order (amendment 1-3): `<provider>/<model>`, `<model>`, then both without
|
|
29
|
+
* a date at the end - a provider answers with a dated name (`gpt-5-2025-08-07`) the list may only have undated.
|
|
30
|
+
*/
|
|
31
|
+
function listNamesOf({ provider, model }) {
|
|
32
|
+
const bare = model.replace(/^models\//, '');
|
|
33
|
+
const undated = bare.replace(/-(\d{4}-\d{2}-\d{2}|\d{8})$/, '');
|
|
34
|
+
const names = [`${provider}/${bare}`, bare, `${provider}/${undated}`, undated];
|
|
35
|
+
return [...new Set(names)];
|
|
36
|
+
}
|
|
37
|
+
/** The model's prices per million tokens, from the list - or nothing when it is not there with both prices. */
|
|
38
|
+
function priceInList(list, ref) {
|
|
39
|
+
for (const name of listNamesOf(ref)) {
|
|
40
|
+
const entry = list[name];
|
|
41
|
+
if (typeof entry?.input_cost_per_token !== 'number' || typeof entry?.output_cost_per_token !== 'number')
|
|
42
|
+
continue;
|
|
43
|
+
return {
|
|
44
|
+
listedAs: name,
|
|
45
|
+
inputPerMTok: perMTok(entry.input_cost_per_token),
|
|
46
|
+
outputPerMTok: perMTok(entry.output_cost_per_token),
|
|
47
|
+
cacheReadPerMTok: perMTok(entry.cache_read_input_token_cost),
|
|
48
|
+
cacheWritePerMTok: perMTok(entry.cache_creation_input_token_cost)
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
/* USD per token to USD per million tokens, without the float noise of the multiplication (0.00000125 → 1.25). */
|
|
54
|
+
function perMTok(perToken) {
|
|
55
|
+
if (typeof perToken !== 'number' || !Number.isFinite(perToken))
|
|
56
|
+
return undefined;
|
|
57
|
+
return Math.round(perToken * 1e12) / 1e6;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* What tokens cost at a price, in USD (amendment 1-6). The cached parts of the input are charged at the cache prices,
|
|
61
|
+
* the input price when the model has none; the rest of the input at the input price.
|
|
62
|
+
*/
|
|
63
|
+
function costOf(tokens, price) {
|
|
64
|
+
const read = tokens.cacheReadTokens ?? 0;
|
|
65
|
+
const write = tokens.cacheWriteTokens ?? 0;
|
|
66
|
+
const plain = Math.max(0, tokens.inputTokens - read - write);
|
|
67
|
+
const usd = plain * price.inputPerMTok +
|
|
68
|
+
read * (price.cacheReadPerMTok ?? price.inputPerMTok) +
|
|
69
|
+
write * (price.cacheWritePerMTok ?? price.inputPerMTok) +
|
|
70
|
+
tokens.outputTokens * price.outputPerMTok;
|
|
71
|
+
return usd / 1e6;
|
|
72
|
+
}
|
|
73
|
+
const modelKey = ({ provider, model }) => `${provider}\u0000${model}`;
|
|
74
|
+
exports.modelKey = modelKey;
|
|
75
|
+
/** Every price in the table, by model. */
|
|
76
|
+
async function readModelPrices() {
|
|
77
|
+
const rows = await (0, shell_1.getRepository)('AiModelPrice').find();
|
|
78
|
+
return new Map(rows.map(one => [(0, exports.modelKey)(one), one]));
|
|
79
|
+
}
|
|
80
|
+
/** Reads the LiteLLM list. A list that cannot be read is a refusal that names it. */
|
|
81
|
+
async function fetchModelPriceList() {
|
|
82
|
+
try {
|
|
83
|
+
const response = await fetch(exports.MODEL_PRICE_LIST_URL, { signal: AbortSignal.timeout(PRICE_LIST_TIMEOUT_MS) });
|
|
84
|
+
if (!response.ok)
|
|
85
|
+
throw new Error(`status ${response.status}`);
|
|
86
|
+
return await response.json();
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
throw new shell_1.Refusal('ai-model-price-list-unreachable', {}, `The model price list could not be read: ${error?.message ?? error}`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/* Models being filled now, so two saves at once read the list once. */
|
|
93
|
+
const filling = new Set();
|
|
94
|
+
/**
|
|
95
|
+
* Gives the models that have no price one from the list (amendment 1-2). A model already priced is left as it is; a
|
|
96
|
+
* model the list does not have stays unpriced. Never throws: a failure is logged and the models stay unpriced.
|
|
97
|
+
*/
|
|
98
|
+
async function fillMissingModelPrices(models) {
|
|
99
|
+
try {
|
|
100
|
+
const prices = await readModelPrices();
|
|
101
|
+
const wanted = new Map();
|
|
102
|
+
for (const one of models) {
|
|
103
|
+
const key = (0, exports.modelKey)(one);
|
|
104
|
+
if (one.provider && one.model && !prices.has(key) && !filling.has(key))
|
|
105
|
+
wanted.set(key, one);
|
|
106
|
+
}
|
|
107
|
+
if (!wanted.size)
|
|
108
|
+
return [];
|
|
109
|
+
for (const key of wanted.keys())
|
|
110
|
+
filling.add(key);
|
|
111
|
+
try {
|
|
112
|
+
const list = await fetchModelPriceList();
|
|
113
|
+
const rows = [...wanted.values()].flatMap(ref => {
|
|
114
|
+
const listed = priceInList(list, ref);
|
|
115
|
+
return listed ? [{ ...ref, ...listed, source: 'litellm' }] : [];
|
|
116
|
+
});
|
|
117
|
+
if (rows.length)
|
|
118
|
+
await (0, shell_1.getRepository)('AiModelPrice').save(rows);
|
|
119
|
+
const unlisted = [...wanted.values()].filter(ref => !rows.some(row => (0, exports.modelKey)(row) === (0, exports.modelKey)(ref)));
|
|
120
|
+
if (unlisted.length) {
|
|
121
|
+
console.warn(`[ai-client-base] no price in the model price list for: ${unlisted.map(one => `${one.provider}:${one.model}`).join(', ')}`);
|
|
122
|
+
}
|
|
123
|
+
return rows.map(({ provider, model }) => ({ provider, model }));
|
|
124
|
+
}
|
|
125
|
+
finally {
|
|
126
|
+
for (const key of wanted.keys())
|
|
127
|
+
filling.delete(key);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
console.error('[ai-client-base] could not fill model prices:', error?.message ?? error);
|
|
132
|
+
return [];
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/** Reads one model's prices from the list again, over what the table has - the administrator's own act. */
|
|
136
|
+
async function refreshModelPriceFromList(ref, user) {
|
|
137
|
+
const listed = priceInList(await fetchModelPriceList(), ref);
|
|
138
|
+
if (!listed) {
|
|
139
|
+
throw new shell_1.Refusal('ai-model-price-not-listed', { ...ref }, `The model price list has no price for ${ref.model}.`);
|
|
140
|
+
}
|
|
141
|
+
return upsertModelPrice(ref, { ...listed, source: 'litellm' }, user);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Reads the prices of these models from the list again, in one fetch (amendment 1-8). A price entered by hand is left
|
|
145
|
+
* as it is; every other row takes the list's prices. Anyone who may change the domain's settings may ask for it, since
|
|
146
|
+
* the values can only be the list's. A list that cannot be read is a refusal.
|
|
147
|
+
*/
|
|
148
|
+
async function refreshModelPricesFromList(models, user) {
|
|
149
|
+
const list = await fetchModelPriceList();
|
|
150
|
+
const prices = await readModelPrices();
|
|
151
|
+
const result = { updated: [], kept: [], unlisted: [] };
|
|
152
|
+
const unique = new Map(models.filter(one => one.provider && one.model).map(one => [(0, exports.modelKey)(one), one]));
|
|
153
|
+
for (const [key, ref] of unique) {
|
|
154
|
+
if (prices.get(key)?.source === 'manual') {
|
|
155
|
+
result.kept.push(ref);
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
const listed = priceInList(list, ref);
|
|
159
|
+
if (!listed) {
|
|
160
|
+
result.unlisted.push(ref);
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
await upsertModelPrice(ref, { ...listed, source: 'litellm' }, user);
|
|
164
|
+
result.updated.push(ref);
|
|
165
|
+
}
|
|
166
|
+
return result;
|
|
167
|
+
}
|
|
168
|
+
/** The administrator's own prices for a model. */
|
|
169
|
+
async function saveModelPrice(ref, user) {
|
|
170
|
+
if (!ref.provider || !ref.model)
|
|
171
|
+
throw new shell_1.Refusal('ai-model-price-model-required', {}, 'Name the provider and the model.');
|
|
172
|
+
for (const field of ['inputPerMTok', 'outputPerMTok', 'cacheReadPerMTok', 'cacheWritePerMTok']) {
|
|
173
|
+
const value = ref[field];
|
|
174
|
+
const required = field === 'inputPerMTok' || field === 'outputPerMTok';
|
|
175
|
+
if (value == null ? required : !Number.isFinite(value) || value < 0) {
|
|
176
|
+
throw new shell_1.Refusal('ai-model-price-invalid', { field }, `${field} must be a price of 0 or more.`);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return upsertModelPrice(ref, {
|
|
180
|
+
inputPerMTok: ref.inputPerMTok,
|
|
181
|
+
outputPerMTok: ref.outputPerMTok,
|
|
182
|
+
cacheReadPerMTok: ref.cacheReadPerMTok ?? null,
|
|
183
|
+
cacheWritePerMTok: ref.cacheWritePerMTok ?? null,
|
|
184
|
+
listedAs: null,
|
|
185
|
+
source: 'manual'
|
|
186
|
+
}, user);
|
|
187
|
+
}
|
|
188
|
+
async function upsertModelPrice(ref, values, user) {
|
|
189
|
+
const repository = (0, shell_1.getRepository)('AiModelPrice');
|
|
190
|
+
const found = await repository.findOne({ where: { provider: ref.provider, model: ref.model } });
|
|
191
|
+
return repository.save({ ...(found ?? {}), provider: ref.provider, model: ref.model, ...values, updater: { id: user.id } });
|
|
192
|
+
}
|
|
193
|
+
/** The models of every domain's agents and of every usage row - what the server prices when it starts. */
|
|
194
|
+
async function readModelsInUse(domainIds) {
|
|
195
|
+
const settings = await (0, shell_1.getRepository)(setting_base_1.Setting).find({
|
|
196
|
+
where: { name: agent_spec_js_1.AI_AGENTS_SETTING, ...(domainIds && { domain: { id: (0, typeorm_1.In)(domainIds) } }) }
|
|
197
|
+
});
|
|
198
|
+
const fromAgents = settings.flatMap(one => (0, agent_spec_js_1.parseAiAgentsSetting)(one.value).agents.map(({ provider, model }) => ({ provider, model })));
|
|
199
|
+
const query = (0, shell_1.getRepository)('AiUsage')
|
|
200
|
+
.createQueryBuilder('usage')
|
|
201
|
+
.select('usage.provider', 'provider')
|
|
202
|
+
.addSelect('usage.model', 'model')
|
|
203
|
+
.groupBy('usage.provider')
|
|
204
|
+
.addGroupBy('usage.model');
|
|
205
|
+
if (domainIds)
|
|
206
|
+
query.leftJoin('usage.domain', 'domain').where('domain.id IN (:...domainIds)', { domainIds });
|
|
207
|
+
const fromUsage = await query.getRawMany();
|
|
208
|
+
const unique = new Map();
|
|
209
|
+
for (const one of [...fromAgents, ...fromUsage])
|
|
210
|
+
if (one.provider && one.model)
|
|
211
|
+
unique.set((0, exports.modelKey)(one), one);
|
|
212
|
+
return [...unique.values()];
|
|
213
|
+
}
|
|
214
|
+
//# sourceMappingURL=model-price.js.map
|
|
@@ -10,5 +10,7 @@ export * from './decision/index.js';
|
|
|
10
10
|
export * from './capability.js';
|
|
11
11
|
export * from './agent/index.js';
|
|
12
12
|
import { AiUsage } from './agent/ai-usage.js';
|
|
13
|
-
|
|
13
|
+
import { AiModelPrice } from './agent/ai-model-price.js';
|
|
14
|
+
export declare const entities: (typeof AiUsage | typeof AiModelPrice)[];
|
|
15
|
+
import './agent/model-price-boot.js';
|
|
14
16
|
export * from './testing/tool-call-context.js';
|
|
@@ -16,6 +16,8 @@ tslib_1.__exportStar(require("./capability.js"), exports);
|
|
|
16
16
|
/* AI agents per domain - the one door to a model, and its usage (design/ai/agent-settings.md). */
|
|
17
17
|
tslib_1.__exportStar(require("./agent/index.js"), exports);
|
|
18
18
|
const ai_usage_js_1 = require("./agent/ai-usage.js");
|
|
19
|
-
|
|
19
|
+
const ai_model_price_js_1 = require("./agent/ai-model-price.js");
|
|
20
|
+
exports.entities = [ai_usage_js_1.AiUsage, ai_model_price_js_1.AiModelPrice];
|
|
21
|
+
require("./agent/model-price-boot.js");
|
|
20
22
|
tslib_1.__exportStar(require("./testing/tool-call-context.js"), exports);
|
|
21
23
|
//# sourceMappingURL=index.js.map
|
|
@@ -12,6 +12,7 @@ const sdk_1 = tslib_1.__importDefault(require("@anthropic-ai/sdk"));
|
|
|
12
12
|
const factory_js_1 = require("../factory.js");
|
|
13
13
|
const parse_json_js_1 = require("../utils/parse-json.js");
|
|
14
14
|
const request_options_js_1 = require("./request-options.js");
|
|
15
|
+
const usage_js_1 = require("./usage.js");
|
|
15
16
|
const DEFAULT_MODEL = 'claude-sonnet-4-6';
|
|
16
17
|
const DEFAULT_MAX_TOKENS = 4096;
|
|
17
18
|
class AnthropicAIClient {
|
|
@@ -119,11 +120,7 @@ class AnthropicAIClient {
|
|
|
119
120
|
}
|
|
120
121
|
exports.AnthropicAIClient = AnthropicAIClient;
|
|
121
122
|
function reportUsage(response, options, requested) {
|
|
122
|
-
options.onUsage?.(
|
|
123
|
-
model: response?.model ?? requested,
|
|
124
|
-
inputTokens: response?.usage?.input_tokens,
|
|
125
|
-
outputTokens: response?.usage?.output_tokens
|
|
126
|
-
});
|
|
123
|
+
options.onUsage?.((0, usage_js_1.anthropicUsageOf)(response?.model ?? requested, response?.usage));
|
|
127
124
|
}
|
|
128
125
|
// ── 변환 유틸 ─────────────────────────────────────────────────────────
|
|
129
126
|
function extractTextContent(msg) {
|
|
@@ -14,6 +14,7 @@ const generative_ai_1 = require("@google/generative-ai");
|
|
|
14
14
|
const factory_js_1 = require("../factory.js");
|
|
15
15
|
const parse_json_js_1 = require("../utils/parse-json.js");
|
|
16
16
|
const request_options_js_1 = require("./request-options.js");
|
|
17
|
+
const usage_js_1 = require("./usage.js");
|
|
17
18
|
// `gemini-3-pro` 는 2026-03-09 종료됨. 후속 권장 모델로 변경.
|
|
18
19
|
// 사용자가 자기 환경에서 활용 가능한 정확한 ID 는 ListModels 로 확인:
|
|
19
20
|
// curl "https://generativelanguage.googleapis.com/v1beta/models?key=$KEY"
|
|
@@ -163,12 +164,7 @@ class ProviderListError extends Error {
|
|
|
163
164
|
}
|
|
164
165
|
exports.ProviderListError = ProviderListError;
|
|
165
166
|
function reportUsage(result, options, requested) {
|
|
166
|
-
|
|
167
|
-
options.onUsage?.({
|
|
168
|
-
model: result?.response?.modelVersion ?? requested,
|
|
169
|
-
inputTokens: usage?.promptTokenCount,
|
|
170
|
-
outputTokens: usage?.candidatesTokenCount
|
|
171
|
-
});
|
|
167
|
+
options.onUsage?.((0, usage_js_1.geminiUsageOf)(result?.response?.modelVersion ?? requested, result?.response?.usageMetadata));
|
|
172
168
|
}
|
|
173
169
|
// ── 변환 유틸 ─────────────────────────────────────────────────────────
|
|
174
170
|
function baseGenerationConfig(options) {
|