Skip to content
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

Merged
merged 4 commits into from
Jan 24, 2025
Merged

fix: Updates #37

merged 4 commits into from
Jan 24, 2025

Conversation

ponderingdemocritus
Copy link
Contributor

@ponderingdemocritus ponderingdemocritus commented Jan 24, 2025

  • Processor refactor
  • Allow user live runtime inputs into Erchestrator

Summary by CodeRabbit

Release Notes

  • New Features

    • Added a comprehensive Twitter bot example with advanced interaction capabilities
    • Introduced a new API example demonstrating bot functionality
    • Enhanced console-based user interaction for bot examples
  • Improvements

    • Improved type safety for handler roles
    • Refined content processing and enrichment mechanisms
    • Streamlined import and module organization
  • Chores

    • Updated project scripts to support new example execution
    • Reorganized type imports and module structures
  • Technical Updates

    • Added new methods to Orchestrator for more flexible input/output handling
    • Introduced HandlerRole and IOHandler for more robust handler management

Copy link
Contributor

coderabbitai bot commented Jan 24, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

This 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 ./types directory within the core module, introducing new types like HandlerRole and IOHandler, and updating example files to demonstrate the new architecture. A new example API bot has been added, showcasing the enhanced capabilities of the framework with improved type safety and modular handler management.

Changes

File Change Summary
examples/example-api.ts New comprehensive Twitter bot example with API interactions
examples/example-twitter.ts Updated with type-safe handler roles and console input
package.json Added new api script for running the example API bot
packages/core/src/core/orchestrator.ts Added new methods for dispatching actions and inputs, enhanced autonomous flow handling
packages/core/src/core/processor.ts Restructured content processing with more detailed enrichment
packages/core/src/core/types/index.ts Introduced HandlerRole enum and IOHandler interface
Multiple packages/core/src/core/* files Updated import paths to use local ./types

Sequence Diagram

sequenceDiagram
    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
Loading

Possibly related PRs

Poem

🐰 A Rabbit's Code Refrain 🌟

Types dancing in their new home,
Handlers leaping with type-safe chrome,
Orchestrator's flow so bright,
Imports aligned, code burning light!

Hop, hop, hooray! 🎉


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 874b51e and 274db30.

📒 Files selected for processing (23)
  • examples/example-api.ts (1 hunks)
  • examples/example-goal.ts (1 hunks)
  • examples/example-twitter.ts (7 hunks)
  • packages/core/src/core/chain-of-thought.ts (2 hunks)
  • packages/core/src/core/chains/evm.ts (1 hunks)
  • packages/core/src/core/chains/solana.ts (1 hunks)
  • packages/core/src/core/chains/starknet.ts (1 hunks)
  • packages/core/src/core/character.ts (1 hunks)
  • packages/core/src/core/consciousness.ts (1 hunks)
  • packages/core/src/core/goal-manager.ts (1 hunks)
  • packages/core/src/core/index.ts (2 hunks)
  • packages/core/src/core/io/twitter.ts (1 hunks)
  • packages/core/src/core/llm-client.ts (1 hunks)
  • packages/core/src/core/logger.ts (1 hunks)
  • packages/core/src/core/orchestrator.ts (4 hunks)
  • packages/core/src/core/processor.ts (6 hunks)
  • packages/core/src/core/room-manager.ts (1 hunks)
  • packages/core/src/core/room.ts (1 hunks)
  • packages/core/src/core/step-manager.ts (1 hunks)
  • packages/core/src/core/types/index.ts (2 hunks)
  • packages/core/src/core/utils.ts (1 hunks)
  • packages/core/src/core/vector-db.ts (1 hunks)
  • packages/core/src/index.ts (0 hunks)
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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 the generateContentId 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. If main() 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 single IOHandler 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., “run pnpm 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

📥 Commits

Reviewing files that changed from the base of the PR and between 44d0b9e and 874b51e.

📒 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 importing HandlerRole and IOHandler from a central definition. This ensures consistent type usage across the codebase.


48-48: Confirm concurrency safety.
If multiple threads call hasProcessedContent at the same time for the same contentId, ensure no data race or duplication occurs.


Line range hint 50-62: Provide consistent defaults in repeated fields.
When hasProcessed is true, returning placeholders for fields like sentiment, entities, and intent 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.
Calling markContentAsProcessed here updates a global marker. Double-check that simultaneously processed content is handled without collisions.


163-179: Good structured return.
Returning alreadyProcessed 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.
Importing IOHandler 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 new dispatchToAction 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: Use HandlerRole consistently.
Replacing string literals with HandlerRole 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 for twitter_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 capturing inReplyTo 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.

Comment on lines +127 to +154
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"),
})
),
Copy link
Contributor

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.

Comment on lines +182 to +197
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

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.

Comment on lines +82 to +129
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,
};
},
});
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant