nyxcore-systems
7 min read

From Scratch to Smart: Infusing Collective Wisdom into Our AI Workflows

We just hit a major milestone: integrating our cross-project consolidation system directly into our AI workflow builder. Now, our 'Project Wisdom' steps leverage real, aggregated intelligence instead of starting from scratch, making our automated processes truly smarter.

AIWorkflowsPromptEngineeringFullstackPrismatRPCNextJS

Building intelligent systems often means moving from generic capabilities to domain-specific expertise. For us, a significant leap in that journey just happened. We've officially integrated our "consolidation system" – a repository of cross-project patterns and insights – directly into our workflow builder. This means our AI-powered workflows can now tap into a collective intelligence, making their "wisdom" steps genuinely insightful.

Let's dive into how we made our workflows smarter.

The Quest for Real Wisdom

Our Deep Build Pipeline, a multi-phase AI workflow, has a crucial "Project Wisdom" step. Its original mandate was to synthesize insights from the current project context. While effective, we knew it could be more. What if it could learn from all past projects? What if it could identify common pitfalls, successful strategies, and recurring patterns across our entire dataset?

This was the driving force behind this session: to bridge the gap between our accumulated "consolidation reports" (which aggregate these cross-project patterns) and the live execution of our AI workflows. The goal was simple: make the "Project Wisdom" step leverage this real, cross-project pattern data.

And I'm thrilled to report: it's fully implemented and type-checking clean. The Deep Build Pipeline's "Project Wisdom" step now dynamically pulls from linked consolidation reports via a new {{consolidations}} template variable.

Architecting Collective Intelligence

Integrating this wasn't just about passing a variable; it required changes across our data model, backend engine, API, and frontend experience.

1. Data Model: Linking Workflows to Wisdom

The first step was to establish the connection. A Workflow needed to know which consolidation reports it should draw wisdom from.

prisma
// prisma/schema.prisma
model Workflow {
  // ... other fields
  consolidationIds String[] @db.Uuid // New: Link to consolidation reports
  // ...
}

A quick npx prisma db push brought our database schema into sync. This simple array of UUIDs forms the backbone of our integration, allowing a workflow to reference multiple consolidation reports.

2. The Backend Brains: Injecting Context

The core logic resides within our workflow-engine.ts, which is responsible for executing the steps of any given workflow.

We extended the ChainContext – the central object holding all runtime data for a workflow – to include consolidationContent. This content isn't just raw data; it's pre-processed by generatePromptHints() from our consolidation-service.ts, transforming structured consolidation data into a prompt-friendly format.

typescript
// src/server/services/workflow-engine.ts (simplified)
import { generatePromptHints } from '~/server/services/consolidation-service';

interface ChainContext {
  // ... other context variables
  consolidationContent: string; // The pre-processed consolidation data
}

async function loadConsolidationContent(consolidationIds: string[]): Promise<string> {
  if (!consolidationIds || consolidationIds.length === 0) {
    return "[No consolidations linked to this workflow]";
  }
  const completedConsolidations = await prisma.consolidation.findMany({
    where: {
      id: { in: consolidationIds },
      status: "completed", // Only use completed consolidations
    },
  });
  return generatePromptHints(completedConsolidations);
}

export async function runWorkflow(...) {
  // ...
  const consolidationContent = await loadConsolidationContent(workflow.consolidationIds);
  const chainContext: ChainContext = {
    // ...
    consolidationContent,
  };

  // The new template variable resolver
  const resolvePrompt = (template: string, context: ChainContext) => {
    let resolved = template;
    resolved = resolved.replace(/\{\{consolidations\}\}/g, context.consolidationContent);
    // ... resolve other variables
    return resolved;
  };

  // ... rest of the workflow execution
}

Now, before a workflow even begins, we load and process the linked consolidation data, making it available as {{consolidations}} for any step's prompt.

3. API & Frontend: User Control and Visibility

On the API side (src/server/trpc/routers/workflows.ts), we updated our create and duplicate mutations to accept and persist consolidationIds. This ensures that when a user creates or clones a workflow, their chosen consolidations are carried over.

The frontend experience was crucial for usability:

  • New Workflow Page: We added a consolidation picker (src/app/(dashboard)/dashboard/workflows/new/page.tsx). This queries completed consolidations and presents them as a checkbox list, showing their name, project count, and pattern count. Users can now easily select which "wisdom" reports to link.
  • Workflow Settings Panel: On the individual workflow page (src/app/(dashboard)/dashboard/workflows/[id]/page.tsx), the settings panel now displays the linked consolidations as clickable links, navigating directly to the consolidation report for easy review.

4. Crafting the "Wisdom" Prompt

With the data flowing, the final piece was to update our deepWisdom step template prompt (src/lib/constants.ts). This is where the magic truly happens, guiding the LLM to leverage the injected wisdom.

typescript
// src/lib/constants.ts (simplified prompt snippet)
export const deepWisdomStepTemplate = `
You are an expert architect tasked with distilling core insights.
...
Here are key patterns and lessons from past projects (Consolidation Intelligence):
---
{{consolidations}}
---

Considering the current project context and the Consolidation Intelligence provided,
identify overarching patterns, potential pitfalls, and strategic recommendations.
Focus on how lessons from past projects can be *applied* to this one.

Output Format:
### Project Wisdom
[Core insights from current project context]

### Lessons Applied
[How patterns/lessons from {{consolidations}} specifically apply or inform this project]

### Strategic Recommendations
[Actionable recommendations based on combined wisdom]
`;

Notice the {{consolidations}} variable directly embedded in the prompt. This tells the AI, "Hey, here's some valuable cross-project data; use it!" We also added a dedicated "Lessons Applied" output section to explicitly capture how the AI integrated this collective intelligence.

Lessons from the Trenches

While this specific session went smoothly (a rare and beautiful thing!), prior sessions have taught us invaluable lessons that are worth sharing:

  • Prisma JSON Fields: When resetting or clearing JSON fields in Prisma, always use Prisma.JsonNull instead of null. null might remove the field, while Prisma.JsonNull sets it to JSON null, preserving the field's presence with a null value. Subtle, but critical for schema consistency.
  • TypeScript Iteration: If you're using Maps or Sets and iterating over them (e.g., for...of map.entries()), and you encounter TypeScript errors related to --downlevelIteration, consider using Array.from(map.entries()). This often resolves compatibility issues when targeting older JS versions.
  • Schema Evolution Gotchas: When adding new required columns to an existing database table, make sure to either define a @default("") value in your Prisma schema or make the new column optional (?). Otherwise, prisma migrate (or db push in development) will fail because existing rows won't have a value for the new required column.

What's Next? Testing and Refinement

No feature is truly done until it's been thoroughly tested in the wild. Our immediate next steps involve:

  1. End-to-End Test: Create a new workflow using the Deep Build Pipeline template, link some completed consolidations, and run it.
  2. Consolidation Picker Verification: Confirm the picker only shows completed consolidations.
  3. Prompt Content Check: Verify that the {{consolidations}} content is correctly injected into the prompt during execution.
  4. Fallback Test: Run a workflow without linking any consolidations to ensure the [No consolidations linked to this workflow] fallback appears correctly.
  5. UI Navigation: Test that the clickable consolidation links in the execution page settings panel navigate as expected.
  6. Display Enhancement: Currently, the settings panel shows generic "Consolidation" badges. We'll explore adding a trpc.consolidation.getMany endpoint to resolve and display the actual names of linked consolidations for a better user experience.

This milestone significantly elevates the intelligence of our workflow system. By actively incorporating lessons from past projects, our AI workflows are no longer just executing steps; they're learning, adapting, and applying collective wisdom. It's exciting to see our systems move closer to a truly intelligent, self-improving state.

Stay tuned for more updates as we continue to push the boundaries of AI-powered development!

json
{
  "thingsDone": [
    "Integrated consolidation system into workflow builder's 'Project Wisdom' step.",
    "Enabled workflows to use real cross-project pattern data.",
    "Implemented `consolidationIds` field in `Workflow` model.",
    "Added `{{consolidations}}` template variable resolving pre-processed consolidation content.",
    "Updated workflow engine to load and process consolidation data.",
    "Modified tRPC mutations for `create` and `duplicate` workflows to handle `consolidationIds`.",
    "Rewrote `deepWisdom` prompt template to leverage `{{consolidations}}` and include 'Lessons Applied' output.",
    "Developed a consolidation picker on the new workflow page.",
    "Implemented display of linked consolidations in the workflow settings panel with clickable links."
  ],
  "pains": [
    "Remembered to use `Prisma.JsonNull` for clearing JSON fields, not `null`.",
    "Recalled using `Array.from(map.entries())` to avoid `--downlevelIteration` TS errors with Maps.",
    "Considered making new required columns optional or adding `@default('')` to avoid schema push failures on existing rows."
  ],
  "successes": [
    "Fully implemented and type-checking clean integration.",
    "Deep Build Pipeline's 'Project Wisdom' step now pulls from linked consolidation reports.",
    "Seamless user experience for linking and viewing consolidations.",
    "No new issues encountered during this specific development session."
  ],
  "techStack": [
    "Prisma",
    "PostgreSQL",
    "TypeScript",
    "Next.js",
    "tRPC",
    "dnd-kit (from prior session context)",
    "SSE (from prior session context)",
    "MarkdownRenderer (from prior session context)"
  ]
}