convex-weflayr 0.0.0-stage → 0.1.0
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/LICENSE +201 -0
- package/README.md +345 -2
- package/dist/ai-sdk/index.d.ts +4 -0
- package/dist/ai-sdk/index.d.ts.map +1 -0
- package/dist/ai-sdk/index.js +7 -0
- package/dist/ai-sdk/index.js.map +1 -0
- package/dist/client/index.d.ts +216 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +255 -0
- package/dist/client/index.js.map +1 -0
- package/dist/component/_generated/api.d.ts +34 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +166 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +133 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +80 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/convex.config.d.ts +3 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +3 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/lib.d.ts +142 -0
- package/dist/component/lib.d.ts.map +1 -0
- package/dist/component/lib.js +359 -0
- package/dist/component/lib.js.map +1 -0
- package/dist/component/schema.d.ts +81 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +47 -0
- package/dist/component/schema.js.map +1 -0
- package/package.json +115 -4
- package/src/ai-sdk/index.ts +8 -0
- package/src/client/index.ts +388 -0
- package/src/component/_generated/api.ts +50 -0
- package/src/component/_generated/component.ts +201 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +169 -0
- package/src/component/convex.config.ts +3 -0
- package/src/component/lib.ts +404 -0
- package/src/component/schema.ts +59 -0
- package/src/test.ts +18 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,346 @@
|
|
|
1
|
-
#
|
|
1
|
+
# convex-weflayr
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A Convex component that sends your app's model calls through the
|
|
4
|
+
[Weflayr](https://weflayr.com) gateway, and saves what each call cost, per
|
|
5
|
+
customer, in your own database.
|
|
6
|
+
|
|
7
|
+
Weflayr can show cost, run benchmarks and compare models, but only if every
|
|
8
|
+
request says which feature, which call and which customer it belongs to. In a
|
|
9
|
+
Convex app you would build that header by hand on every call, and the cost data
|
|
10
|
+
would stay in Weflayr's dashboard, out of reach of your own code. This component
|
|
11
|
+
adds the header for you and writes each call's usage into tables your app can
|
|
12
|
+
query. So your app can act on cost: show a customer their usage, warn at 80% of
|
|
13
|
+
a plan, stop at 100%.
|
|
14
|
+
|
|
15
|
+
```mermaid
|
|
16
|
+
flowchart LR
|
|
17
|
+
A["Convex action<br/>(your AI feature)"] -->|"1. model call + header<br/>feature, call, customer"| G["Weflayr gateway"]
|
|
18
|
+
G -->|"2. answer + usage"| A
|
|
19
|
+
A -->|"3. track() saves the call"| C[("convex-weflayr tables<br/>calls, daily totals, budgets")]
|
|
20
|
+
C -->|"4. cost per customer,<br/>call, feature, day"| Q["Your queries and UI"]
|
|
21
|
+
C -->|"5. assertWithinBudget()"| A
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## What you get
|
|
25
|
+
|
|
26
|
+
- **Cost per customer.** Every call is saved with its customer, call name,
|
|
27
|
+
feature, plan, model, tokens, cost, latency and status. Totals by customer,
|
|
28
|
+
call, feature and day are one query away, and they update live in your UI.
|
|
29
|
+
- **Budgets.** Give a customer a monthly dollar budget. Their next call is
|
|
30
|
+
stopped before the model runs, and other customers carry on.
|
|
31
|
+
- **Agent runs.** Give every step of a run the same run id. You see what the run
|
|
32
|
+
cost, step by step, and you can stop it part way.
|
|
33
|
+
- **Cheaper models, with proof.** Each call has a fixed name, so Weflayr can
|
|
34
|
+
replay your real calls on other models and score the answers.
|
|
35
|
+
- **Calls that survive a gateway failure.** `withFallback()` can run the call
|
|
36
|
+
direct if the gateway fails. This is optional.
|
|
37
|
+
- **A Vercel AI SDK model** for the gateway, if you use the AI SDK.
|
|
38
|
+
|
|
39
|
+
## Try the demo
|
|
40
|
+
|
|
41
|
+
The `example/` folder is a small app that uses every part of the component. It
|
|
42
|
+
makes real calls, so you bring your own Weflayr key: sign up at
|
|
43
|
+
[app.weflayr.com](https://app.weflayr.com), create an API key, and paste it into
|
|
44
|
+
the app. You do not need a key from any model provider.
|
|
45
|
+
|
|
46
|
+
```sh
|
|
47
|
+
npm install
|
|
48
|
+
npx convex dev # asks you to log in, then creates a project
|
|
49
|
+
npm run dev
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
`npx convex dev` writes `CONVEX_URL` to `.env.local`. The Vite app reads
|
|
53
|
+
`VITE_CONVEX_URL`, so add a line with the same address:
|
|
54
|
+
|
|
55
|
+
```sh
|
|
56
|
+
VITE_CONVEX_URL=https://your-deployment.convex.cloud
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Open the app and paste your key. Then:
|
|
60
|
+
|
|
61
|
+
- **Send calls** sends one call, or runs a four-step agent and shows its cost
|
|
62
|
+
step by step.
|
|
63
|
+
- **Customers** shows spend per customer, call and day.
|
|
64
|
+
- **Budgets** sets a monthly budget and stops a customer who has used it.
|
|
65
|
+
- **Calls** lists every saved call.
|
|
66
|
+
|
|
67
|
+
### What it looks like
|
|
68
|
+
|
|
69
|
+
An agent run. Four steps share one run id, so the cost of the run is the sum of
|
|
70
|
+
its steps:
|
|
71
|
+
|
|
72
|
+

|
|
73
|
+
|
|
74
|
+
A budget. After three calls the customer has used $0.000021 of a $0.00002
|
|
75
|
+
budget, and the fourth call is stopped before the model runs:
|
|
76
|
+
|
|
77
|
+

|
|
78
|
+
|
|
79
|
+
Spend by customer and by call, and every saved call:
|
|
80
|
+
|
|
81
|
+

|
|
82
|
+
|
|
83
|
+

|
|
84
|
+
|
|
85
|
+
The same calls in the Weflayr dashboard, grouped by the feature names the
|
|
86
|
+
component sent:
|
|
87
|
+
|
|
88
|
+

|
|
89
|
+
|
|
90
|
+
About your key: it is saved in your browser's localStorage and sent with each
|
|
91
|
+
call so the call can be made. It is never saved in the database. Each browser
|
|
92
|
+
gets a random id, so you only see your own calls. The database holds the
|
|
93
|
+
customer name, call name, model, token counts, cost, latency and status of each
|
|
94
|
+
call, and no prompt or answer text.
|
|
95
|
+
|
|
96
|
+
## Install
|
|
97
|
+
|
|
98
|
+
```sh
|
|
99
|
+
npm install convex-weflayr
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Add the component to `convex/convex.config.ts`:
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
import { defineApp } from "convex/server";
|
|
106
|
+
import weflayr from "convex-weflayr/convex.config";
|
|
107
|
+
|
|
108
|
+
const app = defineApp();
|
|
109
|
+
app.use(weflayr);
|
|
110
|
+
|
|
111
|
+
export default app;
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Set your Weflayr key and the customers that may use the gateway:
|
|
115
|
+
|
|
116
|
+
```sh
|
|
117
|
+
npx convex env set WEFLAYR_API_KEY <your Weflayr key>
|
|
118
|
+
npx convex env set WEFLAYR_CUSTOMERS "*"
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
`WEFLAYR_CUSTOMERS` is a comma separated list of customer ids, or `*` for all.
|
|
122
|
+
When it is empty, no customer goes through the gateway.
|
|
123
|
+
|
|
124
|
+
## Use it
|
|
125
|
+
|
|
126
|
+
Create one `Weflayr` instance. Name your calls and the feature each one belongs
|
|
127
|
+
to. A benchmark in Weflayr is set up per call name, so keep the names stable.
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
import { Weflayr } from "convex-weflayr";
|
|
131
|
+
import { components } from "./_generated/api";
|
|
132
|
+
|
|
133
|
+
export const weflayr = new Weflayr(components.weflayr, {
|
|
134
|
+
features: {
|
|
135
|
+
"draft-reply": "support-replies",
|
|
136
|
+
"classify-ticket": "support-replies",
|
|
137
|
+
"summarise-thread": "thread-summaries",
|
|
138
|
+
},
|
|
139
|
+
// Dollars per million tokens, from your provider's price list.
|
|
140
|
+
prices: {
|
|
141
|
+
"claude-haiku-5-5": { inputPerMillion: 0.1, outputPerMillion: 0.5 },
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Call a model through the gateway with the AI SDK:
|
|
147
|
+
|
|
148
|
+
```ts
|
|
149
|
+
import { generateText } from "ai";
|
|
150
|
+
import { weflayrModel } from "convex-weflayr/ai-sdk";
|
|
151
|
+
|
|
152
|
+
const meta = { customer: workspace.name, plan: workspace.plan, call: "classify-ticket" };
|
|
153
|
+
|
|
154
|
+
const result = await weflayr.track(
|
|
155
|
+
ctx,
|
|
156
|
+
{ ...meta, model: "claude-haiku-5-5" },
|
|
157
|
+
() =>
|
|
158
|
+
generateText({
|
|
159
|
+
model: weflayrModel(weflayr, "claude-haiku-5-5"),
|
|
160
|
+
headers: weflayr.headers(meta),
|
|
161
|
+
prompt,
|
|
162
|
+
}),
|
|
163
|
+
);
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
`ctx` is any action context. `track` times the call and saves it. If the call
|
|
167
|
+
fails, it saves the failure and throws the error again. Without the AI SDK,
|
|
168
|
+
`weflayr.connection()` returns the base URL and key for any OpenAI compatible
|
|
169
|
+
client.
|
|
170
|
+
|
|
171
|
+
### Streaming
|
|
172
|
+
|
|
173
|
+
`track` reads token usage from the result of `generateText`. A `streamText`
|
|
174
|
+
result only has its usage when the stream ends, so record the call yourself in
|
|
175
|
+
`onFinish`:
|
|
176
|
+
|
|
177
|
+
```ts
|
|
178
|
+
const started = Date.now();
|
|
179
|
+
const result = streamText({
|
|
180
|
+
model: weflayrModel(weflayr, "claude-haiku-5-5"),
|
|
181
|
+
headers: weflayr.headers(meta),
|
|
182
|
+
prompt,
|
|
183
|
+
onFinish: async ({ usage }) => {
|
|
184
|
+
await weflayr.record(ctx, {
|
|
185
|
+
...meta,
|
|
186
|
+
model: "claude-haiku-5-5",
|
|
187
|
+
inputTokens: usage.inputTokens ?? 0,
|
|
188
|
+
outputTokens: usage.outputTokens ?? 0,
|
|
189
|
+
latencyMs: Date.now() - started,
|
|
190
|
+
status: "ok",
|
|
191
|
+
});
|
|
192
|
+
},
|
|
193
|
+
});
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### Keep a call alive if the gateway fails
|
|
197
|
+
|
|
198
|
+
`withFallback` tries the gateway first. If it fails, it runs the same call
|
|
199
|
+
direct, so your customer still gets an answer. You supply the direct call, with
|
|
200
|
+
your own provider key.
|
|
201
|
+
|
|
202
|
+
```ts
|
|
203
|
+
const result = await weflayr.withFallback(
|
|
204
|
+
ctx,
|
|
205
|
+
{ ...meta, model },
|
|
206
|
+
{
|
|
207
|
+
gateway: () => generateText({ model: weflayrModel(weflayr, model), headers: weflayr.headers(meta), prompt }),
|
|
208
|
+
direct: () => generateText({ model: yourProvider(model), prompt }),
|
|
209
|
+
},
|
|
210
|
+
);
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
The failed attempt is saved as `error` and the direct run as `fallback`. A
|
|
214
|
+
customer who is not on the allow list runs the direct call, and that call is not
|
|
215
|
+
saved.
|
|
216
|
+
|
|
217
|
+
### Group the calls of one job
|
|
218
|
+
|
|
219
|
+
Pass the same `runId` (`newRunId()` makes one) to every call of one job. It is
|
|
220
|
+
saved with each call and sent to Weflayr as the `run_id` tag.
|
|
221
|
+
`weflayr.runCost(ctx, runId)` returns the cost of the whole job and of each
|
|
222
|
+
step.
|
|
223
|
+
|
|
224
|
+
Weflayr also runs an MCP server at `https://mcp.weflayr.com/`, so an agent can
|
|
225
|
+
read cost and margin as a tool.
|
|
226
|
+
|
|
227
|
+
### Budgets
|
|
228
|
+
|
|
229
|
+
```ts
|
|
230
|
+
await weflayr.setBudget(ctx, "acme", 25); // 25 dollars a month, UTC calendar month
|
|
231
|
+
await weflayr.assertWithinBudget(ctx, "acme"); // throws WeflayrBudgetError once spent
|
|
232
|
+
const status = await weflayr.budget(ctx, "acme"); // { limitUsd, spentUsd, remainingUsd, allowed }
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
A customer without a budget always passes. Spend is the sum of saved cost for
|
|
236
|
+
the month. A call already running when the limit is reached can still finish.
|
|
237
|
+
|
|
238
|
+
### Read the numbers
|
|
239
|
+
|
|
240
|
+
```ts
|
|
241
|
+
const range = lastDays(30);
|
|
242
|
+
await weflayr.usageByCustomer(ctx, range);
|
|
243
|
+
await weflayr.usageByCall(ctx, range, "acme");
|
|
244
|
+
await weflayr.usageByFeature(ctx, range);
|
|
245
|
+
await weflayr.usageByDay(ctx, range, "acme");
|
|
246
|
+
await weflayr.recentCalls(ctx, { customer: "acme", limit: 20 });
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
Each row has `calls`, `errors`, `fallbacks`, `inputTokens`, `outputTokens`,
|
|
250
|
+
`costUsd` and `averageLatencyMs`. Inside a Convex query, call
|
|
251
|
+
`ctx.runQuery(components.weflayr.lib.usageByCustomer, range)` and the result
|
|
252
|
+
updates live in your UI.
|
|
253
|
+
|
|
254
|
+
## How cost is worked out
|
|
255
|
+
|
|
256
|
+
The gateway does not send a price back with each response, so the component
|
|
257
|
+
uses, in order:
|
|
258
|
+
|
|
259
|
+
1. A cost you pass: `costUsd` in the call details, or from `extract` in `track`.
|
|
260
|
+
Saved as `reported`.
|
|
261
|
+
2. An estimate from your `prices` table. Saved as `estimated`.
|
|
262
|
+
3. Zero, saved as `none`, when neither is available.
|
|
263
|
+
|
|
264
|
+
Weflayr's dashboard is the source of truth for what you were billed. Use the
|
|
265
|
+
saved cost to compare customers and enforce budgets, and fill `prices` from your
|
|
266
|
+
provider's price list.
|
|
267
|
+
|
|
268
|
+
## What is saved
|
|
269
|
+
|
|
270
|
+
| Table | One row per | Kept |
|
|
271
|
+
| --------- | -------------------------- | ------------------------------------------ |
|
|
272
|
+
| `calls` | model call | until you run `weflayr.purge(ctx, before)` |
|
|
273
|
+
| `daily` | customer, call and UTC day | always |
|
|
274
|
+
| `budgets` | customer | until cleared |
|
|
275
|
+
|
|
276
|
+
Queries read the `daily` rows, so they stay fast as `calls` grows. Days and
|
|
277
|
+
months are UTC.
|
|
278
|
+
|
|
279
|
+
## Notes
|
|
280
|
+
|
|
281
|
+
- Saving a call never fails the call. If the write fails, the component logs a
|
|
282
|
+
warning and returns your answer.
|
|
283
|
+
- The `plan`, `runId` and any `tags` you pass are sent to Weflayr as tags on the
|
|
284
|
+
request.
|
|
285
|
+
- Customer names go to Weflayr as written. Use an id or a name you are happy to
|
|
286
|
+
see in its dashboard.
|
|
287
|
+
|
|
288
|
+
## Limitations
|
|
289
|
+
|
|
290
|
+
- Cost is an estimate from your `prices` table unless you pass the cost of a
|
|
291
|
+
call.
|
|
292
|
+
- A budget compares saved cost with the limit before a call starts, so a call
|
|
293
|
+
already running can push a customer past it.
|
|
294
|
+
- A customer who is not on the allow list runs the direct call, and that call is
|
|
295
|
+
not saved.
|
|
296
|
+
- `usageByCustomer` and the other reports read one `daily` row per customer,
|
|
297
|
+
call and day in the range. With many customers, keep the range short or pass a
|
|
298
|
+
customer, so a query stays inside Convex's read limits.
|
|
299
|
+
- `track` cannot read usage from a stream. Record streamed calls in `onFinish`,
|
|
300
|
+
as shown under Streaming.
|
|
301
|
+
|
|
302
|
+
## Troubleshooting
|
|
303
|
+
|
|
304
|
+
**No calls are saved.** The customer is not allowed through the gateway. Set
|
|
305
|
+
`WEFLAYR_CUSTOMERS` to the customer id, or `*`, and check that
|
|
306
|
+
`WEFLAYR_API_KEY` is set in the same Convex deployment.
|
|
307
|
+
|
|
308
|
+
**Cost shows $0.** The model is missing from `prices`. Add it, in dollars per
|
|
309
|
+
million tokens.
|
|
310
|
+
|
|
311
|
+
**A budget never stops anyone.** A budget compares saved cost, so it needs
|
|
312
|
+
`prices`, or a `costUsd` on each call.
|
|
313
|
+
|
|
314
|
+
**The gateway returns 402.** Your Weflayr balance is empty. Add credit in the
|
|
315
|
+
Weflayr dashboard.
|
|
316
|
+
|
|
317
|
+
## Test
|
|
318
|
+
|
|
319
|
+
```ts
|
|
320
|
+
import { convexTest } from "convex-test";
|
|
321
|
+
import { register } from "convex-weflayr/test";
|
|
322
|
+
|
|
323
|
+
const t = convexTest(schema, modules);
|
|
324
|
+
register(t); // or register(t, "myName") if you named the component yourself
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
## Contributing
|
|
328
|
+
|
|
329
|
+
```sh
|
|
330
|
+
npm install
|
|
331
|
+
npm test
|
|
332
|
+
npm run lint
|
|
333
|
+
npm run typecheck
|
|
334
|
+
npm run build
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
See [CONTRIBUTING.md](./CONTRIBUTING.md). To publish, see
|
|
338
|
+
[PUBLISHING.md](./PUBLISHING.md).
|
|
339
|
+
|
|
340
|
+
## Changelog
|
|
341
|
+
|
|
342
|
+
See [CHANGELOG.md](./CHANGELOG.md).
|
|
343
|
+
|
|
344
|
+
## License
|
|
345
|
+
|
|
346
|
+
Apache-2.0
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Weflayr } from "../client/index.js";
|
|
2
|
+
/** A Vercel AI SDK model that sends its request through the Weflayr gateway. Pass `weflayr.headers(meta)` as the request headers. */
|
|
3
|
+
export declare function weflayrModel(weflayr: Weflayr, modelId: string): import("@ai-sdk/provider").LanguageModelV4;
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/ai-sdk/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAElD,qIAAqI;AACrI,wBAAgB,YAAY,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,8CAG7D"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { createOpenAI } from "@ai-sdk/openai";
|
|
2
|
+
/** A Vercel AI SDK model that sends its request through the Weflayr gateway. Pass `weflayr.headers(meta)` as the request headers. */
|
|
3
|
+
export function weflayrModel(weflayr, modelId) {
|
|
4
|
+
const { baseURL, apiKey } = weflayr.connection();
|
|
5
|
+
return createOpenAI({ baseURL, apiKey, name: "weflayr" }).chat(modelId);
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/ai-sdk/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAG9C,qIAAqI;AACrI,MAAM,UAAU,YAAY,CAAC,OAAgB,EAAE,OAAe;IAC5D,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC;IACjD,OAAO,YAAY,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;AAC1E,CAAC"}
|