@positronic/cloudflare 0.0.67 → 0.0.69

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 (37) hide show
  1. package/dist/src/api/auth-middleware.js +25 -0
  2. package/dist/src/api/brains.js +67 -16
  3. package/dist/src/api/secrets.js +3 -0
  4. package/dist/src/brain-runner-do.js +51 -5
  5. package/dist/src/create-r2-store.js +319 -0
  6. package/dist/src/dev-server.js +19 -1
  7. package/dist/src/governor-client-wrapper.js +364 -0
  8. package/dist/src/governor-do.js +387 -0
  9. package/dist/src/index.js +3 -0
  10. package/dist/src/monitor-do.js +25 -12
  11. package/dist/src/rate-limit-headers.js +199 -0
  12. package/dist/src/schedule-do.js +38 -22
  13. package/dist/src/token-estimator.js +39 -0
  14. package/dist/types/api/auth-middleware.d.ts +5 -0
  15. package/dist/types/api/auth-middleware.d.ts.map +1 -1
  16. package/dist/types/api/brains.d.ts.map +1 -1
  17. package/dist/types/api/secrets.d.ts.map +1 -1
  18. package/dist/types/brain-runner-do.d.ts +9 -1
  19. package/dist/types/brain-runner-do.d.ts.map +1 -1
  20. package/dist/types/create-r2-store.d.ts +14 -0
  21. package/dist/types/create-r2-store.d.ts.map +1 -0
  22. package/dist/types/dev-server.d.ts.map +1 -1
  23. package/dist/types/governor-client-wrapper.d.ts +15 -0
  24. package/dist/types/governor-client-wrapper.d.ts.map +1 -0
  25. package/dist/types/governor-do.d.ts +25 -0
  26. package/dist/types/governor-do.d.ts.map +1 -0
  27. package/dist/types/index.d.ts +3 -0
  28. package/dist/types/index.d.ts.map +1 -1
  29. package/dist/types/monitor-do.d.ts +13 -4
  30. package/dist/types/monitor-do.d.ts.map +1 -1
  31. package/dist/types/rate-limit-headers.d.ts +10 -0
  32. package/dist/types/rate-limit-headers.d.ts.map +1 -0
  33. package/dist/types/schedule-do.d.ts +11 -3
  34. package/dist/types/schedule-do.d.ts.map +1 -1
  35. package/dist/types/token-estimator.d.ts +9 -0
  36. package/dist/types/token-estimator.d.ts.map +1 -0
  37. package/package.json +5 -4
@@ -0,0 +1,364 @@
1
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
2
+ try {
3
+ var info = gen[key](arg);
4
+ var value = info.value;
5
+ } catch (error) {
6
+ reject(error);
7
+ return;
8
+ }
9
+ if (info.done) {
10
+ resolve(value);
11
+ } else {
12
+ Promise.resolve(value).then(_next, _throw);
13
+ }
14
+ }
15
+ function _async_to_generator(fn) {
16
+ return function() {
17
+ var self = this, args = arguments;
18
+ return new Promise(function(resolve, reject) {
19
+ var gen = fn.apply(self, args);
20
+ function _next(value) {
21
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
22
+ }
23
+ function _throw(err) {
24
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
25
+ }
26
+ _next(undefined);
27
+ });
28
+ };
29
+ }
30
+ function _ts_generator(thisArg, body) {
31
+ var f, y, t, _ = {
32
+ label: 0,
33
+ sent: function() {
34
+ if (t[0] & 1) throw t[1];
35
+ return t[1];
36
+ },
37
+ trys: [],
38
+ ops: []
39
+ }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
40
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
41
+ return this;
42
+ }), g;
43
+ function verb(n) {
44
+ return function(v) {
45
+ return step([
46
+ n,
47
+ v
48
+ ]);
49
+ };
50
+ }
51
+ function step(op) {
52
+ if (f) throw new TypeError("Generator is already executing.");
53
+ while(g && (g = 0, op[0] && (_ = 0)), _)try {
54
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
55
+ if (y = 0, t) op = [
56
+ op[0] & 2,
57
+ t.value
58
+ ];
59
+ switch(op[0]){
60
+ case 0:
61
+ case 1:
62
+ t = op;
63
+ break;
64
+ case 4:
65
+ _.label++;
66
+ return {
67
+ value: op[1],
68
+ done: false
69
+ };
70
+ case 5:
71
+ _.label++;
72
+ y = op[1];
73
+ op = [
74
+ 0
75
+ ];
76
+ continue;
77
+ case 7:
78
+ op = _.ops.pop();
79
+ _.trys.pop();
80
+ continue;
81
+ default:
82
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
83
+ _ = 0;
84
+ continue;
85
+ }
86
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
87
+ _.label = op[1];
88
+ break;
89
+ }
90
+ if (op[0] === 6 && _.label < t[1]) {
91
+ _.label = t[1];
92
+ t = op;
93
+ break;
94
+ }
95
+ if (t && _.label < t[2]) {
96
+ _.label = t[2];
97
+ _.ops.push(op);
98
+ break;
99
+ }
100
+ if (t[2]) _.ops.pop();
101
+ _.trys.pop();
102
+ continue;
103
+ }
104
+ op = body.call(thisArg, _);
105
+ } catch (e) {
106
+ op = [
107
+ 6,
108
+ e
109
+ ];
110
+ y = 0;
111
+ } finally{
112
+ f = t = 0;
113
+ }
114
+ if (op[0] & 5) throw op[1];
115
+ return {
116
+ value: op[0] ? op[1] : void 0,
117
+ done: true
118
+ };
119
+ }
120
+ }
121
+ import { estimateRequestTokens } from './token-estimator.js';
122
+ var governorNamespace = null;
123
+ export function setGovernorBinding(ns) {
124
+ governorNamespace = ns;
125
+ }
126
+ function getGovernorStub(identity) {
127
+ if (!governorNamespace) return null;
128
+ return governorNamespace.get(governorNamespace.idFromName(identity));
129
+ }
130
+ function computeIdentity(modelId, apiKey) {
131
+ return _async_to_generator(function() {
132
+ var data, hashBuffer, hashArray;
133
+ return _ts_generator(this, function(_state) {
134
+ switch(_state.label){
135
+ case 0:
136
+ data = new TextEncoder().encode("".concat(modelId, ":").concat(apiKey));
137
+ return [
138
+ 4,
139
+ crypto.subtle.digest('SHA-256', data)
140
+ ];
141
+ case 1:
142
+ hashBuffer = _state.sent();
143
+ hashArray = new Uint8Array(hashBuffer);
144
+ return [
145
+ 2,
146
+ Array.from(hashArray).map(function(b) {
147
+ return b.toString(16).padStart(2, '0');
148
+ }).join('')
149
+ ];
150
+ }
151
+ });
152
+ })();
153
+ }
154
+ function governedCall(_0) {
155
+ return _async_to_generator(function(param) {
156
+ var governorStub, modelId, estimatedTokens, call, extractHeaders, error, result, headers;
157
+ return _ts_generator(this, function(_state) {
158
+ switch(_state.label){
159
+ case 0:
160
+ governorStub = param.governorStub, modelId = param.modelId, estimatedTokens = param.estimatedTokens, call = param.call, extractHeaders = param.extractHeaders;
161
+ if (!governorStub) {
162
+ return [
163
+ 2,
164
+ call()
165
+ ];
166
+ }
167
+ _state.label = 1;
168
+ case 1:
169
+ _state.trys.push([
170
+ 1,
171
+ 3,
172
+ ,
173
+ 4
174
+ ]);
175
+ return [
176
+ 4,
177
+ governorStub.waitForCapacity(modelId, estimatedTokens)
178
+ ];
179
+ case 2:
180
+ _state.sent();
181
+ return [
182
+ 3,
183
+ 4
184
+ ];
185
+ case 3:
186
+ error = _state.sent();
187
+ console.warn('Governor waitForCapacity failed, proceeding without rate limiting:', error);
188
+ return [
189
+ 3,
190
+ 4
191
+ ];
192
+ case 4:
193
+ return [
194
+ 4,
195
+ call()
196
+ ];
197
+ case 5:
198
+ result = _state.sent();
199
+ headers = extractHeaders(result);
200
+ if (headers) {
201
+ governorStub.reportHeaders(headers);
202
+ }
203
+ return [
204
+ 2,
205
+ result
206
+ ];
207
+ }
208
+ });
209
+ }).apply(this, arguments);
210
+ }
211
+ export function rateGoverned(client) {
212
+ var _client_apiKey;
213
+ var apiKey = (_client_apiKey = client.apiKey) !== null && _client_apiKey !== void 0 ? _client_apiKey : '';
214
+ var cachedIdentity = null;
215
+ function getIdentity() {
216
+ return _async_to_generator(function() {
217
+ var _client_modelId, modelId;
218
+ return _ts_generator(this, function(_state) {
219
+ switch(_state.label){
220
+ case 0:
221
+ if (cachedIdentity) return [
222
+ 2,
223
+ cachedIdentity
224
+ ];
225
+ modelId = (_client_modelId = client.modelId) !== null && _client_modelId !== void 0 ? _client_modelId : 'unknown';
226
+ return [
227
+ 4,
228
+ computeIdentity(modelId, apiKey)
229
+ ];
230
+ case 1:
231
+ cachedIdentity = _state.sent();
232
+ return [
233
+ 2,
234
+ cachedIdentity
235
+ ];
236
+ }
237
+ });
238
+ })();
239
+ }
240
+ var wrapper = {
241
+ get identity () {
242
+ return client.identity;
243
+ },
244
+ get modelId () {
245
+ return client.modelId;
246
+ },
247
+ get apiKey () {
248
+ return client.apiKey;
249
+ },
250
+ generateObject: function generateObject(params) {
251
+ return _async_to_generator(function() {
252
+ var identity, governorStub, estimated, _client_modelId;
253
+ return _ts_generator(this, function(_state) {
254
+ switch(_state.label){
255
+ case 0:
256
+ return [
257
+ 4,
258
+ getIdentity()
259
+ ];
260
+ case 1:
261
+ identity = _state.sent();
262
+ governorStub = getGovernorStub(identity);
263
+ estimated = estimateRequestTokens({
264
+ prompt: params.prompt,
265
+ messages: params.messages,
266
+ system: params.system
267
+ });
268
+ return [
269
+ 2,
270
+ governedCall({
271
+ governorStub: governorStub,
272
+ modelId: (_client_modelId = client.modelId) !== null && _client_modelId !== void 0 ? _client_modelId : 'unknown',
273
+ estimatedTokens: estimated,
274
+ call: function() {
275
+ return client.generateObject(params);
276
+ },
277
+ extractHeaders: function(result) {
278
+ return result.responseHeaders;
279
+ }
280
+ })
281
+ ];
282
+ }
283
+ });
284
+ })();
285
+ },
286
+ createToolResultMessage: client.createToolResultMessage ? function(toolCallId, toolName, result) {
287
+ return client.createToolResultMessage(toolCallId, toolName, result);
288
+ } : undefined,
289
+ streamText: function streamText(params) {
290
+ return _async_to_generator(function() {
291
+ var identity, governorStub, estimated, _client_modelId;
292
+ return _ts_generator(this, function(_state) {
293
+ switch(_state.label){
294
+ case 0:
295
+ return [
296
+ 4,
297
+ getIdentity()
298
+ ];
299
+ case 1:
300
+ identity = _state.sent();
301
+ governorStub = getGovernorStub(identity);
302
+ estimated = estimateRequestTokens({
303
+ prompt: params.prompt,
304
+ messages: params.messages,
305
+ system: params.system
306
+ });
307
+ return [
308
+ 2,
309
+ governedCall({
310
+ governorStub: governorStub,
311
+ modelId: (_client_modelId = client.modelId) !== null && _client_modelId !== void 0 ? _client_modelId : 'unknown',
312
+ estimatedTokens: estimated,
313
+ call: function() {
314
+ return client.streamText(params);
315
+ },
316
+ extractHeaders: function(result) {
317
+ return result.responseHeaders;
318
+ }
319
+ })
320
+ ];
321
+ }
322
+ });
323
+ })();
324
+ }
325
+ };
326
+ if (client.generateText) {
327
+ wrapper.generateText = function(params) {
328
+ return _async_to_generator(function() {
329
+ var identity, governorStub, estimated, _client_modelId;
330
+ return _ts_generator(this, function(_state) {
331
+ switch(_state.label){
332
+ case 0:
333
+ return [
334
+ 4,
335
+ getIdentity()
336
+ ];
337
+ case 1:
338
+ identity = _state.sent();
339
+ governorStub = getGovernorStub(identity);
340
+ estimated = estimateRequestTokens({
341
+ messages: params.messages,
342
+ system: params.system
343
+ });
344
+ return [
345
+ 2,
346
+ governedCall({
347
+ governorStub: governorStub,
348
+ modelId: (_client_modelId = client.modelId) !== null && _client_modelId !== void 0 ? _client_modelId : 'unknown',
349
+ estimatedTokens: estimated,
350
+ call: function() {
351
+ return client.generateText(params);
352
+ },
353
+ extractHeaders: function(result) {
354
+ return result.responseHeaders;
355
+ }
356
+ })
357
+ ];
358
+ }
359
+ });
360
+ })();
361
+ };
362
+ }
363
+ return wrapper;
364
+ }