@agentuity/cli 0.0.31 → 0.0.33
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/cmd/bundle/ast.d.ts.map +1 -1
- package/dist/cmd/bundle/bundler.d.ts.map +1 -1
- package/dist/cmd/bundle/patch/_util.d.ts +20 -0
- package/dist/cmd/bundle/patch/_util.d.ts.map +1 -0
- package/dist/cmd/bundle/patch/aisdk.d.ts +3 -0
- package/dist/cmd/bundle/patch/aisdk.d.ts.map +1 -0
- package/dist/cmd/bundle/patch/index.d.ts +4 -0
- package/dist/cmd/bundle/patch/index.d.ts.map +1 -0
- package/dist/cmd/bundle/plugin.d.ts.map +1 -1
- package/dist/cmd/dev/index.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/cmd/bundle/ast.ts +3 -0
- package/src/cmd/bundle/bundler.ts +6 -4
- package/src/cmd/bundle/patch/_util.ts +67 -0
- package/src/cmd/bundle/patch/aisdk.ts +130 -0
- package/src/cmd/bundle/patch/index.ts +97 -0
- package/src/cmd/bundle/plugin.ts +25 -0
- package/src/cmd/dev/index.ts +5 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ast.d.ts","sourceRoot":"","sources":["../../../src/cmd/bundle/ast.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ast.d.ts","sourceRoot":"","sources":["../../../src/cmd/bundle/ast.ts"],"names":[],"mappings":"AAwJA,wBAAgB,kBAAkB,CACjC,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,GACd,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CA0E/B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bundler.d.ts","sourceRoot":"","sources":["../../../src/cmd/bundle/bundler.ts"],"names":[],"mappings":"AAMA,MAAM,WAAW,aAAa;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,GAAG,CAAC,EAAE,OAAO,CAAC;CACd;AAED,wBAAsB,MAAM,CAAC,EAAE,GAAW,EAAE,OAAO,EAAE,EAAE,aAAa,
|
|
1
|
+
{"version":3,"file":"bundler.d.ts","sourceRoot":"","sources":["../../../src/cmd/bundle/bundler.ts"],"names":[],"mappings":"AAMA,MAAM,WAAW,aAAa;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,GAAG,CAAC,EAAE,OAAO,CAAC;CACd;AAED,wBAAsB,MAAM,CAAC,EAAE,GAAW,EAAE,OAAO,EAAE,EAAE,aAAa,iBAgInE"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface PatchFunctionAction {
|
|
2
|
+
before?: string;
|
|
3
|
+
after?: string;
|
|
4
|
+
}
|
|
5
|
+
export interface PatchClassAction {
|
|
6
|
+
methods: Record<string, PatchFunctionAction>;
|
|
7
|
+
}
|
|
8
|
+
export interface PatchModule {
|
|
9
|
+
module: string;
|
|
10
|
+
filename?: string;
|
|
11
|
+
functions?: Record<string, PatchFunctionAction>;
|
|
12
|
+
classes?: Record<string, PatchClassAction>;
|
|
13
|
+
body?: PatchFunctionAction;
|
|
14
|
+
}
|
|
15
|
+
export declare function generateEnvWarning(envkey: string): string;
|
|
16
|
+
export declare function generateJSArgsPatch(index: number, inject: string): string;
|
|
17
|
+
export declare function generateEnvGuard(name: string, inject: string, alt?: string): string;
|
|
18
|
+
export declare function generateGatewayEnvGuard(apikey: string, apikeyval: string, apibase: string, provider: string): string;
|
|
19
|
+
export declare function searchBackwards(contents: string, offset: number, val: string): number;
|
|
20
|
+
//# sourceMappingURL=_util.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_util.d.ts","sourceRoot":"","sources":["../../../../src/cmd/bundle/patch/_util.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,mBAAmB;IACnC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,gBAAgB;IAChC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,mBAAmB,CAAC,CAAC;CAC7C;AAED,MAAM,WAAW,WAAW;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,mBAAmB,CAAC,CAAC;IAChD,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC;IAC3C,IAAI,CAAC,EAAE,mBAAmB,CAAC;CAC3B;AAED,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAOzD;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAIzE;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,MAAM,CAKnF;AAED,wBAAgB,uBAAuB,CACtC,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,GACd,MAAM,CAaR;AAED,wBAAgB,eAAe,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAOrF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aisdk.d.ts","sourceRoot":"","sources":["../../../../src/cmd/bundle/patch/aisdk.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,KAAK,WAAW,EAIhB,MAAM,SAAS,CAAC;AA6BjB,wBAAgB,eAAe,IAAI,GAAG,CAAC,MAAM,EAAE,WAAW,CAAC,CAuD1D"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/cmd/bundle/patch/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,WAAW,EAAmB,MAAM,SAAS,CAAC;AAE5D,wBAAgB,eAAe,IAAI,GAAG,CAAC,MAAM,EAAE,WAAW,CAAC,CAM1D;AAED,wBAAsB,UAAU,CAC/B,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,WAAW,GAChB,OAAO,CAAC,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,CAkF/B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../../src/cmd/bundle/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,KAAK,CAAC;
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../../src/cmd/bundle/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,KAAK,CAAC;AAyGrC,QAAA,MAAM,gBAAgB,EAAE,SAwKvB,CAAC;AAEF,eAAe,gBAAgB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/cmd/dev/index.ts"],"names":[],"mappings":"AAOA,eAAO,MAAM,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/cmd/dev/index.ts"],"names":[],"mappings":"AAOA,eAAO,MAAM,OAAO,mCAsXlB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agentuity/cli",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.33",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./src/index.ts",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"prepublishOnly": "bun run clean && bun run build"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@agentuity/core": "0.0.
|
|
31
|
+
"@agentuity/core": "0.0.31",
|
|
32
32
|
"acorn-loose": "^8.5.2",
|
|
33
33
|
"astring": "^1.9.0",
|
|
34
34
|
"commander": "^14.0.2",
|
package/src/cmd/bundle/ast.ts
CHANGED
|
@@ -58,7 +58,7 @@ export async function bundle({ dev = false, rootDir }: BundleOptions) {
|
|
|
58
58
|
outdir: outDir,
|
|
59
59
|
define,
|
|
60
60
|
sourcemap: dev ? 'inline' : 'external',
|
|
61
|
-
env: '
|
|
61
|
+
env: 'disable',
|
|
62
62
|
plugins: [AgentuityBundler],
|
|
63
63
|
target: 'bun',
|
|
64
64
|
format: 'esm',
|
|
@@ -79,7 +79,7 @@ export async function bundle({ dev = false, rootDir }: BundleOptions) {
|
|
|
79
79
|
// Find workspace root for monorepo support
|
|
80
80
|
let workspaceRoot = rootDir;
|
|
81
81
|
let currentDir = rootDir;
|
|
82
|
-
while (
|
|
82
|
+
while (true) {
|
|
83
83
|
const pkgPath = join(currentDir, 'package.json');
|
|
84
84
|
if (existsSync(pkgPath)) {
|
|
85
85
|
const pkg = JSON.parse(await Bun.file(pkgPath).text());
|
|
@@ -88,7 +88,9 @@ export async function bundle({ dev = false, rootDir }: BundleOptions) {
|
|
|
88
88
|
break;
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
|
-
|
|
91
|
+
const parent = resolve(currentDir, '..');
|
|
92
|
+
if (parent === currentDir) break; // reached filesystem root
|
|
93
|
+
currentDir = parent;
|
|
92
94
|
}
|
|
93
95
|
|
|
94
96
|
// Make webEntrypoints - just the HTML files themselves
|
|
@@ -102,7 +104,7 @@ export async function bundle({ dev = false, rootDir }: BundleOptions) {
|
|
|
102
104
|
outdir: join(outDir, 'web'),
|
|
103
105
|
define,
|
|
104
106
|
sourcemap: dev ? 'inline' : 'linked',
|
|
105
|
-
env: '
|
|
107
|
+
env: 'AGENTUITY_PUBLIC_*',
|
|
106
108
|
plugins: [AgentuityBundler],
|
|
107
109
|
target: 'browser',
|
|
108
110
|
format: 'cjs',
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
export interface PatchFunctionAction {
|
|
2
|
+
before?: string;
|
|
3
|
+
after?: string;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface PatchClassAction {
|
|
7
|
+
methods: Record<string, PatchFunctionAction>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface PatchModule {
|
|
11
|
+
module: string;
|
|
12
|
+
filename?: string;
|
|
13
|
+
functions?: Record<string, PatchFunctionAction>;
|
|
14
|
+
classes?: Record<string, PatchClassAction>;
|
|
15
|
+
body?: PatchFunctionAction;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function generateEnvWarning(envkey: string): string {
|
|
19
|
+
return `if (process.env.AGENTUITY_ENVIRONMENT === 'development' || process.env.NODE_ENV !== 'production') {
|
|
20
|
+
console.error('The required environment variable ${envkey} must be set in your project .env file or in your local system environment.');
|
|
21
|
+
} else {
|
|
22
|
+
console.error('The required environment variable ${envkey} is required for this project. Use "agentuity env set ${envkey}" to set it and redeploy your project.');
|
|
23
|
+
}
|
|
24
|
+
`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function generateJSArgsPatch(index: number, inject: string): string {
|
|
28
|
+
return `const _newargs = [...(_args ?? [])];
|
|
29
|
+
_newargs[${index}] = {..._newargs[${index}], ${inject}};
|
|
30
|
+
_args = _newargs;`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function generateEnvGuard(name: string, inject: string, alt?: string): string {
|
|
34
|
+
return `if (!process.env.${name} || process.env.${name} === process.env.AGENTUITY_SDK_KEY) {
|
|
35
|
+
${inject}
|
|
36
|
+
} else {
|
|
37
|
+
${alt ?? ''}}`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function generateGatewayEnvGuard(
|
|
41
|
+
apikey: string,
|
|
42
|
+
apikeyval: string,
|
|
43
|
+
apibase: string,
|
|
44
|
+
provider: string
|
|
45
|
+
): string {
|
|
46
|
+
return `{
|
|
47
|
+
const apikey = process.env.AGENTUITY_SDK_KEY;
|
|
48
|
+
const url = process.env.AGENTUITY_AIGATEWAY_URL || process.env.AGENTUITY_TRANSPORT_URL || (apikey ? 'https://agentuity.ai' : '');
|
|
49
|
+
if (url && apikey) {
|
|
50
|
+
process.env.${apikey} = ${apikeyval};
|
|
51
|
+
process.env.${apibase} = url + '/gateway/${provider}';
|
|
52
|
+
console.debug('Enabled Agentuity AI Gateway for ${provider}');
|
|
53
|
+
} else {
|
|
54
|
+
${generateEnvWarning(apikey)}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function searchBackwards(contents: string, offset: number, val: string): number {
|
|
61
|
+
for (let i = offset; i >= 0; i--) {
|
|
62
|
+
if (contents.charAt(i) == val) {
|
|
63
|
+
return i;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return -1;
|
|
67
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type PatchModule,
|
|
3
|
+
generateEnvWarning,
|
|
4
|
+
generateEnvGuard,
|
|
5
|
+
generateJSArgsPatch,
|
|
6
|
+
} from './_util';
|
|
7
|
+
|
|
8
|
+
function generateAISDKPatch(patches: Map<string, PatchModule>) {
|
|
9
|
+
const vercelTelemetryPatch = generateJSArgsPatch(0, ` ` + '');
|
|
10
|
+
|
|
11
|
+
const enableTelemetryPatch = `
|
|
12
|
+
// Enable experimental telemetry to capture response text
|
|
13
|
+
const opts = {...(_args[0] ?? {}) };
|
|
14
|
+
opts.experimental_telemetry = { isEnabled: true };
|
|
15
|
+
_args[0] = opts;
|
|
16
|
+
`;
|
|
17
|
+
|
|
18
|
+
const comboPatch = vercelTelemetryPatch + enableTelemetryPatch;
|
|
19
|
+
const functionPatch = { before: comboPatch };
|
|
20
|
+
|
|
21
|
+
const patch: PatchModule = {
|
|
22
|
+
module: 'ai',
|
|
23
|
+
functions: {
|
|
24
|
+
generateText: functionPatch,
|
|
25
|
+
streamText: functionPatch,
|
|
26
|
+
generateObject: functionPatch,
|
|
27
|
+
streamObject: functionPatch,
|
|
28
|
+
embed: functionPatch,
|
|
29
|
+
embedMany: functionPatch,
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
patches.set('@vercel/ai', patch);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function generatePatches(): Map<string, PatchModule> {
|
|
36
|
+
const patches = new Map<string, PatchModule>();
|
|
37
|
+
generateAISDKPatch(patches);
|
|
38
|
+
createVercelAIProviderPatch(
|
|
39
|
+
patches,
|
|
40
|
+
'@ai-sdk/openai',
|
|
41
|
+
'createOpenAI',
|
|
42
|
+
'OPENAI_API_KEY',
|
|
43
|
+
'openai'
|
|
44
|
+
);
|
|
45
|
+
createVercelAIProviderPatch(
|
|
46
|
+
patches,
|
|
47
|
+
'@ai-sdk/anthropic',
|
|
48
|
+
'createAnthropic',
|
|
49
|
+
'ANTHROPIC_API_KEY',
|
|
50
|
+
'anthropic'
|
|
51
|
+
);
|
|
52
|
+
createVercelAIProviderPatch(
|
|
53
|
+
patches,
|
|
54
|
+
'@ai-sdk/cohere',
|
|
55
|
+
'createCohere',
|
|
56
|
+
'COHERE_API_KEY',
|
|
57
|
+
'cohere'
|
|
58
|
+
);
|
|
59
|
+
createVercelAIProviderPatch(
|
|
60
|
+
patches,
|
|
61
|
+
'@ai-sdk/deepseek',
|
|
62
|
+
'createDeepSeek',
|
|
63
|
+
'DEEPSEEK_API_KEY',
|
|
64
|
+
'deepseek'
|
|
65
|
+
);
|
|
66
|
+
createVercelAIProviderPatch(
|
|
67
|
+
patches,
|
|
68
|
+
'@ai-sdk/google',
|
|
69
|
+
'createGoogleGenerativeAI',
|
|
70
|
+
'GOOGLE_GENERATIVE_AI_API_KEY',
|
|
71
|
+
'google-ai-studio'
|
|
72
|
+
);
|
|
73
|
+
createVercelAIProviderPatch(patches, '@ai-sdk/xai', 'createXai', 'XAI_API_KEY', 'grok');
|
|
74
|
+
createVercelAIProviderPatch(patches, '@ai-sdk/groq', 'createGroq', 'GROQ_API_KEY', 'groq');
|
|
75
|
+
createVercelAIProviderPatch(
|
|
76
|
+
patches,
|
|
77
|
+
'@ai-sdk/mistral',
|
|
78
|
+
'createMistral',
|
|
79
|
+
'MISTRAL_API_KEY',
|
|
80
|
+
'mistral'
|
|
81
|
+
);
|
|
82
|
+
createVercelAIProviderPatch(
|
|
83
|
+
patches,
|
|
84
|
+
'@ai-sdk/perplexity',
|
|
85
|
+
'createPerplexity',
|
|
86
|
+
'PERPLEXITY_API_KEY',
|
|
87
|
+
'perplexity-ai'
|
|
88
|
+
);
|
|
89
|
+
return patches;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function generateVercelAIProvider(name: string, envkey: string): string {
|
|
93
|
+
return (
|
|
94
|
+
generateJSArgsPatch(0, '') +
|
|
95
|
+
`const opts = {...(_args[0] ?? {}) };
|
|
96
|
+
if (!opts.baseURL) {
|
|
97
|
+
const apikey = process.env.AGENTUITY_SDK_KEY;
|
|
98
|
+
const url = process.env.AGENTUITY_TRANSPORT_URL;
|
|
99
|
+
if (url && apikey) {
|
|
100
|
+
opts.apiKey = apikey;
|
|
101
|
+
opts.baseURL = url + '/gateway/${name}';
|
|
102
|
+
_args[0] = opts;
|
|
103
|
+
} else {
|
|
104
|
+
${generateEnvWarning(envkey)}
|
|
105
|
+
}
|
|
106
|
+
}`
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function createVercelAIProviderPatch(
|
|
111
|
+
patches: Map<string, PatchModule>,
|
|
112
|
+
module: string,
|
|
113
|
+
createFn: string,
|
|
114
|
+
envkey: string,
|
|
115
|
+
provider: string
|
|
116
|
+
) {
|
|
117
|
+
const patch = {
|
|
118
|
+
module: module,
|
|
119
|
+
functions: {
|
|
120
|
+
[createFn]: {
|
|
121
|
+
before: generateEnvGuard(
|
|
122
|
+
envkey,
|
|
123
|
+
generateVercelAIProvider(provider, envkey),
|
|
124
|
+
`console.log("User provided API Key set for ${provider}. Switch to Agentuity AI Gateway for better logs, metrics and billing.");`
|
|
125
|
+
),
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
patches.set(module, patch);
|
|
130
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { generatePatches as aisdkGeneratePatches } from './aisdk';
|
|
2
|
+
import { type PatchModule, searchBackwards } from './_util';
|
|
3
|
+
|
|
4
|
+
export function generatePatches(): Map<string, PatchModule> {
|
|
5
|
+
const patches = new Map<string, PatchModule>();
|
|
6
|
+
for (const [name, patch] of aisdkGeneratePatches()) {
|
|
7
|
+
patches.set(name, patch);
|
|
8
|
+
}
|
|
9
|
+
return patches;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export async function applyPatch(
|
|
13
|
+
filename: string,
|
|
14
|
+
patch: PatchModule
|
|
15
|
+
): Promise<[string, Bun.Loader]> {
|
|
16
|
+
let contents = await Bun.file(filename).text();
|
|
17
|
+
const isJS = filename.endsWith('.js') || filename.endsWith('.mjs');
|
|
18
|
+
let suffix = '';
|
|
19
|
+
if (patch.functions) {
|
|
20
|
+
for (const fn of Object.keys(patch.functions)) {
|
|
21
|
+
const mod = patch.functions[fn];
|
|
22
|
+
let fnname = `function ${fn}`;
|
|
23
|
+
let index = contents.indexOf(fnname);
|
|
24
|
+
let isConstVariable = false;
|
|
25
|
+
if (index === -1) {
|
|
26
|
+
fnname = 'const ' + fn + ' = ';
|
|
27
|
+
index = contents.indexOf(fnname);
|
|
28
|
+
isConstVariable = true;
|
|
29
|
+
if (index === -1) {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const eol = searchBackwards(contents, index, '\n');
|
|
34
|
+
if (eol < 0) {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
const prefix = contents.substring(eol + 1, index).trim();
|
|
38
|
+
const isAsync = prefix.includes('async');
|
|
39
|
+
const isExport = prefix.includes('export');
|
|
40
|
+
const newname = '__agentuity_' + fn;
|
|
41
|
+
let newfnname: string;
|
|
42
|
+
if (isConstVariable) {
|
|
43
|
+
newfnname = 'const ' + newname + ' = ';
|
|
44
|
+
} else {
|
|
45
|
+
newfnname = 'function ' + newname;
|
|
46
|
+
}
|
|
47
|
+
let fnprefix = '';
|
|
48
|
+
if (isAsync) {
|
|
49
|
+
fnprefix = 'async ';
|
|
50
|
+
}
|
|
51
|
+
if (isExport) {
|
|
52
|
+
fnprefix += 'export ' + fnprefix;
|
|
53
|
+
}
|
|
54
|
+
contents = contents.replace(fnname, newfnname);
|
|
55
|
+
if (isJS) {
|
|
56
|
+
suffix += fnprefix + 'function ' + fn + '() {\n';
|
|
57
|
+
suffix += 'let args = arguments;\n';
|
|
58
|
+
} else {
|
|
59
|
+
suffix += fnprefix + fnname + '(...args) {\n';
|
|
60
|
+
}
|
|
61
|
+
suffix += '\tlet _args = args;\n';
|
|
62
|
+
|
|
63
|
+
if (mod.before) {
|
|
64
|
+
suffix += mod.before;
|
|
65
|
+
suffix += '\n';
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (isJS) {
|
|
69
|
+
// For JS: use .apply to preserve 'this' context
|
|
70
|
+
suffix += '\tlet result = ' + newname + '.apply(this, _args);\n';
|
|
71
|
+
} else {
|
|
72
|
+
// For TS: use spread operator
|
|
73
|
+
suffix += '\tlet result = ' + newname + '(..._args);\n';
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (isAsync) {
|
|
77
|
+
suffix += '\tif (result instanceof Promise) {\n';
|
|
78
|
+
suffix += '\t\tresult = await result;\n';
|
|
79
|
+
suffix += '\t}\n';
|
|
80
|
+
}
|
|
81
|
+
if (mod.after) {
|
|
82
|
+
suffix += mod.after;
|
|
83
|
+
suffix += '\n';
|
|
84
|
+
}
|
|
85
|
+
suffix += '\treturn result;\n';
|
|
86
|
+
suffix += '}\n';
|
|
87
|
+
contents = contents + '\n' + suffix;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (patch.body?.before) {
|
|
91
|
+
contents = patch.body.before + '\n' + contents;
|
|
92
|
+
}
|
|
93
|
+
if (patch.body?.after) {
|
|
94
|
+
contents = contents + '\n' + patch.body.after;
|
|
95
|
+
}
|
|
96
|
+
return [contents, isJS ? 'js' : 'ts'];
|
|
97
|
+
}
|
package/src/cmd/bundle/plugin.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { BunPlugin } from 'bun';
|
|
|
2
2
|
import { dirname, basename, join } from 'node:path';
|
|
3
3
|
import { existsSync, writeFileSync } from 'node:fs';
|
|
4
4
|
import { parseAgentMetadata } from './ast';
|
|
5
|
+
import { applyPatch, generatePatches } from './patch';
|
|
5
6
|
|
|
6
7
|
function toCamelCase(str: string): string {
|
|
7
8
|
return str
|
|
@@ -140,6 +141,30 @@ const AgentuityBundler: BunPlugin = {
|
|
|
140
141
|
};
|
|
141
142
|
});
|
|
142
143
|
|
|
144
|
+
const patches = generatePatches();
|
|
145
|
+
for (const [, patch] of patches) {
|
|
146
|
+
let modulePath = join('node_modules', patch.module, '.*');
|
|
147
|
+
if (patch.filename) {
|
|
148
|
+
modulePath = join('node_modules', patch.module, patch.filename + '.*');
|
|
149
|
+
}
|
|
150
|
+
build.onLoad(
|
|
151
|
+
{
|
|
152
|
+
filter: new RegExp(modulePath),
|
|
153
|
+
namespace: 'file',
|
|
154
|
+
},
|
|
155
|
+
async (args) => {
|
|
156
|
+
if (build.config.target !== 'bun') {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
const [contents, loader] = await applyPatch(args.path, patch);
|
|
160
|
+
return {
|
|
161
|
+
contents,
|
|
162
|
+
loader,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
143
168
|
build.onLoad(
|
|
144
169
|
{
|
|
145
170
|
filter: new RegExp(join(rootDir, 'app.ts')),
|
package/src/cmd/dev/index.ts
CHANGED
|
@@ -233,6 +233,11 @@ export const command = createCommand({
|
|
|
233
233
|
process.exit(exitCode);
|
|
234
234
|
}
|
|
235
235
|
// Non-zero exit codes are treated as restartable failures
|
|
236
|
+
// But if it's exit code 1 (common error exit), also exit the CLI
|
|
237
|
+
if (exitCode === 1 && !shuttingDownForRestart) {
|
|
238
|
+
logger.trace('Server exited with error code 1, stopping CLI');
|
|
239
|
+
process.exit(exitCode);
|
|
240
|
+
}
|
|
236
241
|
})
|
|
237
242
|
.catch((error) => {
|
|
238
243
|
logger.trace(
|