-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Updates #37
fix: Updates #37
Conversation
Caution Review failedThe pull request is closed. WalkthroughThis pull request introduces a comprehensive refactoring of the core package's module structure, focusing on centralizing type definitions and improving the organization of import statements. The changes primarily involve moving type imports to a local Changes
Sequence DiagramsequenceDiagram
participant User
participant Bot
participant Orchestrator
participant InputHandler
participant LLMClient
participant OutputHandler
User->>Bot: Send message
Bot->>Orchestrator: Dispatch input
Orchestrator->>InputHandler: Process input
InputHandler->>LLMClient: Generate response
LLMClient-->>InputHandler: Return processed content
InputHandler->>Orchestrator: Return result
Orchestrator->>OutputHandler: Dispatch output
OutputHandler->>User: Send reply
Possibly related PRs
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (23)
✨ Finishing Touches
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
🔭 Outside diff range comments (1)
packages/core/src/core/processor.ts (1)
Line range hint
216-311
: Consider breaking up thegenerateContentId
method.
This method covers many scenarios (tweets, arrays, objects). Splitting each scenario into helper functions would reduce complexity and improve readability. Also ensure random suffix usage is cryptographically sufficient if collisions are a concern.
🧹 Nitpick comments (16)
packages/core/src/core/processor.ts (5)
81-82
: Review prompt injection concerns.
You're exposing raw user/content text in the prompt. If malicious strings are inserted, they could override the system instructions. Consider minimal or sanitized embedding of user content in the prompt.
84-84
: Clarify the “Related Context” annotation.
Add a heading or separator to visually distinguish related context from the content in the final prompt, reducing confusion and risk of misinterpretation by the LLM.
87-89
: Limit disclosure of character data if sensitive.
Exposing your entire character JSON might be too verbose or could reveal internal logic not intended for the user. Evaluate whether it’s fully necessary for the LLM.
90-98
: Avoid prompt bloat with many outputs.
Embedding all available outputs in the prompt helps the LLM know the scope. However, if the list of outputs grows, consider dynamic or paginated approaches to limit token consumption.
124-125
: Ensure system prompt remains well-defined.
Your system prompt is appended inline. Consider a dedicated constant or separate file to ensure consistency and maintainability.packages/core/src/core/orchestrator.ts (2)
258-334
: Nice expansion with runAutonomousFlow.
A queue-based approach for chaining suggested outputs or actions is a creative design. Keep in mind that deeper nested flows might require cycle detection or recursion guards.
336-389
: Graceful fallback on missing or invalid input.
This method logs errors thoroughly, which is helpful. Consider returning structured errors or partial responses if needed for better upstream debugging.examples/example-twitter.ts (2)
102-102
: Adjust intervals if needed.
role: HandlerRole.INPUT
with 5-minute intervals for “consciousness_thoughts” is good for an occasional background task. Just be mindful of performance overhead.
125-125
: Validate tweet constraints.
The.regex(/^[\x20-\x7E]*$/)
is a good start, but confirm 280-char limit if your domain absolutely requires it for typical Tweets.examples/example-api.ts (5)
23-32
: Consider documenting the main function’s purpose and parameters more explicitly.
Providing additional docstrings or inline comments clarifying how developers can extend or customize the bot would be beneficial for maintainability.
31-31
: Be cautious about purging data in non-development environments.
Purging the vector database on every startup can lead to data loss in production. Consider making this an optional or environment-specific step.
62-80
: Handle rate limits or unexpected errors when fetching GitHub issues.
GitHub’s API can return rate-limit or error responses, which may need more robust handling and logging or retry logic to ensure reliability.
168-222
: Potential concurrency/logging improvements in the readline interface.
If multiple concurrent user inputs or messages are ever processed, you may want to queue or serialize them. Adding detailed logging for each step can also aid debugging, especially when used interactively in production settings.
230-234
: Graceful error handling on startup failures.
The overall approach is good. Ifmain()
throws, the bot logs the error and exits. You could consider a retry or a backoff mechanism for certain recoverable startup errors (e.g., if the DB is temporarily unavailable).packages/core/src/types/index.ts (1)
539-560
: Consider specialized interfaces for INPUT, OUTPUT, and ACTION.
While using a singleIOHandler
interface is flexible, creating separate interfaces for each role can provide stronger type safety and avoid confusion about optional properties like intervals on OUTPUT handlers.package.json (1)
8-8
: Document the new “api” script in the project readme.
Adding a brief usage instruction (e.g., “runpnpm api
to launch the API example”) helps new developers quickly understand how to use the added script.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
examples/example-api.ts
(1 hunks)examples/example-twitter.ts
(8 hunks)package.json
(1 hunks)packages/core/src/core/orchestrator.ts
(4 hunks)packages/core/src/core/processor.ts
(6 hunks)packages/core/src/types/index.ts
(1 hunks)
🔇 Additional comments (20)
packages/core/src/core/processor.ts (8)
17-17
: Leverage typed roles for clarity.
Great job importingHandlerRole
andIOHandler
from a central definition. This ensures consistent type usage across the codebase.
48-48
: Confirm concurrency safety.
If multiple threads callhasProcessedContent
at the same time for the samecontentId
, ensure no data race or duplication occurs.
Line range hint
50-62
: Provide consistent defaults in repeated fields.
WhenhasProcessed
is true, returning placeholders for fields likesentiment
,entities
, andintent
is a good approach to keep the structure consistent. Consider clearly labeling them as defaults in logs for clarity.
72-79
: Handle empty related memories.
Retrieving related memories (with a max of 3) is fine, but ensure the system gracefully handles when no memories are found.
99-107
: Use consistent formatting for actions.
The approach here is consistent with # Available Outputs but ensure uniform labeling or grouping for clarity in prompts if the system grows.
108-117
: Validate your custom prompt sections.
The<thinking id="...">
syntax is a creative way to structure prompt instructions. Confirm your LLM properly interprets these sections and doesn’t treat them as literal user input.
160-161
: Confirm marker-based dedup flow.
CallingmarkContentAsProcessed
here updates a global marker. Double-check that simultaneously processed content is handled without collisions.
163-179
: Good structured return.
ReturningalreadyProcessed
along with enriched context is a clear approach that fosters consistent usage across the system.packages/core/src/core/orchestrator.ts (3)
8-8
: Centralize type usage.
ImportingIOHandler
from../types
helps unify the handler definition. Good move ensuring consistent usage across orchestrator and processor.
Line range hint
166-181
: Evaluate the re-scheduling logic.
When re-scheduling recurring inputs, confirm that removing the old handler from the queue also occurs if it’s replaced by the newly scheduled one. Otherwise, there might be concurrency or duplication issues.
204-257
: Dispatch to action is well-structured.
The newdispatchToAction
method distinctly enforces the action role, which is good. Consider future expansions for type-checking input data early.examples/example-twitter.ts (6)
11-11
: UseHandlerRole
consistently.
Replacing string literals withHandlerRole
helps maintain a strongly typed schema. This is a good practice across your codebase.
23-23
: Readline import is helpful for local debugging.
Readline fosters user interactivity. It’s wise to ensure it’s abstracted away if production runs need to be headless.
74-77
: Trace intervals.
Registering an input handler fortwitter_mentions
with a 1-minute interval is reasonable. Keep an eye on rate limits or potential over-polling issues.
147-147
: Check reply logic.
You’re capturinginReplyTo
for tweets. If that field is missing or invalid, consider a fallback or an error.
166-175
: Console instructions are user-friendly.
Providing instructions like “type messages, type exit to quit” is a nice user experience improvement.
187-187
: Graceful cleanup on shutdown.
Closing the readline interface and removing handlers ensures a neat exit. Good job.examples/example-api.ts (2)
1-22
: Extensive documentation and imports look great.
This header comment thoroughly explains the bot's functionality. The chosen imports appear consistent with the usage in this file. Good job on readability here!
214-227
: Ensure removed IO handlers actually exist.
“twitter_mentions”, “consciousness_thoughts”, “twitter_reply”, and “twitter_thought” are removed during shutdown but never registered in this file. This may cause confusion or errors if the orchestrator can’t find them. Recommend verifying or removing these lines as needed.packages/core/src/types/index.ts (1)
533-537
: HandlerRole enum is clearly defined.
It’s a neat way to categorize handlers. Making these string-based ensures consistency across the system.
classification: z.object({ | ||
contentType: z.string(), | ||
requiresProcessing: z.boolean(), | ||
context: z.object({ | ||
topic: z.string(), | ||
urgency: z.enum(["high", "medium", "low"]), | ||
additionalContext: z.string(), | ||
}), | ||
}), | ||
enrichment: z.object({ | ||
summary: z.string().max(1000), | ||
topics: z.array(z.string()).max(20), | ||
sentiment: z.enum(["positive", "negative", "neutral"]), | ||
entities: z.array(z.string()), | ||
intent: z.string().describe("The intent of the content"), | ||
}), | ||
suggestedOutputs: z.array( | ||
z.object({ | ||
name: z.string().describe("The name of the output or action"), | ||
data: z | ||
.any() | ||
.describe("The data that matches the output's schema"), | ||
confidence: z.number().describe("The confidence score (0-1)"), | ||
reasoning: z | ||
.string() | ||
.describe("The reasoning for the suggestion"), | ||
}) | ||
), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Modularize larger schemas.
This Zod schema is fairly large. Reusing a single definition for classification, enrichment, or suggestions across files can prevent duplication and reduce risk of drift.
this.logger.error("Processor.process", "Processing failed", { error }); | ||
return { | ||
originalContent: contentStr, | ||
timestamp, | ||
context: { | ||
timeContext: this.getTimeContext(timestamp), | ||
content, | ||
metadata: {}, | ||
enrichedContext: { | ||
timeContext: this.getTimeContext(new Date()), | ||
summary: contentStr.slice(0, 100), | ||
topics: [], | ||
relatedMemories: relatedMemories.map((m: SearchResult) => m.content), | ||
relatedMemories: [], | ||
sentiment: "neutral", | ||
entities: [], | ||
intent: "unknown", | ||
availableOutputs: Array.from(this.ioHandlers.keys()), | ||
}, | ||
suggestedOutputs: [], | ||
alreadyProcessed: false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add caution with partial data on errors.
Here you log the error and return partial context. Confirm sensitive info isn’t leaked to logs and sanitize or mask if needed.
orchestrator.registerIOHandler({ | ||
name: "universalApiCall", | ||
role: HandlerRole.ACTION, | ||
// The agent must fill out these fields to make a valid request | ||
schema: z | ||
.object({ | ||
method: z.enum(["GET", "POST", "PUT", "PATCH", "DELETE"]), | ||
url: z.string().url(), | ||
headers: z.record(z.string()).optional(), | ||
body: z.union([z.string(), z.record(z.any())]).optional(), | ||
}) | ||
.describe( | ||
"Use this to fetch data from an API. It should include the method, url, headers, and body." | ||
), | ||
handler: async (payload) => { | ||
const { method, url, headers, body } = payload as { | ||
method: string; | ||
url: string; | ||
headers?: Record<string, string>; | ||
body?: any; | ||
}; | ||
|
||
// Make the HTTP call | ||
const response = await fetch(url, { | ||
method, | ||
headers: { | ||
"Content-Type": "application/json", | ||
...headers, | ||
}, | ||
body: body ? JSON.stringify(body) : undefined, | ||
}); | ||
|
||
// Return JSON or text | ||
let responseData; | ||
try { | ||
responseData = await response.json(); | ||
} catch { | ||
responseData = await response.text(); | ||
} | ||
|
||
// Return the result so the agent can process it further | ||
return { | ||
status: response.status, | ||
statusText: response.statusText, | ||
data: responseData, | ||
}; | ||
}, | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Extend error handling in universalApiCall.
Although you catch JSON parse errors, you might also handle network issues, timeouts, or unexpected status codes to improve reliability. Consider adding retry logic or clearer logging for non-200 responses.
Summary by CodeRabbit
Release Notes
New Features
Improvements
Chores
Technical Updates
HandlerRole
andIOHandler
for more robust handler management