@applica-software-guru/persona-sdk 0.1.113 → 0.1.115
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/README.md +86 -1
- package/dist/bundle.cjs.js +1 -1
- package/dist/bundle.cjs.js.map +1 -1
- package/dist/bundle.es.js +468 -315
- package/dist/bundle.es.js.map +1 -1
- package/dist/index.d.ts +339 -3
- package/package.json +1 -1
- package/src/agents/types.ts +6 -2
- package/src/billing/billing-client.ts +3 -0
- package/src/billing/types.ts +16 -0
- package/src/index.ts +27 -0
- package/src/model-aliases/model-aliases-api.ts +94 -0
- package/src/model-aliases/references.ts +20 -0
- package/src/model-aliases/types.ts +125 -0
- package/src/notifications/notifications-api.ts +42 -0
- package/src/notifications/types.ts +39 -0
- package/src/persona-sdk.ts +14 -0
- package/src/sessions/types.ts +2 -0
- package/src/workflows/channels-api.ts +61 -0
- package/src/workflows/types.ts +45 -0
- package/src/workflows/workflows-api.ts +5 -0
- package/tests/billing-aliases.test.mjs +71 -0
- package/tests/channels-api.test.mjs +55 -0
- package/tests/model-aliases-api.test.mjs +198 -0
- package/tests/notifications-api.test.mjs +88 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** Prefix of a model name that references a model alias. */
|
|
2
|
+
export const ALIAS_PREFIX = 'alias:';
|
|
3
|
+
|
|
4
|
+
/** True when `modelName` is an `alias:<name>` reference. */
|
|
5
|
+
export function isAliasRef(modelName?: string | null): boolean {
|
|
6
|
+
return !!modelName && modelName.trim().toLowerCase().startsWith(ALIAS_PREFIX);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** `"alias:smart"` -> `"smart"`. Throws when `modelName` is not a reference. */
|
|
10
|
+
export function aliasNameFromRef(modelName: string): string {
|
|
11
|
+
if (!isAliasRef(modelName)) {
|
|
12
|
+
throw new Error(`Not a model alias reference: ${JSON.stringify(modelName)}`);
|
|
13
|
+
}
|
|
14
|
+
return modelName.trim().slice(ALIAS_PREFIX.length).trim();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** `"smart"` -> `"alias:smart"`, to set as the `modelName` of an agent. */
|
|
18
|
+
export function aliasRef(name: string): string {
|
|
19
|
+
return ALIAS_PREFIX + name.trim();
|
|
20
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import type { ReasoningEffort } from '../agents/types';
|
|
2
|
+
|
|
3
|
+
/** Applied only to the fields an agent leaves empty. `temperature` is not here on purpose. */
|
|
4
|
+
export interface ModelAliasDefaults {
|
|
5
|
+
reasoningEffort?: ReasoningEffort | null;
|
|
6
|
+
maxOutputTokens?: number | null;
|
|
7
|
+
contextWindow?: number | null;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export type ModelAliasHealthStatus = 'unknown' | 'ok' | 'deprecating' | 'missing' | 'no_credentials';
|
|
11
|
+
export type ModelAliasIssueRole = 'target' | 'fallback';
|
|
12
|
+
|
|
13
|
+
/** A problem found by the health check on one of the alias' models. */
|
|
14
|
+
export interface ModelAliasIssue {
|
|
15
|
+
model: string;
|
|
16
|
+
role: ModelAliasIssueRole;
|
|
17
|
+
status: ModelAliasHealthStatus;
|
|
18
|
+
detail?: string | null;
|
|
19
|
+
/** Provider-announced retirement date (`YYYY-MM-DD`), when known. */
|
|
20
|
+
deprecationDate?: string | null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface ModelAliasHealth {
|
|
24
|
+
status: ModelAliasHealthStatus;
|
|
25
|
+
checkedAt?: string | null;
|
|
26
|
+
issues: ModelAliasIssue[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Audit entry: one changed field of the alias (the last 50 are kept). */
|
|
30
|
+
export interface ModelAliasChange {
|
|
31
|
+
at: string;
|
|
32
|
+
by?: string | null;
|
|
33
|
+
field: 'target' | 'fallbacks' | 'defaults';
|
|
34
|
+
old?: unknown;
|
|
35
|
+
new?: unknown;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* A named indirection to a concrete `<provider>/<model>`. An agent references it as
|
|
40
|
+
* `alias:<name>` (see {@link aliasRef}); editing the alias moves every agent using it.
|
|
41
|
+
*/
|
|
42
|
+
export interface ModelAlias {
|
|
43
|
+
id: string;
|
|
44
|
+
/** Lowercase slug used in `alias:<name>`. Immutable. */
|
|
45
|
+
name: string;
|
|
46
|
+
/** null => global (super admins only); set => per-project. Immutable. */
|
|
47
|
+
projectId?: string | null;
|
|
48
|
+
description?: string | null;
|
|
49
|
+
/** Concrete litellm-style `<provider>/<model>`, never another alias. */
|
|
50
|
+
target: string;
|
|
51
|
+
/** Ordered alternatives, used at runtime when the target cannot serve a call. */
|
|
52
|
+
fallbacks: string[];
|
|
53
|
+
defaults: ModelAliasDefaults;
|
|
54
|
+
/** Result of the last health check. Managed by the server. */
|
|
55
|
+
health: ModelAliasHealth;
|
|
56
|
+
/** Managed by the server. */
|
|
57
|
+
history: ModelAliasChange[];
|
|
58
|
+
createdAt?: string;
|
|
59
|
+
updatedAt?: string;
|
|
60
|
+
updatedBy?: string | null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* What to send to create or update an alias. The server-managed fields (`health`,
|
|
65
|
+
* `history`, timestamps) are not part of it; `name` is required but immutable, and
|
|
66
|
+
* the scope can only be chosen (via `projectId`) by super admins when creating.
|
|
67
|
+
*/
|
|
68
|
+
export interface ModelAliasInput {
|
|
69
|
+
name: string;
|
|
70
|
+
target: string;
|
|
71
|
+
projectId?: string | null;
|
|
72
|
+
description?: string | null;
|
|
73
|
+
fallbacks?: string[];
|
|
74
|
+
defaults?: ModelAliasDefaults;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** An agent that references an alias, and in which model slots. */
|
|
78
|
+
export interface AliasUsage {
|
|
79
|
+
agentId: string;
|
|
80
|
+
agentName?: string | null;
|
|
81
|
+
projectId?: string | null;
|
|
82
|
+
/** `model`, `compaction`, `loopRecovery`, `attachmentsPreprocessor`, `knowledge`. */
|
|
83
|
+
usedIn: string[];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Agents referencing an alias; also the detail of the 409 returned when deleting one in use. */
|
|
87
|
+
export interface AliasImpact {
|
|
88
|
+
aliasId: string;
|
|
89
|
+
aliasName: string;
|
|
90
|
+
/** May exceed `usages.length` when the list is capped. */
|
|
91
|
+
total: number;
|
|
92
|
+
usages: AliasUsage[];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export type ModelCapability = 'function_calling' | 'vision' | 'audio_input' | 'pdf_input' | 'reasoning';
|
|
96
|
+
|
|
97
|
+
/** What litellm's registry knows about a model. */
|
|
98
|
+
export interface ModelProfile {
|
|
99
|
+
model: string;
|
|
100
|
+
contextWindow?: number | null;
|
|
101
|
+
/** USD per million tokens. */
|
|
102
|
+
inputCostPerMillion?: number | null;
|
|
103
|
+
outputCostPerMillion?: number | null;
|
|
104
|
+
capabilities: ModelCapability[];
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** A candidate to replace a model, or to add as a fallback. */
|
|
108
|
+
export interface ModelSuggestion extends ModelProfile {
|
|
109
|
+
provider: string;
|
|
110
|
+
sameProvider: boolean;
|
|
111
|
+
/** e.g. a smaller context window, a much higher price. */
|
|
112
|
+
warnings: string[];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** A proposal only: nothing is applied, the alias is edited by hand. */
|
|
116
|
+
export interface AliasSuggestions {
|
|
117
|
+
aliasId?: string | null;
|
|
118
|
+
/** The model the suggestions are measured against (the alias' target by default). */
|
|
119
|
+
referenceModel: string;
|
|
120
|
+
/** null when the registry does not know the reference model (e.g. already retired). */
|
|
121
|
+
reference?: ModelProfile | null;
|
|
122
|
+
notes: string[];
|
|
123
|
+
replacements: ModelSuggestion[];
|
|
124
|
+
fallbacks: ModelSuggestion[];
|
|
125
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { HttpApi } from '../http-api';
|
|
2
|
+
import { Paginated } from '../paginated';
|
|
3
|
+
import { AuthenticationProvider } from '../auth/authentication-provider';
|
|
4
|
+
import { Notification, NotificationListParams } from './types';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* In-platform notifications. A project sees its own; platform-wide ones (no project)
|
|
8
|
+
* are for super admins. Agent API keys have no access. Read state is shared by
|
|
9
|
+
* everyone who can see a notification.
|
|
10
|
+
*/
|
|
11
|
+
export class NotificationsApi extends HttpApi {
|
|
12
|
+
constructor(baseUrl: string, auth: string | AuthenticationProvider) {
|
|
13
|
+
super(baseUrl, auth);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async list(params: NotificationListParams = {}): Promise<Paginated<Notification>> {
|
|
17
|
+
const { page, size } = this.normalizePageParams(params.page, params.size);
|
|
18
|
+
const query: Record<string, unknown> = { page, size };
|
|
19
|
+
if (params.unreadOnly) query.unreadOnly = true;
|
|
20
|
+
if (params.includeResolved) query.includeResolved = true;
|
|
21
|
+
if (params.projectId) query.projectId = params.projectId;
|
|
22
|
+
return this.httpGet<Paginated<Notification>>('/notifications', query);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Open, unread notifications: the value of the badge. */
|
|
26
|
+
async unreadCount(projectId?: string | null): Promise<number> {
|
|
27
|
+
const result = await this.httpGet<{ count: number }>(
|
|
28
|
+
'/notifications/unread-count',
|
|
29
|
+
projectId ? { projectId } : undefined
|
|
30
|
+
);
|
|
31
|
+
return result.count;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async markRead(id: string): Promise<Notification> {
|
|
35
|
+
return this.httpPost<Notification>('/notifications/' + id + '/read');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Mark as read everything the caller can see. */
|
|
39
|
+
async markAllRead(projectId?: string | null): Promise<void> {
|
|
40
|
+
await this.httpPost<void>('/notifications/read-all', undefined, projectId ? { projectId } : undefined);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export type NotificationSeverity = 'info' | 'warning' | 'critical';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* An in-platform notification (badge + banner). `title` and `message` are an English
|
|
5
|
+
* fallback: clients localize from `code` and `data`.
|
|
6
|
+
*/
|
|
7
|
+
export interface Notification {
|
|
8
|
+
id: string;
|
|
9
|
+
/** null => platform-wide, visible to super admins only. */
|
|
10
|
+
projectId?: string | null;
|
|
11
|
+
/** Producer area, e.g. `model_alias`. */
|
|
12
|
+
type: string;
|
|
13
|
+
/** Stable machine code, e.g. `model_alias.missing`, `model_alias.runtime_fallback`. */
|
|
14
|
+
code: string;
|
|
15
|
+
severity: NotificationSeverity;
|
|
16
|
+
title: string;
|
|
17
|
+
message: string;
|
|
18
|
+
/** What it is about, for deep links. */
|
|
19
|
+
entityType?: string | null;
|
|
20
|
+
entityId?: string | null;
|
|
21
|
+
dedupeKey?: string | null;
|
|
22
|
+
data: Record<string, unknown>;
|
|
23
|
+
createdAt?: string;
|
|
24
|
+
readAt?: string | null;
|
|
25
|
+
readBy?: string | null;
|
|
26
|
+
/** Set when the problem it describes no longer applies. */
|
|
27
|
+
resolvedAt?: string | null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface NotificationListParams {
|
|
31
|
+
/** Only the ones not read yet. */
|
|
32
|
+
unreadOnly?: boolean;
|
|
33
|
+
/** Include the ones whose problem is gone (hidden by default). */
|
|
34
|
+
includeResolved?: boolean;
|
|
35
|
+
/** Super admins: the view of this project (plus platform-wide ones). */
|
|
36
|
+
projectId?: string | null;
|
|
37
|
+
page?: number;
|
|
38
|
+
size?: number;
|
|
39
|
+
}
|
package/src/persona-sdk.ts
CHANGED
|
@@ -13,6 +13,8 @@ import { SessionsApi } from './sessions/sessions-api';
|
|
|
13
13
|
import { MissionsApi } from './missions/missions-api';
|
|
14
14
|
import { BillingClient } from './billing/billing-client';
|
|
15
15
|
import { ValuesApi } from './values/values-api';
|
|
16
|
+
import { ModelAliasesApi } from './model-aliases/model-aliases-api';
|
|
17
|
+
import { NotificationsApi } from './notifications/notifications-api';
|
|
16
18
|
|
|
17
19
|
export class PersonaSdk {
|
|
18
20
|
private readonly baseUrl: string;
|
|
@@ -103,6 +105,18 @@ export class PersonaSdk {
|
|
|
103
105
|
return new BillingClient(this.billingBaseUrl, auth);
|
|
104
106
|
}
|
|
105
107
|
|
|
108
|
+
modelAliases(apiKey: string): ModelAliasesApi;
|
|
109
|
+
modelAliases(authProvider: AuthenticationProvider): ModelAliasesApi;
|
|
110
|
+
modelAliases(auth: string | AuthenticationProvider): ModelAliasesApi {
|
|
111
|
+
return new ModelAliasesApi(this.baseUrl, auth);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
notifications(apiKey: string): NotificationsApi;
|
|
115
|
+
notifications(authProvider: AuthenticationProvider): NotificationsApi;
|
|
116
|
+
notifications(auth: string | AuthenticationProvider): NotificationsApi {
|
|
117
|
+
return new NotificationsApi(this.baseUrl, auth);
|
|
118
|
+
}
|
|
119
|
+
|
|
106
120
|
values(apiKey: string): ValuesApi;
|
|
107
121
|
values(authProvider: AuthenticationProvider): ValuesApi;
|
|
108
122
|
values(auth: string | AuthenticationProvider): ValuesApi {
|
package/src/sessions/types.ts
CHANGED
|
@@ -84,6 +84,8 @@ export interface UsageCost {
|
|
|
84
84
|
price?: number;
|
|
85
85
|
credits?: number;
|
|
86
86
|
unit?: string;
|
|
87
|
+
/** Name of the model alias the usage went through, if any. `service` names the concrete model that served it. */
|
|
88
|
+
alias?: string | null;
|
|
87
89
|
}
|
|
88
90
|
|
|
89
91
|
export interface CommittedUsage {
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { HttpApi } from '../http-api';
|
|
2
|
+
import { AuthenticationProvider } from '../auth/authentication-provider';
|
|
3
|
+
import { ChannelIdentity, ChannelLink, ChannelProvider, NotificationChannel } from './types';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Notification channels: where the project's human tasks are announced and
|
|
7
|
+
* can be answered outside the inbox (a Telegram group, one message per
|
|
8
|
+
* task). Linking goes through one-shot deep links returned by the service.
|
|
9
|
+
* Identity endpoints require a user (IAM bearer) session. Lives on the
|
|
10
|
+
* workflows service.
|
|
11
|
+
*/
|
|
12
|
+
export class ChannelsApi extends HttpApi {
|
|
13
|
+
constructor(baseUrl: string, auth: string | AuthenticationProvider) {
|
|
14
|
+
super(baseUrl, auth);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** The providers enabled on the server (e.g. the Telegram bot). */
|
|
18
|
+
async providers(): Promise<ChannelProvider[]> {
|
|
19
|
+
return this.httpGet<ChannelProvider[]>('/channels/providers');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The project's channels, with their setup status. */
|
|
23
|
+
async list(): Promise<NotificationChannel[]> {
|
|
24
|
+
return this.httpGet<NotificationChannel[]>('/channels');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** One-shot deep link that links a group to the project. */
|
|
28
|
+
async createLink(provider = 'telegram'): Promise<ChannelLink> {
|
|
29
|
+
return this.httpPost<ChannelLink>('/channels/' + provider + '/link-token');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async update(
|
|
33
|
+
channelId: string,
|
|
34
|
+
changes: { includePayload?: boolean; locale?: string }
|
|
35
|
+
): Promise<NotificationChannel> {
|
|
36
|
+
return this.httpPatch<NotificationChannel>('/channels/' + channelId, changes);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Re-checks the channel setup (the bot can still post in the group). */
|
|
40
|
+
async verify(channelId: string): Promise<NotificationChannel> {
|
|
41
|
+
return this.httpPost<NotificationChannel>('/channels/' + channelId + '/verify');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async unlink(channelId: string): Promise<void> {
|
|
45
|
+
await this.httpDelete('/channels/' + channelId);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The caller's linked accounts. */
|
|
49
|
+
async myIdentities(): Promise<ChannelIdentity[]> {
|
|
50
|
+
return this.httpGet<ChannelIdentity[]>('/channels/identities/me');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** One-shot deep link that links the caller's account. */
|
|
54
|
+
async createIdentityLink(provider = 'telegram'): Promise<ChannelLink> {
|
|
55
|
+
return this.httpPost<ChannelLink>('/channels/identities/' + provider + '/link-token');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async unlinkIdentity(provider = 'telegram'): Promise<void> {
|
|
59
|
+
await this.httpDelete('/channels/identities/me/' + provider);
|
|
60
|
+
}
|
|
61
|
+
}
|
package/src/workflows/types.ts
CHANGED
|
@@ -152,6 +152,8 @@ export interface HumanTask {
|
|
|
152
152
|
origin?: 'node' | 'agent_escalation';
|
|
153
153
|
question?: string;
|
|
154
154
|
payload?: Record<string, unknown>;
|
|
155
|
+
/** Closed set of answers (buttons); absent = free-text answer. */
|
|
156
|
+
options?: string[];
|
|
155
157
|
assignee?: string;
|
|
156
158
|
role?: string;
|
|
157
159
|
status: string;
|
|
@@ -163,6 +165,49 @@ export interface HumanTask {
|
|
|
163
165
|
sessionCode?: string;
|
|
164
166
|
}
|
|
165
167
|
|
|
168
|
+
export type ChannelStatus = 'active' | 'incomplete' | 'disconnected';
|
|
169
|
+
|
|
170
|
+
/** A place where a project's human tasks are announced (e.g. a Telegram group). */
|
|
171
|
+
export interface NotificationChannel {
|
|
172
|
+
id: string;
|
|
173
|
+
projectId: string;
|
|
174
|
+
provider: string;
|
|
175
|
+
chatId: string;
|
|
176
|
+
title?: string;
|
|
177
|
+
status: ChannelStatus;
|
|
178
|
+
/** Why the channel is not active (e.g. `removed`, `cannot_post`). */
|
|
179
|
+
statusDetail?: string;
|
|
180
|
+
includePayload: boolean;
|
|
181
|
+
locale: string;
|
|
182
|
+
linkedBy?: string;
|
|
183
|
+
createdAt?: string;
|
|
184
|
+
updatedAt?: string;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** An external account linked to the current user. */
|
|
188
|
+
export interface ChannelIdentity {
|
|
189
|
+
id: string;
|
|
190
|
+
provider: string;
|
|
191
|
+
externalUserId: string;
|
|
192
|
+
externalUsername?: string;
|
|
193
|
+
iamUserId: string;
|
|
194
|
+
iamUserEmail?: string;
|
|
195
|
+
createdAt?: string;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export interface ChannelProvider {
|
|
199
|
+
provider: string;
|
|
200
|
+
botUsername?: string;
|
|
201
|
+
ready: boolean;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** One-shot deep link completing a group or account link. */
|
|
205
|
+
export interface ChannelLink {
|
|
206
|
+
token: string;
|
|
207
|
+
url: string;
|
|
208
|
+
expiresAt: string;
|
|
209
|
+
}
|
|
210
|
+
|
|
166
211
|
export interface Workflow {
|
|
167
212
|
id?: string;
|
|
168
213
|
projectId?: string;
|
|
@@ -5,6 +5,7 @@ import { Revision } from '../revisions/types';
|
|
|
5
5
|
import { Workflow } from './types';
|
|
6
6
|
import { WorkflowExecutionsApi } from './workflow-executions-api';
|
|
7
7
|
import { HumanTasksApi } from './human-tasks-api';
|
|
8
|
+
import { ChannelsApi } from './channels-api';
|
|
8
9
|
|
|
9
10
|
export class WorkflowsApi extends HttpApi {
|
|
10
11
|
constructor(baseUrl: string, auth: string | AuthenticationProvider) {
|
|
@@ -43,6 +44,10 @@ export class WorkflowsApi extends HttpApi {
|
|
|
43
44
|
return new HumanTasksApi(this.getBaseUrl(), this.getAuthProvider());
|
|
44
45
|
}
|
|
45
46
|
|
|
47
|
+
channels(): ChannelsApi {
|
|
48
|
+
return new ChannelsApi(this.getBaseUrl(), this.getAuthProvider());
|
|
49
|
+
}
|
|
50
|
+
|
|
46
51
|
async listRevisions(workflowId: string): Promise<Revision[]> {
|
|
47
52
|
return this.httpGet<Revision[]>('/workflows/' + workflowId + '/revisions');
|
|
48
53
|
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import test from 'node:test';
|
|
3
|
+
|
|
4
|
+
import { BillingClient } from '../dist/bundle.es.js';
|
|
5
|
+
|
|
6
|
+
function recordFetch(context, body) {
|
|
7
|
+
const originalFetch = globalThis.fetch;
|
|
8
|
+
context.after(() => {
|
|
9
|
+
globalThis.fetch = originalFetch;
|
|
10
|
+
});
|
|
11
|
+
const calls = [];
|
|
12
|
+
globalThis.fetch = async (url, init) => {
|
|
13
|
+
calls.push({ url: new URL(String(url)), method: init.method });
|
|
14
|
+
return new Response(JSON.stringify(body), { status: 200, headers: { 'Content-Type': 'application/json' } });
|
|
15
|
+
};
|
|
16
|
+
return calls;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const client = () => new BillingClient('https://billing.test', 'api-key');
|
|
20
|
+
|
|
21
|
+
test('usage stats carry the breakdown by alias', async (context) => {
|
|
22
|
+
const stats = {
|
|
23
|
+
totals: { creditsUsed: 5100, usageCount: 15 },
|
|
24
|
+
daily: [],
|
|
25
|
+
byModel: [],
|
|
26
|
+
byAlias: [
|
|
27
|
+
{ alias: 'smart', credits: 5000, count: 12, totalCost: 0.5, totalPrice: 5 },
|
|
28
|
+
{ alias: 'cheap', credits: 100, count: 3 },
|
|
29
|
+
],
|
|
30
|
+
availableModels: [],
|
|
31
|
+
};
|
|
32
|
+
const calls = recordFetch(context, stats);
|
|
33
|
+
|
|
34
|
+
const result = await client().getUsageStats('p1', '2026-11-01', '2026-11-30');
|
|
35
|
+
|
|
36
|
+
assert.equal(calls[0].url.pathname, '/projects/p1/subscription/usage-stats');
|
|
37
|
+
assert.deepEqual(result.byAlias, stats.byAlias);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('listUsages filters by alias, one parameter per alias', async (context) => {
|
|
41
|
+
const calls = recordFetch(context, { content: [], totalElements: 0, totalPages: 0, page: 0, size: 20 });
|
|
42
|
+
|
|
43
|
+
await client().listUsages('p1', undefined, undefined, undefined, undefined, 0, 20, ['smart', 'my alias']);
|
|
44
|
+
|
|
45
|
+
assert.equal(calls[0].url.pathname, '/projects/p1/subscription/usages');
|
|
46
|
+
assert.deepEqual(calls[0].url.searchParams.getAll('alias'), ['smart', 'my alias']);
|
|
47
|
+
assert.equal(calls[0].url.searchParams.get('page'), '0');
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test('listUsages without aliases sends no alias parameter', async (context) => {
|
|
51
|
+
const calls = recordFetch(context, { content: [], totalElements: 0, totalPages: 0, page: 0, size: 20 });
|
|
52
|
+
|
|
53
|
+
await client().listUsages('p1', undefined, undefined, ['gpt-5']);
|
|
54
|
+
await client().listUsages('p1', undefined, undefined, undefined, undefined, undefined, undefined, []);
|
|
55
|
+
|
|
56
|
+
for (const call of calls) assert.equal(call.url.searchParams.has('alias'), false);
|
|
57
|
+
assert.deepEqual(calls[0].url.searchParams.getAll('model'), ['gpt-5']);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('usage details keep the alias a line went through', async (context) => {
|
|
61
|
+
const usage = {
|
|
62
|
+
id: 'u1',
|
|
63
|
+
creditsUsed: 200,
|
|
64
|
+
details: [{ service: 'model.openai.gpt-5.prompt_tokens', credits: 200, unit: 'tokens', alias: 'smart' }],
|
|
65
|
+
};
|
|
66
|
+
recordFetch(context, { content: [usage], totalElements: 1, totalPages: 1, page: 0, size: 20 });
|
|
67
|
+
|
|
68
|
+
const page = await client().listUsages('p1', undefined, undefined, undefined, undefined, 0, 20, ['smart']);
|
|
69
|
+
|
|
70
|
+
assert.equal(page.content[0].details[0].alias, 'smart');
|
|
71
|
+
});
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import test from 'node:test';
|
|
3
|
+
|
|
4
|
+
import { ChannelsApi } from '../dist/bundle.es.js';
|
|
5
|
+
|
|
6
|
+
function jsonResponse(body) {
|
|
7
|
+
return new Response(JSON.stringify(body), {
|
|
8
|
+
status: 200,
|
|
9
|
+
headers: { 'Content-Type': 'application/json' },
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function recordFetch(context, body) {
|
|
14
|
+
const originalFetch = globalThis.fetch;
|
|
15
|
+
context.after(() => {
|
|
16
|
+
globalThis.fetch = originalFetch;
|
|
17
|
+
});
|
|
18
|
+
const calls = [];
|
|
19
|
+
globalThis.fetch = async (url, init) => {
|
|
20
|
+
calls.push({ url: String(url), method: init.method, body: init.body ? JSON.parse(init.body) : undefined });
|
|
21
|
+
return jsonResponse(body);
|
|
22
|
+
};
|
|
23
|
+
return calls;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
test('createLink asks the workflows service for a group deep link', async (context) => {
|
|
27
|
+
const link = { token: 't', url: 'https://t.me/bot?startgroup=t', expiresAt: '2026-10-28T10:00:00' };
|
|
28
|
+
const calls = recordFetch(context, link);
|
|
29
|
+
|
|
30
|
+
const api = new ChannelsApi('https://workflows.test', 'api-key');
|
|
31
|
+
const result = await api.createLink();
|
|
32
|
+
|
|
33
|
+
assert.equal(calls[0].method, 'POST');
|
|
34
|
+
assert.equal(calls[0].url, 'https://workflows.test/channels/telegram/link-token');
|
|
35
|
+
assert.equal(result.url, link.url);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('identity links and settings use the expected endpoints', async (context) => {
|
|
39
|
+
const calls = recordFetch(context, {});
|
|
40
|
+
const api = new ChannelsApi('https://workflows.test', 'api-key');
|
|
41
|
+
|
|
42
|
+
await api.createIdentityLink();
|
|
43
|
+
await api.update('wf-chan_1', { includePayload: false });
|
|
44
|
+
await api.verify('wf-chan_1');
|
|
45
|
+
|
|
46
|
+
assert.deepEqual(
|
|
47
|
+
calls.map((call) => `${call.method} ${call.url}`),
|
|
48
|
+
[
|
|
49
|
+
'POST https://workflows.test/channels/identities/telegram/link-token',
|
|
50
|
+
'PATCH https://workflows.test/channels/wf-chan_1',
|
|
51
|
+
'POST https://workflows.test/channels/wf-chan_1/verify',
|
|
52
|
+
]
|
|
53
|
+
);
|
|
54
|
+
assert.deepEqual(calls[1].body, { includePayload: false });
|
|
55
|
+
});
|