Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/mail/providers/query-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {
type PersistedClient,
type Persister,
} from '@tanstack/react-query-persist-client';
import { createTRPCClient, httpBatchLink, loggerLink } from '@trpc/client';
import { QueryCache, QueryClient, hashKey } from '@tanstack/react-query';
import { createTRPCContext } from '@trpc/tanstack-react-query';
import { createTRPCClient, httpBatchLink } from '@trpc/client';
import { useMemo, type PropsWithChildren } from 'react';
import type { AppRouter } from '@zero/server/trpc';
import { CACHE_BURST_KEY } from '@/lib/constants';
Expand Down Expand Up @@ -87,7 +87,7 @@ export const { TRPCProvider, useTRPC, useTRPCClient } = createTRPCContext<AppRou

export const trpcClient = createTRPCClient<AppRouter>({
links: [
loggerLink({ enabled: () => true }),
// loggerLink({ enabled: () => true }),
httpBatchLink({
transformer: superjson,
url: getUrl(),
Expand Down
3 changes: 3 additions & 0 deletions apps/mail/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ export default defineConfig({
// include: ['novel', '@tiptap/extension-placeholder'],
// },
// },
esbuild: {
pure: ['console.log'],
},
build: {
sourcemap: false,
},
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/lib/auth-providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const authProviders = (env: Record<string, string>): ProviderConfig[] =>
prompt: env.FORCE_GOOGLE_AUTH ? 'consent' : undefined,
accessType: 'offline',
scope: [
'https://mail.google.com/',
'https://www.googleapis.com/auth/gmail.modify',
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email',
Expand Down
40 changes: 21 additions & 19 deletions apps/server/src/lib/driver/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export class GoogleMailManager implements MailManager {
}
public getScope(): string {
return [
'https://mail.google.com/',
'https://www.googleapis.com/auth/gmail.modify',
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email',
Expand Down Expand Up @@ -255,25 +256,27 @@ export class GoogleMailManager implements MailManager {
});

const getArchiveCountEffect = Effect.tryPromise({
try: () => this.gmail.users.threads.list({
userId: 'me',
q: 'in:archive',
maxResults: 1,
}),
try: () =>
this.gmail.users.threads.list({
userId: 'me',
q: 'in:archive',
maxResults: 1,
}),
catch: (error) => ({ _tag: 'ArchiveFetchFailed' as const, error }),
});

const processLabelEffect = (label: any) =>
Effect.tryPromise({
try: () => this.gmail.users.labels.get({
userId: 'me',
id: label.id ?? undefined,
}),
try: () =>
this.gmail.users.labels.get({
userId: 'me',
id: label.id ?? undefined,
}),
catch: (error) => ({ _tag: 'LabelFetchFailed' as const, error, labelId: label.id }),
}).pipe(
Effect.map((res) => {
if ('_tag' in res) return null;

let labelName = (res.data.name ?? res.data.id ?? '').toLowerCase();
if (labelName === 'draft') {
labelName = 'drafts';
Expand All @@ -288,10 +291,10 @@ export class GoogleMailManager implements MailManager {

const mainEffect = Effect.gen(function* () {
// Fetch user labels and archive count concurrently
const [userLabelsResult, archiveResult] = yield* Effect.all([
getUserLabelsEffect,
getArchiveCountEffect,
], { concurrency: 'unbounded' });
const [userLabelsResult, archiveResult] = yield* Effect.all(
[getUserLabelsEffect, getArchiveCountEffect],
{ concurrency: 'unbounded' },
);

// Handle label list failure
if ('_tag' in userLabelsResult && userLabelsResult._tag === 'LabelListFailed') {
Expand All @@ -308,7 +311,9 @@ export class GoogleMailManager implements MailManager {
const labelResults = yield* Effect.all(labelEffects, { concurrency: 'unbounded' });

// Filter and collect results
const mapped: LabelCount[] = labelResults.filter((item): item is LabelCount => item !== null);
const mapped: LabelCount[] = labelResults.filter(
(item): item is LabelCount => item !== null,
);

// Add archive count if successful
if (!('_tag' in archiveResult)) {
Expand Down Expand Up @@ -545,7 +550,6 @@ export class GoogleMailManager implements MailManager {
addOrOptions: { addLabels: string[]; removeLabels: string[] } | string[],
maybeRemove?: string[],
) {

const options = Array.isArray(addOrOptions)
? { addLabels: addOrOptions as string[], removeLabels: maybeRemove ?? [] }
: addOrOptions;
Expand Down Expand Up @@ -1411,9 +1415,7 @@ export class GoogleMailManager implements MailManager {
}

const userLabels = await this.getUserLabels();
const existing = userLabels.find(
(l) => l.name?.toLowerCase() === labelName.toLowerCase(),
);
const existing = userLabels.find((l) => l.name?.toLowerCase() === labelName.toLowerCase());
if (existing && existing.id) {
this.labelIdCache[labelName] = existing.id;
return existing.id;
Expand Down
193 changes: 91 additions & 102 deletions apps/server/src/lib/sequential-thinking.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import type { env } from 'cloudflare:workers';
import { McpAgent } from 'agents/mcp';
import z from 'zod';

interface ThoughtData {
thought: string;
Expand Down Expand Up @@ -172,7 +173,7 @@ export class SequentialThinkingProcessor {
}
}

export class ThinkingMCP extends McpAgent<typeof env, Record<string, unknown>, { userId: string }> {
export class ThinkingMCP extends McpAgent<typeof env> {
thinkingServer = new SequentialThinkingProcessor();
server = new McpServer({
name: 'thinking-mcp',
Expand All @@ -181,106 +182,94 @@ export class ThinkingMCP extends McpAgent<typeof env, Record<string, unknown>, {
});

async init(): Promise<void> {
this.server.tool('Test', () => {
return {
content: [{ type: 'text' as const, text: 'Hello World' }],
};
});

console.log('Here!');

// this.server.registerTool(
// 'sequentialthinking',
// {
// description: `A detailed tool for dynamic and reflective problem-solving through thoughts.
// This tool helps analyze problems through a flexible thinking process that can adapt and evolve.
// Each thought can build on, question, or revise previous insights as understanding deepens.

// When to use this tool:
// - Breaking down complex problems into steps
// - Planning and design with room for revision
// - Analysis that might need course correction
// - Problems where the full scope might not be clear initially
// - Problems that require a multi-step solution
// - Tasks that need to maintain context over multiple steps
// - Situations where irrelevant information needs to be filtered out

// Key features:
// - You can adjust total_thoughts up or down as you progress
// - You can question or revise previous thoughts
// - You can add more thoughts even after reaching what seemed like the end
// - You can express uncertainty and explore alternative approaches
// - Not every thought needs to build linearly - you can branch or backtrack
// - Generates a solution hypothesis
// - Verifies the hypothesis based on the Chain of Thought steps
// - Repeats the process until satisfied
// - Provides a correct answer

// Parameters explained:
// - thought: Your current thinking step, which can include:
// * Regular analytical steps
// * Revisions of previous thoughts
// * Questions about previous decisions
// * Realizations about needing more analysis
// * Changes in approach
// * Hypothesis generation
// * Hypothesis verification
// - next_thought_needed: True if you need more thinking, even if at what seemed like the end
// - thought_number: Current number in sequence (can go beyond initial total if needed)
// - total_thoughts: Current estimate of thoughts needed (can be adjusted up/down)
// - is_revision: A boolean indicating if this thought revises previous thinking
// - revises_thought: If is_revision is true, which thought number is being reconsidered
// - branch_from_thought: If branching, which thought number is the branching point
// - branch_id: Identifier for the current branch (if any)
// - needs_more_thoughts: If reaching end but realizing more thoughts needed

// You should:
// 1. Start with an initial estimate of needed thoughts, but be ready to adjust
// 2. Feel free to question or revise previous thoughts
// 3. Don't hesitate to add more thoughts if needed, even at the "end"
// 4. Express uncertainty when present
// 5. Mark thoughts that revise previous thinking or branch into new paths
// 6. Ignore information that is irrelevant to the current step
// 7. Generate a solution hypothesis when appropriate
// 8. Verify the hypothesis based on the Chain of Thought steps
// 9. Repeat the process until satisfied with the solution
// 10. Provide a single, ideally correct answer as the final output
// 11. Only set next_thought_needed to false when truly done and a satisfactory answer is reached`,
// inputSchema: {
// thought: z.string().describe('Your current thinking step'),
// nextThoughtNeeded: z.boolean().describe('Whether another thought step is needed'),
// thoughtNumber: z.number().int().min(1).describe('Current thought number'),
// totalThoughts: z.number().int().min(1).describe('Estimated total thoughts needed'),
// isRevision: z.boolean().optional().describe('Whether this revises previous thinking'),
// revisesThought: z
// .number()
// .int()
// .min(1)
// .optional()
// .describe('Which thought is being reconsidered'),
// branchFromThought: z
// .number()
// .int()
// .min(1)
// .optional()
// .describe('Branching point thought number'),
// branchId: z.string().optional().describe('Branch identifier'),
// needsMoreThoughts: z.boolean().optional().describe('If more thoughts are needed'),
// },
// },
// (params) => {
// return this.thinkingServer.processThought({
// thought: params.thought,
// nextThoughtNeeded: params.nextThoughtNeeded,
// thoughtNumber: params.thoughtNumber,
// totalThoughts: params.totalThoughts,
// isRevision: params.isRevision,
// revisesThought: params.revisesThought,
// branchFromThought: params.branchFromThought,
// branchId: params.branchId,
// needsMoreThoughts: params.needsMoreThoughts,
// });
// },
// );
this.server.registerTool(
'sequentialthinking',
{
description: `A detailed tool for dynamic and reflective problem-solving through thoughts.
This tool helps analyze problems through a flexible thinking process that can adapt and evolve.
Each thought can build on, question, or revise previous insights as understanding deepens.
When to use this tool:
- Breaking down complex problems into steps
- Planning and design with room for revision
- Analysis that might need course correction
- Problems where the full scope might not be clear initially
- Problems that require a multi-step solution
- Tasks that need to maintain context over multiple steps
- Situations where irrelevant information needs to be filtered out
Key features:
- You can adjust total_thoughts up or down as you progress
- You can question or revise previous thoughts
- You can add more thoughts even after reaching what seemed like the end
- You can express uncertainty and explore alternative approaches
- Not every thought needs to build linearly - you can branch or backtrack
- Generates a solution hypothesis
- Verifies the hypothesis based on the Chain of Thought steps
- Repeats the process until satisfied
- Provides a correct answer
Parameters explained:
- thought: Your current thinking step, which can include:
* Regular analytical steps
* Revisions of previous thoughts
* Questions about previous decisions
* Realizations about needing more analysis
* Changes in approach
* Hypothesis generation
* Hypothesis verification
- next_thought_needed: True if you need more thinking, even if at what seemed like the end
- thought_number: Current number in sequence (can go beyond initial total if needed)
- total_thoughts: Current estimate of thoughts needed (can be adjusted up/down)
- is_revision: A boolean indicating if this thought revises previous thinking
- revises_thought: If is_revision is true, which thought number is being reconsidered
- branch_from_thought: If branching, which thought number is the branching point
- branch_id: Identifier for the current branch (if any)
- needs_more_thoughts: If reaching end but realizing more thoughts needed
You should:
1. Start with an initial estimate of needed thoughts, but be ready to adjust
2. Feel free to question or revise previous thoughts
3. Don't hesitate to add more thoughts if needed, even at the "end"
4. Express uncertainty when present
5. Mark thoughts that revise previous thinking or branch into new paths
6. Ignore information that is irrelevant to the current step
7. Generate a solution hypothesis when appropriate
8. Verify the hypothesis based on the Chain of Thought steps
9. Repeat the process until satisfied with the solution
10. Provide a single, ideally correct answer as the final output
11. Only set next_thought_needed to false when truly done and a satisfactory answer is reached`,
inputSchema: {
thought: z.string().describe('Your current thinking step'),
nextThoughtNeeded: z.boolean().describe('Whether another thought step is needed'),
thoughtNumber: z.number().int().min(1).describe('Current thought number'),
totalThoughts: z.number().int().min(1).describe('Estimated total thoughts needed'),
isRevision: z.boolean().optional().describe('Whether this revises previous thinking'),
revisesThought: z
.number()
.int()
.min(1)
.optional()
.describe('Which thought is being reconsidered'),
branchFromThought: z
.number()
.int()
.min(1)
.optional()
.describe('Branching point thought number'),
branchId: z.string().optional().describe('Branch identifier'),
needsMoreThoughts: z.boolean().optional().describe('If more thoughts are needed'),
},
},
(params) => {
return this.thinkingServer.processThought({
thought: params.thought,
nextThoughtNeeded: params.nextThoughtNeeded,
thoughtNumber: params.thoughtNumber,
totalThoughts: params.totalThoughts,
isRevision: params.isRevision,
revisesThought: params.revisesThought,
branchFromThought: params.branchFromThought,
branchId: params.branchId,
needsMoreThoughts: params.needsMoreThoughts,
});
},
);
}
}
14 changes: 13 additions & 1 deletion apps/server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { oAuthDiscoveryMetadata } from 'better-auth/plugins';
import { getZeroDB, verifyToken } from './lib/server-utils';
import { eq, and, desc, asc, inArray } from 'drizzle-orm';
import { EWorkflowType, runWorkflow } from './pipelines';
import { ThinkingMCP } from './lib/sequential-thinking';
import { ZeroAgent, ZeroDriver } from './routes/agent';
import { contextStorage } from 'hono/context-storage';
import { defaultUserSettings } from './lib/schemas';
Expand Down Expand Up @@ -610,6 +611,17 @@ export default class extends WorkerEntrypoint<typeof env> {
},
{ replaceRequest: false },
)
.mount(
'/mcp/thinking/sse',
async (request, env, ctx) => {
return ThinkingMCP.serveSSE('/mcp/thinking/sse', { binding: 'THINKING_MCP' }).fetch(
request,
env,
ctx,
);
},
{ replaceRequest: false },
)
.mount(
'/mcp',
async (request, env, ctx) => {
Expand Down Expand Up @@ -841,4 +853,4 @@ export default class extends WorkerEntrypoint<typeof env> {
}
}

export { ZeroAgent, ZeroMCP, ZeroDB, ZeroDriver };
export { ZeroAgent, ZeroMCP, ZeroDB, ZeroDriver, ThinkingMCP };
Loading