@astrive-ai/agent 1.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.
@@ -0,0 +1,70 @@
1
+ import { IProvider, Message } from "@astrive-ai/types";
2
+
3
+ export interface ReflectionContext {
4
+ provider: IProvider;
5
+ originalInput: string;
6
+ }
7
+
8
+ export interface ReviewResult {
9
+ hasIssues: boolean;
10
+ issues: string[];
11
+ suggestions: string[];
12
+ }
13
+
14
+ export class ReflectionEngine {
15
+ public async reflect(response: string, context: ReflectionContext): Promise<string> {
16
+ const review = await this.reviewResponse(response, context.provider);
17
+
18
+ if (review.hasIssues) {
19
+ const prompt = `The previous response to "${context.originalInput}" had issues. Please improve it based on this feedback:
20
+ Issues: ${review.issues.join(", ")}
21
+ Suggestions: ${review.suggestions.join(", ")}
22
+ Original response: ${response}
23
+
24
+ Please provide the corrected response.`;
25
+
26
+ const improvedResponse = await context.provider.chat({
27
+ messages: [
28
+ { role: "system", content: "You are an expert editor who improves responses based on feedback. Provide ONLY the improved response." },
29
+ { role: "user", content: prompt }
30
+ ],
31
+ model: "llama-3.3-70b-versatile"
32
+ });
33
+
34
+ return improvedResponse.content || response;
35
+ }
36
+
37
+ return response;
38
+ }
39
+
40
+ private async reviewResponse(response: string, provider: IProvider): Promise<ReviewResult> {
41
+ const prompt = `Review the following response for accuracy, clarity, and completeness.
42
+ Response: "${response}"
43
+
44
+ Provide your review in the following JSON format ONLY:
45
+ {
46
+ "hasIssues": boolean,
47
+ "issues": ["list of specific issues"],
48
+ "suggestions": ["list of actionable suggestions to fix issues"]
49
+ }`;
50
+
51
+ try {
52
+ const reviewResponse = await provider.chat({
53
+ messages: [
54
+ { role: "system", content: "You are a critical reviewer. Always respond with valid JSON matching the requested schema." },
55
+ { role: "user", content: prompt }
56
+ ],
57
+ model: "llama-3.3-70b-versatile"
58
+ });
59
+
60
+ const content = (reviewResponse.content || "").trim();
61
+ const jsonStart = content.indexOf('{');
62
+ const jsonEnd = content.lastIndexOf('}') + 1;
63
+ const jsonString = content.slice(jsonStart, jsonEnd);
64
+
65
+ return JSON.parse(jsonString) as ReviewResult;
66
+ } catch (e) {
67
+ return { hasIssues: false, issues: [], suggestions: [] };
68
+ }
69
+ }
70
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "rootDir": "./src"
6
+ },
7
+ "include": ["src/**/*"]
8
+ }