ai-functions 0.2.10 → 0.2.13

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 (93) hide show
  1. package/db/mongo.ts +3 -3
  2. package/dist/cjs/db/cache.d.ts +1 -0
  3. package/dist/cjs/db/cache.js +12 -0
  4. package/dist/cjs/db/mongo.d.ts +31 -0
  5. package/dist/cjs/db/mongo.js +47 -0
  6. package/dist/cjs/examples/data.d.ts +1105 -0
  7. package/dist/cjs/examples/data.js +1108 -0
  8. package/dist/cjs/functions/ai.d.ts +18 -0
  9. package/dist/cjs/functions/ai.js +99 -0
  10. package/dist/cjs/functions/ai.test.d.ts +1 -0
  11. package/dist/cjs/functions/ai.test.js +40 -0
  12. package/dist/cjs/functions/gpt.d.ts +4 -0
  13. package/dist/cjs/functions/gpt.js +24 -0
  14. package/dist/cjs/functions/list.d.ts +7 -0
  15. package/dist/cjs/functions/list.js +134 -0
  16. package/dist/cjs/index.d.ts +3 -0
  17. package/dist/cjs/index.js +19 -0
  18. package/dist/cjs/package.json +3 -0
  19. package/dist/cjs/queue/kafka.d.ts +0 -0
  20. package/dist/cjs/queue/kafka.js +1 -0
  21. package/dist/cjs/queue/memory.d.ts +0 -0
  22. package/dist/cjs/queue/memory.js +1 -0
  23. package/dist/cjs/queue/mongo.d.ts +30 -0
  24. package/dist/cjs/queue/mongo.js +69 -0
  25. package/dist/cjs/streams/kafka.d.ts +0 -0
  26. package/dist/cjs/streams/kafka.js +1 -0
  27. package/dist/cjs/streams/memory.d.ts +0 -0
  28. package/dist/cjs/streams/memory.js +1 -0
  29. package/dist/cjs/streams/mongo.d.ts +0 -0
  30. package/dist/cjs/streams/mongo.js +1 -0
  31. package/dist/cjs/streams/types.d.ts +0 -0
  32. package/dist/cjs/streams/types.js +1 -0
  33. package/dist/cjs/types.d.ts +11 -0
  34. package/dist/cjs/types.js +2 -0
  35. package/dist/cjs/utils/completion.d.ts +9 -0
  36. package/dist/cjs/utils/completion.js +45 -0
  37. package/dist/cjs/utils/schema.d.ts +10 -0
  38. package/dist/cjs/utils/schema.js +64 -0
  39. package/dist/cjs/utils/schema.test.d.ts +1 -0
  40. package/dist/cjs/utils/schema.test.js +62 -0
  41. package/dist/cjs/utils/state.d.ts +1 -0
  42. package/dist/cjs/utils/state.js +21 -0
  43. package/dist/mjs/db/cache.d.ts +1 -0
  44. package/dist/mjs/db/cache.js +5 -0
  45. package/dist/mjs/db/mongo.d.ts +31 -0
  46. package/dist/mjs/db/mongo.js +48 -0
  47. package/dist/mjs/examples/data.d.ts +1105 -0
  48. package/dist/mjs/examples/data.js +1105 -0
  49. package/dist/mjs/functions/ai.d.ts +18 -0
  50. package/dist/mjs/functions/ai.js +79 -0
  51. package/dist/mjs/functions/ai.test.d.ts +1 -0
  52. package/dist/mjs/functions/ai.test.js +29 -0
  53. package/dist/mjs/functions/gpt.d.ts +4 -0
  54. package/dist/mjs/functions/gpt.js +10 -0
  55. package/dist/mjs/functions/list.d.ts +7 -0
  56. package/dist/mjs/functions/list.js +72 -0
  57. package/dist/mjs/index.d.ts +3 -0
  58. package/dist/mjs/index.js +3 -0
  59. package/dist/mjs/package.json +3 -0
  60. package/dist/mjs/queue/kafka.d.ts +0 -0
  61. package/dist/mjs/queue/kafka.js +1 -0
  62. package/dist/mjs/queue/memory.d.ts +0 -0
  63. package/dist/mjs/queue/memory.js +1 -0
  64. package/dist/mjs/queue/mongo.d.ts +30 -0
  65. package/dist/mjs/queue/mongo.js +42 -0
  66. package/dist/mjs/streams/kafka.d.ts +0 -0
  67. package/dist/mjs/streams/kafka.js +1 -0
  68. package/dist/mjs/streams/memory.d.ts +0 -0
  69. package/dist/mjs/streams/memory.js +1 -0
  70. package/dist/mjs/streams/mongo.d.ts +0 -0
  71. package/dist/mjs/streams/mongo.js +1 -0
  72. package/dist/mjs/streams/types.d.ts +0 -0
  73. package/dist/mjs/streams/types.js +1 -0
  74. package/dist/mjs/types.d.ts +11 -0
  75. package/dist/mjs/types.js +1 -0
  76. package/dist/mjs/utils/completion.d.ts +9 -0
  77. package/dist/mjs/utils/completion.js +20 -0
  78. package/dist/mjs/utils/schema.d.ts +10 -0
  79. package/dist/mjs/utils/schema.js +59 -0
  80. package/dist/mjs/utils/schema.test.d.ts +1 -0
  81. package/dist/mjs/utils/schema.test.js +60 -0
  82. package/dist/mjs/utils/state.d.ts +1 -0
  83. package/dist/mjs/utils/state.js +19 -0
  84. package/fixup +11 -0
  85. package/functions/ai.ts +1 -1
  86. package/functions/list.ts +1 -1
  87. package/package.json +10 -3
  88. package/tsconfig-backup.json +105 -0
  89. package/tsconfig-base.json +26 -0
  90. package/tsconfig-cjs.json +8 -0
  91. package/tsconfig.json +5 -102
  92. package/types.ts +1 -1
  93. package/utils/completion.ts +1 -1
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateSchema = exports.parseStringDescription = void 0;
4
+ // This is a helper function to generate a JSON schema for string properties.
5
+ // It checks if a string includes '|' which would indicate an enum,
6
+ // or if it starts with 'number: ' or 'boolean: ' which would indicate
7
+ // a number or boolean type respectively, otherwise it defaults to string.
8
+ const parseStringDescription = (description) => {
9
+ // Check if the description indicates an enum for string type
10
+ if (description.includes('|')) {
11
+ return { type: 'string', enum: description.split('|').map((v) => v.trim()) };
12
+ }
13
+ else {
14
+ // Check for 'number: ' prefix
15
+ if (description.startsWith('number: ')) {
16
+ return {
17
+ type: 'number',
18
+ description: description.replace('number: ', ''),
19
+ };
20
+ }
21
+ // Check for 'boolean: ' prefix
22
+ else if (description.startsWith('boolean: ')) {
23
+ return {
24
+ type: 'boolean',
25
+ description: description.replace('boolean: ', ''),
26
+ };
27
+ }
28
+ // Default to string type
29
+ else {
30
+ return { type: 'string', description };
31
+ }
32
+ }
33
+ };
34
+ exports.parseStringDescription = parseStringDescription;
35
+ /**
36
+ * Given a property description object, generate a JSON schema.
37
+ *
38
+ * @param propDescriptions - A record object with keys as property names
39
+ * and values as descriptions or nested property description objects.
40
+ * @returns A JSON schema object based on the provided descriptions.
41
+ */
42
+ const generateSchema = (propDescriptions) => {
43
+ // If the propDescriptions is for an object structure
44
+ if (typeof propDescriptions !== 'object' || propDescriptions === null || Array.isArray(propDescriptions)) {
45
+ throw new Error('The propDescriptions parameter should be an object.');
46
+ }
47
+ const properties = {};
48
+ const required = Object.keys(propDescriptions);
49
+ for (const [key, description] of Object.entries(propDescriptions)) {
50
+ if (typeof description === 'string') {
51
+ // Handle the string description
52
+ properties[key] = (0, exports.parseStringDescription)(description);
53
+ }
54
+ else if (typeof description === 'object' && !Array.isArray(description)) {
55
+ // Recursive call for nested objects
56
+ properties[key] = (0, exports.generateSchema)(description);
57
+ }
58
+ else {
59
+ throw new Error(`Invalid description for key "${key}".`);
60
+ }
61
+ }
62
+ return { type: 'object', properties, required };
63
+ };
64
+ exports.generateSchema = generateSchema;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const vitest_1 = require("vitest");
4
+ const schema_1 = require("./schema");
5
+ // Tests for parseStringDescription function
6
+ (0, vitest_1.describe)('parseStringDescription', () => {
7
+ (0, vitest_1.it)('should create a string schema for plain strings', () => {
8
+ const result = (0, schema_1.parseStringDescription)('A plain string description');
9
+ (0, vitest_1.expect)(result).toEqual({
10
+ type: 'string',
11
+ description: 'A plain string description',
12
+ });
13
+ });
14
+ (0, vitest_1.it)('should create a string schema with enum for piped strings', () => {
15
+ const result = (0, schema_1.parseStringDescription)('option1 | option2 | option3');
16
+ (0, vitest_1.expect)(result).toEqual({
17
+ type: 'string',
18
+ enum: ['option1', 'option2', 'option3'],
19
+ });
20
+ });
21
+ (0, vitest_1.it)('should create a number schema when prefixed with "number: "', () => {
22
+ const result = (0, schema_1.parseStringDescription)('number: A number description');
23
+ (0, vitest_1.expect)(result).toEqual({
24
+ type: 'number',
25
+ description: 'A number description',
26
+ });
27
+ });
28
+ (0, vitest_1.it)('should create a boolean schema when prefixed with "boolean: "', () => {
29
+ const result = (0, schema_1.parseStringDescription)('boolean: A boolean description');
30
+ (0, vitest_1.expect)(result).toEqual({
31
+ type: 'boolean',
32
+ description: 'A boolean description',
33
+ });
34
+ });
35
+ });
36
+ // Tests for generateSchema function
37
+ (0, vitest_1.describe)('generateSchema', () => {
38
+ (0, vitest_1.it)('should create a complex object schema', () => {
39
+ const result = (0, schema_1.generateSchema)({
40
+ name: 'The name of the person',
41
+ age: 'number: The age of the person',
42
+ isActive: 'boolean: Whether the person is active or not',
43
+ });
44
+ const expected = {
45
+ type: 'object',
46
+ properties: {
47
+ name: { type: 'string', description: 'The name of the person' },
48
+ age: { type: 'number', description: 'The age of the person' },
49
+ isActive: {
50
+ type: 'boolean',
51
+ description: 'Whether the person is active or not',
52
+ },
53
+ },
54
+ required: ['name', 'age', 'isActive'],
55
+ };
56
+ (0, vitest_1.expect)(result).toEqual(expected);
57
+ });
58
+ (0, vitest_1.it)('should throw an error for invalid propDescriptions', () => {
59
+ const callWithInvalidArg = () => (0, schema_1.generateSchema)('invalid argument');
60
+ (0, vitest_1.expect)(callWithInvalidArg).toThrow('The propDescriptions parameter should be an object.');
61
+ });
62
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const xstate_1 = require("xstate");
4
+ // Stateless machine definition
5
+ // machine.transition(...) is a pure function used by the interpreter.
6
+ const toggleMachine = (0, xstate_1.createMachine)({
7
+ id: 'Switch',
8
+ initial: 'inactive',
9
+ states: {
10
+ inactive: { on: { Toggle: 'active' } },
11
+ active: { on: { Toggle: 'inactive' } },
12
+ },
13
+ });
14
+ // Machine instance with internal state
15
+ const toggleService = (0, xstate_1.createActor)(toggleMachine).start();
16
+ toggleService.subscribe((state) => console.log(state.value));
17
+ // => 'inactive'
18
+ toggleService.send({ type: 'Toggle' });
19
+ // => 'active'
20
+ toggleService.send({ type: 'Toggle' });
21
+ // => 'inactive'
@@ -0,0 +1 @@
1
+ export declare const md5: (data: string | object) => string;
@@ -0,0 +1,5 @@
1
+ import crypto from 'node:crypto';
2
+ export const md5 = (data) => {
3
+ const input = typeof data === 'string' ? data : JSON.stringify(data);
4
+ return crypto.createHash('md5').update(input).digest('hex');
5
+ };
@@ -0,0 +1,31 @@
1
+ import type { MongoClient, Db, Collection, InsertOneResult } from 'mongodb';
2
+ import type { ChatCompletion, ChatCompletionCreateParamsNonStreaming } from 'openai/resources';
3
+ import { QueueInput } from '../queue/mongo';
4
+ export declare let client: MongoClient;
5
+ export declare let db: Db;
6
+ export declare let cache: Collection;
7
+ export declare let cacheTTL: number;
8
+ export declare let events: Collection;
9
+ export declare let queue: Collection;
10
+ export declare let actors: Collection;
11
+ export type AIDBConfig = {
12
+ client: MongoClient;
13
+ db?: Db | string;
14
+ cache?: Collection | string;
15
+ cacheTTL?: number;
16
+ actors?: Collection | string;
17
+ events?: Collection | string;
18
+ queue?: Collection | string;
19
+ };
20
+ export type AIDB = {
21
+ client: MongoClient;
22
+ db: Db;
23
+ cache: Collection;
24
+ cacheTTL: number;
25
+ actors: Collection;
26
+ events: Collection;
27
+ queue: Collection;
28
+ log: (prompt: ChatCompletionCreateParamsNonStreaming, completion: ChatCompletion) => Promise<InsertOneResult<any>>;
29
+ send: (input: QueueInput | QueueInput[]) => Promise<InsertOneResult<any>>;
30
+ };
31
+ export declare const AIDB: (args: AIDBConfig | MongoClient) => AIDB;
@@ -0,0 +1,48 @@
1
+ export let client;
2
+ export let db;
3
+ export let cache;
4
+ export let cacheTTL;
5
+ export let events;
6
+ export let queue;
7
+ export let actors;
8
+ export const AIDB = (args) => {
9
+ const config = args.client ? args : { client: args };
10
+ client = config.client;
11
+ db = typeof config.db === 'string' ? client.db(config.db) : config.db ?? client.db();
12
+ cache = typeof config.cache === 'string' ? db.collection(config.cache) : config.cache ?? db.collection('ai-cache');
13
+ cacheTTL = config.cacheTTL ?? 1000 * 60 * 60 * 24 * 30;
14
+ events =
15
+ typeof config.events === 'string' ? db.collection(config.events) : config.events ?? db.collection('ai-events');
16
+ // queue = typeof config.queue === 'string' ? db.collection(config.queue) : config.queue ?? db.collection('ai-queue')
17
+ // actors = typeof config.queue === 'string' ? db.collection(config.queue) : config.queue ?? db.collection('ai-actors')
18
+ return { client, db, cache, actors, events, queue, log }; // , send } as AIDB
19
+ };
20
+ // const send = (input: QueueInput | QueueInput[]) =>
21
+ // Array.isArray(input) ? queue.insertMany(input) : queue.insertOne(input)
22
+ const log = (prompt, completion) => {
23
+ const system = prompt.messages.find((message) => message.role === 'system')?.content;
24
+ const userMessages = prompt.messages.filter((message) => message.role === 'user').map((message) => message.content);
25
+ const user = userMessages.length === 1 ? userMessages[0] : userMessages;
26
+ const content = completion.choices?.[0].message.content;
27
+ const tool = completion.choices?.[0].message.tool_calls?.[0].function;
28
+ let functionData;
29
+ try {
30
+ if (tool)
31
+ functionData = JSON.parse(tool.arguments);
32
+ }
33
+ catch (err) {
34
+ functionData = tool?.arguments;
35
+ }
36
+ const timestamp = new Date();
37
+ const event = {
38
+ timestamp,
39
+ user,
40
+ system,
41
+ content,
42
+ functionName: tool?.name,
43
+ functionData,
44
+ prompt,
45
+ completion,
46
+ };
47
+ return events.insertOne(event, { ignoreUndefined: true });
48
+ };