File size: 1,437 Bytes
d47b053
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import type { StudioToolDefinition, StudioToolResult } from '../domain/types'
import type { StudioRuntimeBackedToolContext } from '../runtime/tool-runtime-context'
import { toWorkspaceRelativePath } from './workspace-paths'
import { writeWorkspaceFile } from './workspace-edits'

interface WriteToolInput {
  path?: string
  file?: string
  content?: string
}

export function createStudioWriteTool(): StudioToolDefinition<WriteToolInput> {
  return {
    name: 'write',
    description: 'Write a file in the current workspace.',
    category: 'edit',
    permission: 'write',
    allowedAgents: ['builder'],
    requiresTask: false,
    execute: async (input, context) => executeWriteTool(input, context as StudioRuntimeBackedToolContext)
  }
}

async function executeWriteTool(input: WriteToolInput, context: StudioRuntimeBackedToolContext): Promise<StudioToolResult> {
  const target = input.path ?? input.file
  if (!target) {
    throw new Error('Write tool requires "path" or "file"')
  }

  const result = await writeWorkspaceFile(context.session.directory, target, input.content ?? '')
  const relativePath = toWorkspaceRelativePath(context.session.directory, result.absolutePath).replace(/\\/g, '/')

  return {
    title: `Wrote ${relativePath}`,
    output: `File written successfully: ${relativePath}`,
    metadata: {
      path: relativePath,
      absolutePath: result.absolutePath,
      bytes: result.bytes
    }
  }
}