@cogitator-ai/self-modifying 17.0.17 → 18.0.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.
Files changed (114) hide show
  1. package/README.md +112 -48
  2. package/dist/architecture-evolution/capability-analyzer.d.ts.map +1 -1
  3. package/dist/architecture-evolution/capability-analyzer.js +44 -40
  4. package/dist/architecture-evolution/capability-analyzer.js.map +1 -1
  5. package/dist/architecture-evolution/evolution-strategy.d.ts.map +1 -1
  6. package/dist/architecture-evolution/evolution-strategy.js +4 -2
  7. package/dist/architecture-evolution/evolution-strategy.js.map +1 -1
  8. package/dist/architecture-evolution/index.d.ts +2 -2
  9. package/dist/architecture-evolution/index.d.ts.map +1 -1
  10. package/dist/architecture-evolution/index.js +1 -1
  11. package/dist/architecture-evolution/index.js.map +1 -1
  12. package/dist/architecture-evolution/parameter-optimizer.d.ts +5 -4
  13. package/dist/architecture-evolution/parameter-optimizer.d.ts.map +1 -1
  14. package/dist/architecture-evolution/parameter-optimizer.js +62 -34
  15. package/dist/architecture-evolution/parameter-optimizer.js.map +1 -1
  16. package/dist/architecture-evolution/prompts.d.ts +21 -2
  17. package/dist/architecture-evolution/prompts.d.ts.map +1 -1
  18. package/dist/architecture-evolution/prompts.js +189 -37
  19. package/dist/architecture-evolution/prompts.js.map +1 -1
  20. package/dist/constraints/modification-validator.d.ts +1 -0
  21. package/dist/constraints/modification-validator.d.ts.map +1 -1
  22. package/dist/constraints/modification-validator.js +45 -18
  23. package/dist/constraints/modification-validator.js.map +1 -1
  24. package/dist/constraints/rollback-manager.d.ts +1 -0
  25. package/dist/constraints/rollback-manager.d.ts.map +1 -1
  26. package/dist/constraints/rollback-manager.js +25 -1
  27. package/dist/constraints/rollback-manager.js.map +1 -1
  28. package/dist/events/event-emitter.d.ts +0 -1
  29. package/dist/events/event-emitter.d.ts.map +1 -1
  30. package/dist/events/event-emitter.js +6 -10
  31. package/dist/events/event-emitter.js.map +1 -1
  32. package/dist/index.d.ts +4 -4
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/index.js +3 -3
  35. package/dist/index.js.map +1 -1
  36. package/dist/meta-reasoning/meta-reasoner.d.ts +4 -1
  37. package/dist/meta-reasoning/meta-reasoner.d.ts.map +1 -1
  38. package/dist/meta-reasoning/meta-reasoner.js +120 -43
  39. package/dist/meta-reasoning/meta-reasoner.js.map +1 -1
  40. package/dist/meta-reasoning/observation-collector.d.ts.map +1 -1
  41. package/dist/meta-reasoning/observation-collector.js +6 -2
  42. package/dist/meta-reasoning/observation-collector.js.map +1 -1
  43. package/dist/meta-reasoning/prompts.d.ts +10 -14
  44. package/dist/meta-reasoning/prompts.d.ts.map +1 -1
  45. package/dist/meta-reasoning/prompts.js +108 -33
  46. package/dist/meta-reasoning/prompts.js.map +1 -1
  47. package/dist/meta-reasoning/strategy-selector.d.ts.map +1 -1
  48. package/dist/meta-reasoning/strategy-selector.js +6 -0
  49. package/dist/meta-reasoning/strategy-selector.js.map +1 -1
  50. package/dist/self-modifying-agent.d.ts +88 -15
  51. package/dist/self-modifying-agent.d.ts.map +1 -1
  52. package/dist/self-modifying-agent.js +583 -376
  53. package/dist/self-modifying-agent.js.map +1 -1
  54. package/dist/tool-generation/gap-analyzer.d.ts +0 -1
  55. package/dist/tool-generation/gap-analyzer.d.ts.map +1 -1
  56. package/dist/tool-generation/gap-analyzer.js +16 -15
  57. package/dist/tool-generation/gap-analyzer.js.map +1 -1
  58. package/dist/tool-generation/generated-tool-store.d.ts.map +1 -1
  59. package/dist/tool-generation/generated-tool-store.js +90 -10
  60. package/dist/tool-generation/generated-tool-store.js.map +1 -1
  61. package/dist/tool-generation/index.d.ts +3 -3
  62. package/dist/tool-generation/index.d.ts.map +1 -1
  63. package/dist/tool-generation/index.js +2 -2
  64. package/dist/tool-generation/index.js.map +1 -1
  65. package/dist/tool-generation/prompts.d.ts +4 -3
  66. package/dist/tool-generation/prompts.d.ts.map +1 -1
  67. package/dist/tool-generation/prompts.js +96 -18
  68. package/dist/tool-generation/prompts.js.map +1 -1
  69. package/dist/tool-generation/tool-generator.d.ts +6 -6
  70. package/dist/tool-generation/tool-generator.d.ts.map +1 -1
  71. package/dist/tool-generation/tool-generator.js +88 -23
  72. package/dist/tool-generation/tool-generator.js.map +1 -1
  73. package/dist/tool-generation/tool-sandbox.d.ts +10 -11
  74. package/dist/tool-generation/tool-sandbox.d.ts.map +1 -1
  75. package/dist/tool-generation/tool-sandbox.js +346 -156
  76. package/dist/tool-generation/tool-sandbox.js.map +1 -1
  77. package/dist/tool-generation/tool-validator.d.ts +4 -8
  78. package/dist/tool-generation/tool-validator.d.ts.map +1 -1
  79. package/dist/tool-generation/tool-validator.js +74 -28
  80. package/dist/tool-generation/tool-validator.js.map +1 -1
  81. package/dist/utils/index.d.ts.map +1 -1
  82. package/dist/utils/index.js +30 -5
  83. package/dist/utils/index.js.map +1 -1
  84. package/dist/utils/llm-helper.d.ts.map +1 -1
  85. package/dist/utils/llm-helper.js +1 -0
  86. package/dist/utils/llm-helper.js.map +1 -1
  87. package/package.json +3 -12
  88. package/src/__tests__/component-regressions.test.ts +599 -0
  89. package/src/__tests__/meta-reasoning.test.ts +5 -1
  90. package/src/__tests__/sandbox-security.test.ts +186 -0
  91. package/src/__tests__/self-modifying-agent.test.ts +405 -11
  92. package/src/architecture-evolution/capability-analyzer.ts +63 -44
  93. package/src/architecture-evolution/evolution-strategy.ts +5 -2
  94. package/src/architecture-evolution/index.ts +2 -0
  95. package/src/architecture-evolution/parameter-optimizer.ts +82 -40
  96. package/src/architecture-evolution/prompts.ts +224 -42
  97. package/src/constraints/modification-validator.ts +48 -21
  98. package/src/constraints/rollback-manager.ts +20 -1
  99. package/src/events/event-emitter.ts +6 -15
  100. package/src/index.ts +14 -0
  101. package/src/meta-reasoning/meta-reasoner.ts +124 -52
  102. package/src/meta-reasoning/observation-collector.ts +6 -2
  103. package/src/meta-reasoning/prompts.ts +131 -56
  104. package/src/meta-reasoning/strategy-selector.ts +6 -0
  105. package/src/self-modifying-agent.ts +797 -413
  106. package/src/tool-generation/gap-analyzer.ts +21 -19
  107. package/src/tool-generation/generated-tool-store.ts +97 -10
  108. package/src/tool-generation/index.ts +13 -3
  109. package/src/tool-generation/prompts.ts +120 -18
  110. package/src/tool-generation/tool-generator.ts +120 -26
  111. package/src/tool-generation/tool-sandbox.ts +408 -206
  112. package/src/tool-generation/tool-validator.ts +90 -37
  113. package/src/utils/index.ts +29 -5
  114. package/src/utils/llm-helper.ts +1 -0
@@ -1,5 +1,6 @@
1
1
  import type {
2
2
  Tool,
3
+ ToolSchema,
3
4
  LLMBackend,
4
5
  CapabilityGap,
5
6
  GeneratedTool,
@@ -8,7 +9,8 @@ import type {
8
9
  } from '@cogitator-ai/types';
9
10
  import { z, type ZodType } from 'zod';
10
11
  import { ToolValidator } from './tool-validator';
11
- import { ToolSandbox } from './tool-sandbox';
12
+ import { ToolSandbox, type SandboxTestCase } from './tool-sandbox';
13
+ import { llmChat } from '../utils/llm-helper';
12
14
  import {
13
15
  TOOL_GENERATION_SYSTEM_PROMPT,
14
16
  buildToolGenerationPrompt,
@@ -22,6 +24,10 @@ export interface ToolGeneratorOptions {
22
24
  model?: string;
23
25
  }
24
26
 
27
+ export interface GenerateOptions {
28
+ parameters?: Record<string, unknown>;
29
+ }
30
+
25
31
  export interface GenerationResult {
26
32
  tool: GeneratedTool | null;
27
33
  validationResult: ToolValidationResult | null;
@@ -52,7 +58,8 @@ export class ToolGenerator {
52
58
  async generate(
53
59
  gap: CapabilityGap,
54
60
  existingTools: Tool[],
55
- testCases?: Array<{ input: unknown; expectedOutput?: unknown }>
61
+ testCases?: SandboxTestCase[],
62
+ options: GenerateOptions = {}
56
63
  ): Promise<GenerationResult> {
57
64
  let currentTool: GeneratedTool | null = null;
58
65
  let validationResult: ToolValidationResult | null = null;
@@ -65,15 +72,23 @@ export class ToolGenerator {
65
72
 
66
73
  try {
67
74
  if (currentTool === null) {
68
- currentTool = await this.generateInitial(gap, existingTools);
75
+ currentTool = await this.generateInitial(gap, existingTools, options.parameters);
69
76
  } else if (validationResult) {
70
- currentTool = await this.improve(currentTool, validationResult, iterations);
77
+ const improved = await this.improve(currentTool, validationResult, iterations);
78
+ if (!improved) {
79
+ continue;
80
+ }
81
+ currentTool = improved;
71
82
  }
72
83
 
73
84
  if (!currentTool) {
74
85
  continue;
75
86
  }
76
87
 
88
+ if (options.parameters) {
89
+ currentTool.parameters = options.parameters;
90
+ }
91
+
77
92
  validationResult = await this.validator.validate(currentTool, testCases);
78
93
 
79
94
  if (validationResult.isValid) {
@@ -120,7 +135,7 @@ export class ToolGenerator {
120
135
  async generateQuick(
121
136
  description: string,
122
137
  name: string,
123
- _parameters: Record<string, unknown>
138
+ parameters: Record<string, unknown>
124
139
  ): Promise<GeneratedTool | null> {
125
140
  const gap: CapabilityGap = {
126
141
  id: `quick_${Date.now()}`,
@@ -132,13 +147,22 @@ export class ToolGenerator {
132
147
  reasoning: 'User-requested quick generation',
133
148
  };
134
149
 
135
- const result = await this.generate(gap, []);
136
- return result.tool;
150
+ const result = await this.generate(
151
+ gap,
152
+ [],
153
+ undefined,
154
+ Object.keys(parameters).length > 0
155
+ ? { parameters: { type: 'object', properties: parameters } }
156
+ : {}
157
+ );
158
+
159
+ return result.success ? result.tool : null;
137
160
  }
138
161
 
139
162
  private async generateInitial(
140
163
  gap: CapabilityGap,
141
- existingTools: Tool[]
164
+ existingTools: Tool[],
165
+ parameters?: Record<string, unknown>
142
166
  ): Promise<GeneratedTool | null> {
143
167
  const toolSummaries = existingTools.map((t) => ({
144
168
  name: t.name,
@@ -149,17 +173,19 @@ export class ToolGenerator {
149
173
  maxLines: 100,
150
174
  securityLevel: 'strict',
151
175
  allowedModules: this.config.sandboxConfig?.allowedModules,
176
+ parameters,
152
177
  });
153
178
 
154
- const response = await this.callLLM(
179
+ const content = await llmChat(
180
+ this.llm,
155
181
  [
156
182
  { role: 'system', content: TOOL_GENERATION_SYSTEM_PROMPT },
157
183
  { role: 'user', content: prompt },
158
184
  ],
159
- 0.4
185
+ { model: this.model, temperature: 0.4 }
160
186
  );
161
187
 
162
- const tool = parseToolGenerationResponse(response.content);
188
+ const tool = parseToolGenerationResponse(content);
163
189
 
164
190
  if (tool) {
165
191
  tool.metadata = {
@@ -179,18 +205,21 @@ export class ToolGenerator {
179
205
  ): Promise<GeneratedTool | null> {
180
206
  const prompt = buildToolImprovementPrompt(tool, validationResult, iteration);
181
207
 
182
- const response = await this.callLLM(
208
+ const content = await llmChat(
209
+ this.llm,
183
210
  [
184
211
  { role: 'system', content: TOOL_GENERATION_SYSTEM_PROMPT },
185
212
  { role: 'user', content: prompt },
186
213
  ],
187
- 0.3
214
+ { model: this.model, temperature: 0.3 }
188
215
  );
189
216
 
190
- const improved = parseToolGenerationResponse(response.content);
217
+ const improved = parseToolGenerationResponse(content);
191
218
 
192
219
  if (improved) {
193
220
  improved.id = tool.id;
221
+ improved.name = tool.name;
222
+ improved.createdAt = tool.createdAt;
194
223
  improved.version = tool.version + 1;
195
224
  improved.metadata = {
196
225
  ...tool.metadata,
@@ -206,9 +235,18 @@ export class ToolGenerator {
206
235
  createExecutableTool(generated: GeneratedTool): Tool {
207
236
  const sandbox = this.sandbox;
208
237
  const tool = generated;
238
+ const paramSchema = buildZodSchema(generated.parameters);
239
+ const jsonSchema = toToolJsonSchema(generated.parameters);
209
240
 
210
241
  const execute = async (params: unknown): Promise<unknown> => {
211
- const result = await sandbox.execute(tool, params);
242
+ const parsed = paramSchema.safeParse(params);
243
+ if (!parsed.success) {
244
+ throw new Error(
245
+ `Invalid parameters: ${parsed.error.issues.map((i) => `${i.path.join('.')}: ${i.message}`).join('; ')}`
246
+ );
247
+ }
248
+
249
+ const result = await sandbox.execute(tool, parsed.data);
212
250
  if (!result.success) {
213
251
  throw new Error(result.error ?? 'Tool execution failed');
214
252
  }
@@ -218,26 +256,82 @@ export class ToolGenerator {
218
256
  return {
219
257
  name: generated.name,
220
258
  description: generated.description,
221
- parameters: z.record(z.string(), z.unknown()) as ZodType<unknown>,
259
+ parameters: paramSchema as ZodType<unknown>,
222
260
  execute,
223
261
  toJSON: () => ({
224
262
  name: generated.name,
225
263
  description: generated.description,
226
- parameters: {
227
- type: 'object' as const,
228
- properties: generated.parameters as Record<string, unknown>,
229
- },
264
+ parameters: jsonSchema,
230
265
  }),
231
266
  };
232
267
  }
268
+ }
269
+
270
+ function toToolJsonSchema(parameters: Record<string, unknown>): ToolSchema['parameters'] {
271
+ const properties =
272
+ parameters.type === 'object' &&
273
+ parameters.properties &&
274
+ typeof parameters.properties === 'object' &&
275
+ !Array.isArray(parameters.properties)
276
+ ? (parameters.properties as Record<string, unknown>)
277
+ : {};
278
+ const required = Array.isArray(parameters.required)
279
+ ? parameters.required.filter((key): key is string => typeof key === 'string')
280
+ : [];
281
+
282
+ return required.length > 0
283
+ ? { type: 'object', properties, required }
284
+ : { type: 'object', properties };
285
+ }
233
286
 
234
- private async callLLM(
235
- messages: Array<{ role: 'system' | 'user' | 'assistant'; content: string }>,
236
- temperature: number
287
+ function buildZodSchema(parameters: Record<string, unknown>): ZodType<unknown> {
288
+ if (
289
+ parameters.type !== 'object' ||
290
+ !parameters.properties ||
291
+ typeof parameters.properties !== 'object'
237
292
  ) {
238
- if (this.llm.complete) {
239
- return this.llm.complete({ messages, temperature });
293
+ return z.record(z.string(), z.unknown());
294
+ }
295
+
296
+ const properties = parameters.properties as Record<string, Record<string, unknown>>;
297
+ const required = Array.isArray(parameters.required)
298
+ ? new Set(parameters.required as string[])
299
+ : new Set<string>();
300
+
301
+ const shape: Record<string, ZodType<unknown>> = {};
302
+
303
+ for (const [key, propSchema] of Object.entries(properties)) {
304
+ let fieldSchema: ZodType<unknown> = jsonTypeToZod(propSchema.type as string | undefined);
305
+
306
+ if (!required.has(key)) {
307
+ fieldSchema = fieldSchema.optional() as ZodType<unknown>;
240
308
  }
241
- return this.llm.chat({ model: this.model, messages, temperature });
309
+
310
+ shape[key] = fieldSchema;
311
+ }
312
+
313
+ if (Object.keys(shape).length === 0) {
314
+ return z.record(z.string(), z.unknown());
315
+ }
316
+
317
+ return z.object(shape).passthrough() as ZodType<unknown>;
318
+ }
319
+
320
+ function jsonTypeToZod(type?: string): ZodType<unknown> {
321
+ switch (type) {
322
+ case 'string':
323
+ return z.string();
324
+ case 'number':
325
+ return z.number();
326
+ case 'integer':
327
+ return z.number().int();
328
+ case 'boolean':
329
+ return z.boolean();
330
+ case 'array':
331
+ return z.array(z.unknown());
332
+ case 'object':
333
+ return z.record(z.string(), z.unknown());
334
+ default:
335
+ return z.unknown();
242
336
  }
243
337
  }