@positronic/core 0.0.3 → 0.0.5

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.
@@ -1,474 +0,0 @@
1
- function _array_like_to_array(arr, len) {
2
- if (len == null || len > arr.length) len = arr.length;
3
- for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
4
- return arr2;
5
- }
6
- function _array_without_holes(arr) {
7
- if (Array.isArray(arr)) return _array_like_to_array(arr);
8
- }
9
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
10
- try {
11
- var info = gen[key](arg);
12
- var value = info.value;
13
- } catch (error) {
14
- reject(error);
15
- return;
16
- }
17
- if (info.done) {
18
- resolve(value);
19
- } else {
20
- Promise.resolve(value).then(_next, _throw);
21
- }
22
- }
23
- function _async_to_generator(fn) {
24
- return function() {
25
- var self = this, args = arguments;
26
- return new Promise(function(resolve, reject) {
27
- var gen = fn.apply(self, args);
28
- function _next(value) {
29
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
30
- }
31
- function _throw(err) {
32
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
33
- }
34
- _next(undefined);
35
- });
36
- };
37
- }
38
- function _class_call_check(instance, Constructor) {
39
- if (!(instance instanceof Constructor)) {
40
- throw new TypeError("Cannot call a class as a function");
41
- }
42
- }
43
- function _defineProperties(target, props) {
44
- for(var i = 0; i < props.length; i++){
45
- var descriptor = props[i];
46
- descriptor.enumerable = descriptor.enumerable || false;
47
- descriptor.configurable = true;
48
- if ("value" in descriptor) descriptor.writable = true;
49
- Object.defineProperty(target, descriptor.key, descriptor);
50
- }
51
- }
52
- function _create_class(Constructor, protoProps, staticProps) {
53
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
54
- if (staticProps) _defineProperties(Constructor, staticProps);
55
- return Constructor;
56
- }
57
- function _define_property(obj, key, value) {
58
- if (key in obj) {
59
- Object.defineProperty(obj, key, {
60
- value: value,
61
- enumerable: true,
62
- configurable: true,
63
- writable: true
64
- });
65
- } else {
66
- obj[key] = value;
67
- }
68
- return obj;
69
- }
70
- function _iterable_to_array(iter) {
71
- if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
72
- }
73
- function _non_iterable_spread() {
74
- throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
75
- }
76
- function _object_spread(target) {
77
- for(var i = 1; i < arguments.length; i++){
78
- var source = arguments[i] != null ? arguments[i] : {};
79
- var ownKeys = Object.keys(source);
80
- if (typeof Object.getOwnPropertySymbols === "function") {
81
- ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
82
- return Object.getOwnPropertyDescriptor(source, sym).enumerable;
83
- }));
84
- }
85
- ownKeys.forEach(function(key) {
86
- _define_property(target, key, source[key]);
87
- });
88
- }
89
- return target;
90
- }
91
- function _to_consumable_array(arr) {
92
- return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
93
- }
94
- function _unsupported_iterable_to_array(o, minLen) {
95
- if (!o) return;
96
- if (typeof o === "string") return _array_like_to_array(o, minLen);
97
- var n = Object.prototype.toString.call(o).slice(8, -1);
98
- if (n === "Object" && o.constructor) n = o.constructor.name;
99
- if (n === "Map" || n === "Set") return Array.from(n);
100
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
101
- }
102
- function _ts_generator(thisArg, body) {
103
- var f, y, t, _ = {
104
- label: 0,
105
- sent: function() {
106
- if (t[0] & 1) throw t[1];
107
- return t[1];
108
- },
109
- trys: [],
110
- ops: []
111
- }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
112
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
113
- return this;
114
- }), g;
115
- function verb(n) {
116
- return function(v) {
117
- return step([
118
- n,
119
- v
120
- ]);
121
- };
122
- }
123
- function step(op) {
124
- if (f) throw new TypeError("Generator is already executing.");
125
- while(g && (g = 0, op[0] && (_ = 0)), _)try {
126
- 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;
127
- if (y = 0, t) op = [
128
- op[0] & 2,
129
- t.value
130
- ];
131
- switch(op[0]){
132
- case 0:
133
- case 1:
134
- t = op;
135
- break;
136
- case 4:
137
- _.label++;
138
- return {
139
- value: op[1],
140
- done: false
141
- };
142
- case 5:
143
- _.label++;
144
- y = op[1];
145
- op = [
146
- 0
147
- ];
148
- continue;
149
- case 7:
150
- op = _.ops.pop();
151
- _.trys.pop();
152
- continue;
153
- default:
154
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
155
- _ = 0;
156
- continue;
157
- }
158
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
159
- _.label = op[1];
160
- break;
161
- }
162
- if (op[0] === 6 && _.label < t[1]) {
163
- _.label = t[1];
164
- t = op;
165
- break;
166
- }
167
- if (t && _.label < t[2]) {
168
- _.label = t[2];
169
- _.ops.push(op);
170
- break;
171
- }
172
- if (t[2]) _.ops.pop();
173
- _.trys.pop();
174
- continue;
175
- }
176
- op = body.call(thisArg, _);
177
- } catch (e) {
178
- op = [
179
- 6,
180
- e
181
- ];
182
- y = 0;
183
- } finally{
184
- f = t = 0;
185
- }
186
- if (op[0] & 5) throw op[1];
187
- return {
188
- value: op[0] ? op[1] : void 0,
189
- done: true
190
- };
191
- }
192
- }
193
- import { jest } from '@jest/globals';
194
- import { BRAIN_EVENTS } from './dsl/constants.js';
195
- import { applyPatches } from './dsl/json-patch.js';
196
- import { BrainRunner } from './dsl/brain-runner.js';
197
- /**
198
- * Mock implementation of ObjectGenerator for testing
199
- */ export var MockObjectGenerator = /*#__PURE__*/ function() {
200
- "use strict";
201
- function MockObjectGenerator() {
202
- _class_call_check(this, MockObjectGenerator);
203
- _define_property(this, "generateObjectMock", void 0);
204
- _define_property(this, "calls", []);
205
- this.generateObjectMock = jest.fn();
206
- }
207
- _create_class(MockObjectGenerator, [
208
- {
209
- key: "generateObject",
210
- value: function generateObject(params) {
211
- return _async_to_generator(function() {
212
- return _ts_generator(this, function(_state) {
213
- this.calls.push({
214
- params: params,
215
- timestamp: new Date()
216
- });
217
- return [
218
- 2,
219
- this.generateObjectMock(params)
220
- ];
221
- });
222
- }).call(this);
223
- }
224
- },
225
- {
226
- /**
227
- * Mock a response for the next generateObject call
228
- */ key: "mockNextResponse",
229
- value: function mockNextResponse(response) {
230
- this.generateObjectMock.mockResolvedValueOnce(response);
231
- }
232
- },
233
- {
234
- /**
235
- * Mock multiple responses in sequence
236
- */ key: "mockResponses",
237
- value: function mockResponses() {
238
- var _this = this;
239
- for(var _len = arguments.length, responses = new Array(_len), _key = 0; _key < _len; _key++){
240
- responses[_key] = arguments[_key];
241
- }
242
- responses.forEach(function(response) {
243
- _this.generateObjectMock.mockResolvedValueOnce(response);
244
- });
245
- }
246
- },
247
- {
248
- /**
249
- * Mock an error for the next generateObject call
250
- */ key: "mockNextError",
251
- value: function mockNextError(error) {
252
- var errorObj = typeof error === 'string' ? new Error(error) : error;
253
- this.generateObjectMock.mockRejectedValueOnce(errorObj);
254
- }
255
- },
256
- {
257
- key: "mock",
258
- get: /**
259
- * Get the underlying jest.Mock for advanced mocking scenarios
260
- */ function get() {
261
- return this.generateObjectMock;
262
- }
263
- },
264
- {
265
- /**
266
- * Clear all mocks and call history
267
- */ key: "clear",
268
- value: function clear() {
269
- this.generateObjectMock.mockClear();
270
- this.calls = [];
271
- }
272
- },
273
- {
274
- /**
275
- * Reset mock to initial state
276
- */ key: "reset",
277
- value: function reset() {
278
- this.generateObjectMock.mockReset();
279
- this.calls = [];
280
- }
281
- },
282
- {
283
- /**
284
- * Get all calls made to generateObject
285
- */ key: "getCalls",
286
- value: function getCalls() {
287
- return this.calls;
288
- }
289
- },
290
- {
291
- /**
292
- * Get the last call made to generateObject
293
- */ key: "getLastCall",
294
- value: function getLastCall() {
295
- return this.calls[this.calls.length - 1];
296
- }
297
- },
298
- {
299
- /**
300
- * Assert that generateObject was called with specific parameters
301
- */ key: "expectCalledWith",
302
- value: function expectCalledWith(expected) {
303
- var lastCall = this.getLastCall();
304
- if (!lastCall) {
305
- throw new Error('No calls made to generateObject');
306
- }
307
- if (expected.prompt !== undefined) {
308
- if (typeof expected.prompt === 'function') {
309
- expect(expected.prompt(lastCall.params.prompt || '')).toBe(true);
310
- } else {
311
- expect(lastCall.params.prompt).toBe(expected.prompt);
312
- }
313
- }
314
- if (expected.schemaName !== undefined) {
315
- expect(lastCall.params.schemaName).toBe(expected.schemaName);
316
- }
317
- if (expected.messages !== undefined) {
318
- expect(lastCall.params.messages).toEqual(expected.messages);
319
- }
320
- if (expected.system !== undefined) {
321
- expect(lastCall.params.system).toBe(expected.system);
322
- }
323
- }
324
- },
325
- {
326
- /**
327
- * Assert that generateObject was called N times
328
- */ key: "expectCallCount",
329
- value: function expectCallCount(count) {
330
- expect(this.generateObjectMock).toHaveBeenCalledTimes(count);
331
- }
332
- }
333
- ]);
334
- return MockObjectGenerator;
335
- }();
336
- /**
337
- * Creates a mock client for testing brains
338
- */ export function createMockClient() {
339
- return new MockObjectGenerator();
340
- }
341
- /**
342
- * Test adapter that collects brain execution results
343
- */ var TestAdapter = /*#__PURE__*/ function() {
344
- "use strict";
345
- function TestAdapter(initialState) {
346
- _class_call_check(this, TestAdapter);
347
- _define_property(this, "initialState", void 0);
348
- _define_property(this, "stepTitles", void 0);
349
- _define_property(this, "currentState", void 0);
350
- _define_property(this, "completed", void 0);
351
- _define_property(this, "error", void 0);
352
- this.initialState = initialState;
353
- this.stepTitles = [];
354
- this.completed = false;
355
- this.error = null;
356
- this.currentState = _object_spread({}, initialState);
357
- }
358
- _create_class(TestAdapter, [
359
- {
360
- key: "dispatch",
361
- value: function dispatch(event) {
362
- return _async_to_generator(function() {
363
- return _ts_generator(this, function(_state) {
364
- switch(event.type){
365
- case BRAIN_EVENTS.STEP_COMPLETE:
366
- this.stepTitles.push(event.stepTitle);
367
- if (event.patch && event.patch.length > 0) {
368
- this.currentState = applyPatches(this.currentState, event.patch);
369
- }
370
- break;
371
- case BRAIN_EVENTS.COMPLETE:
372
- this.completed = true;
373
- break;
374
- case BRAIN_EVENTS.ERROR:
375
- this.error = event.error;
376
- break;
377
- }
378
- return [
379
- 2
380
- ];
381
- });
382
- }).call(this);
383
- }
384
- },
385
- {
386
- key: "getExecutedSteps",
387
- value: function getExecutedSteps() {
388
- return _to_consumable_array(this.stepTitles);
389
- }
390
- },
391
- {
392
- key: "getFinalState",
393
- value: function getFinalState() {
394
- return _object_spread({}, this.currentState);
395
- }
396
- },
397
- {
398
- key: "isCompleted",
399
- value: function isCompleted() {
400
- return this.completed;
401
- }
402
- },
403
- {
404
- key: "getError",
405
- value: function getError() {
406
- return this.error;
407
- }
408
- }
409
- ]);
410
- return TestAdapter;
411
- }();
412
- /**
413
- * Runs a brain with test utilities and returns collected data
414
- */ export function runBrainTest(_0) {
415
- return _async_to_generator(function(brain) {
416
- var options, _options_client, client, resources, _options_initialState, initialState, brainOptions, testAdapter, runner, error;
417
- var _arguments = arguments;
418
- return _ts_generator(this, function(_state) {
419
- switch(_state.label){
420
- case 0:
421
- options = _arguments.length > 1 && _arguments[1] !== void 0 ? _arguments[1] : {};
422
- _options_client = options.client, client = _options_client === void 0 ? createMockClient() : _options_client, resources = options.resources, _options_initialState = options.initialState, initialState = _options_initialState === void 0 ? {} : _options_initialState, brainOptions = options.brainOptions;
423
- // Create test adapter
424
- testAdapter = new TestAdapter(initialState);
425
- // Create brain runner with test adapter
426
- runner = new BrainRunner({
427
- adapters: [
428
- testAdapter
429
- ],
430
- client: client,
431
- resources: resources
432
- });
433
- _state.label = 1;
434
- case 1:
435
- _state.trys.push([
436
- 1,
437
- 3,
438
- ,
439
- 4
440
- ]);
441
- // Run the brain
442
- return [
443
- 4,
444
- runner.run(brain, {
445
- initialState: initialState,
446
- options: brainOptions
447
- })
448
- ];
449
- case 2:
450
- _state.sent();
451
- return [
452
- 3,
453
- 4
454
- ];
455
- case 3:
456
- error = _state.sent();
457
- return [
458
- 3,
459
- 4
460
- ];
461
- case 4:
462
- return [
463
- 2,
464
- {
465
- finalState: testAdapter.getFinalState(),
466
- steps: testAdapter.getExecutedSteps(),
467
- error: testAdapter.getError(),
468
- completed: testAdapter.isCompleted()
469
- }
470
- ];
471
- }
472
- });
473
- }).apply(this, arguments);
474
- }
@@ -1,3 +0,0 @@
1
- // Test utilities for Positronic brains
2
- // Import from '@positronic/core/testing' to use these utilities
3
- export { createMockClient, MockObjectGenerator, runBrainTest } from './test-utils.js';
@@ -1,94 +0,0 @@
1
- import type { ObjectGenerator, Message } from './clients/types.js';
2
- import { z } from 'zod';
3
- import { jest } from '@jest/globals';
4
- import type { Brain } from './dsl/brain.js';
5
- import type { State } from './dsl/types.js';
6
- import type { Resources } from './resources/resources.js';
7
- /**
8
- * Mock implementation of ObjectGenerator for testing
9
- */
10
- export declare class MockObjectGenerator implements ObjectGenerator {
11
- private generateObjectMock;
12
- private calls;
13
- constructor();
14
- generateObject<T extends z.AnyZodObject>(params: Parameters<ObjectGenerator['generateObject']>[0]): Promise<z.infer<T>>;
15
- /**
16
- * Mock a response for the next generateObject call
17
- */
18
- mockNextResponse<T>(response: T): void;
19
- /**
20
- * Mock multiple responses in sequence
21
- */
22
- mockResponses(...responses: any[]): void;
23
- /**
24
- * Mock an error for the next generateObject call
25
- */
26
- mockNextError(error: Error | string): void;
27
- /**
28
- * Get the underlying jest.Mock for advanced mocking scenarios
29
- */
30
- get mock(): jest.Mock<any>;
31
- /**
32
- * Clear all mocks and call history
33
- */
34
- clear(): void;
35
- /**
36
- * Reset mock to initial state
37
- */
38
- reset(): void;
39
- /**
40
- * Get all calls made to generateObject
41
- */
42
- getCalls(): {
43
- params: Parameters<ObjectGenerator["generateObject"]>[0];
44
- timestamp: Date;
45
- }[];
46
- /**
47
- * Get the last call made to generateObject
48
- */
49
- getLastCall(): {
50
- params: Parameters<ObjectGenerator["generateObject"]>[0];
51
- timestamp: Date;
52
- };
53
- /**
54
- * Assert that generateObject was called with specific parameters
55
- */
56
- expectCalledWith(expected: {
57
- prompt?: string | ((actual: string) => boolean);
58
- schemaName?: string;
59
- messages?: Message[];
60
- system?: string;
61
- }): void;
62
- /**
63
- * Assert that generateObject was called N times
64
- */
65
- expectCallCount(count: number): void;
66
- }
67
- /**
68
- * Creates a mock client for testing brains
69
- */
70
- export declare function createMockClient(): MockObjectGenerator;
71
- /**
72
- * Test runner options
73
- */
74
- export interface TestRunnerOptions<TState extends State = State> {
75
- client?: ObjectGenerator;
76
- resources?: Resources;
77
- initialState?: TState;
78
- }
79
- /**
80
- * Result of running a brain test
81
- */
82
- export interface TestRunResult<TState extends State = State> {
83
- finalState: TState;
84
- steps: string[];
85
- error: Error | null;
86
- completed: boolean;
87
- }
88
- /**
89
- * Runs a brain with test utilities and returns collected data
90
- */
91
- export declare function runBrainTest<TOptions extends object = {}, TState extends State = {}>(brain: Brain<TOptions, TState, any>, options?: TestRunnerOptions<TState> & {
92
- brainOptions?: TOptions;
93
- }): Promise<TestRunResult<TState>>;
94
- //# sourceMappingURL=test-utils.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"test-utils.d.ts","sourceRoot":"","sources":["../../src/test-utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AACnE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AAErC,OAAO,KAAK,EAAc,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,gB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AAG,IAAI;IA2BR;;OAEG;IACH,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;CAGrC;AAED;;GAEG;AACH,wBAAgB,gBAAgB,IAAI,mBAAmB,CAEtD;AAiDD;;GAEG;AACH,MAAM,WAAW,iBAAiB,CAAC,MAAM,SAAS,KAAK,GAAG,KAAK;IAC7D,MAAM,CAAC,EAAE,eAAe,CAAC;IACzB,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED;;GAEG;AACH,MAAM,WAAW,aAAa,CAAC,MAAM,SAAS,KAAK,GAAG,KAAK;IACzD,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IACpB,SAAS,EAAE,OAAO,CAAC;CACpB;AAED;;GAEG;AACH,wBAAsB,YAAY,CAChC,QAAQ,SAAS,MAAM,GAAG,EAAE,EAC5B,MAAM,SAAS,KAAK,GAAG,EAAE,EAEzB,KAAK,EAAE,KAAK,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,CAAC,EACnC,OAAO,GAAE,iBAAiB,CAAC,MAAM,CAAC,GAAG;IAAE,YAAY,CAAC,EAAE,QAAQ,CAAA;CAAO,GACpE,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC,CAmChC"}
@@ -1,2 +0,0 @@
1
- export { createMockClient, MockObjectGenerator, runBrainTest, type TestRunnerOptions, type TestRunResult, } from './test-utils.js';
2
- //# sourceMappingURL=testing.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"testing.d.ts","sourceRoot":"","sources":["../../src/testing.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,gBAAgB,EAChB,mBAAmB,EACnB,YAAY,EACZ,KAAK,iBAAiB,EACtB,KAAK,aAAa,GACnB,MAAM,iBAAiB,CAAC"}