ai-workflows 1.0.0 → 1.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/dist/scripts/generate-types.js +57 -0
- package/dist/src/ai-proxy.d.ts +5 -0
- package/dist/src/ai-proxy.js +94 -0
- package/dist/{index.d.ts → src/index.d.ts} +2 -0
- package/dist/{index.js → src/index.js} +2 -0
- package/dist/src/index.test.d.ts +1 -0
- package/dist/src/index.test.js +35 -0
- package/package.json +7 -3
- package/dist/index.test.js +0 -9
- /package/dist/{index.test.d.ts → scripts/generate-types.d.ts} +0 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { readFileSync, writeFileSync, readdirSync } from 'node:fs';
|
|
2
|
+
import { fileURLToPath } from 'node:url';
|
|
3
|
+
import { dirname, resolve } from 'node:path';
|
|
4
|
+
import matter from 'gray-matter';
|
|
5
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = dirname(__filename);
|
|
7
|
+
const aiDir = resolve(process.cwd(), 'ai');
|
|
8
|
+
const outputFile = resolve(process.cwd(), 'src/ai-types.d.ts');
|
|
9
|
+
/**
|
|
10
|
+
* Converts an MDX output type definition to a TypeScript type
|
|
11
|
+
* Handles enum types (separated by |) and string types
|
|
12
|
+
*/
|
|
13
|
+
function convertOutputTypeToTS(type) {
|
|
14
|
+
if (type.includes('|')) {
|
|
15
|
+
// Handle enum types (e.g., "Article | BlogPosting | Thing")
|
|
16
|
+
return type.split('|').map(t => `'${t.trim()}'`).join(' | ');
|
|
17
|
+
}
|
|
18
|
+
return 'string';
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Generates TypeScript type definitions from MDX files
|
|
22
|
+
*/
|
|
23
|
+
function generateTypes() {
|
|
24
|
+
const mdxFiles = readdirSync(aiDir).filter(file => file.endsWith('.mdx'));
|
|
25
|
+
let typeDefinitions = `// Generated TypeScript definitions for AI methods
|
|
26
|
+
// DO NOT EDIT DIRECTLY - Generated from MDX files in ai/
|
|
27
|
+
|
|
28
|
+
declare const ai: {
|
|
29
|
+
`;
|
|
30
|
+
for (const file of mdxFiles) {
|
|
31
|
+
const methodName = file.replace('.mdx', '');
|
|
32
|
+
const content = readFileSync(resolve(aiDir, file), 'utf-8');
|
|
33
|
+
const { data: frontmatter } = matter(content);
|
|
34
|
+
if (!frontmatter.output?.type) {
|
|
35
|
+
console.warn(`Warning: ${file} is missing output type definition`);
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
const tsType = convertOutputTypeToTS(frontmatter.output.type);
|
|
39
|
+
const description = frontmatter.output.description || 'No description provided';
|
|
40
|
+
typeDefinitions += ` /**
|
|
41
|
+
* ${description}
|
|
42
|
+
* @param input The input data for the AI operation
|
|
43
|
+
* @returns A promise that resolves to the generated output
|
|
44
|
+
*/
|
|
45
|
+
${methodName}(input: unknown): Promise<${tsType}>;
|
|
46
|
+
|
|
47
|
+
`;
|
|
48
|
+
}
|
|
49
|
+
typeDefinitions += `}
|
|
50
|
+
|
|
51
|
+
export { ai }
|
|
52
|
+
`;
|
|
53
|
+
writeFileSync(outputFile, typeDefinitions);
|
|
54
|
+
console.log(`Generated type definitions at ${outputFile}`);
|
|
55
|
+
}
|
|
56
|
+
// Run the generator
|
|
57
|
+
generateTypes();
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { readFileSync, existsSync } from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import matter from 'gray-matter';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { OpenAI } from 'openai';
|
|
6
|
+
const aiBasePath = path.resolve(process.cwd(), 'ai');
|
|
7
|
+
const openai = new OpenAI({
|
|
8
|
+
apiKey: process.env.OPENAI_API_KEY
|
|
9
|
+
});
|
|
10
|
+
// Schema for required MDX frontmatter
|
|
11
|
+
const frontmatterSchema = z.object({
|
|
12
|
+
model: z.string(),
|
|
13
|
+
system: z.string(),
|
|
14
|
+
output: z.object({
|
|
15
|
+
type: z.string(),
|
|
16
|
+
description: z.string()
|
|
17
|
+
})
|
|
18
|
+
});
|
|
19
|
+
/**
|
|
20
|
+
* Creates a Zod schema from an output definition in MDX frontmatter
|
|
21
|
+
* Handles enum types (separated by |) and string types with descriptions
|
|
22
|
+
* @param outputDef - The output definition from MDX frontmatter
|
|
23
|
+
* @returns A Zod schema for validating the output
|
|
24
|
+
*/
|
|
25
|
+
function createZodSchemaFromOutput(outputDef) {
|
|
26
|
+
const { type, description } = outputDef;
|
|
27
|
+
// Create schema based on output type
|
|
28
|
+
const schema = type.includes('|')
|
|
29
|
+
? z.object({
|
|
30
|
+
type: z.enum(type.split('|').map(t => t.trim())),
|
|
31
|
+
description: z.string()
|
|
32
|
+
})
|
|
33
|
+
: z.object({
|
|
34
|
+
type: z.string(),
|
|
35
|
+
description: z.string()
|
|
36
|
+
});
|
|
37
|
+
return schema.describe(description);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The ai object is a Proxy that dynamically loads MDX files for each method call
|
|
41
|
+
* Example: ai.summarize(ticket) will load ai/summarize.mdx and use its configuration
|
|
42
|
+
*/
|
|
43
|
+
export const ai = new Proxy({}, {
|
|
44
|
+
get(_target, methodName) {
|
|
45
|
+
return async (input) => {
|
|
46
|
+
try {
|
|
47
|
+
// Check if MDX file exists
|
|
48
|
+
const mdxFile = path.join(aiBasePath, `${methodName}.mdx`);
|
|
49
|
+
if (!existsSync(mdxFile)) {
|
|
50
|
+
throw new Error(`No MDX file found for ai.${methodName}(). Create ${methodName}.mdx in the ai/ directory.`);
|
|
51
|
+
}
|
|
52
|
+
// Load and parse the MDX file
|
|
53
|
+
const fileContent = readFileSync(mdxFile, 'utf-8');
|
|
54
|
+
const { data: rawFrontmatter, content } = matter(fileContent);
|
|
55
|
+
// Validate frontmatter against schema
|
|
56
|
+
const frontmatter = frontmatterSchema.parse(rawFrontmatter);
|
|
57
|
+
// Create Zod schema from output definition
|
|
58
|
+
const outputSchema = createZodSchemaFromOutput(frontmatter.output);
|
|
59
|
+
// Generate response using OpenAI
|
|
60
|
+
const completion = await openai.chat.completions.create({
|
|
61
|
+
model: frontmatter.model,
|
|
62
|
+
messages: [
|
|
63
|
+
{ role: 'system', content: frontmatter.system },
|
|
64
|
+
{ role: 'user', content: `${content}\n\nInput: ${JSON.stringify(input, null, 2)}` }
|
|
65
|
+
],
|
|
66
|
+
response_format: { type: 'json_object' },
|
|
67
|
+
temperature: 0.7, // Add some creativity while maintaining structure
|
|
68
|
+
});
|
|
69
|
+
// Parse and validate the response
|
|
70
|
+
let result;
|
|
71
|
+
try {
|
|
72
|
+
result = JSON.parse(completion.choices[0].message.content || '{}');
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
// Narrow error type for proper handling
|
|
76
|
+
const parseError = error;
|
|
77
|
+
throw new Error(`Failed to parse OpenAI response as JSON: ${parseError.message}`);
|
|
78
|
+
}
|
|
79
|
+
// Validate against our schema and return
|
|
80
|
+
return outputSchema.parse(result);
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
// Enhance error messages for different types of failures
|
|
84
|
+
if (error instanceof z.ZodError) {
|
|
85
|
+
throw new Error(`Schema validation failed in ai.${methodName}(): ${error.errors.map(e => e.message).join(', ')}`);
|
|
86
|
+
}
|
|
87
|
+
else if (error instanceof Error) {
|
|
88
|
+
throw new Error(`Error in ai.${methodName}(): ${error.message}`);
|
|
89
|
+
}
|
|
90
|
+
throw error;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { describe, it, expect, beforeAll } from '@jest/globals';
|
|
2
|
+
import { add, ai } from './index.js';
|
|
3
|
+
import { mkdir, writeFile } from 'fs/promises';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
describe('add', () => {
|
|
6
|
+
it('should add two numbers correctly', () => {
|
|
7
|
+
expect(add(2, 3)).toBe(5);
|
|
8
|
+
expect(add(-1, 1)).toBe(0);
|
|
9
|
+
expect(add(0, 0)).toBe(0);
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
describe('ai', () => {
|
|
13
|
+
beforeAll(async () => {
|
|
14
|
+
// Create ai directory and test MDX file
|
|
15
|
+
await mkdir(path.resolve(process.cwd(), 'ai'), { recursive: true });
|
|
16
|
+
await writeFile(path.resolve(process.cwd(), 'ai/test.mdx'), `---
|
|
17
|
+
model: gpt-3.5-turbo
|
|
18
|
+
system: Test system prompt
|
|
19
|
+
output:
|
|
20
|
+
type: Article | BlogPosting
|
|
21
|
+
description: Test description
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
# Test MDX
|
|
25
|
+
|
|
26
|
+
Test content`);
|
|
27
|
+
});
|
|
28
|
+
it('should handle AI method calls correctly', async () => {
|
|
29
|
+
const result = await ai.test({ input: 'test' });
|
|
30
|
+
expect(result).toEqual({
|
|
31
|
+
type: 'Article',
|
|
32
|
+
description: 'Test description'
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
});
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai-workflows",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.1.0",
|
|
5
5
|
"description": "Composable AI Workflows & Durable Execution Framework",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"module": "dist/index.js",
|
|
@@ -14,12 +14,13 @@
|
|
|
14
14
|
"bin"
|
|
15
15
|
],
|
|
16
16
|
"scripts": {
|
|
17
|
-
"build": "tsc",
|
|
17
|
+
"build": "tsc && NODE_OPTIONS=\"--experimental-specifier-resolution=node --loader ts-node/esm\" ts-node scripts/generate-types.ts",
|
|
18
18
|
"test": "jest",
|
|
19
19
|
"test:watch": "jest --watch",
|
|
20
20
|
"lint": "eslint src",
|
|
21
21
|
"format": "prettier --write \"src/**/*.{ts,tsx,js,jsx}\" \"*.{md,mdx}\"",
|
|
22
|
-
"prepublishOnly": "pnpm run build"
|
|
22
|
+
"prepublishOnly": "pnpm run build",
|
|
23
|
+
"generate-types": "NODE_OPTIONS=\"--experimental-specifier-resolution=node --loader ts-node/esm\" ts-node scripts/generate-types.ts"
|
|
23
24
|
},
|
|
24
25
|
"keywords": [
|
|
25
26
|
"ai",
|
|
@@ -46,6 +47,7 @@
|
|
|
46
47
|
"@semantic-release/github": "^11.0.1",
|
|
47
48
|
"@semantic-release/npm": "^12.0.1",
|
|
48
49
|
"@semantic-release/release-notes-generator": "^14.0.1",
|
|
50
|
+
"@swc/core": "^1.10.1",
|
|
49
51
|
"@testing-library/jest-dom": "^6.4.2",
|
|
50
52
|
"@testing-library/react": "^16.1.0",
|
|
51
53
|
"@types/jest": "^29.5.14",
|
|
@@ -63,6 +65,7 @@
|
|
|
63
65
|
"prettier": "^3.4.2",
|
|
64
66
|
"semantic-release": "^24.2.0",
|
|
65
67
|
"ts-jest": "^29.2.5",
|
|
68
|
+
"ts-node": "^10.9.2",
|
|
66
69
|
"typescript": "^5.7.2"
|
|
67
70
|
},
|
|
68
71
|
"dependencies": {
|
|
@@ -71,6 +74,7 @@
|
|
|
71
74
|
"ai-functions": "^0.2.19",
|
|
72
75
|
"gray-matter": "^4.0.3",
|
|
73
76
|
"mdxld": "^0.1.1",
|
|
77
|
+
"openai": "^4.77.0",
|
|
74
78
|
"react": "^18.3.1",
|
|
75
79
|
"react-dom": "^18.2.0",
|
|
76
80
|
"yaml": "^2.6.1",
|
package/dist/index.test.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from '@jest/globals';
|
|
2
|
-
import { add } from './index.js';
|
|
3
|
-
describe('add', () => {
|
|
4
|
-
it('should add two numbers correctly', () => {
|
|
5
|
-
expect(add(2, 3)).toBe(5);
|
|
6
|
-
expect(add(-1, 1)).toBe(0);
|
|
7
|
-
expect(add(0, 0)).toBe(0);
|
|
8
|
-
});
|
|
9
|
-
});
|
|
File without changes
|