File size: 5,055 Bytes
4381a41
 
 
 
2a0602e
 
 
4381a41
2a0602e
 
 
 
 
 
 
4381a41
2a0602e
 
 
4381a41
 
 
 
2a0602e
4381a41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const fs = require('fs');
const path = require('path');
const { nanoid } = require('nanoid');

// Persist tools to /data (HF persistent storage) when USE_PERSISTENT_STORAGE
// is explicitly set. Defaults to the local tools/ directory to avoid hanging
// on a slow/broken network mount during startup.
function resolveBaseDir() {
  if (process.env.USE_PERSISTENT_STORAGE) {
    const candidate = '/data/forge-tools';
    try {
      fs.mkdirSync(candidate, { recursive: true });
      fs.accessSync(candidate, fs.constants.W_OK);
      return candidate;
    } catch {}
  }
  const fallback = path.join(__dirname, '..', 'tools');
  fs.mkdirSync(fallback, { recursive: true });
  return fallback;
}

const BASE_DIR = resolveBaseDir();
const MANIFEST_PATH = path.join(BASE_DIR, 'manifest.json');
const usingPersistentStorage = !!(process.env.USE_PERSISTENT_STORAGE && BASE_DIR.startsWith('/data'));

function loadManifest() {
  try {
    return JSON.parse(fs.readFileSync(MANIFEST_PATH, 'utf8'));
  } catch {
    return [];
  }
}

function saveManifest(tools) {
  fs.writeFileSync(MANIFEST_PATH, JSON.stringify(tools, null, 2));
}

let tools = loadManifest();

function persist() {
  saveManifest(tools);
}

function listTools(status) {
  return status ? tools.filter((t) => t.status === status) : tools;
}

function getToolById(id) {
  return tools.find((t) => t.id === id);
}

function getToolByName(name) {
  return tools.find((t) => t.name === name && t.status === 'approved');
}

// Tool definitions formatted for the Inference Port chat/completions "tools" param.
function getApprovedToolDefs() {
  return tools
    .filter((t) => t.status === 'approved')
    .map((t) => ({
      type: 'function',
      function: {
        name: t.name,
        description: t.description,
        parameters: t.parameters && Object.keys(t.parameters).length
          ? t.parameters
          : { type: 'object', properties: {} },
      },
    }));
}

function proposeTool({ name, description, parameters, code, reason }) {
  const safeName = String(name).trim().toLowerCase().replace(/[^a-z0-9_]/g, '_').slice(0, 64);
  const entry = {
    id: nanoid(10),
    name: safeName,
    description: description || '',
    parameters: parameters || { type: 'object', properties: {} },
    code,
    reason: reason || '',
    status: 'pending',
    kind: 'new',
    version: 1,
    createdAt: new Date().toISOString(),
    lastError: null,
    fixesToolId: null,
  };
  tools.push(entry);
  persist();
  return entry;
}

// A fix is proposed as its own pending entry linked back to the broken tool.
// The original tool stays approved and usable (with lastError noted) until
// the fix itself is approved, at which point it replaces the original code.
function proposeFix(originalId, newCode, reason) {
  const original = getToolById(originalId);
  if (!original) throw new Error(`No tool with id ${originalId}`);
  const entry = {
    id: nanoid(10),
    name: original.name,
    description: original.description,
    parameters: original.parameters,
    code: newCode,
    reason: reason || 'Automatic fix for a runtime error',
    status: 'pending',
    kind: 'fix',
    version: (original.version || 1) + 1,
    createdAt: new Date().toISOString(),
    lastError: null,
    fixesToolId: originalId,
  };
  tools.push(entry);
  persist();
  return entry;
}

function approveTool(id) {
  const entry = getToolById(id);
  if (!entry) throw new Error(`No tool with id ${id}`);
  entry.status = 'approved';

  if (entry.kind === 'fix' && entry.fixesToolId) {
    const original = getToolById(entry.fixesToolId);
    if (original) {
      original.code = entry.code;
      original.version = entry.version;
      original.lastError = null;
      original.status = 'approved';
      // The fix entry itself is folded into the original; drop the duplicate.
      tools = tools.filter((t) => t.id !== entry.id);
      persist();
      return original;
    }
  }
  persist();
  return entry;
}

function rejectTool(id, reason) {
  const entry = getToolById(id);
  if (!entry) throw new Error(`No tool with id ${id}`);
  entry.status = 'rejected';
  entry.rejectReason = reason || '';
  if (entry.kind === 'fix') {
    // Reject the fix but leave the original tool as-is (still broken, still approved
    // so the agent can decide whether to try a different fix or a different approach).
    // We snapshot a copy before removing it from the persisted list so the caller
    // still gets a "rejected" object back for display purposes.
    const snapshot = { ...entry };
    tools = tools.filter((t) => t.id !== entry.id);
    persist();
    return snapshot;
  }
  persist();
  return entry;
}

function recordError(id, error) {
  const entry = getToolById(id);
  if (!entry) return;
  entry.lastError = { message: error.message, at: new Date().toISOString() };
  persist();
}

module.exports = {
  BASE_DIR,
  usingPersistentStorage,
  listTools,
  getToolById,
  getToolByName,
  getApprovedToolDefs,
  proposeTool,
  proposeFix,
  approveTool,
  rejectTool,
  recordError,
};