1- import { PutObjectCommand , S3Client } from '@aws-sdk/client-s3' ;
1+ import { s3Client } from '@/app/s3' ;
2+ import { PutObjectCommand } from '@aws-sdk/client-s3' ;
23import type { UIMessage , UIMessageStreamWriter } from 'ai' ;
34import { tool } from 'ai' ;
45import z from 'zod/v3' ;
@@ -8,25 +9,14 @@ interface Params {
89 writer : UIMessageStreamWriter < UIMessage < never , DataPart > > ;
910}
1011
11- const DEFAULTS = {
12- bucket : 'comp-testing-lambda-tasks' ,
13- region : 'us-east-1' ,
14- orgId : 'org_689ce3dced87cc45f600a04b' ,
15- taskId : 'tsk_689ce3dd6f19f4cf1f0ea061' ,
16- } ;
17-
1812const inputSchema = z . object ( {
1913 content : z . string ( ) . min ( 1 ) . describe ( 'The full file content to store' ) ,
20- bucket : z . string ( ) . optional ( ) . describe ( 'Target S3 bucket' ) ,
21- region : z . string ( ) . optional ( ) . describe ( 'AWS region for the S3 bucket' ) ,
2214 orgId : z . string ( ) . optional ( ) . describe ( 'Organization identifier' ) ,
2315 taskId : z . string ( ) . optional ( ) . describe ( 'Task identifier' ) ,
2416 contentType : z . string ( ) . optional ( ) . describe ( 'MIME type, defaults to text/plain for generic code' ) ,
2517} ) ;
2618interface ToolInput {
2719 content : string ;
28- bucket ?: string ;
29- region ?: string ;
3020 orgId ?: string ;
3121 taskId ?: string ;
3222 contentType ?: string ;
@@ -41,7 +31,7 @@ export const storeToS3 = ({ writer }: Params) => {
4131 const { toolCallId } = ctx ;
4232 const parsed : unknown = inputSchema . parse ( args ) ;
4333 const input = parsed as ToolInput ;
44- const { content, bucket , region , orgId, taskId, contentType } = input ;
34+ const { content, orgId, taskId, contentType } = input ;
4535
4636 // Validate task format: must export a function via module.exports
4737 // Validate task format: must export a function with only event parameter
@@ -79,10 +69,9 @@ export const storeToS3 = ({ writer }: Params) => {
7969 } ) ;
8070 return message ;
8171 }
82- const resolvedBucket = bucket || DEFAULTS . bucket ;
83- const resolvedRegion = region || DEFAULTS . region ;
84- const resolvedOrgId = orgId || DEFAULTS . orgId ;
85- const resolvedTaskId = taskId || DEFAULTS . taskId ;
72+ const resolvedBucket = process . env . TASKS_AUTOMATION_BUCKET ;
73+ const resolvedOrgId = orgId ;
74+ const resolvedTaskId = taskId ;
8675 const keyBase = `${ resolvedOrgId } /${ resolvedTaskId } ` ;
8776 const key = `${ keyBase } .automation.js` ;
8877
@@ -93,20 +82,11 @@ export const storeToS3 = ({ writer }: Params) => {
9382 status : 'uploading' ,
9483 bucket : resolvedBucket ,
9584 key,
96- region : resolvedRegion ,
9785 } ,
9886 } ) ;
9987
10088 try {
101- const credentials =
102- process . env . APP_AWS_ACCESS_KEY_ID && process . env . APP_AWS_SECRET_ACCESS_KEY
103- ? {
104- accessKeyId : process . env . APP_AWS_ACCESS_KEY_ID as string ,
105- secretAccessKey : process . env . APP_AWS_SECRET_ACCESS_KEY as string ,
106- }
107- : undefined ;
108- const s3 = new S3Client ( { region : resolvedRegion , credentials } ) ;
109- await s3 . send (
89+ await s3Client . send (
11090 new PutObjectCommand ( {
11191 Bucket : resolvedBucket ,
11292 Key : key ,
@@ -130,7 +110,6 @@ export const storeToS3 = ({ writer }: Params) => {
130110 status : 'done' ,
131111 bucket : resolvedBucket ,
132112 key,
133- region : resolvedRegion ,
134113 } ,
135114 } ) ;
136115
@@ -144,7 +123,6 @@ export const storeToS3 = ({ writer }: Params) => {
144123 status : 'error' ,
145124 bucket : resolvedBucket ,
146125 key,
147- region : resolvedRegion ,
148126 error : { message } ,
149127 } ,
150128 } ) ;
0 commit comments