briyah 2.0.9 → 2.0.11

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.
Files changed (55) hide show
  1. package/data/common/config/image_models.json +85 -6
  2. package/data/common/config/model_prices.json +30170 -32794
  3. package/dist-sdk/server/src/ai/LLM/anthropic.service.d.ts +7 -0
  4. package/dist-sdk/server/src/ai/LLM/anthropic.service.js +27 -24
  5. package/dist-sdk/server/src/ai/LLM/attachment-type.d.ts +21 -0
  6. package/dist-sdk/server/src/ai/LLM/attachment-type.js +76 -0
  7. package/dist-sdk/server/src/ai/LLM/base-ai.service.d.ts +6 -0
  8. package/dist-sdk/server/src/ai/LLM/base-ai.service.js +22 -17
  9. package/dist-sdk/server/src/ai/LLM/comfyui.service.js +1 -1
  10. package/dist-sdk/server/src/ai/LLM/deepseek.service.d.ts +1 -1
  11. package/dist-sdk/server/src/ai/LLM/deepseek.service.js +3 -2
  12. package/dist-sdk/server/src/ai/LLM/fal.service.d.ts +1 -1
  13. package/dist-sdk/server/src/ai/LLM/fal.service.js +4 -3
  14. package/dist-sdk/server/src/ai/LLM/googleai.service.d.ts +1 -1
  15. package/dist-sdk/server/src/ai/LLM/googleai.service.js +5 -4
  16. package/dist-sdk/server/src/ai/LLM/grok.service.d.ts +1 -1
  17. package/dist-sdk/server/src/ai/LLM/grok.service.js +3 -2
  18. package/dist-sdk/server/src/ai/LLM/mock.service.d.ts +1 -1
  19. package/dist-sdk/server/src/ai/LLM/mock.service.js +3 -2
  20. package/dist-sdk/server/src/ai/LLM/openai.service.d.ts +7 -1
  21. package/dist-sdk/server/src/ai/LLM/openai.service.js +30 -12
  22. package/dist-sdk/server/src/ai/LLM/self-hosted.service.js +1 -1
  23. package/dist-sdk/server/src/ai/LLM/together.service.d.ts +1 -1
  24. package/dist-sdk/server/src/ai/LLM/together.service.js +5 -4
  25. package/dist-sdk/server/src/ai/agent-config.d.ts +4 -0
  26. package/dist-sdk/server/src/ai/agent-store.service.js +3 -0
  27. package/dist-sdk/server/src/ai/agent.d.ts +7 -0
  28. package/dist-sdk/server/src/ai/agent.js +6 -0
  29. package/dist-sdk/server/src/app/balance.service.d.ts +155 -27
  30. package/dist-sdk/server/src/app/balance.service.js +290 -97
  31. package/dist-sdk/server/src/app/usage-context.d.ts +24 -0
  32. package/dist-sdk/server/src/app/usage-context.js +35 -0
  33. package/dist-sdk/server/src/app.controller.d.ts +5 -0
  34. package/dist-sdk/server/src/app.controller.js +13 -2
  35. package/dist-sdk/server/src/app.service.d.ts +39 -4
  36. package/dist-sdk/server/src/app.service.js +40 -3
  37. package/dist-sdk/server/src/common/errors.d.ts +13 -1
  38. package/dist-sdk/server/src/common/errors.js +21 -1
  39. package/dist-sdk/server/src/packs/pack-instantiation.service.js +1 -0
  40. package/dist-sdk/server/src/packs/pack-manifest.d.ts +3 -1
  41. package/dist-sdk/server/src/packs/pack-manifest.js +2 -0
  42. package/dist-sdk/server/src/room/illustration/illustration.service.d.ts +17 -2
  43. package/dist-sdk/server/src/room/illustration/illustration.service.js +35 -9
  44. package/dist-sdk/server/src/room/illustration/illustration.types.d.ts +6 -0
  45. package/dist-sdk/server/src/room/room-config.d.ts +11 -1
  46. package/dist-sdk/server/src/room/room-store.service.js +4 -0
  47. package/dist-sdk/server/src/room/room.d.ts +9 -1
  48. package/dist-sdk/server/src/room/room.js +16 -1
  49. package/dist-sdk/server/src/sdk/index.d.ts +7 -1
  50. package/dist-sdk/server/src/sdk/index.js +19 -1
  51. package/dist-sdk/server/src/tools/core/illustrate.tool.js +10 -1
  52. package/dist-sdk/server/src/tools/native-tool.types.d.ts +3 -2
  53. package/dist-sdk/shared/types/app.types.d.ts +27 -0
  54. package/dist-sdk/shared/types/app.types.js +6 -1
  55. package/package.json +1 -1
@@ -33,6 +33,10 @@ export interface AgentConfig {
33
33
  beginInstruction?: string;
34
34
  promptCacheTTL?: number;
35
35
  ownerRoomId?: string;
36
+ usageTags?: {
37
+ user?: string;
38
+ feature?: string;
39
+ };
36
40
  toolNames?: string[];
37
41
  useModerator?: boolean;
38
42
  /**
@@ -386,6 +386,8 @@ let AgentStoreService = class AgentStoreService {
386
386
  agent.templateAgentId = config.templateAgentId;
387
387
  // Restore room ownership (for agents cloned as part of published rooms)
388
388
  agent.ownerRoomId = config.ownerRoomId;
389
+ if (config.usageTags)
390
+ agent.usageTags = { ...config.usageTags };
389
391
  // Restore which pack the agent is filed under
390
392
  agent.pack = config.pack;
391
393
  agent.packAgentKey = config.packAgentKey;
@@ -499,6 +501,7 @@ let AgentStoreService = class AgentStoreService {
499
501
  beginInstruction: agent.beginInstruction,
500
502
  promptCacheTTL: agent.promptCacheTTL,
501
503
  ownerRoomId: agent.ownerRoomId, // NEW: Persist room ownership
504
+ usageTags: agent.usageTags,
502
505
  toolNames: agent.toolNames,
503
506
  useModerator: agent.useModerator,
504
507
  pack: agent.pack,
@@ -8,6 +8,7 @@ import { ArtifactService } from './artifact.service';
8
8
  import { UserPathsService } from '../config/user-paths.service';
9
9
  import { IConfigService } from '../config/env-config';
10
10
  import { BalanceService } from '../app/balance.service';
11
+ import { UsageTags } from '../app/usage-context';
11
12
  import { AgentStoreService } from './agent-store.service';
12
13
  export declare class Agent {
13
14
  id?: string;
@@ -32,6 +33,12 @@ export declare class Agent {
32
33
  isPublished: boolean;
33
34
  templateAgentId?: string;
34
35
  ownerRoomId?: string;
36
+ /**
37
+ * Who and what this agent's AI calls are for, recorded with every charge in
38
+ * the usage ledger (see BalanceService). Saved with the agent; wins over the
39
+ * tags of the context it runs in (runWithUsageTags).
40
+ */
41
+ usageTags?: UsageTags;
35
42
  /** The pack this agent is filed under. See {@link AgentConfig.pack}. */
36
43
  pack?: string;
37
44
  /** The pack template's key, when this agent came from one. */
@@ -31,6 +31,12 @@ class Agent {
31
31
  isPublished = false;
32
32
  templateAgentId;
33
33
  ownerRoomId; // The room this agent belongs to; it is hidden from the agent list and deleted with the room
34
+ /**
35
+ * Who and what this agent's AI calls are for, recorded with every charge in
36
+ * the usage ledger (see BalanceService). Saved with the agent; wins over the
37
+ * tags of the context it runs in (runWithUsageTags).
38
+ */
39
+ usageTags;
34
40
  /** The pack this agent is filed under. See {@link AgentConfig.pack}. */
35
41
  pack;
36
42
  /** The pack template's key, when this agent came from one. */
@@ -1,55 +1,183 @@
1
1
  import { UserPathsService } from '../config/user-paths.service';
2
2
  import { BalanceMessageService } from './balance-message.service';
3
+ import { UsageTags } from './usage-context';
4
+ /** The agent a charge is for; structural, so this file needs nothing from agent.ts. */
5
+ export interface UsageSource {
6
+ id?: string;
7
+ agentName?: string;
8
+ serviceName?: string;
9
+ modelName?: string;
10
+ usageTags?: UsageTags | null;
11
+ }
12
+ /** The monthly (or any period's) allowance: granted by the host, spent first, never carried over. */
13
+ export interface AllowanceState {
14
+ amount: number;
15
+ used: number;
16
+ /** ISO date-time the period began (when it was granted). */
17
+ periodStart: string | null;
18
+ /** The host's reference for the grant (e.g. the invoice that paid for it). */
19
+ reference: string | null;
20
+ }
21
+ /** What a user can spend: the allowance, then purchased credit. All in dollars. */
22
+ export interface CreditSummary {
23
+ allowance: AllowanceState & {
24
+ remaining: number;
25
+ };
26
+ /** Bought credit, kept until used. Negative only when the last call overdrew it. */
27
+ purchased: number;
28
+ /** What the next call can draw on: allowance remaining plus purchased. */
29
+ total: number;
30
+ }
31
+ /** One line of the usage ledger. */
32
+ export interface UsageEntry {
33
+ kind: 'charge' | 'grant' | 'purchase';
34
+ at: string;
35
+ /** Charge: what it cost, markup included. Grant or purchase: the amount. */
36
+ amount: number;
37
+ markup?: number;
38
+ /** Charge: how much came out of each pot. */
39
+ fromAllowance?: number;
40
+ fromPurchased?: number;
41
+ agentId?: string;
42
+ agentName?: string;
43
+ serviceName?: string;
44
+ modelName?: string;
45
+ tags?: UsageTags;
46
+ /** Grant: the host's reference. */
47
+ reference?: string;
48
+ }
49
+ /** A total of charges under one label. */
50
+ export interface UsageTotal {
51
+ key: string;
52
+ amount: number;
53
+ calls: number;
54
+ }
55
+ export interface UsageReport {
56
+ from: string;
57
+ to: string;
58
+ total: number;
59
+ calls: number;
60
+ fromAllowance: number;
61
+ fromPurchased: number;
62
+ /** By `tags.user`; untagged charges are under "". */
63
+ byUser: UsageTotal[];
64
+ /** By `tags.feature`, else the agent's name. */
65
+ byFeature: UsageTotal[];
66
+ /** By calendar day (UTC), YYYY-MM-DD. */
67
+ byDay: UsageTotal[];
68
+ /** The charges themselves, oldest first, when asked for. */
69
+ entries?: UsageEntry[];
70
+ }
3
71
  /**
4
- * Service for managing user balance
5
- * Tracks AI usage costs and enforces spending limits
72
+ * A user's AI credit: what they may spend, what they spent it on, and the
73
+ * limit that stops AI calls when it is gone.
74
+ *
75
+ * Credit sits in two pots, spent in this order:
76
+ * - **The allowance**, granted by the host for a period (for example, each
77
+ * month an invoice is paid). A new grant replaces it: unused allowance is not
78
+ * carried over. A user who is never granted one simply has none.
79
+ * - **Purchased credit**, added by payments ({@link addBalance}) and kept until
80
+ * used. New users start with `STARTING_BALANCE` here.
81
+ *
82
+ * The state is `userconfig/credits.json`; the single number Briyah kept before
83
+ * (`userconfig/balance`) is read once as purchased credit and then retired.
84
+ * Every charge, grant and purchase is also appended to the usage ledger,
85
+ * `usage/<yyyy-mm>.jsonl` in the user's data directory, with the agent and the
86
+ * usage tags it ran under (see usage-context.ts).
87
+ *
88
+ * A call is allowed while the total is not negative, as before: the last call
89
+ * may overdraw by its own cost, and the overdraft is taken out of the next
90
+ * allowance granted.
6
91
  */
7
92
  export declare class BalanceService {
8
93
  private userId;
9
94
  private userPaths;
10
- private balanceFilePath;
95
+ private creditsFilePath;
96
+ private legacyBalanceFilePath;
11
97
  private markupFilePath;
98
+ private usageDir;
12
99
  private startingBalance;
13
100
  private balanceMessageService?;
14
101
  constructor(userId: string, userPaths: UserPathsService, balanceMessageService?: BalanceMessageService);
15
- /**
16
- * Get current balance (loads from file)
17
- * @returns Current balance as number
18
- * @throws If balance file is corrupted
19
- */
102
+ /** What the next call can draw on: allowance remaining plus purchased credit. */
20
103
  getBalance(): number;
21
104
  /**
22
- * Get current markup (loads from file)
23
- * @returns Current markup total
24
- * @throws If markup file is corrupted
25
- */
26
- getMarkup(): number;
27
- /**
28
- * Check if user has sufficient balance to make AI calls
29
- * @returns true if balance >= 0, false otherwise
105
+ * Whether the user may make another AI call.
106
+ * @returns true while the total is not negative
30
107
  */
31
108
  hasSufficientBalance(): boolean;
32
109
  /**
33
- * Decrement balance by specified amount
34
- * @param amount Amount to subtract from balance
35
- * @param markup Amount to add to markup total
110
+ * Charges an AI call: out of the allowance first, the rest out of purchased
111
+ * credit (which the last call may overdraw), and records it in the ledger.
112
+ * @param amount What the call cost, markup included
113
+ * @param markup The markup part, also added to the platform's markup total
114
+ * @param source The agent the call was for, whose usage tags are recorded with it
36
115
  * @throws If amount is negative
37
116
  */
38
- decrementBalance(amount: number, markup?: number): void;
117
+ decrementBalance(amount: number, markup?: number, source?: UsageSource): void;
39
118
  /**
40
- * Add funds to balance
41
- * @param amount Amount to add to balance
119
+ * Adds purchased credit (after a successful payment). It is kept until used,
120
+ * whatever happens to the allowance.
42
121
  * @throws If amount is negative or zero
43
122
  */
44
123
  addBalance(amount: number): void;
45
124
  /**
46
- * Save balance to file
47
- * @param balance New balance value
125
+ * Starts a new allowance period: the allowance becomes `amount`, unspent, and
126
+ * whatever was left of the last one is gone. Purchased credit is untouched,
127
+ * except that an overdraft (purchased below zero) is paid out of the new
128
+ * allowance first.
129
+ *
130
+ * Applied once per reference: granting a reference already granted changes
131
+ * nothing, so a payment confirmed twice grants once.
132
+ *
133
+ * @param amount The new allowance in dollars (0 ends the allowance)
134
+ * @param options.reference The host's id for this grant, e.g. an invoice id
135
+ * @param options.periodStart When the period began; now when omitted
136
+ * @returns Whether it was applied, and the credit after it
137
+ */
138
+ grantAllowance(amount: number, options: {
139
+ reference: string;
140
+ periodStart?: Date | string;
141
+ }): {
142
+ granted: boolean;
143
+ credits: CreditSummary;
144
+ };
145
+ /** The allowance, purchased credit and total, as of now. */
146
+ getCredits(): CreditSummary;
147
+ /**
148
+ * What was spent between two moments (inclusive), with totals by user, by
149
+ * feature and by day. Defaults: from the start of the current allowance
150
+ * period (or of this month, without one) to now.
151
+ *
152
+ * @param options.entries Also return the charges themselves
48
153
  */
49
- private saveBalance;
154
+ getUsage(options?: {
155
+ from?: Date | string;
156
+ to?: Date | string;
157
+ entries?: boolean;
158
+ }): UsageReport;
159
+ /** Grants and purchases between two moments, oldest first. */
160
+ getCreditEvents(options?: {
161
+ from?: Date | string;
162
+ to?: Date | string;
163
+ }): UsageEntry[];
164
+ /**
165
+ * Get current markup (loads from file)
166
+ * @returns Current markup total
167
+ */
168
+ getMarkup(): number;
169
+ private summarize;
50
170
  /**
51
- * Save markup to file
52
- * @param markup
171
+ * The credit state, created on first use: from the old single balance file
172
+ * when there is one (it was all purchased credit, as far as anyone knew),
173
+ * otherwise STARTING_BALANCE.
53
174
  */
175
+ private readState;
176
+ private readLegacyBalance;
177
+ /** Writes the state through a temporary file, so a crash never leaves half a file. */
178
+ private writeState;
179
+ private appendUsage;
180
+ /** Ledger lines from `from` to `to` inclusive, oldest first. */
181
+ private readLedger;
54
182
  private saveMarkup;
55
183
  }