Merge pull request #585 from rowboatlabs/dev

Dev changes
This commit is contained in:
Ramnique Singh 2026-05-28 23:01:55 +05:30 committed by GitHub
commit 56246b84e6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
96 changed files with 10155 additions and 3042 deletions

View file

@ -70,7 +70,7 @@ The `once` trigger from the prior model has been **dropped** — it didn't fit t
Two paths, both producing identical on-disk YAML:
1. **Hand-written** — type the `live:` block directly into the note's frontmatter. The scheduler picks it up on its next 15-second tick.
2. **Sidebar chat** — mention a note (or have it attached) and ask Copilot for something dynamic. Copilot is tuned to recognize a wide range of phrasings beyond "live" or "track" (see "Prompts Catalog → Copilot trigger paragraph"); it loads the `live-note` skill, edits the frontmatter via `workspace-edit`, then **runs the agent once by default** so the user immediately sees content. The auto-run is skipped only when the user explicitly says not to run yet.
2. **Sidebar chat** — mention a note (or have it attached) and ask Copilot for something dynamic. Copilot is tuned to recognize a wide range of phrasings beyond "live" or "track" (see "Prompts Catalog → Copilot trigger paragraph"); it loads the `live-note` skill, edits the frontmatter via `file-editText`, then **runs the agent once by default** so the user immediately sees content. The auto-run is skipped only when the user explicitly says not to run yet.
When the note is **already live** and the user asks to track something new, Copilot extends the existing `live.objective` in natural-language prose. It does not create a second `live:` block.
@ -92,8 +92,8 @@ When a trigger fires, the live-note agent receives a short message:
- For event runs only: the matching `eventMatchCriteria` text and the event payload, with a Pass-2 decision directive ("only edit if the event genuinely warrants it").
The agent's system prompt tells it to:
1. Call `workspace-readFile` to read the current note (the body may be long; no body snapshot is passed in the message — fetch fresh).
2. Make small, **patch-style** edits with `workspace-edit` — change one region, re-read, change the next region — rather than one-shot rewrites.
1. Call `file-readText` to read the current note (the body may be long; no body snapshot is passed in the message — fetch fresh).
2. Make small, **patch-style** edits with `file-editText` — change one region, re-read, change the next region — rather than one-shot rewrites.
3. Follow default body structure unless the objective overrides: H1 stays the title; a 1-3 sentence rolling summary at the top; H2 sub-topic sections below, freshest first.
4. Never modify YAML frontmatter — that's owned by the user and the runtime.
5. End with a 1-2 sentence summary stored as `lastRunSummary`.
@ -115,7 +115,7 @@ Backend (main process)
├─ Event processor (5 s) ──┼──► runLiveNoteAgent() ──► live-note-agent
└─ Builtin tool │ │
run-live-note-agent ────┘ ▼
workspace-readFile / -edit
file-readText / -edit
body region(s) rewritten on disk
@ -249,7 +249,7 @@ The contract (defined in the run-agent system prompt — `packages/core/src/know
- Then content organized by sub-topic under H2 headings, freshest/most-important first.
- Tightness over decoration.
- **Override** — if the objective specifies a different layout (e.g. "show the top 5 stories at the top, with a one-paragraph summary above them"), follow that exactly.
- **Patch-style updates** — make small, incremental `workspace-edit` calls (read → edit one region → re-read → next), not one-shot whole-body rewrites. This preserves user-added content the agent didn't account for and keeps diffs reviewable.
- **Patch-style updates** — make small, incremental `file-editText` calls (read → edit one region → re-read → next), not one-shot whole-body rewrites. This preserves user-added content the agent didn't account for and keeps diffs reviewable.
- **Boundaries**: never modify the frontmatter; the agent is the sole writer of the body below the H1.
---
@ -316,7 +316,7 @@ Every LLM-facing prompt in the feature, with file pointers. After any edit: `cd
- **Purpose**: the user message seeded into each agent run.
- **File**: `packages/core/src/knowledge/live-note/runner.ts` (`buildMessage`).
- **Inputs**: `filePath` (presented as `knowledge/${filePath}` in the message), `live.objective`, `live.triggers?.eventMatchCriteria` (only on event runs), `trigger`, optional `context`, plus `localNow` / `tz`.
- **Behavior**: tells the agent to call `workspace-readFile` itself (no body snapshot included, since the body can be long and may have been edited by a concurrent run) and to make patch-style edits.
- **Behavior**: tells the agent to call `file-readText` itself (no body snapshot included, since the body can be long and may have been edited by a concurrent run) and to make patch-style edits.
Three branches by `trigger`:
- **`manual`** — base message. If `context` is passed, it's appended as a `**Context:**` section. The `run-live-note-agent` tool uses this path for both plain refreshes and context-biased backfills.

View file

@ -2,7 +2,8 @@ import { createServer, Server } from 'http';
import { URL } from 'url';
const OAUTH_CALLBACK_PATH = '/oauth/callback';
const DEFAULT_PORT = 8080;
export const DEFAULT_PORT = 8080;
export const PORT_RANGE_SIZE = 10;
/** Escape HTML special characters to prevent XSS */
function escapeHtml(str: string): string {
@ -19,13 +20,8 @@ export interface AuthServerResult {
port: number;
}
/**
* Create a local HTTP server to handle OAuth callback
* Listens on http://localhost:8080/oauth/callback
* Passes the full callback URL (including iss, scope, etc.) so openid-client validation succeeds.
*/
export function createAuthServer(
port: number = DEFAULT_PORT,
function tryBindPort(
port: number,
onCallback: (callbackUrl: URL) => void | Promise<void>
): Promise<AuthServerResult> {
return new Promise((resolve, reject) => {
@ -37,7 +33,7 @@ export function createAuthServer(
}
const url = new URL(req.url, `http://localhost:${port}`);
if (url.pathname === OAUTH_CALLBACK_PATH) {
const error = url.searchParams.get('error');
@ -96,8 +92,10 @@ export function createAuthServer(
});
server.on('error', (err: NodeJS.ErrnoException) => {
if (err.code === 'EADDRINUSE') {
reject(new Error(`Port ${port} is already in use`));
server.close();
if (err.code === 'EADDRINUSE' || err.code === 'EACCES') {
// Signal caller to try next port
reject(Object.assign(new Error(err.code), { code: err.code }));
} else {
reject(err);
}
@ -105,3 +103,51 @@ export function createAuthServer(
});
}
/**
* Create a local HTTP server to handle OAuth callback.
*
* Defaults to fixed-port behaviour: only `port` is tried, and a clear error is
* thrown if it cannot be bound. This is the right behaviour for any provider
* whose redirect URI is pre-registered (Google BYOK, Composio, etc.) those
* callers must keep using the exact port they've handed to the provider.
*
* Opt into `{ fallback: true }` only when the caller is prepared to use the
* port returned in `AuthServerResult` (i.e. the redirect URI is built from the
* actual bound port, not hard-coded). With fallback enabled, scans `port`
* through `port + PORT_RANGE_SIZE - 1` and binds the first available, handling
* both EADDRINUSE and EACCES (the latter is common on Windows when
* Hyper-V/WSL2 reserve the port).
*/
export async function createAuthServer(
port: number = DEFAULT_PORT,
onCallback: (callbackUrl: URL) => void | Promise<void>,
opts: { fallback?: boolean } = {},
): Promise<AuthServerResult> {
const fallback = opts.fallback === true;
const limit = fallback ? port + PORT_RANGE_SIZE - 1 : port;
for (let p = port; p <= limit; p++) {
try {
return await tryBindPort(p, onCallback);
} catch (err) {
const code = (err as NodeJS.ErrnoException).code;
if (fallback && (code === 'EADDRINUSE' || code === 'EACCES') && p < limit) {
console.warn(`[OAuth] Port ${p} unavailable (${code}), trying ${p + 1}`);
continue;
}
if (!fallback) {
const reason = code === 'EACCES' || code === 'EADDRINUSE'
? `Port ${port} is unavailable (${code}). This port must be free for sign-in to work — close any app using it and try again.`
: (err instanceof Error ? err.message : String(err));
throw new Error(reason);
}
throw new Error(
`No available port found in range ${port}${limit}. Free a port in that range and try again.`
);
}
}
// Unreachable — loop always returns or throws — but satisfies TypeScript
throw new Error(`No available port found in range ${port}${limit}.`);
}

View file

@ -31,6 +31,9 @@ import { listGatewayModels } from '@x/core/dist/models/gateway.js';
import type { IModelConfigRepo } from '@x/core/dist/models/repo.js';
import type { IOAuthRepo } from '@x/core/dist/auth/repo.js';
import { IGranolaConfigRepo } from '@x/core/dist/knowledge/granola/repo.js';
import { ICodeModeConfigRepo } from '@x/core/dist/code-mode/repo.js';
import { checkCodeModeAgentStatus } from '@x/core/dist/code-mode/status.js';
import { invalidateCopilotInstructionsCache } from '@x/core/dist/application/assistant/instructions.js';
import { triggerSync as triggerGranolaSync } from '@x/core/dist/knowledge/granola/sync.js';
import { ISlackConfigRepo } from '@x/core/dist/slack/repo.js';
import { isOnboardingComplete, markOnboardingComplete } from '@x/core/dist/config/note_creation_config.js';
@ -48,7 +51,7 @@ import { summarizeMeeting } from '@x/core/dist/knowledge/summarize_meeting.js';
import { getAccessToken } from '@x/core/dist/auth/tokens.js';
import { getRowboatConfig } from '@x/core/dist/config/rowboat.js';
import { runLiveNoteAgent } from '@x/core/dist/knowledge/live-note/runner.js';
import { listImportantThreads, listEverythingElseThreads, saveMessageBodyHeight, triggerSync as triggerGmailSync } from '@x/core/dist/knowledge/sync_gmail.js';
import { listImportantThreads, listEverythingElseThreads, saveMessageBodyHeight, triggerSync as triggerGmailSync, sendThreadReply, archiveThread, trashThread, markThreadRead, getAccountEmail, getConnectionStatus as getGmailConnectionStatus } from '@x/core/dist/knowledge/sync_gmail.js';
import { liveNoteBus } from '@x/core/dist/knowledge/live-note/bus.js';
import { getInstallationId } from '@x/core/dist/analytics/installation.js';
import { API_URL } from '@x/core/dist/config/env.js';
@ -494,6 +497,24 @@ export function setupIpcHandlers() {
triggerGmailSync();
return {};
},
'gmail:sendReply': async (_event, args) => {
return sendThreadReply(args);
},
'gmail:getConnectionStatus': async () => {
return getGmailConnectionStatus();
},
'gmail:getAccountEmail': async () => {
return { email: await getAccountEmail() };
},
'gmail:archiveThread': async (_event, args) => {
return archiveThread(args.threadId);
},
'gmail:trashThread': async (_event, args) => {
return trashThread(args.threadId);
},
'gmail:markThreadRead': async (_event, args) => {
return markThreadRead(args.threadId);
},
'gmail:saveMessageHeight': async (_event, args) => {
saveMessageBodyHeight(args.threadId, args.messageId, args.height);
return {};
@ -508,7 +529,7 @@ export function setupIpcHandlers() {
return runsCore.createRun(args);
},
'runs:createMessage': async (_event, args) => {
return { messageId: await runsCore.createMessage(args.runId, args.message, args.voiceInput, args.voiceOutput, args.searchEnabled, args.middlePaneContext) };
return { messageId: await runsCore.createMessage(args.runId, args.message, args.voiceInput, args.voiceOutput, args.searchEnabled, args.middlePaneContext, args.codeMode) };
},
'runs:authorizePermission': async (_event, args) => {
await runsCore.authorizePermission(args.runId, args.authorization);
@ -612,6 +633,20 @@ export function setupIpcHandlers() {
const config = await repo.getConfig();
return { enabled: config.enabled };
},
'codeMode:getConfig': async () => {
const repo = container.resolve<ICodeModeConfigRepo>('codeModeConfigRepo');
const config = await repo.getConfig();
return { enabled: config.enabled };
},
'codeMode:setConfig': async (_event, args) => {
const repo = container.resolve<ICodeModeConfigRepo>('codeModeConfigRepo');
await repo.setConfig({ enabled: args.enabled });
invalidateCopilotInstructionsCache();
return { success: true };
},
'codeMode:checkAgentStatus': async () => {
return await checkCodeModeAgentStatus();
},
'granola:setConfig': async (_event, args) => {
const repo = container.resolve<IGranolaConfigRepo>('granolaConfigRepo');
await repo.setConfig({ enabled: args.enabled });

View file

@ -51,6 +51,7 @@ import {
extractDeepLinkFromArgv,
setMainWindowForDeepLinks,
} from "./deeplink.js";
import { disconnectGoogleIfScopesStale } from "./oauth-handler.js";
const execAsync = promisify(exec);
@ -351,6 +352,11 @@ app.whenReady().then(async () => {
registerConsumer(backgroundTaskEventConsumer);
initEventProcessor();
// If the stored Google grant predates a scope change (only old scopes),
// disconnect it now so the user re-connects with the current scopes before
// any Google sync runs against the stale grant.
await disconnectGoogleIfScopesStale();
// start gmail sync
initGmailSync();

View file

@ -1,6 +1,7 @@
import { shell } from 'electron';
import type { Server } from 'http';
import { createAuthServer } from './auth-server.js';
import { DEFAULT_CALLBACK_PORT } from '@x/core/dist/auth/client-repo.js';
import * as oauthClient from '@x/core/dist/auth/oauth-client.js';
import type { Configuration } from '@x/core/dist/auth/oauth-client.js';
import { getProviderConfig, getAvailableProviders } from '@x/core/dist/auth/providers.js';
@ -17,7 +18,9 @@ import { isSignedIn } from '@x/core/dist/account/account.js';
import { getWebappUrl } from '@x/core/dist/config/remote-config.js';
import { claimTokensViaBackend } from '@x/core/dist/auth/google-backend-oauth.js';
const REDIRECT_URI = 'http://localhost:8080/oauth/callback';
function buildRedirectUri(port: number): string {
return `http://localhost:${port}/oauth/callback`;
}
/** Top-level openid-client messages that often wrap a more specific cause. */
const OPAQUE_OAUTH_TOP_MESSAGES = new Set(['invalid response encountered']);
@ -114,9 +117,15 @@ function getClientRegistrationRepo(): IClientRegistrationRepo {
}
/**
* Get or create OAuth configuration for a provider
* Get or create OAuth configuration for a provider.
* `redirectUri` is required for DCR providers it is the actual callback URI
* (including port) that was just bound, so the registration and auth URL stay in sync.
*/
async function getProviderConfiguration(provider: string, credentialsOverride?: { clientId: string; clientSecret: string }): Promise<Configuration> {
async function getProviderConfiguration(
provider: string,
redirectUri: string = buildRedirectUri(DEFAULT_CALLBACK_PORT),
credentialsOverride?: { clientId: string; clientSecret: string },
): Promise<Configuration> {
const config = await getProviderConfig(provider);
const resolveClientCredentials = async (): Promise<{ clientId: string; clientSecret?: string }> => {
if (config.client.mode === 'static' && config.client.clientId) {
@ -148,7 +157,7 @@ async function getProviderConfiguration(provider: string, credentialsOverride?:
console.log(`[OAuth] ${provider}: Discovery from issuer with DCR`);
const clientRepo = getClientRegistrationRepo();
const existingRegistration = await clientRepo.getClientRegistration(provider);
if (existingRegistration) {
console.log(`[OAuth] ${provider}: Using existing DCR registration`);
return await oauthClient.discoverConfiguration(
@ -157,18 +166,21 @@ async function getProviderConfiguration(provider: string, credentialsOverride?:
);
}
// Register new client
// Register new client with the actual redirect URI (port already bound)
const scopes = config.scopes || [];
const { config: oauthConfig, registration } = await oauthClient.registerClient(
config.discovery.issuer,
[REDIRECT_URI],
[redirectUri],
scopes
);
// Save registration for future use
await clientRepo.saveClientRegistration(provider, registration);
console.log(`[OAuth] ${provider}: DCR registration saved`);
// Parse port from redirectUri (e.g. "http://localhost:8081/...") and save
const boundPort = new URL(redirectUri).port
? parseInt(new URL(redirectUri).port, 10)
: DEFAULT_CALLBACK_PORT;
await clientRepo.saveClientRegistration(provider, registration, boundPort);
console.log(`[OAuth] ${provider}: DCR registration saved (port ${boundPort})`);
return oauthConfig;
}
} else {
@ -176,7 +188,7 @@ async function getProviderConfiguration(provider: string, credentialsOverride?:
if (config.client.mode !== 'static') {
throw new Error('DCR requires discovery mode "issuer", not "static"');
}
console.log(`[OAuth] ${provider}: Using static endpoints (no discovery)`);
const { clientId, clientSecret } = await resolveClientCredentials();
return oauthClient.createStaticConfiguration(
@ -189,6 +201,37 @@ async function getProviderConfiguration(provider: string, credentialsOverride?:
}
}
/**
* Determine which port to start the OAuth callback server on for a DCR provider.
*
* If the provider has an existing registration, probes the port it was registered
* on. If that port is still available, returns it so the existing client_id keeps
* working. If it is blocked, clears the stale registration (forcing re-registration
* on the next available port) and returns DEFAULT_CALLBACK_PORT as the scan base.
*
* Exported for unit testing.
*/
export async function resolveStartPort(
provider: string,
clientRepo: IClientRegistrationRepo,
): Promise<number> {
const existingReg = await clientRepo.getClientRegistration(provider);
if (!existingReg) return DEFAULT_CALLBACK_PORT;
const registeredPort = await clientRepo.getRegisteredPort(provider);
try {
// Probe — fixed-port (no fallback) so we know whether the exact registered port is free
const probe = await createAuthServer(registeredPort, () => { /* probe */ });
probe.server.close();
console.log(`[OAuth] ${provider}: registered port ${registeredPort} still available`);
return registeredPort;
} catch {
console.log(`[OAuth] ${provider}: registered port ${registeredPort} blocked, clearing DCR registration`);
await clientRepo.clearClientRegistration(provider);
return DEFAULT_CALLBACK_PORT;
}
}
/**
* Initiate OAuth flow for a provider
*/
@ -225,154 +268,188 @@ export async function connectProvider(provider: string, credentials?: { clientId
}
}
// Get or create OAuth configuration
const config = await getProviderConfiguration(provider, credentials);
// For static-client providers (Google BYOK) the redirect URI is pre-registered
// at the OAuth provider console on a fixed port — we must not scan.
// For DCR providers, resolveStartPort handles the re-registration trap.
const isStaticClient = providerConfig.client.mode === 'static';
const startPort = isStaticClient
? DEFAULT_CALLBACK_PORT
: await resolveStartPort(provider, getClientRegistrationRepo());
// Generate PKCE codes
const { verifier: codeVerifier, challenge: codeChallenge } = await oauthClient.generatePKCE();
const state = oauthClient.generateState();
// Get scopes from config
const scopes = providerConfig.scopes || [];
// Store flow state
activeFlows.set(state, { codeVerifier, provider, config });
// Build authorization URL
const authUrl = oauthClient.buildAuthorizationUrl(config, {
redirect_uri: REDIRECT_URI,
scope: scopes.join(' '),
code_challenge: codeChallenge,
state,
});
// Create callback server
// --- Callback server ---
// Declare `state` before the closure so the callback can close over its binding.
// The variable is assigned below, before shell.openExternal, so it is always
// set by the time any browser request arrives.
let state = '';
let callbackHandled = false;
const { server } = await createAuthServer(8080, async (callbackUrl) => {
// Guard against duplicate callbacks (browser may send multiple requests)
if (callbackHandled) return;
callbackHandled = true;
const receivedState = callbackUrl.searchParams.get('state');
if (receivedState == null || receivedState === '') {
throw new Error(
'OAuth callback missing state parameter. Complete sign-in in the browser or check the redirect URI.'
);
}
if (receivedState !== state) {
throw new Error('Invalid state parameter - possible CSRF attack');
}
const flow = activeFlows.get(state);
if (!flow || flow.provider !== provider) {
throw new Error('Invalid OAuth flow state');
}
try {
// Use full callback URL (includes iss, scope, etc.) so openid-client validation succeeds
console.log(`[OAuth] Exchanging authorization code for tokens (${provider})...`);
const tokens = await oauthClient.exchangeCodeForTokens(
flow.config,
callbackUrl,
flow.codeVerifier,
state
);
// Save tokens and credentials. For Google, BYOK is the only path
// that reaches this token exchange (rowboat path returns above
// before any local server runs); stamp mode: 'byok' so a future
// refresh / reconnect can't get confused with a rowboat entry.
console.log(`[OAuth] Token exchange successful for ${provider}`);
await oauthRepo.upsert(provider, {
tokens,
...(credentials ? { clientId: credentials.clientId, clientSecret: credentials.clientSecret } : {}),
...(provider === 'google' ? { mode: 'byok' as const } : {}),
error: null,
});
// Trigger immediate sync for relevant providers
if (provider === 'google') {
triggerGmailSync();
triggerCalendarSync();
} else if (provider === 'fireflies-ai') {
triggerFirefliesSync();
const { server, port: boundPort } = await createAuthServer(
startPort,
async (callbackUrl) => {
// Guard against duplicate callbacks (browser may send multiple requests)
if (callbackHandled) return;
callbackHandled = true;
const receivedState = callbackUrl.searchParams.get('state');
if (receivedState == null || receivedState === '') {
throw new Error(
'OAuth callback missing state parameter. Complete sign-in in the browser or check the redirect URI.'
);
}
if (receivedState !== state) {
throw new Error('Invalid state parameter - possible CSRF attack');
}
// For Rowboat sign-in, ensure user + Stripe customer exist before
// notifying the renderer. Without this, parallel API calls from
// multiple renderer hooks race to create the user, causing duplicates.
let signedInUserId: string | undefined;
if (provider === 'rowboat') {
try {
const billing = await getBillingInfo();
if (billing.userId) {
signedInUserId = billing.userId;
analyticsIdentify(billing.userId, {
...(billing.userEmail ? { email: billing.userEmail } : {}),
plan: billing.subscriptionPlan,
status: billing.subscriptionStatus,
});
analyticsCapture('user_signed_in', {
plan: billing.subscriptionPlan,
status: billing.subscriptionStatus,
});
const flow = activeFlows.get(state);
if (!flow || flow.provider !== provider) {
throw new Error('Invalid OAuth flow state');
}
try {
// Use full callback URL (includes iss, scope, etc.) so openid-client validation succeeds
console.log(`[OAuth] Exchanging authorization code for tokens (${provider})...`);
const tokens = await oauthClient.exchangeCodeForTokens(
flow.config,
callbackUrl,
flow.codeVerifier,
state
);
// Save tokens and credentials. For Google, BYOK is the only path
// that reaches this token exchange (rowboat path returns above
// before any local server runs); stamp mode: 'byok' so a future
// refresh / reconnect can't get confused with a rowboat entry.
console.log(`[OAuth] Token exchange successful for ${provider}`);
await oauthRepo.upsert(provider, {
tokens,
...(credentials ? { clientId: credentials.clientId, clientSecret: credentials.clientSecret } : {}),
...(provider === 'google' ? { mode: 'byok' as const } : {}),
error: null,
});
// Trigger immediate sync for relevant providers
if (provider === 'google') {
triggerGmailSync();
triggerCalendarSync();
} else if (provider === 'fireflies-ai') {
triggerFirefliesSync();
}
// For Rowboat sign-in, ensure user + Stripe customer exist before
// notifying the renderer. Without this, parallel API calls from
// multiple renderer hooks race to create the user, causing duplicates.
let signedInUserId: string | undefined;
if (provider === 'rowboat') {
try {
const billing = await getBillingInfo();
if (billing.userId) {
signedInUserId = billing.userId;
analyticsIdentify(billing.userId, {
...(billing.userEmail ? { email: billing.userEmail } : {}),
plan: billing.subscriptionPlan,
status: billing.subscriptionStatus,
});
analyticsCapture('user_signed_in', {
plan: billing.subscriptionPlan,
status: billing.subscriptionStatus,
});
}
} catch (meError) {
console.error('[OAuth] Failed to initialize user via /v1/me:', meError);
}
} catch (meError) {
console.error('[OAuth] Failed to initialize user via /v1/me:', meError);
}
}
// Emit success event to renderer
emitOAuthEvent({
provider,
success: true,
...(signedInUserId ? { userId: signedInUserId } : {}),
});
} catch (error) {
console.error('OAuth token exchange failed:', error);
// Log cause chain for debugging (e.g. OAUTH_INVALID_RESPONSE -> OperationProcessingError)
let cause: unknown = error;
while (cause != null && typeof cause === 'object' && 'cause' in cause) {
cause = (cause as { cause?: unknown }).cause;
if (cause != null) {
console.error('[OAuth] Caused by:', cause);
// Emit success event to renderer
emitOAuthEvent({
provider,
success: true,
...(signedInUserId ? { userId: signedInUserId } : {}),
});
} catch (error) {
console.error('OAuth token exchange failed:', error);
// Log cause chain for debugging (e.g. OAUTH_INVALID_RESPONSE -> OperationProcessingError)
let cause: unknown = error;
while (cause != null && typeof cause === 'object' && 'cause' in cause) {
cause = (cause as { cause?: unknown }).cause;
if (cause != null) {
console.error('[OAuth] Caused by:', cause);
}
}
const errorMessage = getOAuthErrorMessage(error);
emitOAuthEvent({ provider, success: false, error: errorMessage });
throw error;
} finally {
// Clean up
activeFlows.delete(state);
if (activeFlow && activeFlow.state === state) {
clearTimeout(activeFlow.cleanupTimeout);
activeFlow.server.close();
activeFlow = null;
}
}
const errorMessage = getOAuthErrorMessage(error);
emitOAuthEvent({ provider, success: false, error: errorMessage });
throw error;
} finally {
// Clean up
},
// Static providers (Google BYOK) keep fixed-port behaviour to match the
// pre-registered redirect URI at the provider's console. DCR providers
// can fall back since we register the actual bound port below.
{ fallback: !isStaticClient },
);
// Server is bound. Any throw between here and `activeFlow = ...` would
// leak the port — `cancelActiveFlow` only closes it once activeFlow is set.
try {
// TOCTOU guard: resolveStartPort probed the registered port and found it
// free, but the port could have been grabbed between probe and real bind,
// causing fallback to a different port. The cached client_id is registered
// for the old port — clear it so getProviderConfiguration re-registers
// with the actual bound port.
if (!isStaticClient && boundPort !== startPort) {
console.log(`[OAuth] ${provider}: bound port ${boundPort} differs from start port ${startPort}, clearing stale DCR registration`);
await getClientRegistrationRepo().clearClientRegistration(provider);
}
const redirectUri = buildRedirectUri(boundPort);
const config = await getProviderConfiguration(provider, redirectUri, credentials);
const { verifier: codeVerifier, challenge: codeChallenge } = await oauthClient.generatePKCE();
state = oauthClient.generateState();
const scopes = providerConfig.scopes || [];
activeFlows.set(state, { codeVerifier, provider, config });
const authUrl = oauthClient.buildAuthorizationUrl(config, {
redirect_uri: redirectUri,
scope: scopes.join(' '),
code_challenge: codeChallenge,
state,
});
// Set timeout to clean up abandoned flows (2 minutes)
const cleanupTimeout = setTimeout(() => {
if (activeFlow?.state === state) {
console.log(`[OAuth] Cleaning up abandoned OAuth flow for ${provider} (timeout)`);
cancelActiveFlow('timed_out');
}
}, 2 * 60 * 1000);
activeFlow = {
provider,
state,
server,
cleanupTimeout,
};
// Open in system browser (shares cookies/sessions with user's regular browser)
shell.openExternal(authUrl.toString());
return { success: true };
} catch (setupError) {
// Post-bind setup failed — close the server so the port is released and
// a retry isn't blocked by our own zombie listener.
server.close();
if (state) {
activeFlows.delete(state);
if (activeFlow && activeFlow.state === state) {
clearTimeout(activeFlow.cleanupTimeout);
activeFlow.server.close();
activeFlow = null;
}
}
});
// Set timeout to clean up abandoned flows (2 minutes)
// This prevents memory leaks if user never completes the OAuth flow
const cleanupTimeout = setTimeout(() => {
if (activeFlow?.state === state) {
console.log(`[OAuth] Cleaning up abandoned OAuth flow for ${provider} (timeout)`);
cancelActiveFlow('timed_out');
}
}, 2 * 60 * 1000); // 2 minutes
// Store complete flow state for cleanup
activeFlow = {
provider,
state,
server,
cleanupTimeout,
};
// Open in system browser (shares cookies/sessions with user's regular browser)
shell.openExternal(authUrl.toString());
// Wait for callback (server will handle it)
return { success: true };
throw setupError;
}
} catch (error) {
console.error('OAuth connection failed:', error);
return {
@ -431,7 +508,7 @@ export async function disconnectProvider(provider: string): Promise<{ success: b
if (connection.mode === 'rowboat' && connection.tokens?.access_token) {
try {
const revokeUrl = `https://oauth2.googleapis.com/revoke?token=${encodeURIComponent(connection.tokens.access_token)}`;
const res = await fetch(revokeUrl, { method: 'POST' });
const res = await fetch(revokeUrl, { method: 'POST', signal: AbortSignal.timeout(5000) });
if (!res.ok) {
console.warn(`[OAuth] Google revoke returned ${res.status}; continuing with local disconnect`);
}
@ -455,6 +532,81 @@ export async function disconnectProvider(provider: string): Promise<{ success: b
}
}
/**
* Startup migration for Google scope changes. When a connected Google grant was
* issued before a scope was added (e.g. old installs on gmail.readonly that
* never received gmail.modify), invalidate it so the user is prompted to
* reconnect and re-grant with the current scopes. The currently-requested
* scopes in the provider config are the source of truth: a grant missing any
* of them is treated as stale.
*
* We revoke + clear the stale token but DELIBERATELY keep the provider entry
* with an `error` set rather than calling disconnectProvider (which deletes the
* whole entry). The renderer's reconnect prompts the sidebar "Reconnect your
* accounts" alert and the connectors "Reconnect" row key off this `error`
* field, not off the connected flag. A fully deleted entry has no error and is
* indistinguishable from "never connected", so no prompt would ever appear.
*
* Tokens with no recorded scopes (very old installs that never persisted them)
* are also treated as stale. Safe to call on every startup it's a no-op once
* the grant covers all current scopes, and once invalidated the early return on
* the missing token keeps it from re-running until the user reconnects.
*/
export async function disconnectGoogleIfScopesStale(): Promise<void> {
try {
const oauthRepo = getOAuthRepo();
const connection = await oauthRepo.read('google');
// Not connected (or already invalidated) — nothing to migrate.
if (!connection.tokens) {
return;
}
const providerConfig = await getProviderConfig('google');
const requiredScopes = providerConfig.scopes ?? [];
if (requiredScopes.length === 0) {
return;
}
const granted = new Set(connection.tokens.scopes ?? []);
const missingScopes = requiredScopes.filter((scope) => !granted.has(scope));
if (missingScopes.length === 0) {
return;
}
console.log(
`[OAuth] Google grant is missing current scopes [${missingScopes.join(', ')}]; ` +
'invalidating it so the user is prompted to reconnect with the new scopes.'
);
// Best-effort revoke at Google for rowboat-mode grants (mirrors disconnectProvider).
if (connection.mode === 'rowboat' && connection.tokens.access_token) {
try {
const revokeUrl = `https://oauth2.googleapis.com/revoke?token=${encodeURIComponent(connection.tokens.access_token)}`;
const res = await fetch(revokeUrl, { method: 'POST', signal: AbortSignal.timeout(5000) });
if (!res.ok) {
console.warn(`[OAuth] Google revoke returned ${res.status}; continuing with local invalidation`);
}
} catch (error) {
console.warn('[OAuth] Google revoke failed; continuing with local invalidation:', error);
}
}
// Drop the stale token but keep the entry with an error so the reconnect
// prompt fires (see the note above).
await oauthRepo.upsert('google', {
tokens: null,
error: 'Google permissions changed. Please reconnect to continue.',
});
// Nudge any already-open window to re-read state. The renderer's initial
// mount also re-reads, so the prompt shows even if no window is up yet.
emitOAuthEvent({ provider: 'google', success: false });
} catch (error) {
console.error('[OAuth] Google scope migration check failed:', error);
}
}
/**
* Get access token for a provider (internal use only)
* Refreshes token if expired

View file

@ -35,6 +35,30 @@
}
}
/* Radix Collapsible expand/collapse animate height (via the radix CSS var)
plus a subtle fade. Used by the web search card. */
@keyframes collapsible-down {
from {
height: 0;
opacity: 0;
}
to {
height: var(--radix-collapsible-content-height);
opacity: 1;
}
}
@keyframes collapsible-up {
from {
height: var(--radix-collapsible-content-height);
opacity: 1;
}
to {
height: 0;
opacity: 0;
}
}
@media (prefers-reduced-motion: no-preference) {
a:nth-of-type(2) .logo {
animation: logo-spin infinite 20s linear;
@ -232,6 +256,10 @@
color: var(--gm-text-faint);
}
.gmail-row-shell {
position: relative;
}
.gmail-row {
display: grid;
grid-template-columns: 12px minmax(140px, 0.22fr) minmax(0, 1fr) 60px;
@ -249,20 +277,56 @@
transition: background 120ms ease;
}
.gmail-row-actions {
position: absolute;
right: 16px;
top: 50%;
transform: translateY(-50%);
display: flex;
align-items: center;
gap: 2px;
opacity: 0;
pointer-events: none;
transition: opacity 120ms ease;
}
.gmail-row-shell:hover .gmail-row-actions {
opacity: 1;
pointer-events: auto;
}
.gmail-row-shell:hover .gmail-row-date {
visibility: hidden;
}
.gmail-row-action {
display: inline-flex;
align-items: center;
justify-content: center;
width: 26px;
height: 26px;
border: none;
border-radius: 4px;
background: transparent;
color: var(--gm-text-muted);
cursor: pointer;
transition: background 120ms ease, color 120ms ease;
}
.gmail-row-action:hover {
background: var(--gm-bg-pill-hover);
color: var(--gm-text-strong);
}
.gmail-row-action-danger:hover {
color: #e8453c;
}
.gmail-row:hover {
background: var(--gm-bg-row-hover);
box-shadow: none;
}
.upcoming-event-row {
background-color: transparent;
transition: background-color 120ms ease;
}
.upcoming-event-row:hover {
background-color: var(--gm-bg-pill-hover);
}
.gmail-row-selected {
background: var(--gm-bg-row-selected);
box-shadow: inset 2px 0 0 var(--gm-accent);
@ -496,6 +560,7 @@
.gmail-message-from span,
.gmail-message-to,
.gmail-message-cc,
.gmail-message-date {
color: var(--gm-text-muted);
font-size: 12px;
@ -694,6 +759,126 @@
font: inherit;
}
.gmail-compose-label {
flex: none;
min-width: 28px;
color: var(--gm-text-muted);
}
.gmail-compose-subject-input {
min-width: 0;
flex: 1;
border: none;
outline: none;
background: transparent;
color: var(--gm-text);
font: inherit;
}
/* Recipient (To / Cc / Bcc) rows with editable chips */
.gmail-recipient-row {
display: flex;
align-items: flex-start;
gap: 8px;
min-height: 34px;
padding: 5px 12px;
border-bottom: 1px solid var(--gm-border);
font-size: 13px;
}
.gmail-recipient-label {
flex: none;
min-width: 28px;
padding-top: 5px;
color: var(--gm-text-muted);
}
.gmail-recipient-field {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 4px;
flex: 1;
min-width: 0;
}
.gmail-recipient-chip {
display: inline-flex;
align-items: center;
gap: 4px;
max-width: 100%;
height: 24px;
padding: 0 4px 0 10px;
border-radius: 12px;
background: var(--gm-bg-pill);
color: var(--gm-text);
font-size: 12px;
line-height: 1;
}
.gmail-recipient-chip-label {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
max-width: 240px;
}
.gmail-recipient-chip-remove {
display: inline-flex;
align-items: center;
justify-content: center;
width: 16px;
height: 16px;
border: none;
border-radius: 50%;
background: transparent;
color: var(--gm-text-muted);
font-size: 14px;
line-height: 1;
cursor: pointer;
}
.gmail-recipient-chip-remove:hover {
background: var(--gm-bg-pill-hover);
color: var(--gm-text);
}
.gmail-recipient-input {
flex: 1 1 80px;
min-width: 80px;
height: 24px;
border: none;
outline: none;
background: transparent;
color: var(--gm-text);
font: inherit;
font-size: 13px;
}
.gmail-recipient-trailing {
flex: none;
padding-top: 5px;
}
.gmail-recipient-toggles {
display: flex;
gap: 10px;
}
.gmail-recipient-toggles button {
border: none;
background: transparent;
color: var(--gm-text-muted);
font: inherit;
font-size: 12px;
cursor: pointer;
}
.gmail-recipient-toggles button:hover {
color: var(--gm-text);
text-decoration: underline;
}
.gmail-compose-toolbar {
display: flex;
align-items: center;
@ -1015,6 +1200,10 @@
--scrollbar-track: oklch(0.95 0 0);
--scrollbar-thumb: oklch(0.75 0 0);
--scrollbar-thumb-hover: oklch(0.65 0 0);
/* Subtle raised-card surface: tints toward foreground, so it reads a hair
darker than the background in light mode and a hair lighter in dark mode.
Shared by the web search card and tool-call group. */
--card-surface: color-mix(in oklab, var(--background) 98.5%, var(--foreground));
--rowboat-panel: oklch(0.97 0 0);
--rowboat-raised: oklch(1 0 0);
--rowboat-wash: color-mix(in oklab, var(--background) 88%, var(--primary) 12%);

File diff suppressed because it is too large Load diff

View file

@ -9,6 +9,7 @@ import { useState, useRef, useEffect } from "react";
export type AskHumanRequestProps = ComponentProps<"div"> & {
query: string;
options?: string[];
onResponse: (response: string) => void;
isProcessing?: boolean;
};
@ -16,17 +17,21 @@ export type AskHumanRequestProps = ComponentProps<"div"> & {
export const AskHumanRequest = ({
className,
query,
options,
onResponse,
isProcessing = false,
...props
}: AskHumanRequestProps) => {
const [response, setResponse] = useState("");
const textareaRef = useRef<HTMLTextAreaElement>(null);
const hasOptions = Array.isArray(options) && options.length > 0;
useEffect(() => {
// Auto-focus the textarea when component mounts
textareaRef.current?.focus();
}, []);
// Auto-focus the textarea when in free-text mode; nothing to focus for buttons.
if (!hasOptions) {
textareaRef.current?.focus();
}
}, [hasOptions]);
const handleSubmit = () => {
const trimmed = response.trim();
@ -36,6 +41,11 @@ export const AskHumanRequest = ({
}
};
const handleOptionClick = (option: string) => {
if (isProcessing) return;
onResponse(option);
};
const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
@ -65,30 +75,47 @@ export const AskHumanRequest = ({
{query}
</p>
</div>
<div className="space-y-2">
<Textarea
ref={textareaRef}
value={response}
onChange={(e) => setResponse(e.target.value)}
onKeyDown={handleKeyDown}
placeholder="Type your response..."
disabled={isProcessing}
rows={3}
className="resize-none"
/>
<div className="flex justify-end">
<Button
variant="default"
size="sm"
onClick={handleSubmit}
disabled={!canSubmit}
className="gap-2"
>
<ArrowUpIcon className="size-4" />
Send Response
</Button>
{hasOptions ? (
<div className="flex flex-wrap gap-2">
{options!.map((option) => (
<Button
key={option}
variant="outline"
size="sm"
onClick={() => handleOptionClick(option)}
disabled={isProcessing}
className="bg-background"
>
{option}
</Button>
))}
</div>
</div>
) : (
<div className="space-y-2">
<Textarea
ref={textareaRef}
value={response}
onChange={(e) => setResponse(e.target.value)}
onKeyDown={handleKeyDown}
placeholder="Type your response..."
disabled={isProcessing}
rows={3}
className="resize-none"
/>
<div className="flex justify-end">
<Button
variant="default"
size="sm"
onClick={handleSubmit}
disabled={!canSubmit}
className="gap-2"
>
<ArrowUpIcon className="size-4" />
Send Response
</Button>
</div>
</div>
)}
</div>
</div>
</div>

View file

@ -9,9 +9,10 @@ import {
DropdownMenuItem,
} from "@/components/ui/dropdown-menu";
import { cn } from "@/lib/utils";
import { AlertTriangleIcon, CheckCircleIcon, CheckIcon, ChevronDownIcon, XCircleIcon, XIcon } from "lucide-react";
import type { ComponentProps } from "react";
import { AlertTriangleIcon, CheckIcon, ChevronDownIcon, RefreshCwIcon, Terminal, XIcon } from "lucide-react";
import { useState, type ComponentProps } from "react";
import { ToolCallPart } from "@x/shared/dist/message.js";
import { ToolPermissionMetadata } from "@x/shared/dist/runs.js";
import z from "zod";
export type PermissionRequestProps = ComponentProps<"div"> & {
@ -20,8 +21,18 @@ export type PermissionRequestProps = ComponentProps<"div"> & {
onApproveSession?: () => void;
onApproveAlways?: () => void;
onDeny?: () => void;
onSwitchAgent?: (newAgent: 'claude' | 'codex') => void;
isProcessing?: boolean;
response?: 'approve' | 'deny' | null;
permission?: z.infer<typeof ToolPermissionMetadata>;
};
const fileActionLabels: Record<string, string> = {
read: "Read file",
list: "List folder",
search: "Search files",
write: "Write files",
delete: "Delete path",
};
export const PermissionRequest = ({
@ -31,28 +42,47 @@ export const PermissionRequest = ({
onApproveSession,
onApproveAlways,
onDeny,
onSwitchAgent,
isProcessing = false,
response = null,
permission,
...props
}: PermissionRequestProps) => {
// Extract command from arguments if it's executeCommand
const command = toolCall.toolName === "executeCommand"
const command = permission?.kind === "command" || toolCall.toolName === "executeCommand"
? (typeof toolCall.arguments === "object" && toolCall.arguments !== null && "command" in toolCall.arguments
? String(toolCall.arguments.command)
: JSON.stringify(toolCall.arguments))
: null;
const filePermission = permission?.kind === "file" ? permission : null;
// Detect acpx coding-agent invocations so we can show the agent identity and
// offer a one-click swap-and-retry.
const acpxAgent: 'claude' | 'codex' | null = (() => {
if (!command) return null;
const match = command.match(/\bacpx\b[\s\S]*?\b(claude|codex)\b\s+exec\b/);
return match ? (match[1] as 'claude' | 'codex') : null;
})();
const otherAgent: 'claude' | 'codex' | null = acpxAgent === 'claude' ? 'codex' : acpxAgent === 'codex' ? 'claude' : null;
const agentDisplay = acpxAgent === 'claude' ? 'Claude Code' : acpxAgent === 'codex' ? 'Codex' : null;
const otherDisplay = otherAgent === 'claude' ? 'Claude Code' : otherAgent === 'codex' ? 'Codex' : null;
const isResponded = response !== null;
const isApproved = response === 'approve';
// Once a response is chosen, collapse the details to just the header.
// Users can click the header to expand them again.
const [expanded, setExpanded] = useState(false);
const showDetails = !isResponded || expanded;
return (
<div
className={cn(
"not-prose mb-4 w-full rounded-md border",
isResponded
? isApproved
? "border-green-500/50 bg-green-50/50 dark:bg-green-950/20"
: "border-red-500/50 bg-red-50/50 dark:bg-red-950/20"
? "border-green-500/60 bg-green-200/80 dark:border-green-500/40 dark:bg-green-900/40"
: "border-[#fa2525]/70 bg-[#fa2525]/30 dark:border-[#fa2525]/60 dark:bg-[#fa2525]/30"
: "border-amber-500/50 bg-amber-50/50 dark:bg-amber-950/20",
className
)}
@ -60,50 +90,41 @@ export const PermissionRequest = ({
>
<div className="p-4 space-y-4">
<div className="flex items-start gap-3">
{isResponded ? (
isApproved ? (
<CheckCircleIcon className="size-5 text-green-600 dark:text-green-500 shrink-0 mt-0.5" />
) : (
<XCircleIcon className="size-5 text-red-600 dark:text-red-500 shrink-0 mt-0.5" />
)
) : (
{!isResponded && (
<AlertTriangleIcon className="size-5 text-amber-600 dark:text-amber-500 shrink-0 mt-0.5" />
)}
<div className="flex-1 space-y-2">
<div className="flex items-center gap-2">
<div
className={cn("flex items-center gap-2", isResponded && "cursor-pointer select-none")}
onClick={isResponded ? () => setExpanded((v) => !v) : undefined}
>
<div className="flex-1">
<h3 className="font-semibold text-sm text-foreground">
{isResponded ? (isApproved ? "Permission Granted" : "Permission Denied") : "Permission Required"}
</h3>
<p className="text-sm text-muted-foreground mt-1">
{isResponded ? "Requested:" : "The agent wants to execute:"} <span className="font-mono font-medium">{toolCall.toolName}</span>
{agentDisplay && (
<Badge
variant="secondary"
className="ml-2 align-middle bg-secondary text-foreground"
>
<Terminal className="size-3 mr-1" />
{agentDisplay}
</Badge>
)}
</p>
</div>
{isResponded && (
<Badge
variant="secondary"
<ChevronDownIcon
className={cn(
"shrink-0",
isApproved
? "bg-green-100 text-green-700 dark:bg-green-900/50 dark:text-green-400"
: "bg-red-100 text-red-700 dark:bg-red-900/50 dark:text-red-400"
"size-4 shrink-0 text-muted-foreground transition-transform",
expanded ? "rotate-180" : "rotate-0"
)}
>
{isApproved ? (
<>
<CheckIcon className="size-3 mr-1" />
Approved
</>
) : (
<>
<XIcon className="size-3 mr-1" />
Denied
</>
)}
</Badge>
/>
)}
</div>
{command && (
{showDetails && command && (
<div className="rounded-md border bg-background/50 p-3 mt-3">
<p className="text-xs font-medium text-muted-foreground mb-1.5 uppercase tracking-wide">
Command
@ -113,7 +134,35 @@ export const PermissionRequest = ({
</pre>
</div>
)}
{!command && toolCall.arguments && (
{showDetails && filePermission && (
<div className="rounded-md border bg-background/50 p-3 mt-3 space-y-3">
<div>
<p className="text-xs font-medium text-muted-foreground mb-1.5 uppercase tracking-wide">
Action
</p>
<p className="text-xs font-medium text-foreground">
{fileActionLabels[filePermission.operation] ?? filePermission.operation}
</p>
</div>
<div>
<p className="text-xs font-medium text-muted-foreground mb-1.5 uppercase tracking-wide">
Path{filePermission.paths.length === 1 ? "" : "s"}
</p>
<pre className="whitespace-pre-wrap text-xs font-mono text-foreground break-all">
{filePermission.paths.join("\n")}
</pre>
</div>
<div>
<p className="text-xs font-medium text-muted-foreground mb-1.5 uppercase tracking-wide">
Approval Scope
</p>
<pre className="whitespace-pre-wrap text-xs font-mono text-foreground break-all">
{filePermission.pathPrefix}
</pre>
</div>
</div>
)}
{showDetails && !command && !filePermission && toolCall.arguments && (
<div className="rounded-md border bg-background/50 p-3 mt-3">
<p className="text-xs font-medium text-muted-foreground mb-1.5 uppercase tracking-wide">
Arguments
@ -133,12 +182,12 @@ export const PermissionRequest = ({
size="sm"
onClick={onApprove}
disabled={isProcessing}
className={cn("flex-1", command && "rounded-r-none")}
className={cn("flex-1", (command || filePermission) && "rounded-r-none")}
>
<CheckIcon className="size-4" />
Approve
</Button>
{command && (
{(command || filePermission) && (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
@ -171,6 +220,18 @@ export const PermissionRequest = ({
<XIcon className="size-4" />
Deny
</Button>
{otherAgent && otherDisplay && onSwitchAgent && (
<Button
variant="secondary"
size="sm"
onClick={() => onSwitchAgent(otherAgent)}
disabled={isProcessing}
className="flex-1"
>
<RefreshCwIcon className="size-4" />
Use {otherDisplay} instead
</Button>
)}
</div>
)}
</div>

View file

@ -1,6 +1,5 @@
"use client";
import { Badge } from "@/components/ui/badge";
import {
Collapsible,
CollapsibleContent,
@ -9,17 +8,15 @@ import {
import { cn } from "@/lib/utils";
import type { ToolUIPart } from "ai";
import {
CheckCircleIcon,
ChevronDownIcon,
CircleIcon,
ClockIcon,
WrenchIcon,
CircleCheck,
LoaderIcon,
XCircleIcon,
} from "lucide-react";
import { type ComponentProps, type ReactNode, isValidElement, useState } from "react";
import { AnimatePresence, motion } from "motion/react";
import type { ToolCall, ToolGroup as ToolGroupType } from "@/lib/chat-conversation";
import { getToolDisplayName, getToolGroupSummary, toToolState } from "@/lib/chat-conversation";
import { getToolActionsSummary, getToolDisplayName, getToolGroupSummary, toToolState } from "@/lib/chat-conversation";
const formatToolValue = (value: unknown) => {
if (typeof value === "string") return value;
@ -52,7 +49,10 @@ export type ToolProps = ComponentProps<typeof Collapsible>;
export const Tool = ({ className, ...props }: ToolProps) => (
<Collapsible
className={cn("not-prose mb-4 w-full rounded-md border", className)}
className={cn(
"not-prose mb-4 w-full rounded-[28px] border bg-[var(--card-surface)] transition-colors duration-150 ease-out hover:border-foreground/30",
className
)}
{...props}
/>
);
@ -62,37 +62,17 @@ export type ToolHeaderProps = {
type: ToolUIPart["type"];
state: ToolUIPart["state"];
className?: string;
/** Hide the leading status icon (used for child rows inside a tool group). */
hideLeadIcon?: boolean;
};
const getStatusBadge = (status: ToolUIPart["state"]) => {
const labels: Record<ToolUIPart["state"], string> = {
"input-streaming": "Pending",
"input-available": "Running",
// @ts-expect-error state only available in AI SDK v6
"approval-requested": "Awaiting Approval",
"approval-responded": "Responded",
"output-available": "Completed",
"output-error": "Error",
"output-denied": "Denied",
};
const icons: Record<ToolUIPart["state"], ReactNode> = {
"input-streaming": <CircleIcon className="size-4" />,
"input-available": <ClockIcon className="size-4 animate-pulse" />,
// @ts-expect-error state only available in AI SDK v6
"approval-requested": <ClockIcon className="size-4 text-yellow-600" />,
"approval-responded": <CheckCircleIcon className="size-4 text-blue-600" />,
"output-available": <CheckCircleIcon className="size-4 text-green-600" />,
"output-error": <XCircleIcon className="size-4 text-red-600" />,
"output-denied": <XCircleIcon className="size-4 text-orange-600" />,
};
return (
<Badge className="gap-1.5 rounded-full text-xs" variant="secondary">
{icons[status]}
{labels[status]}
</Badge>
);
// Lead icon shown to the left of the tool label: spinner while running, a
// green check when done, a red cross on error. Shared by ToolHeader (single
// tools) and the tool-call group.
const getLeadIcon = (state: ToolUIPart["state"]): ReactNode => {
if (state === "output-available") return <CircleCheck className="size-4 shrink-0 text-green-600" />;
if (state === "output-error") return <XCircleIcon className="size-4 shrink-0 text-red-600" />;
return <LoaderIcon className="size-4 shrink-0 animate-spin text-muted-foreground" />;
};
export const ToolHeader = ({
@ -100,6 +80,7 @@ export const ToolHeader = ({
title,
type,
state,
hideLeadIcon,
...props
}: ToolHeaderProps) => {
const displayTitle = title ?? type.split("-").slice(1).join("-")
@ -107,13 +88,13 @@ export const ToolHeader = ({
return (
<CollapsibleTrigger
className={cn(
"flex w-full items-center justify-between gap-4 p-3",
"group flex w-full cursor-pointer items-center justify-between gap-3 px-4 py-2.5",
className
)}
{...props}
>
<div className="flex min-w-0 flex-1 items-center gap-2">
<WrenchIcon className="size-4 shrink-0 text-muted-foreground" />
{!hideLeadIcon && getLeadIcon(state)}
<span
className="min-w-0 flex-1 truncate text-left font-medium text-sm"
title={displayTitle}
@ -121,10 +102,7 @@ export const ToolHeader = ({
{displayTitle}
</span>
</div>
<div className="flex shrink-0 items-center gap-3">
{getStatusBadge(state)}
<ChevronDownIcon className="size-4 text-muted-foreground transition-transform group-data-[state=open]:rotate-180" />
</div>
<ChevronDownIcon className="size-4 shrink-0 text-muted-foreground transition-transform group-data-[state=open]:rotate-180" />
</CollapsibleTrigger>
)
};
@ -134,7 +112,7 @@ export type ToolContentProps = ComponentProps<typeof CollapsibleContent>;
export const ToolContent = ({ className, ...props }: ToolContentProps) => (
<CollapsibleContent
className={cn(
"data-[state=closed]:fade-out-0 data-[state=closed]:slide-out-to-top-2 data-[state=open]:slide-in-from-top-2 text-popover-foreground outline-none data-[state=closed]:animate-out data-[state=open]:animate-in",
"overflow-hidden text-popover-foreground outline-none data-[state=open]:animate-[collapsible-down_0.09s_ease-out] data-[state=closed]:animate-[collapsible-up_0.08s_ease-in]",
className
)}
{...props}
@ -247,41 +225,48 @@ export const ToolGroupComponent = ({ group, isToolOpen, onToolOpenChange }: Tool
const isCompleted = state === 'output-available' || state === 'output-error'
const runningTool = group.items.find(t => t.status === 'running' || t.status === 'pending')
const currentTool = runningTool ?? group.items[group.items.length - 1]
const summary = isCompleted
? `Ran ${group.items.length} tool${group.items.length !== 1 ? 's' : ''}`
const toolCount = group.items.length
const ranLabel = `Ran ${toolCount} tool${toolCount !== 1 ? 's' : ''}`
const actions = isCompleted ? getToolActionsSummary(group.items) : ''
// Plain string used as the AnimatePresence key + tooltip; the rendered node
// shows the action summary in a lighter gray than the "Ran N tools" prefix.
const summaryText = isCompleted
? `${ranLabel} · ${actions}`
: currentTool ? getToolDisplayName(currentTool) : getToolGroupSummary(group.items)
const summaryNode: ReactNode = isCompleted
? <>{ranLabel} <span className="font-normal text-muted-foreground">{`· ${actions}`}</span></>
: summaryText
const leadIcon = getLeadIcon(state)
return (
<Collapsible
open={open}
onOpenChange={setOpen}
className="not-prose mb-4 w-full rounded-md border"
className="not-prose mb-4 w-full rounded-[28px] border bg-[var(--card-surface)] transition-colors duration-150 ease-out hover:border-foreground/30"
>
<CollapsibleTrigger className="flex w-full items-center justify-between gap-4 p-3">
<CollapsibleTrigger className="flex w-full cursor-pointer items-center justify-between gap-3 px-4 py-2.5">
<div className="flex min-w-0 flex-1 items-center gap-2">
<WrenchIcon className="size-4 shrink-0 text-muted-foreground" />
{leadIcon}
<div className="relative min-w-0 flex-1 overflow-hidden" style={{ height: '1.25rem' }}>
<AnimatePresence mode="popLayout" initial={false}>
<motion.span
key={summary}
key={summaryText}
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -10 }}
transition={{ duration: 0.18, ease: 'easeOut' }}
className="absolute inset-0 truncate text-left font-medium text-sm leading-5"
title={summary}
title={summaryText}
>
{summary}
{summaryNode}
</motion.span>
</AnimatePresence>
</div>
</div>
<div className="flex shrink-0 items-center gap-3">
{getStatusBadge(state)}
<ChevronDownIcon className={cn("size-4 text-muted-foreground transition-transform", open && "rotate-180")} />
</div>
<ChevronDownIcon className={cn("size-4 shrink-0 text-muted-foreground transition-transform", open && "rotate-180")} />
</CollapsibleTrigger>
<CollapsibleContent className="border-t">
<CollapsibleContent className="overflow-hidden data-[state=open]:animate-[collapsible-down_0.09s_ease-out] data-[state=closed]:animate-[collapsible-up_0.08s_ease-in]">
<div className="flex flex-col gap-2 p-2">
{group.items.map((tool) => {
const toolState = toToolState(tool.status)
@ -291,12 +276,14 @@ export const ToolGroupComponent = ({ group, isToolOpen, onToolOpenChange }: Tool
key={tool.id}
open={isOpen}
onOpenChange={(o) => onToolOpenChange(tool.id, o)}
className="mb-0 border-border/60"
className="mb-0 rounded-[20px] border-border/60 bg-transparent hover:border-border/60"
>
<ToolHeader
title={getToolDisplayName(tool)}
type={`tool-${tool.name}`}
state={toolState}
className="text-muted-foreground"
hideLeadIcon
/>
<ToolContent>
<ToolTabbedContent

View file

@ -5,12 +5,14 @@ import {
CollapsibleContent,
CollapsibleTrigger,
} from "@/components/ui/collapsible";
import { cn } from "@/lib/utils";
import {
CheckCircleIcon,
ChevronDownIcon,
GlobeIcon,
LoaderIcon,
} from "lucide-react";
import { useEffect, useMemo, useState } from "react";
import { AnimatePresence, motion } from "motion/react";
interface WebSearchResultProps {
query: string;
@ -19,39 +21,219 @@ interface WebSearchResultProps {
title?: string;
}
// How long each fetched website stays on the rolling header before the
// next one slides in. Kept slow enough to read the domain + title.
const ROLL_INTERVAL_MS = 700;
// How many favicons to show in the settled stack before the rest collapse
// into a "+N" chip. The text names this many domains too, so the chip count
// (total - MAX_STACK) lines up with the "and N others" in the summary.
const MAX_STACK = 3;
function getDomain(url: string): string {
try {
return new URL(url).hostname;
return new URL(url).hostname.replace(/^www\./, "");
} catch {
return url;
}
}
function faviconUrl(domain: string, size = 32): string {
return `https://www.google.com/s2/favicons?domain=${domain}&sz=${size}`;
}
// Collapse the result list into unique domains, preserving order.
function uniqueDomains(results: WebSearchResultProps["results"]): string[] {
const seen = new Set<string>();
const out: string[] = [];
for (const result of results) {
const domain = getDomain(result.url);
if (seen.has(domain)) continue;
seen.add(domain);
out.push(domain);
}
return out;
}
// Summary with text hierarchy: "Searched" + "and N others" are secondary
// weight/color, the domain names are primary text at medium weight.
function buildSearchedSummary(domains: string[]): React.ReactNode {
const muted = "font-normal text-muted-foreground";
const name = (d: string) => <span className="font-medium text-foreground">{d}</span>;
if (domains.length === 1) {
return (
<>
<span className={muted}>Searched </span>
{name(domains[0])}
</>
);
}
if (domains.length === 2) {
return (
<>
<span className={muted}>Searched </span>
{name(domains[0])}
<span className={muted}> and </span>
{name(domains[1])}
</>
);
}
const others = domains.length - 2;
return (
<>
<span className={muted}>Searched </span>
{name(domains[0])}
<span className={muted}>, </span>
{name(domains[1])}
<span className={muted}>{` and ${others} other${others !== 1 ? "s" : ""}`}</span>
</>
);
}
type RollPhase = "searching" | "rolling" | "settled";
export function WebSearchResult({ query, results, status, title = "Searched the web" }: WebSearchResultProps) {
const isRunning = status === "pending" || status === "running";
const [open, setOpen] = useState(false);
return (
<Collapsible defaultOpen className="not-prose mb-4 w-full rounded-md border">
<CollapsibleTrigger className="flex w-full items-center justify-between gap-4 p-3">
<div className="flex items-center gap-2">
<GlobeIcon className="size-4 text-muted-foreground" />
<span className="font-medium text-sm">{title}</span>
</div>
<ChevronDownIcon className="size-4 text-muted-foreground transition-transform group-data-[state=open]:rotate-180" />
</CollapsibleTrigger>
<CollapsibleContent>
<div className="px-3 pb-3 space-y-3">
{/* Query + result count */}
<div className="flex items-center justify-between gap-2">
<div className="flex items-center gap-2 text-sm text-muted-foreground min-w-0">
<GlobeIcon className="size-3.5 shrink-0" />
<span className="truncate">{query}</span>
</div>
{results.length > 0 && (
<span className="text-xs text-muted-foreground whitespace-nowrap">
{results.length} result{results.length !== 1 ? "s" : ""}
const domains = useMemo(() => uniqueDomains(results), [results]);
// Drive the one-shot rolling reveal. Results arrive all at once, so we
// simulate "fetching one site at a time" by stepping through them with the
// same slide animation the tool group uses, then settle on a summary.
// `settled` is seeded from the initial status so a card loaded already-
// complete from history skips straight to the summary (no roll).
const [settled, setSettled] = useState(() => !isRunning);
const [rollIndex, setRollIndex] = useState(0);
// Phase is fully derived: searching while the tool runs, rolling once
// results land, then settled. No setState-in-effect needed for transitions.
const phase: RollPhase = isRunning
? "searching"
: !settled && results.length > 0
? "rolling"
: "settled";
// Warm the browser cache for every favicon the moment results arrive, so
// each icon is already loaded by the time its row rolls in (~700ms each).
// Without this the network fetch lags the text and rows flash icon-less.
useEffect(() => {
for (const result of results) {
const img = new Image();
img.src = faviconUrl(getDomain(result.url));
}
}, [results]);
// Advance the roll, then settle after the last site has had its moment.
// setState only fires inside the timeout callback, never synchronously.
useEffect(() => {
if (phase !== "rolling") return;
const isLast = rollIndex >= results.length - 1;
const timer = setTimeout(
() => (isLast ? setSettled(true) : setRollIndex((i) => i + 1)),
ROLL_INTERVAL_MS,
);
return () => clearTimeout(timer);
}, [phase, rollIndex, results.length]);
// Build the content for the compact (collapsed) header line. Each distinct
// value gets a unique key so AnimatePresence runs the slide transition.
let headerKey: string;
let headerContent: React.ReactNode;
if (phase === "searching") {
headerKey = "searching";
headerContent = (
<span className="flex min-w-0 flex-1 items-center gap-2 text-muted-foreground">
<LoaderIcon className="size-4 shrink-0 animate-spin" />
<span className="truncate">Searching the web&hellip;</span>
</span>
);
} else if (phase === "rolling") {
const result = results[rollIndex];
const domain = getDomain(result.url);
headerKey = `roll-${rollIndex}`;
headerContent = (
<span className="flex min-w-0 flex-1 items-center gap-2">
<img src={faviconUrl(domain)} alt="" className="size-4 shrink-0 rounded-sm bg-muted/60" />
<span className="truncate">
<span className="text-muted-foreground">{domain}</span>
<span className="text-muted-foreground/50"> &middot; </span>
<span>{result.title}</span>
</span>
</span>
);
} else {
headerKey = "settled";
const stack = domains.slice(0, MAX_STACK);
// Chip count matches the "and N others" in the text (total minus the 2
// named domains), shown only when there are sites beyond the stack.
const overflow = domains.length > MAX_STACK ? domains.length - 2 : 0;
headerContent = (
<span className="flex min-w-0 flex-1 items-center gap-2.5">
{domains.length > 0 ? (
<span className="flex shrink-0 items-center">
{stack.map((domain, i) => (
<img
key={domain}
src={faviconUrl(domain)}
alt=""
className="size-5 rounded-full bg-muted object-cover -ml-[5px] first:ml-0"
style={{ zIndex: stack.length - i }}
/>
))}
{overflow > 0 && (
<span className="ml-0.5 flex size-5 shrink-0 items-center justify-center rounded-full bg-foreground/10 dark:bg-muted text-[10px] font-medium text-muted-foreground">
+{overflow}
</span>
)}
</span>
) : (
<GlobeIcon className="size-4 shrink-0 text-muted-foreground" />
)}
<span className="truncate text-sm">
{domains.length > 0 ? buildSearchedSummary(domains) : title}
</span>
</span>
);
}
return (
<Collapsible
open={open}
onOpenChange={setOpen}
className="not-prose mb-4 w-full rounded-[28px] border bg-[var(--card-surface)] transition-colors duration-150 ease-out hover:border-foreground/30"
>
<CollapsibleTrigger className="flex w-full cursor-pointer items-center justify-between gap-3 px-4 py-2.5">
{/* Rolling header: clipped, fixed height so sliding lines stay contained */}
<div className="relative min-w-0 flex-1 overflow-hidden" style={{ height: "1.5rem" }}>
<AnimatePresence mode="popLayout" initial={false}>
<motion.span
key={headerKey}
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -10 }}
transition={{ duration: 0.18, ease: "easeOut" }}
className="absolute inset-0 flex items-center text-left font-medium text-sm"
>
{headerContent}
</motion.span>
</AnimatePresence>
</div>
<div className="flex shrink-0 items-center gap-2">
{phase === "settled" && domains.length > 0 && (
<span className="whitespace-nowrap text-xs text-muted-foreground">
{domains.length} source{domains.length !== 1 ? "s" : ""}
</span>
)}
<ChevronDownIcon className={cn("size-4 text-muted-foreground transition-transform", open && "rotate-180")} />
</div>
</CollapsibleTrigger>
<CollapsibleContent className="overflow-hidden data-[state=open]:animate-[collapsible-down_0.09s_ease-out] data-[state=closed]:animate-[collapsible-up_0.08s_ease-in]">
<div className="px-4 pb-3 space-y-3">
{/* Query */}
<div className="flex items-center gap-2 text-sm text-muted-foreground min-w-0">
<GlobeIcon className="size-3.5 shrink-0" />
<span className="truncate">{query}</span>
</div>
{/* Results list */}
@ -73,7 +255,7 @@ export function WebSearchResult({ query, results, status, title = "Searched the
>
<div className="flex items-center gap-2 min-w-0">
<img
src={`https://www.google.com/s2/favicons?domain=${domain}&sz=16`}
src={faviconUrl(domain)}
alt=""
className="size-4 shrink-0"
/>
@ -88,20 +270,13 @@ export function WebSearchResult({ query, results, status, title = "Searched the
</div>
)}
{/* Status */}
<div className="flex items-center gap-1.5 text-xs text-muted-foreground">
{isRunning ? (
<>
<LoaderIcon className="size-3.5 animate-spin" />
<span>Searching...</span>
</>
) : (
<>
<CheckCircleIcon className="size-3.5 text-green-600" />
<span>Done</span>
</>
)}
</div>
{/* Status — only while the search is still running. */}
{isRunning && (
<div className="flex items-center gap-1.5 text-xs text-muted-foreground">
<LoaderIcon className="size-3.5 animate-spin" />
<span>Searching...</span>
</div>
)}
</div>
</CollapsibleContent>
</Collapsible>

View file

@ -1418,6 +1418,18 @@ export interface BgTasksViewProps {
* "Edit with Copilot" button in the detail-view sidebar footer.
*/
onEditWithCopilot?: (slug: string) => void
/**
* If provided, the view opens with this task already selected. Updates to
* this prop sync into internal state so the sidebar can swap which task is
* focused without remounting the view.
*/
initialSlug?: string | null
/**
* Bump this counter to force a re-focus on `initialSlug` even when the
* slug value itself didn't change (e.g. user clicks the same task in the
* sidebar twice after navigating away inside the view).
*/
slugVersion?: number
}
function formatLastRanLabel(iso: string | null | undefined): string {
@ -1425,9 +1437,12 @@ function formatLastRanLabel(iso: string | null | undefined): string {
return formatRelativeTime(iso) || 'Never'
}
export function BgTasksView({ onCreateWithCopilot, onEditWithCopilot }: BgTasksViewProps = {}) {
export function BgTasksView({ onCreateWithCopilot, onEditWithCopilot, initialSlug, slugVersion }: BgTasksViewProps = {}) {
const [items, setItems] = useState<BackgroundTaskSummary[]>([])
const [selectedSlug, setSelectedSlug] = useState<string | null>(null)
const [selectedSlug, setSelectedSlug] = useState<string | null>(initialSlug ?? null)
useEffect(() => {
setSelectedSlug(initialSlug ?? null)
}, [initialSlug, slugVersion])
const [loading, setLoading] = useState(true)
const [error, setError] = useState<string | null>(null)
const [showNewDialog, setShowNewDialog] = useState(false)

View file

@ -0,0 +1,61 @@
import { useEffect, useState } from "react"
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog"
import { Button } from "@/components/ui/button"
import type { BillingErrorMatch } from "@/lib/billing-error"
interface BillingRowboatAccount {
config?: {
appUrl?: string | null
} | null
}
interface BillingErrorDialogProps {
open: boolean
match: BillingErrorMatch | null
onOpenChange: (open: boolean) => void
}
export function BillingErrorDialog({ open, match, onOpenChange }: BillingErrorDialogProps) {
const [appUrl, setAppUrl] = useState<string | null>(null)
useEffect(() => {
if (!open) return
window.ipc
.invoke('account:getRowboat', null)
.then((account: BillingRowboatAccount) => setAppUrl(account.config?.appUrl ?? null))
.catch(() => {})
}, [open])
if (!match) return null
const handleUpgrade = () => {
if (appUrl) window.open(`${appUrl}?intent=upgrade`)
onOpenChange(false)
}
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle>{match.title}</DialogTitle>
<DialogDescription>{match.subtitle}</DialogDescription>
</DialogHeader>
<DialogFooter className="gap-2 sm:gap-2">
<Button variant="outline" onClick={() => onOpenChange(false)}>
Dismiss
</Button>
<Button onClick={handleUpgrade} disabled={!appUrl}>
{match.cta}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
)
}

View file

@ -0,0 +1,106 @@
import { ArrowUpRight, Bot, Mail, MessageSquare, Sparkles, Telescope } from 'lucide-react'
import { cn } from '@/lib/utils'
import { formatRelativeTime } from '@/lib/relative-time'
export interface ChatEmptyStateRun {
id: string
title?: string
createdAt: string
}
interface ChatEmptyStateProps {
recentRuns?: ChatEmptyStateRun[]
onSelectRun?: (runId: string) => void
onOpenChatHistory?: () => void
/** Fill the composer with a starter prompt (does not submit). */
onPickPrompt: (prompt: string) => void
/** Use a wider column — for the full-screen chat where the narrow column looks cramped. */
wide?: boolean
}
const SUGGESTED_ACTIONS: { icon: typeof Mail; title: string; sub: string; prompt: string }[] = [
{ icon: Mail, title: 'Draft a reply', sub: 'to an email', prompt: "Let's draft a reply to [name]'s email" },
{ icon: Bot, title: 'Set up a background agent', sub: 'that automates tasks', prompt: 'Set up a background agent that automates [task]' },
{ icon: Telescope, title: 'Research a topic', sub: 'create a local wiki for me', prompt: 'Research [topic] and create a local wiki for me' },
]
/**
* Empty-state body for the chat surface: greeting, recent chats, and starter
* action cards. Shown in both the side-pane copilot and full-screen chat.
*/
export function ChatEmptyState({
recentRuns = [],
onSelectRun,
onOpenChatHistory,
onPickPrompt,
wide = false,
}: ChatEmptyStateProps) {
return (
<div className={cn('mx-auto flex w-full flex-col gap-6 px-2 py-6', wide ? 'max-w-2xl' : 'max-w-md')}>
<div className="flex items-center gap-3">
<div className="flex size-9 shrink-0 items-center justify-center rounded-[10px] border border-border bg-background text-foreground">
<Sparkles className="size-[17px]" />
</div>
<div>
<div className="text-base font-semibold tracking-tight">What are we working on?</div>
<div className="text-xs text-muted-foreground">Ask anything, or pick up where you left off.</div>
</div>
</div>
{recentRuns.length > 0 && (
<div>
<div className="flex items-center px-1 pb-2 text-[10.5px] font-semibold uppercase tracking-wider text-muted-foreground">
<span className="flex-1">Recent chats</span>
{onOpenChatHistory && (
<button
type="button"
onClick={onOpenChatHistory}
className="inline-flex items-center gap-0.5 text-[11px] font-medium normal-case tracking-normal text-primary hover:underline"
>
View all
<ArrowUpRight className="size-3" />
</button>
)}
</div>
<div className="flex flex-col gap-0.5">
{recentRuns.slice(0, 4).map((run) => (
<button
key={run.id}
type="button"
onClick={() => onSelectRun?.(run.id)}
className="flex items-center gap-2.5 rounded-md px-2.5 py-2 text-left hover:bg-accent"
>
<MessageSquare className="size-3.5 shrink-0 text-muted-foreground" />
<span className="min-w-0 flex-1 truncate text-[13px]">{run.title || '(Untitled chat)'}</span>
<span className="shrink-0 text-[11px] text-muted-foreground">{formatRelativeTime(run.createdAt)}</span>
</button>
))}
</div>
</div>
)}
<div>
<div className="px-1 pb-2 text-[10.5px] font-semibold uppercase tracking-wider text-muted-foreground">
{recentRuns.length > 0 ? 'Or start fresh' : 'Get started'}
</div>
<div className="flex flex-col gap-2">
{SUGGESTED_ACTIONS.map((action) => (
<button
key={action.title}
type="button"
onClick={() => onPickPrompt(action.prompt)}
className="flex items-start gap-2.5 rounded-lg border border-border bg-background px-3 py-2.5 text-left transition-colors hover:bg-accent"
>
<action.icon className="mt-0.5 size-3.5 shrink-0 text-muted-foreground" />
<div className="min-w-0 flex-1">
<div className="text-[12.8px] font-medium">{action.title}</div>
<div className="mt-0.5 text-[11.5px] text-muted-foreground">{action.sub}</div>
</div>
</button>
))}
</div>
</div>
</div>
)
}

View file

@ -0,0 +1,114 @@
import { ArrowUpRight, ChevronDown, MessageSquare, Plus } from 'lucide-react'
import { Button } from '@/components/ui/button'
import { cn } from '@/lib/utils'
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip'
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu'
import { formatRelativeTime } from '@/lib/relative-time'
export interface ChatHeaderRecentRun {
id: string
title?: string
createdAt: string
}
export interface ChatHeaderProps {
activeTitle: string
onNewChatTab: () => void
recentRuns?: ChatHeaderRecentRun[]
activeRunId?: string | null
onSelectRun?: (runId: string) => void
onOpenChatHistory?: () => void
}
/**
* Header controls for the copilot/chat surface: the active-chat title with a
* recent-chats history dropdown, plus the new-chat button. Rendered identically
* whether the chat lives in the side pane (ChatSidebar) or full screen (App
* content header). There is a single chat conversation at a time switching
* between chats happens through the history dropdown.
*/
export function ChatHeader({
activeTitle,
onNewChatTab,
recentRuns = [],
activeRunId,
onSelectRun,
onOpenChatHistory,
}: ChatHeaderProps) {
const hasHistory = recentRuns.length > 0 || Boolean(onOpenChatHistory)
return (
<>
{hasHistory ? (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<button
type="button"
className="titlebar-no-drag flex min-w-0 flex-1 items-center gap-2 rounded-md px-3 text-sm font-medium text-foreground outline-none hover:bg-accent/60"
aria-label="Chat history"
>
<MessageSquare className="size-4 shrink-0 text-muted-foreground" />
<span className="truncate">{activeTitle}</span>
<ChevronDown className="size-3.5 shrink-0 text-muted-foreground" />
</button>
</DropdownMenuTrigger>
<DropdownMenuContent align="start" className="w-72">
{recentRuns.length > 0 && (
<DropdownMenuLabel className="text-[10.5px] font-semibold uppercase tracking-wider text-muted-foreground">
Recent
</DropdownMenuLabel>
)}
{recentRuns.slice(0, 6).map((run) => (
<DropdownMenuItem
key={run.id}
onClick={() => onSelectRun?.(run.id)}
className={cn('gap-2', activeRunId === run.id && 'bg-accent')}
>
<span className="min-w-0 flex-1 truncate">{run.title || '(Untitled chat)'}</span>
<span className="shrink-0 text-[11px] text-muted-foreground">
{formatRelativeTime(run.createdAt)}
</span>
</DropdownMenuItem>
))}
{onOpenChatHistory && (
<>
{recentRuns.length > 0 && <DropdownMenuSeparator />}
<DropdownMenuItem onClick={onOpenChatHistory} className="gap-2 text-primary">
<ArrowUpRight className="size-4" />
View all chats
</DropdownMenuItem>
</>
)}
</DropdownMenuContent>
</DropdownMenu>
) : (
<div className="flex min-w-0 flex-1 items-center gap-2 px-3 text-sm font-medium text-foreground">
<MessageSquare className="size-4 shrink-0 text-muted-foreground" />
<span className="truncate">{activeTitle}</span>
</div>
)}
<Tooltip>
<TooltipTrigger asChild>
<Button
variant="ghost"
size="icon"
onClick={onNewChatTab}
className="titlebar-no-drag my-1 h-8 w-8 shrink-0 text-muted-foreground hover:text-foreground"
aria-label="New chat"
>
<Plus className="size-5" />
</Button>
</TooltipTrigger>
<TooltipContent side="bottom">New chat</TooltipContent>
</Tooltip>
</>
)
}

View file

@ -0,0 +1,177 @@
import { useCallback, useMemo, useState } from 'react'
import { ExternalLink, MessageSquare, SearchIcon, SquarePen, Trash2 } from 'lucide-react'
import { Button } from '@/components/ui/button'
import {
ContextMenu,
ContextMenuContent,
ContextMenuItem,
ContextMenuSeparator,
ContextMenuTrigger,
} from '@/components/ui/context-menu'
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog'
import { formatRelativeTime } from '@/lib/relative-time'
type Run = {
id: string
title?: string
createdAt: string
agentId: string
}
type ChatHistoryViewProps = {
runs: Run[]
currentRunId?: string | null
processingRunIds?: Set<string>
onSelectRun: (runId: string) => void
onOpenInNewTab?: (runId: string) => void
onDeleteRun: (runId: string) => Promise<void> | void
onNewChat?: () => void
onOpenSearch?: () => void
}
export function ChatHistoryView({
runs,
currentRunId,
processingRunIds,
onSelectRun,
onOpenInNewTab,
onDeleteRun,
onNewChat,
onOpenSearch,
}: ChatHistoryViewProps) {
const [pendingDeleteId, setPendingDeleteId] = useState<string | null>(null)
const sortedRuns = useMemo(() => {
return [...runs].sort((a, b) => {
const at = new Date(a.createdAt).getTime()
const bt = new Date(b.createdAt).getTime()
return (Number.isNaN(bt) ? 0 : bt) - (Number.isNaN(at) ? 0 : at)
})
}, [runs])
const handleConfirmDelete = useCallback(async () => {
if (!pendingDeleteId) return
const id = pendingDeleteId
setPendingDeleteId(null)
await onDeleteRun(id)
}, [pendingDeleteId, onDeleteRun])
return (
<div className="flex h-full flex-col overflow-hidden">
<div className="shrink-0 flex items-center justify-between gap-3 border-b border-border px-8 py-6">
<h1 className="text-2xl font-bold tracking-tight">Chat history</h1>
<div className="flex items-center gap-2">
{onOpenSearch && (
<button
type="button"
onClick={onOpenSearch}
className="inline-flex items-center gap-1.5 rounded-md border border-border bg-background px-3 py-1.5 text-sm text-foreground transition-colors hover:bg-accent"
>
<SearchIcon className="size-4" />
<span>Search</span>
</button>
)}
{onNewChat && (
<Button size="sm" onClick={onNewChat}>
<SquarePen className="size-4" />
New chat
</Button>
)}
</div>
</div>
<div className="flex-1 overflow-y-auto">
<div className="min-w-[480px]">
<div className="sticky top-0 z-10 flex items-center border-b border-border bg-background px-6 py-2 text-xs font-medium text-muted-foreground">
<div className="flex-1">Title</div>
<div className="w-32 shrink-0">Created</div>
</div>
{sortedRuns.length === 0 ? (
<div className="px-6 py-8 text-sm text-muted-foreground">No chats yet.</div>
) : (
sortedRuns.map((run) => {
const isActive = currentRunId === run.id
const isProcessing = processingRunIds?.has(run.id)
return (
<ContextMenu key={run.id}>
<ContextMenuTrigger asChild>
<button
type="button"
onClick={(e) => {
if (e.metaKey && onOpenInNewTab) {
onOpenInNewTab(run.id)
} else {
onSelectRun(run.id)
}
}}
className={[
'flex w-full items-center border-b border-border/60 px-6 py-1.5 text-left text-sm transition-colors hover:bg-accent',
isActive ? 'bg-accent/60' : '',
].join(' ')}
>
<div className="flex flex-1 items-center gap-2 min-w-0">
<MessageSquare className="size-4 shrink-0 text-muted-foreground" />
<span className="min-w-0 truncate">{run.title || '(Untitled chat)'}</span>
</div>
<div className="w-32 shrink-0 text-xs text-muted-foreground tabular-nums">
{formatRelativeTime(run.createdAt)}
</div>
</button>
</ContextMenuTrigger>
<ContextMenuContent className="w-48">
{onOpenInNewTab && (
<>
<ContextMenuItem onClick={() => onOpenInNewTab(run.id)}>
<ExternalLink className="mr-2 size-4" />
Open in new tab
</ContextMenuItem>
<ContextMenuSeparator />
</>
)}
{!isProcessing && (
<ContextMenuItem
variant="destructive"
onClick={() => setPendingDeleteId(run.id)}
>
<Trash2 className="mr-2 size-4" />
Delete
</ContextMenuItem>
)}
</ContextMenuContent>
</ContextMenu>
)
})
)}
</div>
</div>
<Dialog open={!!pendingDeleteId} onOpenChange={(open) => { if (!open) setPendingDeleteId(null) }}>
<DialogContent showCloseButton={false} className="sm:max-w-sm">
<DialogHeader>
<DialogTitle>Delete chat</DialogTitle>
<DialogDescription>
Are you sure you want to delete this chat?
</DialogDescription>
</DialogHeader>
<DialogFooter>
<Button variant="outline" onClick={() => setPendingDeleteId(null)}>
Cancel
</Button>
<Button variant="destructive" onClick={() => void handleConfirmDelete()}>
Delete
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</div>
)
}

View file

@ -18,6 +18,7 @@ import {
Mic,
Plus,
Square,
Terminal,
X,
} from 'lucide-react'
@ -28,7 +29,6 @@ import {
DropdownMenuItem,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu'
import {
@ -109,7 +109,7 @@ function getAttachmentIcon(kind: AttachmentIconKind) {
}
interface ChatInputInnerProps {
onSubmit: (message: PromptInputMessage, mentions?: FileMention[], attachments?: StagedAttachment[], searchEnabled?: boolean) => void
onSubmit: (message: PromptInputMessage, mentions?: FileMention[], attachments?: StagedAttachment[], searchEnabled?: boolean, codeMode?: 'claude' | 'codex') => void
onStop?: () => void
isProcessing: boolean
isStopping?: boolean
@ -133,6 +133,10 @@ interface ChatInputInnerProps {
onTtsModeChange?: (mode: 'summary' | 'full') => void
/** Fired when the user picks a different model in the dropdown (only when no run exists yet). */
onSelectedModelChange?: (model: SelectedModel | null) => void
/** Work directory for this chat (per-chat). Null when none is set. */
workDir?: string | null
/** Fired when the user sets/changes/clears the work directory for this chat. */
onWorkDirChange?: (value: string | null) => void
}
function ChatInputInner({
@ -159,6 +163,8 @@ function ChatInputInner({
onToggleTts,
onTtsModeChange,
onSelectedModelChange,
workDir = null,
onWorkDirChange,
}: ChatInputInnerProps) {
const controller = usePromptInputController()
const message = controller.textInput.value
@ -173,7 +179,9 @@ function ChatInputInner({
const [searchEnabled, setSearchEnabled] = useState(false)
const [searchAvailable, setSearchAvailable] = useState(false)
const [isRowboatConnected, setIsRowboatConnected] = useState(false)
const [workDir, setWorkDir] = useState<string | null>(null)
const [codingAgent, setCodingAgent] = useState<'claude' | 'codex'>('claude')
const [codeModeEnabled, setCodeModeEnabled] = useState(false)
const [codeModeFeatureEnabled, setCodeModeFeatureEnabled] = useState(false)
// When a run exists, freeze the dropdown to the run's resolved model+provider.
useEffect(() => {
@ -256,54 +264,137 @@ function ChatInputInner({
return () => window.removeEventListener('models-config-changed', handler)
}, [loadModelConfig])
// Load currently configured work directory
const loadWorkDir = useCallback(async () => {
try {
const result = await window.ipc.invoke('workspace:readFile', { path: 'config/workdir.json' })
const parsed = JSON.parse(result.data)
const value = typeof parsed?.path === 'string' ? parsed.path.trim() : ''
setWorkDir(value || null)
} catch {
setWorkDir(null)
// Load the global code-mode feature flag (from settings) and stay in sync.
useEffect(() => {
const load = () => {
window.ipc.invoke('codeMode:getConfig', null)
.then((r) => setCodeModeFeatureEnabled(r.enabled))
.catch(() => setCodeModeFeatureEnabled(false))
}
load()
window.addEventListener('code-mode-config-changed', load)
return () => window.removeEventListener('code-mode-config-changed', load)
}, [])
// If the feature is turned off in settings, also turn off any per-conversation chip.
useEffect(() => {
loadWorkDir()
}, [isActive, loadWorkDir])
if (!codeModeFeatureEnabled && codeModeEnabled) {
setCodeModeEnabled(false)
}
}, [codeModeFeatureEnabled, codeModeEnabled])
// Listen for coding-agent runs that were triggered without the explicit code-mode
// toggle. App.tsx dispatches this when it sees an acpx executeCommand fire. We
// flip the pill on with the detected agent so the UI reflects what's happening.
useEffect(() => {
const handler = (ev: Event) => {
const detail = (ev as CustomEvent<{ runId?: string; agent?: 'claude' | 'codex' }>).detail
if (!detail || !detail.agent) return
if (runId && detail.runId && detail.runId !== runId) return
setCodeModeEnabled(true)
setCodingAgent(detail.agent)
}
window.addEventListener('code-mode-detected', handler)
return () => window.removeEventListener('code-mode-detected', handler)
}, [runId])
// Cross-platform basename — handles both / and \ separators.
const basename = useCallback((p: string): string => {
const trimmed = p.replace(/[\\/]+$/, '')
const idx = Math.max(trimmed.lastIndexOf('/'), trimmed.lastIndexOf('\\'))
return idx >= 0 ? trimmed.slice(idx + 1) : trimmed
}, [])
// Load coding-agent preference for a given workdir.
// Storage: config/coding-agents.json — { [workDirPath]: 'claude' | 'codex' }
const loadCodingAgentFor = useCallback(async (dir: string | null): Promise<'claude' | 'codex'> => {
if (!dir) return 'claude'
try {
const result = await window.ipc.invoke('workspace:readFile', { path: 'config/coding-agents.json' })
const parsed = JSON.parse(result.data) as Record<string, unknown>
const value = parsed?.[dir]
if (value === 'codex' || value === 'claude') return value
} catch {
/* file missing or invalid — fall through to default */
}
return 'claude'
}, [])
const persistCodingAgent = useCallback(async (dir: string, agent: 'claude' | 'codex') => {
let existing: Record<string, 'claude' | 'codex'> = {}
try {
const result = await window.ipc.invoke('workspace:readFile', { path: 'config/coding-agents.json' })
const parsed = JSON.parse(result.data) as Record<string, unknown>
for (const [k, v] of Object.entries(parsed ?? {})) {
if (v === 'claude' || v === 'codex') existing[k] = v
}
} catch { /* start fresh */ }
existing[dir] = agent
await window.ipc.invoke('workspace:writeFile', {
path: 'config/coding-agents.json',
data: JSON.stringify(existing, null, 2),
})
}, [])
// Work directory is owned per-chat by the parent (App). This component only
// drives the picker dialog and reports changes up via onWorkDirChange. Whenever
// the work directory changes, load its persisted coding-agent preference.
useEffect(() => {
let cancelled = false
loadCodingAgentFor(workDir).then((agent) => {
if (!cancelled) setCodingAgent(agent)
})
return () => { cancelled = true }
}, [workDir, loadCodingAgentFor])
const handleSetWorkDir = useCallback(async () => {
try {
let defaultPath: string | undefined = workDir ?? undefined
try {
const { root } = await window.ipc.invoke('workspace:getRoot', null)
const workspaceRel = 'knowledge/Workspace'
const exists = await window.ipc.invoke('workspace:exists', { path: workspaceRel })
if (!exists.exists) {
await window.ipc.invoke('workspace:mkdir', { path: workspaceRel, recursive: true })
}
defaultPath = `${root.replace(/\/$/, '')}/${workspaceRel}`
} catch (err) {
console.error('Failed to resolve Workspace path; falling back to current workDir', err)
}
const { path: chosen } = await window.ipc.invoke('dialog:openDirectory', {
title: 'Choose work directory',
defaultPath: workDir ?? undefined,
defaultPath,
})
if (!chosen) return
await window.ipc.invoke('workspace:writeFile', {
path: 'config/workdir.json',
data: JSON.stringify({ path: chosen }, null, 2),
})
setWorkDir(chosen)
onWorkDirChange?.(chosen)
setCodingAgent(await loadCodingAgentFor(chosen))
toast.success(`Work directory set: ${chosen}`)
} catch (err) {
console.error('Failed to set work directory', err)
toast.error('Failed to set work directory')
}
}, [workDir])
}, [workDir, onWorkDirChange, loadCodingAgentFor])
const handleClearWorkDir = useCallback(async () => {
const handleClearWorkDir = useCallback(() => {
onWorkDirChange?.(null)
setCodingAgent('claude')
toast.success('Work directory cleared')
}, [onWorkDirChange])
const handleToggleCodingAgent = useCallback(async () => {
const next: 'claude' | 'codex' = codingAgent === 'claude' ? 'codex' : 'claude'
setCodingAgent(next)
// Persist only when scoped to a workdir; without one there's nothing to key on.
if (!workDir) return
try {
await window.ipc.invoke('workspace:writeFile', {
path: 'config/workdir.json',
data: JSON.stringify({}, null, 2),
})
setWorkDir(null)
toast.success('Work directory cleared')
await persistCodingAgent(workDir, next)
} catch (err) {
console.error('Failed to clear work directory', err)
toast.error('Failed to clear work directory')
console.error('Failed to save coding agent', err)
toast.error('Failed to save coding agent')
// revert on failure
setCodingAgent(codingAgent)
}
}, [])
}, [workDir, codingAgent, persistCodingAgent])
// Check search tool availability (exa or signed-in via gateway)
useEffect(() => {
@ -389,12 +480,15 @@ function ChatInputInner({
const handleSubmit = useCallback(() => {
if (!canSubmit) return
onSubmit({ text: message.trim(), files: [] }, controller.mentions.mentions, attachments, searchEnabled || undefined)
// codeMode is sticky per conversation — don't reset after send.
const effectiveCodeMode = codeModeEnabled ? codingAgent : undefined
onSubmit({ text: message.trim(), files: [] }, controller.mentions.mentions, attachments, searchEnabled || undefined, effectiveCodeMode)
controller.textInput.clear()
controller.mentions.clearMentions()
setAttachments([])
setSearchEnabled(false)
}, [attachments, canSubmit, controller, message, onSubmit, searchEnabled])
// Web search toggle stays on for the rest of the chat session; the user
// turns it off explicitly. (Not persisted across app restarts.)
}, [attachments, canSubmit, controller, message, onSubmit, searchEnabled, codeModeEnabled, codingAgent, workDir])
const handleKeyDown = useCallback((e: React.KeyboardEvent) => {
if (e.key === 'Enter' && !e.shiftKey) {
@ -539,15 +633,20 @@ function ChatInputInner({
</div>
<div className="flex items-center gap-2 px-4 pb-3">
<DropdownMenu>
<DropdownMenuTrigger asChild>
<button
type="button"
className="flex h-7 w-7 shrink-0 items-center justify-center rounded-full text-muted-foreground transition-colors hover:bg-muted hover:text-foreground"
aria-label="Add"
>
<Plus className="h-4 w-4" />
</button>
</DropdownMenuTrigger>
<Tooltip>
<TooltipTrigger asChild>
<DropdownMenuTrigger asChild>
<button
type="button"
className="flex h-7 w-7 shrink-0 items-center justify-center rounded-full text-muted-foreground transition-colors hover:bg-muted hover:text-foreground"
aria-label="Add"
>
<Plus className="h-4 w-4" />
</button>
</DropdownMenuTrigger>
</TooltipTrigger>
<TooltipContent side="top">Add files or set work directory</TooltipContent>
</Tooltip>
<DropdownMenuContent align="start" className="min-w-56">
<DropdownMenuItem onSelect={() => fileInputRef.current?.click()}>
<ImagePlus className="size-4" />
@ -557,28 +656,29 @@ function ChatInputInner({
<FolderCog className="size-4" />
<span>{workDir ? 'Change work directory' : 'Set work directory'}</span>
</DropdownMenuItem>
{workDir && (
<>
<DropdownMenuSeparator />
<DropdownMenuItem onSelect={() => { void handleClearWorkDir() }}>
<X className="size-4" />
<span>Clear work directory</span>
</DropdownMenuItem>
</>
)}
</DropdownMenuContent>
</DropdownMenu>
{workDir && (
<Tooltip>
<TooltipTrigger asChild>
<button
type="button"
onClick={handleSetWorkDir}
className="flex h-7 max-w-[180px] shrink-0 items-center gap-1.5 rounded-full border border-border bg-muted/40 px-2.5 text-xs text-muted-foreground transition-colors hover:bg-muted hover:text-foreground"
>
<FolderCog className="h-3.5 w-3.5" />
<span className="truncate">{workDir.split('/').pop() || workDir}</span>
</button>
<div className="group flex h-7 max-w-[180px] shrink-0 items-center rounded-full border border-border bg-muted/40 pl-2.5 pr-2 text-xs text-muted-foreground transition-colors hover:bg-muted hover:text-foreground">
<button
type="button"
onClick={handleSetWorkDir}
className="flex min-w-0 items-center gap-1.5"
>
<FolderCog className="h-3.5 w-3.5 shrink-0" />
<span className="truncate">{basename(workDir) || workDir}</span>
</button>
<button
type="button"
onClick={handleClearWorkDir}
aria-label="Remove work directory"
className="flex h-3.5 w-0 shrink-0 items-center justify-center overflow-hidden opacity-0 transition-all duration-150 ease-out hover:text-red-500 group-hover:ml-1 group-hover:w-3.5 group-hover:opacity-100"
>
<X className="h-3.5 w-3.5 shrink-0" />
</button>
</div>
</TooltipTrigger>
<TooltipContent side="top">
Work directory: {workDir}
@ -586,27 +686,75 @@ function ChatInputInner({
</Tooltip>
)}
{searchAvailable && (
searchEnabled ? (
<button
type="button"
onClick={() => setSearchEnabled(false)}
className="flex h-7 shrink-0 items-center gap-1.5 rounded-full border border-blue-200 bg-blue-50 px-2.5 text-blue-600 transition-colors hover:bg-blue-100 dark:border-blue-800 dark:bg-blue-950 dark:text-blue-400 dark:hover:bg-blue-900"
<button
type="button"
onClick={() => setSearchEnabled((v) => !v)}
aria-label="Search"
aria-pressed={searchEnabled}
className={cn(
'flex h-7 shrink-0 items-center rounded-full border px-1.5 transition-colors duration-150 ease-out',
searchEnabled
? 'border-blue-200 bg-blue-50 text-blue-600 hover:bg-blue-100 dark:border-blue-800 dark:bg-blue-950 dark:text-blue-400 dark:hover:bg-blue-900'
: 'border-transparent text-muted-foreground hover:bg-muted hover:text-foreground'
)}
>
<Globe className="h-4 w-4 shrink-0" />
<span
className={cn(
'overflow-hidden whitespace-nowrap text-xs font-medium transition-all duration-150 ease-out',
searchEnabled ? 'ml-1.5 max-w-[60px] opacity-100' : 'max-w-0 opacity-0'
)}
>
<Globe className="h-3.5 w-3.5" />
<span className="text-xs font-medium">Search</span>
<X className="h-3 w-3" />
</button>
) : (
<button
type="button"
onClick={() => setSearchEnabled(true)}
className="flex h-7 w-7 shrink-0 items-center justify-center rounded-full text-muted-foreground transition-colors hover:bg-muted hover:text-foreground"
aria-label="Search"
>
<Globe className="h-4 w-4" />
</button>
)
Search
</span>
</button>
)}
{codeModeFeatureEnabled && (codeModeEnabled ? (
<div className="flex h-7 shrink-0 items-center rounded-full bg-secondary text-xs font-medium text-foreground">
<Tooltip>
<TooltipTrigger asChild>
<button
type="button"
onClick={() => setCodeModeEnabled(false)}
className="flex h-full items-center gap-1.5 rounded-l-full pl-2.5 pr-2 transition-colors hover:bg-secondary/70"
>
<Terminal className="h-3.5 w-3.5" />
<span>Code</span>
</button>
</TooltipTrigger>
<TooltipContent side="top">Code mode on click to disable</TooltipContent>
</Tooltip>
<span className="text-foreground/30">·</span>
<Tooltip>
<TooltipTrigger asChild>
<button
type="button"
onClick={handleToggleCodingAgent}
className="flex h-full items-center rounded-r-full pl-2 pr-2.5 transition-colors hover:bg-secondary/70"
>
<span>{codingAgent === 'claude' ? 'Claude' : 'Codex'}</span>
</button>
</TooltipTrigger>
<TooltipContent side="top">
Coding agent: {codingAgent === 'claude' ? 'Claude Code' : 'Codex'} click to swap
</TooltipContent>
</Tooltip>
</div>
) : (
<Tooltip>
<TooltipTrigger asChild>
<button
type="button"
onClick={() => setCodeModeEnabled(true)}
className="flex h-7 w-7 shrink-0 items-center justify-center rounded-full text-muted-foreground transition-colors hover:bg-muted hover:text-foreground"
aria-label="Code mode"
>
<Terminal className="h-4 w-4" />
</button>
</TooltipTrigger>
<TooltipContent side="top">Use a coding agent (Claude Code or Codex)</TooltipContent>
</Tooltip>
))}
<div className="flex-1" />
{lockedModel ? (
<span
@ -767,7 +915,7 @@ export interface ChatInputWithMentionsProps {
knowledgeFiles: string[]
recentFiles: string[]
visibleFiles: string[]
onSubmit: (message: PromptInputMessage, mentions?: FileMention[], attachments?: StagedAttachment[]) => void
onSubmit: (message: PromptInputMessage, mentions?: FileMention[], attachments?: StagedAttachment[], searchEnabled?: boolean, codeMode?: 'claude' | 'codex') => void
onStop?: () => void
isProcessing: boolean
isStopping?: boolean
@ -790,6 +938,8 @@ export interface ChatInputWithMentionsProps {
onToggleTts?: () => void
onTtsModeChange?: (mode: 'summary' | 'full') => void
onSelectedModelChange?: (model: SelectedModel | null) => void
workDir?: string | null
onWorkDirChange?: (value: string | null) => void
}
export function ChatInputWithMentions({
@ -819,6 +969,8 @@ export function ChatInputWithMentions({
onToggleTts,
onTtsModeChange,
onSelectedModelChange,
workDir,
onWorkDirChange,
}: ChatInputWithMentionsProps) {
return (
<PromptInputProvider knowledgeFiles={knowledgeFiles} recentFiles={recentFiles} visibleFiles={visibleFiles}>
@ -846,6 +998,8 @@ export function ChatInputWithMentions({
onToggleTts={onToggleTts}
onTtsModeChange={onTtsModeChange}
onSelectedModelChange={onSelectedModelChange}
workDir={workDir}
onWorkDirChange={onWorkDirChange}
/>
</PromptInputProvider>
)

View file

@ -1,10 +1,12 @@
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { Bug, Maximize2, Minimize2, MoreHorizontal, SquarePen } from 'lucide-react'
import { ArrowLeft, ArrowRight, Bug, MoreHorizontal } from 'lucide-react'
import { toast } from 'sonner'
import { Button } from '@/components/ui/button'
import { cn } from '@/lib/utils'
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip'
import { ChatHeader } from '@/components/chat-header'
import { ChatEmptyState } from '@/components/chat-empty-state'
import {
DropdownMenu,
DropdownMenuContent,
@ -14,7 +16,6 @@ import {
import {
Conversation,
ConversationContent,
ConversationEmptyState,
ConversationScrollButton,
} from '@/components/ai-elements/conversation'
import {
@ -29,13 +30,12 @@ import { ComposioConnectCard } from '@/components/ai-elements/composio-connect-c
import { PermissionRequest } from '@/components/ai-elements/permission-request'
import { TerminalOutput } from '@/components/terminal-output'
import { AskHumanRequest } from '@/components/ai-elements/ask-human-request'
import { Suggestions } from '@/components/ai-elements/suggestions'
import { type PromptInputMessage, type FileMention } from '@/components/ai-elements/prompt-input'
import { FileCardProvider } from '@/contexts/file-card-context'
import { MarkdownPreOverride } from '@/components/ai-elements/markdown-code-override'
import { defaultRemarkPlugins } from 'streamdown'
import remarkBreaks from 'remark-breaks'
import { TabBar, type ChatTab } from '@/components/tab-bar'
import { type ChatTab } from '@/components/tab-bar'
import { ChatInputWithMentions, type StagedAttachment, type SelectedModel } from '@/components/chat-input-with-mentions'
import { ChatMessageAttachments } from '@/components/chat-message-attachments'
import { useSidebar } from '@/components/ui/sidebar'
@ -59,6 +59,7 @@ import {
parseAttachedFiles,
toToolState,
} from '@/lib/chat-conversation'
import { matchBillingError } from '@/lib/billing-error'
const streamdownComponents = { pre: MarkdownPreOverride }
@ -92,60 +93,6 @@ function AutoScrollPre({ className, children }: { className?: string; children:
)
}
/* ─── Billing error helpers ─── */
const BILLING_ERROR_PATTERNS = [
{
pattern: /upgrade required/i,
title: 'A subscription is required',
subtitle: 'Get started with a plan to access AI features in Rowboat.',
cta: 'Subscribe',
},
{
pattern: /not enough credits/i,
title: 'You\'ve run out of credits',
subtitle: 'Upgrade your plan for more credits. Free usage resets daily at 00:00 UTC.',
cta: 'Upgrade plan',
},
{
pattern: /subscription not active/i,
title: 'Your subscription is inactive',
subtitle: 'Reactivate your subscription to continue using AI features.',
cta: 'Reactivate',
},
] as const
function matchBillingError(message: string) {
return BILLING_ERROR_PATTERNS.find(({ pattern }) => pattern.test(message)) ?? null
}
interface BillingRowboatAccount {
config?: {
appUrl?: string | null
} | null
}
function BillingErrorCTA({ label }: { label: string }) {
const [appUrl, setAppUrl] = useState<string | null>(null)
useEffect(() => {
window.ipc.invoke('account:getRowboat', null)
.then((account: BillingRowboatAccount) => setAppUrl(account.config?.appUrl ?? null))
.catch(() => {})
}, [])
if (!appUrl) return null
return (
<button
onClick={() => window.open(`${appUrl}?intent=upgrade`)}
className="mt-1 rounded-md bg-amber-500/20 px-3 py-1.5 text-xs font-medium text-amber-100 transition-colors hover:bg-amber-500/30"
>
{label}
</button>
)
}
const MIN_WIDTH = 360
const MAX_WIDTH = 1600
const MIN_MAIN_PANE_WIDTH = 420
@ -180,10 +127,10 @@ interface ChatSidebarProps {
chatTabs: ChatTab[]
activeChatTabId: string
getChatTabTitle: (tab: ChatTab) => string
isChatTabProcessing: (tab: ChatTab) => boolean
onSwitchChatTab: (tabId: string) => void
onCloseChatTab: (tabId: string) => void
onNewChatTab: () => void
recentRuns?: { id: string; title?: string; createdAt: string }[]
onSelectRun?: (runId: string) => void
onOpenChatHistory?: () => void
onOpenFullScreen?: () => void
conversation: ConversationItem[]
currentAssistantMessage: string
@ -202,6 +149,8 @@ interface ChatSidebarProps {
getInitialDraft?: (tabId: string) => string | undefined
onDraftChangeForTab?: (tabId: string, text: string) => void
onSelectedModelChangeForTab?: (tabId: string, model: SelectedModel | null) => void
workDirByTab?: Record<string, string | null>
onWorkDirChangeForTab?: (tabId: string, value: string | null) => void
pendingAskHumanRequests?: ChatTabViewState['pendingAskHumanRequests']
allPermissionRequests?: ChatTabViewState['allPermissionRequests']
permissionResponses?: ChatTabViewState['permissionResponses']
@ -235,10 +184,10 @@ export function ChatSidebar({
chatTabs,
activeChatTabId,
getChatTabTitle,
isChatTabProcessing,
onSwitchChatTab,
onCloseChatTab,
onNewChatTab,
recentRuns = [],
onSelectRun,
onOpenChatHistory,
onOpenFullScreen,
conversation,
currentAssistantMessage,
@ -257,6 +206,8 @@ export function ChatSidebar({
getInitialDraft,
onDraftChangeForTab,
onSelectedModelChangeForTab,
workDirByTab = {},
onWorkDirChangeForTab,
pendingAskHumanRequests = new Map(),
allPermissionRequests = new Map(),
permissionResponses = new Map(),
@ -387,7 +338,6 @@ export function ChatSidebar({
if (tabId === activeChatTabId) return activeTabState
return chatTabStates[tabId] ?? emptyTabState
}, [activeChatTabId, activeTabState, chatTabStates, emptyTabState])
const hasConversation = activeTabState.conversation.length > 0 || Boolean(activeTabState.currentAssistantMessage)
const activeRunId = activeTabState.runId
const handleDownloadChatLog = useCallback(async () => {
if (!activeRunId) {
@ -517,19 +467,8 @@ export function ChatSidebar({
}
if (isErrorMessage(item)) {
const billingError = matchBillingError(item.message)
if (billingError) {
return (
<Message key={item.id} from="assistant" data-message-id={item.id}>
<MessageContent className="rounded-lg border border-amber-500/30 bg-amber-500/10 px-4 py-3">
<div className="space-y-2">
<p className="text-sm font-medium text-amber-200">{billingError.title}</p>
<p className="text-xs text-amber-300/80">{billingError.subtitle}</p>
<BillingErrorCTA label={billingError.cta} />
</div>
</MessageContent>
</Message>
)
if (matchBillingError(item.message)) {
return null
}
return (
<Message key={item.id} from="assistant" data-message-id={item.id}>
@ -589,28 +528,17 @@ export function ChatSidebar({
transition: isMaximized ? 'padding-left 200ms linear' : undefined,
}}
>
<TabBar
tabs={chatTabs}
activeTabId={activeChatTabId}
getTabTitle={getChatTabTitle}
getTabId={(tab) => tab.id}
isProcessing={isChatTabProcessing}
onSwitchTab={onSwitchChatTab}
onCloseTab={onCloseChatTab}
<ChatHeader
activeTitle={(() => {
const activeTab = chatTabs.find((tab) => tab.id === activeChatTabId)
return activeTab ? getChatTabTitle(activeTab) : 'New chat'
})()}
onNewChatTab={onNewChatTab}
recentRuns={recentRuns}
activeRunId={runId}
onSelectRun={onSelectRun}
onOpenChatHistory={onOpenChatHistory}
/>
<Tooltip>
<TooltipTrigger asChild>
<Button
variant="ghost"
size="icon"
onClick={onNewChatTab}
className="titlebar-no-drag my-1 h-8 w-8 shrink-0 text-muted-foreground hover:text-foreground"
>
<SquarePen className="size-5" />
</Button>
</TooltipTrigger>
<TooltipContent side="bottom">New chat tab</TooltipContent>
</Tooltip>
<DropdownMenu>
<Tooltip>
<TooltipTrigger asChild>
@ -647,14 +575,12 @@ export function ChatSidebar({
size="icon"
onClick={onOpenFullScreen}
className="titlebar-no-drag my-1 mr-2 h-8 w-8 shrink-0 text-muted-foreground hover:text-foreground"
aria-label={isMaximized ? 'Restore two-pane view' : 'Maximize chat view'}
aria-label={isMaximized ? 'Dock chat to side pane' : 'Expand chat'}
>
{isMaximized ? <Minimize2 className="size-5" /> : <Maximize2 className="size-5" />}
{isMaximized ? <ArrowRight className="size-5" /> : <ArrowLeft className="size-5" />}
</Button>
</TooltipTrigger>
<TooltipContent side="bottom">
{isMaximized ? 'Restore two-pane view' : 'Maximize chat view'}
</TooltipContent>
<TooltipContent side="bottom">{isMaximized ? 'Dock to side pane' : 'Expand chat'}</TooltipContent>
</Tooltip>
)}
</header>
@ -683,11 +609,19 @@ export function ChatSidebar({
anchorRequestKey={viewportAnchors[tab.id]?.requestKey}
className="relative flex-1"
>
<ConversationContent className={tabHasConversation ? 'mx-auto w-full max-w-4xl px-3 pb-28' : 'mx-auto w-full max-w-4xl min-h-full items-center justify-center px-3 pb-0'}>
<ConversationContent className={cn(
'mx-auto w-full max-w-4xl px-3',
tabHasConversation ? 'pb-28' : 'pb-0',
!tabHasConversation && isMaximized && 'min-h-full items-center justify-center',
)}>
{!tabHasConversation ? (
<ConversationEmptyState className="h-auto">
<div className="text-sm text-muted-foreground">Ask anything...</div>
</ConversationEmptyState>
<ChatEmptyState
wide={isMaximized}
recentRuns={recentRuns}
onSelectRun={onSelectRun}
onOpenChatHistory={onOpenChatHistory}
onPickPrompt={setLocalPresetMessage}
/>
) : (
<>
{groupConversationItems(
@ -711,7 +645,6 @@ export function ChatSidebar({
const response = tabState.permissionResponses.get(item.id) || null
return (
<React.Fragment key={item.id}>
{rendered}
<PermissionRequest
toolCall={permRequest.toolCall}
onApprove={() => onPermissionResponse(permRequest.toolCall.toolCallId, permRequest.subflow, 'approve')}
@ -721,6 +654,7 @@ export function ChatSidebar({
isProcessing={isActive && isProcessing}
response={response}
/>
{rendered}
</React.Fragment>
)
}
@ -765,9 +699,6 @@ export function ChatSidebar({
<div className="sticky bottom-0 z-10 bg-background pb-12 pt-0 shadow-lg">
<div className="pointer-events-none absolute inset-x-0 -top-6 h-6 bg-linear-to-t from-background to-transparent" />
<div className="mx-auto w-full max-w-4xl px-3">
{!hasConversation && (
<Suggestions onSelect={setLocalPresetMessage} className="mb-3 justify-center" />
)}
{chatTabs.map((tab) => {
const isActive = tab.id === activeChatTabId
const tabState = getTabState(tab.id)
@ -796,6 +727,8 @@ export function ChatSidebar({
initialDraft={getInitialDraft?.(tab.id)}
onDraftChange={onDraftChangeForTab ? (text) => onDraftChangeForTab(tab.id, text) : undefined}
onSelectedModelChange={onSelectedModelChangeForTab ? (m) => onSelectedModelChangeForTab(tab.id, m) : undefined}
workDir={workDirByTab[tab.id] ?? null}
onWorkDirChange={onWorkDirChangeForTab ? (v) => onWorkDirChangeForTab(tab.id, v) : undefined}
isRecording={isActive && isRecording}
recordingText={isActive ? recordingText : undefined}
recordingState={isActive ? recordingState : undefined}

View file

@ -1,5 +1,5 @@
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { Bold, Forward, Italic, Link as LinkIcon, List, ListOrdered, LoaderIcon, Paperclip, Quote, RefreshCw, Reply, Search, Send, Sparkles, Strikethrough } from 'lucide-react'
import { Archive, Bold, CheckCheck, Forward, Italic, Link as LinkIcon, List, ListOrdered, LoaderIcon, Mail, Paperclip, Quote, RefreshCw, Reply, ReplyAll, Search, Send, Sparkles, Strikethrough, Trash2 } from 'lucide-react'
import { useEditor, EditorContent, type Editor } from '@tiptap/react'
import StarterKit from '@tiptap/starter-kit'
import Link from '@tiptap/extension-link'
@ -8,9 +8,16 @@ import type { blocks } from '@x/shared'
import { cn } from '@/lib/utils'
import { toast } from '@/lib/toast'
import { useTheme } from '@/contexts/theme-context'
import { SettingsDialog } from '@/components/settings-dialog'
type GmailThread = blocks.GmailThread
type GmailThreadMessage = blocks.GmailThreadMessage
type GmailConnectionStatus = {
connected: boolean
hasRequiredScope: boolean
missingScopes: string[]
email: string | null
}
function formatInboxTime(value?: string): string {
if (!value) return ''
@ -79,6 +86,112 @@ function latestMessage(thread: GmailThread): GmailThreadMessage | undefined {
return thread.messages[thread.messages.length - 1]
}
// Split a raw header recipient string (e.g. `"Jo Bloggs" <jo@x.com>, b@y.com`) into
// individual address tokens, respecting commas inside quotes/angle brackets.
function splitAddresses(raw?: string): string[] {
if (!raw) return []
const tokens: string[] = []
let buf = ''
let inQuote = false
let depth = 0
for (const ch of raw) {
if (ch === '"') inQuote = !inQuote
else if (ch === '<') depth += 1
else if (ch === '>') depth = Math.max(0, depth - 1)
if ((ch === ',' || ch === ';' || ch === '\n') && !inQuote && depth === 0) {
const token = buf.trim()
if (token) tokens.push(token)
buf = ''
continue
}
buf += ch
}
const last = buf.trim()
if (last) tokens.push(last)
return tokens
}
// Display label for a recipient chip: the display name if present, else the bare address.
function recipientLabel(token: string): string {
const named = token.match(/^\s*"?([^"<]+?)"?\s*<[^>]+>\s*$/)
if (named?.[1]?.trim()) return named[1].trim()
return extractAddress(token)
}
// Dedupe tokens by lowercased email address, dropping any whose address is in `exclude`.
function dedupeRecipients(tokens: string[], exclude: Set<string>): string[] {
const seen = new Set<string>(exclude)
const out: string[] = []
for (const token of tokens) {
const addr = extractAddress(token).toLowerCase()
if (!addr || seen.has(addr)) continue
seen.add(addr)
out.push(token)
}
return out
}
// Compute the To / Cc recipients for a reply, reply-all, or forward, excluding "me".
function buildRecipients(
mode: ComposeMode,
thread: GmailThread,
selfEmail: string,
): { to: string[]; cc: string[] } {
if (mode === 'forward') return { to: [], cc: [] }
const latest = latestMessage(thread)
const self = selfEmail.toLowerCase()
const fromAddr = latest?.from ? extractAddress(latest.from).toLowerCase() : ''
const iAmSender = Boolean(self) && fromAddr === self
// If my own message is the latest, reply to whoever I sent it to; otherwise reply to the sender.
const rawTo = iAmSender ? splitAddresses(latest?.to) : (latest?.from ? [latest.from] : [])
const ccPool = iAmSender
? splitAddresses(latest?.cc)
: [...splitAddresses(latest?.to), ...splitAddresses(latest?.cc)]
const selfSet = new Set<string>(self ? [self] : [])
const to = dedupeRecipients(rawTo, selfSet)
if (iAmSender && to.length === 0 && self && rawTo.some((token) => extractAddress(token).toLowerCase() === self)) {
to.push(self)
}
if (mode === 'reply') return { to, cc: [] }
const ccExclude = new Set<string>(selfSet)
for (const token of to) ccExclude.add(extractAddress(token).toLowerCase())
const cc = dedupeRecipients(ccPool, ccExclude)
return { to, cc }
}
// Subject line for a reply ("Re: …") or forward ("Fwd: …"), avoiding double prefixes.
function composeSubject(mode: ComposeMode, rawSubject?: string): string {
const raw = (rawSubject || '').trim()
if (mode === 'forward') return /^fwd:/i.test(raw) ? raw : `Fwd: ${raw}`.trim()
return /^re:/i.test(raw) ? raw : `Re: ${raw}`.trim()
}
function buildForwardedContent(thread: GmailThread): string {
const message = latestMessage(thread)
if (!message) return ''
const rows = [
'---------- Forwarded message ---------',
message.from ? `From: ${message.from}` : null,
message.date ? `Date: ${formatFullDate(message.date)}` : null,
message.subject || thread.subject ? `Subject: ${message.subject || thread.subject}` : null,
message.to ? `To: ${message.to}` : null,
message.cc ? `Cc: ${message.cc}` : null,
].filter((line): line is string => Boolean(line))
const body = (message.body || snippet(message.bodyHtml)).trim()
return [
'<p></p>',
'<blockquote>',
...rows.map((line) => `<p>${escapeHtml(line)}</p>`),
body ? `<p>${escapeHtml(body).replace(/\n/g, '<br />')}</p>` : '',
'</blockquote>',
].join('')
}
const PREFETCH_HOVER_MS = 180
const PREFETCH_MAX_IMAGES_PER_THREAD = 12
@ -373,7 +486,7 @@ function MessageAttachments({ attachments }: { attachments: NonNullable<GmailThr
)
}
type ComposeMode = 'reply' | 'forward'
type ComposeMode = 'reply' | 'replyAll' | 'forward'
function ComposeToolbarButton({
editor,
@ -474,20 +587,110 @@ function ComposeToolbar({ editor, onOpenLink }: { editor: Editor; onOpenLink: ()
)
}
function RecipientField({
label,
value,
onChange,
autoFocus,
trailing,
}: {
label: string
value: string[]
onChange: (next: string[]) => void
autoFocus?: boolean
trailing?: React.ReactNode
}) {
const [draft, setDraft] = useState('')
const inputRef = useRef<HTMLInputElement>(null)
useEffect(() => {
if (autoFocus) inputRef.current?.focus()
}, [autoFocus])
const commit = (raw: string) => {
const additions = splitAddresses(raw)
if (additions.length === 0) return
onChange(dedupeRecipients([...value, ...additions], new Set()))
setDraft('')
}
const onKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
if (event.key === 'Enter' || event.key === ',' || event.key === ';' || (event.key === 'Tab' && draft.trim())) {
if (draft.trim()) {
event.preventDefault()
commit(draft)
}
} else if (event.key === 'Backspace' && !draft && value.length > 0) {
onChange(value.slice(0, -1))
}
}
return (
<div className="gmail-recipient-row">
<span className="gmail-recipient-label">{label}</span>
<div className="gmail-recipient-field">
{value.map((token, index) => (
<span key={`${token}-${index}`} className="gmail-recipient-chip" title={extractAddress(token)}>
<span className="gmail-recipient-chip-label">{recipientLabel(token)}</span>
<button
type="button"
className="gmail-recipient-chip-remove"
aria-label={`Remove ${extractAddress(token)}`}
onMouseDown={(event) => event.preventDefault()}
onClick={() => onChange(value.filter((_, idx) => idx !== index))}
>
×
</button>
</span>
))}
<input
ref={inputRef}
className="gmail-recipient-input"
value={draft}
onChange={(event) => setDraft(event.target.value)}
onKeyDown={onKeyDown}
onBlur={() => { if (draft.trim()) commit(draft) }}
onPaste={(event) => {
const text = event.clipboardData.getData('text')
if (text && /[,;\n]/.test(text)) {
event.preventDefault()
commit(text)
}
}}
/>
</div>
{trailing && <div className="gmail-recipient-trailing">{trailing}</div>}
</div>
)
}
function ComposeBox({
mode,
thread,
selfEmail,
onClose,
}: {
mode: ComposeMode
thread: GmailThread
selfEmail: string
onClose: () => void
}) {
const latest = latestMessage(thread)
const to = mode === 'reply' ? extractAddress(latest?.from) : ''
const initialRecipients = useMemo(
() => buildRecipients(mode, thread, selfEmail),
[mode, thread, selfEmail],
)
const [toList, setToList] = useState<string[]>(initialRecipients.to)
const [ccList, setCcList] = useState<string[]>(initialRecipients.cc)
const [bccList, setBccList] = useState<string[]>([])
const [showCc, setShowCc] = useState<boolean>(initialRecipients.cc.length > 0)
const [showBcc, setShowBcc] = useState<boolean>(false)
const [subject, setSubject] = useState<string>(() => composeSubject(mode, thread.subject))
const modeLabel = mode === 'forward' ? 'Forward' : mode === 'replyAll' ? 'Reply all' : 'Reply'
const initialContent = useMemo(() => {
if (mode !== 'reply') return ''
if (mode === 'forward') return buildForwardedContent(thread)
// Gmail-side draft (user's own work) wins over the AI-generated draft.
const source = thread.gmail_draft || thread.draft_response
if (!source) return ''
@ -495,14 +698,14 @@ function ComposeBox({
.split(/\n{2,}/)
.map((para) => `<p>${escapeHtml(para).replace(/\n/g, '<br />')}</p>`)
.join('')
}, [mode, thread.gmail_draft, thread.draft_response])
}, [mode, thread])
const editor = useEditor({
extensions: [
StarterKit,
StarterKit.configure({ link: false }),
Link.configure({ openOnClick: false, autolink: true }),
Placeholder.configure({
placeholder: mode === 'reply' ? 'Write your reply…' : 'Write a message…',
placeholder: mode === 'forward' ? 'Write a message…' : 'Write your reply…',
}),
],
editorProps: {
@ -554,52 +757,65 @@ function ComposeBox({
if (editor && sel) editor.chain().focus().setTextSelection(sel).run()
}
const [sending, setSending] = useState(false)
const sendInGmail = async () => {
if (!editor) {
window.open(thread.threadUrl, '_blank')
return
}
if (!editor || sending) return
const html = editor.getHTML()
const text = editor.getText().trim()
let copied = false
if (text) {
try {
if (typeof ClipboardItem !== 'undefined' && navigator.clipboard?.write) {
await navigator.clipboard.write([
new ClipboardItem({
'text/html': new Blob([html], { type: 'text/html' }),
'text/plain': new Blob([text], { type: 'text/plain' }),
}),
])
copied = true
} else if (navigator.clipboard?.writeText) {
await navigator.clipboard.writeText(text)
copied = true
}
} catch (err) {
console.warn('[Gmail] clipboard write failed:', err)
}
if (!text) {
toast('Draft is empty.', 'error')
return
}
window.open(thread.threadUrl, '_blank')
if (copied) {
toast('Draft copied — open the reply in Gmail and paste.', 'info')
} else if (text) {
toast('Could not copy draft. Open Gmail and paste manually.', 'error')
if (toList.length === 0) {
toast('Add at least one recipient.', 'error')
return
}
// Build References chain from all known message ids (newest last).
const messageIds = thread.messages
.map((m) => m.messageIdHeader)
.filter((v): v is string => Boolean(v))
const references = messageIds.join(' ')
const inReplyTo = latest?.messageIdHeader
const isForward = mode === 'forward'
setSending(true)
try {
const result = await window.ipc.invoke('gmail:sendReply', {
threadId: isForward ? undefined : thread.threadId,
to: toList.join(', '),
cc: ccList.length ? ccList.join(', ') : undefined,
bcc: bccList.length ? bccList.join(', ') : undefined,
subject: subject.trim() || composeSubject(mode, thread.subject),
bodyHtml: html,
bodyText: text,
inReplyTo: isForward ? undefined : inReplyTo,
references: isForward ? undefined : references || undefined,
})
if (result.error) {
toast(`Send failed: ${result.error}`, 'error')
return
}
toast('Sent.', 'success')
onClose()
} catch (err) {
toast(`Send failed: ${err instanceof Error ? err.message : String(err)}`, 'error')
} finally {
setSending(false)
}
}
const refineWithCopilot = () => {
if (!editor) return
const currentDraft = editor.getText().trim()
const subject = thread.subject || '(No subject)'
const threadSubject = thread.subject || '(No subject)'
const lines: string[] = []
lines.push(`Help me refine this draft email response. **Please ask me how I want to refine it before making any changes** — wait for my answer, then apply the edits.`)
lines.push('')
lines.push(`**Mode:** ${mode === 'reply' ? 'Reply' : 'Forward'}`)
lines.push(`**Subject:** ${subject}`)
lines.push(`**Mode:** ${modeLabel}`)
lines.push(`**Subject:** ${threadSubject}`)
lines.push('')
lines.push(`## Thread (${thread.messages.length} message${thread.messages.length === 1 ? '' : 's'})`)
lines.push('')
@ -624,17 +840,32 @@ function ComposeBox({
return (
<div className="gmail-compose-card">
<div className="gmail-compose-header">
<span>{mode === 'reply' ? 'Reply' : 'Forward'}</span>
<button type="button" onClick={onClose} aria-label="Close compose">x</button>
</div>
<div className="gmail-compose-line">
<span>{mode === 'reply' ? 'To' : 'Recipients'}</span>
<input value={to} placeholder="Recipients" readOnly={mode === 'reply'} />
<span>{modeLabel}</span>
<button type="button" onClick={onClose} aria-label="Close compose">×</button>
</div>
<RecipientField
label="To"
value={toList}
onChange={setToList}
autoFocus={mode === 'forward'}
trailing={
<div className="gmail-recipient-toggles">
{!showCc && <button type="button" onClick={() => setShowCc(true)}>Cc</button>}
{!showBcc && <button type="button" onClick={() => setShowBcc(true)}>Bcc</button>}
</div>
}
/>
{showCc && <RecipientField label="Cc" value={ccList} onChange={setCcList} />}
{showBcc && <RecipientField label="Bcc" value={bccList} onChange={setBccList} />}
{mode === 'forward' && (
<div className="gmail-compose-line">
<span>Subject</span>
<input value={`Fwd: ${thread.subject || '(No subject)'}`} readOnly />
<span className="gmail-compose-label">Subject</span>
<input
className="gmail-compose-subject-input"
value={subject}
onChange={(event) => setSubject(event.target.value)}
placeholder="Subject"
/>
</div>
)}
<EditorContent editor={editor} className="gmail-compose-editor" />
@ -665,10 +896,11 @@ function ComposeBox({
type="button"
className="gmail-send-button"
onClick={() => { void sendInGmail() }}
title="Copy draft and open this thread in Gmail"
disabled={sending}
title="Send this reply via Gmail"
>
<Send size={15} />
Send
{sending ? <LoaderIcon size={15} className="animate-spin" /> : <Send size={15} />}
{sending ? 'Sending…' : 'Send'}
</button>
<button
type="button"
@ -697,10 +929,25 @@ function ThreadDetail({
hidden?: boolean
}) {
const [composeMode, setComposeMode] = useState<ComposeMode | null>(null)
const [selfEmail, setSelfEmail] = useState<string>('')
const [expandedIndices, setExpandedIndices] = useState<Set<number>>(
() => new Set(thread.messages.length > 0 ? [thread.messages.length - 1] : [])
)
// The connected Gmail address, so reply-all can exclude "me".
useEffect(() => {
let cancelled = false
window.ipc.invoke('gmail:getAccountEmail', {})
.then((res) => { if (!cancelled && res?.email) setSelfEmail(res.email) })
.catch(() => {})
return () => { cancelled = true }
}, [])
const canReplyAll = useMemo(() => {
const { to, cc } = buildRecipients('replyAll', thread, selfEmail)
return cc.length > 0 || to.length > 1
}, [thread, selfEmail])
const toggleExpand = useCallback((index: number) => {
setExpandedIndices((prev) => {
const next = new Set(prev)
@ -751,7 +998,10 @@ function ThreadDetail({
</div>
</div>
{isExpanded ? (
<div className="gmail-message-to">to {message.to || 'me'}</div>
<>
<div className="gmail-message-to">to {message.to || 'me'}</div>
{message.cc && <div className="gmail-message-cc">cc {message.cc}</div>}
</>
) : (
<div className="gmail-message-snippet">{snippet(message.body)}</div>
)}
@ -770,6 +1020,12 @@ function ThreadDetail({
<Reply size={16} />
Reply
</button>
{canReplyAll && (
<button type="button" onClick={() => setComposeMode('replyAll')}>
<ReplyAll size={16} />
Reply all
</button>
)}
<button type="button" onClick={() => setComposeMode('forward')}>
<Forward size={16} />
Forward
@ -778,8 +1034,10 @@ function ThreadDetail({
{composeMode && (
<ComposeBox
key={composeMode}
mode={composeMode}
thread={thread}
selfEmail={selfEmail}
onClose={() => setComposeMode(null)}
/>
)}
@ -817,15 +1075,59 @@ function clearLoadingFlag(state: SectionState | null): SectionState {
return { ...state, loadingPage: false }
}
export function EmailView() {
export type EmailViewProps = {
/** If provided, the view opens with this thread already expanded. */
initialThreadId?: string | null
/** Bump to re-focus on the same threadId after navigating away inside the view. */
threadIdVersion?: number
}
export function EmailView({ initialThreadId, threadIdVersion }: EmailViewProps = {}) {
const [important, setImportant] = useState<SectionState>(() => clearLoadingFlag(persistedImportant))
const [other, setOther] = useState<SectionState>(() => clearLoadingFlag(persistedOther))
const hadPersistedDataOnMount = useRef(persistedImportant !== null)
const [selectedThreadId, setSelectedThreadId] = useState<string | null>(null)
const [openedThreadIds, setOpenedThreadIds] = useState<string[]>([])
const [selectedThreadId, setSelectedThreadId] = useState<string | null>(initialThreadId ?? null)
const [openedThreadIds, setOpenedThreadIds] = useState<string[]>(initialThreadId ? [initialThreadId] : [])
useEffect(() => {
setSelectedThreadId(initialThreadId ?? null)
if (initialThreadId) {
setOpenedThreadIds((prev) => {
const without = prev.filter((id) => id !== initialThreadId)
return [...without, initialThreadId].slice(-MAX_KEPT_OPEN)
})
}
}, [initialThreadId, threadIdVersion])
const [refreshing, setRefreshing] = useState(!hadPersistedDataOnMount.current)
const [error, setError] = useState<string | null>(null)
const [query, setQuery] = useState('')
// Gmail sync uses the native Google OAuth connection.
const [emailConnection, setEmailConnection] = useState<GmailConnectionStatus | null>(null)
const [settingsOpen, setSettingsOpen] = useState(false)
useEffect(() => {
let cancelled = false
const check = async () => {
try {
const status = await window.ipc.invoke('gmail:getConnectionStatus', {})
if (!cancelled) setEmailConnection(status)
} catch {
if (!cancelled) {
setEmailConnection({
connected: false,
hasRequiredScope: false,
missingScopes: [],
email: null,
})
}
}
}
void check()
const cleanupOAuthConnect = window.ipc.on('oauth:didConnect', () => { void check() })
return () => {
cancelled = true
cleanupOAuthConnect()
}
}, [])
useEffect(() => { persistedImportant = important }, [important])
useEffect(() => { persistedOther = other }, [other])
@ -835,18 +1137,81 @@ export function EmailView() {
else setOther(updater)
}, [])
const toggleThread = useCallback((threadId: string) => {
const updateThreadInState = useCallback((threadId: string, updater: (t: GmailThread) => GmailThread) => {
const mapSection = (prev: SectionState): SectionState => ({
...prev,
threads: prev.threads.map((t) => (t.threadId === threadId ? updater(t) : t)),
})
setImportant(mapSection)
setOther(mapSection)
}, [])
const removeThreadFromState = useCallback((threadId: string) => {
const filterSection = (prev: SectionState): SectionState => ({
...prev,
threads: prev.threads.filter((t) => t.threadId !== threadId),
})
setImportant(filterSection)
setOther(filterSection)
setSelectedThreadId((current) => (current === threadId ? null : current))
setOpenedThreadIds((prev) => prev.filter((id) => id !== threadId))
}, [])
const markThreadReadAction = useCallback(async (threadId: string) => {
updateThreadInState(threadId, (t) => ({
...t,
unread: false,
messages: t.messages.map((m) => ({ ...m, unread: false })),
}))
try {
const result = await window.ipc.invoke('gmail:markThreadRead', { threadId })
if (!result.ok && result.error) console.warn('[Gmail] mark-read failed:', result.error)
} catch (err) {
console.warn('[Gmail] mark-read failed:', err)
}
}, [updateThreadInState])
const archiveThreadAction = useCallback(async (threadId: string) => {
try {
const result = await window.ipc.invoke('gmail:archiveThread', { threadId })
if (result.ok) {
removeThreadFromState(threadId)
} else if (result.error) {
toast(`Archive failed: ${result.error}`, 'error')
}
} catch (err) {
toast(`Archive failed: ${err instanceof Error ? err.message : String(err)}`, 'error')
}
}, [removeThreadFromState])
const trashThreadAction = useCallback(async (threadId: string) => {
try {
const result = await window.ipc.invoke('gmail:trashThread', { threadId })
if (result.ok) {
removeThreadFromState(threadId)
} else if (result.error) {
toast(`Delete failed: ${result.error}`, 'error')
}
} catch (err) {
toast(`Delete failed: ${err instanceof Error ? err.message : String(err)}`, 'error')
}
}, [removeThreadFromState])
const toggleThread = useCallback((thread: GmailThread) => {
setSelectedThreadId((current) => {
const next = current === threadId ? null : threadId
const next = current === thread.threadId ? null : thread.threadId
if (next) {
setOpenedThreadIds((prev) => {
const without = prev.filter((id) => id !== next)
return [...without, next].slice(-MAX_KEPT_OPEN)
})
if (thread.unread) {
void markThreadReadAction(thread.threadId)
}
}
return next
})
}, [])
}, [markThreadReadAction])
const prefetchedRef = useRef<Set<string>>(new Set())
const hoverTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
@ -1093,29 +1458,69 @@ export function EmailView() {
const hasAny = important.threads.length > 0 || other.threads.length > 0
const initialLoading = !hasAny && refreshing
const needsEmailConnect = emailConnection?.connected === false
const needsEmailReconnect = emailConnection?.connected === true && !emailConnection.hasRequiredScope
const renderRow = (thread: GmailThread) => {
const latest = latestMessage(thread)
const isSelected = thread.threadId === selectedThreadId
const isUnread = thread.unread === true
const isMounted = openedThreadIds.includes(thread.threadId)
const stop = (e: React.MouseEvent | React.KeyboardEvent) => {
e.stopPropagation()
}
return (
<div key={thread.threadId} className="gmail-row-group">
<button
type="button"
className={cn('gmail-row', isSelected && 'gmail-row-selected', isUnread && 'gmail-row-unread')}
onClick={() => toggleThread(thread.threadId)}
<div
className={cn('gmail-row-shell', isSelected && 'gmail-row-shell-selected')}
onMouseEnter={() => scheduleHoverPrefetch(thread)}
onMouseLeave={cancelHoverPrefetch}
>
<span className="gmail-row-dot" aria-hidden />
<span className="gmail-row-sender">{extractName(latest?.from || thread.from)}</span>
<span className="gmail-row-content">
<strong>{thread.summary || thread.subject || '(No subject)'}</strong>
<span>{thread.summary ? thread.subject : snippet(latest?.body || thread.latest_email)}</span>
</span>
<span className="gmail-row-date">{formatInboxTime(latest?.date || thread.date)}</span>
</button>
<button
type="button"
className={cn('gmail-row', isSelected && 'gmail-row-selected', isUnread && 'gmail-row-unread')}
onClick={() => toggleThread(thread)}
>
<span className="gmail-row-dot" aria-hidden />
<span className="gmail-row-sender">{extractName(latest?.from || thread.from)}</span>
<span className="gmail-row-content">
<strong>{thread.summary || thread.subject || '(No subject)'}</strong>
<span>{thread.summary ? thread.subject : snippet(latest?.body || thread.latest_email)}</span>
</span>
<span className="gmail-row-date">{formatInboxTime(latest?.date || thread.date)}</span>
</button>
<div className="gmail-row-actions" onMouseDown={stop} onClick={stop}>
{isUnread && (
<button
type="button"
className="gmail-row-action"
title="Mark as read"
aria-label="Mark as read"
onClick={(e) => { stop(e); void markThreadReadAction(thread.threadId) }}
>
<CheckCheck size={15} />
</button>
)}
<button
type="button"
className="gmail-row-action"
title="Archive"
aria-label="Archive"
onClick={(e) => { stop(e); void archiveThreadAction(thread.threadId) }}
>
<Archive size={15} />
</button>
<button
type="button"
className="gmail-row-action gmail-row-action-danger"
title="Delete"
aria-label="Delete"
onClick={(e) => { stop(e); void trashThreadAction(thread.threadId) }}
>
<Trash2 size={15} />
</button>
</div>
</div>
{isMounted && (
<ThreadDetail
thread={thread}
@ -1185,12 +1590,30 @@ export function EmailView() {
</section>
)}
</div>
) : needsEmailConnect || needsEmailReconnect ? (
<div className="gmail-empty-state flex flex-col items-center gap-3 py-16 text-center">
<Mail size={28} className="opacity-50" />
<p>
{needsEmailReconnect
? 'Reconnect your email to enable Gmail sync and actions.'
: 'Connect your email to see your inbox here.'}
</p>
<button
type="button"
onClick={() => setSettingsOpen(true)}
className="inline-flex items-center gap-1.5 rounded-md border border-border bg-background px-3.5 py-1.5 text-sm font-medium text-foreground transition-colors hover:bg-accent"
>
<Mail size={15} />
{needsEmailReconnect ? 'Reconnect your email' : 'Connect your email'}
</button>
</div>
) : (
<div className="gmail-empty-state">
{initialLoading ? 'Loading Gmail threads…' : 'No Gmail threads in your inbox cache yet.'}
</div>
)}
</div>
<SettingsDialog open={settingsOpen} onOpenChange={setSettingsOpen} defaultTab="connections" />
</div>
)
}

View file

@ -1,100 +0,0 @@
"use client"
import * as React from "react"
import { useState } from "react"
import { MessageCircle } from "lucide-react"
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover"
import {
Tooltip,
TooltipContent,
TooltipTrigger,
} from "@/components/ui/tooltip"
import { Button } from "@/components/ui/button"
interface HelpPopoverProps {
children: React.ReactNode
tooltip?: string
}
export function HelpPopover({ children, tooltip }: HelpPopoverProps) {
const [open, setOpen] = useState(false)
const handleDiscordClick = () => {
window.open("https://discord.com/invite/wajrgmJQ6b", "_blank")
}
return (
<Popover open={open} onOpenChange={setOpen}>
{tooltip ? (
<Tooltip open={open ? false : undefined}>
<TooltipTrigger asChild>
<PopoverTrigger asChild>
{children}
</PopoverTrigger>
</TooltipTrigger>
<TooltipContent side="right" sideOffset={8}>
{tooltip}
</TooltipContent>
</Tooltip>
) : (
<PopoverTrigger asChild>
{children}
</PopoverTrigger>
)}
<PopoverContent
side="right"
align="end"
sideOffset={4}
className="w-80 p-0"
>
<div className="p-4 border-b">
<h4 className="font-semibold text-sm">Help & Support</h4>
<p className="text-xs text-muted-foreground mt-1">
Get help from our community
</p>
</div>
<div className="p-2">
<Button
variant="ghost"
className="w-full justify-start gap-3 h-auto py-3"
onClick={handleDiscordClick}
>
<div className="flex size-8 items-center justify-center rounded-md bg-[#5865F2]">
<MessageCircle className="size-4 text-white" />
</div>
<div className="flex flex-col items-start">
<span className="text-sm font-medium">Join our Discord</span>
<span className="text-xs text-muted-foreground">
Chat with the community
</span>
</div>
</Button>
</div>
<div className="px-4 py-3 border-t flex justify-center gap-3 text-xs text-muted-foreground">
<a
href="https://www.rowboatlabs.com/terms-of-service"
target="_blank"
rel="noopener noreferrer"
className="hover:text-foreground transition-colors"
>
Terms of Service
</a>
<span>·</span>
<a
href="https://www.rowboatlabs.com/privacy-policy"
target="_blank"
rel="noopener noreferrer"
className="hover:text-foreground transition-colors"
>
Privacy Policy
</a>
</div>
</PopoverContent>
</Popover>
)
}

View file

@ -0,0 +1,593 @@
import { useCallback, useEffect, useMemo, useState } from 'react'
import { ArrowRight, Bot, Calendar, Clock, FileText, Mail, MessageSquare, Mic, Plug, Plus, Video } from 'lucide-react'
import { extractConferenceLink } from '@/lib/calendar-event'
import { SettingsDialog } from '@/components/settings-dialog'
interface TreeNode {
path: string
name: string
kind: 'file' | 'dir'
children?: TreeNode[]
stat?: { size: number; mtimeMs: number }
}
type RunItem = { id: string; title?: string; createdAt: string }
type TaskItem = { slug: string; name: string; active: boolean; lastRunAt?: string; lastAttemptAt?: string }
type HomeViewProps = {
tree: TreeNode[]
runs: RunItem[]
bgTaskSummaries: TaskItem[]
onOpenEmail: () => void
onOpenMeetings: () => void
onOpenAgents: () => void
onOpenAgent: (slug: string) => void
onOpenNote: (path: string) => void
onOpenRun: (runId: string) => void
onTakeMeetingNotes: () => void
onOpenChat?: () => void
}
type CalEvent = {
id: string
summary: string
start: Date
end: Date | null
isAllDay: boolean
conferenceLink: string | null
rawStart: { dateTime?: string; date?: string } | undefined
rawEnd: { dateTime?: string; date?: string } | undefined
location: string | null
htmlLink: string | null
source: string
}
type RawCalEvent = {
id?: string
summary?: string
start?: { dateTime?: string; date?: string }
end?: { dateTime?: string; date?: string }
location?: string
htmlLink?: string
status?: string
attendees?: Array<{ self?: boolean; responseStatus?: string }>
}
type EmailThread = { threadId: string; subject: string; from: string }
type ToolkitPreview = { slug: string; logo: string; name: string; description: string }
function greeting(): string {
const h = new Date().getHours()
if (h < 12) return 'Good morning'
if (h < 18) return 'Good afternoon'
return 'Good evening'
}
function todayLabel(): string {
return new Date().toLocaleDateString([], { weekday: 'short', day: 'numeric', month: 'short' })
}
function timeOfDay(d: Date): string {
return d.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })
}
function relativeFromNow(start: Date): string {
const ms = start.getTime() - Date.now()
if (ms <= 0) return 'now'
const min = Math.round(ms / 60000)
if (min < 60) return `in ${min}m`
const hr = Math.round(min / 60)
if (hr < 24) return `in ${hr}h`
return start.toLocaleDateString([], { weekday: 'short' })
}
function relativeAgo(iso?: string): string {
if (!iso) return ''
const t = new Date(iso).getTime()
if (Number.isNaN(t)) return ''
const min = Math.round((Date.now() - t) / 60000)
if (min < 1) return 'just now'
if (min < 60) return `${min}m ago`
const hr = Math.round(min / 60)
if (hr < 24) return `${hr}h ago`
const d = Math.round(hr / 24)
return `${d}d ago`
}
function parseAllDay(s: string): Date | null {
const m = /^(\d{4})-(\d{2})-(\d{2})/.exec(s)
if (!m) return null
return new Date(Number(m[1]), Number(m[2]) - 1, Number(m[3]))
}
function normalizeCalEvent(raw: RawCalEvent, sourcePath: string): CalEvent | null {
if (raw.status === 'cancelled') return null
const declined = raw.attendees?.find((a) => a.self)?.responseStatus === 'declined'
if (declined) return null
const timed = raw.start?.dateTime
const allDay = raw.start?.date
const isAllDay = !timed && Boolean(allDay)
let start: Date | null = null
let end: Date | null = null
if (timed) {
start = new Date(timed)
end = raw.end?.dateTime ? new Date(raw.end.dateTime) : null
} else if (allDay) {
start = parseAllDay(allDay)
end = raw.end?.date ? parseAllDay(raw.end.date) : null
}
if (!start || Number.isNaN(start.getTime())) return null
return {
id: raw.id ?? sourcePath,
summary: raw.summary?.trim() || '(No title)',
start,
end,
isAllDay,
conferenceLink: extractConferenceLink(raw as unknown as Record<string, unknown>) ?? null,
rawStart: raw.start,
rawEnd: raw.end,
location: raw.location?.trim() || null,
htmlLink: raw.htmlLink ?? null,
source: sourcePath,
}
}
function noteLabel(node: TreeNode): string {
if (node.kind === 'file' && node.name.toLowerCase().endsWith('.md')) return node.name.slice(0, -3)
return node.name
}
function triggerMeetingCapture(event: CalEvent, openConference: boolean) {
window.__pendingCalendarEvent = {
summary: event.summary,
start: event.rawStart,
end: event.rawEnd,
location: event.location ?? undefined,
htmlLink: event.htmlLink ?? undefined,
conferenceLink: event.conferenceLink ?? undefined,
source: event.source,
}
if (openConference && event.conferenceLink) {
window.open(event.conferenceLink, '_blank')
}
window.dispatchEvent(new Event('calendar-block:join-meeting'))
}
const CARD = 'rounded-xl border border-border bg-card p-4'
const TOOLKIT_PREVIEW_LIMIT = 8
let cachedToolkitPreviews: ToolkitPreview[] | null = null
let cachedToolkitLogosLoaded = false
function ToolkitPreviewIcon({
toolkit,
onInvalid,
}: {
toolkit: ToolkitPreview
onInvalid: (slug: string) => void
}) {
const [loaded, setLoaded] = useState(false)
if (!loaded) {
return (
<img
src={toolkit.logo}
alt=""
className="hidden"
onLoad={(event) => {
const img = event.currentTarget
if (img.naturalWidth > 1 && img.naturalHeight > 1) {
setLoaded(true)
} else {
onInvalid(toolkit.slug)
}
}}
onError={() => onInvalid(toolkit.slug)}
/>
)
}
return (
<div
title={`${toolkit.name}: ${toolkit.description}`}
aria-label={toolkit.name}
className="flex size-6 shrink-0 items-center justify-center rounded-md border border-border bg-muted/60"
>
<img
src={toolkit.logo}
alt=""
className="size-5 shrink-0 object-contain"
onError={() => onInvalid(toolkit.slug)}
/>
</div>
)
}
export function HomeView({
tree,
runs,
bgTaskSummaries,
onOpenEmail,
onOpenMeetings,
onOpenAgents,
onOpenAgent,
onOpenNote,
onOpenRun,
onTakeMeetingNotes,
onOpenChat,
}: HomeViewProps) {
const [events, setEvents] = useState<CalEvent[]>([])
const [emails, setEmails] = useState<EmailThread[]>([])
const [toolkitPreviews, setToolkitPreviews] = useState<ToolkitPreview[]>(cachedToolkitPreviews ?? [])
const [toolkitLogosLoaded, setToolkitLogosLoaded] = useState(cachedToolkitLogosLoaded)
const [connectionsSettingsOpen, setConnectionsSettingsOpen] = useState(false)
const loadEvents = useCallback(async () => {
try {
const exists = await window.ipc.invoke('workspace:exists', { path: 'calendar_sync' })
if (!exists.exists) { setEvents([]); return }
const entries = await window.ipc.invoke('workspace:readdir', {
path: 'calendar_sync',
opts: { recursive: false, includeHidden: false, includeStats: false },
})
const jsonEntries = entries.filter((e) => e.kind === 'file' && e.name.endsWith('.json'))
const settled = await Promise.allSettled(
jsonEntries.map(async (entry): Promise<CalEvent | null> => {
const result = await window.ipc.invoke('workspace:readFile', { path: entry.path, encoding: 'utf8' })
return normalizeCalEvent(JSON.parse(result.data) as RawCalEvent, entry.path)
}),
)
const out: CalEvent[] = []
for (const r of settled) if (r.status === 'fulfilled' && r.value) out.push(r.value)
out.sort((a, b) => a.start.getTime() - b.start.getTime())
setEvents(out)
} catch (err) {
console.error('Home: failed to load events', err)
}
}, [])
const loadEmails = useCallback(async () => {
try {
const result = await window.ipc.invoke('gmail:getImportant', { limit: 25 })
setEmails(
result.threads
.filter((t) => t.unread === true)
.slice(0, 3)
.map((t) => ({ threadId: t.threadId, subject: t.subject ?? '(No subject)', from: t.from ?? '' })),
)
} catch (err) {
console.error('Home: failed to load emails', err)
}
}, [])
const loadConnectorLogos = useCallback(async () => {
if (cachedToolkitLogosLoaded) return
try {
const configured = await window.ipc.invoke('composio:is-configured', null)
if (!configured.configured) return
const toolkits = await window.ipc.invoke('composio:list-toolkits', {})
const previews = toolkits.items
.filter((toolkit) => Boolean(toolkit.meta.logo))
.slice(0, TOOLKIT_PREVIEW_LIMIT)
.map((toolkit) => ({
slug: toolkit.slug,
logo: toolkit.meta.logo,
name: toolkit.name,
description: toolkit.meta.description,
}))
cachedToolkitPreviews = previews
setToolkitPreviews(previews)
} catch {
cachedToolkitPreviews = []
} finally {
cachedToolkitLogosLoaded = true
setToolkitLogosLoaded(true)
}
}, [])
const removeToolkitPreview = useCallback((slug: string) => {
setToolkitPreviews((prev) => {
const next = prev.filter((toolkit) => toolkit.slug !== slug)
cachedToolkitPreviews = next
return next
})
}, [])
useEffect(() => { void loadEvents(); void loadEmails(); void loadConnectorLogos() }, [loadEvents, loadEmails, loadConnectorLogos])
// Upcoming (not-yet-ended) events, soonest first.
const upcoming = useMemo(() => {
const now = Date.now()
return events.filter((e) => {
const end = e.end ?? (e.isAllDay ? new Date(e.start.getTime() + 864e5) : e.start)
return end.getTime() > now
})
}, [events])
const nextEvent = upcoming[0]
const todaysEvents = useMemo(() => {
const now = new Date()
return upcoming.filter((e) =>
e.start.getFullYear() === now.getFullYear() &&
e.start.getMonth() === now.getMonth() &&
e.start.getDate() === now.getDate(),
)
}, [upcoming])
const activeAgents = useMemo(() => bgTaskSummaries.filter((t) => t.active), [bgTaskSummaries])
const recentAgent = useMemo(() => {
const t = (s?: string) => (s ? new Date(s).getTime() || 0 : 0)
return [...bgTaskSummaries].sort((a, b) =>
Math.max(t(b.lastRunAt), t(b.lastAttemptAt)) - Math.max(t(a.lastRunAt), t(a.lastAttemptAt)),
)[0]
}, [bgTaskSummaries])
const recentNotes = useMemo<TreeNode[]>(() => {
const out: TreeNode[] = []
const walk = (nodes: TreeNode[]) => {
for (const n of nodes) {
if (n.path === 'knowledge/Meetings' || n.path === 'knowledge/Workspace') continue
if (n.kind === 'file') out.push(n)
else if (n.children?.length) walk(n.children)
}
}
walk(tree)
return out
.filter((n) => n.stat?.mtimeMs)
.sort((a, b) => (b.stat?.mtimeMs ?? 0) - (a.stat?.mtimeMs ?? 0))
.slice(0, 2)
}, [tree])
const recentActivity = useMemo(() => {
const items: Array<{ key: string; icon: 'note' | 'chat'; label: string; kind: string; when: number; open: () => void }> = []
for (const n of recentNotes) {
items.push({ key: `n:${n.path}`, icon: 'note', label: noteLabel(n), kind: 'note', when: n.stat?.mtimeMs ?? 0, open: () => onOpenNote(n.path) })
}
for (const r of runs.slice(0, 4)) {
items.push({ key: `r:${r.id}`, icon: 'chat', label: r.title || '(Untitled chat)', kind: 'chat', when: new Date(r.createdAt).getTime() || 0, open: () => onOpenRun(r.id) })
}
return items.sort((a, b) => b.when - a.when).slice(0, 4)
}, [recentNotes, runs, onOpenNote, onOpenRun])
return (
<div className="flex h-full flex-col overflow-hidden bg-muted/30">
<div className="flex-1 overflow-y-auto px-9 py-7">
<div className="mx-auto flex max-w-[760px] flex-col gap-[18px]">
{/* Greeting */}
<div className="flex items-baseline gap-3">
<h1 className="text-[28px] font-semibold tracking-tight">{greeting()}</h1>
<span className="text-sm text-muted-foreground">{todayLabel()}</span>
</div>
{/* Up-next hero */}
{nextEvent && (
<div className="flex items-center gap-[18px] rounded-xl bg-foreground p-[18px] text-background">
<div className="flex size-[52px] shrink-0 items-center justify-center rounded-xl bg-background/10">
<Mic className="size-[22px]" />
</div>
<div className="min-w-0 flex-1">
<div className="mb-1 text-[11px] uppercase tracking-wide text-background/55">
Up next · {nextEvent.isAllDay ? 'today' : relativeFromNow(nextEvent.start)}
</div>
<div className="mb-0.5 truncate text-[17px] font-medium">{nextEvent.summary}</div>
<div className="truncate text-[13px] text-background/70">
{nextEvent.isAllDay ? 'All day' : `${timeOfDay(nextEvent.start)}${nextEvent.end ? ` ${timeOfDay(nextEvent.end)}` : ''}`}
{nextEvent.location ? ` · ${nextEvent.location}` : ''}
</div>
</div>
<div className="flex shrink-0 gap-2">
<button
type="button"
onClick={onTakeMeetingNotes}
className="rounded-md bg-background px-3.5 py-2 text-[13px] font-medium text-foreground"
>
Take notes
</button>
{nextEvent.conferenceLink && (
<button
type="button"
onClick={() => window.open(nextEvent.conferenceLink!, '_blank')}
className="rounded-md border border-background/20 px-3 py-2 text-background"
aria-label="Join meeting"
>
<Video className="size-[13px]" />
</button>
)}
</div>
</div>
)}
{/* Inbox + Background agents */}
<div className="grid grid-cols-2 gap-[18px]">
<div className={CARD}>
<div className="mb-3 flex items-center gap-2">
<Mail className="size-[15px]" />
<span className="text-sm font-medium">Inbox</span>
{emails.length > 0 && (
<span className="rounded-lg bg-destructive px-1.5 py-px text-[10.5px] font-semibold uppercase tracking-wide text-white">
{emails.length} new
</span>
)}
<span className="flex-1" />
<button type="button" onClick={onOpenEmail} className="text-xs text-primary hover:underline">Open </button>
</div>
{emails.length === 0 ? (
<div className="py-1 text-[12.5px] text-muted-foreground">No unread important email.</div>
) : emails.map((e, i) => (
<button
key={e.threadId}
type="button"
onClick={onOpenEmail}
className={`flex w-full gap-2.5 py-[7px] text-left text-[12.5px] ${i ? 'border-t border-border' : ''}`}
>
<span className="w-[92px] shrink-0 truncate text-muted-foreground">{formatFrom(e.from)}</span>
<span className="flex-1 truncate">{e.subject}</span>
</button>
))}
</div>
<div className={CARD}>
<div className="mb-3 flex items-center gap-2">
<Bot className="size-[15px]" />
<span className="text-sm font-medium">Background agents</span>
<span className="flex-1" />
<span className="text-xs text-muted-foreground">{activeAgents.length} active</span>
<button type="button" onClick={onOpenAgents} className="text-xs text-primary hover:underline">Open </button>
</div>
{recentAgent ? (
<button
type="button"
onClick={() => onOpenAgent(recentAgent.slug)}
className="flex w-full items-center gap-2.5 py-[7px] text-left text-[13px]"
>
<span className={`size-2 shrink-0 rounded-full ${recentAgent.active ? 'bg-emerald-500' : 'bg-muted-foreground'}`} />
<span className="flex-1 truncate font-medium">{recentAgent.name}</span>
<span className="text-[11.5px] text-muted-foreground">{relativeAgo(recentAgent.lastRunAt) || '—'}</span>
</button>
) : (
<div className="py-1 text-[12.5px] text-muted-foreground">No agents yet.</div>
)}
<button
type="button"
onClick={onOpenAgents}
className="mt-3.5 flex items-center gap-2 border-t border-border pt-3 text-[12.5px] text-primary"
>
<Plus className="size-3" />
Create an agent
</button>
</div>
</div>
{/* Today's schedule */}
<div className={CARD}>
<div className="mb-3.5 flex items-center gap-2">
<Calendar className="size-[14px]" />
<span className="text-sm font-medium">Today's schedule</span>
<span className="flex-1" />
<button type="button" onClick={onOpenMeetings} className="text-xs text-primary hover:underline">All meetings </button>
</div>
{todaysEvents.length === 0 ? (
<div className="py-1 text-[13px] italic text-muted-foreground">No more events today.</div>
) : todaysEvents.map((e, i) => (
<div key={e.id} className={`group flex items-center gap-3.5 py-2 text-[13px] ${i ? 'border-t border-border' : ''}`}>
<span className="w-[90px] shrink-0 font-mono text-[11.5px] text-muted-foreground">
{e.isAllDay ? 'All day' : `${timeOfDay(e.start)}${e.end ? ` ${timeOfDay(e.end)}` : ''}`}
</span>
<span className={`size-2 shrink-0 rounded-full ${i === 0 ? 'bg-emerald-500' : 'bg-border'}`} />
<span className="min-w-0 flex-1 truncate font-medium">{e.summary}</span>
<div className="flex shrink-0 items-center gap-1 opacity-0 transition-opacity group-hover:opacity-100 focus-within:opacity-100">
<button
type="button"
onClick={() => triggerMeetingCapture(e, false)}
className="inline-flex items-center gap-1 rounded-md border border-border px-2 py-1 text-[11.5px] text-foreground transition-colors hover:bg-accent"
>
<Mic className="size-3" />
Take notes
</button>
{e.conferenceLink && (
<button
type="button"
onClick={() => triggerMeetingCapture(e, true)}
className="inline-flex items-center gap-1 rounded-md border border-border px-2 py-1 text-[11.5px] text-foreground transition-colors hover:bg-accent"
>
<Video className="size-3" />
Join &amp; take notes
</button>
)}
</div>
</div>
))}
</div>
{/* Recent activity */}
{recentActivity.length > 0 && (
<div className={CARD}>
<div className="mb-3 flex items-center gap-2">
<Clock className="size-[14px]" />
<span className="text-sm font-medium">Recent activity</span>
</div>
{recentActivity.map((a, i) => (
<button
key={a.key}
type="button"
onClick={a.open}
className={`flex w-full items-center gap-3 py-2 text-left text-[13px] ${i ? 'border-t border-border' : ''}`}
>
{a.icon === 'note' ? <FileText className="size-[13px] shrink-0 text-muted-foreground" /> : <MessageSquare className="size-[13px] shrink-0 text-muted-foreground" />}
<span className="flex-1 truncate">{a.label}</span>
<span className="w-[60px] text-right text-[11px] text-muted-foreground">{a.kind}</span>
</button>
))}
</div>
)}
{/* Tool connections */}
<div className={CARD}>
<div className="flex items-start gap-3">
<div className="flex size-8 shrink-0 items-center justify-center rounded-lg border border-border bg-muted text-muted-foreground">
<Plug className="size-[14px]" />
</div>
<div className="min-w-0 flex-1">
<div className="text-[13.5px] leading-snug">
<span className="font-medium">Connect your tools.</span>
<span className="text-muted-foreground"> Bring context from the apps you already use.</span>
</div>
<div className="mt-3 flex min-h-5 flex-wrap items-center gap-1.5">
{toolkitLogosLoaded && toolkitPreviews.map((toolkit) => (
<ToolkitPreviewIcon
key={toolkit.slug}
toolkit={toolkit}
onInvalid={removeToolkitPreview}
/>
))}
<button
type="button"
onClick={() => setConnectionsSettingsOpen(true)}
className="ml-1 flex h-5 shrink-0 items-center gap-1 rounded-md px-1 text-[12px] font-medium text-primary hover:underline"
>
Connections
<ArrowRight className="size-3" />
</button>
</div>
</div>
</div>
</div>
<SettingsDialog
defaultTab="connections"
open={connectionsSettingsOpen}
onOpenChange={setConnectionsSettingsOpen}
/>
{/* Open chat CTA */}
{onOpenChat && (
<button
type="button"
onClick={onOpenChat}
className="flex items-center gap-3.5 rounded-xl border border-border bg-card p-4 text-left transition-colors hover:bg-accent"
>
<div className="flex size-9 shrink-0 items-center justify-center rounded-lg border border-border bg-card text-muted-foreground">
<MessageSquare className="size-[15px]" />
</div>
<div className="min-w-0 flex-1 text-[13.5px] leading-snug">
<span className="font-medium">Ask anything</span>
<span className="text-muted-foreground"> create presentations, do research, collaborate on docs.</span>
</div>
<span className="flex shrink-0 items-center gap-1 text-[12.5px] font-medium text-primary">
New chat
<ArrowRight className="size-3.5" />
</span>
</button>
)}
</div>
</div>
</div>
)
}
function formatFrom(from: string): string {
const m = /^\s*"?([^"<]+?)"?\s*<.+>\s*$/.exec(from)
return (m ? m[1] : from).trim()
}

View file

@ -1,33 +1,11 @@
import { useEffect, useState } from 'react'
import { useEffect, useMemo, useState } from 'react'
import { AlertCircleIcon, ExternalLinkIcon, FileTextIcon, Loader2Icon } from 'lucide-react'
const MAX_SIZE_BYTES = 5 * 1024 * 1024
const CACHE_MAX_ENTRIES = 20
type CacheEntry = { html: string; mtimeMs: number; size: number }
const htmlCache = new Map<string, CacheEntry>()
function getCached(path: string, mtimeMs: number, size: number): string | null {
const entry = htmlCache.get(path)
if (!entry || entry.mtimeMs !== mtimeMs || entry.size !== size) return null
// Refresh LRU position
htmlCache.delete(path)
htmlCache.set(path, entry)
return entry.html
}
function setCached(path: string, html: string, mtimeMs: number, size: number) {
htmlCache.set(path, { html, mtimeMs, size })
while (htmlCache.size > CACHE_MAX_ENTRIES) {
const oldest = htmlCache.keys().next().value
if (oldest === undefined) break
htmlCache.delete(oldest)
}
}
type ViewerState =
| { kind: 'loading' }
| { kind: 'loaded'; html: string }
| { kind: 'loaded' }
| { kind: 'empty' }
| { kind: 'tooLarge'; sizeMB: number }
| { kind: 'error'; message: string }
@ -36,9 +14,15 @@ interface HtmlFileViewerProps {
path: string
}
function toAppWorkspaceUrl(path: string): string {
const segments = path.split('/').filter(Boolean).map((seg) => encodeURIComponent(seg))
return `app://workspace/${segments.join('/')}`
}
export function HtmlFileViewer({ path }: HtmlFileViewerProps) {
const [state, setState] = useState<ViewerState>({ kind: 'loading' })
const [iframeLoaded, setIframeLoaded] = useState(false)
const iframeSrc = useMemo(() => toAppWorkspaceUrl(path), [path])
useEffect(() => {
let cancelled = false
@ -57,19 +41,11 @@ export function HtmlFileViewer({ path }: HtmlFileViewerProps) {
setState({ kind: 'tooLarge', sizeMB: stat.size / (1024 * 1024) })
return
}
const cachedHtml = getCached(path, stat.mtimeMs, stat.size)
if (cachedHtml !== null) {
setState(cachedHtml.trim() === '' ? { kind: 'empty' } : { kind: 'loaded', html: cachedHtml })
return
}
const result = await window.ipc.invoke('workspace:readFile', { path })
if (cancelled) return
setCached(path, result.data, stat.mtimeMs, stat.size)
if (!result.data || result.data.trim() === '') {
if (stat.size === 0) {
setState({ kind: 'empty' })
return
}
setState({ kind: 'loaded', html: result.data })
setState({ kind: 'loaded' })
} catch (err) {
if (cancelled) return
const message = err instanceof Error ? err.message : String(err)
@ -124,20 +100,16 @@ export function HtmlFileViewer({ path }: HtmlFileViewerProps) {
)
}
// We use `srcDoc` here (not `src=app://workspace/<path>`) so the iframe
// gets a null origin with no base URL. Trade-off: relative assets inside
// the file — `<link href="./style.css">`, `<img src="./pic.png">`,
// `<script src="./foo.js">` — will silently 404. Self-contained HTML
// works fine; HTML that ships next to sibling assets will look broken.
// TODO: switch to `src=app://workspace/<path>` if we want relative-asset
// support; that path also resolves through the existing path-traversal
// guard in resolveWorkspacePath.
// Serve via the `app://workspace/<rel-path>` protocol so the iframe has a
// proper base URL — relative `<link>`, `<img>`, `<script>` references next
// to the file resolve correctly (the path-traversal guard in
// resolveWorkspacePath already gates the protocol handler).
return (
<div className="relative h-full w-full">
{state.kind === 'loaded' && (
<iframe
key={path}
srcDoc={state.html}
src={iframeSrc}
sandbox="allow-scripts"
className="h-full w-full border-0 bg-white"
title="HTML preview"

View file

@ -0,0 +1,803 @@
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import {
ArrowLeft,
ChevronRight,
Copy,
ExternalLink,
FilePlus,
FileText,
FolderOpen,
FolderPlus,
Network,
Pencil,
SearchIcon,
Table2,
Trash2,
} from 'lucide-react'
import {
ContextMenu,
ContextMenuContent,
ContextMenuItem,
ContextMenuSeparator,
ContextMenuTrigger,
} from '@/components/ui/context-menu'
import { Input } from '@/components/ui/input'
import { VoiceNoteButton } from '@/components/sidebar-content'
import { formatRelativeTime } from '@/lib/relative-time'
import { toast } from '@/lib/toast'
import { cn } from '@/lib/utils'
interface TreeNode {
path: string
name: string
kind: 'file' | 'dir'
children?: TreeNode[]
stat?: { size: number; mtimeMs: number }
}
export type KnowledgeViewActions = {
createNote: (parentPath?: string) => void
createFolder: (parentPath?: string) => Promise<string>
rename: (path: string, newName: string, isDir: boolean) => Promise<void>
remove: (path: string) => Promise<void>
copyPath: (path: string) => void
revealInFileManager: (path: string, isDir: boolean) => void
onOpenInNewTab?: (path: string) => void
}
type KnowledgeViewProps = {
tree: TreeNode[]
actions: KnowledgeViewActions
// Folder currently being browsed (null = root overview). Controlled by the
// app so drill-down participates in the global back/forward history.
folderPath: string | null
onNavigateFolder: (path: string | null) => void
onOpenNote: (path: string) => void
onOpenGraph: () => void
onOpenSearch: () => void
onOpenBases: () => void
onVoiceNoteCreated?: (path: string) => void
}
// Folders that have their own dedicated destinations elsewhere in the app.
const HIDDEN_PATHS = new Set(['knowledge/Meetings', 'knowledge/Workspace'])
// Theme-aware accent palette for folder avatars — colored letter on a faint
// tint of the same hue. Mirrors the design's six-colour rotation.
const AVATAR_PALETTE = [
'bg-indigo-500/10 text-indigo-600 dark:text-indigo-400',
'bg-violet-500/10 text-violet-600 dark:text-violet-400',
'bg-amber-500/10 text-amber-600 dark:text-amber-400',
'bg-rose-500/10 text-rose-600 dark:text-rose-400',
'bg-emerald-500/10 text-emerald-600 dark:text-emerald-400',
'bg-sky-500/10 text-sky-600 dark:text-sky-400',
] as const
function avatarClass(name: string): string {
let hash = 0
for (let i = 0; i < name.length; i++) hash = (hash * 31 + name.charCodeAt(i)) >>> 0
return AVATAR_PALETTE[hash % AVATAR_PALETTE.length]
}
function isMarkdown(node: TreeNode): boolean {
return node.kind === 'file' && node.name.toLowerCase().endsWith('.md')
}
// All markdown notes within a node (recurses into subfolders).
function collectNotes(node: TreeNode): TreeNode[] {
if (node.kind === 'file') return isMarkdown(node) ? [node] : []
const out: TreeNode[] = []
for (const child of node.children ?? []) out.push(...collectNotes(child))
return out
}
function recentNotes(node: TreeNode, limit: number): TreeNode[] {
return collectNotes(node)
.sort((a, b) => (b.stat?.mtimeMs ?? 0) - (a.stat?.mtimeMs ?? 0))
.slice(0, limit)
}
function latestMtime(node: TreeNode): number {
let max = node.stat?.mtimeMs ?? 0
for (const child of node.children ?? []) max = Math.max(max, latestMtime(child))
return max
}
function sortNodes(nodes: TreeNode[]): TreeNode[] {
return [...nodes].sort((a, b) => {
if (a.kind !== b.kind) return a.kind === 'dir' ? -1 : 1
return a.name.localeCompare(b.name)
})
}
function findNode(nodes: TreeNode[], path: string): TreeNode | null {
for (const node of nodes) {
if (node.path === path) return node
if (node.children) {
const found = findNode(node.children, path)
if (found) return found
}
}
return null
}
function formatModified(mtimeMs?: number): string {
if (!mtimeMs) return ''
const rel = formatRelativeTime(new Date(mtimeMs).toISOString())
if (!rel || rel === 'just now') return rel
return `${rel} ago`
}
function getFileManagerName(): string {
if (typeof navigator === 'undefined') return 'File Manager'
const platform = navigator.platform.toLowerCase()
if (platform.includes('mac')) return 'Finder'
if (platform.includes('win')) return 'Explorer'
return 'File Manager'
}
function displayName(node: TreeNode): string {
if (isMarkdown(node)) return node.name.slice(0, -3)
return node.name
}
export function KnowledgeView({
tree,
actions,
folderPath,
onNavigateFolder,
onOpenNote,
onOpenGraph,
onOpenSearch,
onOpenBases,
onVoiceNoteCreated,
}: KnowledgeViewProps) {
const [renameTarget, setRenameTarget] = useState<string | null>(null)
const topLevel = useMemo(
() => tree.filter((n) => !HIDDEN_PATHS.has(n.path)),
[tree],
)
const folders = useMemo(
() => sortNodes(topLevel.filter((n) => n.kind === 'dir')),
[topLevel],
)
const looseNotes = useMemo(
() => sortNodes(topLevel.filter((n) => isMarkdown(n))),
[topLevel],
)
const totalNotes = useMemo(
() => topLevel.reduce((sum, n) => sum + collectNotes(n).length, 0),
[topLevel],
)
const openFolder = useCallback((path: string) => onNavigateFolder(path), [onNavigateFolder])
// When the open folder no longer exists (deleted/renamed externally), fall
// back to the root overview rather than holding a dangling drill-down.
const currentFolder = folderPath ? findNode(tree, folderPath) : null
return (
<div className="flex h-full flex-col overflow-hidden">
<div className="shrink-0 flex items-start justify-between gap-4 border-b border-border px-8 py-6">
<div className="min-w-0">
<h1 className="text-2xl font-bold tracking-tight">Notes</h1>
<p className="mt-1 text-sm text-muted-foreground">
{totalNotes} {totalNotes === 1 ? 'note' : 'notes'} across {folders.length}{' '}
{folders.length === 1 ? 'folder' : 'folders'}
</p>
</div>
<div className="flex shrink-0 items-center gap-2">
<VoiceNoteButton onNoteCreated={onVoiceNoteCreated} />
<SecondaryButton icon={SearchIcon} label="Search" onClick={onOpenSearch} />
<SecondaryButton icon={Network} label="Graph" onClick={onOpenGraph} />
<button
type="button"
onClick={() => actions.createNote(currentFolder?.path)}
className="inline-flex items-center gap-1.5 rounded-lg bg-primary px-3 py-1.5 text-sm font-medium text-primary-foreground transition-colors hover:bg-primary/90"
>
<FilePlus className="size-4" />
<span>New note</span>
</button>
</div>
</div>
<div className="flex-1 overflow-y-auto">
<div className="mx-auto w-full max-w-3xl px-8 py-6">
{currentFolder ? (
<FolderDetail
folder={currentFolder}
actions={actions}
renameTarget={renameTarget}
onRequestRename={setRenameTarget}
onClearRename={() => setRenameTarget(null)}
onNavigate={onNavigateFolder}
onOpenFolder={openFolder}
onOpenNote={onOpenNote}
/>
) : (
<>
<SectionHeader label={`Folders · ${folders.length}`} aside="Sorted by name" />
{folders.length === 0 ? (
<EmptyState text="No folders yet." />
) : (
<div className="overflow-hidden rounded-xl border border-border">
{folders.map((node, i) => (
<div key={node.path} className={cn(i > 0 && 'border-t border-border/60')}>
<FolderCard
node={node}
actions={actions}
renameTarget={renameTarget}
onRequestRename={setRenameTarget}
onClearRename={() => setRenameTarget(null)}
onOpenFolder={openFolder}
onOpenNote={onOpenNote}
/>
</div>
))}
</div>
)}
{looseNotes.length > 0 && (
<div className="mt-8">
<SectionHeader label={`Loose notes · ${looseNotes.length}`} />
<div className="overflow-hidden rounded-xl border border-border">
{looseNotes.map((node, i) => (
<div key={node.path} className={cn(i > 0 && 'border-t border-border/60')}>
<ItemRow
node={node}
actions={actions}
renameTarget={renameTarget}
onRequestRename={setRenameTarget}
onClearRename={() => setRenameTarget(null)}
onOpenFolder={openFolder}
onOpenNote={onOpenNote}
/>
</div>
))}
</div>
</div>
)}
</>
)}
<QuickActions
actions={actions}
currentFolder={currentFolder}
onOpenBases={onOpenBases}
onFolderCreated={setRenameTarget}
/>
</div>
</div>
</div>
)
}
function QuickActions({
actions,
currentFolder,
onOpenBases,
onFolderCreated,
}: {
actions: KnowledgeViewActions
currentFolder: TreeNode | null
onOpenBases: () => void
onFolderCreated: (path: string) => void
}) {
// Inside a folder these target that folder; at the root they target knowledge/.
const parent = currentFolder?.path
return (
<div className="mt-8">
<SectionHeader label="Quick actions" />
<div className="flex flex-wrap gap-2">
<QuickAction icon={FilePlus} label="New note" onClick={() => actions.createNote(parent)} />
<QuickAction
icon={FolderPlus}
label="New folder"
onClick={async () => {
try {
const path = await actions.createFolder(parent)
onFolderCreated(path)
} catch { /* ignore */ }
}}
/>
<QuickAction icon={Table2} label="Open as base" onClick={onOpenBases} />
<QuickAction
icon={FolderOpen}
label={`Reveal in ${getFileManagerName()}`}
onClick={() => actions.revealInFileManager(parent ?? 'knowledge', true)}
/>
</div>
</div>
)
}
function SecondaryButton({
icon: Icon,
label,
onClick,
}: {
icon: typeof SearchIcon
label: string
onClick: () => void
}) {
return (
<button
type="button"
onClick={onClick}
className="inline-flex items-center gap-1.5 rounded-lg border border-border bg-background px-3 py-1.5 text-sm text-foreground transition-colors hover:bg-accent"
>
<Icon className="size-4" />
<span>{label}</span>
</button>
)
}
function QuickAction({
icon: Icon,
label,
onClick,
}: {
icon: typeof FilePlus
label: string
onClick: () => void
}) {
return (
<button
type="button"
onClick={onClick}
className="inline-flex items-center gap-2 rounded-lg border border-border bg-background px-3 py-2 text-sm text-foreground transition-colors hover:bg-accent"
>
<Icon className="size-4 text-muted-foreground" />
<span>{label}</span>
</button>
)
}
function SectionHeader({ label, aside }: { label: string; aside?: string }) {
return (
<div className="mb-2.5 flex items-center justify-between">
<span className="text-[11px] font-semibold uppercase tracking-wider text-muted-foreground">
{label}
</span>
{aside && <span className="text-xs text-muted-foreground">{aside}</span>}
</div>
)
}
function EmptyState({ text }: { text: string }) {
return (
<div className="rounded-xl border border-dashed border-border px-6 py-10 text-center text-sm text-muted-foreground">
{text}
</div>
)
}
function FolderAvatar({ name, className }: { name: string; className?: string }) {
return (
<div
className={cn(
'flex size-8 shrink-0 items-center justify-center rounded-md text-[13px] font-bold',
avatarClass(name),
className,
)}
>
{name.charAt(0).toUpperCase() || '?'}
</div>
)
}
function FolderCard({
node,
actions,
renameTarget,
onRequestRename,
onClearRename,
onOpenFolder,
onOpenNote,
}: {
node: TreeNode
actions: KnowledgeViewActions
renameTarget: string | null
onRequestRename: (path: string) => void
onClearRename: () => void
onOpenFolder: (path: string) => void
onOpenNote: (path: string) => void
}) {
const count = useMemo(() => collectNotes(node).length, [node])
const peek = useMemo(() => recentNotes(node, 3), [node])
const modified = formatModified(latestMtime(node))
const renameActive = renameTarget === node.path
const card = (
<div
role="button"
tabIndex={0}
onClick={() => onOpenFolder(node.path)}
onKeyDown={(e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault()
onOpenFolder(node.path)
}
}}
className="group flex w-full cursor-pointer items-start gap-3 px-4 py-3 text-left transition-colors hover:bg-accent/50"
>
<FolderAvatar name={node.name} className="mt-0.5" />
<div className="min-w-0 flex-1">
{renameActive ? (
<RenameField
initial={node.name}
isDir
path={node.path}
actions={actions}
onDone={onClearRename}
/>
) : (
<span className="block truncate text-sm font-semibold text-foreground">
{node.name}
</span>
)}
<div className="mt-0.5 text-xs text-muted-foreground">
{count} {count === 1 ? 'note' : 'notes'}
</div>
{peek.length > 0 && (
<div className="mt-2 flex flex-wrap gap-1.5">
{peek.map((n) => (
<button
key={n.path}
type="button"
onClick={(e) => {
e.stopPropagation()
onOpenNote(n.path)
}}
className="max-w-[200px] truncate rounded-full border border-border/60 bg-muted px-2.5 py-0.5 text-xs text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"
>
{displayName(n)}
</button>
))}
</div>
)}
</div>
<div className="flex shrink-0 items-center gap-2 pt-1">
<span className="text-xs text-muted-foreground tabular-nums whitespace-nowrap">
{modified}
</span>
<ChevronRight className="size-4 text-muted-foreground/40 opacity-0 transition-opacity group-hover:opacity-100" />
</div>
</div>
)
return (
<RowContextMenu node={node} actions={actions} onRequestRename={onRequestRename}>
{card}
</RowContextMenu>
)
}
function FolderDetail({
folder,
actions,
renameTarget,
onRequestRename,
onClearRename,
onNavigate,
onOpenFolder,
onOpenNote,
}: {
folder: TreeNode
actions: KnowledgeViewActions
renameTarget: string | null
onRequestRename: (path: string) => void
onClearRename: () => void
onNavigate: (path: string | null) => void
onOpenFolder: (path: string) => void
onOpenNote: (path: string) => void
}) {
const items = useMemo(() => sortNodes(folder.children ?? []), [folder])
// Breadcrumb segments from "knowledge/A/B" → [{ name: 'A', path }, ...].
const crumbs = useMemo(() => {
const rel = folder.path.startsWith('knowledge/')
? folder.path.slice('knowledge/'.length)
: folder.path
const parts = rel.split('/').filter(Boolean)
const out: { name: string; path: string }[] = []
let acc = 'knowledge'
for (const part of parts) {
acc = `${acc}/${part}`
out.push({ name: part, path: acc })
}
return out
}, [folder.path])
return (
<>
<div className="mb-4 flex min-w-0 items-center gap-1.5 text-sm">
<button
type="button"
onClick={() => {
const parent = crumbs.length >= 2 ? crumbs[crumbs.length - 2].path : null
onNavigate(parent)
}}
className="inline-flex items-center gap-1 rounded-md px-1.5 py-1 text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"
aria-label="Back"
>
<ArrowLeft className="size-4" />
</button>
<button
type="button"
onClick={() => onNavigate(null)}
className="rounded-md px-1.5 py-1 text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"
>
Notes
</button>
{crumbs.map((c, i) => (
<span key={c.path} className="flex min-w-0 items-center gap-1.5">
<ChevronRight className="size-3.5 shrink-0 text-muted-foreground/50" />
{i === crumbs.length - 1 ? (
<span className="truncate font-medium text-foreground">{c.name}</span>
) : (
<button
type="button"
onClick={() => onNavigate(c.path)}
className="truncate rounded-md px-1.5 py-1 text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"
>
{c.name}
</button>
)}
</span>
))}
</div>
<SectionHeader label={`${items.length} ${items.length === 1 ? 'item' : 'items'}`} />
{items.length === 0 ? (
<EmptyState text="This folder is empty." />
) : (
<div className="overflow-hidden rounded-xl border border-border">
{items.map((node, i) => (
<div key={node.path} className={cn(i > 0 && 'border-t border-border/60')}>
<ItemRow
node={node}
actions={actions}
renameTarget={renameTarget}
onRequestRename={onRequestRename}
onClearRename={onClearRename}
onOpenFolder={onOpenFolder}
onOpenNote={onOpenNote}
/>
</div>
))}
</div>
)}
</>
)
}
function ItemRow({
node,
actions,
renameTarget,
onRequestRename,
onClearRename,
onOpenFolder,
onOpenNote,
}: {
node: TreeNode
actions: KnowledgeViewActions
renameTarget: string | null
onRequestRename: (path: string) => void
onClearRename: () => void
onOpenFolder: (path: string) => void
onOpenNote: (path: string) => void
}) {
const isDir = node.kind === 'dir'
const renameActive = renameTarget === node.path
const modified = formatModified(isDir ? latestMtime(node) : node.stat?.mtimeMs)
const count = useMemo(() => (isDir ? collectNotes(node).length : 0), [isDir, node])
const handleOpen = useCallback(() => {
if (isDir) onOpenFolder(node.path)
else onOpenNote(node.path)
}, [isDir, node.path, onOpenFolder, onOpenNote])
const row = (
<div
role="button"
tabIndex={0}
onClick={handleOpen}
onKeyDown={(e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault()
handleOpen()
}
}}
className="group flex w-full cursor-pointer items-center gap-3 px-4 py-2.5 text-left transition-colors hover:bg-accent/50"
>
{isDir ? (
<FolderAvatar name={node.name} />
) : (
<div className="flex size-8 shrink-0 items-center justify-center rounded-md bg-muted text-muted-foreground">
<FileText className="size-4" />
</div>
)}
<div className="min-w-0 flex-1">
{renameActive ? (
<RenameField
initial={displayName(node)}
isDir={isDir}
path={node.path}
actions={actions}
onDone={onClearRename}
/>
) : (
<span className="block truncate text-sm text-foreground">{displayName(node)}</span>
)}
{isDir && (
<div className="mt-0.5 text-xs text-muted-foreground">
{count} {count === 1 ? 'note' : 'notes'}
</div>
)}
</div>
<div className="flex shrink-0 items-center gap-2">
<span className="text-xs text-muted-foreground tabular-nums whitespace-nowrap">
{modified}
</span>
{isDir && (
<ChevronRight className="size-4 text-muted-foreground/40 opacity-0 transition-opacity group-hover:opacity-100" />
)}
</div>
</div>
)
return (
<RowContextMenu node={node} actions={actions} onRequestRename={onRequestRename}>
{row}
</RowContextMenu>
)
}
function RenameField({
initial,
isDir,
path,
actions,
onDone,
}: {
initial: string
isDir: boolean
path: string
actions: KnowledgeViewActions
onDone: () => void
}) {
const [value, setValue] = useState(initial)
const inputRef = useRef<HTMLInputElement | null>(null)
const isSubmittingRef = useRef(false)
useEffect(() => {
requestAnimationFrame(() => {
inputRef.current?.focus()
inputRef.current?.select()
})
}, [])
const submit = useCallback(async () => {
if (isSubmittingRef.current) return
isSubmittingRef.current = true
const trimmed = value.trim()
if (trimmed && trimmed !== initial) {
try {
await actions.rename(path, trimmed, isDir)
toast('Renamed successfully', 'success')
} catch {
toast('Failed to rename', 'error')
}
}
onDone()
}, [actions, initial, isDir, onDone, path, value])
const cancel = useCallback(() => {
isSubmittingRef.current = true
onDone()
}, [onDone])
return (
<Input
ref={inputRef}
value={value}
onChange={(e) => setValue(e.target.value)}
onClick={(e) => e.stopPropagation()}
onKeyDown={(e) => {
e.stopPropagation()
if (e.key === 'Enter') {
e.preventDefault()
void submit()
} else if (e.key === 'Escape') {
e.preventDefault()
cancel()
}
}}
onBlur={() => {
if (!isSubmittingRef.current) void submit()
}}
className="h-7 text-sm"
/>
)
}
function RowContextMenu({
node,
actions,
onRequestRename,
children,
}: {
node: TreeNode
actions: KnowledgeViewActions
onRequestRename: (path: string) => void
children: React.ReactNode
}) {
const isDir = node.kind === 'dir'
const handleDelete = useCallback(async () => {
try {
await actions.remove(node.path)
toast('Moved to trash', 'success')
} catch {
toast('Failed to delete', 'error')
}
}, [actions, node.path])
const handleCopyPath = useCallback(() => {
actions.copyPath(node.path)
toast('Path copied', 'success')
}, [actions, node.path])
return (
<ContextMenu>
<ContextMenuTrigger asChild>{children}</ContextMenuTrigger>
<ContextMenuContent className="w-48" onCloseAutoFocus={(e) => e.preventDefault()}>
{isDir && (
<>
<ContextMenuItem onClick={() => actions.createNote(node.path)}>
<FilePlus className="mr-2 size-4" />
New Note
</ContextMenuItem>
<ContextMenuItem onClick={() => void actions.createFolder(node.path)}>
<FolderPlus className="mr-2 size-4" />
New Folder
</ContextMenuItem>
<ContextMenuSeparator />
</>
)}
{!isDir && actions.onOpenInNewTab && (
<>
<ContextMenuItem onClick={() => actions.onOpenInNewTab!(node.path)}>
<ExternalLink className="mr-2 size-4" />
Open in new tab
</ContextMenuItem>
<ContextMenuSeparator />
</>
)}
<ContextMenuItem onClick={handleCopyPath}>
<Copy className="mr-2 size-4" />
Copy Path
</ContextMenuItem>
<ContextMenuItem onClick={() => actions.revealInFileManager(node.path, isDir)}>
<FolderOpen className="mr-2 size-4" />
Open in {getFileManagerName()}
</ContextMenuItem>
<ContextMenuSeparator />
<ContextMenuItem onClick={() => onRequestRename(node.path)}>
<Pencil className="mr-2 size-4" />
Rename
</ContextMenuItem>
<ContextMenuItem variant="destructive" onClick={handleDelete}>
<Trash2 className="mr-2 size-4" />
Delete
</ContextMenuItem>
</ContextMenuContent>
</ContextMenu>
)
}

View file

@ -1,5 +1,5 @@
import { useEditor, EditorContent, Extension, Editor } from '@tiptap/react'
import { Plugin, PluginKey } from '@tiptap/pm/state'
import { Plugin, PluginKey, TextSelection } from '@tiptap/pm/state'
import { Decoration, DecorationSet, EditorView } from '@tiptap/pm/view'
import StarterKit from '@tiptap/starter-kit'
import Link from '@tiptap/extension-link'
@ -83,7 +83,8 @@ function nodeToText(node: JsonNode): string {
return text
} else if (child.type === 'wikiLink') {
const path = (child.attrs?.path as string) || ''
return path ? `[[${path}]]` : ''
const label = (child.attrs?.label as string | null | undefined) || ''
return path ? `[[${path}${label ? `|${label}` : ''}]]` : ''
} else if (child.type === 'hardBreak') {
return '\n'
}
@ -189,7 +190,8 @@ function blockToMarkdown(node: JsonNode): string {
return '---'
case 'wikiLink': {
const path = (node.attrs?.path as string) || ''
return `[[${path}]]`
const label = (node.attrs?.label as string | null | undefined) || ''
return `[[${path}${label ? `|${label}` : ''}]]`
}
case 'image': {
const src = (node.attrs?.src as string) || ''
@ -297,7 +299,7 @@ import { FrontmatterProperties } from './frontmatter-properties'
import { WikiLink } from '@/extensions/wiki-link'
import { Popover, PopoverAnchor, PopoverContent } from '@/components/ui/popover'
import { Command, CommandEmpty, CommandItem, CommandList } from '@/components/ui/command'
import { ensureMarkdownExtension, normalizeWikiPath, wikiLabel } from '@/lib/wiki-links'
import { ensureMarkdownExtension, normalizeWikiPath, splitWikiFragment, wikiLabel } from '@/lib/wiki-links'
import { extractAllFrontmatterValues, buildFrontmatter } from '@/lib/frontmatter'
import { RowboatMentionPopover } from './rowboat-mention-popover'
import '@/styles/editor.css'
@ -523,6 +525,106 @@ const TabIndentExtension = Extension.create({
},
})
const slugifyHeading = (text: string) =>
text
.trim()
.toLowerCase()
.replace(/[^\w\s-]/g, '')
.replace(/\s+/g, '-')
.replace(/-+/g, '-')
const decodeLinkTarget = (target: string) => {
try {
return decodeURIComponent(target)
} catch {
return target
}
}
const scrollToHeading = (view: EditorView, rawTarget: string) => {
const target = decodeLinkTarget(rawTarget.replace(/^#/, '')).trim()
if (!target) return false
const targetSlug = slugifyHeading(target)
let foundPos: number | null = null
view.state.doc.descendants((node, pos) => {
if (node.type.name !== 'heading') return true
const headingText = node.textContent.trim()
if (
headingText.toLowerCase() === target.toLowerCase()
|| slugifyHeading(headingText) === targetSlug
) {
foundPos = pos
return false
}
return true
})
if (foundPos === null) return false
const selectionPos = Math.min(foundPos + 1, view.state.doc.content.size)
view.dispatch(
view.state.tr.setSelection(TextSelection.near(view.state.doc.resolve(selectionPos)))
)
view.focus()
const domAtPos = view.domAtPos(foundPos + 1)
const node = domAtPos.node
const headingEl = node.nodeType === Node.ELEMENT_NODE
? (node as HTMLElement)
: node.parentElement
headingEl?.scrollIntoView({ block: 'start', behavior: 'smooth' })
return true
}
const stripMarkdownExtension = (path: string) =>
path.toLowerCase().endsWith('.md') ? path.slice(0, -3) : path
const isSameNotePath = (linkPath: string, notePath?: string) => {
if (!notePath) return false
const normalizedLink = stripMarkdownExtension(normalizeWikiPath(linkPath)).toLowerCase()
const normalizedNote = stripMarkdownExtension(normalizeWikiPath(notePath)).toLowerCase()
return normalizedLink === normalizedNote
}
const isExternalHref = (href: string) =>
/^(https?:|mailto:|tel:)/i.test(href)
const collapseRelativeSegments = (relPath: string) => {
const parts = relPath.split('/').filter((part) => part !== '' && part !== '.')
const stack: string[] = []
for (const part of parts) {
if (part === '..') {
if (stack.length === 0) return null
stack.pop()
} else {
stack.push(part)
}
}
return stack.join('/')
}
const resolveWorkspaceLinkPath = (href: string, notePath?: string) => {
const withoutHash = href.split('#')[0]
const withoutQuery = withoutHash.split('?')[0]
const decoded = decodeLinkTarget(withoutQuery)
if (!decoded) return null
if (/^file:\/\//i.test(decoded)) {
try {
return decodeURIComponent(new URL(decoded).pathname)
} catch {
return decoded
}
}
if (/^[a-zA-Z]:[\\/]/.test(decoded) || decoded.startsWith('/')) return decoded
if (decoded.startsWith('knowledge/') || !notePath) return collapseRelativeSegments(decoded.replace(/^\.\//, ''))
const noteDir = notePath.split('/').slice(0, -1).join('/')
return collapseRelativeSegments(`${noteDir}/${decoded.replace(/^\.\//, '')}`)
}
export interface MarkdownEditorHandle {
/** Returns {path, lineNumber} for the cursor's current position, or null if no notePath / no editor. */
getCursorContext: () => { path: string; lineNumber: number } | null
@ -546,6 +648,13 @@ export const MarkdownEditor = forwardRef<MarkdownEditorHandle, MarkdownEditorPro
}, ref) {
const isInternalUpdate = useRef(false)
const wrapperRef = useRef<HTMLDivElement>(null)
// Read wikiLinks lazily inside the editor config via this ref. wikiLinks changes
// identity whenever the workspace directory tree changes (file watcher → new file
// list), and it used to be a useEditor() dependency — so any background write to
// the workspace destroyed and recreated the entire editor, resetting scroll to the
// top. Keeping it off the dep array (and reading the ref at event time) means the
// editor instance survives directory changes.
const wikiLinksRef = useRef(wikiLinks)
const [activeWikiLink, setActiveWikiLink] = useState<WikiLinkMatch | null>(null)
const [anchorPosition, setAnchorPosition] = useState<{ left: number; top: number } | null>(null)
const [selectionHighlight, setSelectionHighlight] = useState<SelectionHighlightRange>(null)
@ -568,6 +677,7 @@ export const MarkdownEditor = forwardRef<MarkdownEditorHandle, MarkdownEditorPro
// Keep ref in sync with state for the plugin to access
selectionHighlightRef.current = selectionHighlight
wikiLinksRef.current = wikiLinks
// Memoize the selection highlight extension
const selectionHighlightExtension = useMemo(
@ -644,6 +754,7 @@ export const MarkdownEditor = forwardRef<MarkdownEditorHandle, MarkdownEditorPro
heading: {
levels: [1, 2, 3],
},
link: false,
}),
Link.configure({
openOnClick: false,
@ -673,11 +784,9 @@ export const MarkdownEditor = forwardRef<MarkdownEditorHandle, MarkdownEditorPro
TranscriptBlockExtension,
MermaidBlockExtension,
WikiLink.configure({
onCreate: wikiLinks?.onCreate
? (path: string) => {
void wikiLinks.onCreate(path)
}
: undefined,
onCreate: (path: string) => {
void wikiLinksRef.current?.onCreate?.(path)
},
}),
TaskList,
TaskItem.configure({
@ -804,15 +913,57 @@ export const MarkdownEditor = forwardRef<MarkdownEditorHandle, MarkdownEditorPro
handleClickOn: (_view, _pos, node, _nodePos, event) => {
if (node.type.name === 'wikiLink') {
event.preventDefault()
wikiLinks?.onOpen?.(node.attrs.path)
const wikiPath = String(node.attrs.path ?? '')
const { path: linkedNotePath, heading } = splitWikiFragment(wikiPath)
if (heading && (!linkedNotePath || isSameNotePath(linkedNotePath, notePath))) {
return scrollToHeading(_view, heading)
}
wikiLinksRef.current?.onOpen?.(node.attrs.path)
return true
}
return false
},
handleDOMEvents: {
click: (view, event) => {
const target = event.target as Element | null
const link = target?.closest('a[href]') as HTMLAnchorElement | null
if (!link) return false
if (link.dataset.type === 'wiki-link') return false
const href = link.getAttribute('href') ?? ''
if (!href) return false
if (href.startsWith('#')) {
event.preventDefault()
return scrollToHeading(view, href)
}
if (isExternalHref(href)) {
event.preventDefault()
window.open(href, '_blank')
return true
}
const workspacePath = resolveWorkspaceLinkPath(href, notePath)
if (!workspacePath) return false
event.preventDefault()
void window.ipc.invoke('shell:openPath', { path: workspacePath }).then((result) => {
if (result.error) console.error('Failed to open linked file:', result.error)
}).catch((err) => {
console.error('Failed to open linked file:', err)
})
return true
},
},
},
// NOTE: wikiLinks is intentionally NOT a dependency — it's read via wikiLinksRef
// at event time. Including it rebuilds the whole editor on every directory change
// (file watcher), which resets scroll to the top. See wikiLinksRef declaration.
}, [
editorSessionKey,
maybeCommitPrimaryHeading,
notePath,
preventTitleHeadingDemotion,
promoteFirstParagraphToTitleHeading,
])
@ -1060,11 +1211,37 @@ export const MarkdownEditor = forwardRef<MarkdownEditorHandle, MarkdownEditorPro
// Normalize for comparison (trim trailing whitespace from lines)
const normalizeForCompare = (s: string) => s.split('\n').map(line => line.trimEnd()).join('\n').trim()
if (normalizeForCompare(currentContent) !== normalizeForCompare(content)) {
// Preserve scroll + selection across an external content sync. setContent()
// resets the selection to the top of the doc and ProseMirror scrolls it into
// view; without restoring, a background writer touching the open file (graph
// builder, live-note runner, version-history commit) yanks the viewport back
// to the top repeatedly — making the note impossible to scroll. This editor
// instance is bound to a single note path, so the prior scrollTop is always
// valid for the reloaded content.
const wrapper = wrapperRef.current
const prevScrollTop = wrapper?.scrollTop ?? 0
const hadFocus = editor.isFocused
const { from: prevFrom, to: prevTo } = editor.state.selection
isInternalUpdate.current = true
const preprocessed = preprocessMarkdown(content)
// Treat tab-open content as baseline: do not add hydration to undo history.
editor.chain().setMeta('addToHistory', false).setContent(preprocessed).run()
// Only restore the caret for a focused editor, so we never steal focus or
// scroll for a passive viewer. Clamp to the (possibly shorter) new doc.
if (hadFocus) {
const docSize = editor.state.doc.content.size
const from = Math.min(prevFrom, docSize)
const to = Math.min(prevTo, docSize)
try {
editor.chain().setMeta('addToHistory', false).setTextSelection({ from, to }).run()
} catch { /* selection no longer valid in the new doc — ignore */ }
}
isInternalUpdate.current = false
// Restore scroll last so it wins over any scrollIntoView triggered above.
if (wrapper) wrapper.scrollTop = prevScrollTop
}
}
}, [editor, content])

View file

@ -1,7 +1,10 @@
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { Calendar, ChevronDown, Loader2, Mic, Square, Video } from 'lucide-react'
import { createPortal } from 'react-dom'
import { Calendar, ChevronDown, Clock, ExternalLink, Loader2, MapPin, Mic, Square, UserRound, UsersRound, Video, X } from 'lucide-react'
import { Button } from '@/components/ui/button'
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'
import { SettingsDialog } from '@/components/settings-dialog'
import { formatRelativeTime } from '@/lib/relative-time'
import { extractConferenceLink } from '@/lib/calendar-event'
import { cn } from '@/lib/utils'
@ -39,14 +42,32 @@ type RawCalendarEvent = {
start?: { dateTime?: string; date?: string }
end?: { dateTime?: string; date?: string }
location?: string
description?: string
htmlLink?: string
status?: string
attendees?: Array<{ email?: string; self?: boolean; responseStatus?: string }>
creator?: CalendarPerson
organizer?: CalendarPerson
attendees?: CalendarAttendee[]
conferenceData?: { entryPoints?: Array<{ entryPointType?: string; uri?: string }> }
hangoutLink?: string
conferenceLink?: string
}
type CalendarPerson = {
email?: string
displayName?: string
self?: boolean
}
type CalendarAttendee = CalendarPerson & {
responseStatus?: string
optional?: boolean
}
type DescriptionPart =
| { type: 'text'; text: string }
| { type: 'link'; text: string; href: string }
type UpcomingEvent = {
id: string
summary: string
@ -54,8 +75,12 @@ type UpcomingEvent = {
end: Date | null
isAllDay: boolean
location: string | null
description: string | null
htmlLink: string | null
conferenceLink: string | null
creator: CalendarPerson | null
organizer: CalendarPerson | null
attendees: CalendarAttendee[]
source: string // workspace path to the calendar_sync JSON
rawStart: { dateTime?: string; date?: string } | undefined
rawEnd: { dateTime?: string; date?: string } | undefined
@ -124,8 +149,12 @@ function normalizeEvent(raw: RawCalendarEvent, sourcePath: string): UpcomingEven
end,
isAllDay,
location: raw.location?.trim() || null,
description: raw.description?.trim() || null,
htmlLink: raw.htmlLink ?? null,
conferenceLink,
creator: raw.creator ?? null,
organizer: raw.organizer ?? null,
attendees: raw.attendees ?? [],
source: sourcePath,
rawStart: raw.start,
rawEnd: raw.end,
@ -184,11 +213,177 @@ function formatEventTimeRange(event: UpcomingEvent): string {
return `${start} ${end}`
}
// Compact range for the upcoming list: drops the leading meridiem when both
// ends share it ("9:00 11:00 AM" instead of "9:00 AM 11:00 AM").
function formatEventTimeRangeCompact(event: UpcomingEvent): string {
if (event.isAllDay) return 'All day'
const startStr = event.start.toLocaleTimeString([], { hour: 'numeric', minute: '2-digit' })
if (!event.end) return startStr
const sameDay = localDateKey(event.start) === localDateKey(event.end)
if (!sameDay) return formatEventTimeRange(event)
const endStr = event.end.toLocaleTimeString([], { hour: 'numeric', minute: '2-digit' })
const meridiemRe = /\s*[AP]M$/i
const startMer = startStr.match(meridiemRe)?.[0]?.trim().toUpperCase()
const endMer = endStr.match(meridiemRe)?.[0]?.trim().toUpperCase()
if (startMer && endMer && startMer === endMer) {
return `${startStr.replace(meridiemRe, '')} ${endStr}`
}
return `${startStr} ${endStr}`
}
// Whether a timed event is happening right now.
function isEventNow(event: UpcomingEvent): boolean {
if (event.isAllDay) return false
const now = Date.now()
const start = event.start.getTime()
const end = event.end ? event.end.getTime() : start + 30 * 60 * 1000
return start <= now && now < end
}
// Human label for the conferencing provider behind an event's join link.
function meetingPlatformLabel(link: string | null): string | null {
if (!link) return null
if (/zoom\.us|zoomgov\.com/i.test(link)) return 'Zoom'
if (/teams\.(?:microsoft|live)\.com/i.test(link)) return 'Teams'
if (/meet\.google\.com/i.test(link)) return 'Meet'
return 'Video call'
}
function formatEventDetailTime(event: UpcomingEvent): string {
if (!event.isAllDay) {
const date = event.start.toLocaleDateString([], { weekday: 'long', month: 'long', day: 'numeric' })
return `${date}, ${formatEventTimeRange(event)}`
}
const start = event.start.toLocaleDateString([], { weekday: 'long', month: 'long', day: 'numeric' })
if (!event.end) return `${start}, all day`
const exclusiveEnd = addDays(event.end, -1)
if (localDateKey(exclusiveEnd) === localDateKey(event.start)) return `${start}, all day`
const end = exclusiveEnd.toLocaleDateString([], { weekday: 'long', month: 'long', day: 'numeric' })
return `${start} ${end}, all day`
}
function personLabel(person: CalendarPerson | null | undefined): string | null {
if (!person) return null
return person.displayName?.trim() || person.email?.trim() || null
}
function attendeeLabel(attendee: CalendarAttendee): string | null {
const label = personLabel(attendee)
if (!label) return null
if (attendee.self) return `${label} (you)`
return label
}
function normalizeDescriptionParts(parts: DescriptionPart[]): DescriptionPart[] {
const normalized: DescriptionPart[] = []
for (const part of parts) {
const text = part.text.replace(/\n{3,}/g, '\n\n')
if (!text) continue
const previous = normalized[normalized.length - 1]
if (previous?.type === 'text' && part.type === 'text') {
previous.text += text
} else if (part.type === 'link') {
normalized.push({ ...part, text })
} else {
normalized.push({ type: 'text', text })
}
}
return normalized
}
function isSafeDescriptionHref(value: string): boolean {
try {
const url = new URL(value, window.location.href)
return url.protocol === 'http:' || url.protocol === 'https:' || url.protocol === 'mailto:'
} catch {
return false
}
}
function linkifyText(value: string): DescriptionPart[] {
const parts: DescriptionPart[] = []
const urlRe = /\bhttps?:\/\/[^\s<>"')\]]+|\bwww\.[^\s<>"')\]]+/gi
let lastIndex = 0
for (const match of value.matchAll(urlRe)) {
const raw = match[0]
const index = match.index ?? 0
if (index > lastIndex) parts.push({ type: 'text', text: value.slice(lastIndex, index) })
const href = raw.startsWith('www.') ? `https://${raw}` : raw
parts.push({ type: 'link', text: raw, href })
lastIndex = index + raw.length
}
if (lastIndex < value.length) parts.push({ type: 'text', text: value.slice(lastIndex) })
return parts
}
function parseDescriptionParts(value: string): DescriptionPart[] {
const withLineBreaks = value.replace(/<\s*br\s*\/?>/gi, '\n').replace(/<\/\s*(p|div|li|tr|h[1-6])\s*>/gi, '\n')
if (typeof DOMParser === 'undefined') {
return normalizeDescriptionParts(linkifyText(withLineBreaks.replace(/<[^>]*>/g, '').trim()))
}
const doc = new DOMParser().parseFromString(withLineBreaks, 'text/html')
const parts: DescriptionPart[] = []
const visit = (node: Node) => {
if (node.nodeType === Node.TEXT_NODE) {
parts.push(...linkifyText(node.textContent ?? ''))
return
}
if (!(node instanceof HTMLElement)) return
if (node.tagName === 'A') {
const href = node.getAttribute('href') ?? ''
const text = node.textContent?.trim() || href
if (href && isSafeDescriptionHref(href)) {
parts.push({ type: 'link', text, href })
return
}
}
if (node.tagName === 'BR') {
parts.push({ type: 'text', text: '\n' })
return
}
node.childNodes.forEach(visit)
if (/^(P|DIV|LI|TR|H[1-6])$/.test(node.tagName)) {
parts.push({ type: 'text', text: '\n' })
}
}
doc.body.childNodes.forEach(visit)
return normalizeDescriptionParts(parts).map((part, index, all) => {
if (index === 0 || index === all.length - 1) return { ...part, text: part.text.trim() }
return part
}).filter((part) => part.text.length > 0)
}
function UpcomingEvents() {
const [events, setEvents] = useState<UpcomingEvent[]>([])
const [loading, setLoading] = useState(true)
const [error, setError] = useState<string | null>(null)
const [refreshTick, setRefreshTick] = useState(0)
// Calendar sync uses the native Google OAuth connection.
const [calendarConnected, setCalendarConnected] = useState<boolean | null>(null)
const [settingsOpen, setSettingsOpen] = useState(false)
useEffect(() => {
let cancelled = false
const check = async () => {
try {
const oauthState = await window.ipc.invoke('oauth:getState', null)
if (!cancelled) setCalendarConnected(oauthState.config?.google?.connected ?? false)
} catch {
if (!cancelled) setCalendarConnected(false)
}
}
void check()
const cleanupOAuthConnect = window.ipc.on('oauth:didConnect', () => { void check() })
return () => {
cancelled = true
cleanupOAuthConnect()
}
}, [])
const loadEvents = useCallback(async () => {
setLoading(true)
@ -273,8 +468,9 @@ function UpcomingEvents() {
break
}
})
// Refresh on the hour so day labels and "ended" filtering stay current.
const tick = setInterval(() => setRefreshTick((t) => t + 1), 60 * 60 * 1000)
// Refresh every minute so the "now" highlight, day labels, and "ended"
// filtering stay current without waiting on a calendar sync.
const tick = setInterval(() => setRefreshTick((t) => t + 1), 60 * 1000)
return () => {
cleanup()
clearInterval(tick)
@ -304,164 +500,284 @@ function UpcomingEvents() {
Coming up
</h3>
{loading && events.length === 0 ? null : (
<span
className="text-[11px] uppercase tracking-wider"
style={{ color: 'var(--gm-text-faint)' }}
>
<span className="text-[11px] uppercase tracking-wider text-muted-foreground">
{totalVisible} {totalVisible === 1 ? 'event' : 'events'}
</span>
)}
</div>
{loading && events.length === 0 ? (
{calendarConnected === false && events.length === 0 ? (
<div className="flex flex-col items-center gap-3 py-12 text-center">
<Calendar className="size-7 text-muted-foreground opacity-50" />
<p className="text-sm text-muted-foreground">Connect your calendar to see upcoming meetings here.</p>
<button
type="button"
onClick={() => setSettingsOpen(true)}
className="inline-flex items-center gap-1.5 rounded-md border border-border bg-background px-3.5 py-1.5 text-sm font-medium text-foreground transition-colors hover:bg-accent"
>
<Calendar className="size-4" />
Connect your calendar
</button>
</div>
) : loading && events.length === 0 ? (
<div className="flex items-center justify-center py-6">
<Loader2 className="size-4 animate-spin text-muted-foreground" />
</div>
) : error ? (
<div className="py-4 text-sm text-muted-foreground">{error}</div>
) : (
<div
className="overflow-hidden rounded-xl border"
style={{ borderColor: 'var(--gm-border)', background: 'var(--gm-bg)' }}
>
{visibleDays.map((day, idx) => (
<UpcomingDayRow
<div className="flex flex-col gap-3">
{visibleDays.map((day) => (
<UpcomingDayCard
key={day.dateKey}
day={day}
isToday={day.dateKey === todayKey}
isLast={idx === visibleDays.length - 1}
/>
))}
</div>
)}
</div>
<SettingsDialog open={settingsOpen} onOpenChange={setSettingsOpen} defaultTab="connections" />
</section>
)
}
function UpcomingDayRow({ day, isToday, isLast }: { day: DayGroup; isToday: boolean; isLast: boolean }) {
function UpcomingDayCard({ day, isToday }: { day: DayGroup; isToday: boolean }) {
const dayNum = day.date.getDate()
const month = day.date.toLocaleDateString([], { month: 'short' })
const weekday = day.date.toLocaleDateString([], { weekday: 'short' })
const count = day.events.length
return (
<div
className="grid"
style={{
gridTemplateColumns: '96px 1fr',
borderBottom: isLast ? undefined : '1px dashed var(--gm-border-strong)',
}}
>
<div className="flex items-start gap-2 px-4 py-4">
<span
className="leading-none"
style={{ fontSize: 30, fontWeight: 400, color: 'var(--gm-text-strong)' }}
>
{dayNum}
</span>
<span className="flex flex-col leading-tight">
<span
className="flex items-center gap-1"
style={{ fontSize: 12, fontWeight: 600, color: 'var(--gm-text)' }}
>
{month}
{isToday ? (
<span
aria-hidden
className="inline-block rounded-full"
style={{ width: 5, height: 5, background: 'var(--gm-accent)' }}
/>
) : null}
<div className="overflow-hidden rounded-xl border bg-card">
<div className="flex items-center justify-between gap-3 border-b bg-muted px-5 py-3.5">
<div className="flex min-w-0 items-baseline gap-2">
<span className="text-[22px] font-bold leading-none text-foreground">{dayNum}</span>
<span className="truncate text-[13px] text-muted-foreground">
{month} · {weekday}
</span>
<span style={{ fontSize: 12, color: 'var(--gm-text-faint)' }}>{weekday}</span>
{isToday ? (
<span className="shrink-0 rounded-md bg-foreground px-1.5 py-0.5 text-[10px] font-semibold uppercase tracking-wide text-background">
Today
</span>
) : null}
</div>
<span className="shrink-0 text-xs text-muted-foreground">
{count} {count === 1 ? 'event' : 'events'}
</span>
</div>
<div className="flex flex-col py-3 pr-3">
{day.events.length === 0 ? (
<div
className="flex w-full items-center gap-3 px-3 py-2 text-sm"
style={{ color: 'var(--gm-text-faint)', minHeight: 40 }}
>
<span aria-hidden className="self-stretch shrink-0" style={{ width: 3 }} />
<span>{isToday ? 'No events today' : 'No events'}</span>
</div>
) : (
day.events.map((ev) => <UpcomingEventItem key={ev.id} event={ev} />)
)}
</div>
{count === 0 ? (
<div className="px-5 py-4 text-sm text-muted-foreground">
{isToday ? 'No events today' : 'No events'}
</div>
) : (
day.events.map((ev, idx) => (
<UpcomingEventItem key={ev.id} event={ev} isLast={idx === count - 1} />
))
)}
</div>
)
}
function UpcomingEventItem({ event }: { event: UpcomingEvent }) {
const handleOpen = useCallback(() => {
if (event.htmlLink) window.open(event.htmlLink, '_blank')
}, [event.htmlLink])
function NowBadge() {
return (
<span className="shrink-0 rounded bg-green-600 px-1.5 py-px text-[10px] font-bold uppercase leading-[1.5] tracking-wide text-white">
Now
</span>
)
}
function UpcomingEventItem({ event, isLast }: { event: UpcomingEvent; isLast: boolean }) {
const [open, setOpen] = useState(false)
const isNow = isEventNow(event)
const platform = meetingPlatformLabel(event.conferenceLink)
const subtitle = platform ?? event.location
const titleAndLocation = event.location ? `${event.summary} · ${event.location}` : event.summary
return (
<div
role="button"
tabIndex={0}
onClick={handleOpen}
onKeyDown={(e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault()
handleOpen()
}
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<div
role="button"
tabIndex={0}
title={titleAndLocation}
className={cn(
'group flex w-full cursor-pointer items-center gap-4 px-5 py-3 text-left transition-colors',
!isLast && 'border-b',
isNow ? 'bg-muted' : 'hover:bg-muted/50',
)}
>
<span className="shrink-0 text-[13px] tabular-nums text-muted-foreground" style={{ width: 118 }}>
{formatEventTimeRangeCompact(event)}
</span>
<span className="flex min-w-0 flex-1 flex-col">
<span className="flex items-center gap-2">
<span className="truncate text-sm font-semibold text-foreground">
{event.summary}
</span>
{isNow ? <NowBadge /> : null}
</span>
{subtitle ? (
<span className="mt-0.5 flex items-center gap-1.5 text-xs text-muted-foreground">
{platform ? <Video className="size-3.5 shrink-0" /> : <MapPin className="size-3.5 shrink-0" />}
<span className="truncate">{subtitle}</span>
</span>
) : null}
</span>
<div className="shrink-0">
{event.conferenceLink ? (
<SplitJoinButton
onJoinAndNotes={() => triggerMeetingCapture(event, true)}
onNotesOnly={() => triggerMeetingCapture(event, false)}
/>
) : (
<button
type="button"
onClick={(e) => { e.stopPropagation(); triggerMeetingCapture(event, false) }}
onMouseDown={(e) => e.stopPropagation()}
className="inline-flex items-center gap-1.5 rounded-md border bg-background px-2.5 py-1.5 text-xs font-medium text-foreground transition-colors hover:bg-accent"
>
<Mic className="size-3.5" />
Take notes
</button>
)}
</div>
</div>
</PopoverTrigger>
<EventDetailsPopover event={event} onClose={() => setOpen(false)} />
</Popover>
)
}
function EventDetailsPopover({ event, onClose }: { event: UpcomingEvent; onClose: () => void }) {
const organizer = personLabel(event.organizer) ?? personLabel(event.creator)
const attendees = event.attendees.map(attendeeLabel).filter((label): label is string => Boolean(label))
const descriptionParts = event.description ? parseDescriptionParts(event.description) : []
const handleMeetingCapture = (openConference: boolean) => {
onClose()
triggerMeetingCapture(event, openConference)
}
return (
<PopoverContent
align="start"
side="bottom"
sideOffset={6}
className="w-[min(380px,calc(100vw-32px))] rounded-lg p-0 shadow-xl"
style={{
backgroundColor: 'var(--muted, #f4f4f5)',
borderColor: 'var(--border, #e4e4e7)',
color: 'var(--popover-foreground, #09090b)',
}}
title={titleAndLocation}
className={cn(
'upcoming-event-row group flex w-full items-center gap-3 px-3 py-2 text-left cursor-pointer',
)}
style={{ color: 'var(--gm-text)', minHeight: 40 }}
>
<span
aria-hidden
className="self-stretch rounded-full"
style={{ width: 3, background: 'var(--gm-accent)', opacity: 0.55 }}
/>
<span className="min-w-0 flex-1">
<span
className="block truncate"
style={{ fontSize: 14, fontWeight: 500, color: 'var(--gm-text-strong)' }}
>
{event.summary}
</span>
<span
className="mt-0.5 block truncate"
style={{ fontSize: 12, color: 'var(--gm-text-muted)' }}
>
{formatEventTimeRange(event)}
{event.location ? <span style={{ color: 'var(--gm-text-faint)' }}> · {event.location}</span> : null}
</span>
</span>
<div className="shrink-0 opacity-0 transition-opacity group-hover:opacity-100 focus-within:opacity-100">
{event.conferenceLink ? (
<SplitJoinButton
onJoinAndNotes={() => triggerMeetingCapture(event, true)}
onNotesOnly={() => triggerMeetingCapture(event, false)}
/>
) : (
<div className="flex items-center justify-end gap-1 border-b px-3 py-2" style={{ borderColor: 'var(--border, #e4e4e7)' }}>
{event.htmlLink ? (
<button
type="button"
onClick={(e) => { e.stopPropagation(); triggerMeetingCapture(event, false) }}
onMouseDown={(e) => e.stopPropagation()}
className="inline-flex items-center gap-1 rounded-md px-2 py-1 text-xs transition-colors"
style={{
background: 'var(--gm-bg-pill)',
color: 'var(--gm-text)',
border: '1px solid var(--gm-border)',
}}
onMouseEnter={(e) => { (e.currentTarget as HTMLButtonElement).style.background = 'var(--gm-bg-pill-hover)' }}
onMouseLeave={(e) => { (e.currentTarget as HTMLButtonElement).style.background = 'var(--gm-bg-pill)' }}
onClick={() => window.open(event.htmlLink!, '_blank')}
className="inline-flex size-8 items-center justify-center rounded-md transition-colors"
style={{ color: 'var(--muted-foreground, #71717a)' }}
aria-label="Open in Google Calendar"
title="Open in Google Calendar"
onMouseEnter={(e) => { (e.currentTarget as HTMLButtonElement).style.background = 'var(--background, #ffffff)' }}
onMouseLeave={(e) => { (e.currentTarget as HTMLButtonElement).style.background = 'transparent' }}
>
<Mic className="size-3" />
Take notes
<ExternalLink className="size-4" />
</button>
)}
) : null}
<button
type="button"
onClick={onClose}
className="inline-flex size-8 items-center justify-center rounded-md transition-colors"
style={{ color: 'var(--muted-foreground, #71717a)' }}
aria-label="Close event details"
title="Close"
onMouseEnter={(e) => { (e.currentTarget as HTMLButtonElement).style.background = 'var(--background, #ffffff)' }}
onMouseLeave={(e) => { (e.currentTarget as HTMLButtonElement).style.background = 'transparent' }}
>
<X className="size-4" />
</button>
</div>
<div className="space-y-4 px-5 py-4">
<div className="flex gap-3">
<span
aria-hidden
className="mt-1.5 h-3 w-3 shrink-0 rounded-sm"
style={{ background: 'var(--primary, #18181b)' }}
/>
<div className="min-w-0">
<h4 className="break-words text-[20px] font-normal leading-6" style={{ color: 'var(--foreground, #09090b)' }}>
{event.summary}
</h4>
</div>
</div>
<EventDetailRow icon={<Clock className="size-4" />} value={formatEventDetailTime(event)} />
{event.location ? <EventDetailRow icon={<MapPin className="size-4" />} value={event.location} /> : null}
{organizer ? <EventDetailRow icon={<UserRound className="size-4" />} value={`Organizer: ${organizer}`} /> : null}
{attendees.length > 0 ? (
<EventDetailRow
icon={<UsersRound className="size-4" />}
value={attendees.slice(0, 8).join(', ') + (attendees.length > 8 ? `, +${attendees.length - 8} more` : '')}
/>
) : null}
{event.conferenceLink ? (
<div className="flex gap-3">
<Video className="mt-1 size-4 shrink-0" style={{ color: 'var(--muted-foreground, #71717a)' }} />
<div className="flex flex-wrap gap-2">
<Button type="button" size="sm" onClick={() => handleMeetingCapture(true)}>
Join & take notes
</Button>
<Button type="button" size="sm" variant="outline" onClick={() => handleMeetingCapture(false)}>
Take notes only
</Button>
</div>
</div>
) : (
<div className="flex gap-3">
<Mic className="mt-1 size-4 shrink-0" style={{ color: 'var(--muted-foreground, #71717a)' }} />
<Button type="button" size="sm" variant="outline" onClick={() => handleMeetingCapture(false)}>
Take notes
</Button>
</div>
)}
{descriptionParts.length > 0 ? (
<div className="flex gap-3">
<span className="mt-1 size-4 shrink-0" />
<div className="max-h-40 overflow-auto whitespace-pre-wrap break-words text-sm leading-5" style={{ color: 'var(--foreground, #27272a)' }}>
{descriptionParts.map((part, index) => {
if (part.type === 'text') return <span key={index}>{part.text}</span>
return (
<a
key={index}
href={part.href}
onClick={(e) => {
e.preventDefault()
window.open(part.href, '_blank')
}}
className="underline underline-offset-2"
style={{ color: 'var(--primary, #18181b)' }}
>
{part.text}
</a>
)
})}
</div>
</div>
) : null}
</div>
</PopoverContent>
)
}
function EventDetailRow({ icon, value }: { icon: React.ReactNode; value: string }) {
return (
<div className="flex gap-3 text-sm leading-5">
<span className="mt-0.5 shrink-0" style={{ color: 'var(--muted-foreground, #71717a)' }}>{icon}</span>
<span className="min-w-0 break-words" style={{ color: 'var(--foreground, #27272a)' }}>{value}</span>
</div>
)
}
@ -471,41 +787,46 @@ function SplitJoinButton({ onJoinAndNotes, onNotesOnly }: {
onNotesOnly: () => void
}) {
const [open, setOpen] = useState(false)
const ref = useRef<HTMLDivElement>(null)
const containerRef = useRef<HTMLDivElement>(null)
const menuRef = useRef<HTMLDivElement>(null)
// Fixed-position coords for the portaled menu so it isn't clipped by the
// calendar card's `overflow-hidden`.
const [menuPos, setMenuPos] = useState<{ top: number; right: number } | null>(null)
const updatePos = useCallback(() => {
const rect = containerRef.current?.getBoundingClientRect()
if (!rect) return
setMenuPos({ top: rect.bottom + 4, right: window.innerWidth - rect.right })
}, [])
useEffect(() => {
if (!open) return
updatePos()
const handler = (e: MouseEvent) => {
const target = e.target
if (ref.current && target instanceof globalThis.Node && !ref.current.contains(target)) {
setOpen(false)
}
if (!(target instanceof globalThis.Node)) return
if (containerRef.current?.contains(target) || menuRef.current?.contains(target)) return
setOpen(false)
}
document.addEventListener('mousedown', handler)
return () => document.removeEventListener('mousedown', handler)
}, [open])
window.addEventListener('resize', updatePos)
window.addEventListener('scroll', updatePos, true)
return () => {
document.removeEventListener('mousedown', handler)
window.removeEventListener('resize', updatePos)
window.removeEventListener('scroll', updatePos, true)
}
}, [open, updatePos])
return (
<div
ref={ref}
style={{ position: 'relative', display: 'inline-flex', alignItems: 'stretch' }}
>
<div ref={containerRef} className="relative inline-flex items-stretch">
<button
type="button"
onMouseDown={(e) => e.stopPropagation()}
onClick={(e) => { e.stopPropagation(); onJoinAndNotes() }}
className="inline-flex items-center gap-1 px-2 py-1 text-xs transition-colors"
style={{
background: 'var(--gm-bg-pill)',
color: 'var(--gm-text)',
border: '1px solid var(--gm-border)',
borderTopLeftRadius: 6,
borderBottomLeftRadius: 6,
}}
onMouseEnter={(e) => { (e.currentTarget as HTMLButtonElement).style.background = 'var(--gm-bg-pill-hover)' }}
onMouseLeave={(e) => { (e.currentTarget as HTMLButtonElement).style.background = 'var(--gm-bg-pill)' }}
className="inline-flex items-center gap-1.5 rounded-l-md border bg-background px-2.5 py-1.5 text-xs font-medium text-foreground transition-colors hover:bg-accent"
>
<Video className="size-3" />
<Video className="size-3.5" />
Join & take notes
</button>
<button
@ -513,49 +834,30 @@ function SplitJoinButton({ onJoinAndNotes, onNotesOnly }: {
onMouseDown={(e) => e.stopPropagation()}
onClick={(e) => { e.stopPropagation(); setOpen((v) => !v) }}
aria-label="More meeting options"
className="inline-flex items-center justify-center px-1.5 py-1 transition-colors"
style={{
background: 'var(--gm-bg-pill)',
color: 'var(--gm-text)',
border: '1px solid var(--gm-border)',
borderLeft: 'none',
borderTopRightRadius: 6,
borderBottomRightRadius: 6,
}}
onMouseEnter={(e) => { (e.currentTarget as HTMLButtonElement).style.background = 'var(--gm-bg-pill-hover)' }}
onMouseLeave={(e) => { (e.currentTarget as HTMLButtonElement).style.background = 'var(--gm-bg-pill)' }}
className="inline-flex items-center justify-center rounded-r-md border border-l-0 bg-background px-1.5 py-1.5 text-foreground transition-colors hover:bg-accent"
>
<ChevronDown className="size-3" />
</button>
{open && (
<div
style={{
position: 'absolute',
top: 'calc(100% + 4px)',
right: 0,
zIndex: 50,
background: 'var(--gm-bg-card)',
border: '1px solid var(--gm-border)',
borderRadius: 6,
boxShadow: '0 4px 12px rgba(0,0,0,0.12)',
minWidth: 144,
overflow: 'hidden',
}}
>
<button
type="button"
onMouseDown={(e) => e.stopPropagation()}
onClick={(e) => { e.stopPropagation(); setOpen(false); onNotesOnly() }}
className="flex w-full items-center gap-1 px-2 py-1.5 text-xs"
style={{ background: 'transparent', color: 'var(--gm-text)', whiteSpace: 'nowrap', border: 'none' }}
onMouseEnter={(e) => { (e.currentTarget as HTMLButtonElement).style.background = 'var(--gm-bg-row-hover)' }}
onMouseLeave={(e) => { (e.currentTarget as HTMLButtonElement).style.background = 'transparent' }}
>
<Mic className="size-3" />
Take notes only
</button>
</div>
)}
{open && menuPos
? createPortal(
<div
ref={menuRef}
style={{ position: 'fixed', top: menuPos.top, right: menuPos.right, zIndex: 60 }}
className="min-w-36 overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-lg"
>
<button
type="button"
onMouseDown={(e) => e.stopPropagation()}
onClick={(e) => { e.stopPropagation(); setOpen(false); onNotesOnly() }}
className="flex w-full items-center gap-1.5 whitespace-nowrap px-2.5 py-1.5 text-xs transition-colors hover:bg-accent"
>
<Mic className="size-3" />
Take notes only
</button>
</div>,
document.body,
)
: null}
</div>
)
}

View file

@ -42,6 +42,7 @@ export function RichMarkdownViewer({ content }: { content: string }) {
heading: {
levels: [1, 2, 3],
},
link: false,
}),
Link.configure({
openOnClick: true,

View file

@ -21,7 +21,7 @@ interface SearchResult {
path: string
}
type SearchType = 'knowledge' | 'chat'
export type SearchType = 'knowledge' | 'chat'
function activeTabToTypes(section: ActiveSection): SearchType[] {
if (section === 'knowledge') return ['knowledge']
@ -46,6 +46,9 @@ interface CommandPaletteProps {
onOpenChange: (open: boolean) => void
onSelectFile: (path: string) => void
onSelectRun: (runId: string) => void
// Overrides the sidebar-section default for the initial scope (e.g. the
// knowledge view opens search scoped to knowledge).
defaultScope?: SearchType
}
export function CommandPalette({
@ -53,6 +56,7 @@ export function CommandPalette({
onOpenChange,
onSelectFile,
onSelectRun,
defaultScope,
}: CommandPaletteProps) {
const { activeSection } = useSidebarSection()
const searchInputRef = useRef<HTMLInputElement>(null)
@ -61,7 +65,7 @@ export function CommandPalette({
const [results, setResults] = useState<SearchResult[]>([])
const [isSearching, setIsSearching] = useState(false)
const [activeTypes, setActiveTypes] = useState<Set<SearchType>>(
() => new Set(activeTabToTypes(activeSection))
() => new Set(defaultScope ? [defaultScope] : activeTabToTypes(activeSection))
)
const debouncedQuery = useDebounce(query, 250)
@ -69,9 +73,9 @@ export function CommandPalette({
useEffect(() => {
if (open) {
setQuery('')
setActiveTypes(new Set(activeTabToTypes(activeSection)))
setActiveTypes(new Set(defaultScope ? [defaultScope] : activeTabToTypes(activeSection)))
}
}, [open, activeSection])
}, [open, activeSection, defaultScope])
useEffect(() => {
if (!open) return

View file

@ -2,7 +2,7 @@
import * as React from "react"
import { useState, useEffect, useCallback, useMemo } from "react"
import { Server, Key, Shield, Palette, Monitor, Sun, Moon, Loader2, CheckCircle2, Plus, X, Wrench, Search, ChevronRight, Link2, Tags, Mail, BookOpen, User, Plug } from "lucide-react"
import { Server, Key, Shield, Palette, Monitor, Sun, Moon, Loader2, CheckCircle2, Plus, X, Wrench, Search, ChevronRight, Link2, Tags, Mail, BookOpen, User, Plug, HelpCircle, MessageCircle, Bug, Terminal, AlertTriangle, RefreshCw } from "lucide-react"
import {
Dialog,
@ -11,6 +11,7 @@ import {
} from "@/components/ui/dialog"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Separator } from "@/components/ui/separator"
import {
Select,
SelectContent,
@ -25,7 +26,7 @@ import { toast } from "sonner"
import { AccountSettings } from "@/components/settings/account-settings"
import { ConnectedAccountsSettings } from "@/components/settings/connected-accounts-settings"
type ConfigTab = "account" | "connected-accounts" | "models" | "mcp" | "security" | "appearance" | "tools" | "note-tagging"
type ConfigTab = "account" | "connections" | "models" | "mcp" | "security" | "code-mode" | "appearance" | "note-tagging" | "help"
interface TabConfig {
id: ConfigTab
@ -43,10 +44,10 @@ const tabs: TabConfig[] = [
description: "Manage your Rowboat account",
},
{
id: "connected-accounts",
label: "Connected Accounts",
id: "connections",
label: "Connections",
icon: Plug,
description: "Manage connected services",
description: "Manage accounts and tools",
},
{
id: "models",
@ -69,18 +70,18 @@ const tabs: TabConfig[] = [
path: "config/security.json",
description: "Configure allowed shell commands",
},
{
id: "code-mode",
label: "Code Mode",
icon: Terminal,
description: "Delegate coding tasks to Claude Code or Codex",
},
{
id: "appearance",
label: "Appearance",
icon: Palette,
description: "Customize the look and feel",
},
{
id: "tools",
label: "Tools Library",
icon: Wrench,
description: "Browse and enable toolkits",
},
{
id: "note-tagging",
label: "Note Tagging",
@ -88,10 +89,93 @@ const tabs: TabConfig[] = [
path: "config/tags.json",
description: "Configure tags for notes and emails",
},
{
id: "help",
label: "Help",
icon: HelpCircle,
description: "Get help and support",
},
]
interface SettingsDialogProps {
children: React.ReactNode
/** Optional trigger element. Omit when controlling `open` externally. */
children?: React.ReactNode
/** Tab to open on when the dialog is shown. Defaults to "account". */
defaultTab?: ConfigTab
/** Controlled open state. When provided, the dialog is fully controlled. */
open?: boolean
onOpenChange?: (open: boolean) => void
}
// --- Help & Support tab ---
function HelpSettings() {
return (
<div className="space-y-4">
<div>
<h4 className="text-sm font-medium">Help &amp; Support</h4>
<p className="text-xs text-muted-foreground mt-0.5">Get help from our community</p>
</div>
<Button
variant="outline"
className="w-full justify-start gap-3 h-auto py-3"
onClick={() => window.open("https://github.com/rowboatlabs/rowboat/issues/new", "_blank")}
>
<div className="flex size-8 items-center justify-center rounded-md bg-destructive/10">
<Bug className="size-4 text-destructive" />
</div>
<div className="flex flex-col items-start">
<span className="text-sm font-medium">Report a bug</span>
<span className="text-xs text-muted-foreground">Send feedback to the Rowboat team</span>
</div>
</Button>
<Button
variant="outline"
className="w-full justify-start gap-3 h-auto py-3"
onClick={() => window.open("https://discord.com/invite/wajrgmJQ6b", "_blank")}
>
<div className="flex size-8 items-center justify-center rounded-md bg-[#5865F2]">
<MessageCircle className="size-4 text-white" />
</div>
<div className="flex flex-col items-start">
<span className="text-sm font-medium">Join our Discord</span>
<span className="text-xs text-muted-foreground">Chat with the community</span>
</div>
</Button>
<Button
variant="outline"
className="w-full justify-start gap-3 h-auto py-3"
onClick={() => window.open("mailto:contact@rowboatlabs.com", "_blank")}
>
<div className="flex size-8 items-center justify-center rounded-md bg-muted">
<Mail className="size-4" />
</div>
<div className="flex flex-col items-start">
<span className="text-sm font-medium">Contact us</span>
<span className="text-xs text-muted-foreground">contact@rowboatlabs.com</span>
</div>
</Button>
<div className="flex gap-3 text-xs text-muted-foreground">
<a
href="https://www.rowboatlabs.com/terms-of-service"
target="_blank"
rel="noopener noreferrer"
className="hover:text-foreground transition-colors"
>
Terms of Service
</a>
<span>·</span>
<a
href="https://www.rowboatlabs.com/privacy-policy"
target="_blank"
rel="noopener noreferrer"
className="hover:text-foreground transition-colors"
>
Privacy Policy
</a>
</div>
</div>
)
}
// --- Theme option for Appearance tab ---
@ -1570,11 +1654,208 @@ function NoteTaggingSettings({ dialogOpen }: { dialogOpen: boolean }) {
)
}
// --- Code Mode Settings ---
type AgentStatus = { installed: boolean; signedIn: boolean }
type CodeModeAgentStatus = { claude: AgentStatus; codex: AgentStatus }
function AgentStatusRow({
name,
installLink,
signInCommand,
status,
}: {
name: string
installLink: string
signInCommand: string
status: AgentStatus | null
}) {
const ready = status?.installed && status?.signedIn
const needsSignInOnly = status?.installed && !status?.signedIn
return (
<div className="rounded-md border px-3 py-2.5 flex items-center gap-3">
<Terminal className="size-4 text-muted-foreground shrink-0" />
<div className="flex-1 min-w-0">
<div className="text-sm font-medium">{name}</div>
<div className="text-xs text-muted-foreground mt-0.5 flex items-center gap-3">
<span className={cn("inline-flex items-center gap-1", status?.installed ? "text-green-600" : "text-muted-foreground")}>
{status?.installed ? <CheckCircle2 className="size-3" /> : <X className="size-3" />}
Installed
</span>
<span className={cn("inline-flex items-center gap-1", status?.signedIn ? "text-green-600" : "text-muted-foreground")}>
{status?.signedIn ? <CheckCircle2 className="size-3" /> : <X className="size-3" />}
Signed in
</span>
</div>
</div>
{ready ? (
<span className="rounded-full bg-green-500/10 px-2 py-0.5 text-[10px] font-medium leading-none text-green-600">
Ready
</span>
) : needsSignInOnly ? (
<span className="text-xs text-muted-foreground shrink-0">
Run <code className="rounded bg-muted px-1 py-0.5 font-mono text-[11px] text-foreground">{signInCommand}</code>
</span>
) : (
<a
href={installLink}
target="_blank"
rel="noopener noreferrer"
className="text-xs text-primary hover:underline shrink-0"
>
Install &amp; sign in
</a>
)}
</div>
)
}
function CodeModeSettings({ dialogOpen }: { dialogOpen: boolean }) {
const [enabled, setEnabled] = useState(false)
const [loading, setLoading] = useState(true)
const [saving, setSaving] = useState(false)
const [status, setStatus] = useState<CodeModeAgentStatus | null>(null)
const [statusLoading, setStatusLoading] = useState(false)
const loadStatus = useCallback(async () => {
setStatusLoading(true)
try {
const result = await window.ipc.invoke("codeMode:checkAgentStatus", null)
setStatus(result)
} catch {
setStatus(null)
} finally {
setStatusLoading(false)
}
}, [])
useEffect(() => {
if (!dialogOpen) return
let cancelled = false
async function load() {
setLoading(true)
try {
const result = await window.ipc.invoke("codeMode:getConfig", null)
if (!cancelled) setEnabled(result.enabled)
} catch {
if (!cancelled) setEnabled(false)
} finally {
if (!cancelled) setLoading(false)
}
}
load()
loadStatus()
return () => { cancelled = true }
}, [dialogOpen, loadStatus])
const handleToggle = useCallback(async (next: boolean) => {
setSaving(true)
setEnabled(next)
try {
await window.ipc.invoke("codeMode:setConfig", { enabled: next })
window.dispatchEvent(new Event("code-mode-config-changed"))
toast.success(next ? "Code mode enabled" : "Code mode disabled")
} catch {
setEnabled(!next)
toast.error("Failed to update code mode")
} finally {
setSaving(false)
}
}, [])
const anyReady = status?.claude.installed && status?.claude.signedIn
|| status?.codex.installed && status?.codex.signedIn
if (loading) {
return (
<div className="h-full flex items-center justify-center text-muted-foreground text-sm">
<Loader2 className="size-4 animate-spin mr-2" />
Loading...
</div>
)
}
return (
<div className="space-y-5">
<div className="space-y-2 text-sm text-muted-foreground leading-relaxed">
<p>
<strong className="text-foreground">Code mode</strong> lets the assistant delegate coding tasks
to <strong className="text-foreground">Claude Code</strong> or <strong className="text-foreground">Codex</strong> running
on your machine. Pick the agent inline from the composer; the assistant calls it via
<code className="mx-1 rounded bg-muted px-1 py-0.5 text-[11px]">acpx</code>
and streams results back into chat.
</p>
<p>
Requires an active <strong className="text-foreground">Claude Code</strong> subscription or
a <strong className="text-foreground">ChatGPT/Codex</strong> subscription. You can have one or both.
</p>
</div>
<div className="space-y-2">
<div className="flex items-center justify-between">
<span className="text-xs font-medium text-muted-foreground uppercase tracking-wider">Agent status</span>
<button
onClick={() => { void loadStatus() }}
disabled={statusLoading}
className="inline-flex items-center gap-1 text-xs text-muted-foreground hover:text-foreground transition-colors"
>
{statusLoading ? <Loader2 className="size-3 animate-spin" /> : <RefreshCw className="size-3" />}
Re-check
</button>
</div>
<div className="space-y-2">
<AgentStatusRow
name="Claude Code"
installLink="https://claude.ai/code"
signInCommand="claude login"
status={status?.claude ?? null}
/>
<AgentStatusRow
name="Codex"
installLink="https://developers.openai.com/codex/cli"
signInCommand="codex login"
status={status?.codex ?? null}
/>
</div>
</div>
<div className="rounded-md border px-3 py-3 flex items-start gap-3">
<div className="flex-1 min-w-0">
<div className="text-sm font-medium">Enable code mode</div>
<div className="text-xs text-muted-foreground mt-0.5">
Shows the code mode chip in the composer and lets the assistant delegate to your installed agents.
</div>
</div>
<Switch
checked={enabled}
onCheckedChange={handleToggle}
disabled={saving}
/>
</div>
{enabled && status && !anyReady && (
<div className="rounded-md border border-amber-500/40 bg-amber-50/60 dark:bg-amber-950/20 px-3 py-2.5 flex items-start gap-2 text-xs">
<AlertTriangle className="size-4 text-amber-600 dark:text-amber-500 shrink-0 mt-0.5" />
<div className="text-amber-900 dark:text-amber-200">
Neither Claude Code nor Codex is ready. Install at least one and sign in with a subscription
account, then click Re-check.
</div>
</div>
)}
</div>
)
}
// --- Main Settings Dialog ---
export function SettingsDialog({ children }: SettingsDialogProps) {
const [open, setOpen] = useState(false)
const [activeTab, setActiveTab] = useState<ConfigTab>("account")
export function SettingsDialog({ children, defaultTab = "account", open: controlledOpen, onOpenChange }: SettingsDialogProps) {
const [internalOpen, setInternalOpen] = useState(false)
const open = controlledOpen ?? internalOpen
const setOpen = useCallback((next: boolean) => {
if (onOpenChange) onOpenChange(next)
else setInternalOpen(next)
}, [onOpenChange])
const [activeTab, setActiveTab] = useState<ConfigTab>(defaultTab)
const [content, setContent] = useState("")
const [originalContent, setOriginalContent] = useState("")
const [loading, setLoading] = useState(false)
@ -1582,6 +1863,11 @@ export function SettingsDialog({ children }: SettingsDialogProps) {
const [error, setError] = useState<string | null>(null)
const [rowboatConnected, setRowboatConnected] = useState(false)
// Reset to the requested default tab each time the dialog is opened
useEffect(() => {
if (open) setActiveTab(defaultTab)
}, [open, defaultTab])
// Check if user is signed in to Rowboat
useEffect(() => {
if (!open) return
@ -1607,7 +1893,7 @@ export function SettingsDialog({ children }: SettingsDialogProps) {
}
const loadConfig = useCallback(async (tab: ConfigTab) => {
if (tab === "appearance" || tab === "models" || tab === "note-tagging" || tab === "account" || tab === "connected-accounts") return
if (tab === "appearance" || tab === "models" || tab === "note-tagging" || tab === "account" || tab === "connections" || tab === "help" || tab === "code-mode") return
const tabConfig = tabs.find((t) => t.id === tab)!
if (!tabConfig.path) return
setLoading(true)
@ -1673,7 +1959,7 @@ export function SettingsDialog({ children }: SettingsDialogProps) {
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>{children}</DialogTrigger>
{children && <DialogTrigger asChild>{children}</DialogTrigger>}
<DialogContent
className="max-w-[900px]! w-[900px] h-[600px] p-0 gap-0 overflow-hidden"
>
@ -1715,11 +2001,21 @@ export function SettingsDialog({ children }: SettingsDialogProps) {
</div>
{/* Content */}
<div className={cn("flex-1 p-4 min-h-0", (activeTab === "models" || activeTab === "tools" || activeTab === "account" || activeTab === "connected-accounts") ? "overflow-y-auto" : activeTab === "note-tagging" ? "overflow-hidden flex flex-col" : "overflow-hidden")}>
<div className={cn("flex-1 p-4 min-h-0", (activeTab === "models" || activeTab === "connections" || activeTab === "account" || activeTab === "code-mode") ? "overflow-y-auto" : activeTab === "note-tagging" ? "overflow-hidden flex flex-col" : "overflow-hidden")}>
{activeTab === "account" ? (
<AccountSettings dialogOpen={open} />
) : activeTab === "connected-accounts" ? (
<ConnectedAccountsSettings dialogOpen={open} />
) : activeTab === "connections" ? (
<div className="space-y-6">
<div className="space-y-2">
<h4 className="text-sm font-semibold">Primary accounts</h4>
<ConnectedAccountsSettings dialogOpen={open} />
</div>
<Separator />
<div className="space-y-2">
<h4 className="text-sm font-semibold">Library</h4>
<ToolsLibrarySettings dialogOpen={open} rowboatConnected={rowboatConnected} />
</div>
</div>
) : activeTab === "models" ? (
rowboatConnected
? <RowboatModelSettings dialogOpen={open} />
@ -1728,8 +2024,10 @@ export function SettingsDialog({ children }: SettingsDialogProps) {
<NoteTaggingSettings dialogOpen={open} />
) : activeTab === "appearance" ? (
<AppearanceSettings />
) : activeTab === "tools" ? (
<ToolsLibrarySettings dialogOpen={open} rowboatConnected={rowboatConnected} />
) : activeTab === "help" ? (
<HelpSettings />
) : activeTab === "code-mode" ? (
<CodeModeSettings dialogOpen={open} />
) : loading ? (
<div className="h-full flex items-center justify-center text-muted-foreground text-sm">
Loading...

View file

@ -17,11 +17,44 @@ import {
import { Separator } from "@/components/ui/separator"
import { useBilling } from "@/hooks/useBilling"
import { toast } from "sonner"
import type { BillingUsageBucket } from "@x/shared/dist/billing.js"
interface AccountSettingsProps {
dialogOpen: boolean
}
function formatPlanName(plan: string | null | undefined) {
if (!plan) return 'No Plan'
return `${plan.charAt(0).toUpperCase()}${plan.slice(1)} Plan`
}
function CreditUsageBar({ label, bucket, helper }: {
label: string
bucket: BillingUsageBucket
helper?: string
}) {
const pct = bucket.sanctionedCredits > 0
? Math.min(100, Math.max(0, Math.round((bucket.usedCredits / bucket.sanctionedCredits) * 100)))
: 0
return (
<div className="space-y-1.5">
<div className="flex items-start justify-between gap-3">
<div>
<p className="text-xs font-medium text-muted-foreground">{label}</p>
{helper ? <p className="text-[11px] text-muted-foreground">{helper}</p> : null}
</div>
<p className="shrink-0 text-xs font-medium tabular-nums">
{pct}%
</p>
</div>
<div className="h-2 overflow-hidden rounded-full bg-muted">
<div className="h-full rounded-full bg-primary transition-all" style={{ width: `${pct}%` }} />
</div>
</div>
)
}
export function AccountSettings({ dialogOpen }: AccountSettingsProps) {
const [isRowboatConnected, setIsRowboatConnected] = useState(false)
const [connectionLoading, setConnectionLoading] = useState(true)
@ -164,7 +197,7 @@ export function AccountSettings({ dialogOpen }: AccountSettingsProps) {
<div className="flex items-center justify-between">
<div>
<p className="text-sm font-medium capitalize">
{billing.subscriptionPlan ? `${billing.subscriptionPlan} Plan` : 'No Plan'}
{formatPlanName(billing.subscriptionPlan)}
</p>
{billing.subscriptionStatus === 'trialing' && billing.trialExpiresAt ? (() => {
const days = Math.max(0, Math.ceil((new Date(billing.trialExpiresAt).getTime() - Date.now()) / (1000 * 60 * 60 * 24)))
@ -179,14 +212,19 @@ export function AccountSettings({ dialogOpen }: AccountSettingsProps) {
{!billing.subscriptionPlan && (
<p className="text-xs text-muted-foreground">Subscribe to access AI features</p>
)}
{billing.subscriptionPlan === 'free' && (
<p className="text-xs text-muted-foreground">Free usage resets daily at 00:00 UTC.</p>
)}
</div>
<Button variant="outline" size="sm" onClick={() => appUrl && window.open(`${appUrl}?intent=upgrade`)}>
{!billing.subscriptionPlan ? 'Subscribe' : billing.subscriptionPlan === 'free' ? 'Upgrade' : 'Change plan'}
</Button>
</div>
<div className="space-y-3 border-t pt-3">
<CreditUsageBar label="Plan usage" bucket={billing.monthly} />
<CreditUsageBar
label="Daily use"
bucket={billing.daily}
helper="Daily usage resets at 00:00 UTC"
/>
</div>
</div>
) : (
<p className="text-xs text-muted-foreground">Unable to load plan details</p>

View file

@ -26,10 +26,10 @@ export function ConnectedAccountsSettings({ dialogOpen }: ConnectedAccountsSetti
return (
<div
key={provider}
className="flex items-center justify-between gap-3 rounded-lg px-4 py-3 hover:bg-accent/50 transition-colors"
className="flex items-center justify-between gap-2 rounded-md px-3 py-2 hover:bg-accent/50 transition-colors"
>
<div className="flex items-center gap-3 min-w-0">
<div className="flex size-9 items-center justify-center rounded-lg bg-muted">
<div className="flex items-center gap-2.5 min-w-0">
<div className="flex size-8 items-center justify-center rounded-md bg-muted">
{icon}
</div>
<div className="flex flex-col min-w-0">
@ -119,15 +119,15 @@ export function ConnectedAccountsSettings({ dialogOpen }: ConnectedAccountsSetti
{/* Email & Calendar Section */}
{(c.useComposioForGoogle || c.useComposioForGoogleCalendar || c.providers.includes('google')) && (
<>
<div className="px-4 py-2">
<div className="px-3 pt-1 pb-0.5">
<span className="text-xs font-medium text-muted-foreground uppercase tracking-wider">
Email & Calendar
</span>
</div>
{c.useComposioForGoogle ? (
<div className="flex items-center justify-between gap-3 rounded-lg px-4 py-3 hover:bg-accent/50 transition-colors">
<div className="flex items-center gap-3 min-w-0">
<div className="flex size-9 items-center justify-center rounded-lg bg-muted">
<div className="flex items-center justify-between gap-2 rounded-md px-3 py-2 hover:bg-accent/50 transition-colors">
<div className="flex items-center gap-2.5 min-w-0">
<div className="flex size-8 items-center justify-center rounded-md bg-muted">
<Mail className="size-4" />
</div>
<div className="flex flex-col min-w-0">
@ -174,9 +174,9 @@ export function ConnectedAccountsSettings({ dialogOpen }: ConnectedAccountsSetti
c.providers.includes('google') && renderOAuthProvider('google', 'Google', <Mail className="size-4" />, 'Sync emails and calendar')
)}
{c.useComposioForGoogleCalendar && (
<div className="flex items-center justify-between gap-3 rounded-lg px-4 py-3 hover:bg-accent/50 transition-colors">
<div className="flex items-center gap-3 min-w-0">
<div className="flex size-9 items-center justify-center rounded-lg bg-muted">
<div className="flex items-center justify-between gap-2 rounded-md px-3 py-2 hover:bg-accent/50 transition-colors">
<div className="flex items-center gap-2.5 min-w-0">
<div className="flex size-8 items-center justify-center rounded-md bg-muted">
<Calendar className="size-4" />
</div>
<div className="flex flex-col min-w-0">
@ -220,14 +220,14 @@ export function ConnectedAccountsSettings({ dialogOpen }: ConnectedAccountsSetti
</div>
</div>
)}
<Separator className="my-3" />
<Separator className="my-2" />
</>
)}
{/* Meeting Notes Section */}
{c.providers.includes('fireflies-ai') && (
<>
<div className="px-4 py-2">
<div className="px-3 pt-1 pb-0.5">
<span className="text-xs font-medium text-muted-foreground uppercase tracking-wider">
Meeting Notes
</span>

File diff suppressed because it is too large Load diff

View file

@ -380,7 +380,7 @@ function SidebarContent({ className, ...props }: React.ComponentProps<"div">) {
data-slot="sidebar-content"
data-sidebar="content"
className={cn(
"flex min-h-0 flex-1 flex-col gap-2 overflow-auto group-data-[collapsible=icon]:overflow-hidden",
"flex min-h-0 flex-1 flex-col gap-1 overflow-auto group-data-[collapsible=icon]:overflow-hidden",
className
)}
{...props}
@ -393,7 +393,7 @@ function SidebarGroup({ className, ...props }: React.ComponentProps<"div">) {
<div
data-slot="sidebar-group"
data-sidebar="group"
className={cn("relative flex w-full min-w-0 flex-col p-2", className)}
className={cn("relative flex w-full min-w-0 flex-col px-2 py-1", className)}
{...props}
/>
)
@ -462,7 +462,7 @@ function SidebarMenu({ className, ...props }: React.ComponentProps<"ul">) {
<ul
data-slot="sidebar-menu"
data-sidebar="menu"
className={cn("flex w-full min-w-0 flex-col gap-1", className)}
className={cn("flex w-full min-w-0 flex-col gap-0", className)}
{...props}
/>
)

View file

@ -0,0 +1,531 @@
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import {
ChevronRight,
Copy,
File as FileIcon,
FilePlus,
Folder as FolderIcon,
FolderOpen,
FolderPlus,
Home,
Pencil,
Plus,
Trash2,
UploadCloud,
} from 'lucide-react'
import { Button } from '@/components/ui/button'
import {
ContextMenu,
ContextMenuContent,
ContextMenuItem,
ContextMenuSeparator,
ContextMenuTrigger,
} from '@/components/ui/context-menu'
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu'
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog'
import { Input } from '@/components/ui/input'
import { toast } from '@/lib/toast'
import { cn } from '@/lib/utils'
const WORKSPACE_ROOT = 'knowledge/Workspace'
interface TreeNode {
path: string
name: string
kind: 'file' | 'dir'
children?: TreeNode[]
}
type WorkspaceActions = {
remove: (path: string) => Promise<void>
copyPath: (path: string) => void
revealInFileManager: (path: string, isDir: boolean) => void
}
type WorkspaceViewProps = {
tree: TreeNode[]
initialPath?: string | null
actions: WorkspaceActions
onOpenNote: (path: string) => void
onCreateWorkspace: (name: string) => Promise<void>
}
function getFileManagerName(): string {
if (typeof navigator === 'undefined') return 'File Manager'
const platform = navigator.platform.toLowerCase()
if (platform.includes('mac')) return 'Finder'
if (platform.includes('win')) return 'Explorer'
return 'File Manager'
}
function findNode(nodes: TreeNode[] | undefined, path: string): TreeNode | null {
if (!nodes) return null
for (const node of nodes) {
if (node.path === path) return node
if (node.kind === 'dir' && path.startsWith(`${node.path}/`)) {
const found = findNode(node.children, path)
if (found) return found
}
}
return null
}
function countChildren(node: TreeNode | null): number {
if (!node || node.kind !== 'dir' || !node.children) return 0
return node.children.length
}
async function uniqueChildPath(parent: string, name: string): Promise<string> {
const dot = name.lastIndexOf('.')
const base = dot > 0 ? name.slice(0, dot) : name
const ext = dot > 0 ? name.slice(dot) : ''
let candidate = `${parent}/${name}`
let i = 1
while ((await window.ipc.invoke('workspace:exists', { path: candidate })).exists) {
candidate = `${parent}/${base} (${i})${ext}`
i += 1
}
return candidate
}
function readFileAsBase64(file: File): Promise<string> {
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onload = () => {
const result = reader.result as string
resolve(result.split(',')[1] ?? '')
}
reader.onerror = reject
reader.readAsDataURL(file)
})
}
export function WorkspaceView({ tree, initialPath, actions, onOpenNote, onCreateWorkspace }: WorkspaceViewProps) {
const [currentPath, setCurrentPath] = useState<string>(initialPath || WORKSPACE_ROOT)
const [addOpen, setAddOpen] = useState(false)
const [newName, setNewName] = useState('')
const [creating, setCreating] = useState(false)
const [error, setError] = useState<string | null>(null)
const [renameTarget, setRenameTarget] = useState<string | null>(null)
const [renameValue, setRenameValue] = useState('')
const [isDraggingOver, setIsDraggingOver] = useState(false)
const [uploading, setUploading] = useState(false)
const dragDepthRef = useRef(0)
const filesInputRef = useRef<HTMLInputElement | null>(null)
const folderInputRef = useRef<HTMLInputElement | null>(null)
useEffect(() => {
if (initialPath) setCurrentPath(initialPath)
}, [initialPath])
const isRoot = currentPath === WORKSPACE_ROOT
const fileManagerName = getFileManagerName()
const currentNode = useMemo(() => findNode(tree, currentPath), [tree, currentPath])
const items = useMemo<TreeNode[]>(() => {
const children = currentNode?.children ?? []
const filtered = isRoot ? children.filter((c) => c.kind === 'dir') : children
return [...filtered].sort((a, b) => {
if (a.kind !== b.kind) return a.kind === 'dir' ? -1 : 1
return a.name.localeCompare(b.name)
})
}, [currentNode, isRoot])
const breadcrumbs = useMemo(() => {
if (isRoot) return [] as { path: string; name: string }[]
const rel = currentPath.slice(WORKSPACE_ROOT.length + 1)
const parts = rel.split('/').filter(Boolean)
let acc = WORKSPACE_ROOT
return parts.map((seg) => {
acc = `${acc}/${seg}`
return { path: acc, name: seg }
})
}, [currentPath, isRoot])
const handleItemClick = useCallback(
(item: TreeNode) => {
if (renameTarget) return
if (item.kind === 'dir') {
setCurrentPath(item.path)
} else {
onOpenNote(item.path)
}
},
[onOpenNote, renameTarget],
)
const beginRename = useCallback((item: TreeNode) => {
setRenameTarget(item.path)
setRenameValue(item.name)
}, [])
const commitRename = useCallback(async () => {
if (!renameTarget) return
const node = items.find((i) => i.path === renameTarget)
const trimmed = renameValue.trim()
setRenameTarget(null)
if (!node || !trimmed || trimmed === node.name || trimmed.includes('/')) return
const parent = renameTarget.slice(0, renameTarget.lastIndexOf('/'))
try {
await window.ipc.invoke('workspace:rename', { from: renameTarget, to: `${parent}/${trimmed}` })
toast('Renamed', 'success')
} catch {
toast('Failed to rename', 'error')
}
}, [renameTarget, renameValue, items])
const handleDelete = useCallback(async (item: TreeNode) => {
try {
await actions.remove(item.path)
toast('Moved to trash', 'success')
} catch {
toast('Failed to delete', 'error')
}
}, [actions])
const uploadFiles = useCallback(async (files: FileList | File[], preserveStructure = false) => {
const list = Array.from(files)
if (list.length === 0) return
setUploading(true)
try {
for (const file of list) {
const data = await readFileAsBase64(file)
const rel = (file as File & { webkitRelativePath?: string }).webkitRelativePath
const target = preserveStructure && rel
? `${currentPath}/${rel}`
: await uniqueChildPath(currentPath, file.name)
await window.ipc.invoke('workspace:writeFile', {
path: target,
data,
opts: { encoding: 'base64', mkdirp: true },
})
}
toast(list.length === 1 ? 'Added' : `${list.length} items added`, 'success')
} catch (err) {
console.error('Failed to add files:', err)
toast('Failed to add', 'error')
} finally {
setUploading(false)
}
}, [currentPath])
// Drag-and-drop (only inside a workspace folder, not at the root grid).
// stopPropagation keeps the drop from also reaching the copilot's
// document-level drop listener when it lands on the workspace area.
const dropEnabled = !isRoot
const handleDragEnter = useCallback((e: React.DragEvent) => {
if (!dropEnabled) return
if (!Array.from(e.dataTransfer.types).includes('Files')) return
e.preventDefault()
e.stopPropagation()
dragDepthRef.current += 1
setIsDraggingOver(true)
}, [dropEnabled])
const handleDragOver = useCallback((e: React.DragEvent) => {
if (!dropEnabled) return
if (!Array.from(e.dataTransfer.types).includes('Files')) return
e.preventDefault()
e.stopPropagation()
e.dataTransfer.dropEffect = 'copy'
}, [dropEnabled])
const handleDragLeave = useCallback((e: React.DragEvent) => {
if (!dropEnabled) return
e.preventDefault()
e.stopPropagation()
dragDepthRef.current -= 1
if (dragDepthRef.current <= 0) {
dragDepthRef.current = 0
setIsDraggingOver(false)
}
}, [dropEnabled])
const handleDrop = useCallback((e: React.DragEvent) => {
if (!dropEnabled) return
e.preventDefault()
e.stopPropagation()
dragDepthRef.current = 0
setIsDraggingOver(false)
if (e.dataTransfer.files?.length) void uploadFiles(e.dataTransfer.files)
}, [dropEnabled, uploadFiles])
const resetAddDialog = useCallback(() => {
setNewName('')
setError(null)
setCreating(false)
}, [])
const handleCreate = useCallback(async () => {
const trimmed = newName.trim()
if (!trimmed) {
setError('Name is required')
return
}
if (trimmed.includes('/')) {
setError('Name cannot contain "/"')
return
}
setCreating(true)
setError(null)
try {
await onCreateWorkspace(trimmed)
setAddOpen(false)
resetAddDialog()
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to create workspace')
setCreating(false)
}
}, [newName, onCreateWorkspace, resetAddDialog])
return (
<div className="flex h-full flex-col overflow-hidden">
<div className="flex shrink-0 items-center justify-between gap-3 border-b border-border px-6 py-4">
<div className="flex min-w-0 items-center gap-1 text-sm">
<button
type="button"
onClick={() => setCurrentPath(WORKSPACE_ROOT)}
className={cn(
'inline-flex items-center gap-1.5 rounded-md px-2 py-1 transition-colors',
isRoot ? 'text-foreground' : 'text-muted-foreground hover:text-foreground hover:bg-accent',
)}
>
<Home className="size-4" />
<span className="font-medium">Workspace</span>
</button>
{breadcrumbs.map((crumb, idx) => {
const isLast = idx === breadcrumbs.length - 1
return (
<span key={crumb.path} className="flex items-center gap-1">
<ChevronRight className="size-4 text-muted-foreground/60" />
{isLast ? (
<span className="rounded-md px-2 py-1 font-medium text-foreground truncate">
{crumb.name}
</span>
) : (
<button
type="button"
onClick={() => setCurrentPath(crumb.path)}
className="rounded-md px-2 py-1 text-muted-foreground transition-colors hover:bg-accent hover:text-foreground truncate"
>
{crumb.name}
</button>
)}
</span>
)
})}
</div>
{isRoot ? (
<Button size="sm" onClick={() => setAddOpen(true)}>
<Plus className="size-4" />
Add workspace
</Button>
) : (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button size="sm">
<Plus className="size-4" />
Add
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuItem onClick={() => filesInputRef.current?.click()}>
<FilePlus className="mr-2 size-4" />
Add files
</DropdownMenuItem>
<DropdownMenuItem onClick={() => folderInputRef.current?.click()}>
<FolderPlus className="mr-2 size-4" />
Add folder
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
)}
</div>
<input
ref={filesInputRef}
type="file"
multiple
className="hidden"
onChange={(e) => {
if (e.target.files?.length) void uploadFiles(e.target.files, false)
e.target.value = ''
}}
/>
<input
ref={folderInputRef}
type="file"
// @ts-expect-error non-standard but supported in Chromium/Electron
webkitdirectory=""
directory=""
multiple
className="hidden"
onChange={(e) => {
if (e.target.files?.length) void uploadFiles(e.target.files, true)
e.target.value = ''
}}
/>
<div
className="relative flex-1 overflow-y-auto px-6 py-6"
onDragEnter={handleDragEnter}
onDragOver={handleDragOver}
onDragLeave={handleDragLeave}
onDrop={handleDrop}
>
{items.length === 0 ? (
<div className="flex h-full flex-col items-center justify-center gap-3 text-center text-muted-foreground">
<FolderIcon className="size-10 opacity-50" />
<div className="text-sm">
{isRoot
? 'No workspaces yet. Create one to get started.'
: 'This folder is empty. Drag files in or use New note / New folder.'}
</div>
{isRoot && (
<Button size="sm" variant="outline" onClick={() => setAddOpen(true)}>
<Plus className="size-4" />
Add workspace
</Button>
)}
</div>
) : (
<div className="grid grid-cols-[repeat(auto-fill,minmax(180px,1fr))] gap-3">
{items.map((item) => {
const childCount = item.kind === 'dir' ? countChildren(item) : 0
const Icon = item.kind === 'dir' ? FolderIcon : FileIcon
const isRenaming = renameTarget === item.path
const card = (
<button
type="button"
onClick={() => handleItemClick(item)}
className="group flex w-full flex-col items-start gap-2 rounded-lg border border-border bg-card p-4 text-left transition-colors hover:border-foreground/20 hover:bg-accent"
>
<Icon className="size-6 text-muted-foreground group-hover:text-foreground" />
<div className="min-w-0 w-full">
{isRenaming ? (
<Input
autoFocus
value={renameValue}
onClick={(e) => e.stopPropagation()}
onChange={(e) => setRenameValue(e.target.value)}
onBlur={() => void commitRename()}
onKeyDown={(e) => {
e.stopPropagation()
if (e.key === 'Enter') { e.preventDefault(); void commitRename() }
else if (e.key === 'Escape') { e.preventDefault(); setRenameTarget(null) }
}}
className="h-6 text-sm"
/>
) : (
<div className="truncate text-sm font-medium">{item.name}</div>
)}
{item.kind === 'dir' && !isRenaming && (
<div className="text-xs text-muted-foreground">
{childCount} {childCount === 1 ? 'item' : 'items'}
</div>
)}
</div>
</button>
)
return (
<ContextMenu key={item.path}>
<ContextMenuTrigger asChild>{card}</ContextMenuTrigger>
<ContextMenuContent className="w-48">
<ContextMenuItem onClick={() => beginRename(item)}>
<Pencil className="mr-2 size-4" />
Rename
</ContextMenuItem>
<ContextMenuItem onClick={() => { actions.copyPath(item.path); toast('Path copied', 'success') }}>
<Copy className="mr-2 size-4" />
Copy Path
</ContextMenuItem>
<ContextMenuItem onClick={() => actions.revealInFileManager(item.path, item.kind === 'dir')}>
<FolderOpen className="mr-2 size-4" />
Show in {fileManagerName}
</ContextMenuItem>
<ContextMenuSeparator />
<ContextMenuItem variant="destructive" onClick={() => void handleDelete(item)}>
<Trash2 className="mr-2 size-4" />
Delete
</ContextMenuItem>
</ContextMenuContent>
</ContextMenu>
)
})}
</div>
)}
{dropEnabled && isDraggingOver && (
<div className="pointer-events-none absolute inset-3 z-10 flex flex-col items-center justify-center gap-2 rounded-xl border-2 border-dashed border-primary/60 bg-primary/5 text-primary">
<UploadCloud className="size-8" />
<span className="text-sm font-medium">Drop files to add to this folder</span>
</div>
)}
{uploading && (
<div className="pointer-events-none absolute bottom-4 right-4 z-10 rounded-md bg-foreground/80 px-3 py-1.5 text-xs text-background">
Adding files
</div>
)}
</div>
<Dialog
open={addOpen}
onOpenChange={(open) => {
setAddOpen(open)
if (!open) resetAddDialog()
}}
>
<DialogContent>
<DialogHeader>
<DialogTitle>New workspace</DialogTitle>
<DialogDescription>
Workspaces are top-level folders inside knowledge/Workspace.
</DialogDescription>
</DialogHeader>
<div className="grid gap-2">
<label htmlFor="workspace-name" className="text-sm font-medium">Name</label>
<Input
id="workspace-name"
value={newName}
onChange={(e) => setNewName(e.target.value)}
placeholder="e.g. Alpha"
autoFocus
onKeyDown={(e) => {
if (e.key === 'Enter' && !creating) {
e.preventDefault()
void handleCreate()
}
}}
/>
{error && <p className="text-xs text-destructive">{error}</p>}
</div>
<DialogFooter>
<Button
variant="outline"
onClick={() => {
setAddOpen(false)
resetAddDialog()
}}
disabled={creating}
>
Cancel
</Button>
<Button onClick={() => void handleCreate()} disabled={creating || !newName.trim()}>
{creating ? 'Creating…' : 'Create'}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</div>
)
}

View file

@ -1,5 +1,5 @@
import { InputRule, Node, mergeAttributes } from '@tiptap/core'
import { ensureMarkdownExtension, normalizeWikiPath, wikiLabel } from '@/lib/wiki-links'
import { ensureMarkdownExtension, normalizeWikiPath, splitWikiAlias, splitWikiFragment, wikiLabel } from '@/lib/wiki-links'
const wikiLinkInputRegex = /\[\[([^[\]]+)\]\]$/
const wikiLinkTokenRegex = /\[\[([^[\]]+)\]\]/g
@ -25,9 +25,12 @@ const replaceWikiLinksInTextNode = (textNode: Text) => {
for (const match of matches) {
const matchIndex = match.index ?? 0
const matchText = match[0] ?? ''
const rawPath = match[1]?.trim() ?? ''
const normalizedPath = rawPath ? normalizeWikiPath(rawPath) : ''
const isValidPath = normalizedPath && !normalizedPath.endsWith('/') && !normalizedPath.includes('..')
const rawLink = match[1]?.trim() ?? ''
const { label } = splitWikiAlias(rawLink)
const normalizedPath = rawLink ? normalizeWikiPath(rawLink) : ''
const { path: basePath, heading } = splitWikiFragment(normalizedPath)
const isHeadingOnlyLink = !basePath && Boolean(heading)
const isValidPath = isHeadingOnlyLink || (normalizedPath && !basePath.endsWith('/') && !basePath.includes('..'))
if (matchIndex > lastIndex) {
fragment.appendChild(document.createTextNode(text.slice(lastIndex, matchIndex)))
@ -35,7 +38,8 @@ const replaceWikiLinksInTextNode = (textNode: Text) => {
if (isValidPath) {
const el = document.createElement('wiki-link')
el.setAttribute('data-path', ensureMarkdownExtension(normalizedPath))
el.setAttribute('data-path', isHeadingOnlyLink ? normalizedPath : ensureMarkdownExtension(normalizedPath))
if (label) el.setAttribute('data-label', label)
fragment.appendChild(el)
} else {
fragment.appendChild(document.createTextNode(matchText))
@ -80,6 +84,9 @@ export const WikiLink = Node.create<WikiLinkOptions>({
path: {
default: '',
},
label: {
default: null,
},
}
},
@ -89,28 +96,34 @@ export const WikiLink = Node.create<WikiLinkOptions>({
tag: 'wiki-link[data-path]',
getAttrs: (element: Element) => ({
path: (element as HTMLElement).getAttribute('data-path') ?? '',
label: (element as HTMLElement).getAttribute('data-label'),
}),
},
{
tag: 'a[data-type="wiki-link"]',
getAttrs: (element: Element) => ({
path: (element as HTMLElement).getAttribute('data-path') ?? '',
label: (element as HTMLElement).getAttribute('data-label'),
}),
},
]
},
renderHTML({ node, HTMLAttributes }) {
const label = wikiLabel(node.attrs.path) || node.attrs.path
const label = node.attrs.label || wikiLabel(node.attrs.path) || node.attrs.path
return [
'a',
mergeAttributes(HTMLAttributes, {
'data-type': 'wiki-link',
'data-path': node.attrs.path,
'href': '#',
'class': 'wiki-link',
'aria-label': node.attrs.path,
}),
mergeAttributes(
HTMLAttributes,
{
'data-type': 'wiki-link',
'data-path': node.attrs.path,
'href': '#',
'class': 'wiki-link',
'aria-label': node.attrs.path,
},
node.attrs.label ? { 'data-label': node.attrs.label } : {}
),
label,
]
},
@ -120,7 +133,8 @@ export const WikiLink = Node.create<WikiLinkOptions>({
markdown: {
serialize(state: { write: (text: string) => void }, node: { attrs: { path?: string } }) {
const path = node.attrs.path ?? ''
state.write(`[[${path}]]`)
const label = (node.attrs as { label?: string }).label
state.write(`[[${path}${label ? `|${label}` : ''}]]`)
},
parse: {
updateDOM(element: HTMLElement) {
@ -137,14 +151,20 @@ export const WikiLink = Node.create<WikiLinkOptions>({
new InputRule({
find: wikiLinkInputRegex,
handler: ({ state, range, match }) => {
const rawPath = match[1]?.trim()
const normalizedPath = rawPath ? normalizeWikiPath(rawPath) : ''
if (!normalizedPath || normalizedPath.endsWith('/') || normalizedPath.includes('..')) return null
const rawLink = match[1]?.trim()
const { label } = splitWikiAlias(rawLink ?? '')
const normalizedPath = rawLink ? normalizeWikiPath(rawLink) : ''
const { path: basePath, heading } = splitWikiFragment(normalizedPath)
const isHeadingOnlyLink = !basePath && Boolean(heading)
if (
!normalizedPath
|| (!isHeadingOnlyLink && (basePath.endsWith('/') || basePath.includes('..')))
) return null
if (state.selection.$from.parent.type.spec.code) return null
if (state.selection.$from.marks().some((mark) => mark.type.spec.code)) return null
const finalPath = ensureMarkdownExtension(normalizedPath)
state.tr.replaceWith(range.from, range.to, this.type.create({ path: finalPath }))
const finalPath = isHeadingOnlyLink ? normalizedPath : ensureMarkdownExtension(normalizedPath)
state.tr.replaceWith(range.from, range.to, this.type.create({ path: finalPath, label }))
onCreate?.(finalPath)
},
}),

View file

@ -0,0 +1,26 @@
export const BILLING_ERROR_PATTERNS = [
{
pattern: /upgrade required/i,
title: 'A subscription is required',
subtitle: 'Get started with a plan to access AI features in Rowboat.',
cta: 'Subscribe',
},
{
pattern: /not enough credits/i,
title: "You've run out of credits",
subtitle: 'Upgrade your plan for more usage. Daily usage resets at 00:00 UTC.',
cta: 'Upgrade plan',
},
{
pattern: /subscription not active/i,
title: 'Your subscription is inactive',
subtitle: 'Reactivate your subscription to continue using AI features.',
cta: 'Reactivate',
},
] as const
export type BillingErrorMatch = (typeof BILLING_ERROR_PATTERNS)[number]
export function matchBillingError(message: string): BillingErrorMatch | null {
return BILLING_ERROR_PATTERNS.find(({ pattern }) => pattern.test(message)) ?? null
}

View file

@ -1,7 +1,23 @@
/**
* Matches a video-conference join URL for the providers we support (Zoom,
* Microsoft Teams, Google Meet). Captures the full URL up to the first
* whitespace, quote, or angle/round/square bracket.
*/
const MEETING_URL_RE =
/https?:\/\/(?:[a-z0-9-]+\.)*(?:zoom\.us|zoomgov\.com|teams\.microsoft\.com|teams\.live\.com|meet\.google\.com)\/[^\s"'<>)\]]+/i
function findMeetingUrl(value: unknown): string | undefined {
if (typeof value !== 'string') return undefined
const match = MEETING_URL_RE.exec(value)
// Calendar descriptions are often HTML, so decode &amp; back to & in the URL.
return match ? match[0].replace(/&amp;/g, '&') : undefined
}
/**
* Extract a video conference link from raw Google Calendar event JSON.
* Checks conferenceData.entryPoints (video type), hangoutLink, then falls back
* to a top-level conferenceLink if present.
* Checks conferenceData.entryPoints (video type), hangoutLink, a top-level
* conferenceLink, then falls back to scanning the location/description for a
* known meeting URL (Zoom, Microsoft Teams, Google Meet).
*/
export function extractConferenceLink(raw: Record<string, unknown>): string | undefined {
const confData = raw.conferenceData as { entryPoints?: { entryPointType?: string; uri?: string }[] } | undefined
@ -11,5 +27,5 @@ export function extractConferenceLink(raw: Record<string, unknown>): string | un
}
if (typeof raw.hangoutLink === 'string') return raw.hangoutLink
if (typeof raw.conferenceLink === 'string') return raw.conferenceLink
return undefined
return findMeetingUrl(raw.location) ?? findMeetingUrl(raw.description)
}

View file

@ -479,19 +479,19 @@ export const getComposioConnectCardData = (tool: ToolCall): ComposioConnectCardD
// Human-friendly display names for builtin tools
const TOOL_DISPLAY_NAMES: Record<string, string> = {
'workspace-readFile': 'Reading file',
'workspace-writeFile': 'Writing file',
'workspace-edit': 'Editing file',
'workspace-readdir': 'Reading directory',
'workspace-exists': 'Checking path',
'workspace-stat': 'Getting file info',
'workspace-glob': 'Finding files',
'workspace-grep': 'Searching files',
'workspace-mkdir': 'Creating directory',
'workspace-rename': 'Renaming',
'workspace-copy': 'Copying file',
'workspace-remove': 'Removing',
'workspace-getRoot': 'Getting workspace root',
'file-readText': 'Reading file',
'file-writeText': 'Writing file',
'file-editText': 'Editing file',
'file-list': 'Reading directory',
'file-exists': 'Checking path',
'file-stat': 'Getting file info',
'file-glob': 'Finding files',
'file-grep': 'Searching files',
'file-mkdir': 'Creating directory',
'file-rename': 'Renaming',
'file-copy': 'Copying file',
'file-remove': 'Removing',
'file-getRoot': 'Getting file root',
'loadSkill': 'Loading skill',
'parseFile': 'Parsing file',
'LLMParse': 'Extracting content',
@ -517,9 +517,41 @@ const TOOL_DISPLAY_NAMES: Record<string, string> = {
* For builtin tools, returns a static friendly name (e.g., "Reading file").
* Falls back to the raw tool name if no mapping exists.
*/
// Phrases shown while a code-mode task is running. They advance over time (5s
// each) to read as progress, then hold on the last one until the task finishes.
const CODE_MODE_RUNNING_LABELS = [
'Working on the task…',
'Inspecting the project…',
'Digging into the code…',
'Figuring it out…',
'Making the changes…',
'Wiring things up…',
'Putting it together…',
]
const CODE_MODE_LABEL_INTERVAL_MS = 5000
// Detect acpx coding-agent invocations (code mode) and produce a status-aware
// label, e.g. "Working on the task…" → "Completed the task".
export const getCodeModeCommandLabel = (tool: ToolCall): string | null => {
if (tool.name !== 'executeCommand') return null
const input = normalizeToolInput(tool.input) as Record<string, unknown> | undefined
const command = typeof input?.command === 'string' ? input.command : ''
const match = command.match(/\bacpx\b[\s\S]*?\b(claude|codex)\b\s+exec\b/)
if (!match) return null
if (tool.status === 'error') return `Couldn't complete the task`
if (tool.status === 'completed') return `Completed the task`
// Advance through the phrases from the tool's start, holding on the last.
const elapsed = Math.max(0, Date.now() - tool.timestamp)
const step = Math.floor(elapsed / CODE_MODE_LABEL_INTERVAL_MS)
const idx = Math.min(step, CODE_MODE_RUNNING_LABELS.length - 1)
return CODE_MODE_RUNNING_LABELS[idx]
}
export const getToolDisplayName = (tool: ToolCall): string => {
const browserLabel = getBrowserControlLabel(tool)
if (browserLabel) return browserLabel
const codeModeLabel = getCodeModeCommandLabel(tool)
if (codeModeLabel) return codeModeLabel
const composioData = getComposioActionCardData(tool)
if (composioData) return composioData.label
return TOOL_DISPLAY_NAMES[tool.name] || tool.name
@ -653,6 +685,63 @@ export const getToolGroupSummary = (tools: ToolCall[]): string => {
return names.join(' · ')
}
// Past-tense action phrases for summarizing a finished tool group, e.g.
// "read 3 files, listed directory". Keyed by builtin tool name.
const TOOL_ACTION_VERBS: Record<string, { verb: string; one: string; many: string }> = {
'file-readText': { verb: 'read', one: 'file', many: 'files' },
'file-writeText': { verb: 'wrote', one: 'file', many: 'files' },
'file-editText': { verb: 'edited', one: 'file', many: 'files' },
'file-list': { verb: 'listed', one: 'directory', many: 'directories' },
'file-exists': { verb: 'checked', one: 'path', many: 'paths' },
'file-stat': { verb: 'inspected', one: 'file', many: 'files' },
'file-glob': { verb: 'searched for', one: 'file', many: 'files' },
'file-grep': { verb: 'searched', one: 'file', many: 'files' },
'file-mkdir': { verb: 'created', one: 'directory', many: 'directories' },
'file-rename': { verb: 'renamed', one: 'file', many: 'files' },
'file-copy': { verb: 'copied', one: 'file', many: 'files' },
'file-remove': { verb: 'removed', one: 'file', many: 'files' },
'file-getRoot': { verb: 'resolved', one: 'file root', many: 'file roots' },
'executeCommand': { verb: 'ran', one: 'command', many: 'commands' },
'executeMcpTool': { verb: 'ran', one: 'MCP tool', many: 'MCP tools' },
'listMcpServers': { verb: 'listed', one: 'MCP server', many: 'MCP servers' },
'listMcpTools': { verb: 'listed', one: 'MCP tool', many: 'MCP tools' },
'save-to-memory': { verb: 'saved', one: 'memory', many: 'memories' },
'loadSkill': { verb: 'loaded', one: 'skill', many: 'skills' },
'parseFile': { verb: 'parsed', one: 'file', many: 'files' },
}
// Summarize what a group of tools actually did, grouping identical actions
// and counting them: "read 3 files, listed directory". Unmapped tools fall
// back to their lowercased display name.
export const getToolActionsSummary = (tools: ToolCall[]): string => {
const order: string[] = []
const grouped = new Map<string, { phrase: typeof TOOL_ACTION_VERBS[string] | null; count: number; fallback: string }>()
for (const tool of tools) {
const phrase = TOOL_ACTION_VERBS[tool.name] ?? null
const key = phrase ? `${phrase.verb}|${phrase.one}` : tool.name
const existing = grouped.get(key)
if (existing) {
existing.count++
} else {
grouped.set(key, { phrase, count: 1, fallback: getToolDisplayName(tool) })
order.push(key)
}
}
const phrases = order.map((key) => {
const { phrase, count, fallback } = grouped.get(key)!
if (!phrase) return fallback.toLowerCase()
if (count > 1) return `${phrase.verb} ${count} ${phrase.many}`
const article = /^[aeiou]/i.test(phrase.one) ? 'an' : 'a'
return `${phrase.verb} ${article} ${phrase.one}`
})
// Show at most two operations; collapse the rest into "more...".
const MAX_ACTIONS = 2
if (phrases.length > MAX_ACTIONS) {
return `${phrases.slice(0, MAX_ACTIONS).join(', ')}, more...`
}
return phrases.join(', ')
}
export const inferRunTitleFromMessage = (content: string): string | undefined => {
const { message } = parseAttachedFiles(content)
const normalized = message.replace(/\s+/g, ' ').trim()

View file

@ -3,24 +3,50 @@ const KNOWLEDGE_PREFIX = 'knowledge/'
export const stripKnowledgePrefix = (path: string) =>
path.startsWith(KNOWLEDGE_PREFIX) ? path.slice(KNOWLEDGE_PREFIX.length) : path
export const splitWikiAlias = (input: string) => {
const separatorIndex = input.indexOf('|')
if (separatorIndex === -1) return { target: input, label: undefined }
const target = input.slice(0, separatorIndex)
const label = input.slice(separatorIndex + 1).trim()
return { target, label: label || undefined }
}
export const splitWikiFragment = (path: string) => {
const hashIndex = path.indexOf('#')
if (hashIndex === -1) return { path: path, heading: undefined }
const basePath = path.slice(0, hashIndex)
const heading = path.slice(hashIndex + 1).trim()
return { path: basePath, heading: heading || undefined }
}
export const normalizeWikiPath = (input: string) => {
const trimmed = input.trim().replace(/^\/+/, '').replace(/^\.\//, '')
const { target } = splitWikiAlias(input)
const trimmed = target.trim().replace(/^\/+/, '').replace(/^\.\//, '')
return stripKnowledgePrefix(trimmed)
}
export const ensureMarkdownExtension = (path: string) => {
if (path.toLowerCase().endsWith('.md')) return path
return `${path}.md`
const { path: basePath, heading } = splitWikiFragment(path)
if (!basePath) return heading ? `#${heading}` : path
const filePath = basePath.toLowerCase().endsWith('.md') ? basePath : `${basePath}.md`
return heading ? `${filePath}#${heading}` : filePath
}
export const toKnowledgePath = (wikiPath: string) => {
const normalized = normalizeWikiPath(wikiPath)
if (!normalized || normalized.includes('..') || normalized.endsWith('/')) return null
return `${KNOWLEDGE_PREFIX}${ensureMarkdownExtension(normalized)}`
const { path: basePath } = splitWikiFragment(normalized)
if (!basePath || basePath.includes('..') || basePath.endsWith('/')) return null
return `${KNOWLEDGE_PREFIX}${ensureMarkdownExtension(basePath)}`
}
export const wikiLabel = (wikiPath: string) => {
const { label } = splitWikiAlias(wikiPath)
if (label) return label
const normalized = normalizeWikiPath(wikiPath)
const name = normalized.split('/').pop() || normalized
const { path: basePath, heading } = splitWikiFragment(normalized)
if (!basePath && heading) return heading
const name = (basePath || normalized).split('/').pop() || normalized
return name.replace(/\.md$/i, '')
}

View file

@ -5,8 +5,10 @@
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"scripts": {
"build": "rm -rf dist && tsc",
"dev": "tsc -w"
"build": "rm -rf dist && tsc -p tsconfig.build.json",
"dev": "tsc -w -p tsconfig.build.json",
"test": "vitest run",
"test:watch": "vitest"
},
"dependencies": {
"@ai-sdk/anthropic": "^2.0.63",
@ -29,8 +31,8 @@
"express": "^5.2.1",
"glob": "^13.0.0",
"google-auth-library": "^10.5.0",
"isomorphic-git": "^1.29.0",
"googleapis": "^169.0.0",
"isomorphic-git": "^1.29.0",
"mammoth": "^1.11.0",
"node-html-markdown": "^2.0.0",
"ollama-ai-provider-v2": "^1.5.4",
@ -48,6 +50,7 @@
"@types/express": "^5.0.6",
"@types/node": "^25.0.3",
"@types/papaparse": "^5.5.2",
"@types/pdf-parse": "^1.1.5"
"@types/pdf-parse": "^1.1.5",
"vitest": "catalog:"
}
}

View file

@ -3,17 +3,19 @@ import fs from "fs";
import path from "path";
import { WorkDir } from "../config/config.js";
import { Agent, ToolAttachment } from "@x/shared/dist/agent.js";
import { AssistantContentPart, AssistantMessage, Message, MessageList, ProviderOptions, ToolCallPart, ToolMessage } from "@x/shared/dist/message.js";
import { AssistantContentPart, AssistantMessage, Message, MessageList, ProviderOptions, ToolCallPart, ToolMessage, UserMessageContext } from "@x/shared/dist/message.js";
import { LanguageModel, stepCountIs, streamText, tool, Tool, ToolSet } from "ai";
import { z } from "zod";
import { LlmStepStreamEvent } from "@x/shared/dist/llm-step-events.js";
import { execTool } from "../application/lib/exec-tool.js";
import { AskHumanRequestEvent, RunEvent, ToolPermissionRequestEvent } from "@x/shared/dist/runs.js";
import { AskHumanRequestEvent, RunEvent, ToolPermissionMetadata, ToolPermissionRequestEvent } from "@x/shared/dist/runs.js";
import { BuiltinTools } from "../application/lib/builtin-tools.js";
import { buildCopilotAgent } from "../application/assistant/agent.js";
import { buildLiveNoteAgent } from "../knowledge/live-note/agent.js";
import { buildBackgroundTaskAgent } from "../background-tasks/agent.js";
import { isBlocked, extractCommandNames } from "../application/lib/command-executor.js";
import { getFileAccessAllowList, type FileAccessGrant, type FileAccessOperation } from "../config/security.js";
import { resolveFilePathForPermission } from "../filesystem/files.js";
import container from "../di/container.js";
import { IModelConfigRepo } from "../models/repo.js";
import { createProvider } from "../models/models.js";
@ -21,7 +23,7 @@ import { resolveProviderConfig } from "../models/defaults.js";
import { IAgentsRepo } from "./repo.js";
import { IMonotonicallyIncreasingIdGenerator } from "../application/lib/id-gen.js";
import { IBus } from "../application/lib/bus.js";
import { IMessageQueue } from "../application/lib/message-queue.js";
import { IMessageQueue, type MiddlePaneContext } from "../application/lib/message-queue.js";
import { IRunsRepo } from "../runs/repo.js";
import { IRunsLock } from "../runs/lock.js";
import { IAbortRegistry } from "../runs/abort-registry.js";
@ -36,12 +38,143 @@ import { getRaw as getInlineTaskAgentRaw } from "../knowledge/inline_task_agent.
import { getRaw as getAgentNotesAgentRaw } from "../knowledge/agent_notes_agent.js";
const AGENT_NOTES_DIR = path.join(WorkDir, 'knowledge', 'Agent Notes');
const WORKDIR_CONFIG_FILE = path.join(WorkDir, 'config', 'workdir.json');
function loadUserWorkDir(): string | null {
// Work directory is scoped per run (per chat). Each run gets its own sidecar
// config file so setting it in one chat does not leak into others.
function workDirConfigFile(runId: string): string {
return path.join(WorkDir, 'config', `workdir-${runId}.json`);
}
type ToolPermissionMetadataValue = z.infer<typeof ToolPermissionMetadata>;
function isPathInside(parent: string, child: string): boolean {
const relative = path.relative(parent, child);
return relative === '' || (!!relative && !relative.startsWith('..') && !path.isAbsolute(relative));
}
function fileGrantCoversPath(grant: FileAccessGrant, operation: FileAccessOperation, resolvedPath: string): boolean {
return grant.operation === operation && isPathInside(path.resolve(grant.pathPrefix), path.resolve(resolvedPath));
}
function commonPathPrefix(paths: string[]): string {
if (!paths.length) return path.resolve(WorkDir);
const split = paths.map(p => path.resolve(p).split(path.sep).filter(Boolean));
const first = split[0];
const common: string[] = [];
for (let i = 0; i < first.length; i++) {
if (split.every(parts => parts[i] === first[i])) {
common.push(first[i]);
} else {
break;
}
}
const prefix = `${path.sep}${common.join(path.sep)}`;
return prefix === path.sep ? prefix : path.resolve(prefix);
}
function grantPrefixForTool(toolName: string, resolvedPaths: string[]): string {
if (toolName === 'file-list' || toolName === 'file-glob' || toolName === 'file-grep' || toolName === 'file-mkdir') {
return commonPathPrefix(resolvedPaths);
}
const parentPaths = resolvedPaths.map(p => path.dirname(p));
return commonPathPrefix(parentPaths);
}
function filePermissionTargets(toolName: string, args: Record<string, unknown>): { operation: FileAccessOperation; paths: string[] } | null {
const pathArg = typeof args.path === 'string' ? args.path : undefined;
switch (toolName) {
case 'file-readText':
case 'parseFile':
case 'LLMParse':
case 'file-exists':
case 'file-stat':
return pathArg ? { operation: 'read', paths: [pathArg] } : null;
case 'file-list':
return pathArg ? { operation: 'list', paths: [pathArg || '.'] } : null;
case 'file-glob':
return { operation: 'search', paths: [typeof args.cwd === 'string' && args.cwd ? args.cwd : '.'] };
case 'file-grep':
return { operation: 'search', paths: [typeof args.searchPath === 'string' && args.searchPath ? args.searchPath : '.'] };
case 'file-writeText':
case 'file-editText':
case 'file-mkdir':
return pathArg ? { operation: 'write', paths: [pathArg] } : null;
case 'file-copy':
case 'file-rename': {
const from = typeof args.from === 'string' ? args.from : undefined;
const to = typeof args.to === 'string' ? args.to : undefined;
return from && to ? { operation: 'write', paths: [from, to] } : null;
}
case 'file-remove':
return pathArg ? { operation: 'delete', paths: [pathArg] } : null;
default:
return null;
}
}
async function getToolPermissionMetadata(
toolCall: z.infer<typeof ToolCallPart>,
underlyingTool: z.infer<typeof ToolAttachment>,
sessionAllowedCommands: Set<string>,
sessionAllowedFileAccess: FileAccessGrant[],
): Promise<ToolPermissionMetadataValue | null> {
if (underlyingTool.type !== 'builtin') {
return null;
}
if (underlyingTool.name === 'executeCommand') {
const args = toolCall.arguments;
if (!args || typeof args !== 'object' || !('command' in args)) {
return null;
}
const command = String((args as { command: unknown }).command);
if (!isBlocked(command, sessionAllowedCommands)) {
return null;
}
return {
kind: 'command',
commandNames: extractCommandNames(command),
};
}
const args = toolCall.arguments && typeof toolCall.arguments === 'object'
? toolCall.arguments as Record<string, unknown>
: {};
const targets = filePermissionTargets(underlyingTool.name, args);
if (!targets) {
return null;
}
const resolvedTargets = await Promise.all(targets.paths.map(p => resolveFilePathForPermission(p)));
const outsideWorkspacePaths = resolvedTargets
.filter(target => !target.isInsideWorkspace)
.map(target => target.canonicalPath);
if (!outsideWorkspacePaths.length) {
return null;
}
const persistentGrants = getFileAccessAllowList();
const allGrants = [...persistentGrants, ...sessionAllowedFileAccess];
const uncovered = outsideWorkspacePaths.filter(resolvedPath =>
!allGrants.some(grant => fileGrantCoversPath(grant, targets.operation, resolvedPath))
);
if (!uncovered.length) {
return null;
}
return {
kind: 'file',
operation: targets.operation,
paths: uncovered,
pathPrefix: grantPrefixForTool(underlyingTool.name, uncovered),
};
}
function loadUserWorkDir(runId: string): string | null {
try {
if (!fs.existsSync(WORKDIR_CONFIG_FILE)) return null;
const raw = fs.readFileSync(WORKDIR_CONFIG_FILE, 'utf-8');
const file = workDirConfigFile(runId);
if (!fs.existsSync(file)) return null;
const raw = fs.readFileSync(file, 'utf-8');
const parsed = JSON.parse(raw) as { path?: unknown };
const value = typeof parsed.path === 'string' ? parsed.path.trim() : '';
return value || null;
@ -95,13 +228,103 @@ function loadAgentNotesContext(): string | null {
} catch { /* ignore */ }
if (otherFiles.length > 0) {
sections.push(`## More Specific Preferences\nFor more specific preferences, you can read these files using workspace-readFile. Only read them when relevant to the current task.\n\n${otherFiles.map(f => `- knowledge/Agent Notes/${f}`).join('\n')}`);
sections.push(`## More Specific Preferences\nFor more specific preferences, you can read these files using file-readText. Only read them when relevant to the current task.\n\n${otherFiles.map(f => `- knowledge/Agent Notes/${f}`).join('\n')}`);
}
if (sections.length === 0) return null;
return `# Agent Memory\n\n${sections.join('\n\n')}`;
}
function isCopilotLikeAgent(agentName: string | null | undefined): boolean {
return agentName === 'copilot' || agentName === 'rowboatx';
}
function formatCurrentDateTime(now: Date): string {
return now.toLocaleString('en-US', {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: '2-digit',
timeZoneName: 'short',
});
}
function toUserMessageContextMiddlePane(middlePaneContext: MiddlePaneContext | null): z.infer<typeof UserMessageContext>['middlePane'] {
if (!middlePaneContext) {
return { kind: 'empty' };
}
if (middlePaneContext.kind === 'note') {
return {
kind: 'note',
path: middlePaneContext.path,
content: middlePaneContext.content,
};
}
return {
kind: 'browser',
url: middlePaneContext.url,
title: middlePaneContext.title,
};
}
function buildUserMessageContext({
agentName,
middlePaneContext,
}: {
agentName: string | null | undefined;
middlePaneContext: MiddlePaneContext | null;
}): z.infer<typeof UserMessageContext> {
return {
currentDateTime: formatCurrentDateTime(new Date()),
...(isCopilotLikeAgent(agentName)
? { middlePane: toUserMessageContextMiddlePane(middlePaneContext) }
: {}),
};
}
function formatUserMessageContextForLlm(userMessageContext: z.infer<typeof UserMessageContext>): string {
const sections: string[] = [];
if (userMessageContext.currentDateTime) {
sections.push(`Current date and time: ${userMessageContext.currentDateTime}`);
}
if (userMessageContext.middlePane) {
if (userMessageContext.middlePane.kind === 'empty') {
sections.push(`Middle pane:\nState: empty`);
} else if (userMessageContext.middlePane.kind === 'note') {
sections.push(`Middle pane:\nState: note\nPath: ${userMessageContext.middlePane.path}\n\nContent:\n\`\`\`\n${userMessageContext.middlePane.content}\n\`\`\``);
} else {
sections.push(`Middle pane:\nState: browser\nURL: ${userMessageContext.middlePane.url}\nTitle: ${userMessageContext.middlePane.title}`);
}
}
if (sections.length === 0) {
return '';
}
return `# User Context
${sections.join('\n\n')}
# User Message
`;
}
const USER_CONTEXT_SYSTEM_INSTRUCTIONS = `# Hidden User Context
User messages may include a hidden "# User Context" section before "# User Message". Treat it as runtime metadata captured when that specific user message was sent. The actual user-authored text starts under "# User Message".
Use "Current date and time" for temporal reasoning.
If Middle pane context is present, it reflects what the user had open at the time of that specific message and overrides earlier middle-pane references. If the conversation history references a different note or browser page, the user had since closed or navigated away from it. Do not treat earlier context as current.
If Middle pane state is empty, the user was not looking at any relevant note or web page at that point. Answer the user's message on its own merits.
If Middle pane state is note, the supplied path and content are available so you can reference the note when relevant. The user may or may not be talking about this note. Do NOT assume every message is about it. Only reference or act on this note when the user's message clearly relates to it, such as "this note", "what I'm looking at", "here", "above", "below", or questions whose subject is plainly the note's content. For unrelated questions, ignore this note entirely and answer normally. Do not mention that you can see this note unless it is relevant to the answer.
If Middle pane state is browser, only the URL and page title are supplied; the page content itself is NOT included. If you need the page content to answer, use the browser tools available to you to read the page. The user may or may not be talking about this page. Only reference or act on this page when the user's message clearly relates to it, such as "this page", "this article", "what I'm looking at", "this site", or "summarize this". For unrelated questions, ignore this page entirely and answer normally. Do not mention that you can see the browser unless it is relevant to the answer.`;
export interface IAgentRuntime {
trigger(runId: string): Promise<void>;
}
@ -259,9 +482,10 @@ export async function mapAgentTool(t: z.infer<typeof ToolAttachment>): Promise<T
case "builtin": {
if (t.name === "ask-human") {
return tool({
description: "Ask a human before proceeding",
description: "Ask a human before proceeding. Optionally pass `options` (an array of short button labels) to render the question as a one-click choice; the user's response will be the chosen label verbatim.",
inputSchema: z.object({
question: z.string().describe("The question to ask the human"),
options: z.array(z.string()).optional().describe("Optional short button labels (2-4 recommended). If provided, the user picks one with a single click instead of typing. The response you receive will be the chosen label."),
}),
});
}
@ -588,17 +812,18 @@ export function convertFromMessages(messages: z.infer<typeof Message>[]): ModelM
providerOptions,
});
break;
case "user":
case "user": {
const userMessageContextPrefix = msg.userMessageContext ? formatUserMessageContextForLlm(msg.userMessageContext) : '';
if (typeof msg.content === 'string') {
// Legacy string — pass through unchanged
result.push({
role: "user",
content: msg.content,
content: `${userMessageContextPrefix}${msg.content}`,
providerOptions,
});
} else {
// New content parts array — collapse to text for LLM
const textSegments: string[] = [];
const textSegments: string[] = userMessageContextPrefix ? [userMessageContextPrefix] : [];
const attachmentLines: string[] = [];
for (const part of msg.content) {
@ -612,7 +837,11 @@ export function convertFromMessages(messages: z.infer<typeof Message>[]): ModelM
}
if (attachmentLines.length > 0) {
textSegments.unshift("User has attached the following files:", ...attachmentLines, "");
if (userMessageContextPrefix) {
textSegments.push("User has attached the following files:", ...attachmentLines, "");
} else {
textSegments.unshift("User has attached the following files:", ...attachmentLines, "");
}
}
result.push({
@ -622,6 +851,7 @@ export function convertFromMessages(messages: z.infer<typeof Message>[]): ModelM
});
}
break;
}
case "tool":
result.push({
role: "tool",
@ -683,6 +913,7 @@ export class AgentState {
allowedToolCallIds: Record<string, true> = {};
deniedToolCallIds: Record<string, true> = {};
sessionAllowedCommands: Set<string> = new Set();
sessionAllowedFileAccess: FileAccessGrant[] = [];
getPendingPermissions(): z.infer<typeof ToolPermissionRequestEvent>[] {
const response: z.infer<typeof ToolPermissionRequestEvent>[] = [];
@ -828,6 +1059,15 @@ export class AgentState {
switch (event.response) {
case "approve":
this.allowedToolCallIds[event.toolCallId] = true;
{
const permissionRequest = this.pendingToolPermissionRequests[event.toolCallId];
if (event.scope === "session" && permissionRequest?.permission?.kind === "file") {
this.sessionAllowedFileAccess.push({
operation: permissionRequest.permission.operation,
pathPrefix: permissionRequest.permission.pathPrefix,
});
}
}
// For session scope, extract command names and add to session allowlist
if (event.scope === "session") {
const toolCall = this.toolCallIdMap[event.toolCallId];
@ -922,6 +1162,7 @@ export async function* streamAgent({
let voiceInput = false;
let voiceOutput: 'summary' | 'full' | null = null;
let searchEnabled = false;
let codeMode: 'claude' | 'codex' | null = null;
let middlePaneContext:
| { kind: 'note'; path: string; content: string }
| { kind: 'browser'; url: string; title: string }
@ -1070,6 +1311,9 @@ export async function* streamAgent({
if (msg.searchEnabled) {
searchEnabled = true;
}
// Code mode is per-message: latest message decides whether the assistant
// should route coding work through the code-with-agents skill / chosen agent.
codeMode = msg.codeMode ?? null;
if (msg.voiceOutput) {
voiceOutput = msg.voiceOutput;
}
@ -1077,6 +1321,10 @@ export async function* streamAgent({
// latest user message. If the user closed the pane between messages, clear it.
middlePaneContext = msg.middlePaneContext ?? null;
loopLogger.log('dequeued user message', msg.messageId);
const userMessageContext = buildUserMessageContext({
agentName: state.agentName,
middlePaneContext,
});
yield* processEvent({
runId,
type: "message",
@ -1084,6 +1332,7 @@ export async function* streamAgent({
message: {
role: "user",
content: msg.message,
userMessageContext,
},
subflow: [],
});
@ -1105,24 +1354,14 @@ export async function* streamAgent({
loopLogger.log('running llm turn');
// stream agent response and build message
const messageBuilder = new StreamStepMessageBuilder();
const now = new Date();
const currentDateTime = now.toLocaleString('en-US', {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: '2-digit',
timeZoneName: 'short'
});
let instructionsWithDateTime = `Current date and time: ${currentDateTime}\n\n${agent.instructions}`;
let instructionsWithDateTime = `${agent.instructions}\n\n${USER_CONTEXT_SYSTEM_INSTRUCTIONS}`;
// Inject Agent Notes context for copilot
if (state.agentName === 'copilot' || state.agentName === 'rowboatx') {
const agentNotesContext = loadAgentNotesContext();
if (agentNotesContext) {
instructionsWithDateTime += `\n\n${agentNotesContext}`;
}
const userWorkDir = loadUserWorkDir();
const userWorkDir = loadUserWorkDir(runId);
if (userWorkDir) {
loopLogger.log('injecting user work directory', userWorkDir);
instructionsWithDateTime += `\n\n# User Work Directory
@ -1135,28 +1374,15 @@ Treat this as the **default location** for file operations whenever the user ref
- "save this", "export it", "write that to a file" write the output into the work directory unless the user names another location.
- "open the file I was just working on", "the doc from earlier" assume the work directory first.
Use absolute paths rooted at this directory. On macOS/Linux call \`executeCommand\` with POSIX commands (\`ls\`, \`cat\`, \`cp\`, etc.) operating on \`${userWorkDir}\`. On Windows use the equivalent cmd syntax. For reading file contents use \`parseFile\` or \`LLMParse\` with the absolute path; you do NOT need to copy the file into the workspace first.
Use absolute paths rooted at this directory with the \`file-*\` tools. For example, list with \`file-list({ path: "${userWorkDir}" })\`, read text with \`file-readText\`, and write text with \`file-writeText\`. For PDFs, Office docs, images, scanned docs, and other non-text files, use \`parseFile\` or \`LLMParse\` with the absolute path; you do NOT need to copy the file into the workspace first.
**Exceptions these ALWAYS take precedence over the work directory default:**
1. **Knowledge base questions.** If the user asks about anything in the knowledge graph (notes, people, organizations, projects, topics) or paths starting with \`knowledge/\`, use the workspace tools against \`knowledge/\` as documented above. Do NOT redirect those into the work directory.
1. **Knowledge base questions.** If the user asks about anything in the knowledge graph (notes, people, organizations, projects, topics) or paths starting with \`knowledge/\`, use file tools against \`knowledge/\` as documented above. Do NOT redirect those into the work directory.
2. **Explicit paths.** If the user names a different directory or gives an absolute/relative path (e.g. "in ~/Downloads", "from /tmp/foo", "the Desktop"), honor that path exactly and ignore the work-directory default for that request.
3. **Workspace-specific operations.** Anything that obviously belongs in the Rowboat workspace (config files, MCP servers, agent schedules, etc.) stays in the workspace, not the work directory.
Do not announce the work directory unless it's relevant. Just use it.`;
}
// Always inject a Middle Pane section so the LLM has a clear, up-to-date signal
// that supersedes any earlier middle-pane mention in the conversation history.
const middlePaneHeader = `\n\n# Middle Pane (Current State)\nThis section reflects what the user has open in the middle pane RIGHT NOW, at the time of their latest message. **This is authoritative and overrides any earlier mention of a note or web page in this conversation** — if the conversation history references a different note or browser page, the user has since closed or navigated away from it. Do not treat earlier context as current.\n\n`;
if (!middlePaneContext) {
loopLogger.log('injecting middle pane context (empty)');
instructionsWithDateTime += `${middlePaneHeader}**Nothing relevant is open in the middle pane right now.** The user is not looking at any note or web page. If earlier in this conversation you referenced a note or browser page as "what the user is viewing", that is no longer accurate — do not refer to it as currently open. Answer the user's latest message on its own merits.`;
} else if (middlePaneContext.kind === 'note') {
loopLogger.log('injecting middle pane context (note)', middlePaneContext.path);
instructionsWithDateTime += `${middlePaneHeader}The user has a note open. Its path and full content are provided below so you can reference it when relevant.\n\n**How to use this context:**\n- The user may or may not be talking about this note. Do NOT assume every message is about it.\n- Only reference or act on this note when the user's message clearly relates to it (e.g. "this note", "what I'm looking at", "here", "above", "below", or questions whose subject is plainly this note's content).\n- For unrelated questions (general chat, questions about other notes, tasks, emails, calendar, etc.), ignore this context entirely and answer normally.\n- Do not mention that you can see this note unless it is relevant to the answer.\n\n## Open note path\n${middlePaneContext.path}\n\n## Open note content\n\`\`\`\n${middlePaneContext.content}\n\`\`\``;
} else if (middlePaneContext.kind === 'browser') {
loopLogger.log('injecting middle pane context (browser)', middlePaneContext.url);
instructionsWithDateTime += `${middlePaneHeader}The user has the embedded browser open and is viewing a web page. Only the URL and page title are shown below — the page content itself is NOT included here. If you need the page content to answer, use the browser tools available to you to read the page.\n\n**How to use this context:**\n- The user may or may not be talking about this page. Do NOT assume every message is about it.\n- Only reference or act on this page when the user's message clearly relates to it (e.g. "this page", "this article", "what I'm looking at", "this site", "summarize this").\n- For unrelated questions (general chat, questions about other notes, tasks, emails, calendar, etc.), ignore this context entirely and answer normally.\n- Do not mention that you can see the browser unless it is relevant to the answer.\n\n## Current page\nURL: ${middlePaneContext.url}\nTitle: ${middlePaneContext.title}`;
}
}
if (voiceInput) {
loopLogger.log('voice input enabled, injecting voice input prompt');
@ -1173,6 +1399,50 @@ Do not announce the work directory unless it's relevant. Just use it.`;
loopLogger.log('search enabled, injecting search prompt');
instructionsWithDateTime += `\n\n# Search\nThe user has requested a search. Use the web-search tool to answer their query.`;
}
if (codeMode) {
loopLogger.log('code mode enabled, injecting coding-agent context', codeMode);
const agentDisplay = codeMode === 'claude' ? 'Claude Code' : 'Codex';
const otherAgent = codeMode === 'claude' ? 'codex' : 'claude';
const otherDisplay = codeMode === 'claude' ? 'Codex' : 'Claude Code';
// Deterministic, per-chat session name so the coding agent keeps
// context across the user's requests within this chat. Reusing the
// same -s <name> resumes the session; the first call creates it.
const sessionName = `rowboat-${runId}`;
instructionsWithDateTime += `\n\n# Code Mode (Active) — Default agent: ${agentDisplay}
The user has turned on **code mode** and the composer chip is set to **${agentDisplay}** (\`${codeMode}\`). Use this as the **default** agent for coding tasks in this turn.
**The user can override the agent at any time, two ways:**
1. By toggling the chip in the composer (preferred).
2. By asking you directly in chat ("use codex", "switch to claude", "do this with ${otherDisplay}", etc.). When the user explicitly asks to use a different agent in the current message, honor that use \`${otherAgent}\` instead of \`${codeMode}\` for this turn, and briefly mention they can also toggle it via the chip for stickiness.
**Persistent session for this chat session name: \`${sessionName}\`.** This chat uses one named agent session so the agent keeps context across your requests. The session must exist before it can be prompted (\`-s\` only resumes; it does not create).
**1. First coding action in this chat ensure the session exists:**
\`\`\`
npx acpx@latest --approve-all --cwd <workdir> <agent> sessions ensure --name ${sessionName}
\`\`\`
(\`ensure\` creates the session if missing and reuses it if it already exists — safe to call when reopening this chat later.)
**2. Then run the prompt:**
\`\`\`
npx acpx@latest --approve-all --timeout 600 --cwd <workdir> <agent> -s ${sessionName} "<prompt>"
\`\`\`
**3. Every follow-up coding request in this chat reuse the same session (do NOT create again):**
\`\`\`
npx acpx@latest --approve-all --timeout 600 --cwd <workdir> <agent> -s ${sessionName} "<prompt>"
\`\`\`
Run these as **separate, sequential** \`executeCommand\` calls — issue the \`sessions ensure\` call first and WAIT for it to finish, then issue the prompt call. Do NOT fire both in the same turn / batch.
Where \`<agent>\` is either \`claude\` or \`codex\` — pick based on (in priority order): an explicit in-chat override → the chip setting (\`${codeMode}\`). Use \`${sessionName}\` exactly — do NOT invent a different name, and do NOT use \`exec\` (it is one-shot and forgets).
If the user's message is clearly NOT a coding request (small talk, an unrelated question), answer directly without invoking the coding agent. Code mode signals readiness, not that every message must route through the agent.`;
}
let streamError: string | null = null;
for await (const event of streamLlm(
model,
@ -1228,25 +1498,34 @@ Do not announce the work directory unless it's relevant. Just use it.`;
const underlyingTool = agent.tools![part.toolName];
if (underlyingTool.type === "builtin" && underlyingTool.name === "ask-human") {
loopLogger.log('emitting ask-human-request, toolCallId:', part.toolCallId);
const rawOptions = (part.arguments as { options?: unknown }).options;
const options = Array.isArray(rawOptions)
? rawOptions.filter((o): o is string => typeof o === 'string' && o.trim().length > 0)
: undefined;
yield* processEvent({
runId,
type: "ask-human-request",
toolCallId: part.toolCallId,
query: part.arguments.question,
...(options && options.length > 0 ? { options } : {}),
subflow: [],
});
}
if (underlyingTool.type === "builtin" && underlyingTool.name === "executeCommand") {
// if command is blocked, then seek permission
if (isBlocked(part.arguments.command, state.sessionAllowedCommands)) {
loopLogger.log('emitting tool-permission-request, toolCallId:', part.toolCallId);
yield* processEvent({
runId,
type: "tool-permission-request",
toolCall: part,
subflow: [],
});
}
const permission = await getToolPermissionMetadata(
part,
underlyingTool,
state.sessionAllowedCommands,
state.sessionAllowedFileAccess,
);
if (permission) {
loopLogger.log('emitting tool-permission-request, toolCallId:', part.toolCallId);
yield* processEvent({
runId,
type: "tool-permission-request",
toolCall: part,
permission,
subflow: [],
});
}
if (underlyingTool.type === "agent" && underlyingTool.name) {
loopLogger.log('emitting spawn-subflow, toolCallId:', part.toolCallId);

View file

@ -3,6 +3,8 @@ import { getRuntimeContext, getRuntimeContextPrompt } from "./runtime-context.js
import { composioAccountsRepo } from "../../composio/repo.js";
import { isConfigured as isComposioConfigured } from "../../composio/client.js";
import { CURATED_TOOLKITS } from "@x/shared/dist/composio.js";
import container from "../../di/container.js";
import type { ICodeModeConfigRepo } from "../../code-mode/repo.js";
const runtimeContextPrompt = getRuntimeContextPrompt(getRuntimeContext());
@ -29,7 +31,7 @@ Load the \`composio-integration\` skill when the user asks to interact with any
`;
}
function buildStaticInstructions(composioEnabled: boolean, catalog: string): string {
function buildStaticInstructions(composioEnabled: boolean, catalog: string, codeModeEnabled: boolean = true): string {
// Conditionally include Composio-related instruction sections
const emailDraftSuffix = composioEnabled
? ` Do NOT load this skill for reading, fetching, or checking emails — use the \`composio-integration\` skill for that instead.`
@ -80,7 +82,9 @@ ${thirdPartyBlock}**Meeting Prep:** When users ask you to prepare for a meeting,
**Document Collaboration:** When users ask you to work on a document, collaborate on writing, create a new document, edit/refine existing notes, or say things like "let's work on [X]", "help me write [X]", "create a doc for [X]", or "let's draft [X]", you MUST load the \`doc-collab\` skill first. **This applies even for small one-off edits** — the skill carries the canonical *terse-and-scannable* writing style for the knowledge base, and that style applies whether you're authoring a fresh note or fixing a single section. Load it before writing anything into a note.
**Code with Agents:** When users ask you to write code, build a project, create a script, fix a bug, or do any software development task, load the \`code-with-agents\` skill first. It provides guidance for delegating coding work to Claude Code or Codex via acpx.
${codeModeEnabled
? `**Code with Agents:** When users ask you to write code, build a project, create a script, fix a bug, or do any software development task — **including simple things like "create a .c file" or "write a hello-world in Python"** — your FIRST action MUST be \`loadSkill('code-with-agents')\`. Do NOT reach for \`executeCommand\` (PowerShell / bash / shell) or any workspace file tool to do code work yourself before loading this skill. The skill decides whether to delegate to Claude Code / Codex (via acpx) or hand control back to you, and it presents the user a one-click choice when needed. Paths outside the Rowboat workspace root (e.g. \`G:/...\`, \`~/projects/...\`) are NORMAL for coding tasks — do NOT raise "outside workspace" concerns or fall back to your own tools.`
: `**Code with Agents (disabled):** Code mode is currently OFF in the user's settings. Do NOT load \`code-with-agents\` and do NOT call acpx. Handle coding requests yourself with your normal tools if you can. After answering, add a final line letting the user know they can delegate coding to Claude Code or Codex by enabling Code Mode in Settings → Code Mode.`}
**App Control:** When users ask you to open notes, show the bases or graph view, filter or search notes, or manage saved views, load the \`app-navigation\` skill first. It provides structured guidance for navigating the app UI and controlling the knowledge base view.
@ -140,39 +144,39 @@ Users can interact with the knowledge graph through you, open it directly in Obs
**CRITICAL PATH REQUIREMENT:**
- The workspace root is the configured workdir
- The knowledge base is in the \`knowledge/\` subfolder
- When using workspace tools, ALWAYS include \`knowledge/\` in the path
- **WRONG:** \`workspace-grep({ pattern: "John", path: "" })\` or \`path: "."\` or any absolute path to the workspace root
- **CORRECT:** \`workspace-grep({ pattern: "John", path: "knowledge/" })\`
- When searching knowledge, ALWAYS include \`knowledge/\` in the search path
- **WRONG:** \`file-grep({ pattern: "John", searchPath: "" })\` or \`searchPath: "."\` or any absolute path to the workspace root
- **CORRECT:** \`file-grep({ pattern: "John", searchPath: "knowledge/" })\`
Use the builtin workspace tools to search and read the knowledge base:
Use the builtin file tools to search and read the knowledge base:
**Finding notes:**
\`\`\`
# List all people notes
workspace-readdir("knowledge/People")
file-list("knowledge/People")
# Search for a person by name - MUST include knowledge/ in path
workspace-grep({ pattern: "Sarah Chen", path: "knowledge/" })
file-grep({ pattern: "Sarah Chen", searchPath: "knowledge/" })
# Find notes mentioning a company - MUST include knowledge/ in path
workspace-grep({ pattern: "Acme Corp", path: "knowledge/" })
file-grep({ pattern: "Acme Corp", searchPath: "knowledge/" })
\`\`\`
**Reading notes:**
\`\`\`
# Read a specific person's note
workspace-readFile("knowledge/People/Sarah Chen.md")
file-readText("knowledge/People/Sarah Chen.md")
# Read an organization note
workspace-readFile("knowledge/Organizations/Acme Corp.md")
file-readText("knowledge/Organizations/Acme Corp.md")
\`\`\`
**When a user mentions someone by name:**
1. First, search for them: \`workspace-grep({ pattern: "John", path: "knowledge/" })\`
2. Read their note to get full context: \`workspace-readFile("knowledge/People/John Smith.md")\`
1. First, search for them: \`file-grep({ pattern: "John", searchPath: "knowledge/" })\`
2. Read their note to get full context: \`file-readText("knowledge/People/John Smith.md")\`
3. Use the context (role, organization, past interactions, commitments) in your response
**NEVER use an empty path or root path. ALWAYS set path to \`knowledge/\` or a subfolder like \`knowledge/People/\`.**
**NEVER use an empty search path or root path for knowledge lookup. ALWAYS set searchPath to \`knowledge/\` or a subfolder like \`knowledge/People/\`.**
## When to Access the Knowledge Graph
@ -237,29 +241,23 @@ ${toolPriority}
${runtimeContextPrompt}
## Workspace Access & Scope
- **Inside the workspace root:** Use builtin workspace tools (\`workspace-readFile\`, \`workspace-writeFile\`, etc.). These don't require security approval.
- **Outside the workspace root (Desktop, Downloads, Documents, etc.):** Use \`executeCommand\` to run shell commands.
- **IMPORTANT:** Do NOT access files outside the workspace root unless the user explicitly asks you to (e.g., "organize my Desktop", "find a file in Downloads").
**CRITICAL - When the user asks you to work with files outside the workspace root:**
- Follow the detected runtime platform above for shell syntax and filesystem path style.
- On macOS/Linux, use POSIX-style commands and paths (e.g., \`~/Desktop\`, \`~/Downloads\`, \`open\` on macOS).
- On Windows, use cmd-compatible commands and Windows paths (e.g., \`C:\\Users\\<name>\\Desktop\`).
- You CAN access the user's full filesystem via \`executeCommand\` - there is no sandbox restriction on paths.
- NEVER say "I can only run commands inside the workspace root" or "I don't have access to your Desktop" - just use \`executeCommand\`.
- NEVER offer commands for the user to run manually - run them yourself with \`executeCommand\`.
- NEVER say "I'll run shell commands equivalent to..." - just describe what you'll do in plain language (e.g., "I'll move 12 screenshots to a new Screenshots folder").
- NEVER ask what OS the user is on if runtime platform is already available.
## File Access & Scope
- Use builtin file tools (\`file-readText\`, \`file-writeText\`, \`file-editText\`, etc.) for normal file work anywhere on the user's machine.
- Relative paths resolve against the Rowboat workspace root. Use paths like \`knowledge/People/Ada.md\` for knowledge files.
- Use absolute paths or \`~/...\` paths when the user refers to Desktop, Downloads, Documents, the injected work directory, or any other location outside the Rowboat workspace.
- File operations inside the Rowboat workspace normally run without approval. File operations outside the workspace may trigger a permission prompt; this is expected.
- Do NOT use \`executeCommand\` just to read, write, edit, list, search, move, copy, or remove files. Use file tools and let the permission system handle access.
- Do NOT read binary files as text. Use \`parseFile\` or \`LLMParse\` for PDFs, Office docs, images, scanned docs, presentations, and other non-text formats.
- Do NOT access files outside the workspace unless the user explicitly asks you to or the current task clearly requires it.
- Load the \`organize-files\` skill for guidance on file organization tasks.
## Builtin Tools vs Shell Commands
**IMPORTANT**: Rowboat provides builtin tools that are internal and do NOT require any user approval:
- \`workspace-readFile\`, \`workspace-writeFile\`, \`workspace-edit\`, \`workspace-remove\` - File operations
- \`workspace-readdir\`, \`workspace-exists\`, \`workspace-stat\`, \`workspace-glob\`, \`workspace-grep\` - Directory exploration and file search
- \`workspace-mkdir\`, \`workspace-rename\`, \`workspace-copy\` - File/directory management
- \`parseFile\` - Parse and extract text from files (PDF, Excel, CSV, Word .docx). Accepts absolute paths or workspace-relative paths — no need to copy files into the workspace first. Best for well-structured digital documents.
**IMPORTANT**: Rowboat provides builtin tools:
- \`file-readText\`, \`file-writeText\`, \`file-editText\`, \`file-remove\` - File operations
- \`file-list\`, \`file-exists\`, \`file-stat\`, \`file-glob\`, \`file-grep\` - Directory exploration and file search
- \`file-mkdir\`, \`file-rename\`, \`file-copy\` - File/directory management
- \`parseFile\` - Parse and extract text from files (PDF, Excel, CSV, Word .docx). Accepts absolute, ~/..., or relative paths — no need to copy files into the workspace first. Best for well-structured digital documents.
- \`LLMParse\` - Send a file to the configured LLM as a multimodal attachment to extract content as markdown. Use this instead of \`parseFile\` for scanned PDFs, images with text, complex layouts, presentations, or any format where local parsing falls short. Supports documents and images.
- \`analyzeAgent\` - Agent analysis
- \`addMcpServer\`, \`listMcpServers\`, \`listMcpTools\`, \`executeMcpTool\` - MCP server management and execution
@ -270,23 +268,21 @@ ${slackToolsLine}- \`web-search\` - Search the web. Returns rich results with fu
- \`save-to-memory\` - Save observations about the user to the agent memory system. Use this proactively during conversations.
${composioToolsLine}
**Prefer these tools whenever possible** they work instantly with zero friction. For file operations inside the workspace root, always use these instead of \`executeCommand\`.
**Prefer these tools whenever possible.** For file operations anywhere on the machine, use file tools instead of \`executeCommand\`.
**Shell commands via \`executeCommand\`:**
- You can run ANY shell command via \`executeCommand\`. Some commands are pre-approved in \`config/security.json\` within the workspace root and run immediately.
- You can run shell commands via \`executeCommand\`. Some commands are pre-approved in \`config/security.json\` within the workspace root and run immediately.
- Commands not on the pre-approved list will trigger a one-time approval prompt for the user this is fine and expected, just a minor friction. Do NOT let this stop you from running commands you need.
- **Never say "I can't run this command"** or ask the user to run something manually. Just call \`executeCommand\` and let the approval flow handle it.
- When calling \`executeCommand\`, do NOT provide the \`cwd\` parameter unless absolutely necessary. The default working directory is already set to the workspace root.
- Always confirm with the user before executing commands that modify files outside the workspace root (e.g., "I'll move 12 screenshots to ~/Desktop/Screenshots. Proceed?").
- Always confirm with the user before executing commands that modify files outside the workspace root. Prefer file tools for file changes.
**CRITICAL: MCP Server Configuration**
- ALWAYS use the \`addMcpServer\` builtin tool to add or update MCP servers—it validates the configuration before saving
- NEVER manually edit \`config/mcp.json\` using \`workspace-writeFile\` for MCP servers
- NEVER manually edit \`config/mcp.json\` using \`file-writeText\` for MCP servers
- Invalid MCP configs will prevent the agent from starting with validation errors
**Only \`executeCommand\` (shell/bash commands) goes through the approval flow.** If you need to delete a file, use the \`workspace-remove\` builtin tool, not \`executeCommand\` with \`rm\`. If you need to create a file, use \`workspace-writeFile\`, not \`executeCommand\` with \`touch\` or \`echo >\`.
Rowboat's internal builtin tools never require approval only shell commands via \`executeCommand\` do.
File tools and \`executeCommand\` can both go through the approval flow depending on the path or command. If you need to delete a file, use \`file-remove\`, not \`executeCommand\` with \`rm\`. If you need to create a file, use \`file-writeText\`, not \`executeCommand\` with \`touch\` or \`echo >\`.
## File Path References
@ -320,30 +316,29 @@ Never output raw file paths in plain text when they could be wrapped in a filepa
/** Keep backward-compatible export for any external consumers */
export const CopilotInstructions = buildStaticInstructions(true, skillCatalog);
/**
* Cached Composio instructions. Invalidated by calling invalidateCopilotInstructionsCache().
*/
let cachedInstructions: string | null = null;
/**
* Invalidate the cached instructions so the next buildCopilotInstructions() call
* regenerates the Composio section. Call this after connecting/disconnecting a toolkit.
*/
export function invalidateCopilotInstructionsCache(): void {
cachedInstructions = null;
}
/**
* Build full copilot instructions with dynamic Composio tools section.
* Results are cached and reused until invalidated via invalidateCopilotInstructionsCache().
*/
export async function buildCopilotInstructions(): Promise<string> {
if (cachedInstructions !== null) return cachedInstructions;
const composioEnabled = await isComposioConfigured();
const catalog = composioEnabled
? skillCatalog
: buildSkillCatalog({ excludeIds: ['composio-integration'] });
const baseInstructions = buildStaticInstructions(composioEnabled, catalog);
let codeModeEnabled = false;
try {
const repo = container.resolve<ICodeModeConfigRepo>('codeModeConfigRepo');
codeModeEnabled = (await repo.getConfig()).enabled;
} catch {
// repo unavailable — default to disabled
}
const excludeIds: string[] = [];
if (!composioEnabled) excludeIds.push('composio-integration');
if (!codeModeEnabled) excludeIds.push('code-with-agents');
const catalog = excludeIds.length > 0
? buildSkillCatalog({ excludeIds })
: skillCatalog;
const baseInstructions = buildStaticInstructions(composioEnabled, catalog, codeModeEnabled);
const composioPrompt = await getComposioToolsPrompt();
cachedInstructions = composioPrompt
? baseInstructions + '\n' + composioPrompt

View file

@ -14,7 +14,7 @@ Open a specific knowledge file in the editor pane.
- ` + "`path`" + `: Full workspace-relative path (e.g., ` + "`knowledge/People/John Smith.md`" + `)
**Tips:**
- Use ` + "`workspace-grep`" + ` first to find the exact path if you're unsure of the filename.
- Use ` + "`file-grep`" + ` first to find the exact path if you're unsure of the filename.
- Always pass the full ` + "`knowledge/...`" + ` path, not just the filename.
### open-view

View file

@ -25,11 +25,11 @@ Mixed instructions ("summarize and email it") trigger both.
You have three dedicated builtin tools for this skill:
- \`create-background-task\` — materializes a new task on disk. **Use this. Do not write \`task.yaml\` yourself with \`workspace-edit\`, and do not search the codebase for IPC channels like \`bg-task:create\`** — they're renderer-side and not callable from here.
- \`create-background-task\` — materializes a new task on disk. **Use this. Do not write \`task.yaml\` yourself with \`file-editText\`, and do not search the codebase for IPC channels like \`bg-task:create\`** — they're renderer-side and not callable from here.
- \`patch-background-task\` — updates an existing task (instructions / triggers / active / model). Use this for the extend-don't-fork case.
- \`run-background-task-agent\` — manually fires a task to run now. Always call this immediately after \`create-background-task\` so the user sees content.
To inspect what tasks already exist, use \`workspace-glob\` on \`bg-tasks/*/task.yaml\` and \`workspace-readFile\` on candidates. The user's bg-tasks folder is workspace-relative.
To inspect what tasks already exist, use \`file-glob\` on \`bg-tasks/*/task.yaml\` and \`file-readText\` on candidates. The user's bg-tasks folder is workspace-relative.
## Mode: act-first

View file

@ -158,26 +158,26 @@ Pass the paper URL to the summariser. Don't ask for human input.
## Additional Builtin Tools
While \`executeCommand\` is the most versatile, other builtin tools exist for specific Rowboat operations (file management, agent inspection, etc.). These are primarily used by the Rowboat copilot itself and are not typically needed in user agents. If you need file operations, consider using bash commands like \`cat\`, \`echo\`, \`tee\`, etc. through \`executeCommand\`.
While \`executeCommand\` is useful for CLI tools and shell workflows, builtin file tools exist for normal file management. Use \`file-*\` tools for reading, writing, editing, listing, searching, moving, copying, and removing files instead of shell commands.
### Copilot-Specific Builtin Tools
The Rowboat copilot has access to special builtin tools that regular agents don't typically use. These tools help the copilot assist users with workspace management and MCP integration:
The Rowboat copilot has access to special builtin tools that regular agents don't typically use. These tools help the copilot assist users with file management, app workflows, and MCP integration:
#### File & Directory Operations
- \`workspace-readdir\` - List directory contents (supports recursive exploration)
- \`workspace-readFile\` - Read file contents
- \`workspace-writeFile\` - Create or update file contents
- \`workspace-edit\` - Make precise edits by replacing specific text (safer than full rewrites)
- \`workspace-remove\` - Remove files or directories
- \`workspace-exists\` - Check if a file or directory exists
- \`workspace-stat\` - Get file/directory statistics
- \`workspace-mkdir\` - Create directories
- \`workspace-rename\` - Rename or move files/directories
- \`workspace-copy\` - Copy files
- \`workspace-getRoot\` - Get workspace root directory path
- \`workspace-glob\` - Find files matching a glob pattern (e.g., "**/*.ts", "agents/*.md")
- \`workspace-grep\` - Search file contents using regex, returns matching files and lines
- \`file-list\` - List directory contents (supports recursive exploration)
- \`file-readText\` - Read file contents
- \`file-writeText\` - Create or update file contents
- \`file-editText\` - Make precise edits by replacing specific text (safer than full rewrites)
- \`file-remove\` - Remove files or directories
- \`file-exists\` - Check if a file or directory exists
- \`file-stat\` - Get file/directory statistics
- \`file-mkdir\` - Create directories
- \`file-rename\` - Rename or move files/directories
- \`file-copy\` - Copy files
- \`file-getRoot\` - Get the default root for relative file paths
- \`file-glob\` - Find files matching a glob pattern (e.g., "**/*.ts", "agents/*.md")
- \`file-grep\` - Search file contents using regex, returns matching files and lines
#### Agent Operations
- \`analyzeAgent\` - Read and analyze an agent file structure

View file

@ -1,90 +1,140 @@
export const skill = String.raw`
# Code with Agents Skill
Use this skill when the user asks you to write code, build a project, create scripts, fix bugs, or do any software development task that should be delegated to a coding agent (Claude Code or Codex).
Use this skill whenever the user asks you to write code, build a project, create scripts, fix bugs, read/explain code, or do any software development task even simple file creations like "make a .c file".
## Important: delegate ALL coding work
Coding agents operate on **arbitrary file paths** (including paths outside the Rowboat workspace root, like \`G:/4th sem/CN\` or \`~/projects/foo\`). Do NOT raise "outside workspace" concerns, and do NOT fall back to your own \`executeCommand\` (PowerShell / bash) or workspace file tools to do code work yourself.
Once the user has chosen to use Claude Code or Codex, you MUST delegate ALL code-related tasks to the coding agent. This includes:
- Writing, editing, or refactoring code
- Reading, summarizing, or explaining code
- Debugging and fixing bugs
- Running tests or build commands
- Exploring project structure
- Any other task that involves interacting with a codebase
---
Do NOT attempt to do any of these yourself no reading files, no running commands, no writing code. You are the coordinator; the coding agent does the work. Your job is to translate the user's request into a clear prompt and pass it to the agent.
## STEP 1 MANDATORY FIRST ACTION
## Prerequisites
Look in your **system context** for a section titled **"# Code Mode (Active)"**.
The user must have one of the following installed on their machine:
- **Claude Code** https://claude.ai/code
- **Codex** https://codex.openai.com
### Case A "# Code Mode (Active)" IS present
These are external tools that you cannot install for the user.
Code mode is on and the user has selected an agent. Skip directly to Step 2. Do NOT call ask-human.
## Workflow
### Case B "# Code Mode (Active)" is NOT present
### Step 1: Gather requirements
Your **very next tool call MUST be \`ask-human\`** with options. Do not write any explanation text first. Do not describe a plan. Do not check the workspace boundary. Just call:
Before running anything, confirm the following with the user:
\`\`\`
ask-human({
question: "How should I handle this coding request?",
options: [
"Use code mode (Claude Code)",
"Use code mode (Codex)",
"Continue with default Rowboat"
]
})
\`\`\`
1. **Working directory** Ask which folder the code should be written in, unless the user has already specified it. Example: "Which folder should I work in?"
2. **Agent choice** Ask whether to use **Claude Code** or **Codex**. Mention that the chosen agent must already be installed on their machine.
This is non-negotiable. The user gets clickable buttons. Free-text "which agent?" questions are forbidden here.
### Step 2: Confirm execution plan
**Branch on the response:**
- "Use code mode (Claude Code)" proceed to Step 2 with agent = \`claude\`.
- "Use code mode (Codex)" proceed to Step 2 with agent = \`codex\`.
- "Continue with default Rowboat" ABANDON this skill. Handle the request yourself using your own tools (workspace file tools, \`executeCommand\` shell, etc.). The rest of this skill does not apply for this turn.
Once you know the folder and agent, tell the user:
---
> I'll use [Claude Code / Codex] to [description of the task] in \`[folder]\`. Permission requests from the coding agent itself (file writes, command execution, etc.) will be automatically approved once it starts. Wait for the user's confirmation before you execute anything.
## STEP 2 Resolve workdir, confirm, execute
### Step 3: Execute with acpx
**Resolve the workdir** (in this priority order):
1. A path the user named in their original message (e.g. \`G:/4th sem/CN\`).
2. The path from a "# User Work Directory" block in your context.
3. Ask once in plain text: "Which folder should I work in?"
Use the \`executeCommand\` tool to run the coding agent via acpx. The command format is:
**State your intent in one line, then execute immediately do NOT wait for a "yes".** The \`executeCommand\` call surfaces a permission card that is itself the user's confirmation, so an extra in-chat "reply yes to proceed" is redundant friction. Say something like:
**For Claude Code:**
` + "`" + `
npx acpx@latest --approve-all --cwd <folder> claude exec "<prompt>"
` + "`" + `
> Using [Claude Code / Codex] to [task description] in \`[folder]\`.
**For Codex:**
` + "`" + `
npx acpx@latest --approve-all --cwd <folder> codex exec "<prompt>"
` + "`" + `
and then immediately make the \`executeCommand\` call in the same turn.
**Execute** with the chosen agent using a **persistent named session** so follow-up coding requests in this chat resume the same agent and keep context.
Pick \`<agent>\` (\`claude\` or \`codex\`) by, in priority order:
- An explicit in-chat override from the user this turn ("use codex", "switch to claude") honor it.
- The agent chosen in Step 1 / the "# Code Mode (Active)" block.
Pick \`<session-name>\` — **stable for this whole chat**:
- If the "# Code Mode (Active)" block gives a session name (e.g. \`rowboat-<runId>\`), use that exact name.
- Otherwise pick one short, kebab-case name and **reuse it for every coding call this turn and in follow-ups** never a new name each time.
**\`-s\` resumes an existing session; it does NOT create one.** So ensure the session exists once at the start, then prompt:
**1. First coding action in this chat ensure the session exists:**
\`\`\`
npx acpx@latest --approve-all --cwd <folder> <agent> sessions ensure --name <session-name>
\`\`\`
(\`ensure\` creates the session if missing and reuses it if it already exists — so reopening this chat later just resumes the same session instead of erroring.)
**2. Then run the prompt:**
\`\`\`
npx acpx@latest --approve-all --timeout 600 --cwd <folder> <agent> -s <session-name> "<prompt>"
\`\`\`
**3. Every follow-up coding request in this chat reuse the same session (do NOT create again):**
\`\`\`
npx acpx@latest --approve-all --timeout 600 --cwd <folder> <agent> -s <session-name> "<prompt>"
\`\`\`
**Run steps 1 and 2 as separate, sequential \`executeCommand\` calls.** Issue the \`sessions ensure\` call FIRST, wait for it to finish, and only THEN issue the prompt call. Do NOT fire both in the same turn / batch — each must surface and complete its own permission + command block before the next begins.
Do NOT use \`exec\` — it is one-shot and forgets everything.
Concrete example:
\`\`\`
# First coding message in the chat ensure the session, then prompt:
npx acpx@latest --approve-all --cwd "G:\\Blogging\\myblog" claude sessions ensure --name diskspace-check
npx acpx@latest --approve-all --timeout 600 --cwd "G:\\Blogging\\myblog" claude -s diskspace-check "Check the system disk space and report total, used, and free space."
# Follow-up in the same chat reuse the session, no create:
npx acpx@latest --approve-all --timeout 600 --cwd "G:\\Blogging\\myblog" claude -s diskspace-check "Summarize what we did and the final findings."
\`\`\`
### Critical: flag order
The \`--approve-all\` and \`--cwd\` flags are global flags and MUST come before the agent name (\`claude\` or \`codex\`). This is the correct order:
\`--approve-all\`, \`--timeout\`, and \`--cwd\` are GLOBAL flags and MUST appear BEFORE the agent name. \`sessions ensure --name <name>\` and \`-s <session-name>\` come AFTER the agent name:
` + "`" + `
npx acpx@latest [global flags] <agent> exec "<prompt>"
` + "`" + `
- Correct: \`npx acpx@latest --approve-all --timeout 600 --cwd <folder> <agent> -s <session-name> "<prompt>"\`
- Wrong: \`npx acpx@latest <agent> --approve-all -s <name> "..."\` (will fail)
**Correct:**
` + "`" + `
npx acpx@latest --approve-all --cwd ~/projects/myapp claude exec "fix the bug"
` + "`" + `
### Writing good prompts for the agent
**Wrong (will fail):**
` + "`" + `
npx acpx@latest claude --approve-all exec "fix the bug"
` + "`" + `
- Be specific: file names, function signatures, expected behavior.
- Mention constraints (language, framework, style).
- Expand short user requests into clear, actionable prompts.
### Writing good prompts
---
When constructing the prompt for the coding agent:
- Be specific and detailed about what to build or fix
- Include file names, function signatures, and expected behavior
- Mention any constraints (language, framework, style)
- If the user gave you a short request, expand it into a clear, actionable prompt for the agent
## STEP 3 Report results
### Step 4: Report results
After the command finishes:
- Pass through the coding agent's summary as-is. Do not rewrite.
- Refer to file paths as plain text. Do NOT use \`\`\`file:path\`\`\` reference blocks. (This overrides the global "always wrap paths in filepath blocks" rule — for code-mode output, plain text.)
- Only add your own explanation if the command failed (non-zero exit):
- Exit code 5 permissions were denied (shouldn't happen with \`--approve-all\`; flag it).
- Exit code 4 / "No acpx session found" the \`-s <session-name>\` session doesn't exist yet. Create it once with \`npx acpx@latest --approve-all --cwd <folder> <agent> sessions ensure --name <session-name>\`, then retry the prompt. (\`-s\` only resumes; it never creates.)
- "command not found" / agent not installed, or an auth/sign-in error the agent isn't set up. Tell the user to install or sign in to the agent via **Settings Code Mode**, where Rowboat shows the install and sign-in status.
After the command finishes, look for the summary that the coding agent produced at the end of its output and pass that along to the user as-is. Do not rewrite or add to it. Only add your own explanation if the command failed or the exit code is non-zero.
---
Do NOT use file reference blocks (e.g. \`\`\`file:path/to/file\`\`\`) when mentioning code files — they may not open correctly. Just refer to file paths as plain text.
## Once delegating: delegate fully
- If the exit code is 5, it means permissions were denied this should not happen with \`--approve-all\`, but if it does, let the user know
After Step 2 fires, delegate ALL related coding tasks for this turn to the coding agent writing, editing, reading, debugging, exploring structure, running tests. You are the coordinator; the agent does the work.
## Prerequisites (informational)
The user must have one of these installed locally these are external tools you cannot install:
- Claude Code https://claude.ai/code
- Codex https://codex.openai.com
`;
export default skill;

View file

@ -78,19 +78,19 @@ Map each point to a slide layout from the Available Layout Types below. For a ty
## Workflow
1. Use workspace-readFile to check knowledge/ for relevant context about the company, product, team, etc.
1. Use file-readText to check knowledge/ for relevant context about the company, product, team, etc.
2. Ensure Playwright is installed: \`npm install playwright && npx playwright install chromium\`
3. Use workspace-getRoot to get the workspace root path.
3. Use file-getRoot to get the workspace root path.
4. Plan the narrative arc and slide outline (see Content Planning above).
5. Use workspace-writeFile to create the HTML file at tmp/presentation.html (workspace-relative) with slides (1280x720px each).
5. Use file-writeText to create the HTML file at tmp/presentation.html (workspace-relative) with slides (1280x720px each).
6. **Perform the Post-Generation Validation (see below). Fix any issues before proceeding.**
7. Use workspace-writeFile to create the conversion script at tmp/convert.js (workspace-relative) see Playwright Export section.
7. Use file-writeText to create the conversion script at tmp/convert.js (workspace-relative) see Playwright Export section.
8. Run it: \`node <WORKSPACE_ROOT>/tmp/convert.js\`
9. Tell the user: "Your presentation is ready at ~/Desktop/presentation.pdf" and note the theme used.
**Critical**: Never show HTML code to the user. Never ask the user to run commands, install packages, or make technical decisions. The entire pipeline from content to PDF must be invisible to the user.
Use workspace-writeFile and workspace-readFile for ALL file operations. Do NOT use executeCommand to write or read files.
Use file-writeText and file-readText for ALL file operations. Do NOT use executeCommand to write or read files.
## Post-Generation Validation (REQUIRED)
@ -142,14 +142,14 @@ html { -webkit-print-color-adjust: exact !important; print-color-adjust: exact !
## Playwright Export
\`\`\`javascript
// save as tmp/convert.js via workspace-writeFile
// save as tmp/convert.js via file-writeText
const { chromium } = require('playwright');
const path = require('path');
(async () => {
const browser = await chromium.launch();
const page = await browser.newPage();
// Replace <WORKSPACE_ROOT> with the actual absolute path from workspace-getRoot
// Replace <WORKSPACE_ROOT> with the actual absolute path from file-getRoot
await page.goto('file://<WORKSPACE_ROOT>/tmp/presentation.html', { waitUntil: 'networkidle' });
await page.pdf({
path: path.join(process.env.HOME, 'Desktop', 'presentation.pdf'),
@ -162,7 +162,7 @@ const path = require('path');
})();
\`\`\`
Replace \`<WORKSPACE_ROOT>\` with the actual absolute path returned by workspace-getRoot.
Replace \`<WORKSPACE_ROOT>\` with the actual absolute path returned by file-getRoot.
## Available Layout Types (35 Templates)

View file

@ -22,7 +22,7 @@ You are an expert document assistant helping the user create, edit, and refine d
## CRITICAL: Re-read Before Every Response
**Before every response, you MUST use workspace-readFile to re-read the current document.** The user may have edited the file manually outside of this conversation. Always work with the latest version of the file, never rely on a cached or previous version.
**Before every response, you MUST use file-readText to re-read the current document.** The user may have edited the file manually outside of this conversation. Always work with the latest version of the file, never rely on a cached or previous version.
## Core Principles
@ -55,12 +55,12 @@ When the user mentions a document name, search for it using multiple approaches:
1. **Search by name pattern** (handles partial matches, different cases):
\`\`\`
workspace-glob({ pattern: "knowledge/**/*[name]*", path: "knowledge/" })
file-glob({ pattern: "**/*[name]*", cwd: "knowledge/" })
\`\`\`
2. **Search by content** (finds docs that mention the topic):
\`\`\`
workspace-grep({ pattern: "[name]", path: "knowledge/" })
file-grep({ pattern: "[name]", searchPath: "knowledge/" })
\`\`\`
3. **Try common variations:**
@ -106,7 +106,7 @@ workspace-createFile({
**Types of requests:**
1. **Direct edits** - "Change the title to X", "Add a bullet point about Y", "Remove the pricing section"
Make the edit immediately using workspace-editFile
Make the edit immediately using file-editText
2. **Content generation** - "Write an intro", "Draft the executive summary", "Add a section about our approach"
Generate the content and add it to the document
@ -122,21 +122,21 @@ workspace-createFile({
### Step 3: Execute Changes
**For edits, use workspace-editFile:**
**For edits, use file-editText:**
\`\`\`
workspace-editFile({
file-editText({
path: "knowledge/[path].md",
old_string: "[exact text to replace]",
new_string: "[new text]"
oldString: "[exact text to replace]",
newString: "[new text]"
})
\`\`\`
**For additions at the end:**
\`\`\`
workspace-editFile({
file-editText({
path: "knowledge/[path].md",
old_string: "[last line or section]",
new_string: "[last line or section]\n\n[new content]"
oldString: "[last line or section]",
newString: "[last line or section]\n\n[new content]"
})
\`\`\`
@ -156,14 +156,14 @@ When the user mentions people, companies, or projects:
**Search for relevant notes:**
\`\`\`
workspace-grep({ pattern: "[Name]", path: "knowledge/" })
file-grep({ pattern: "[Name]", searchPath: "knowledge/" })
\`\`\`
**Read relevant notes:**
\`\`\`
workspace-readFile("knowledge/People/[Person].md")
workspace-readFile("knowledge/Organizations/[Company].md")
workspace-readFile("knowledge/Projects/[Project].md")
file-readText("knowledge/People/[Person].md")
file-readText("knowledge/Organizations/[Company].md")
file-readText("knowledge/Projects/[Project].md")
\`\`\`
**Use the context:**
@ -237,7 +237,7 @@ Renders a styled table from structured data.
### Block Guidelines
- The JSON must be valid and on a single line (no pretty-printing)
- Insert blocks using \`workspace-editFile\` just like any other content
- Insert blocks using \`file-editText\` just like any other content
- When the user asks for a chart, table, embed, or live dashboard use blocks rather than plain Markdown tables or image links
- When editing a note that already contains blocks, preserve them unless the user asks to change them
- For local dashboards and mini apps, put the site files in \`sites/<slug>/\` and point an \`iframe\` block at \`http://localhost:3210/sites/<slug>/\`

View file

@ -16,11 +16,11 @@ When the user says "draft an email to Monica" or mentions ANY person, organizati
1. **STOP** - Do not draft anything yet
2. **SEARCH** - Look them up in the knowledge base (path MUST be \`knowledge/\`):
\`\`\`
workspace-grep({ pattern: "Monica", path: "knowledge/" })
file-grep({ pattern: "Monica", searchPath: "knowledge/" })
\`\`\`
3. **READ** - Read their note to understand who they are:
\`\`\`
workspace-readFile("knowledge/People/Monica Smith.md")
file-readText("knowledge/People/Monica Smith.md")
\`\`\`
4. **UNDERSTAND** - Extract their role, organization, relationship history, past interactions, open items
5. **THEN DRAFT** - Only now draft the email, using this context
@ -133,19 +133,19 @@ Before drafting, gather relevant context. **Always check the knowledge base firs
First, search for the sender and any mentioned entities (path MUST be \`knowledge/\`):
\`\`\`
# Search for the sender by name or email
workspace-grep({ pattern: "sender_name_or_email", path: "knowledge/" })
file-grep({ pattern: "sender_name_or_email", searchPath: "knowledge/" })
# List all people to find potential matches
workspace-readdir("knowledge/People")
file-list("knowledge/People")
\`\`\`
Then read the relevant notes:
\`\`\`
# Read the sender's note
workspace-readFile("knowledge/People/Sender Name.md")
file-readText("knowledge/People/Sender Name.md")
# Read their organization's note
workspace-readFile("knowledge/Organizations/Company Name.md")
file-readText("knowledge/Organizations/Company Name.md")
\`\`\`
Extract from these notes:

View file

@ -34,7 +34,7 @@ When this skill is loaded, your job is: make a passive note live (or extend the
## Mode: act-first (non-negotiable on strong signals)
Live-note creation and editing are **action-first**. Strong-signal asks (see below) get *executed*, not discussed. Read the file, write the \`live:\` block via \`workspace-edit\`, run the agent once, and confirm in one line at the end. Past tense, not future tense.
Live-note creation and editing are **action-first**. Strong-signal asks (see below) get *executed*, not discussed. Read the file, write the \`live:\` block via \`file-editText\`, run the agent once, and confirm in one line at the end. Past tense, not future tense.
What you must NOT do on a strong-signal ask:
- Don't ask "Should I make edits directly, or show changes first for approval?" that prompt belongs to generic doc editing, not live notes.
@ -77,7 +77,7 @@ When a strong signal lands without a specific note attached, pick the folder by
**Filename**: derive from the topic in title-case (\`News Feed.md\`, \`Coinbase News.md\`, \`SFO Weather.md\`).
**Before creating**: \`workspace-grep\` and \`workspace-glob\` the chosen folder for an existing note that already covers the topic. If one exists with a \`live:\` block, **extend its objective** (see "Already-live notes — extend, don't fork"). If one exists without a \`live:\` block, **make that note live** (don't create a duplicate). Only create a new file when no match is found.
**Before creating**: \`file-grep\` and \`file-glob\` the chosen folder for an existing note that already covers the topic. If one exists with a \`live:\` block, **extend its objective** (see "Already-live notes — extend, don't fork"). If one exists without a \`live:\` block, **make that note live** (don't create a duplicate). Only create a new file when no match is found.
### Default cadence picker (when the user didn't specify timing)
@ -165,8 +165,8 @@ When skipping a re-run (because the user said not to or "later"):
**User:** "i want to set up a news feed to track news for India and the world."
**Right behaviour** (one turn):
1. \`workspace-grep({ pattern: "News Feed", path: "knowledge/Notes/" })\` — search for an existing match.
2. \`workspace-grep({ pattern: "news", path: "knowledge/Notes/" })\` — broader search to catch variants.
1. \`file-grep({ pattern: "News Feed", searchPath: "knowledge/Notes/" })\` — search for an existing match.
2. \`file-grep({ pattern: "news", searchPath: "knowledge/Notes/" })\` — broader search to catch variants.
3. No match found create \`knowledge/Notes/News Feed.md\` with a sensible \`live:\` block (objective covering India + world headlines, a windows trigger for "every morning"-style refresh, plus an \`eventMatchCriteria\` if news might come from synced data).
4. Call \`run-live-note-agent\` with a backfill \`context\` so the body isn't empty.
5. Reply: "Done — created \`knowledge/Notes/News Feed.md\` and made it live, refreshing every morning. Running it once now so you see content right away. Manage it from the Live notes view."
@ -460,16 +460,16 @@ live:
### Making a passive note live (no \`live:\` block yet)
1. \`workspace-readFile({ path })\` — re-read fresh.
1. \`file-readText({ path })\` — re-read fresh.
2. Inspect existing frontmatter (the ` + "`" + `---` + "`" + `-fenced block at the top, if any).
3. \`workspace-edit\`:
3. \`file-editText\`:
- **If the note has frontmatter without a \`live:\` block**: anchor on the closing \`---\` of the frontmatter and insert the \`live:\` block just before it.
- **If the note has no frontmatter at all**: anchor on the very first line of the file. Replace it with a new frontmatter block (\`---\\n\` ... \`\\n---\\n\` followed by the original first line).
### Extending an already-live note
1. \`workspace-readFile({ path })\` — fetch the current \`live.objective\`.
2. Edit the \`objective\` value via \`workspace-edit\` to absorb the new ask in natural language. Keep the \`|\` block scalar style.
1. \`file-readText({ path })\` — fetch the current \`live.objective\`.
2. Edit the \`objective\` value via \`file-editText\` to absorb the new ask in natural language. Keep the \`|\` block scalar style.
3. Don't touch other \`live:\` fields unless the user explicitly asked (e.g. "also run this hourly" → add/edit \`triggers.cronExpr\`).
### Sidebar chat with a specific note
@ -606,17 +606,17 @@ The tool returns ` + "`" + `{ success, runId, action, summary, contentAfter, err
- **Don't add \`triggers\`** if the user explicitly wants manual-only.
- **Don't write** \`lastRunAt\`, \`lastRunId\`, or \`lastRunSummary\` — runtime-managed.
- **Don't schedule** with ` + "`" + `"* * * * *"` + "`" + ` (every minute) unless the user explicitly asks.
- **Don't use \`workspace-writeFile\`** to rewrite the whole file — always \`workspace-edit\` with a unique anchor.
- **Don't use \`file-writeText\`** to rewrite the whole file — always \`file-editText\` with a unique anchor.
## Editing or Removing an Existing Live Note
**Change the objective:** \`workspace-edit\` the \`objective\` value (use \`|\` block scalar).
**Change the objective:** \`file-editText\` the \`objective\` value (use \`|\` block scalar).
**Change triggers:** \`workspace-edit\` the relevant sub-field of the \`triggers\` object.
**Change triggers:** \`file-editText\` the relevant sub-field of the \`triggers\` object.
**Pause without removing:** flip \`active: false\`.
**Make passive (remove the \`live:\` block):** \`workspace-edit\` with \`oldString\` = the entire \`live:\` block (from the \`live:\` line down to the next top-level key or the closing \`---\`), \`newString\` = empty. The note body is left alone — if you want to clear leftover agent output, do that as a separate edit.
**Make passive (remove the \`live:\` block):** \`file-editText\` with \`oldString\` = the entire \`live:\` block (from the \`live:\` line down to the next top-level key or the closing \`---\`), \`newString\` = empty. The note body is left alone — if you want to clear leftover agent output, do that as a separate edit.
## Quick Reference

View file

@ -38,7 +38,7 @@ export const skill = String.raw`
**ALWAYS use the \`addMcpServer\` builtin tool** to add or update MCP server configurations. This tool validates the configuration before saving and prevents startup errors.
**NEVER manually create or edit \`config/mcp.json\`** using \`workspace-writeFile\` for MCP servers—this bypasses validation and will cause errors.
**NEVER manually create or edit \`config/mcp.json\`** using \`file-writeText\` for MCP servers—this bypasses validation and will cause errors.
### MCP Server Configuration Schema

View file

@ -16,12 +16,12 @@ When the user asks to prep for a meeting or mentions attendees:
1. **STOP** - Do not create a generic brief
2. **SEARCH** - Look up each attendee in the knowledge base:
\`\`\`
workspace-grep({ pattern: "Attendee Name", path: "knowledge/" })
file-grep({ pattern: "Attendee Name", searchPath: "knowledge/" })
\`\`\`
3. **READ** - Read their notes to understand who they are:
\`\`\`
workspace-readFile("knowledge/People/Attendee Name.md")
workspace-readFile("knowledge/Organizations/Their Company.md")
file-readText("knowledge/People/Attendee Name.md")
file-readText("knowledge/Organizations/Their Company.md")
\`\`\`
4. **UNDERSTAND** - Extract their role, organization, relationship history, past interactions, open items
5. **THEN BRIEF** - Only now create the meeting brief, using this context
@ -68,13 +68,13 @@ For each attendee, search the knowledge base (path MUST be \`knowledge/\`):
**Search People notes:**
\`\`\`
workspace-grep({ pattern: "attendee_name", path: "knowledge/People/" })
workspace-grep({ pattern: "attendee_email", path: "knowledge/People/" })
file-grep({ pattern: "attendee_name", searchPath: "knowledge/People/" })
file-grep({ pattern: "attendee_email", searchPath: "knowledge/People/" })
\`\`\`
If a person note exists, read it:
\`\`\`
workspace-readFile("knowledge/People/Attendee Name.md")
file-readText("knowledge/People/Attendee Name.md")
\`\`\`
Extract:
@ -86,13 +86,13 @@ Extract:
**Search Organization notes:**
\`\`\`
workspace-grep({ pattern: "company_name", path: "knowledge/Organizations/" })
file-grep({ pattern: "company_name", searchPath: "knowledge/Organizations/" })
\`\`\`
**Search Projects:**
\`\`\`
workspace-grep({ pattern: "attendee_name", path: "knowledge/Projects/" })
workspace-grep({ pattern: "company_name", path: "knowledge/Projects/" })
file-grep({ pattern: "attendee_name", searchPath: "knowledge/Projects/" })
file-grep({ pattern: "company_name", searchPath: "knowledge/Projects/" })
\`\`\`
### Step 4: Create Meeting Brief

View file

@ -58,7 +58,7 @@ Use these as the \`link\` parameter to land the user on a specific view in Rowbo
| Background task view | \`rowboat://open?type=task&name=<task-name>\` | \`rowboat://open?type=task&name=daily-brief\` |
| Suggested topics | \`rowboat://open?type=suggested-topics\` | — |
The \`type=file\` path is workspace-relative (the same path you'd pass to \`workspace-readFile\`).
The \`type=file\` path is workspace-relative (the same path you'd pass to \`file-readText\`).
## Anti-patterns
- **Don't notify per step** of a multi-step task. Notify on completion, not on progress.

View file

@ -1,17 +1,14 @@
import { z, ZodType } from "zod";
import * as path from "path";
import * as fs from "fs/promises";
import { createReadStream, existsSync, readFileSync } from "fs";
import { createInterface } from "readline";
import { execSync } from "child_process";
import { glob } from "glob";
import { existsSync, readFileSync } from "fs";
import { executeCommand, executeCommandAbortable } from "./command-executor.js";
import { resolveSkill, availableSkills } from "../assistant/skills/index.js";
import { executeTool, listServers, listTools } from "../../mcp/mcp.js";
import container from "../../di/container.js";
import { IMcpConfigRepo } from "../..//mcp/repo.js";
import { McpServerDefinition } from "@x/shared/dist/mcp.js";
import * as workspace from "../../workspace/workspace.js";
import * as files from "../../filesystem/files.js";
import { IAgentsRepo } from "../../agents/repo.js";
import { WorkDir } from "../../config/config.js";
import { composioAccountsRepo } from "../../composio/repo.js";
@ -98,21 +95,47 @@ const LLMPARSE_MIME_TYPES: Record<string, string> = {
// When the LLM invokes acpx via executeCommand, pre-resolve claude's real .exe
// from the npm-shim layout and inject it via env so the bridge can spawn it.
function resolveClaudeExeOnWindows(): string | undefined {
const pathDirs = (process.env.PATH ?? '').split(';');
for (const dir of pathDirs) {
const trimmed = dir.trim();
if (!trimmed) continue;
const cmdPath = path.join(trimmed, 'claude.cmd');
if (!existsSync(cmdPath)) continue;
const exeFromLayout = path.join(trimmed, 'node_modules', '@anthropic-ai', 'claude-code', 'bin', 'claude.exe');
// Candidate dirs = everything on PATH, plus well-known npm/pnpm/volta global
// bin dirs. Electron's runtime PATH can omit these even when the user's shell
// includes them, which would otherwise leave us unable to find claude.exe and
// force a fallback to claude.cmd (which Node refuses to spawn — EINVAL).
const home = process.env.USERPROFILE ?? '';
const appData = process.env.APPDATA || (home && path.join(home, 'AppData', 'Roaming'));
const localAppData = process.env.LOCALAPPDATA || (home && path.join(home, 'AppData', 'Local'));
const programFiles = process.env.ProgramFiles || 'C:\\Program Files';
const knownDirs = [
appData && path.join(appData, 'npm'),
localAppData && path.join(localAppData, 'npm'),
appData && path.join(appData, 'pnpm'),
localAppData && path.join(localAppData, 'pnpm'),
home && path.join(home, '.volta', 'bin'),
path.join(programFiles, 'nodejs'),
].filter(Boolean) as string[];
const pathDirs = (process.env.PATH ?? '').split(';').map((d) => d.trim()).filter(Boolean);
const seen = new Set<string>();
const candidates = [...pathDirs, ...knownDirs].filter((d) => {
const key = d.toLowerCase();
if (seen.has(key)) return false;
seen.add(key);
return true;
});
for (const dir of candidates) {
// Direct npm-shim layout: <dir>\node_modules\@anthropic-ai\claude-code\bin\claude.exe
const exeFromLayout = path.join(dir, 'node_modules', '@anthropic-ai', 'claude-code', 'bin', 'claude.exe');
if (existsSync(exeFromLayout)) return exeFromLayout;
// Otherwise parse the claude.cmd shim for the real exe path.
const cmdPath = path.join(dir, 'claude.cmd');
if (!existsSync(cmdPath)) continue;
try {
const content = readFileSync(cmdPath, 'utf-8');
const absMatch = content.match(/[A-Z]:[\\/][^\s"]*claude\.exe/i);
if (absMatch && existsSync(absMatch[0])) return absMatch[0];
const relMatch = content.match(/%~dp0[\\/]?([^\s"%]+claude\.exe)/i);
if (relMatch) {
const resolved = path.join(trimmed, relMatch[1]);
const resolved = path.join(dir, relMatch[1]);
if (existsSync(resolved)) return resolved;
}
} catch {
@ -156,12 +179,12 @@ export const BuiltinTools: z.infer<typeof BuiltinToolsSchema> = {
},
},
'workspace-getRoot': {
description: 'Get the workspace root directory path',
'file-getRoot': {
description: 'Get the default root directory for relative file paths. Relative paths passed to file tools resolve against this directory.',
inputSchema: z.object({}),
execute: async () => {
try {
return await workspace.getRoot();
return { root: WorkDir };
} catch (error) {
return {
error: error instanceof Error ? error.message : 'Unknown error',
@ -170,14 +193,14 @@ export const BuiltinTools: z.infer<typeof BuiltinToolsSchema> = {
},
},
'workspace-exists': {
description: 'Check if a file or directory exists in the workspace',
'file-exists': {
description: 'Check if a file or directory exists. Accepts absolute paths, ~/ paths, or paths relative to the default root.',
inputSchema: z.object({
path: z.string().min(1).describe('Workspace-relative path to check'),
path: z.string().min(1).describe('File or directory path to check'),
}),
execute: async ({ path: relPath }: { path: string }) => {
execute: async ({ path: filePath }: { path: string }) => {
try {
return await workspace.exists(relPath);
return await files.exists(filePath);
} catch (error) {
return {
error: error instanceof Error ? error.message : 'Unknown error',
@ -186,14 +209,14 @@ export const BuiltinTools: z.infer<typeof BuiltinToolsSchema> = {
},
},
'workspace-stat': {
'file-stat': {
description: 'Get file or directory statistics (size, modification time, etc.)',
inputSchema: z.object({
path: z.string().min(1).describe('Workspace-relative path to stat'),
path: z.string().min(1).describe('File or directory path to stat'),
}),
execute: async ({ path: relPath }: { path: string }) => {
execute: async ({ path: filePath }: { path: string }) => {
try {
return await workspace.stat(relPath);
return await files.stat(filePath);
} catch (error) {
return {
error: error instanceof Error ? error.message : 'Unknown error',
@ -202,22 +225,22 @@ export const BuiltinTools: z.infer<typeof BuiltinToolsSchema> = {
},
},
'workspace-readdir': {
'file-list': {
description: 'List directory contents. Can recursively explore directory structure with options.',
inputSchema: z.object({
path: z.string().describe('Workspace-relative directory path (empty string for root)'),
path: z.string().describe('Directory path to list. Use "." for the default root.'),
recursive: z.boolean().optional().describe('Recursively list all subdirectories (default: false)'),
includeStats: z.boolean().optional().describe('Include file stats like size and modification time (default: false)'),
includeHidden: z.boolean().optional().describe('Include hidden files starting with . (default: false)'),
allowedExtensions: z.array(z.string()).optional().describe('Filter by file extensions (e.g., [".json", ".ts"])'),
}),
execute: async ({
path: relPath,
recursive,
includeStats,
includeHidden,
allowedExtensions
}: {
execute: async ({
path: filePath,
recursive,
includeStats,
includeHidden,
allowedExtensions
}: {
path: string;
recursive?: boolean;
includeStats?: boolean;
@ -225,13 +248,12 @@ export const BuiltinTools: z.infer<typeof BuiltinToolsSchema> = {
allowedExtensions?: string[];
}) => {
try {
const entries = await workspace.readdir(relPath || '', {
return await files.list(filePath || '.', {
recursive,
includeStats,
includeHidden,
allowedExtensions,
});
return entries;
} catch (error) {
return {
error: error instanceof Error ? error.message : 'Unknown error',
@ -240,120 +262,24 @@ export const BuiltinTools: z.infer<typeof BuiltinToolsSchema> = {
},
},
'workspace-readFile': {
description: 'Read a file from the workspace. For text files (utf8, the default), returns the content with each line prefixed by its 1-indexed line number (e.g. `12: some text`). Use the `offset` and `limit` parameters to page through large files; defaults read up to 2000 lines starting at line 1. Output is wrapped in `<path>`, `<type>`, `<content>` tags and ends with a footer indicating whether the read reached end-of-file or was truncated. Line numbers in the output are display-only — do NOT include them when later writing or editing the file. For `base64` / `binary` encodings, returns the raw bytes as a string and ignores `offset` / `limit`.',
'file-readText': {
description: 'Read a UTF-8 text file. Returns content with each line prefixed by its 1-indexed line number (e.g. `12: some text`). Use `offset` and `limit` to page through large files; defaults read up to 2000 lines starting at line 1. Output is wrapped in `<path>`, `<resolvedPath>`, `<type>`, `<content>` tags and ends with a footer indicating whether the read reached end-of-file or was truncated. Line numbers are display-only — do NOT include them when later writing or editing the file. Refuses binary files; use parseFile or LLMParse for documents, PDFs, images, and other non-text formats.',
inputSchema: z.object({
path: z.string().min(1).describe('Workspace-relative file path'),
offset: z.coerce.number().int().min(1).optional().describe('1-indexed line to start reading from (default: 1). Utf8 only.'),
limit: z.coerce.number().int().min(1).optional().describe('Maximum number of lines to read (default: 2000). Utf8 only.'),
encoding: z.enum(['utf8', 'base64', 'binary']).optional().describe('File encoding (default: utf8)'),
path: z.string().min(1).describe('Text file path to read'),
offset: z.coerce.number().int().min(1).optional().describe('1-indexed line to start reading from (default: 1).'),
limit: z.coerce.number().int().min(1).optional().describe('Maximum number of lines to read (default: 2000).'),
}),
execute: async ({
path: relPath,
path: filePath,
offset,
limit,
encoding = 'utf8',
}: {
path: string;
offset?: number;
limit?: number;
encoding?: 'utf8' | 'base64' | 'binary';
}) => {
try {
if (encoding !== 'utf8') {
return await workspace.readFile(relPath, encoding);
}
const DEFAULT_READ_LIMIT = 2000;
const MAX_LINE_LENGTH = 2000;
const MAX_LINE_SUFFIX = `... (line truncated to ${MAX_LINE_LENGTH} chars)`;
const MAX_BYTES = 50 * 1024;
const MAX_BYTES_LABEL = `${MAX_BYTES / 1024} KB`;
const absPath = workspace.resolveWorkspacePath(relPath);
const stats = await fs.lstat(absPath);
const stat = workspace.statToSchema(stats, 'file');
const etag = workspace.computeEtag(stats.size, stats.mtimeMs);
const effectiveOffset = offset ?? 1;
const effectiveLimit = limit ?? DEFAULT_READ_LIMIT;
const start = effectiveOffset - 1;
const stream = createReadStream(absPath, { encoding: 'utf8' });
const rl = createInterface({ input: stream, crlfDelay: Infinity });
const collected: string[] = [];
let totalLines = 0;
let bytes = 0;
let truncatedByBytes = false;
let hasMoreLines = false;
try {
for await (const text of rl) {
totalLines += 1;
if (totalLines <= start) continue;
if (collected.length >= effectiveLimit) {
hasMoreLines = true;
continue;
}
const line = text.length > MAX_LINE_LENGTH
? text.substring(0, MAX_LINE_LENGTH) + MAX_LINE_SUFFIX
: text;
const size = Buffer.byteLength(line, 'utf-8') + (collected.length > 0 ? 1 : 0);
if (bytes + size > MAX_BYTES) {
truncatedByBytes = true;
hasMoreLines = true;
break;
}
collected.push(line);
bytes += size;
}
} finally {
rl.close();
stream.destroy();
}
if (totalLines < effectiveOffset && !(totalLines === 0 && effectiveOffset === 1)) {
return { error: `Offset ${effectiveOffset} is out of range for this file (${totalLines} lines)` };
}
const prefixed = collected.map((line, index) => `${index + effectiveOffset}: ${line}`);
const lastReadLine = effectiveOffset + collected.length - 1;
const nextOffset = lastReadLine + 1;
let footer: string;
if (truncatedByBytes) {
footer = `(Output capped at ${MAX_BYTES_LABEL}. Showing lines ${effectiveOffset}-${lastReadLine}. Use offset=${nextOffset} to continue.)`;
} else if (hasMoreLines) {
footer = `(Showing lines ${effectiveOffset}-${lastReadLine} of ${totalLines}. Use offset=${nextOffset} to continue.)`;
} else {
footer = `(End of file - total ${totalLines} lines)`;
}
const content = [
`<path>${relPath}</path>`,
`<type>file</type>`,
`<content>`,
prefixed.join('\n'),
'',
footer,
`</content>`,
].join('\n');
return {
path: relPath,
encoding: 'utf8' as const,
content,
stat,
etag,
offset: effectiveOffset,
limit: effectiveLimit,
totalLines,
hasMore: hasMoreLines || truncatedByBytes,
};
return await files.readText(filePath, offset, limit);
} catch (error) {
return {
error: error instanceof Error ? error.message : 'Unknown error',
@ -362,34 +288,30 @@ export const BuiltinTools: z.infer<typeof BuiltinToolsSchema> = {
},
},
'workspace-writeFile': {
description: 'Write or update file contents in the workspace. Automatically creates parent directories and supports atomic writes.',
'file-writeText': {
description: 'Write or update UTF-8 text file contents. Automatically creates parent directories and supports atomic writes.',
inputSchema: z.object({
path: z.string().min(1).describe('Workspace-relative file path'),
data: z.string().describe('File content to write'),
encoding: z.enum(['utf8', 'base64', 'binary']).optional().describe('Data encoding (default: utf8)'),
path: z.string().min(1).describe('Text file path to write'),
data: z.string().describe('UTF-8 text content to write'),
atomic: z.boolean().optional().describe('Use atomic write (default: true)'),
mkdirp: z.boolean().optional().describe('Create parent directories if needed (default: true)'),
expectedEtag: z.string().optional().describe('ETag to check for concurrent modifications (conflict detection)'),
}),
execute: async ({
path: relPath,
path: filePath,
data,
encoding,
atomic,
mkdirp,
expectedEtag
}: {
path: string;
data: string;
encoding?: 'utf8' | 'base64' | 'binary';
atomic?: boolean;
mkdirp?: boolean;
expectedEtag?: string;
}) => {
try {
return await workspace.writeFile(relPath, data, {
encoding,
return await files.writeText(filePath, data, {
atomic,
mkdirp,
expectedEtag,
@ -402,16 +324,16 @@ export const BuiltinTools: z.infer<typeof BuiltinToolsSchema> = {
},
},
'workspace-edit': {
description: 'Make precise edits to a file by replacing specific text. Safer than rewriting entire files - produces smaller diffs and reduces risk of data loss.',
'file-editText': {
description: 'Make precise edits to a UTF-8 text file by replacing specific text. Safer than rewriting entire files - produces smaller diffs and reduces risk of data loss. Refuses binary files.',
inputSchema: z.object({
path: z.string().min(1).describe('Workspace-relative file path'),
path: z.string().min(1).describe('Text file path to edit'),
oldString: z.string().describe('Exact text to find and replace'),
newString: z.string().describe('Replacement text'),
replaceAll: z.boolean().optional().describe('Replace all occurrences (default: false, fails if not unique)'),
}),
execute: async ({
path: relPath,
path: filePath,
oldString,
newString,
replaceAll = false
@ -422,46 +344,22 @@ export const BuiltinTools: z.infer<typeof BuiltinToolsSchema> = {
replaceAll?: boolean;
}) => {
try {
const result = await workspace.readFile(relPath, 'utf8');
const content = result.data;
const occurrences = content.split(oldString).length - 1;
if (occurrences === 0) {
return { error: 'oldString not found in file' };
}
if (occurrences > 1 && !replaceAll) {
return {
error: `oldString found ${occurrences} times. Use replaceAll: true or provide more context to make it unique.`
};
}
const newContent = replaceAll
? content.replaceAll(oldString, newString)
: content.replace(oldString, newString);
await workspace.writeFile(relPath, newContent, { encoding: 'utf8' });
return {
success: true,
replacements: replaceAll ? occurrences : 1
};
return await files.editText(filePath, oldString, newString, replaceAll);
} catch (error) {
return { error: error instanceof Error ? error.message : 'Unknown error' };
}
},
},
'workspace-mkdir': {
description: 'Create a directory in the workspace',
'file-mkdir': {
description: 'Create a directory',
inputSchema: z.object({
path: z.string().min(1).describe('Workspace-relative directory path'),
path: z.string().min(1).describe('Directory path to create'),
recursive: z.boolean().optional().describe('Create parent directories if needed (default: true)'),
}),
execute: async ({ path: relPath, recursive = true }: { path: string; recursive?: boolean }) => {
execute: async ({ path: filePath, recursive = true }: { path: string; recursive?: boolean }) => {
try {
return await workspace.mkdir(relPath, recursive);
return await files.mkdir(filePath, recursive);
} catch (error) {
return {
error: error instanceof Error ? error.message : 'Unknown error',
@ -470,16 +368,16 @@ export const BuiltinTools: z.infer<typeof BuiltinToolsSchema> = {
},
},
'workspace-rename': {
description: 'Rename or move a file or directory in the workspace',
'file-rename': {
description: 'Rename or move a file or directory',
inputSchema: z.object({
from: z.string().min(1).describe('Source workspace-relative path'),
to: z.string().min(1).describe('Destination workspace-relative path'),
from: z.string().min(1).describe('Source path'),
to: z.string().min(1).describe('Destination path'),
overwrite: z.boolean().optional().describe('Overwrite destination if it exists (default: false)'),
}),
execute: async ({ from, to, overwrite = false }: { from: string; to: string; overwrite?: boolean }) => {
try {
return await workspace.rename(from, to, overwrite);
return await files.rename(from, to, overwrite);
} catch (error) {
return {
error: error instanceof Error ? error.message : 'Unknown error',
@ -488,16 +386,16 @@ export const BuiltinTools: z.infer<typeof BuiltinToolsSchema> = {
},
},
'workspace-copy': {
description: 'Copy a file in the workspace (directories not supported)',
'file-copy': {
description: 'Copy a file (directories not supported)',
inputSchema: z.object({
from: z.string().min(1).describe('Source workspace-relative file path'),
to: z.string().min(1).describe('Destination workspace-relative file path'),
from: z.string().min(1).describe('Source file path'),
to: z.string().min(1).describe('Destination file path'),
overwrite: z.boolean().optional().describe('Overwrite destination if it exists (default: false)'),
}),
execute: async ({ from, to, overwrite = false }: { from: string; to: string; overwrite?: boolean }) => {
try {
return await workspace.copy(from, to, overwrite);
return await files.copy(from, to, overwrite);
} catch (error) {
return {
error: error instanceof Error ? error.message : 'Unknown error',
@ -506,16 +404,16 @@ export const BuiltinTools: z.infer<typeof BuiltinToolsSchema> = {
},
},
'workspace-remove': {
description: 'Remove a file or directory from the workspace. Files are moved to trash by default for safety.',
'file-remove': {
description: 'Remove a file or directory. Files are moved to the Rowboat trash by default for safety.',
inputSchema: z.object({
path: z.string().min(1).describe('Workspace-relative path to remove'),
path: z.string().min(1).describe('Path to remove'),
recursive: z.boolean().optional().describe('Required for directories (default: false)'),
trash: z.boolean().optional().describe('Move to trash instead of permanent delete (default: true)'),
}),
execute: async ({ path: relPath, recursive, trash }: { path: string; recursive?: boolean; trash?: boolean }) => {
execute: async ({ path: filePath, recursive, trash }: { path: string; recursive?: boolean; trash?: boolean }) => {
try {
return await workspace.remove(relPath, {
return await files.remove(filePath, {
recursive,
trash,
});
@ -527,45 +425,26 @@ export const BuiltinTools: z.infer<typeof BuiltinToolsSchema> = {
},
},
'workspace-glob': {
'file-glob': {
description: 'Find files matching a glob pattern (e.g., "**/*.ts", "src/**/*.json"). Much faster than recursive readdir for finding files.',
inputSchema: z.object({
pattern: z.string().describe('Glob pattern to match files'),
cwd: z.string().optional().describe('Subdirectory to search in, relative to workspace root (default: workspace root)'),
cwd: z.string().optional().describe('Directory to search in (default: default root)'),
}),
execute: async ({ pattern, cwd }: { pattern: string; cwd?: string }) => {
try {
const searchDir = cwd ? path.join(WorkDir, cwd) : WorkDir;
// Ensure search directory is within workspace
const resolvedSearchDir = path.resolve(searchDir);
if (!resolvedSearchDir.startsWith(WorkDir)) {
return { error: 'Search directory must be within workspace' };
}
const files = await glob(pattern, {
cwd: searchDir,
nodir: true,
ignore: ['node_modules/**', '.git/**'],
});
return {
files,
count: files.length,
pattern,
cwd: cwd || '.',
};
return await files.glob(pattern, cwd);
} catch (error) {
return { error: error instanceof Error ? error.message : 'Unknown error' };
}
},
},
'workspace-grep': {
description: 'Search file contents using regex. Returns matching files and lines. Uses ripgrep if available, falls back to grep.',
'file-grep': {
description: 'Search text file contents using regex. Returns matching files and lines. Skips binary files.',
inputSchema: z.object({
pattern: z.string().describe('Regex pattern to search for'),
searchPath: z.string().optional().describe('Directory or file to search, relative to workspace root (default: workspace root)'),
searchPath: z.string().optional().describe('Directory or file to search (default: default root)'),
fileGlob: z.string().optional().describe('File pattern filter (e.g., "*.ts", "*.md")'),
contextLines: z.number().optional().describe('Lines of context around matches (default: 0)'),
maxResults: z.number().optional().describe('Maximum results to return (default: 100)'),
@ -584,90 +463,7 @@ export const BuiltinTools: z.infer<typeof BuiltinToolsSchema> = {
maxResults?: number;
}) => {
try {
const targetPath = searchPath ? path.join(WorkDir, searchPath) : WorkDir;
// Ensure target path is within workspace
const resolvedTargetPath = path.resolve(targetPath);
if (!resolvedTargetPath.startsWith(WorkDir)) {
return { error: 'Search path must be within workspace' };
}
// Try ripgrep first
try {
const rgArgs = [
'--json',
'-e', JSON.stringify(pattern),
contextLines > 0 ? `-C ${contextLines}` : '',
fileGlob ? `--glob ${JSON.stringify(fileGlob)}` : '',
`--max-count ${maxResults}`,
'--ignore-case',
JSON.stringify(resolvedTargetPath),
].filter(Boolean).join(' ');
const output = execSync(`rg ${rgArgs}`, {
encoding: 'utf8',
maxBuffer: 10 * 1024 * 1024,
cwd: WorkDir,
});
const matches = output.trim().split('\n')
.filter(Boolean)
.map(line => {
try {
return JSON.parse(line);
} catch {
return null;
}
})
.filter(m => m && m.type === 'match');
return {
matches: matches.map(m => ({
file: path.relative(WorkDir, m.data.path.text),
line: m.data.line_number,
content: m.data.lines.text.trim(),
})),
count: matches.length,
tool: 'ripgrep',
};
} catch {
// Fallback to basic grep if ripgrep not available or failed
const grepArgs = [
'-rn',
fileGlob ? `--include=${JSON.stringify(fileGlob)}` : '',
JSON.stringify(pattern),
JSON.stringify(resolvedTargetPath),
`| head -${maxResults}`,
].filter(Boolean).join(' ');
try {
const output = execSync(`grep ${grepArgs}`, {
encoding: 'utf8',
maxBuffer: 10 * 1024 * 1024,
shell: '/bin/sh',
});
const lines = output.trim().split('\n').filter(Boolean);
return {
matches: lines.map(line => {
const match = line.match(/^(.+?):(\d+):(.*)$/);
if (match) {
return {
file: path.relative(WorkDir, match[1]),
line: parseInt(match[2], 10),
content: match[3].trim(),
};
}
return { file: '', line: 0, content: line };
}),
count: lines.length,
tool: 'grep',
};
} catch {
// No matches found (grep returns non-zero on no matches)
return { matches: [], count: 0, tool: 'grep' };
}
}
return await files.grep({ pattern, searchPath, fileGlob, contextLines, maxResults });
} catch (error) {
return { error: error instanceof Error ? error.message : 'Unknown error' };
}
@ -677,7 +473,7 @@ export const BuiltinTools: z.infer<typeof BuiltinToolsSchema> = {
'parseFile': {
description: 'Parse and extract text content from files (PDF, Excel, CSV, Word .docx). Auto-detects format from file extension.',
inputSchema: z.object({
path: z.string().min(1).describe('File path to parse. Can be an absolute path or a workspace-relative path.'),
path: z.string().min(1).describe('File path to parse. Can be absolute, ~/..., or relative to the default root.'),
}),
execute: async ({ path: filePath }: { path: string }) => {
try {
@ -692,14 +488,7 @@ export const BuiltinTools: z.infer<typeof BuiltinToolsSchema> = {
};
}
// Read file as buffer — support both absolute and workspace-relative paths
let buffer: Buffer;
if (path.isAbsolute(filePath)) {
buffer = await fs.readFile(filePath);
} else {
const result = await workspace.readFile(filePath, 'base64');
buffer = Buffer.from(result.data, 'base64');
}
const { buffer, resolvedPath } = await files.readBuffer(filePath);
if (ext === '.pdf') {
const { PDFParse } = await _importDynamic("pdf-parse");
@ -716,6 +505,7 @@ export const BuiltinTools: z.infer<typeof BuiltinToolsSchema> = {
pages: textResult.total,
title: infoResult.info?.Title || undefined,
author: infoResult.info?.Author || undefined,
resolvedPath,
},
};
} finally {
@ -785,7 +575,7 @@ export const BuiltinTools: z.infer<typeof BuiltinToolsSchema> = {
'LLMParse': {
description: 'Send a file to the configured LLM as a multimodal attachment and ask it to extract content as markdown. Best for scanned PDFs, images with text, complex layouts, or any format where local parsing falls short. Supports documents (PDF, Word, Excel, PowerPoint, CSV, TXT, HTML) and images (PNG, JPG, GIF, WebP, SVG, BMP, TIFF).',
inputSchema: z.object({
path: z.string().min(1).describe('File path to parse. Can be an absolute path or a workspace-relative path.'),
path: z.string().min(1).describe('File path to parse. Can be absolute, ~/..., or relative to the default root.'),
prompt: z.string().optional().describe('Custom instruction for the LLM (defaults to "Convert this file to well-structured markdown.")'),
}),
execute: async ({ path: filePath, prompt }: { path: string; prompt?: string }) => {
@ -801,14 +591,7 @@ export const BuiltinTools: z.infer<typeof BuiltinToolsSchema> = {
};
}
// Read file as buffer — support both absolute and workspace-relative paths
let buffer: Buffer;
if (path.isAbsolute(filePath)) {
buffer = await fs.readFile(filePath);
} else {
const result = await workspace.readFile(filePath, 'base64');
buffer = Buffer.from(result.data, 'base64');
}
const { buffer } = await files.readBuffer(filePath);
const base64 = buffer.toString('base64');
@ -1068,7 +851,7 @@ export const BuiltinTools: z.infer<typeof BuiltinToolsSchema> = {
}
// Fallback to original for backward compatibility
const result = await executeCommand(command, { cwd: workingDir });
const result = await executeCommand(command, { cwd: workingDir, env: envOverride });
return {
success: result.exitCode === 0,
@ -1228,7 +1011,7 @@ export const BuiltinTools: z.infer<typeof BuiltinToolsSchema> = {
case 'open-note': {
const filePath = input.path as string;
try {
const result = await workspace.exists(filePath);
const result = await files.exists(filePath);
if (!result.exists) {
return { success: false, error: `File not found: ${filePath}` };
}
@ -1256,15 +1039,15 @@ export const BuiltinTools: z.infer<typeof BuiltinToolsSchema> = {
// Scan knowledge/ files and extract frontmatter properties
try {
const { parseFrontmatter } = await import("@x/shared/dist/frontmatter.js");
const entries = await workspace.readdir("knowledge", { recursive: true, allowedExtensions: [".md"] });
const files = entries.filter(e => e.kind === 'file');
const entries = await files.list("knowledge", { recursive: true, allowedExtensions: [".md"] });
const noteFiles = entries.filter(e => e.kind === 'file');
const properties = new Map<string, Set<string>>();
let noteCount = 0;
for (const file of files) {
for (const file of noteFiles) {
try {
const { data } = await workspace.readFile(file.path);
const { fields } = parseFrontmatter(data);
const result = await fs.readFile(file.resolvedPath, 'utf8');
const { fields } = parseFrontmatter(result);
noteCount++;
for (const [key, value] of Object.entries(fields)) {
if (!value) continue;
@ -1309,7 +1092,7 @@ export const BuiltinTools: z.infer<typeof BuiltinToolsSchema> = {
const basePath = `bases/${safeName}.base`;
try {
const config = { name: safeName, filters: [], columns: [] };
await workspace.writeFile(basePath, JSON.stringify(config, null, 2), { mkdirp: true });
await files.writeText(basePath, JSON.stringify(config, null, 2), { mkdirp: true });
return { success: true, action: 'create-base', name: safeName, path: basePath };
} catch (error) {
return {
@ -1655,7 +1438,7 @@ export const BuiltinTools: z.infer<typeof BuiltinToolsSchema> = {
},
'create-background-task': {
description: "Create a new background task on disk. This is the tool you call to materialize a bg-task — do NOT try to write `task.yaml` yourself with workspace-edit, and do NOT search the codebase for IPC channels like `bg-task:create`. The framework slugifies the name and lays out `bg-tasks/<slug>/{task.yaml,index.md,runs/}`. After this returns, immediately call `run-background-task-agent` with the returned slug so the user sees content right away.",
description: "Create a new background task on disk. This is the tool you call to materialize a bg-task — do NOT try to write `task.yaml` yourself with file-editText, and do NOT search the codebase for IPC channels like `bg-task:create`. The framework slugifies the name and lays out `bg-tasks/<slug>/{task.yaml,index.md,runs/}`. After this returns, immediately call `run-background-task-agent` with the returned slug so the user sees content right away.",
inputSchema: CreateBackgroundTaskInput,
execute: async (input: z.infer<typeof CreateBackgroundTaskInput>) => {
try {
@ -1675,7 +1458,7 @@ export const BuiltinTools: z.infer<typeof BuiltinToolsSchema> = {
},
'patch-background-task': {
description: "Update an existing background task — instructions, triggers, active, or model/provider. Use this when the user's new ask overlaps with an existing task (extend-don't-fork): rewrite the instructions in full to absorb the new ask rather than creating a duplicate sibling task. Look up existing tasks with `workspace-glob` on `bg-tasks/*/task.yaml` and `workspace-readFile` on the candidates first.",
description: "Update an existing background task — instructions, triggers, active, or model/provider. Use this when the user's new ask overlaps with an existing task (extend-don't-fork): rewrite the instructions in full to absorb the new ask rather than creating a duplicate sibling task. Look up existing tasks with `file-glob` on `bg-tasks/*/task.yaml` and `file-readText` on the candidates first.",
inputSchema: PatchBackgroundTaskInput,
execute: async (input: z.infer<typeof PatchBackgroundTaskInput>) => {
try {

View file

@ -80,6 +80,7 @@ export async function executeCommand(
cwd?: string;
timeout?: number; // timeout in milliseconds
maxBuffer?: number; // max buffer size in bytes
env?: NodeJS.ProcessEnv; // override environment (e.g. CLAUDE_CODE_EXECUTABLE for acpx)
}
): Promise<CommandResult> {
try {
@ -89,6 +90,7 @@ export async function executeCommand(
timeout: options?.timeout,
maxBuffer: options?.maxBuffer || 1024 * 1024, // default 1MB
shell,
env: options?.env,
});
return {

View file

@ -8,17 +8,20 @@ export type MiddlePaneContext =
| { kind: 'note'; path: string; content: string }
| { kind: 'browser'; url: string; title: string };
export type CodeMode = 'claude' | 'codex';
type EnqueuedMessage = {
messageId: string;
message: UserMessageContentType;
voiceInput?: boolean;
voiceOutput?: VoiceOutputMode;
searchEnabled?: boolean;
codeMode?: CodeMode;
middlePaneContext?: MiddlePaneContext;
};
export interface IMessageQueue {
enqueue(runId: string, message: UserMessageContentType, voiceInput?: boolean, voiceOutput?: VoiceOutputMode, searchEnabled?: boolean, middlePaneContext?: MiddlePaneContext): Promise<string>;
enqueue(runId: string, message: UserMessageContentType, voiceInput?: boolean, voiceOutput?: VoiceOutputMode, searchEnabled?: boolean, middlePaneContext?: MiddlePaneContext, codeMode?: CodeMode): Promise<string>;
dequeue(runId: string): Promise<EnqueuedMessage | null>;
}
@ -34,7 +37,7 @@ export class InMemoryMessageQueue implements IMessageQueue {
this.idGenerator = idGenerator;
}
async enqueue(runId: string, message: UserMessageContentType, voiceInput?: boolean, voiceOutput?: VoiceOutputMode, searchEnabled?: boolean, middlePaneContext?: MiddlePaneContext): Promise<string> {
async enqueue(runId: string, message: UserMessageContentType, voiceInput?: boolean, voiceOutput?: VoiceOutputMode, searchEnabled?: boolean, middlePaneContext?: MiddlePaneContext, codeMode?: CodeMode): Promise<string> {
if (!this.store[runId]) {
this.store[runId] = [];
}
@ -45,6 +48,7 @@ export class InMemoryMessageQueue implements IMessageQueue {
voiceInput,
voiceOutput,
searchEnabled,
codeMode,
middlePaneContext,
});
return id;

View file

@ -3,14 +3,21 @@ import fs from 'fs/promises';
import path from 'path';
import { ClientRegistrationResponse } from './types.js';
export const DEFAULT_CALLBACK_PORT = 8080;
export interface IClientRegistrationRepo {
getClientRegistration(provider: string): Promise<ClientRegistrationResponse | null>;
saveClientRegistration(provider: string, registration: ClientRegistrationResponse): Promise<void>;
/** Returns the port that was used when DCR-registering this provider, or DEFAULT_CALLBACK_PORT if not stored. */
getRegisteredPort(provider: string): Promise<number>;
saveClientRegistration(provider: string, registration: ClientRegistrationResponse, port: number): Promise<void>;
clearClientRegistration(provider: string): Promise<void>;
}
// _registeredPort is our private field — stripped by Zod when we parse the RFC response fields
type StoredEntry = Record<string, unknown> & { _registeredPort?: number };
type ClientRegistrationStorage = {
[provider: string]: ClientRegistrationResponse;
[provider: string]: StoredEntry;
};
export class FSClientRegistrationRepo implements IClientRegistrationRepo {
@ -45,14 +52,14 @@ export class FSClientRegistrationRepo implements IClientRegistrationRepo {
async getClientRegistration(provider: string): Promise<ClientRegistrationResponse | null> {
const config = await this.readConfig();
const registration = config[provider];
if (!registration) {
const entry = config[provider];
if (!entry) {
return null;
}
// Validate registration structure
// Validate registration structure (Zod strips unknown fields like _registeredPort)
try {
return ClientRegistrationResponse.parse(registration);
return ClientRegistrationResponse.parse(entry);
} catch {
// Invalid registration, remove it
await this.clearClientRegistration(provider);
@ -60,9 +67,14 @@ export class FSClientRegistrationRepo implements IClientRegistrationRepo {
}
}
async saveClientRegistration(provider: string, registration: ClientRegistrationResponse): Promise<void> {
async getRegisteredPort(provider: string): Promise<number> {
const config = await this.readConfig();
config[provider] = registration;
return config[provider]?._registeredPort ?? DEFAULT_CALLBACK_PORT;
}
async saveClientRegistration(provider: string, registration: ClientRegistrationResponse, port: number): Promise<void> {
const config = await this.readConfig();
config[provider] = { ...registration, _registeredPort: port };
await this.writeConfig(config);
}

View file

@ -26,6 +26,25 @@ export class ReconnectRequiredError extends Error {
}
}
/**
* Thrown when the api signals a transient failure (rate limit, in-flight dedup,
* upstream 5xx) caller should leave stored tokens untouched and retry on its
* next tick rather than flagging the user for reconnect.
*
* In particular: the backend returns 429 with `Refresh in progress, retry shortly`
* when two desktop clients race the same refresh; the proactive in-flight dedup
* in GoogleClientFactory should make that unreachable, but this is the safety
* net if it ever isn't.
*/
export class TransientRefreshError extends Error {
readonly status: number;
constructor(message: string, status: number) {
super(message);
this.name = "TransientRefreshError";
this.status = status;
}
}
interface ApiTokenResponse {
access_token: string;
refresh_token?: string;
@ -104,6 +123,17 @@ export async function refreshTokensViaBackend(
}
throw new Error(`refresh failed: 409 ${err.error ?? ""}`.trim());
}
// 429 = backend dedup said another refresh is in flight; 5xx = upstream
// hiccup. Either way the local tokens are still valid for the next attempt
// — surface as TransientRefreshError so the factory doesn't write a stuck
// error into oauth.json.
if (res.status === 429 || res.status >= 500) {
const err = await readError(res);
throw new TransientRefreshError(
`refresh failed: ${res.status} ${err.error ?? ""}`.trim(),
res.status,
);
}
if (!res.ok) {
const err = await readError(res);
throw new Error(`refresh failed: ${res.status} ${err.error ?? ""}`.trim());

View file

@ -75,9 +75,8 @@ const providerConfigs: ProviderConfig = {
mode: 'static',
},
scopes: [
'https://www.googleapis.com/auth/gmail.readonly',
'https://www.googleapis.com/auth/gmail.modify',
'https://www.googleapis.com/auth/calendar.events.readonly',
'https://www.googleapis.com/auth/drive.readonly',
],
},
'fireflies-ai': {
@ -119,4 +118,3 @@ export async function getProviderConfig(providerName: string): Promise<ProviderC
export function getAvailableProviders(): string[] {
return Object.keys(providerConfigs);
}

View file

@ -15,7 +15,7 @@ You are running with **no user present** to clarify, approve, or watch.
Your task folder is \`bg-tasks/<slug>/\` (the path is given in the run message). It contains:
- \`task.yaml\` — the spec. **Never touch this.** The runtime owns it.
- \`index.md\` — agent-owned. You read and write this freely via \`workspace-readFile\` / \`workspace-edit\`.
- \`index.md\` — agent-owned. You read and write this freely via \`file-readText\` / \`file-editText\`.
- \`runs/\` — your own run logs (jsonl). You don't write to it directly; the runtime does.
You can also read and write anywhere else under the workspace (\`knowledge/\`, etc.) when your instructions call for it.
@ -26,7 +26,7 @@ OUTPUT MODE — keep \`index.md\` aligned to the instructions.
Use when instructions imply a **current state** artifact:
- "Maintain / show / summarize / track / digest of / dashboard for / brief on …"
- "Keep me posted on …" / "What's the latest on …"
On every run: \`workspace-readFile\` \`index.md\`, decide the smallest patch that brings it into alignment with the instructions, apply with \`workspace-edit\`. Patch-style discipline: edit one region, re-read, then edit the next. Avoid one-shot rewrites.
On every run: \`file-readText\` \`index.md\`, decide the smallest patch that brings it into alignment with the instructions, apply with \`file-editText\`. Patch-style discipline: edit one region, re-read, then edit the next. Avoid one-shot rewrites.
ACTION MODE perform a side-effect, append a journal entry.
Use when instructions imply a **recurring action**:

View file

@ -50,7 +50,7 @@ function buildMessage(
**Instructions:**
${task.instructions}
Your task folder is \`${wsFolder}\`. The user-visible artifact is \`${wsFolder}index.md\` — read it with \`workspace-readFile\` and update it with \`workspace-edit\` per the OUTPUT / ACTION mode rule. Do not touch \`${wsFolder}task.yaml\` (the runtime owns it).`;
Your task folder is \`${wsFolder}\`. The user-visible artifact is \`${wsFolder}index.md\` — read it with \`file-readText\` and update it with \`file-editText\` per the OUTPUT / ACTION mode rule. Do not touch \`${wsFolder}task.yaml\` (the runtime owns it).`;
return baseMessage + buildTriggerBlock({
trigger,

View file

@ -20,8 +20,17 @@ export async function getBillingInfo(): Promise<BillingInfo> {
status: string | null;
trialExpiresAt: string | null;
usage: {
sanctionedCredits: number;
availableCredits: number;
monthly: {
sanctionedCredits: number;
usedCredits: number;
availableCredits: number;
};
daily: {
sanctionedCredits: number;
usedCredits: number;
availableCredits: number;
usageDay: string;
};
};
};
};
@ -31,7 +40,7 @@ export async function getBillingInfo(): Promise<BillingInfo> {
subscriptionPlan: body.billing.plan,
subscriptionStatus: body.billing.status,
trialExpiresAt: body.billing.trialExpiresAt ?? null,
sanctionedCredits: body.billing.usage.sanctionedCredits,
availableCredits: body.billing.usage.availableCredits,
monthly: body.billing.usage.monthly,
daily: body.billing.usage.daily,
};
}

View file

@ -0,0 +1,3 @@
export { CodeModeConfig, CodeModeAgentStatus, AgentStatus } from './types.js';
export { FSCodeModeConfigRepo, type ICodeModeConfigRepo } from './repo.js';
export { checkCodeModeAgentStatus } from './status.js';

View file

@ -0,0 +1,47 @@
import fs from 'fs/promises';
import path from 'path';
import { WorkDir } from '../config/config.js';
import { CodeModeConfig } from './types.js';
import { checkCodeModeAgentStatus } from './status.js';
export interface ICodeModeConfigRepo {
getConfig(): Promise<CodeModeConfig>;
setConfig(config: CodeModeConfig): Promise<void>;
}
export class FSCodeModeConfigRepo implements ICodeModeConfigRepo {
private readonly configPath = path.join(WorkDir, 'config', 'code-mode.json');
private agentReadyPromise: Promise<boolean> | null = null;
// Reuse the existing agent check (Claude Code / Codex installed + signed in),
// cached for the process lifetime so we probe (shell + keychain) at most once
// per session rather than on every getConfig call.
private agentReady(): Promise<boolean> {
if (!this.agentReadyPromise) {
this.agentReadyPromise = checkCodeModeAgentStatus()
.then((s) =>
(s.claude.installed && s.claude.signedIn)
|| (s.codex.installed && s.codex.signedIn))
.catch(() => false);
}
return this.agentReadyPromise;
}
async getConfig(): Promise<CodeModeConfig> {
try {
// The file only exists once the user has explicitly toggled code mode
// in settings — always honor that choice.
const content = await fs.readFile(this.configPath, 'utf8');
return CodeModeConfig.parse(JSON.parse(content));
} catch {
// No explicit choice yet: enable automatically when a coding agent is ready.
return { enabled: await this.agentReady() };
}
}
async setConfig(config: CodeModeConfig): Promise<void> {
const validated = CodeModeConfig.parse(config);
await fs.mkdir(path.dirname(this.configPath), { recursive: true });
await fs.writeFile(this.configPath, JSON.stringify(validated, null, 2));
}
}

View file

@ -0,0 +1,199 @@
import { exec } from 'child_process';
import { promisify } from 'util';
import os from 'os';
import path from 'path';
import fs from 'fs/promises';
import { existsSync } from 'fs';
import { CodeModeAgentStatus } from './types.js';
const execAsync = promisify(exec);
// Where claude.cmd / codex.cmd typically live when installed via npm/pnpm/yarn.
// We scan these directly because Electron's spawned shell sometimes doesn't
// inherit the user's full PATH (especially on macOS GUI launches, and even on
// Windows when global npm prefix isn't propagated to system PATH).
function commonInstallPaths(binary: string): string[] {
const home = os.homedir();
if (process.platform === 'win32') {
const appData = process.env.APPDATA || path.join(home, 'AppData', 'Roaming');
const localAppData = process.env.LOCALAPPDATA || path.join(home, 'AppData', 'Local');
const programFiles = process.env.ProgramFiles || 'C:\\Program Files';
return [
path.join(appData, 'npm', `${binary}.cmd`),
path.join(appData, 'npm', `${binary}.exe`),
path.join(localAppData, 'npm', `${binary}.cmd`),
path.join(localAppData, 'pnpm', `${binary}.cmd`),
path.join(home, 'AppData', 'Roaming', 'pnpm', `${binary}.cmd`),
path.join(programFiles, 'nodejs', `${binary}.cmd`),
path.join(home, '.volta', 'bin', `${binary}.cmd`),
];
}
return [
'/usr/local/bin',
'/opt/homebrew/bin', // Apple Silicon Homebrew
'/usr/bin',
path.join(home, '.npm-global', 'bin'),
path.join(home, '.local', 'bin'),
path.join(home, '.volta', 'bin'),
path.join(home, '.nvm', 'versions', 'node'), // partial; nvm has versioned subdirs
path.join(home, 'bin'),
].map(dir => path.join(dir, binary));
}
async function probeShell(binary: string): Promise<boolean> {
try {
if (process.platform === 'win32') {
const { stdout } = await execAsync(`where ${binary}`, { timeout: 5000 });
return stdout.trim().length > 0;
}
// Login shell so ~/.zprofile / ~/.bashrc PATH additions are visible —
// essential for Homebrew, nvm, asdf, volta installs on macOS GUI launches.
const { stdout } = await execAsync(`/bin/sh -lc 'command -v ${binary}'`, { timeout: 5000 });
return stdout.trim().length > 0;
} catch {
return false;
}
}
async function isInstalled(binary: string): Promise<boolean> {
if (await probeShell(binary)) return true;
// Fallback: scan well-known install locations directly.
for (const candidate of commonInstallPaths(binary)) {
if (existsSync(candidate)) return true;
}
return false;
}
function decodeJwtPayload(token: string): Record<string, unknown> | null {
try {
const parts = token.split('.');
if (parts.length < 2) return null;
const padded = parts[1].replace(/-/g, '+').replace(/_/g, '/');
const pad = padded.length % 4 === 0 ? '' : '='.repeat(4 - (padded.length % 4));
const json = Buffer.from(padded + pad, 'base64').toString('utf-8');
const parsed = JSON.parse(json);
return typeof parsed === 'object' && parsed !== null ? parsed as Record<string, unknown> : null;
} catch {
return null;
}
}
// Given the raw credentials JSON (from a file or the macOS Keychain), decide
// whether it represents a usable signed-in state: a valid API key, an unexpired
// access token, or a refresh token (which can mint a new access token).
function isClaudeCredentialSignedIn(raw: string): boolean {
try {
const parsed = JSON.parse(raw) as Record<string, unknown>;
const oauth = parsed.claudeAiOauth as Record<string, unknown> | undefined;
if (oauth) {
const access = typeof oauth.accessToken === 'string' ? oauth.accessToken : '';
const refresh = typeof oauth.refreshToken === 'string' ? oauth.refreshToken : '';
if (refresh.length > 0) return true;
if (access.length > 0) {
if (typeof oauth.expiresAt === 'number' && oauth.expiresAt > 0 && oauth.expiresAt < Date.now()) {
return false;
}
return true;
}
}
if (typeof parsed.apiKey === 'string' && parsed.apiKey.length > 10) return true;
if (typeof parsed.accessToken === 'string' && parsed.accessToken.length > 10) return true;
} catch {
// malformed JSON
}
return false;
}
// Reads Claude Code's credentials from the macOS login Keychain, where the
// CLI stores them on macOS (service "Claude Code-credentials"). On Linux/Windows
// it uses the ~/.claude/.credentials.json file instead, so this is a no-op there.
//
// Caveats:
// - The first read by this app (a different binary than the `claude` CLI that
// created the item) triggers a one-time macOS authorization dialog; the user
// must "Always Allow". Headless/SSH sessions can't show it and will fail.
// - If CLAUDE_CONFIG_DIR is set, Claude appends a SHA-256 suffix to the service
// name, which this lookup won't match — such setups usually keep the file too.
async function readClaudeKeychainCredential(): Promise<string | null> {
if (process.platform !== 'darwin') return null;
try {
const { stdout } = await execAsync(
`security find-generic-password -s "Claude Code-credentials" -w`,
{ timeout: 5000 },
);
const out = stdout.trim();
return out.length > 0 ? out : null;
} catch {
// not present in keychain
return null;
}
}
// Validates Claude Code auth. On macOS the credentials live in the login
// Keychain; on Linux/Windows in ~/.claude/.credentials.json (or ~/.config
// fallback). We check both so detection works across platforms.
async function checkClaudeSignedIn(): Promise<boolean> {
const home = os.homedir();
const candidates = [
path.join(home, '.claude', '.credentials.json'),
path.join(home, '.config', 'claude', '.credentials.json'),
];
for (const full of candidates) {
try {
const raw = await fs.readFile(full, 'utf-8');
if (isClaudeCredentialSignedIn(raw)) return true;
} catch {
// try next candidate
}
}
// macOS: credentials are stored in the Keychain rather than on disk.
const keychainRaw = await readClaudeKeychainCredential();
if (keychainRaw && isClaudeCredentialSignedIn(keychainRaw)) return true;
return false;
}
// Validates Codex auth at ~/.codex/auth.json on all platforms.
// Considered signed in if API key set, or a refresh_token / access_token
// exists. id_token expiry is intentionally NOT used as a rejection signal —
// id_tokens are short-lived (~1h) but refresh_tokens persist for weeks.
async function checkCodexSignedIn(): Promise<boolean> {
const home = os.homedir();
const full = path.join(home, '.codex', 'auth.json');
try {
const raw = await fs.readFile(full, 'utf-8');
const parsed = JSON.parse(raw) as Record<string, unknown>;
if (typeof parsed.OPENAI_API_KEY === 'string' && parsed.OPENAI_API_KEY.length > 10) return true;
const tokens = parsed.tokens as Record<string, unknown> | undefined;
if (tokens) {
const refresh = typeof tokens.refresh_token === 'string' ? tokens.refresh_token : '';
const access = typeof tokens.access_token === 'string' ? tokens.access_token : '';
const id = typeof tokens.id_token === 'string' ? tokens.id_token : '';
if (refresh.length > 0 || access.length > 0 || id.length > 0) return true;
}
} catch {
// file missing or unreadable
}
return false;
}
// Exported for diagnostics — silenced unused-var warning by re-export only.
export { decodeJwtPayload };
export async function checkCodeModeAgentStatus(): Promise<CodeModeAgentStatus> {
const [claudeInstalled, codexInstalled, claudeSignedIn, codexSignedIn] = await Promise.all([
isInstalled('claude'),
isInstalled('codex'),
checkClaudeSignedIn(),
checkCodexSignedIn(),
]);
return {
claude: { installed: claudeInstalled, signedIn: claudeSignedIn },
codex: { installed: codexInstalled, signedIn: codexSignedIn },
};
}

View file

@ -0,0 +1,18 @@
import z from "zod";
export const CodeModeConfig = z.object({
enabled: z.boolean(),
});
export type CodeModeConfig = z.infer<typeof CodeModeConfig>;
export const AgentStatus = z.object({
installed: z.boolean(),
signedIn: z.boolean(),
});
export type AgentStatus = z.infer<typeof AgentStatus>;
export const CodeModeAgentStatus = z.object({
claude: AgentStatus,
codex: AgentStatus,
});
export type CodeModeAgentStatus = z.infer<typeof CodeModeAgentStatus>;

View file

@ -42,26 +42,61 @@ const DEFAULT_ALLOW_LIST = [
"yq"
]
export type FileAccessOperation = "read" | "list" | "search" | "write" | "delete";
export type FileAccessGrant = {
operation: FileAccessOperation;
pathPrefix: string;
};
let cachedAllowList: string[] | null = null;
let cachedFileAccessAllowList: FileAccessGrant[] | null = null;
let cachedMtimeMs: number | null = null;
export async function addToSecurityConfig(commands: string[]): Promise<void> {
ensureSecurityConfigSync();
const current = readAllowList();
const merged = new Set(current);
const current = readSecurityConfig();
const merged = new Set(current.allowedCommands);
for (const cmd of commands) {
const normalized = cmd.trim().toLowerCase();
if (normalized) merged.add(normalized);
}
await fsPromises.writeFile(
SECURITY_CONFIG_PATH,
JSON.stringify(Array.from(merged).sort(), null, 2) + "\n",
JSON.stringify({
allowedCommands: Array.from(merged).sort(),
allowedFileAccess: current.allowedFileAccess,
}, null, 2) + "\n",
"utf8",
);
// Reset cache so next read picks up the new file
resetSecurityAllowListCache();
}
export async function addFileAccessGrant(grant: FileAccessGrant): Promise<void> {
ensureSecurityConfigSync();
const current = readSecurityConfig();
const normalizedGrant = normalizeFileAccessGrant(grant);
const exists = current.allowedFileAccess.some(existing =>
existing.operation === normalizedGrant.operation
&& existing.pathPrefix === normalizedGrant.pathPrefix
);
const allowedFileAccess = exists
? current.allowedFileAccess
: [...current.allowedFileAccess, normalizedGrant].sort((a, b) =>
`${a.operation}:${a.pathPrefix}`.localeCompare(`${b.operation}:${b.pathPrefix}`)
);
await fsPromises.writeFile(
SECURITY_CONFIG_PATH,
JSON.stringify({
allowedCommands: current.allowedCommands,
allowedFileAccess,
}, null, 2) + "\n",
"utf8",
);
resetSecurityAllowListCache();
}
/**
* Async function to ensure security config file exists.
* Called explicitly at app startup via initConfigs().
@ -103,28 +138,74 @@ function normalizeList(commands: unknown[]): string[] {
return Array.from(seen);
}
function parseSecurityPayload(payload: unknown): string[] {
function normalizeFileAccessGrant(grant: FileAccessGrant): FileAccessGrant {
return {
operation: grant.operation,
pathPrefix: path.resolve(grant.pathPrefix),
};
}
function normalizeFileAccessList(grants: unknown[]): FileAccessGrant[] {
const seen = new Set<string>();
const normalized: FileAccessGrant[] = [];
for (const entry of grants) {
if (!entry || typeof entry !== "object") continue;
const maybeGrant = entry as Record<string, unknown>;
const operation = maybeGrant.operation;
const pathPrefix = maybeGrant.pathPrefix;
if (
operation !== "read"
&& operation !== "list"
&& operation !== "search"
&& operation !== "write"
&& operation !== "delete"
) {
continue;
}
if (typeof pathPrefix !== "string" || !pathPrefix.trim()) continue;
const grant = normalizeFileAccessGrant({ operation, pathPrefix });
const key = `${grant.operation}:${grant.pathPrefix}`;
if (seen.has(key)) continue;
seen.add(key);
normalized.push(grant);
}
return normalized;
}
function parseSecurityPayload(payload: unknown): { allowedCommands: string[]; allowedFileAccess: FileAccessGrant[] } {
if (Array.isArray(payload)) {
return normalizeList(payload);
return { allowedCommands: normalizeList(payload), allowedFileAccess: [] };
}
if (payload && typeof payload === "object") {
const maybeObject = payload as Record<string, unknown>;
if (Array.isArray(maybeObject.allowedCommands)) {
return normalizeList(maybeObject.allowedCommands);
const allowedFileAccess = Array.isArray(maybeObject.allowedFileAccess)
? normalizeFileAccessList(maybeObject.allowedFileAccess)
: [];
if (Array.isArray(maybeObject.allowedCommands) || Array.isArray(maybeObject.allowedFileAccess)) {
return {
allowedCommands: Array.isArray(maybeObject.allowedCommands)
? normalizeList(maybeObject.allowedCommands)
: [],
allowedFileAccess,
};
}
const dynamicList = Object.entries(maybeObject)
.filter(([, value]) => Boolean(value))
.map(([key]) => key);
return normalizeList(dynamicList);
return {
allowedCommands: normalizeList(dynamicList),
allowedFileAccess,
};
}
return [];
return { allowedCommands: [], allowedFileAccess: [] };
}
function readAllowList(): string[] {
function readSecurityConfig(): { allowedCommands: string[]; allowedFileAccess: FileAccessGrant[] } {
ensureSecurityConfigSync();
try {
@ -133,10 +214,14 @@ function readAllowList(): string[] {
return parseSecurityPayload(parsed);
} catch (error) {
console.warn(`Failed to read security config at ${SECURITY_CONFIG_PATH}: ${error instanceof Error ? error.message : error}`);
return DEFAULT_ALLOW_LIST;
return { allowedCommands: DEFAULT_ALLOW_LIST, allowedFileAccess: [] };
}
}
function readAllowList(): string[] {
return readSecurityConfig().allowedCommands;
}
export function getSecurityAllowList(): string[] {
ensureSecurityConfigSync();
try {
@ -149,12 +234,32 @@ export function getSecurityAllowList(): string[] {
return cachedAllowList;
} catch {
cachedAllowList = null;
cachedFileAccessAllowList = null;
cachedMtimeMs = null;
return readAllowList();
}
}
export function getFileAccessAllowList(): FileAccessGrant[] {
ensureSecurityConfigSync();
try {
const stats = fs.statSync(SECURITY_CONFIG_PATH);
if (cachedFileAccessAllowList && cachedMtimeMs === stats.mtimeMs) {
return cachedFileAccessAllowList;
}
cachedFileAccessAllowList = readSecurityConfig().allowedFileAccess;
cachedMtimeMs = stats.mtimeMs;
return cachedFileAccessAllowList;
} catch {
cachedAllowList = null;
cachedFileAccessAllowList = null;
cachedMtimeMs = null;
return readSecurityConfig().allowedFileAccess;
}
}
export function resetSecurityAllowListCache() {
cachedAllowList = null;
cachedFileAccessAllowList = null;
cachedMtimeMs = null;
}

View file

@ -11,6 +11,7 @@ import { IAgentRuntime, AgentRuntime } from "../agents/runtime.js";
import { FSOAuthRepo, IOAuthRepo } from "../auth/repo.js";
import { FSClientRegistrationRepo, IClientRegistrationRepo } from "../auth/client-repo.js";
import { FSGranolaConfigRepo, IGranolaConfigRepo } from "../knowledge/granola/repo.js";
import { FSCodeModeConfigRepo, ICodeModeConfigRepo } from "../code-mode/repo.js";
import { IAbortRegistry, InMemoryAbortRegistry } from "../runs/abort-registry.js";
import { FSAgentScheduleRepo, IAgentScheduleRepo } from "../agent-schedule/repo.js";
import { FSAgentScheduleStateRepo, IAgentScheduleStateRepo } from "../agent-schedule/state-repo.js";
@ -38,6 +39,7 @@ container.register({
oauthRepo: asClass<IOAuthRepo>(FSOAuthRepo).singleton(),
clientRegistrationRepo: asClass<IClientRegistrationRepo>(FSClientRegistrationRepo).singleton(),
granolaConfigRepo: asClass<IGranolaConfigRepo>(FSGranolaConfigRepo).singleton(),
codeModeConfigRepo: asClass<ICodeModeConfigRepo>(FSCodeModeConfigRepo).singleton(),
agentScheduleRepo: asClass<IAgentScheduleRepo>(FSAgentScheduleRepo).singleton(),
agentScheduleStateRepo: asClass<IAgentScheduleStateRepo>(FSAgentScheduleStateRepo).singleton(),
slackConfigRepo: asClass<ISlackConfigRepo>(FSSlackConfigRepo).singleton(),

View file

@ -0,0 +1,204 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
let tmpDir: string;
let workspaceDir: string;
beforeEach(async () => {
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "rowboat-files-test-"));
workspaceDir = path.join(tmpDir, "workspace");
process.env.ROWBOAT_WORKDIR = workspaceDir;
vi.resetModules();
vi.doMock("../knowledge/version_history.js", () => ({
commitAll: vi.fn(async () => undefined),
initRepo: vi.fn(async () => undefined),
}));
vi.doMock("../knowledge/deprecate_today_note.js", () => ({
deprecateTodayNote: vi.fn(async () => undefined),
}));
});
afterEach(async () => {
delete process.env.ROWBOAT_WORKDIR;
vi.doUnmock("../knowledge/version_history.js");
vi.doUnmock("../knowledge/deprecate_today_note.js");
vi.resetModules();
await fs.rm(tmpDir, { recursive: true, force: true });
});
async function loadFiles() {
return import("./files.js");
}
describe("filesystem files", () => {
it("resolves relative paths inside ROWBOAT_WORKDIR", async () => {
const files = await loadFiles();
const resolved = files.resolveFilePath("notes/example.md");
expect(resolved.originalPath).toBe("notes/example.md");
expect(resolved.resolvedPath).toBe(path.join(workspaceDir, "notes", "example.md"));
expect(resolved.isInsideWorkspace).toBe(true);
expect(resolved.workspaceRelPath).toBe("notes/example.md");
});
it("keeps absolute paths outside the workspace absolute", async () => {
const files = await loadFiles();
const absolutePath = path.join(tmpDir, "outside.txt");
const resolved = files.resolveFilePath(absolutePath);
expect(resolved.resolvedPath).toBe(absolutePath);
expect(resolved.isInsideWorkspace).toBe(false);
expect(resolved.workspaceRelPath).toBeNull();
});
it("expands home-relative paths", async () => {
const files = await loadFiles();
const resolved = files.resolveFilePath("~/rowboat-test.txt");
expect(resolved.resolvedPath).toBe(path.join(os.homedir(), "rowboat-test.txt"));
expect(resolved.isInsideWorkspace).toBe(false);
});
it("canonicalizes symlinked paths for permission checks", async () => {
const files = await loadFiles();
const externalDir = path.join(tmpDir, "external");
const linkPath = path.join(workspaceDir, "linked");
await fs.mkdir(externalDir, { recursive: true });
await fs.mkdir(workspaceDir, { recursive: true });
try {
await fs.symlink(externalDir, linkPath, "dir");
} catch (error) {
if (error && typeof error === "object" && "code" in error && error.code === "EPERM") {
return;
}
throw error;
}
const canonicalExternalDir = await fs.realpath(externalDir);
const resolved = await files.resolveFilePathForPermission("linked/new-file.txt");
expect(resolved.resolvedPath).toBe(path.join(workspaceDir, "linked", "new-file.txt"));
expect(resolved.canonicalPath).toBe(path.join(canonicalExternalDir, "new-file.txt"));
expect(resolved.isInsideWorkspace).toBe(false);
expect(resolved.workspaceRelPath).toBeNull();
});
it("reads text with line numbers and pagination metadata", async () => {
const files = await loadFiles();
await fs.mkdir(workspaceDir, { recursive: true });
await fs.writeFile(path.join(workspaceDir, "readme.txt"), "alpha\nbeta\ngamma\n", "utf8");
const result = await files.readText("readme.txt", 2, 1);
expect(result.path).toBe("readme.txt");
expect(result.encoding).toBe("utf8");
expect(result.offset).toBe(2);
expect(result.limit).toBe(1);
expect(result.totalLines).toBe(3);
expect(result.hasMore).toBe(true);
expect(result.content).toContain("2: beta");
});
it("rejects files containing NUL bytes as binary", async () => {
const files = await loadFiles();
await fs.mkdir(workspaceDir, { recursive: true });
await fs.writeFile(path.join(workspaceDir, "binary.dat"), Buffer.from([0x61, 0x00, 0x62]));
await expect(files.readText("binary.dat")).rejects.toThrow("binary file");
});
it("rejects files with a high non-printable byte ratio", async () => {
const files = await loadFiles();
await fs.mkdir(workspaceDir, { recursive: true });
await fs.writeFile(path.join(workspaceDir, "control.dat"), Buffer.from([0x01, 0x02, 0x03, 0x41]));
await expect(files.readText("control.dat")).rejects.toThrow("binary file");
});
it("rejects files that decode with many UTF-8 replacement characters", async () => {
const files = await loadFiles();
await fs.mkdir(workspaceDir, { recursive: true });
await fs.writeFile(path.join(workspaceDir, "invalid-utf8.txt"), Buffer.alloc(512, 0xff));
await expect(files.readText("invalid-utf8.txt")).rejects.toThrow("binary file");
});
it("accepts normal text control characters", async () => {
const files = await loadFiles();
await fs.mkdir(workspaceDir, { recursive: true });
await fs.writeFile(path.join(workspaceDir, "tabs.txt"), "one\ttwo\nthree\rfour\r\n", "utf8");
const result = await files.readText("tabs.txt");
expect(result.content).toContain("1: one\ttwo");
expect(result.content).toContain("2: three");
});
it("writes text and creates parent directories", async () => {
const files = await loadFiles();
await files.writeText("nested/dir/file.txt", "hello");
await expect(fs.readFile(path.join(workspaceDir, "nested", "dir", "file.txt"), "utf8")).resolves.toBe("hello");
});
it("rejects stale expectedEtag writes", async () => {
const files = await loadFiles();
await fs.mkdir(workspaceDir, { recursive: true });
await fs.writeFile(path.join(workspaceDir, "etag.txt"), "first", "utf8");
const initial = await files.stat("etag.txt");
await files.writeText("etag.txt", "second");
await expect(files.writeText("etag.txt", "third", { expectedEtag: initial.etag })).rejects.toThrow("ETag mismatch");
});
it("requires unique editText matches unless replaceAll is true", async () => {
const files = await loadFiles();
await fs.mkdir(workspaceDir, { recursive: true });
await fs.writeFile(path.join(workspaceDir, "edit.txt"), "one two one", "utf8");
const ambiguous = await files.editText("edit.txt", "one", "ONE");
expect(ambiguous).toEqual({ error: "oldString found 2 times. Use replaceAll: true or provide more context to make it unique." });
const replaced = await files.editText("edit.txt", "one", "ONE", true);
expect(replaced).toMatchObject({ success: true, replacements: 2 });
await expect(fs.readFile(path.join(workspaceDir, "edit.txt"), "utf8")).resolves.toBe("ONE two ONE");
});
it("runs glob relative to the requested cwd", async () => {
const files = await loadFiles();
await fs.mkdir(path.join(workspaceDir, "src"), { recursive: true });
await fs.writeFile(path.join(workspaceDir, "src", "a.ts"), "export {};", "utf8");
await fs.writeFile(path.join(workspaceDir, "src", "b.md"), "# b", "utf8");
await fs.writeFile(path.join(workspaceDir, "c.ts"), "export {};", "utf8");
const result = await files.glob("*.ts", "src");
expect(result.files).toEqual(["a.ts"]);
expect(result.resolvedFiles).toEqual([path.join(workspaceDir, "src", "a.ts")]);
expect(result.resolvedCwd).toBe(path.join(workspaceDir, "src"));
});
it("greps text files and skips binary files", async () => {
const files = await loadFiles();
await fs.mkdir(workspaceDir, { recursive: true });
await fs.writeFile(path.join(workspaceDir, "match.txt"), "needle\n", "utf8");
await fs.writeFile(path.join(workspaceDir, "binary.dat"), Buffer.from([0x6e, 0x65, 0x65, 0x64, 0x6c, 0x65, 0x00]));
const result = await files.grep({ pattern: "needle", searchPath: "." });
expect(result.count).toBe(1);
expect(result.matches).toEqual([
expect.objectContaining({
file: "match.txt",
line: 1,
content: "needle",
}),
]);
});
});

View file

@ -0,0 +1,641 @@
import fs from 'node:fs/promises';
import { createReadStream, type Stats } from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { createInterface } from 'node:readline';
import { glob as globFiles } from 'glob';
import { WorkDir } from '../config/config.js';
import { withFileLock } from '../knowledge/file-lock.js';
import { commitAll } from '../knowledge/version_history.js';
import { rewriteWikiLinksForRenamedKnowledgeFile } from '../workspace/wiki-link-rewrite.js';
export type FileOperation = 'read' | 'list' | 'search' | 'write' | 'delete';
export type ResolvedFilePath = {
originalPath: string;
resolvedPath: string;
isInsideWorkspace: boolean;
workspaceRelPath: string | null;
};
export type CanonicalFilePath = ResolvedFilePath & {
canonicalPath: string;
};
export type FileStat = {
kind: 'file' | 'dir';
size: number;
mtimeMs: number;
ctimeMs: number;
isSymlink?: boolean;
};
export type DirEntry = {
name: string;
path: string;
resolvedPath: string;
kind: 'file' | 'dir';
stat?: {
size: number;
mtimeMs: number;
};
};
export type ReaddirOptions = {
recursive?: boolean;
includeStats?: boolean;
includeHidden?: boolean;
allowedExtensions?: string[];
};
export type WriteTextOptions = {
atomic?: boolean;
mkdirp?: boolean;
expectedEtag?: string;
};
export type RemoveOptions = {
recursive?: boolean;
trash?: boolean;
};
const DEFAULT_READ_LIMIT = 2000;
const MAX_LINE_LENGTH = 2000;
const MAX_BYTES = 50 * 1024;
const MAX_LINE_SUFFIX = `... (line truncated to ${MAX_LINE_LENGTH} chars)`;
const MAX_BYTES_LABEL = `${MAX_BYTES / 1024} KB`;
let knowledgeCommitTimer: ReturnType<typeof setTimeout> | null = null;
let canonicalWorkspaceRoot: string | null = null;
function isPathInside(parent: string, child: string): boolean {
const relative = path.relative(parent, child);
return relative === '' || (!!relative && !relative.startsWith('..') && !path.isAbsolute(relative));
}
function expandHomePath(inputPath: string): string {
const trimmed = inputPath.trim();
if (!trimmed) {
throw new Error('Path is required');
}
if (trimmed === '~') {
return os.homedir();
}
if (trimmed.startsWith(`~${path.sep}`) || trimmed.startsWith('~/')) {
return path.join(os.homedir(), trimmed.slice(2));
}
return trimmed;
}
export function resolveFilePath(inputPath: string): ResolvedFilePath {
const originalPath = inputPath;
const expandedPath = expandHomePath(inputPath);
const resolvedPath = path.resolve(path.isAbsolute(expandedPath) ? expandedPath : path.join(WorkDir, expandedPath));
const workspaceRoot = path.resolve(WorkDir);
const isInsideWorkspace = isPathInside(workspaceRoot, resolvedPath);
const workspaceRelPath = isInsideWorkspace
? path.relative(workspaceRoot, resolvedPath).split(path.sep).join('/')
: null;
return { originalPath, resolvedPath, isInsideWorkspace, workspaceRelPath };
}
async function getCanonicalWorkspaceRoot(): Promise<string> {
if (canonicalWorkspaceRoot) return canonicalWorkspaceRoot;
try {
canonicalWorkspaceRoot = await fs.realpath(WorkDir);
} catch {
canonicalWorkspaceRoot = path.resolve(WorkDir);
}
return canonicalWorkspaceRoot;
}
async function canonicalizePathForPermission(resolvedPath: string): Promise<string> {
try {
return await fs.realpath(resolvedPath);
} catch {
const parsed = path.parse(resolvedPath);
const missingParts: string[] = [];
let current = resolvedPath;
while (current !== parsed.root) {
try {
const canonicalParent = await fs.realpath(current);
return path.join(canonicalParent, ...missingParts.reverse());
} catch {
missingParts.push(path.basename(current));
current = path.dirname(current);
}
}
return path.resolve(resolvedPath);
}
}
export async function resolveFilePathForPermission(inputPath: string): Promise<CanonicalFilePath> {
const resolved = resolveFilePath(inputPath);
const [canonicalPath, workspaceRoot] = await Promise.all([
canonicalizePathForPermission(resolved.resolvedPath),
getCanonicalWorkspaceRoot(),
]);
const isInsideWorkspace = isPathInside(workspaceRoot, canonicalPath);
const workspaceRelPath = isInsideWorkspace
? path.relative(workspaceRoot, canonicalPath).split(path.sep).join('/')
: null;
return {
...resolved,
canonicalPath,
isInsideWorkspace,
workspaceRelPath,
};
}
export function computeEtag(size: number, mtimeMs: number): string {
return `${size}:${mtimeMs}`;
}
function statToSchema(stats: Stats): FileStat {
return {
kind: stats.isDirectory() ? 'dir' : 'file',
size: stats.size,
mtimeMs: stats.mtimeMs,
ctimeMs: stats.ctimeMs,
isSymlink: stats.isSymbolicLink() ? true : undefined,
};
}
function scheduleKnowledgeCommit(filename: string): void {
if (knowledgeCommitTimer) {
clearTimeout(knowledgeCommitTimer);
}
knowledgeCommitTimer = setTimeout(() => {
knowledgeCommitTimer = null;
commitAll(`Edit ${filename}`, 'You').catch(err => {
console.error('[VersionHistory] Failed to commit after edit:', err);
});
}, 3 * 60 * 1000);
}
function isKnowledgeMarkdownPath(resolved: ResolvedFilePath): boolean {
return !!resolved.workspaceRelPath
&& resolved.workspaceRelPath.startsWith('knowledge/')
&& resolved.workspaceRelPath.endsWith('.md');
}
function scheduleKnowledgeCommitIfNeeded(resolved: ResolvedFilePath): void {
if (isKnowledgeMarkdownPath(resolved)) {
scheduleKnowledgeCommit(path.basename(resolved.resolvedPath));
}
}
async function assertTextFile(resolvedPath: string): Promise<void> {
const stats = await fs.lstat(resolvedPath);
if (!stats.isFile()) {
throw new Error('Path is not a file');
}
if (stats.size === 0) return;
const handle = await fs.open(resolvedPath, 'r');
try {
const buffer = Buffer.alloc(Math.min(stats.size, 8192));
const { bytesRead } = await handle.read(buffer, 0, buffer.length, 0);
const sample = buffer.subarray(0, bytesRead);
let nonPrintableCount = 0;
for (let index = 0; index < sample.length; index++) {
const byte = sample[index];
if (byte === 0) {
throw new Error('Refusing to read binary file as text');
}
if (byte < 9 || (byte > 13 && byte < 32)) {
nonPrintableCount++;
}
}
if (sample.length > 0 && nonPrintableCount / sample.length > 0.3) {
throw new Error('Refusing to read binary file as text');
}
const decoded = sample.toString('utf8');
const replacementChars = (decoded.match(/\uFFFD/g) || []).length;
if (replacementChars > Math.max(3, decoded.length * 0.01)) {
throw new Error('Refusing to read binary file as text');
}
} finally {
await handle.close();
}
}
export async function exists(inputPath: string): Promise<{ exists: boolean; path: string; resolvedPath: string; isInsideWorkspace: boolean }> {
const resolved = resolveFilePath(inputPath);
try {
await fs.access(resolved.resolvedPath);
return { exists: true, path: resolved.originalPath, resolvedPath: resolved.resolvedPath, isInsideWorkspace: resolved.isInsideWorkspace };
} catch {
return { exists: false, path: resolved.originalPath, resolvedPath: resolved.resolvedPath, isInsideWorkspace: resolved.isInsideWorkspace };
}
}
export async function stat(inputPath: string): Promise<FileStat & { path: string; resolvedPath: string; isInsideWorkspace: boolean; etag: string }> {
const resolved = resolveFilePath(inputPath);
const stats = await fs.lstat(resolved.resolvedPath);
return {
...statToSchema(stats),
path: resolved.originalPath,
resolvedPath: resolved.resolvedPath,
isInsideWorkspace: resolved.isInsideWorkspace,
etag: computeEtag(stats.size, stats.mtimeMs),
};
}
export async function list(inputPath: string, opts?: ReaddirOptions): Promise<Array<DirEntry>> {
const root = resolveFilePath(inputPath || '.');
const entries: Array<DirEntry> = [];
async function readDir(currentPath: string, currentDisplayPath: string): Promise<void> {
const items = await fs.readdir(currentPath, { withFileTypes: true });
for (const item of items) {
if (!opts?.includeHidden && item.name.startsWith('.')) {
continue;
}
const itemPath = path.join(currentPath, item.name);
const displayPath = path.posix.join(currentDisplayPath.split(path.sep).join('/'), item.name);
const itemKind = item.isDirectory() ? 'dir' : item.isFile() ? 'file' : null;
if (!itemKind) continue;
if (itemKind === 'file' && opts?.allowedExtensions?.length) {
const ext = path.extname(item.name);
if (!opts.allowedExtensions.includes(ext)) continue;
}
let itemStat: { size: number; mtimeMs: number } | undefined;
if (opts?.includeStats) {
const stats = await fs.lstat(itemPath);
itemStat = { size: stats.size, mtimeMs: stats.mtimeMs };
}
entries.push({
name: item.name,
path: displayPath,
resolvedPath: itemPath,
kind: itemKind,
stat: itemStat,
});
if (itemKind === 'dir' && opts?.recursive) {
await readDir(itemPath, displayPath);
}
}
}
await readDir(root.resolvedPath, root.originalPath || '.');
entries.sort((a, b) => {
if (a.kind !== b.kind) return a.kind === 'dir' ? -1 : 1;
return a.name.localeCompare(b.name);
});
return entries;
}
export async function readText(inputPath: string, offset?: number, limit?: number) {
const resolved = resolveFilePath(inputPath);
await assertTextFile(resolved.resolvedPath);
const stats = await fs.lstat(resolved.resolvedPath);
const stat = statToSchema(stats);
const etag = computeEtag(stats.size, stats.mtimeMs);
const effectiveOffset = offset ?? 1;
const effectiveLimit = limit ?? DEFAULT_READ_LIMIT;
const start = effectiveOffset - 1;
const stream = createReadStream(resolved.resolvedPath, { encoding: 'utf8' });
const rl = createInterface({ input: stream, crlfDelay: Infinity });
const collected: string[] = [];
let totalLines = 0;
let bytes = 0;
let truncatedByBytes = false;
let hasMoreLines = false;
try {
for await (const text of rl) {
totalLines += 1;
if (totalLines <= start) continue;
if (collected.length >= effectiveLimit) {
hasMoreLines = true;
continue;
}
const line = text.length > MAX_LINE_LENGTH
? text.substring(0, MAX_LINE_LENGTH) + MAX_LINE_SUFFIX
: text;
const size = Buffer.byteLength(line, 'utf-8') + (collected.length > 0 ? 1 : 0);
if (bytes + size > MAX_BYTES) {
truncatedByBytes = true;
hasMoreLines = true;
break;
}
collected.push(line);
bytes += size;
}
} finally {
rl.close();
stream.destroy();
}
if (totalLines < effectiveOffset && !(totalLines === 0 && effectiveOffset === 1)) {
return { error: `Offset ${effectiveOffset} is out of range for this file (${totalLines} lines)` };
}
const prefixed = collected.map((line, index) => `${index + effectiveOffset}: ${line}`);
const lastReadLine = effectiveOffset + collected.length - 1;
const nextOffset = lastReadLine + 1;
let footer: string;
if (truncatedByBytes) {
footer = `(Output capped at ${MAX_BYTES_LABEL}. Showing lines ${effectiveOffset}-${lastReadLine}. Use offset=${nextOffset} to continue.)`;
} else if (hasMoreLines) {
footer = `(Showing lines ${effectiveOffset}-${lastReadLine} of ${totalLines}. Use offset=${nextOffset} to continue.)`;
} else {
footer = `(End of file - total ${totalLines} lines)`;
}
const content = [
`<path>${resolved.originalPath}</path>`,
`<resolvedPath>${resolved.resolvedPath}</resolvedPath>`,
`<type>file</type>`,
`<content>`,
prefixed.join('\n'),
'',
footer,
`</content>`,
].join('\n');
return {
path: resolved.originalPath,
resolvedPath: resolved.resolvedPath,
isInsideWorkspace: resolved.isInsideWorkspace,
encoding: 'utf8' as const,
content,
stat,
etag,
offset: effectiveOffset,
limit: effectiveLimit,
totalLines,
hasMore: hasMoreLines || truncatedByBytes,
};
}
export async function readBuffer(inputPath: string): Promise<{ buffer: Buffer; path: string; resolvedPath: string; isInsideWorkspace: boolean }> {
const resolved = resolveFilePath(inputPath);
const buffer = await fs.readFile(resolved.resolvedPath);
return {
buffer,
path: resolved.originalPath,
resolvedPath: resolved.resolvedPath,
isInsideWorkspace: resolved.isInsideWorkspace,
};
}
export async function writeText(inputPath: string, data: string, opts?: WriteTextOptions) {
const resolved = resolveFilePath(inputPath);
const atomic = opts?.atomic !== false;
const mkdirp = opts?.mkdirp !== false;
if (mkdirp) {
await fs.mkdir(path.dirname(resolved.resolvedPath), { recursive: true });
}
const result = await withFileLock(resolved.resolvedPath, async () => {
if (opts?.expectedEtag) {
const existingStats = await fs.lstat(resolved.resolvedPath);
const existingEtag = computeEtag(existingStats.size, existingStats.mtimeMs);
if (existingEtag !== opts.expectedEtag) {
throw new Error('File was modified (ETag mismatch)');
}
}
const buffer = Buffer.from(data, 'utf8');
if (atomic) {
const tempPath = `${resolved.resolvedPath}.tmp.${Date.now()}${Math.random().toString(36).slice(2)}`;
await fs.writeFile(tempPath, buffer);
await fs.rename(tempPath, resolved.resolvedPath);
} else {
await fs.writeFile(resolved.resolvedPath, buffer);
}
const stats = await fs.lstat(resolved.resolvedPath);
return { stat: statToSchema(stats), etag: computeEtag(stats.size, stats.mtimeMs) };
});
scheduleKnowledgeCommitIfNeeded(resolved);
return {
path: resolved.originalPath,
resolvedPath: resolved.resolvedPath,
isInsideWorkspace: resolved.isInsideWorkspace,
stat: result.stat,
etag: result.etag,
};
}
export async function editText(inputPath: string, oldString: string, newString: string, replaceAll = false) {
const resolved = resolveFilePath(inputPath);
await assertTextFile(resolved.resolvedPath);
const content = await fs.readFile(resolved.resolvedPath, 'utf8');
const occurrences = content.split(oldString).length - 1;
if (occurrences === 0) {
return { error: 'oldString not found in file' };
}
if (occurrences > 1 && !replaceAll) {
return { error: `oldString found ${occurrences} times. Use replaceAll: true or provide more context to make it unique.` };
}
const newContent = replaceAll
? content.replaceAll(oldString, newString)
: content.replace(oldString, newString);
await writeText(inputPath, newContent, { atomic: true, mkdirp: true });
return {
success: true,
path: resolved.originalPath,
resolvedPath: resolved.resolvedPath,
isInsideWorkspace: resolved.isInsideWorkspace,
replacements: replaceAll ? occurrences : 1,
};
}
export async function mkdir(inputPath: string, recursive = true): Promise<{ ok: true; path: string; resolvedPath: string; isInsideWorkspace: boolean }> {
const resolved = resolveFilePath(inputPath);
await fs.mkdir(resolved.resolvedPath, { recursive });
return { ok: true, path: resolved.originalPath, resolvedPath: resolved.resolvedPath, isInsideWorkspace: resolved.isInsideWorkspace };
}
export async function rename(from: string, to: string, overwrite = false): Promise<{ ok: true; from: string; to: string; resolvedFrom: string; resolvedTo: string }> {
const source = resolveFilePath(from);
const dest = resolveFilePath(to);
await fs.access(source.resolvedPath);
const fromStats = await fs.lstat(source.resolvedPath);
if (!overwrite) {
try {
await fs.access(dest.resolvedPath);
throw new Error('Destination already exists');
} catch (err: unknown) {
if (err && typeof err === 'object' && 'code' in err && err.code === 'ENOENT') {
// destination does not exist
} else {
throw err;
}
}
}
await fs.mkdir(path.dirname(dest.resolvedPath), { recursive: true });
await fs.rename(source.resolvedPath, dest.resolvedPath);
if (fromStats.isFile() && isKnowledgeMarkdownPath(source) && isKnowledgeMarkdownPath(dest) && source.workspaceRelPath && dest.workspaceRelPath) {
try {
await rewriteWikiLinksForRenamedKnowledgeFile(WorkDir, source.workspaceRelPath, dest.workspaceRelPath);
} catch (error) {
console.error('Failed to rewrite wiki backlinks after file rename:', error);
}
}
return { ok: true, from, to, resolvedFrom: source.resolvedPath, resolvedTo: dest.resolvedPath };
}
export async function copy(from: string, to: string, overwrite = false): Promise<{ ok: true; from: string; to: string; resolvedFrom: string; resolvedTo: string }> {
const source = resolveFilePath(from);
const dest = resolveFilePath(to);
const fromStats = await fs.lstat(source.resolvedPath);
if (fromStats.isDirectory()) {
throw new Error('Copying directories is not supported');
}
if (!overwrite) {
try {
await fs.access(dest.resolvedPath);
throw new Error('Destination already exists');
} catch (err: unknown) {
if (err && typeof err === 'object' && 'code' in err && err.code === 'ENOENT') {
// destination does not exist
} else {
throw err;
}
}
}
await fs.mkdir(path.dirname(dest.resolvedPath), { recursive: true });
await fs.copyFile(source.resolvedPath, dest.resolvedPath);
return { ok: true, from, to, resolvedFrom: source.resolvedPath, resolvedTo: dest.resolvedPath };
}
export async function remove(inputPath: string, opts?: RemoveOptions): Promise<{ ok: true; path: string; resolvedPath: string; trashed?: string }> {
const resolved = resolveFilePath(inputPath);
const stats = await fs.lstat(resolved.resolvedPath);
const trash = opts?.trash !== false;
if (trash) {
const trashDir = path.join(WorkDir, '.trash');
await fs.mkdir(trashDir, { recursive: true });
const timestamp = Date.now();
const basename = path.basename(resolved.resolvedPath);
let finalTrashPath = path.join(trashDir, `${timestamp}-${basename}`);
let counter = 1;
while (true) {
try {
await fs.access(finalTrashPath);
finalTrashPath = path.join(trashDir, `${timestamp}-${counter}-${basename}`);
counter++;
} catch {
break;
}
}
await fs.rename(resolved.resolvedPath, finalTrashPath);
return { ok: true, path: resolved.originalPath, resolvedPath: resolved.resolvedPath, trashed: finalTrashPath };
}
if (stats.isDirectory()) {
if (!opts?.recursive) {
throw new Error('Cannot remove directory without recursive=true');
}
await fs.rm(resolved.resolvedPath, { recursive: true });
} else {
await fs.unlink(resolved.resolvedPath);
}
return { ok: true, path: resolved.originalPath, resolvedPath: resolved.resolvedPath };
}
export async function glob(pattern: string, cwd?: string): Promise<{ files: string[]; resolvedFiles: string[]; count: number; pattern: string; cwd: string; resolvedCwd: string }> {
const root = resolveFilePath(cwd || '.');
const files = await globFiles(pattern, {
cwd: root.resolvedPath,
nodir: true,
ignore: ['node_modules/**', '.git/**'],
});
const resolvedFiles = files.map(file => path.resolve(root.resolvedPath, file));
return {
files,
resolvedFiles,
count: files.length,
pattern,
cwd: cwd || '.',
resolvedCwd: root.resolvedPath,
};
}
export async function grep({
pattern,
searchPath,
fileGlob,
contextLines = 0,
maxResults = 100,
}: {
pattern: string;
searchPath?: string;
fileGlob?: string;
contextLines?: number;
maxResults?: number;
}) {
const root = resolveFilePath(searchPath || '.');
const stats = await fs.lstat(root.resolvedPath);
const candidates = stats.isDirectory()
? await globFiles(fileGlob || '**/*', {
cwd: root.resolvedPath,
nodir: true,
ignore: ['node_modules/**', '.git/**'],
dot: false,
})
: [path.basename(root.resolvedPath)];
const baseDir = stats.isDirectory() ? root.resolvedPath : path.dirname(root.resolvedPath);
const regex = new RegExp(pattern, 'i');
const matches: Array<{ file: string; resolvedPath: string; line: number; content: string; before?: string[]; after?: string[] }> = [];
for (const candidate of candidates) {
if (matches.length >= maxResults) break;
const resolvedPath = stats.isDirectory() ? path.resolve(baseDir, candidate) : root.resolvedPath;
try {
await assertTextFile(resolvedPath);
const text = await fs.readFile(resolvedPath, 'utf8');
const lines = text.split(/\r?\n/);
for (let index = 0; index < lines.length; index++) {
if (!regex.test(lines[index])) continue;
const before = contextLines > 0 ? lines.slice(Math.max(0, index - contextLines), index) : undefined;
const after = contextLines > 0 ? lines.slice(index + 1, Math.min(lines.length, index + 1 + contextLines)) : undefined;
matches.push({
file: stats.isDirectory() ? candidate : root.originalPath,
resolvedPath,
line: index + 1,
content: lines[index].trim(),
before,
after,
});
if (matches.length >= maxResults) break;
}
} catch {
// Skip unreadable and binary files.
}
}
return {
matches,
count: matches.length,
tool: 'internal-grep',
searchPath: searchPath || '.',
resolvedSearchPath: root.resolvedPath,
};
}

View file

@ -1,21 +1,21 @@
export function getRaw(): string {
return `---
tools:
workspace-writeFile:
file-writeText:
type: builtin
name: workspace-writeFile
workspace-readFile:
name: file-writeText
file-readText:
type: builtin
name: workspace-readFile
workspace-edit:
name: file-readText
file-editText:
type: builtin
name: workspace-edit
workspace-readdir:
name: file-editText
file-list:
type: builtin
name: workspace-readdir
workspace-mkdir:
name: file-list
file-mkdir:
type: builtin
name: workspace-mkdir
name: file-mkdir
---
# Agent Notes

View file

@ -264,10 +264,11 @@ async function createNotesFromBatch(
message += `**Instructions:**\n`;
message += `- Use the KNOWLEDGE BASE INDEX below to resolve entities - DO NOT grep/search for existing notes\n`;
message += `- Extract entities (people, organizations, projects, topics) from ALL files below\n`;
message += `- The source files below are INDEPENDENT — they are batched only for efficiency. Two entities are related ONLY if they co-occur within the same single source file (or in an existing note). NEVER link entities just because they appear in this batch (see "Source Scoping" in your instructions)\n`;
message += `- Create or update notes in "knowledge" directory (workspace-relative paths like "knowledge/People/Name.md")\n`;
message += `- You may also create or update "${SUGGESTED_TOPICS_REL_PATH}" to maintain curated suggested-topic cards\n`;
message += `- If the same entity appears in multiple files, merge the information into a single note\n`;
message += `- Use workspace tools to read existing notes or "${SUGGESTED_TOPICS_REL_PATH}" (when you need full content) and write updates\n`;
message += `- If the SAME entity appears in multiple files, merge the information into a single note (this is identity, not a relationship — do not link different entities across files)\n`;
message += `- Use file tools to read existing notes or "${SUGGESTED_TOPICS_REL_PATH}" (when you need full content) and write updates\n`;
message += `- Follow the note templates and guidelines in your instructions\n\n`;
// Add the knowledge base index
@ -297,16 +298,16 @@ async function createNotesFromBatch(
if (event.type !== "tool-invocation") {
return;
}
if (event.toolName !== "workspace-writeFile" && event.toolName !== "workspace-edit") {
if (event.toolName !== "file-writeText" && event.toolName !== "file-editText") {
return;
}
const toolPath = extractPathFromToolInput(event.input);
if (!toolPath) {
return;
}
if (event.toolName === "workspace-writeFile") {
if (event.toolName === "file-writeText") {
notesCreated.add(toolPath);
} else if (event.toolName === "workspace-edit") {
} else if (event.toolName === "file-editText") {
notesModified.add(toolPath);
}
});
@ -357,7 +358,7 @@ async function buildGraphWithFiles(
return { processedFiles: [], notesCreated: new Set(), notesModified: new Set(), hadError: false };
}
const BATCH_SIZE = 10; // Reduced from 25 to 10 files per agent run for faster processing
const BATCH_SIZE = 1; // One source file per agent run — prevents cross-file entity contamination in the graph
const totalBatches = Math.ceil(contentFiles.length / BATCH_SIZE);
console.log(`Processing ${contentFiles.length} files in ${totalBatches} batches (${BATCH_SIZE} files per batch)...`);
@ -543,7 +544,7 @@ async function processVoiceMemosForKnowledge(): Promise<boolean> {
}
// Process in batches like other sources
const BATCH_SIZE = 10;
const BATCH_SIZE = 1; // One source file per agent run — prevents cross-file entity contamination in the graph
const totalBatches = Math.ceil(contentFiles.length / BATCH_SIZE);
const notesCreated = new Set<string>();

View file

@ -109,7 +109,7 @@ export interface Classification {
const ClassificationSchema = z.object({
importance: z.enum(['important', 'other']).describe('important = real correspondence, action-required, or content worth referencing later. other = newsletters, marketing, automated notifications, transactional receipts, cold outreach.'),
summary: z.string().optional().describe('One or two sentences capturing what the thread is about and any implied action. Required when importance is important. Omit when other.'),
draftResponse: z.string().optional().describe('A complete draft reply the user can send as-is or edit. Plain text. Required when importance is important AND the thread implies a response is wanted. Omit when other, or when no response is appropriate (e.g. an FYI from a colleague that does not need a reply).'),
draftResponse: z.string().optional().describe('A complete draft reply the user can send as-is or edit. Plain text with real line breaks (\\n): greeting on its own line, a blank line between paragraphs, and the sign-off on its own line(s) — e.g. "Hi Tyrone,\\n\\nThanks for the follow-up.\\n\\nBest,\\nJohn". Required when importance is important AND the thread implies a response is wanted. Omit when other, or when no response is appropriate (e.g. an FYI from a colleague that does not need a reply).'),
});
const SYSTEM_PROMPT = `You classify a Gmail thread for a personal inbox view and, when appropriate, draft a reply on behalf of the user.
@ -128,7 +128,18 @@ When the thread is important, write a 1-2 sentence summary that captures the gis
When the thread is important AND a reply is reasonably expected from the user, write a complete draft reply they could send as-is.
Apply the user's email-style guide (when provided below) match their tone, sign-off, length, and phrasing patterns. If no style guide is provided, default to a brief, warm, professional voice.
Format it like a real email, not one run-on block. Use actual line breaks: put the greeting on its own line, separate distinct paragraphs with a blank line, and put the sign-off and the name on their own lines. The example below illustrates only the line-break structure not the wording, tone, greeting, or sign-off to use:
Hi Tyrone,
Thanks for the follow-up sorry I missed your earlier note.
Could you resend it with a bit more context so I can get back to you properly?
Best,
John
When an email-style guide is provided below, it takes precedence: follow it for greeting, tone, sign-off, length, and phrasing patterns (while keeping the line-break structure shown above). If no style guide is provided, default to a brief, warm, professional voice.
For scheduling-related threads (where the sender proposes meeting times, asks for the user's availability, or follows up on a meeting request), look at the user's upcoming calendar (provided below) and either:
- Propose 2-3 specific time windows from genuinely free slots, or
@ -144,10 +155,12 @@ Omit the draft when:
Be decisive pick exactly one importance label. Do not hedge.`;
function userReplied(snapshot: GmailThreadSnapshot, userEmail: string | null): boolean {
function userSentLatest(snapshot: GmailThreadSnapshot, userEmail: string | null): boolean {
if (!userEmail) return false;
const latest = snapshot.messages[snapshot.messages.length - 1];
if (!latest) return false;
const needle = userEmail.toLowerCase();
return snapshot.messages.some(m => (m.from || '').toLowerCase().includes(needle));
return (latest.from || '').toLowerCase().includes(needle);
}
function buildPrompt(
@ -206,7 +219,7 @@ export async function classifyThread(
userEmail: string | null,
options: ClassifyOptions = {},
): Promise<Classification> {
if (userReplied(snapshot, userEmail)) {
if (userSentLatest(snapshot, userEmail)) {
return { importance: 'important' };
}

View file

@ -0,0 +1,143 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type { OAuthTokens } from '../auth/types.js';
/**
* Regression for the cold-start race that left a stuck `error` field in
* oauth.json: Gmail + Calendar both call getClient() in the same tick, the
* dedup singleton's check-and-assign were separated by an `await`, two
* parallel refreshes go out, backend 429s the second one, the upsert(error)
* write from the 429 path could land last and stick "Needs reconnect" in
* the UI even though tokens were valid.
*/
interface MockOAuthRepo {
read: ReturnType<typeof vi.fn>;
upsert: ReturnType<typeof vi.fn>;
delete: ReturnType<typeof vi.fn>;
getClientFacingConfig: ReturnType<typeof vi.fn>;
}
let refreshSpy: ReturnType<typeof vi.fn>;
let mockOAuthRepo: MockOAuthRepo;
let storedTokens: OAuthTokens;
beforeEach(() => {
vi.resetModules();
// Expired 1 minute ago — forces the refresh path through getClient.
storedTokens = {
access_token: 'old-access',
refresh_token: 'rt',
expires_at: Math.floor(Date.now() / 1000) - 60,
token_type: 'Bearer',
scopes: ['https://www.googleapis.com/auth/gmail.modify'],
};
mockOAuthRepo = {
read: vi.fn(async () => ({ tokens: storedTokens, mode: 'rowboat' as const })),
upsert: vi.fn(async () => undefined),
delete: vi.fn(async () => undefined),
getClientFacingConfig: vi.fn(async () => ({})),
};
vi.doMock('../di/container.js', () => ({
default: {
resolve: (key: string) => {
if (key === 'oauthRepo') return mockOAuthRepo;
throw new Error(`unexpected DI resolve in test: ${key}`);
},
},
}));
// Real-ish delay so two concurrent callers actually have something to
// overlap on — without it the spy might resolve synchronously and mask
// the very race we're testing for.
refreshSpy = vi.fn(async (_rt: string, scopes?: string[]) => {
await new Promise((r) => setTimeout(r, 25));
return {
access_token: 'new-access',
refresh_token: 'rt',
expires_at: Math.floor(Date.now() / 1000) + 3600,
token_type: 'Bearer' as const,
scopes,
};
});
vi.doMock('../auth/google-backend-oauth.js', async () => {
const actual = await vi.importActual<typeof import('../auth/google-backend-oauth.js')>(
'../auth/google-backend-oauth.js',
);
return {
...actual,
refreshTokensViaBackend: refreshSpy,
};
});
});
afterEach(() => {
vi.doUnmock('../di/container.js');
vi.doUnmock('../auth/google-backend-oauth.js');
vi.resetModules();
});
describe('GoogleClientFactory.getClient', () => {
it('coalesces concurrent callers into a single refresh', async () => {
const { GoogleClientFactory } = await import('./google-client-factory.js');
GoogleClientFactory.clearCache();
// Same tick — this is the exact pattern that sync_gmail.init() and
// sync_calendar.init() produce on cold start.
const [a, b] = await Promise.all([
GoogleClientFactory.getClient(),
GoogleClientFactory.getClient(),
]);
expect(refreshSpy).toHaveBeenCalledTimes(1);
expect(a).not.toBeNull();
expect(a).toBe(b);
// And the failure-path upsert (error: '429…') is never invoked, so
// oauth.json doesn't get a stuck error.
const errorUpserts = mockOAuthRepo.upsert.mock.calls.filter(
([, conn]) => (conn as { error?: string | null }).error,
);
expect(errorUpserts).toHaveLength(0);
});
it('returns cached client when tokens are not expired', async () => {
// Tokens valid for another hour — no refresh should fire.
storedTokens = {
access_token: 'fresh-access',
refresh_token: 'rt',
expires_at: Math.floor(Date.now() / 1000) + 3600,
token_type: 'Bearer',
scopes: ['https://www.googleapis.com/auth/gmail.modify'],
};
mockOAuthRepo.read = vi.fn(async () => ({ tokens: storedTokens, mode: 'rowboat' as const }));
const { GoogleClientFactory } = await import('./google-client-factory.js');
GoogleClientFactory.clearCache();
const a = await GoogleClientFactory.getClient();
const b = await GoogleClientFactory.getClient();
expect(refreshSpy).not.toHaveBeenCalled();
expect(a).toBe(b);
});
it('does not stick an error on transient (429) refresh failure', async () => {
const { TransientRefreshError } = await import('../auth/google-backend-oauth.js');
refreshSpy.mockRejectedValueOnce(new TransientRefreshError('refresh failed: 429 Refresh in progress', 429));
const { GoogleClientFactory } = await import('./google-client-factory.js');
GoogleClientFactory.clearCache();
const result = await GoogleClientFactory.getClient();
expect(result).toBeNull();
const errorUpserts = mockOAuthRepo.upsert.mock.calls.filter(
([, conn]) => (conn as { error?: string | null }).error,
);
expect(errorUpserts).toHaveLength(0);
});
});

View file

@ -8,6 +8,7 @@ import type { Configuration } from '../auth/oauth-client.js';
import { OAuthTokens } from '../auth/types.js';
import {
ReconnectRequiredError,
TransientRefreshError,
refreshTokensViaBackend,
} from '../auth/google-backend-oauth.js';
@ -52,11 +53,14 @@ export class GoogleClientFactory {
};
/**
* Promise singleton so a burst of getClient() calls during the brief
* expiry window all wait on a single refresh round-trip rather than
* fanning out parallel refreshes.
* Promise singleton so concurrent getClient() callers share a single
* pass through the read/refresh/build pipeline rather than fanning
* out parallel refreshes. The check-and-assign must be atomic (no
* `await` between them) so two callers in the same tick can't both
* pass the null check before either assigns that's why getClient()
* is a thin synchronous wrapper around getClientInner().
*/
private static refreshInFlight: Promise<OAuth2Client | null> | null = null;
private static inFlightClient: Promise<OAuth2Client | null> | null = null;
private static async resolveByokCredentials(): Promise<{ clientId: string; clientSecret?: string }> {
const oauthRepo = container.resolve<IOAuthRepo>('oauthRepo');
@ -69,13 +73,24 @@ export class GoogleClientFactory {
}
/**
* Get or create OAuth2Client, reusing cached instance when possible
* Get or create OAuth2Client, reusing the cached instance when possible.
*
* The check-and-assign of `inFlightClient` is synchronous so concurrent
* callers in the same tick coalesce onto a single pipeline run. The actual
* work lives in getClientInner(); this wrapper exists purely to guarantee
* the dedup invariant.
*/
static async getClient(): Promise<OAuth2Client | null> {
if (this.refreshInFlight) {
return this.refreshInFlight;
if (this.inFlightClient) {
return this.inFlightClient;
}
this.inFlightClient = this.getClientInner().finally(() => {
this.inFlightClient = null;
});
return this.inFlightClient;
}
private static async getClientInner(): Promise<OAuth2Client | null> {
const oauthRepo = container.resolve<IOAuthRepo>('oauthRepo');
const connection = await oauthRepo.read(this.PROVIDER_NAME);
const tokens = connection.tokens ?? null;
@ -110,16 +125,12 @@ export class GoogleClientFactory {
// expiry — keeps long-running calls from racing the boundary.
if (oauthClient.isTokenExpired(tokens)) {
if (!tokens.refresh_token) {
console.log('[OAuth] Token expired and no refresh token available for Google.');
console.log('[OAuth] Google token expired and no refresh token available.');
await oauthRepo.upsert(this.PROVIDER_NAME, { error: 'Missing refresh token. Please reconnect.' });
this.clearCache();
return null;
}
this.refreshInFlight = this.refreshAndBuild(tokens, mode).finally(() => {
this.refreshInFlight = null;
});
return this.refreshInFlight;
return this.refreshAndBuild(tokens, mode);
}
// Reuse client if tokens haven't changed
@ -135,7 +146,8 @@ export class GoogleClientFactory {
const oauthRepo = container.resolve<IOAuthRepo>('oauthRepo');
try {
console.log(`[OAuth] Token expired, refreshing via ${mode}...`);
const secsSinceExpiry = Math.floor(Date.now() / 1000) - tokens.expires_at;
console.log(`[OAuth] Google token expired ${secsSinceExpiry}s ago, refreshing via ${mode}...`);
const existingScopes = tokens.scopes;
let refreshedTokens: OAuthTokens;
@ -150,7 +162,8 @@ export class GoogleClientFactory {
}
await oauthRepo.upsert(this.PROVIDER_NAME, { tokens: refreshedTokens, error: null });
console.log('[OAuth] Token refreshed successfully');
const ttl = refreshedTokens.expires_at - Math.floor(Date.now() / 1000);
console.log(`[OAuth] Google token refreshed successfully (mode=${mode}, new expires_at=${refreshedTokens.expires_at}, ttl=${ttl}s)`);
return this.buildAndCacheClient(refreshedTokens, mode);
} catch (error) {
if (error instanceof ReconnectRequiredError) {
@ -159,9 +172,24 @@ export class GoogleClientFactory {
this.clearCache();
return null;
}
if (error instanceof TransientRefreshError) {
// Transient (rate limit, in-flight dedup, upstream 5xx): leave
// stored tokens + cache alone, log, and let the next sync tick
// retry. Writing an `error` here would stick "Needs reconnect"
// in the UI for a problem the user can't fix by reconnecting.
console.warn(`[OAuth] Transient Google refresh failure (status=${error.status}): ${error.message} — will retry on next tick`);
return null;
}
const message = error instanceof Error ? error.message : 'Failed to refresh token for Google';
await oauthRepo.upsert(this.PROVIDER_NAME, { error: message });
console.error('[OAuth] Failed to refresh token for Google:', error);
// Walk cause chain so we can see e.g. `Not signed into Rowboat`
// showing up under a generic `fetch failed` outer error.
let cause: unknown = error;
while (cause != null && typeof cause === 'object' && 'cause' in cause) {
cause = (cause as { cause?: unknown }).cause;
if (cause != null) console.error('[OAuth] Caused by:', cause);
}
this.clearCache();
return null;
}
@ -188,18 +216,41 @@ export class GoogleClientFactory {
* Check if credentials are available and have required scopes
*/
static async hasValidCredentials(requiredScopes: string | string[]): Promise<boolean> {
const status = await this.getCredentialStatus(requiredScopes);
return status.hasRequiredScopes;
}
static async getCredentialStatus(requiredScopes: string | string[]): Promise<{
connected: boolean;
hasRequiredScopes: boolean;
missingScopes: string[];
}> {
const oauthRepo = container.resolve<IOAuthRepo>('oauthRepo');
const { tokens } = await oauthRepo.read(this.PROVIDER_NAME);
if (!tokens) {
return false;
const scopesArray = Array.isArray(requiredScopes) ? requiredScopes : [requiredScopes];
return {
connected: false,
hasRequiredScopes: false,
missingScopes: scopesArray,
};
}
// Check if required scope(s) are present
const scopesArray = Array.isArray(requiredScopes) ? requiredScopes : [requiredScopes];
const granted = new Set(tokens.scopes ?? []);
const missingScopes = scopesArray.filter(scope => !granted.has(scope));
if (!tokens.scopes || tokens.scopes.length === 0) {
return false;
return {
connected: true,
hasRequiredScopes: false,
missingScopes,
};
}
return scopesArray.every(scope => tokens.scopes!.includes(scope));
return {
connected: true,
hasRequiredScopes: missingScopes.length === 0,
missingScopes,
};
}
/**

View file

@ -98,7 +98,7 @@ This brief refreshes every 15 minutes, so it should always reflect the **current
## Technical Instructions
**IMPORTANT:** All workspace tools (workspace-readdir, workspace-readFile, workspace-grep, etc.) take paths **relative to the workspace root**. Use paths like \`calendar_sync/\`, \`gmail_sync/\`, \`knowledge/\` — NOT absolute paths.
**IMPORTANT:** File tools accept relative paths that resolve against the Rowboat workspace root. For workspace data, use paths like \`calendar_sync/\`, \`gmail_sync/\`, \`knowledge/\` — NOT absolute paths.
**IMPORTANT:** Check the current date. If the date has changed since the content was last generated, clear everything and start fresh for the new day.
@ -136,8 +136,8 @@ This is the most time-sensitive section — it orients the user on what's coming
6. **IMPORTANT:** Do NOT say "nothing in the next X hours" if there IS an event within that window. Always compute the actual time difference between now and the next event's start time before writing this section.
### Calendar
1. Use \`workspace-readdir\` with path \`calendar_sync\` to list files
2. Use \`workspace-readFile\` to read each \`.json\` event file (e.g. \`calendar_sync/eventid123.json\`)
1. Use \`file-list\` with path \`calendar_sync\` to list files
2. Use \`file-readText\` to read each \`.json\` event file (e.g. \`calendar_sync/eventid123.json\`)
3. Filter for events happening **today** (compare the event's start dateTime or date to the current date)
4. **After morning:** Only include events that **haven't ended yet**. Don't show meetings that already happened the user was there. If it's afternoon and all meetings are done, show an empty calendar block.
5. **Always** output a \\\`\\\`\\\`calendar block — even if there are no events today. If no events, output an empty events array:
@ -160,8 +160,8 @@ If there are events, include them:
7. If there are no remaining events, don't add filler text the empty calendar block speaks for itself.
### Emails
1. Use \`workspace-readdir\` with path \`gmail_sync\` to list files (skip \`sync_state.json\` and \`attachments/\`)
2. Use \`workspace-readFile\` to read the email markdown files (e.g. \`gmail_sync/threadid123.md\`)
1. Use \`file-list\` with path \`gmail_sync\` to list files (skip \`sync_state.json\` and \`attachments/\`)
2. Use \`file-readText\` to read the email markdown files (e.g. \`gmail_sync/threadid123.md\`)
3. Check the frontmatter \`action\` field — emails with \`action: reply\` or \`action: respond\` need a response
4. Output ALL emails (both action items and FYI) in a single \\\`\\\`\\\`emails block as a JSON array. Emails needing a response get a \`draft_response\`. Write drafts in the user's voice — direct, informal, no fluff. Example:
@ -180,7 +180,7 @@ If there are events, include them:
This section is about things the user might not be aware of from yesterday. Think of it as: "Here's what happened while you were away."
- **Skip recurring/routine events entirely.** The user knows they have standup every day. Don't mention it unless something unusual happened during it.
- **Read yesterday's meeting notes** from \`knowledge/Meetings/\`. The directory structure is nested: \`knowledge/Meetings/<source>/<YYYY-MM-DD>/meeting-<timestamp>.md\` (e.g. \`knowledge/Meetings/rowboat/2026-03-30/meeting-2026-03-30T13-49-27.md\`). Use \`workspace-readdir\` with \`recursive: true\` on \`knowledge/Meetings\` to find all files, then filter for files in a folder matching yesterday's date. Read the matching files with \`workspace-readFile\`. Summarize key outcomes: decisions made, action items assigned, blockers raised, anything that changes priorities.
- **Read yesterday's meeting notes** from \`knowledge/Meetings/\`. The directory structure is nested: \`knowledge/Meetings/<source>/<YYYY-MM-DD>/meeting-<timestamp>.md\` (e.g. \`knowledge/Meetings/rowboat/2026-03-30/meeting-2026-03-30T13-49-27.md\`). Use \`file-list\` with \`recursive: true\` on \`knowledge/Meetings\` to find all files, then filter for files in a folder matching yesterday's date. Read the matching files with \`file-readText\`. Summarize key outcomes: decisions made, action items assigned, blockers raised, anything that changes priorities.
- Check yesterday's emails in \`gmail_sync/\` for anything that went unresolved.
- Surface things that matter: commitments made, deadlines mentioned, important updates.
- **If nothing notable happened, say "Quiet day yesterday — nothing to flag." and move on.** Don't manufacture content.
@ -192,7 +192,7 @@ This is NOT a generic task list. These are the things the user should actually f
- **Do NOT list calendar events as tasks.** They're already in the Calendar section.
- **Do NOT list trivial admin** (filing small invoices, archiving spam, etc.) the user can handle that in 30 seconds without being told to.
- **Pull action items from yesterday's meeting notes** in \`knowledge/Meetings/<source>/<YYYY-MM-DD>/\` — these are often the most important source of real tasks.
- Search through \`knowledge/\` using \`workspace-grep\` and \`workspace-readdir\` for checkbox items (\`- [ ]\`), explicit action items, deadlines, or follow-ups.
- Search through \`knowledge/\` using \`file-grep\` and \`file-list\` for checkbox items (\`- [ ]\`), explicit action items, deadlines, or follow-ups.
- **Rank by importance.** Lead with the most critical item. If something is time-sensitive, say when it needs to happen by.
- Add brief context for why each item matters if it's not obvious.
- **If there are no real tasks, say "No pressing tasks today — good day to make progress on bigger items." Don't invent busywork.**
@ -221,4 +221,4 @@ When you see a target region associated with your task (during a scheduled run),
- Use the existing content as context (e.g., to update rather than regenerate from scratch if appropriate)
- Do NOT include the target tags themselves in your response
`;
}
}

View file

@ -78,13 +78,13 @@ async function labelEmailBatch(
});
let message = `Label the following ${files.length} email files by prepending YAML frontmatter.\n\n`;
message += `**Important:** Use workspace-relative paths with workspace-edit (e.g. "gmail_sync/email.md", NOT absolute paths).\n\n`;
message += `**Important:** Use workspace-relative paths with file-editText (e.g. "gmail_sync/email.md", NOT absolute paths).\n\n`;
for (let i = 0; i < files.length; i++) {
const file = files[i];
const relativePath = path.relative(WorkDir, file.path);
const truncated = file.content.length > MAX_CONTENT_LENGTH
? file.content.slice(0, MAX_CONTENT_LENGTH) + '\n\n[... content truncated, use workspace-readFile for full content ...]'
? file.content.slice(0, MAX_CONTENT_LENGTH) + '\n\n[... content truncated, use file-readText for full content ...]'
: file.content;
message += `## File ${i + 1}: ${relativePath}\n\n`;
@ -98,7 +98,7 @@ async function labelEmailBatch(
if (event.type !== 'tool-invocation') {
return;
}
if (event.toolName !== 'workspace-edit') {
if (event.toolName !== 'file-editText') {
return;
}
try {

View file

@ -3,15 +3,15 @@ import { renderTagSystemForEmails } from './tag_system.js';
export function getRaw(): string {
return `---
tools:
workspace-readFile:
file-readText:
type: builtin
name: workspace-readFile
workspace-edit:
name: file-readText
file-editText:
type: builtin
name: workspace-edit
workspace-readdir:
name: file-editText
file-list:
type: builtin
name: workspace-readdir
name: file-list
---
# Task
@ -70,7 +70,7 @@ ${renderTagSystemForEmails()}
- **Action**: Does this need a response (\`action-required\`), is it time-sensitive (\`urgent\`), or are you waiting on them (\`waiting\`)? Use \`""\` if none apply. **Do NOT use \`fyi\` as an action value** — it is not a valid action tag.
3. **Apply noise tags aggressively.** Noise tags can and should coexist with relationship and topic tags. A salary confirmation from your finance team should have BOTH \`relationship: ['team']\` AND \`filter: ['receipt']\`. The noise tag determines whether a note is created — it overrides relationship and topic signals.
4. Be accurate only apply labels that clearly fit. But when an email IS noise, always add the noise tag even when other tags are present.
5. Use \`workspace-edit\` to prepend YAML frontmatter to the file. The oldString should be the first line of the file (the \`# Subject\` heading), and the newString should be the frontmatter followed by that same first line.
5. Use \`file-editText\` to prepend YAML frontmatter to the file. The oldString should be the first line of the file (the \`# Subject\` heading), and the newString should be the frontmatter followed by that same first line.
6. Always include \`processed: true\` and \`labeled_at\` with the current ISO timestamp.
7. If the email already has frontmatter (starts with \`---\`), skip it.

View file

@ -26,7 +26,7 @@ Every run message has this shape:
**Objective:**
<the user-authored objective usually 1-3 sentences describing what the note should keep being>
Start by calling \`workspace-readFile\` on \`<filePath>\` ... patch-style edits ...
Start by calling \`file-readText\` on \`<filePath>\` ... patch-style edits ...
For **manual** runs, an optional trailing block may appear:
@ -51,10 +51,10 @@ You own the **entire body below the H1** — you may freely add, edit, reorganiz
**Make incremental, patch-style edits not one-shot rewrites.**
The right pattern on every run:
1. \`workspace-readFile\` to fetch the current note.
1. \`file-readText\` to fetch the current note.
2. Decide on the *first* change you need to make (add a section, replace a stale figure, dedupe entries, fix an out-of-date paragraph).
3. \`workspace-edit\` to make that one change.
4. \`workspace-readFile\` again to confirm the result.
3. \`file-editText\` to make that one change.
4. \`file-readText\` again to confirm the result.
5. Decide the *next* change. Repeat.
Why patch-style:
@ -63,8 +63,8 @@ Why patch-style:
- It lets you abort partway if a tool call fails, leaving the note in a consistent partial state instead of a clobbered one.
Avoid:
- Calling \`workspace-writeFile\` to replace the entire body. That's the no-go path.
- Building up the entire new body in your head and emitting it in a single \`workspace-edit\` call with a giant \`oldString\` / \`newString\`. Smaller anchors, more steps.
- Calling \`file-writeText\` to replace the entire body. That's the no-go path.
- Building up the entire new body in your head and emitting it in a single \`file-editText\` call with a giant \`oldString\` / \`newString\`. Smaller anchors, more steps.
# Body Structure (defaults)
@ -105,9 +105,9 @@ When skipping, still end with a summary line (see "Final Summary" below) so the
You have the full workspace toolkit. Quick reference for common cases:
- **\`workspace-readFile\`, \`workspace-edit\`, \`workspace-writeFile\`** — read and modify the note's body. Frontmatter is hands-off. Prefer many small \`workspace-edit\` calls over one giant \`workspace-writeFile\`.
- **\`file-readText\`, \`file-editText\`, \`file-writeText\`** — read and modify the note's body. Frontmatter is hands-off. Prefer many small \`file-editText\` calls over one giant \`file-writeText\`.
- **\`web-search\`** — the public web (news, prices, status pages, documentation). Use when the objective needs information beyond the workspace.
- **\`workspace-grep\`, \`workspace-glob\`, \`workspace-readdir\`** — search the user's knowledge graph and synced data.
- **\`file-grep\`, \`file-glob\`, \`file-list\`** — search the user's knowledge graph and synced data.
- **\`parseFile\`, \`LLMParse\`** — parse PDFs, spreadsheets, Word docs if the objective references attached files.
- **\`composio-*\`, \`listMcpTools\`, \`executeMcpTool\`** — user-connected integrations (Gmail, Calendar, etc.). Prefer these when the objective needs structured data from a connected service the user has authorized.
- **\`browser-control\`** — only when a required source has no API / search alternative and requires JS rendering.
@ -124,9 +124,9 @@ The user's knowledge graph is plain markdown in \`${WorkDir}/knowledge/\`, organ
Synced external data often sits alongside under \`gmail_sync/\`, \`calendar_sync/\`, \`granola_sync/\`, \`fireflies_sync/\` — consult these when the objective references emails, meetings, or calendar events.
**CRITICAL:** Always include the folder prefix in paths. Never pass an empty path or the workspace root.
- \`workspace-grep({ pattern: "Acme", path: "knowledge/" })\`
- \`workspace-readFile("knowledge/People/Sarah Chen.md")\`
- \`workspace-readdir("gmail_sync/")\`
- \`file-grep({ pattern: "Acme", searchPath: "knowledge/" })\`
- \`file-readText("knowledge/People/Sarah Chen.md")\`
- \`file-list("gmail_sync/")\`
# Failure & Fallback

View file

@ -30,7 +30,7 @@ function truncate(s: string | null | undefined, n = SUMMARY_LOG_LIMIT): string {
// Agent run message
// ---------------------------------------------------------------------------
const LIVE_NOTE_EVENT_DECISION_DIRECTIVE = '**Decision:** Determine whether this event genuinely warrants updating the note. If the event is not meaningfully relevant on closer inspection, skip the update — do not call `workspace-edit`. Only edit the file if the event provides new or changed information that the objective implies should be reflected.';
const LIVE_NOTE_EVENT_DECISION_DIRECTIVE = '**Decision:** Determine whether this event genuinely warrants updating the note. If the event is not meaningfully relevant on closer inspection, skip the update — do not call `file-editText`. Only edit the file if the event provides new or changed information that the objective implies should be reflected.';
const LIVE_NOTE_MANUAL_PAREN = 'user-triggered — either the Run button in the Live Note panel or the `run-live-note-agent` tool';
@ -44,8 +44,8 @@ function buildMessage(
const localNow = now.toLocaleString('en-US', { dateStyle: 'full', timeStyle: 'long' });
const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
// Workspace-relative path the agent's tools (workspace-readFile,
// workspace-edit) expect. Internal storage is knowledge/-relative.
// Workspace-relative path the agent's tools (file-readText,
// file-editText) expect. Internal storage is knowledge/-relative.
const wsPath = `knowledge/${filePath}`;
const baseMessage = `Update the live note at \`${wsPath}\`.
@ -55,7 +55,7 @@ function buildMessage(
**Objective:**
${live.objective}
Start by calling \`workspace-readFile\` on \`${wsPath}\` to read the current note (frontmatter + body) — the body may be long and you should fetch it yourself rather than rely on a snapshot. Then make small, incremental edits with \`workspace-edit\` to bring the body in line with the objective: edit one region, re-read to verify, then edit the next region. Avoid one-shot rewrites of the whole body. Do not modify the YAML frontmatter at the top of the file — that block is owned by the user and the runtime.`;
Start by calling \`file-readText\` on \`${wsPath}\` to read the current note (frontmatter + body) — the body may be long and you should fetch it yourself rather than rely on a snapshot. Then make small, incremental edits with \`file-editText\` to bring the body in line with the objective: edit one region, re-read to verify, then edit the next region. Avoid one-shot rewrites of the whole body. Do not modify the YAML frontmatter at the top of the file — that block is owned by the user and the runtime.`;
return baseMessage + buildTriggerBlock({
trigger,

View file

@ -4,27 +4,27 @@ import { renderNoteEffectRules } from './tag_system.js';
export function getRaw(): string {
return `---
tools:
workspace-writeFile:
file-writeText:
type: builtin
name: workspace-writeFile
workspace-readFile:
name: file-writeText
file-readText:
type: builtin
name: workspace-readFile
workspace-edit:
name: file-readText
file-editText:
type: builtin
name: workspace-edit
workspace-readdir:
name: file-editText
file-list:
type: builtin
name: workspace-readdir
workspace-mkdir:
name: file-list
file-mkdir:
type: builtin
name: workspace-mkdir
workspace-grep:
name: file-mkdir
file-grep:
type: builtin
name: workspace-grep
workspace-glob:
name: file-grep
file-glob:
type: builtin
name: workspace-glob
name: file-glob
---
# Context
@ -37,7 +37,7 @@ Sources (emails, meetings, voice memos) are processed in roughly chronological o
# Task
You are a memory agent. Given a single source file (email, meeting transcript, or voice memo), you will:
You are a memory agent. You are given one or more source files (emails, meeting transcripts, or voice memos) to process. **The files in a request are independent of each other** they are batched together only for efficiency, not because they are related. Process each source file on its own terms (see "Source Scoping" below). For each source file you will:
1. **Determine source type (meeting or email)**
2. **Evaluate if the source is worth processing**
@ -49,7 +49,21 @@ You are a memory agent. Given a single source file (email, meeting transcript, o
8. Create new notes or update existing notes
9. **Apply state changes to existing notes**
The core rule: **Both meetings and emails can create notes, but emails require personalized content.**
The core rule: **Both meetings and emails can create notes, but emails require personalized content and a new People/Organization note from an email also requires the user to have replied at least once in the thread (the Email Reply Gate). Emails can always update existing notes regardless.**
# Source Scoping (Batch Isolation) READ FIRST
You may receive several source files in one request. **They are unrelated by default.** Two source files appearing in the same request tells you *nothing* about whether their entities are related.
**The only relationship signal is co-occurrence WITHIN a single source file (or a relationship already recorded in existing notes).** Concretely:
- **Create a link / relationship between two entities ONLY if the connection is evidenced within the same single source file, or is already documented in an existing note.** Example: if email A is between Sarah (Acme) and you, and email B is between David (Globex) and you, you must **not** link SarahDavid or AcmeGlobex they never appeared together.
- **Never infer a relationship from batch co-occurrence.** "Both showed up in this run" is not evidence. When the only thing two entities share is the batch, add no link.
- **The one allowed cross-file operation is identity merging:** if the *same* canonical entity appears in multiple source files in the batch, merge its information into a single note. That is recognizing one entity, not relating two.
- **Activity entries are per-source.** Each activity line describes one source file's interaction and links only the entities actually present in *that* source.
- **When in doubt, omit the link.** A missing edge is a minor gap; a fabricated edge is a wrong fact in the graph (the knowledge graph draws an edge for every \`[[link]]\` you write).
This applies to every step below entity resolution, content extraction, and especially the bidirectional links in Step 10.
You have full read access to the existing knowledge directory. Use this extensively to:
- Find existing notes for people, organizations, projects mentioned
@ -92,17 +106,17 @@ You have access to these tools:
**For reading files:**
\`\`\`
workspace-readFile({ path: "knowledge/People/Sarah Chen.md" })
file-readText({ path: "knowledge/People/Sarah Chen.md" })
\`\`\`
**For creating NEW files:**
\`\`\`
workspace-writeFile({ path: "knowledge/People/Sarah Chen.md", data: "# Sarah Chen\\n\\n..." })
file-writeText({ path: "knowledge/People/Sarah Chen.md", data: "# Sarah Chen\\n\\n..." })
\`\`\`
**For editing EXISTING files (preferred for updates):**
\`\`\`
workspace-edit({
file-editText({
path: "knowledge/People/Sarah Chen.md",
oldString: "## Activity\\n",
newString: "## Activity\\n- **2026-02-03** (meeting): New activity entry\\n"
@ -111,27 +125,27 @@ workspace-edit({
**For listing directories:**
\`\`\`
workspace-readdir({ path: "knowledge/People" })
file-list({ path: "knowledge/People" })
\`\`\`
**For creating directories:**
\`\`\`
workspace-mkdir({ path: "knowledge/Projects", recursive: true })
file-mkdir({ path: "knowledge/Projects", recursive: true })
\`\`\`
**For searching files:**
\`\`\`
workspace-grep({ pattern: "Acme Corp", searchPath: "knowledge", fileGlob: "*.md" })
file-grep({ pattern: "Acme Corp", searchPath: "knowledge", fileGlob: "*.md" })
\`\`\`
**For finding files by pattern:**
\`\`\`
workspace-glob({ pattern: "**/*.md", cwd: "knowledge/People" })
file-glob({ pattern: "**/*.md", cwd: "knowledge/People" })
\`\`\`
**IMPORTANT:**
- Use \`workspace-edit\` for updating existing notes (adding activity, updating fields)
- Use \`workspace-writeFile\` only for creating new notes
- Use \`file-editText\` for updating existing notes (adding activity, updating fields)
- Use \`file-writeText\` only for creating new notes
- Prefer the knowledge_index for entity resolution (it's faster than grep)
# Output
@ -158,7 +172,7 @@ ${renderNoteEffectRules()}
Read the source file and determine if it's a meeting or email.
\`\`\`
workspace-readFile({ path: "{source_file}" })
file-readText({ path: "{source_file}" })
\`\`\`
**Meeting indicators:**
@ -194,6 +208,7 @@ Emails containing calendar invites (\`.ics\` attachments or inline calendar data
- Contains calendar metadata (VCALENDAR, VEVENT)
**Rules for calendar invite emails:**
0. **Exempt from the Email Reply Gate** - a meeting actually scheduled with the user is direct engagement, so you may create the primary-contact note even if the user hasn't sent a text reply in the thread.
1. **CREATE a note for the primary contact** - the person you're actually meeting with
2. **Extract from the invite:** their name, email, organization (from email domain), meeting topic
3. **Skip automated notifications from Google/Outlook** - emails from calendar-no-reply@google.com with no human sender
@ -262,7 +277,7 @@ If processing, continue to Step 2.
# Step 2: Read and Parse Source File
\`\`\`
workspace-readFile({ path: "{source_file}" })
file-readText({ path: "{source_file}" })
\`\`\`
Extract metadata:
@ -359,7 +374,7 @@ From index, find matches for:
Only read the full note content when you need details not in the index (e.g., activity logs, open items):
\`\`\`bash
workspace-readFile({ path: "{knowledge_folder}/People/Sarah Chen.md" })
file-readText({ path: "{knowledge_folder}/People/Sarah Chen.md" })
\`\`\`
**Why read these notes:**
@ -436,7 +451,7 @@ Resolution Map:
**If source_type == "email":**
- The email already passed label-based filtering in Step 1
- Resolved entities Update existing notes
- New entities Create notes (the labels already confirmed this email is worth processing)
- New entities Create notes **only if the email-reply gate passes** (see Step 5 "Email Reply Gate"). If the thread is purely inbound (the user never replied), update existing notes only do not create new canonical People/Organization notes.
## 4c: Disambiguation Rules
@ -445,10 +460,10 @@ When multiple candidates match a variant, disambiguate:
**By organization (strongest signal):**
\`\`\`
# "David" could be David Kim or David Chen
workspace-grep({ pattern: "Acme", searchPath: "{knowledge_folder}/People/David Kim.md" })
file-grep({ pattern: "Acme", searchPath: "{knowledge_folder}/People/David Kim.md" })
# Output: **Organization:** [[Acme Corp]]
workspace-grep({ pattern: "Acme", searchPath: "{knowledge_folder}/People/David Chen.md" })
file-grep({ pattern: "Acme", searchPath: "{knowledge_folder}/People/David Chen.md" })
# Output: **Organization:** [[Other Corp]]
# Source is from Acme context "David" = "David Kim"
@ -456,14 +471,14 @@ workspace-grep({ pattern: "Acme", searchPath: "{knowledge_folder}/People/David C
**By email (definitive):**
\`\`\`
workspace-grep({ pattern: "david@acme.com", searchPath: "{knowledge_folder}/People/David Kim.md" })
file-grep({ pattern: "david@acme.com", searchPath: "{knowledge_folder}/People/David Kim.md" })
# Exact email match is definitive
\`\`\`
**By role:**
\`\`\`
# Source mentions "their CTO"
workspace-grep({ pattern: "Role.*CTO", searchPath: "{knowledge_folder}/People" })
file-grep({ pattern: "Role.*CTO", searchPath: "{knowledge_folder}/People" })
# Filter results by organization context
\`\`\`
@ -508,7 +523,7 @@ For entities not resolved to existing notes, determine if they warrant new notes
**CREATE a note for people who are:**
- External (not @user.domain)
- People you directly interacted with in meetings
- Email correspondents directly participating in the thread (emails that reach this step already passed label-based filtering)
- Email correspondents directly participating in a thread the user has replied to (emails that reach this step already passed label-based filtering; new People/Org notes also require the Email Reply Gate)
- Decision makers or contacts at customers, prospects, or partners
- Investors or potential investors
- Candidates you are interviewing
@ -579,6 +594,21 @@ For people who don't warrant their own note, add to Organization note's Contacts
- Sarah Lee Support, handled wire transfer issue
\`\`\`
### Email Reply Gate (new People/Organization notes only)
**Emails can always update existing notes. But an email may only CREATE a new canonical People or Organization note if the user has replied at least once in the thread.** This stops purely inbound email (cold outreach, newsletters, one-way notifications) from spawning new notes for people the user has never engaged.
**How to check:** The email source lists each message as a \`### From: <sender>\` block. The user has replied if **at least one message in the thread was sent by the user** — a \`### From:\` line whose address matches \`user.email\`. A reply from someone at \`@user.domain\` (the user's own team) also counts as the user's side having engaged.
**Rules:**
- **User replied at least once** the thread is a two-way exchange; you may create new canonical People/Organization notes (still subject to the Direct Interaction and Weekly Importance tests below).
- **Purely inbound** (every message is from external senders; no \`### From:\` matches \`user.email\` or \`@user.domain\`) → do **NOT** create new canonical People/Organization notes. You may still: update notes that already exist, and create/update a suggestion card in \`suggested-topics.md\` if the entity looks strategically relevant.
**Scope:**
- Applies **only to creating new** People/Organization notes from **emails**. It does not block updates to existing notes.
- Does **not** apply to meetings or voice memos (those always create).
- **Exception:** calendar-invite emails for a meeting actually scheduled with the user (see "Calendar Invite Emails") are exempt a scheduled meeting is itself direct engagement, so create the primary-contact note even without a text reply.
### Direct Interaction Test (People and Organizations)
For **new canonical People and Organizations notes**, require **direct interaction**, not just mention.
@ -597,9 +627,13 @@ For **new canonical People and Organizations notes**, require **direct interacti
- The source only establishes a second-degree relationship, not a direct one
**Canonical note rule:**
- For **new People/Organizations**, create the canonical note only if both are true:
1. There is **direct interaction**
2. The entity clears the **weekly importance test**
- For **new People/Organizations**, create the canonical note only if all are true:
1. For **email** sources, the **Email Reply Gate** passes (the user replied in the thread, or it's an exempt calendar invite)
2. There is **direct interaction**
3. The interaction is **not transactional** per the Transactional Interaction Check (see below) reporting an issue, sending/paying an invoice, support questions, scheduling, etc. update existing notes only, never create new ones
4. The entity clears the **weekly importance test**
5. The interaction is **not purely temporary** per the ongoing-relationship soft check (see below)
- **Updates to existing notes are never gated by these checks** a transactional or temporary interaction with a person/org that already has a note still gets logged as activity.
If an entity seems strategically relevant but fails the direct interaction test, do **not** auto-create a canonical note. At most, create a suggestion card in \`suggested-topics.md\`.
@ -638,6 +672,42 @@ This test is mainly for **People** and **Organizations**. **Do NOT use it as the
- Update the existing note even if the current source is weaker; the importance test is mainly for deciding whether to create a **new** People/Organization note
- If a previously tentative person/org is now clearly important enough for a canonical note, create/update the note and remove any tentative suggestion card for that exact entity from \`suggested-topics.md\`
### Transactional Interaction Check (People and Organizations)
**If the source is a transactional interaction a discrete task or exchange that completes and closes do NOT create a new canonical note. You may still UPDATE an existing note** (add an activity entry, mark an open item complete, update a field). The transaction is real activity worth logging when the person/org already matters, but on its own it is not evidence of a durable relationship worth minting a new note.
**Transactional interactions include:**
- Reporting, acknowledging, or resolving an **issue / bug / outage / support ticket**
- Sending, requesting, or paying an **invoice, receipt, or payment confirmation**
- A **how-to or product question** that resolves within the thread
- **Scheduling / logistics / calendar** back-and-forth
- A one-time **purchase, refund, password reset, form submission, or signature request**
- Automated, templated, or notification-style messages
The signal is the **nature of the exchange, not the sender's importance**: even someone at an important company, if they are only handling a transactional task here, does not earn a *new* note from that interaction alone. If the same person/org later shows non-transactional substance (an active deal, evaluation, partnership, ongoing thread), create the note then.
### Ongoing-Relationship Test (soft check, People and Organizations)
A softer companion to the transactional and weekly-importance checks, aimed at filtering out **temporary, one-off interactions** even when the single touchpoint looks substantive.
**Ask:** _"Will the user still be in touch with this person/organization a month from now, or is this a temporary interaction that wraps up once this thread/issue is resolved?"_
If the honest answer is "this is temporary and won't carry forward," **don't create a canonical note** even if there was a real two-way exchange. The interaction can still be logged on an existing org note (e.g. in Contacts) without minting a new People note.
**Temporary / one-off (lean NO don't create):**
- **Customer-support questions** a support rep, or a customer asking a one-time support/how-to question, with no ongoing strategic relationship. Don't create a note for that person.
- A scheduling/logistics back-and-forth that ends when the meeting is booked
- A one-time transactional exchange (a single vendor purchase, a password reset, a refund, a form submission)
- A recruiter or service rep handling a single request
- Anyone where the interaction is clearly self-contained and resolves within this thread
**Durable (lean YES note is OK if the other gates pass):**
- An active customer, prospect, investor, partner, or candidate relationship likely to continue
- A contact in an ongoing deal, project, or evaluation
- Someone with whom a recurring cadence (calls, syncs, threads) is likely
This is a **soft** check: weigh it alongside the weekly-importance and direct-interaction tests rather than as a hard veto. When the relationship is genuinely durable, a single temporary-looking exchange shouldn't block the note. When in doubt and the interaction looks temporary, prefer a suggestion card (or just logging the activity on an existing note) over creating a new canonical note.
## Organizations
**CREATE a note if:**
@ -651,6 +721,8 @@ This test is mainly for **People** and **Organizations**. **Do NOT use it as the
- One-time transactional vendors
- Consumer service companies
- Organizations only referenced through third-party mention or offered introductions
- Transactional interactions (see Transactional Interaction Check) invoices, support tickets, issue reports, scheduling. Update an existing org note if one exists; don't create a new one
- Temporary, self-contained interactions that won't carry forward a month from now (see Ongoing-Relationship Test) e.g. a one-off support exchange
## Projects
@ -959,7 +1031,7 @@ Before writing, compare extracted content against existing notes.
## Check Activity Log
\`\`\`
workspace-grep({ pattern: "2025-01-15", searchPath: "{knowledge_folder}/People/Sarah Chen.md" })
file-grep({ pattern: "2025-01-15", searchPath: "{knowledge_folder}/People/Sarah Chen.md" })
\`\`\`
If an entry for this date/source already exists, this may have been processed. Skip or verify different interaction.
@ -993,20 +1065,20 @@ If new info contradicts existing:
- Wait for the tool to return before generating the next note.
- Do NOT batch multiple write commands in a single response.
**For NEW entities (use workspace-writeFile):**
**For NEW entities (use file-writeText):**
\`\`\`
workspace-writeFile({
file-writeText({
path: "{knowledge_folder}/People/Jennifer.md",
data: "# Jennifer\\n\\n## Summary\\n..."
})
\`\`\`
**For EXISTING entities (use workspace-edit):**
- Read current content first with workspace-readFile
- Use workspace-edit to add activity entry at TOP (reverse chronological)
**For EXISTING entities (use file-editText):**
- Read current content first with file-readText
- Use file-editText to add activity entry at TOP (reverse chronological)
- Update fields using targeted edits
\`\`\`
workspace-edit({
file-editText({
path: "{knowledge_folder}/People/Sarah Chen.md",
oldString: "## Activity\\n",
newString: "## Activity\\n- **2026-02-03** (meeting): Met to discuss project timeline\\n"
@ -1016,8 +1088,8 @@ workspace-edit({
**For \`suggested-topics.md\`:**
- Use workspace-relative path \`suggested-topics.md\`
- Read the current file if you need the latest content
- Use \`workspace-writeFile\` to create or rewrite the file when that is simpler and cleaner
- Use \`workspace-edit\` for small targeted edits only if that keeps the file deduped and readable
- Use \`file-writeText\` to create or rewrite the file when that is simpler and cleaner
- Use \`file-editText\` for small targeted edits only if that keeps the file deduped and readable
## 9b: Apply State Changes
@ -1056,6 +1128,8 @@ After writing, verify links go both ways.
## Bidirectional Link Rules
**Precondition (see "Source Scoping"):** only add a link when the relationship is evidenced **within a single source file** or already recorded in an existing note. Do **not** add links between entities that merely share this batch. Bidirectionality applies *after* a link is justified it never justifies creating one.
| If you add... | Then also add... |
|---------------|------------------|
| Person Organization | Organization Person (in People section) |
@ -1064,6 +1138,8 @@ After writing, verify links go both ways.
| Project Topic | Topic Project (in Related section) |
| Person Person | Person Person (reverse link) |
**Before writing any \`[[link]]\`, ask:** "Did these two entities actually appear together in *this* source file (or an existing note)?" If the only thing they share is the batch, do not link them.
---
${renderNoteTypesBlock()}
@ -1076,9 +1152,12 @@ ${renderNoteTypesBlock()}
|-------------|---------------|----------------|------------------------|
| Meeting | Yes | Yes | Yes |
| Voice memo | Yes | Yes | Yes |
| Email (has create label) | Yes | Yes | Yes |
| Email (create label + user replied in thread) | Yes | Yes | Yes |
| Email (create label, purely inbound no user reply) | Update-only (no new People/Org notes) | Yes | Yes |
| Email (only skip labels) | No (SKIP) | No | No |
**Email Reply Gate:** New canonical People/Organization notes from an email require the user to have replied at least once in the thread (a \`### From:\` matching \`user.email\` or \`@user.domain\`). Purely inbound threads update existing notes only. Calendar invites for a scheduled meeting are exempt.
**Meeting activity format:** Always include a link to the source meeting note:
\`\`\`
**2025-01-15** (meeting): Discussed project timeline with [[People/Sarah Chen]]. See [[Meetings/granola/abc123_Weekly Sync]]
@ -1125,8 +1204,11 @@ Before completing, verify:
**Filtering:**
- [ ] Excluded self (user.name, user.email, @user.domain)
- [ ] Applied relevance test to each person
- [ ] Applied the email reply gate to new People/Organizations from email sources (purely inbound threads create no new notes)
- [ ] Applied the direct interaction test to new People/Organizations
- [ ] Applied the transactional interaction check (issue reports, invoices, support, scheduling update existing notes only never create new ones)
- [ ] Applied the weekly importance test to new People/Organizations
- [ ] Applied the ongoing-relationship soft check (temporary/one-off interactions create no new notes)
- [ ] Transactional contacts in Org Contacts, not People notes
- [ ] Source correctly classified (process vs skip)
- [ ] Third-party mentions did not become new canonical People/Organizations notes
@ -1147,6 +1229,7 @@ Before completing, verify:
- [ ] Logged all state changes in activity
**Structure:**
- [ ] Every \`[[link]]\` reflects a real relationship from a single source file or existing note — none created from batch co-occurrence (Source Scoping)
- [ ] All entity mentions use \`[[Folder/Name]]\` absolute links
- [ ] Activity entries are reverse chronological
- [ ] No duplicate activity entries

View file

@ -3,15 +3,15 @@ import { renderTagSystemForNotes } from './tag_system.js';
export function getRaw(): string {
return `---
tools:
workspace-readFile:
file-readText:
type: builtin
name: workspace-readFile
workspace-edit:
name: file-readText
file-editText:
type: builtin
name: workspace-edit
workspace-readdir:
name: file-editText
file-list:
type: builtin
name: workspace-readdir
name: file-list
---
# Task
@ -23,7 +23,7 @@ You are a note tagging agent. Given a batch of knowledge notes (People, Organiza
2. Determine the note type from its folder path (People/, Organizations/, Projects/, Topics/, Meetings/).
3. Classify the note using the Rowboat Tag System (Note Tags section) appended below.
4. Extract attributes from the note's \`## Info\` section (or \`## About\` for Topics). For Meetings, extract metadata from the note content and file path (see Meeting extraction rules below).
5. Use \`workspace-edit\` to prepend YAML frontmatter to the file. The oldString should be the first line of the file (the \`# Title\` heading), and the newString should be the frontmatter followed by that same first line.
5. Use \`file-editText\` to prepend YAML frontmatter to the file. The oldString should be the first line of the file (the \`# Title\` heading), and the newString should be the frontmatter followed by that same first line.
6. If the note already has frontmatter (starts with \`---\`), skip it.
# Frontmatter Format

View file

@ -30,6 +30,10 @@ function formatEventTime(event: AnyEvent): string {
return `${startStr}${endStr}`;
}
function shouldSyncCalendarEvent(event: cal.Schema$Event): boolean {
return event.eventType !== 'workingLocation';
}
function formatEventBlock(event: AnyEvent, label: 'NEW' | 'UPDATED'): string {
const id = getStr(event, 'id') ?? '(unknown id)';
const title = getStr(event, 'summary') ?? '(no title)';
@ -347,6 +351,9 @@ async function syncCalendarWindow(auth: OAuth2Client, syncDir: string, lookbackD
console.log(`Found ${events.length} events.`);
for (const event of events) {
if (event.id) {
if (!shouldSyncCalendarEvent(event)) {
continue;
}
const result = await saveEvent(event, syncDir);
const attachmentsSaved = await processAttachments(drive, event, syncDir);
currentEventIds.add(event.id);

View file

@ -26,13 +26,21 @@ const CACHE_DIR = path.join(WorkDir, 'inbox_lists');
}
})();
const SYNC_INTERVAL_MS = 30 * 1000; // Check every 30 seconds
const REQUIRED_SCOPE = 'https://www.googleapis.com/auth/gmail.readonly';
const REQUIRED_SCOPE = 'https://www.googleapis.com/auth/gmail.modify';
const MAX_THREADS_IN_DIGEST = 10;
const RECENT_BACKFILL_INTERVAL_MS = 15 * 60 * 1000;
const nhm = new NodeHtmlMarkdown();
// Bump whenever snapshot-building logic changes in a way that should invalidate
// previously cached snapshots (e.g. attachment / recipient parsing fixes). The
// short-circuit in buildAndCacheSnapshot only reuses a cache whose version matches,
// so stale entries are transparently rebuilt on the next sync.
const SNAPSHOT_PARSER_VERSION = 2;
interface SnapshotCacheEntry {
historyId: string;
fetchedAt: string;
parserVersion?: number;
snapshot: GmailThreadSnapshot;
}
@ -55,6 +63,7 @@ function writeCachedSnapshot(threadId: string, historyId: string, snapshot: Gmai
const entry: SnapshotCacheEntry = {
historyId,
fetchedAt: new Date().toISOString(),
parserVersion: SNAPSHOT_PARSER_VERSION,
snapshot,
};
fs.writeFileSync(cachePath(threadId), JSON.stringify(entry), 'utf-8');
@ -77,6 +86,76 @@ export function saveMessageBodyHeight(threadId: string, messageId: string, heigh
}
}
function deleteCachedSnapshot(threadId: string): void {
try {
fs.rmSync(cachePath(threadId), { force: true });
} catch (err) {
console.warn(`[Gmail cache] delete failed for ${threadId}:`, err);
}
}
async function getGmailClientOrThrow() {
const auth = await GoogleClientFactory.getClient();
if (!auth) throw new Error('Gmail is not connected.');
return google.gmail({ version: 'v1', auth });
}
export interface ThreadActionResult {
ok: boolean;
error?: string;
}
export async function archiveThread(threadId: string): Promise<ThreadActionResult> {
try {
const gmailClient = await getGmailClientOrThrow();
await gmailClient.users.threads.modify({
userId: 'me',
id: threadId,
requestBody: { removeLabelIds: ['INBOX'] },
});
deleteCachedSnapshot(threadId);
return { ok: true };
} catch (err) {
return { ok: false, error: err instanceof Error ? err.message : String(err) };
}
}
export async function trashThread(threadId: string): Promise<ThreadActionResult> {
try {
const gmailClient = await getGmailClientOrThrow();
await gmailClient.users.threads.trash({ userId: 'me', id: threadId });
deleteCachedSnapshot(threadId);
return { ok: true };
} catch (err) {
return { ok: false, error: err instanceof Error ? err.message : String(err) };
}
}
export async function markThreadRead(threadId: string): Promise<ThreadActionResult> {
try {
const gmailClient = await getGmailClientOrThrow();
await gmailClient.users.threads.modify({
userId: 'me',
id: threadId,
requestBody: { removeLabelIds: ['UNREAD'] },
});
// Update local cache: clear unread on all messages in the thread.
const cached = readCachedSnapshot(threadId);
if (cached) {
for (const m of cached.snapshot.messages) m.unread = false;
cached.snapshot.unread = false;
try {
fs.writeFileSync(cachePath(threadId), JSON.stringify(cached), 'utf-8');
} catch (err) {
console.warn(`[Gmail cache] markRead write failed for ${threadId}:`, err);
}
}
return { ok: true };
} catch (err) {
return { ok: false, error: err instanceof Error ? err.message : String(err) };
}
}
interface SyncedThread {
threadId: string;
markdown: string;
@ -113,6 +192,7 @@ export interface GmailThreadSnapshot {
sizeBytes?: number;
savedPath: string;
}>;
messageIdHeader?: string;
}>;
}
@ -236,19 +316,24 @@ interface ExtractedAttachment {
* saveAttachment / processThread, so the renderer can hand them to
* shell.openPath via the existing IPC.
*/
function extractAttachments(msgId: string, payload: gmail.Schema$MessagePart): ExtractedAttachment[] {
function extractAttachments(msgId: string, payload: gmail.Schema$MessagePart, html?: string): ExtractedAttachment[] {
const out: ExtractedAttachment[] = [];
const walk = (part: gmail.Schema$MessagePart): void => {
const filename = part.filename;
const attId = part.body?.attachmentId;
if (filename && attId) {
// Exclude only true inline images (image/* with a Content-ID, which
// get baked into bodyHtml as data URLs by inlineCidImages). Other
// parts with Content-ID — PDFs, .log files, .ics, etc. — are real
// attachments; Gmail just stamps Content-ID on most parts.
const cid = part.headers?.find(h => h.name?.toLowerCase() === 'content-id')?.value;
// Exclude only images that are genuinely inline — i.e. their Content-ID
// is actually referenced via `cid:` in the HTML body, so inlineCidImages
// already baked them in as data URLs. Gmail stamps a Content-ID on most
// parts (including real, separately-attached images like screenshots or
// scanned docs), so a Content-ID alone must NOT exclude an attachment;
// otherwise attached images silently disappear from the thread view.
const cidRaw = part.headers?.find(h => h.name?.toLowerCase() === 'content-id')?.value;
const cid = cidRaw?.replace(/^<|>$/g, '').trim();
const mime = part.mimeType || '';
const isInlineImage = !!cid && mime.startsWith('image/');
const referencedInHtml = !!cid && !!html
&& new RegExp(`cid:${cid.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}`, 'i').test(html);
const isInlineImage = mime.startsWith('image/') && referencedInHtml;
if (!isInlineImage) {
const safeName = `${msgId}_${cleanFilename(filename)}`;
out.push({
@ -505,6 +590,7 @@ async function buildAndCacheSnapshot(
threadData.historyId &&
cached &&
cached.historyId === threadData.historyId &&
cached.parserVersion === SNAPSHOT_PARSER_VERSION &&
cached.snapshot.importance
) {
return cached.snapshot;
@ -530,7 +616,7 @@ async function buildAndCacheSnapshot(
}
}
const isDraft = msg.labelIds?.includes('DRAFT') ?? false;
const attachments = msg.payload && msg.id ? extractAttachments(msg.id, msg.payload) : [];
const attachments = msg.payload && msg.id ? extractAttachments(msg.id, msg.payload, parts.html) : [];
return {
id: msg.id || undefined,
from: headerValue(headers, 'From') || 'Unknown',
@ -713,7 +799,9 @@ async function processThread(auth: OAuth2Client, threadId: string, syncDir: stri
} catch (error) {
console.error(`Error processing thread ${threadId}:`, error);
return null;
const status = getErrorStatus(error);
if (status === 404) return null;
throw error;
}
}
@ -757,20 +845,102 @@ async function pruneInboxCache(auth: OAuth2Client): Promise<void> {
}
}
function loadState(stateFile: string): { historyId?: string; last_sync?: string } {
function loadState(stateFile: string): { historyId?: string; last_sync?: string; last_recent_backfill?: string } {
if (fs.existsSync(stateFile)) {
return JSON.parse(fs.readFileSync(stateFile, 'utf-8'));
}
return {};
}
function saveState(historyId: string, stateFile: string) {
function saveState(historyId: string, stateFile: string, extra: { last_recent_backfill?: string } = {}) {
const previous = loadState(stateFile);
fs.writeFileSync(stateFile, JSON.stringify({
historyId,
last_sync: new Date().toISOString()
last_sync: new Date().toISOString(),
last_recent_backfill: extra.last_recent_backfill ?? previous.last_recent_backfill,
...extra,
}, null, 2));
}
function getErrorStatus(error: unknown): number | undefined {
const status = (error as { response?: { status?: number } }).response?.status;
if (status) return status;
const code = Number((error as { code?: number | string }).code);
return Number.isFinite(code) ? code : undefined;
}
function recentDateQuery(lookbackDays: number): string {
const pastDate = new Date();
pastDate.setDate(pastDate.getDate() - lookbackDays);
return pastDate.toISOString().split('T')[0].replace(/-/g, '/');
}
async function listRecentNonDeletedThreadIds(gmailClient: gmail.Gmail, lookbackDays: number): Promise<RecentThreadInfo[]> {
const dateQuery = recentDateQuery(lookbackDays);
const results: RecentThreadInfo[] = [];
const seen = new Set<string>();
let pageToken: string | undefined;
do {
const res = await gmailClient.users.threads.list({
userId: 'me',
q: `after:${dateQuery} -in:spam -in:trash`,
maxResults: 500,
pageToken,
});
for (const thread of res.data.threads || []) {
if (!thread.id || seen.has(thread.id)) continue;
seen.add(thread.id);
results.push({
threadId: thread.id,
historyId: thread.historyId || '',
snippet: thread.snippet || undefined,
});
}
pageToken = res.data.nextPageToken ?? undefined;
} while (pageToken);
return results;
}
function shouldRunRecentBackfill(stateFile: string): boolean {
const state = loadState(stateFile);
if (!state.last_recent_backfill) return true;
const lastRunMs = new Date(state.last_recent_backfill).getTime();
if (!Number.isFinite(lastRunMs)) return true;
return Date.now() - lastRunMs >= RECENT_BACKFILL_INTERVAL_MS;
}
async function backfillMissingRecentThreads(
auth: OAuth2Client,
syncDir: string,
attachmentsDir: string,
stateFile: string,
lookbackDays: number,
): Promise<SyncedThread[]> {
if (!shouldRunRecentBackfill(stateFile)) return [];
const gmailClient = google.gmail({ version: 'v1', auth });
const recentThreads = await listRecentNonDeletedThreadIds(gmailClient, lookbackDays);
const missingThreadIds = recentThreads
.map((thread) => thread.threadId)
.filter((threadId) => !fs.existsSync(path.join(syncDir, `${threadId}.md`)));
const synced: SyncedThread[] = [];
for (const threadId of missingThreadIds) {
const result = await processThread(auth, threadId, syncDir, attachmentsDir);
if (result) synced.push(result);
}
const profile = await gmailClient.users.getProfile({ userId: 'me' });
saveState(profile.data.historyId!, stateFile, { last_recent_backfill: new Date().toISOString() });
if (missingThreadIds.length > 0) {
console.log(`Recent Gmail backfill synced ${synced.length}/${missingThreadIds.length} missing thread(s).`);
}
return synced;
}
async function fullSync(auth: OAuth2Client, syncDir: string, attachmentsDir: string, stateFile: string, lookbackDays: number) {
const gmail = google.gmail({ version: 'v1', auth });
@ -814,6 +984,7 @@ async function fullSync(auth: OAuth2Client, syncDir: string, attachmentsDir: str
const res = await gmail.users.threads.list({
userId: 'me',
q: `after:${dateQuery} -in:spam -in:trash`,
maxResults: 500,
pageToken
});
@ -907,15 +1078,24 @@ async function partialSync(auth: OAuth2Client, startHistoryId: string, syncDir:
};
try {
const res = await gmail.users.history.list({
userId: 'me',
startHistoryId,
historyTypes: ['messageAdded']
});
const changes: gmail.Schema$History[] = [];
let pageToken: string | undefined;
do {
const res = await gmail.users.history.list({
userId: 'me',
startHistoryId,
historyTypes: ['messageAdded'],
maxResults: 500,
pageToken,
});
if (res.data.history) changes.push(...res.data.history);
pageToken = res.data.nextPageToken ?? undefined;
} while (pageToken);
const changes = res.data.history;
if (!changes || changes.length === 0) {
console.log("No new changes.");
const backfilled = await backfillMissingRecentThreads(auth, syncDir, attachmentsDir, stateFile, lookbackDays);
await publishGmailSyncEvent(backfilled);
const profile = await gmail.users.getProfile({ userId: 'me' });
saveState(profile.data.historyId!, stateFile);
return;
@ -937,6 +1117,8 @@ async function partialSync(auth: OAuth2Client, startHistoryId: string, syncDir:
}
if (threadIds.size === 0) {
const backfilled = await backfillMissingRecentThreads(auth, syncDir, attachmentsDir, stateFile, lookbackDays);
await publishGmailSyncEvent(backfilled);
const profile = await gmail.users.getProfile({ userId: 'me' });
saveState(profile.data.historyId!, stateFile);
return;
@ -961,6 +1143,8 @@ async function partialSync(auth: OAuth2Client, startHistoryId: string, syncDir:
const result = await processThread(auth, tid, syncDir, attachmentsDir);
if (result) synced.push(result);
}
const backfilled = await backfillMissingRecentThreads(auth, syncDir, attachmentsDir, stateFile, lookbackDays);
synced.push(...backfilled);
await publishGmailSyncEvent(synced);
@ -1059,6 +1243,162 @@ async function performSync() {
}
}
// --- Send Reply ---
export interface SendReplyOptions {
threadId?: string;
to: string;
cc?: string;
bcc?: string;
subject: string;
bodyHtml: string;
bodyText: string;
inReplyTo?: string;
references?: string;
}
export interface SendReplyResult {
messageId?: string;
error?: string;
}
export interface GmailConnectionStatus {
connected: boolean;
hasRequiredScope: boolean;
missingScopes: string[];
email: string | null;
}
/** The connected Gmail address (cached). Used by the composer to exclude "me" from reply-all. */
export async function getAccountEmail(): Promise<string | null> {
const auth = await GoogleClientFactory.getClient();
if (!auth) return null;
return getUserEmail(auth);
}
export async function getConnectionStatus(): Promise<GmailConnectionStatus> {
const status = await GoogleClientFactory.getCredentialStatus(REQUIRED_SCOPE);
let email: string | null = null;
if (status.connected) {
try {
email = await getAccountEmail();
} catch {
email = null;
}
}
return {
connected: status.connected,
hasRequiredScope: status.hasRequiredScopes,
missingScopes: status.missingScopes,
email,
};
}
function requireSafeHeaderValue(name: string, value: string): string {
if (/[\r\n]/.test(value)) {
throw new Error(`${name} cannot contain line breaks.`);
}
return value.trim();
}
function encodeRfc2047(text: string): string {
requireSafeHeaderValue('Subject', text);
// Only encode if non-ASCII chars present.
// eslint-disable-next-line no-control-regex
if (/^[\x00-\x7F]*$/.test(text)) return text;
return `=?UTF-8?B?${Buffer.from(text).toString('base64')}?=`;
}
function encodeMimeBase64(text: string): string {
return Buffer.from(text, 'utf8')
.toString('base64')
.match(/.{1,76}/g)
?.join('\r\n') ?? '';
}
export async function sendThreadReply(opts: SendReplyOptions): Promise<SendReplyResult> {
try {
const auth = await GoogleClientFactory.getClient();
if (!auth) return { error: 'Gmail is not connected.' };
const gmailClient = google.gmail({ version: 'v1', auth });
const userEmail = await getUserEmail(auth);
if (!userEmail) return { error: 'Could not determine your Gmail address.' };
const safeTo = requireSafeHeaderValue('To', opts.to);
const safeCc = opts.cc?.trim() ? requireSafeHeaderValue('Cc', opts.cc) : undefined;
const safeBcc = opts.bcc?.trim() ? requireSafeHeaderValue('Bcc', opts.bcc) : undefined;
const safeInReplyTo = opts.inReplyTo ? requireSafeHeaderValue('In-Reply-To', opts.inReplyTo) : undefined;
const safeReferences = opts.references ? requireSafeHeaderValue('References', opts.references) : undefined;
const boundary = `b_${Date.now()}_${Math.random().toString(36).slice(2, 10)}`;
const headers: string[] = [];
headers.push(`From: ${requireSafeHeaderValue('From', userEmail)}`);
headers.push(`To: ${safeTo}`);
if (safeCc) headers.push(`Cc: ${safeCc}`);
if (safeBcc) headers.push(`Bcc: ${safeBcc}`);
headers.push(`Subject: ${encodeRfc2047(opts.subject)}`);
if (safeInReplyTo) headers.push(`In-Reply-To: ${safeInReplyTo}`);
if (safeReferences) headers.push(`References: ${safeReferences}`);
headers.push('MIME-Version: 1.0');
headers.push(`Content-Type: multipart/alternative; boundary="${boundary}"`);
const parts: string[] = [];
parts.push(`--${boundary}`);
parts.push('Content-Type: text/plain; charset="UTF-8"');
parts.push('Content-Transfer-Encoding: base64');
parts.push('');
parts.push(encodeMimeBase64(opts.bodyText));
parts.push('');
parts.push(`--${boundary}`);
parts.push('Content-Type: text/html; charset="UTF-8"');
parts.push('Content-Transfer-Encoding: base64');
parts.push('');
parts.push(encodeMimeBase64(opts.bodyHtml));
parts.push('');
parts.push(`--${boundary}--`);
const message = `${headers.join('\r\n')}\r\n\r\n${parts.join('\r\n')}`;
const raw = Buffer.from(message, 'utf8')
.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/, '');
const requestBody: gmail.Schema$Message = { raw };
if (opts.threadId) requestBody.threadId = opts.threadId;
const res = await gmailClient.users.messages.send({
userId: 'me',
requestBody,
});
if (opts.threadId) {
// Clean up any Gmail-side drafts in this thread.
try {
const drafts = await gmailClient.users.drafts.list({ userId: 'me' });
const matching = (drafts.data.drafts || []).filter(
(d) => d.message?.threadId === opts.threadId && d.id
);
await Promise.all(
matching.map((d) =>
gmailClient.users.drafts.delete({ userId: 'me', id: d.id! })
)
);
} catch (cleanupErr) {
console.warn('[Gmail] Draft cleanup after send failed:', cleanupErr);
}
}
// Wake the sync loop so the cache picks up the new message.
triggerSync();
return { messageId: res.data.id || undefined };
} catch (err) {
return { error: err instanceof Error ? err.message : String(err) };
}
}
export async function init() {
console.log("Starting Gmail Sync (TS)...");
console.log(`Will sync every ${SYNC_INTERVAL_MS / 1000} seconds.`);

View file

@ -91,13 +91,13 @@ async function tagNoteBatch(
});
let message = `Tag the following ${files.length} knowledge notes by prepending YAML frontmatter with appropriate tags.\n\n`;
message += `**Important:** Use workspace-relative paths with workspace-edit (e.g. "knowledge/People/Sarah Chen.md", NOT absolute paths).\n\n`;
message += `**Important:** Use workspace-relative paths with file-editText (e.g. "knowledge/People/Sarah Chen.md", NOT absolute paths).\n\n`;
for (let i = 0; i < files.length; i++) {
const file = files[i];
const relativePath = path.relative(WorkDir, file.path);
const truncated = file.content.length > MAX_CONTENT_LENGTH
? file.content.slice(0, MAX_CONTENT_LENGTH) + '\n\n[... content truncated, use workspace-readFile for full content ...]'
? file.content.slice(0, MAX_CONTENT_LENGTH) + '\n\n[... content truncated, use file-readText for full content ...]'
: file.content;
message += `## File ${i + 1}: ${relativePath}\n\n`;
@ -111,7 +111,7 @@ async function tagNoteBatch(
if (event.type !== 'tool-invocation') {
return;
}
if (event.toolName !== 'workspace-edit') {
if (event.toolName !== 'file-editText') {
return;
}
try {

View file

@ -1,20 +1,20 @@
---
tools:
workspace-readFile:
file-readText:
type: builtin
name: workspace-readFile
workspace-writeFile:
name: file-readText
file-writeText:
type: builtin
name: workspace-writeFile
workspace-readdir:
name: file-writeText
file-list:
type: builtin
name: workspace-readdir
workspace-mkdir:
name: file-list
file-mkdir:
type: builtin
name: workspace-mkdir
workspace-exists:
name: file-mkdir
file-exists:
type: builtin
name: workspace-exists
name: file-exists
executeCommand:
type: builtin
name: executeCommand
@ -41,8 +41,8 @@ All state is stored in `pre-built/email-draft/`:
On first run, check if state exists. If not, create it:
1. Use `workspace-exists` to check if `pre-built/email-draft/state.json` exists
2. If not, use `workspace-mkdir` to create `pre-built/email-draft/` and `pre-built/email-draft/drafts/`
1. Use `file-exists` to check if `pre-built/email-draft/state.json` exists
2. If not, use `file-mkdir` to create `pre-built/email-draft/` and `pre-built/email-draft/drafts/`
3. Initialize `state.json` with empty arrays and a timestamp of "1970-01-01T00:00:00Z"
## Processing Flow
@ -56,7 +56,7 @@ Read `pre-built/email-draft/state.json` to get:
### Step 2: Scan for New Emails
List emails in `gmail_sync/` folder using `workspace-readdir`.
List emails in `gmail_sync/` folder using `file-list`.
For each email file:
1. Extract the email ID from filename (e.g., `19048cf9c0317981.md``19048cf9c0317981`)

View file

@ -1,20 +1,20 @@
---
tools:
workspace-readFile:
file-readText:
type: builtin
name: workspace-readFile
workspace-writeFile:
name: file-readText
file-writeText:
type: builtin
name: workspace-writeFile
workspace-readdir:
name: file-writeText
file-list:
type: builtin
name: workspace-readdir
workspace-mkdir:
name: file-list
file-mkdir:
type: builtin
name: workspace-mkdir
workspace-exists:
name: file-mkdir
file-exists:
type: builtin
name: workspace-exists
name: file-exists
executeCommand:
type: builtin
name: executeCommand
@ -40,8 +40,8 @@ All state is stored in `pre-built/meeting-prep/`:
On first run, check if state exists. If not, create it:
1. Use `workspace-exists` to check if `pre-built/meeting-prep/state.json` exists
2. If not, use `workspace-mkdir` to create `pre-built/meeting-prep/` and `pre-built/meeting-prep/briefs/`
1. Use `file-exists` to check if `pre-built/meeting-prep/state.json` exists
2. If not, use `file-mkdir` to create `pre-built/meeting-prep/` and `pre-built/meeting-prep/briefs/`
3. Initialize `state.json` with empty `prepared` array and current timestamp
## Processing Flow
@ -54,7 +54,7 @@ Read `pre-built/meeting-prep/state.json` to get:
### Step 2: Scan for Upcoming Meetings
List calendar events in `calendar_sync/` folder using `workspace-readdir`.
List calendar events in `calendar_sync/` folder using `file-list`.
For each event file:
1. Read the JSON content

View file

@ -9,7 +9,7 @@ import { IAbortRegistry } from "./abort-registry.js";
import { IRunsLock } from "./lock.js";
import { forceCloseAllMcpClients } from "../mcp/mcp.js";
import { extractCommandNames } from "../application/lib/command-executor.js";
import { addToSecurityConfig } from "../config/security.js";
import { addFileAccessGrant, addToSecurityConfig } from "../config/security.js";
import { loadAgent } from "../agents/runtime.js";
import { getDefaultModelAndProvider } from "../models/defaults.js";
@ -39,9 +39,9 @@ export async function createRun(opts: z.infer<typeof CreateRunOptions>): Promise
return run;
}
export async function createMessage(runId: string, message: UserMessageContentType, voiceInput?: boolean, voiceOutput?: VoiceOutputMode, searchEnabled?: boolean, middlePaneContext?: MiddlePaneContext): Promise<string> {
export async function createMessage(runId: string, message: UserMessageContentType, voiceInput?: boolean, voiceOutput?: VoiceOutputMode, searchEnabled?: boolean, middlePaneContext?: MiddlePaneContext, codeMode?: 'claude' | 'codex'): Promise<string> {
const queue = container.resolve<IMessageQueue>('messageQueue');
const id = await queue.enqueue(runId, message, voiceInput, voiceOutput, searchEnabled, middlePaneContext);
const id = await queue.enqueue(runId, message, voiceInput, voiceOutput, searchEnabled, middlePaneContext, codeMode);
const runtime = container.resolve<IAgentRuntime>('agentRuntime');
runtime.trigger(runId);
return id;
@ -60,7 +60,12 @@ export async function authorizePermission(runId: string, ev: z.infer<typeof Tool
&& e.toolCall.toolCallId === rest.toolCallId
&& JSON.stringify(e.subflow) === JSON.stringify(rest.subflow)
);
if (permReqEvent && typeof permReqEvent.toolCall.arguments === 'object' && permReqEvent.toolCall.arguments !== null && 'command' in permReqEvent.toolCall.arguments) {
if (permReqEvent?.permission?.kind === "file") {
await addFileAccessGrant({
operation: permReqEvent.permission.operation,
pathPrefix: permReqEvent.permission.pathPrefix,
});
} else if (permReqEvent && typeof permReqEvent.toolCall.arguments === 'object' && permReqEvent.toolCall.arguments !== null && 'command' in permReqEvent.toolCall.arguments) {
const commandNames = extractCommandNames(String(permReqEvent.toolCall.arguments.command));
if (commandNames.length > 0) {
await addToSecurityConfig(commandNames);

View file

@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"exclude": [
"src/**/*.test.ts",
"src/**/*.spec.ts"
]
}

View file

@ -0,0 +1,11 @@
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
environment: "node",
include: ["src/**/*.test.ts", "src/**/*.spec.ts"],
globals: false,
clearMocks: true,
restoreMocks: true,
},
});

View file

@ -3,13 +3,22 @@ import { z } from 'zod';
export const BillingPlanSchema = z.enum(['free', 'starter', 'pro']);
export type BillingPlan = z.infer<typeof BillingPlanSchema>;
export const BillingUsageBucketSchema = z.object({
sanctionedCredits: z.number(),
usedCredits: z.number(),
availableCredits: z.number(),
});
export type BillingUsageBucket = z.infer<typeof BillingUsageBucketSchema>;
export const BillingInfoSchema = z.object({
userEmail: z.string().nullable(),
userId: z.string().nullable(),
subscriptionPlan: BillingPlanSchema.nullable(),
subscriptionStatus: z.string().nullable(),
trialExpiresAt: z.string().nullable(),
sanctionedCredits: z.number(),
availableCredits: z.number(),
monthly: BillingUsageBucketSchema,
daily: BillingUsageBucketSchema.extend({
usageDay: z.string(),
}),
});
export type BillingInfo = z.infer<typeof BillingInfoSchema>;

View file

@ -123,6 +123,7 @@ export const GmailThreadMessageSchema = z.object({
unread: z.boolean().optional(),
bodyHeight: z.number().int().positive().optional(),
attachments: z.array(GmailAttachmentSchema).optional(),
messageIdHeader: z.string().optional(),
});
export type GmailThreadMessage = z.infer<typeof GmailThreadMessageSchema>;

View file

@ -148,6 +148,50 @@ const ipcSchemas = {
req: z.object({}),
res: z.object({}),
},
'gmail:sendReply': {
req: z.object({
threadId: z.string().min(1).optional(),
to: z.string().min(1),
cc: z.string().optional(),
bcc: z.string().optional(),
subject: z.string(),
bodyHtml: z.string(),
bodyText: z.string(),
inReplyTo: z.string().optional(),
references: z.string().optional(),
}),
res: z.object({
messageId: z.string().optional(),
error: z.string().optional(),
}),
},
'gmail:getConnectionStatus': {
req: z.object({}),
res: z.object({
connected: z.boolean(),
hasRequiredScope: z.boolean(),
missingScopes: z.array(z.string()),
email: z.string().nullable(),
}),
},
'gmail:getAccountEmail': {
req: z.object({}),
res: z.object({
email: z.string().nullable(),
}),
},
'gmail:archiveThread': {
req: z.object({ threadId: z.string().min(1) }),
res: z.object({ ok: z.boolean(), error: z.string().optional() }),
},
'gmail:trashThread': {
req: z.object({ threadId: z.string().min(1) }),
res: z.object({ ok: z.boolean(), error: z.string().optional() }),
},
'gmail:markThreadRead': {
req: z.object({ threadId: z.string().min(1) }),
res: z.object({ ok: z.boolean(), error: z.string().optional() }),
},
'gmail:saveMessageHeight': {
req: z.object({
threadId: z.string().min(1),
@ -184,6 +228,7 @@ const ipcSchemas = {
voiceInput: z.boolean().optional(),
voiceOutput: z.enum(['summary', 'full']).optional(),
searchEnabled: z.boolean().optional(),
codeMode: z.enum(['claude', 'codex']).optional(),
middlePaneContext: z.discriminatedUnion('kind', [
z.object({
kind: z.literal('note'),
@ -380,6 +425,27 @@ const ipcSchemas = {
enabled: z.boolean(),
}),
},
'codeMode:getConfig': {
req: z.null(),
res: z.object({
enabled: z.boolean(),
}),
},
'codeMode:setConfig': {
req: z.object({
enabled: z.boolean(),
}),
res: z.object({
success: z.literal(true),
}),
},
'codeMode:checkAgentStatus': {
req: z.null(),
res: z.object({
claude: z.object({ installed: z.boolean(), signedIn: z.boolean() }),
codex: z.object({ installed: z.boolean(), signedIn: z.boolean() }),
}),
},
'granola:setConfig': {
req: z.object({
enabled: z.boolean(),

View file

@ -50,9 +50,29 @@ export const UserContentPart = z.union([UserTextPart, UserAttachmentPart]);
// Named type for user message content — used everywhere instead of repeating the union
export const UserMessageContent = z.union([z.string(), z.array(UserContentPart)]);
export const UserMessageContext = z.object({
currentDateTime: z.string().optional(),
middlePane: z.discriminatedUnion("kind", [
z.object({
kind: z.literal("empty"),
}),
z.object({
kind: z.literal("note"),
path: z.string(),
content: z.string(),
}),
z.object({
kind: z.literal("browser"),
url: z.string(),
title: z.string(),
}),
]).optional(),
});
export const UserMessage = z.object({
role: z.literal("user"),
content: UserMessageContent,
userMessageContext: UserMessageContext.optional(),
providerOptions: ProviderOptions.optional(),
});
@ -86,4 +106,4 @@ export const Message = z.discriminatedUnion("role", [
UserMessage,
]);
export const MessageList = z.array(Message);
export const MessageList = z.array(Message);

View file

@ -75,6 +75,7 @@ export const AskHumanRequestEvent = BaseRunEvent.extend({
type: z.literal("ask-human-request"),
toolCallId: z.string(),
query: z.string(),
options: z.array(z.string()).optional(),
});
export const AskHumanResponseEvent = BaseRunEvent.extend({
@ -83,9 +84,23 @@ export const AskHumanResponseEvent = BaseRunEvent.extend({
response: z.string(),
});
export const ToolPermissionMetadata = z.discriminatedUnion("kind", [
z.object({
kind: z.literal("command"),
commandNames: z.array(z.string()),
}),
z.object({
kind: z.literal("file"),
operation: z.enum(["read", "list", "search", "write", "delete"]),
paths: z.array(z.string()),
pathPrefix: z.string(),
}),
]);
export const ToolPermissionRequestEvent = BaseRunEvent.extend({
type: z.literal("tool-permission-request"),
toolCall: ToolCallPart,
permission: ToolPermissionMetadata.optional(),
});
export const ToolPermissionResponseEvent = BaseRunEvent.extend({

238
apps/x/pnpm-lock.yaml generated
View file

@ -441,6 +441,9 @@ importers:
'@types/pdf-parse':
specifier: ^1.1.5
version: 1.1.5
vitest:
specifier: 'catalog:'
version: 4.1.7(@opentelemetry/api@1.9.0)(@types/node@25.0.3)(vite@7.3.0(@types/node@25.0.3)(jiti@2.6.1)(lightningcss@1.30.2)(terser@5.46.0)(yaml@2.8.2))
packages/shared:
dependencies:
@ -3263,6 +3266,9 @@ packages:
'@types/cacheable-request@6.0.3':
resolution: {integrity: sha512-IQ3EbTzGxIigb1I3qPZc1rWJnH0BmSKv5QYTalEwweFvyBDLSAe24zP0le/hyi7ecGfZVlIVAg4BZqb8WBwKqw==}
'@types/chai@5.2.3':
resolution: {integrity: sha512-Mw558oeA9fFbv65/y4mHtXDs9bPnFMZAL/jxdPFUpOHHIXX91mcgEHbS5Lahr+pwZFR8A7GQleRWeI6cGFC2UA==}
'@types/connect@3.4.38':
resolution: {integrity: sha512-K6uROf1LD88uDQqJCktA4yzL1YYAK6NgfsI0v/mTgyPKWsX1CnJ0XPSDhViejru1GcRkLWb8RlzFYJRqGUbaug==}
@ -3365,6 +3371,9 @@ packages:
'@types/debug@4.1.12':
resolution: {integrity: sha512-vIChWdVG3LG1SMxEvI/AK+FWJthlrqlTu7fbrlywTkkaONwk/UAGaULXRlf8vkzFBLVm0zkMdCquhL5aOjhXPQ==}
'@types/deep-eql@4.0.2':
resolution: {integrity: sha512-c9h9dVVMigMPc4bwTvC5dxqtqJZwQPePsWjPlpSOnojbor6pGqdk541lfA7AqFQr5pB1BRdq0juY9db81BwyFw==}
'@types/electron-squirrel-startup@1.0.2':
resolution: {integrity: sha512-AzxnvBzNh8K/0SmxMmZtpJf1/IWoGXLP+pQDuUaVkPyotI8ryvAtBSqgxR/qOSvxWHYWrxkeNsJ+Ca5xOuUxJQ==}
@ -3572,6 +3581,35 @@ packages:
peerDependencies:
vite: ^4.2.0 || ^5.0.0 || ^6.0.0 || ^7.0.0
'@vitest/expect@4.1.7':
resolution: {integrity: sha512-1R+tw0ortHEbZDGMymm+pN7/AFQ/RkFFdtd7EN+VBpynKmLbP8A3rpEXdshBJ7+8hQ9zBJh/i1s0yKNtxAnU7w==}
'@vitest/mocker@4.1.7':
resolution: {integrity: sha512-vY7nuamKgfvpA1Koa3oYIw/k7D6kZnpGyNMZW8loow2bsBYla1TFdqTaXncWdRn4pgwNs+90RhnXhJScDwQeJA==}
peerDependencies:
msw: ^2.4.9
vite: ^6.0.0 || ^7.0.0 || ^8.0.0
peerDependenciesMeta:
msw:
optional: true
vite:
optional: true
'@vitest/pretty-format@4.1.7':
resolution: {integrity: sha512-umgCarTOYQWIaDMvGDRZij+6b9oVeLIyJzfN+AS88e0ZOU3QTgNNSTtjQOpcvWr3np1N0j4WgZj+sb3oYBDscw==}
'@vitest/runner@4.1.7':
resolution: {integrity: sha512-BapjmAQ2aI78WdMEfeUWivnfVzB+VPGwWRQcJE0OUq7qEeEcBsCSf+0T5iREBNE5nBb4wA5Ya0W6IA+sghdEFw==}
'@vitest/snapshot@4.1.7':
resolution: {integrity: sha512-ZacLzja+TmJeZ1h14xW2FB/WpeimUD3haBXQPyJqxvo8jQTmfeA8zv58mtjN2C7EHXZDYVcVYdYmAxjkWVvKCw==}
'@vitest/spy@4.1.7':
resolution: {integrity: sha512-kbkI5LMWakyuTIvs6fUJ5qdIVb1XVKsYJAT4OJ938cHMROYMSfmoQdZy0aaAnjbbc8F61vkoTqz/Az+/HiIu5Q==}
'@vitest/utils@4.1.7':
resolution: {integrity: sha512-T532WBu791cBxJlCl6SO+J14l81DQx6uQHm1bQbmCDY7nqlEIgkza/UFnSBNaUtSf41unldDFjdOBYEQC4b5Hw==}
'@vscode/sudo-prompt@9.3.2':
resolution: {integrity: sha512-gcXoCN00METUNFeQOFJ+C9xUI0DKB+0EGMVg7wbVYRHBw2Eq3fKisDZOkRdOz3kqXRKOENMfShPOmypw1/8nOw==}
@ -3764,6 +3802,10 @@ packages:
resolution: {integrity: sha512-3duEwti880xqi4eAMN8AyR4a0ByT90zoYdLlevfrvU43vb0YZwZVfxOgxWrLXXXpyugL0hNZc9G6BiB5B3nUug==}
engines: {node: '>=8'}
assertion-error@2.0.1:
resolution: {integrity: sha512-Izi8RQcffqCeNVgFigKli1ssklIbpHnCYc6AknXGYoB6grJqyeby7jv12JUQgmTAnIDnbck1uxksT4dzN3PWBA==}
engines: {node: '>=12'}
async-lock@1.4.1:
resolution: {integrity: sha512-Az2ZTpuytrtqENulXwO3GGv1Bztugx6TT37NIo7imr/Qo0gsYiGtSdBa2B6fsXhTpVZDNfu1Qn3pk531e3q+nQ==}
@ -3940,6 +3982,10 @@ packages:
resolution: {integrity: sha512-KfdUZsSOw19/ObEWasvBP/Ac4reZvAGauZhs6S/gqNhXhI7cKwvlH7ulj+dOEYnca4bm4SGo8C1bTAQvnTjgQA==}
engines: {node: '>=0.8'}
chai@6.2.2:
resolution: {integrity: sha512-NUPRluOfOiTKBKvWPtSD4PhFvWCqOi0BGStNWs57X9js7XGTprSmFoz5F0tWhR4WPjNeR9jXqdC7/UpSJTnlRg==}
engines: {node: '>=18'}
chalk@4.1.2:
resolution: {integrity: sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==}
engines: {node: '>=10'}
@ -4683,6 +4729,9 @@ packages:
estree-util-is-identifier-name@3.0.0:
resolution: {integrity: sha512-hFtqIDZTIUZ9BXLb8y4pYGyk6+wekIivNVTcmvk8NoOh+VeRn5y6cEHzbURrWbfp1fIqdVipilzj+lfaadNZmg==}
estree-walker@3.0.3:
resolution: {integrity: sha512-7RUKfXgSMMkzt6ZuXmqapOurLGPPfgj6l9uRZ7lRGolvk0y2yocc35LdcxKC5PQZdn2DMqioAQ2NoWcrTKmm6g==}
esutils@2.0.3:
resolution: {integrity: sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==}
engines: {node: '>=0.10.0'}
@ -4717,6 +4766,10 @@ packages:
resolution: {integrity: sha512-adbxcyWV46qiHyvSp50TKt05tB4tK3HcmF7/nxfAdhnox83seTDbwnaqKO4sXRy7roHAIFqJP/Rw/AuEbX61LA==}
engines: {node: '>=6'}
expect-type@1.3.0:
resolution: {integrity: sha512-knvyeauYhqjOYvQ66MznSMs83wmHrCycNEN6Ao+2AeYEfxUIkuiVxdEa1qlGEPK+We3n0THiDciYSsCcgW/DoA==}
engines: {node: '>=12.0.0'}
exponential-backoff@3.1.3:
resolution: {integrity: sha512-ZgEeZXj30q+I0EN+CbSSpIyPaJ5HVQD18Z1m+u1FXbAeT94mr1zw50q4q6jiiC447Nl/YTcIYSAftiGqetwXCA==}
@ -6218,6 +6271,9 @@ packages:
resolution: {integrity: sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA==}
engines: {node: '>= 0.4'}
obug@2.1.1:
resolution: {integrity: sha512-uTqF9MuPraAQ+IsnPf366RG4cP9RtUi7MLO1N3KEc+wb0a6yKpeL0lmk2IB1jY5KHPAlTc6T/JRdC/YqxHNwkQ==}
ollama-ai-provider-v2@1.5.5:
resolution: {integrity: sha512-1YwTFdPjhPNHny/DrOHO+s8oVGGIE5Jib61/KnnjPRNWQhVVimrJJdaAX3e6nNRRDXrY5zbb9cfm2+yVvgsrqw==}
engines: {node: '>=18'}
@ -7021,6 +7077,9 @@ packages:
resolution: {integrity: sha512-ZX99e6tRweoUXqR+VBrslhda51Nh5MTQwou5tnUDgbtyM0dBgmhEDtWGP/xbKn6hqfPRHujUNwz5fy/wbbhnpw==}
engines: {node: '>= 0.4'}
siginfo@2.0.0:
resolution: {integrity: sha512-ybx0WO1/8bSBLEWXZvEd7gMW3Sn3JFlW3TvX1nREbDLRNQNaeNN8WK0meBwPdAaOI7TtRRRJn/Es1zhrrCHu7g==}
signal-exit@3.0.7:
resolution: {integrity: sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==}
@ -7099,10 +7158,16 @@ packages:
resolution: {integrity: sha512-o57Wcn66jMQvfHG1FlYbWeZWW/dHZhJXjpIcTfXldXEk5nz5lStPo3mK0OJQfGR3RbZUlbISexbljkJzuEj/8Q==}
engines: {node: ^12.13.0 || ^14.15.0 || >=16.0.0}
stackback@0.0.2:
resolution: {integrity: sha512-1XMJE5fQo1jGH6Y/7ebnwPOBEkIEnT4QF32d5R1+VXdXveM0IBMJt8zfaxX1P3QhVwrYe+576+jkANtSS2mBbw==}
statuses@2.0.2:
resolution: {integrity: sha512-DvEy55V3DB7uknRo+4iOGT5fP1slR8wQohVdknigZPMpMstaKJQWhwiYBACJE3Ul2pTnATihhBYnRhZQHGBiRw==}
engines: {node: '>= 0.8'}
std-env@4.1.0:
resolution: {integrity: sha512-Rq7ybcX2RuC55r9oaPVEW7/xu3tj8u4GeBYHBWCychFtzMIr86A7e3PPEBPT37sHStKX3+TiX/Fr/ACmJLVlLQ==}
stream-browserify@3.0.0:
resolution: {integrity: sha512-H73RAHsVBapbim0tU2JwwOiXUj+fikfiaoYAKHF3VJfA0pe2BCzkhAHBlLG6REzE+2WNZcxOXjK7lkso+9euLA==}
@ -7244,6 +7309,9 @@ packages:
tiny-invariant@1.3.3:
resolution: {integrity: sha512-+FbBPE1o9QAYvviau/qC5SE3caw21q3xkvWKBtja5vgqOWIHHJ3ioaq1VPfn/Szqctz2bU/oYeKd9/z5BL+PVg==}
tinybench@2.9.0:
resolution: {integrity: sha512-0+DUvqWMValLmha6lr4kD8iAMK1HzV0/aKnCtWb9v9641TnP/MFb7Pc2bxoxQjTXAErryXVgUOfv2YqNllqGeg==}
tinyexec@1.0.2:
resolution: {integrity: sha512-W/KYk+NFhkmsYpuHq5JykngiOCnxeVL8v8dFnqxSD8qEEdRfXk1SDM6JzNqcERbcGYj9tMrDQBYV9cjgnunFIg==}
engines: {node: '>=18'}
@ -7252,6 +7320,10 @@ packages:
resolution: {integrity: sha512-j2Zq4NyQYG5XMST4cbs02Ak8iJUdxRM0XI5QyxXuZOzKOINmWurp3smXu3y5wDcJrptwpSjgXHzIQxR0omXljQ==}
engines: {node: '>=12.0.0'}
tinyrainbow@3.1.0:
resolution: {integrity: sha512-Bf+ILmBgretUrdJxzXM0SgXLZ3XfiaUuOj/IKQHuTXip+05Xn+uyEYdVg0kYDipTBcLrCVyUzAPz7QmArb0mmw==}
engines: {node: '>=14.0.0'}
tiptap-markdown@0.9.0:
resolution: {integrity: sha512-dKLQ9iiuGNgrlGVjrNauF/UBzWu4LYOx5pkD0jNkmQt/GOwfCJsBuzZTsf1jZ204ANHOm572mZ9PYvGh1S7tpQ==}
peerDependencies:
@ -7497,6 +7569,7 @@ packages:
uuid@9.0.1:
resolution: {integrity: sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==}
deprecated: uuid@10 and below is no longer supported. For ESM codebases, update to uuid@latest. For CommonJS codebases, use uuid@11 (but be aware this version will likely be deprecated in 2028).
hasBin: true
validate-npm-package-license@3.0.4:
@ -7565,6 +7638,47 @@ packages:
yaml:
optional: true
vitest@4.1.7:
resolution: {integrity: sha512-flYyaFd2CgoCoU+0UKt3pxksgC+S02iTDN0n3LtqaMeXsI9SBcdNujc2k0DeFLzUn/0k538yNjOSdwgCqcrwJA==}
engines: {node: ^20.0.0 || ^22.0.0 || >=24.0.0}
hasBin: true
peerDependencies:
'@edge-runtime/vm': '*'
'@opentelemetry/api': ^1.9.0
'@types/node': ^20.0.0 || ^22.0.0 || >=24.0.0
'@vitest/browser-playwright': 4.1.7
'@vitest/browser-preview': 4.1.7
'@vitest/browser-webdriverio': 4.1.7
'@vitest/coverage-istanbul': 4.1.7
'@vitest/coverage-v8': 4.1.7
'@vitest/ui': 4.1.7
happy-dom: '*'
jsdom: '*'
vite: ^6.0.0 || ^7.0.0 || ^8.0.0
peerDependenciesMeta:
'@edge-runtime/vm':
optional: true
'@opentelemetry/api':
optional: true
'@types/node':
optional: true
'@vitest/browser-playwright':
optional: true
'@vitest/browser-preview':
optional: true
'@vitest/browser-webdriverio':
optional: true
'@vitest/coverage-istanbul':
optional: true
'@vitest/coverage-v8':
optional: true
'@vitest/ui':
optional: true
happy-dom:
optional: true
jsdom:
optional: true
vscode-jsonrpc@8.2.0:
resolution: {integrity: sha512-C+r0eKJUIfiDIfwJhria30+TYWPtuHJXHtI7J0YlOmKAo7ogxP20T0zxB7HZQIFhIyvoBPwWskjxrvAtfjyZfA==}
engines: {node: '>=14.0.0'}
@ -7643,6 +7757,11 @@ packages:
engines: {node: '>= 8'}
hasBin: true
why-is-node-running@2.3.0:
resolution: {integrity: sha512-hUrmaWBdVDcxvYqnyh09zunKzROWjbZTiNy8dBEjkS7ehEDQibXJ7XvlmtbwuTclUiIyN+CyXQD4Vmko8fNm8w==}
engines: {node: '>=8'}
hasBin: true
wmf@1.0.2:
resolution: {integrity: sha512-/p9K7bEh0Dj6WbXg4JG0xvLQmIadrner1bi45VMJTfnbVHsc7yIajZyoSoK60/dtVBs12Fm6WkUI5/3WAVsNMw==}
engines: {node: '>=0.8'}
@ -11306,6 +11425,11 @@ snapshots:
'@types/node': 25.0.3
'@types/responselike': 1.0.3
'@types/chai@5.2.3':
dependencies:
'@types/deep-eql': 4.0.2
assertion-error: 2.0.1
'@types/connect@3.4.38':
dependencies:
'@types/node': 25.0.3
@ -11435,6 +11559,8 @@ snapshots:
dependencies:
'@types/ms': 2.1.0
'@types/deep-eql@4.0.2': {}
'@types/electron-squirrel-startup@1.0.2': {}
'@types/eslint-scope@3.7.7':
@ -11692,6 +11818,47 @@ snapshots:
transitivePeerDependencies:
- supports-color
'@vitest/expect@4.1.7':
dependencies:
'@standard-schema/spec': 1.1.0
'@types/chai': 5.2.3
'@vitest/spy': 4.1.7
'@vitest/utils': 4.1.7
chai: 6.2.2
tinyrainbow: 3.1.0
'@vitest/mocker@4.1.7(vite@7.3.0(@types/node@25.0.3)(jiti@2.6.1)(lightningcss@1.30.2)(terser@5.46.0)(yaml@2.8.2))':
dependencies:
'@vitest/spy': 4.1.7
estree-walker: 3.0.3
magic-string: 0.30.21
optionalDependencies:
vite: 7.3.0(@types/node@25.0.3)(jiti@2.6.1)(lightningcss@1.30.2)(terser@5.46.0)(yaml@2.8.2)
'@vitest/pretty-format@4.1.7':
dependencies:
tinyrainbow: 3.1.0
'@vitest/runner@4.1.7':
dependencies:
'@vitest/utils': 4.1.7
pathe: 2.0.3
'@vitest/snapshot@4.1.7':
dependencies:
'@vitest/pretty-format': 4.1.7
'@vitest/utils': 4.1.7
magic-string: 0.30.21
pathe: 2.0.3
'@vitest/spy@4.1.7': {}
'@vitest/utils@4.1.7':
dependencies:
'@vitest/pretty-format': 4.1.7
convert-source-map: 2.0.0
tinyrainbow: 3.1.0
'@vscode/sudo-prompt@9.3.2': {}
'@webassemblyjs/ast@1.14.1':
@ -11906,6 +12073,8 @@ snapshots:
arrify@2.0.1: {}
assertion-error@2.0.1: {}
async-lock@1.4.1: {}
async@1.5.2:
@ -12119,6 +12288,8 @@ snapshots:
adler-32: 1.3.1
crc-32: 1.2.2
chai@6.2.2: {}
chalk@4.1.2:
dependencies:
ansi-styles: 4.3.0
@ -12974,6 +13145,10 @@ snapshots:
estree-util-is-identifier-name@3.0.0: {}
estree-walker@3.0.3:
dependencies:
'@types/estree': 1.0.8
esutils@2.0.3: {}
etag@1.8.1: {}
@ -13004,6 +13179,8 @@ snapshots:
signal-exit: 3.0.7
strip-eof: 1.0.0
expect-type@1.3.0: {}
exponential-backoff@3.1.3: {}
express-rate-limit@7.5.1(express@5.2.1):
@ -14943,6 +15120,8 @@ snapshots:
object-keys@1.1.1:
optional: true
obug@2.1.1: {}
ollama-ai-provider-v2@1.5.5(zod@4.2.1):
dependencies:
'@ai-sdk/provider': 2.0.1
@ -15935,6 +16114,8 @@ snapshots:
side-channel-map: 1.0.1
side-channel-weakmap: 1.0.2
siginfo@2.0.0: {}
signal-exit@3.0.7: {}
signal-exit@4.1.0: {}
@ -16014,8 +16195,12 @@ snapshots:
dependencies:
minipass: 3.3.6
stackback@0.0.2: {}
statuses@2.0.2: {}
std-env@4.1.0: {}
stream-browserify@3.0.0:
dependencies:
inherits: 2.0.4
@ -16182,6 +16367,8 @@ snapshots:
tiny-invariant@1.3.3: {}
tinybench@2.9.0: {}
tinyexec@1.0.2: {}
tinyglobby@0.2.15:
@ -16189,6 +16376,8 @@ snapshots:
fdir: 6.5.0(picomatch@4.0.3)
picomatch: 4.0.3
tinyrainbow@3.1.0: {}
tiptap-markdown@0.9.0(@tiptap/core@3.22.4(@tiptap/pm@3.22.4)):
dependencies:
'@tiptap/core': 3.22.4(@tiptap/pm@3.22.4)
@ -16503,6 +16692,50 @@ snapshots:
terser: 5.46.0
yaml: 2.8.2
vite@7.3.0(@types/node@25.0.3)(jiti@2.6.1)(lightningcss@1.30.2)(terser@5.46.0)(yaml@2.8.2):
dependencies:
esbuild: 0.27.2
fdir: 6.5.0(picomatch@4.0.3)
picomatch: 4.0.3
postcss: 8.5.6
rollup: 4.54.0
tinyglobby: 0.2.15
optionalDependencies:
'@types/node': 25.0.3
fsevents: 2.3.3
jiti: 2.6.1
lightningcss: 1.30.2
terser: 5.46.0
yaml: 2.8.2
vitest@4.1.7(@opentelemetry/api@1.9.0)(@types/node@25.0.3)(vite@7.3.0(@types/node@25.0.3)(jiti@2.6.1)(lightningcss@1.30.2)(terser@5.46.0)(yaml@2.8.2)):
dependencies:
'@vitest/expect': 4.1.7
'@vitest/mocker': 4.1.7(vite@7.3.0(@types/node@25.0.3)(jiti@2.6.1)(lightningcss@1.30.2)(terser@5.46.0)(yaml@2.8.2))
'@vitest/pretty-format': 4.1.7
'@vitest/runner': 4.1.7
'@vitest/snapshot': 4.1.7
'@vitest/spy': 4.1.7
'@vitest/utils': 4.1.7
es-module-lexer: 2.0.0
expect-type: 1.3.0
magic-string: 0.30.21
obug: 2.1.1
pathe: 2.0.3
picomatch: 4.0.3
std-env: 4.1.0
tinybench: 2.9.0
tinyexec: 1.0.2
tinyglobby: 0.2.15
tinyrainbow: 3.1.0
vite: 7.3.0(@types/node@25.0.3)(jiti@2.6.1)(lightningcss@1.30.2)(terser@5.46.0)(yaml@2.8.2)
why-is-node-running: 2.3.0
optionalDependencies:
'@opentelemetry/api': 1.9.0
'@types/node': 25.0.3
transitivePeerDependencies:
- msw
vscode-jsonrpc@8.2.0: {}
vscode-languageserver-protocol@3.17.5:
@ -16606,6 +16839,11 @@ snapshots:
dependencies:
isexe: 2.0.0
why-is-node-running@2.3.0:
dependencies:
siginfo: 2.0.0
stackback: 0.0.2
wmf@1.0.2: {}
word-wrap@1.2.5: {}

View file

@ -2,6 +2,9 @@ packages:
- apps/*
- packages/*
catalog:
vitest: 4.1.7
onlyBuiltDependencies:
- core-js
- electron