Remove keyword/params hooks, add planning state

This commit is contained in:
Chris Scott committed 2026-02-24 21:13:27 -05:00
1 parent 0a4be46fd3
commit 745b883052
5 files changed
+100 -440

No files matched your search

-2
View File
@@ -1,6 +1,4 @@
export { createSessionHooks, type SessionHooks } from './session'
export { createKeywordHooks, type KeywordHooks, ACTIVATION_CONTEXT } from './keyword'
export { createParamsHooks, type ParamsHooks } from './params'
export {
buildCustomCompactionPrompt,
formatPlanningState,
-127
View File
@@ -1,127 +0,0 @@
import type { Logger } from '../types'
export interface KeywordHooks {
onMessage: (input: unknown, output: unknown) => Promise<void>
isActivated: (sessionId: string) => boolean
getMode: (sessionId: string) => string | undefined
}
interface TextPart {
type: 'text'
text: string
synthetic?: boolean
ignored?: boolean
}
interface ChatMessageInput {
sessionID?: string
agent?: string
messageID?: string
}
interface ChatMessageOutput {
message?: { id?: string; role: string; sessionID?: string }
parts?: Array<TextPart>
}
interface ActivationState {
activated: boolean
mode?: string
}
const KEYWORD_PATTERNS = [
/remember\s+(this|that)/i,
/recall/i,
/what\s+do\s+you\s+know\s+about/i,
/project\s+memory/i,
/do\s+you\s+remember/i,
/stored\s+memory/i,
/from\s+memory/i,
]
export const ACTIVATION_CONTEXT = `## Project Memory Available
This project has a memory system that stores architectural decisions, conventions, and patterns. You have access to:
- memory-read: Search and retrieve project memories
- memory-write: Store new project memories as suggestions
- memory-delete: Delete a memory by ID
Memory scopes: convention, decision, context
When relevant, use these tools to provide context from the project's knowledge base.`
const MODE_PATTERNS: Record<string, RegExp[]> = {
creative: [
/brainstorm/i,
/be\s+creative/i,
/explore\s+options/i,
/generate\s+ideas/i,
],
deepThink: [
/think\s+hard/i,
/think\s+deeply/i,
/analyze\s+carefully/i,
/think\s+through/i,
],
thorough: [
/go\s+deep/i,
/be\s+thorough/i,
/take\s+your\s+time/i,
],
}
export function createKeywordHooks(logger: Logger): KeywordHooks {
const sessionStateMap = new Map<string, ActivationState>()
return {
async onMessage(input, output) {
const chatInput = input as ChatMessageInput
const chatOutput = output as ChatMessageOutput
const sessionId = chatInput.sessionID
if (!sessionId) return
const existingState = sessionStateMap.get(sessionId)
if (existingState?.activated && existingState.mode) return
const parts = chatOutput.parts ?? []
const textParts = parts.filter((p): p is TextPart => {
if (p.type !== 'text') return false
return typeof p.text === 'string' && p.text.length > 0
})
const userContent = textParts.map(p => p.text).join(' ')
if (!userContent) return
const hasKeyword = KEYWORD_PATTERNS.some((pattern) => pattern.test(userContent))
const matchedModeEntry = Object.entries(MODE_PATTERNS).find(([, patterns]) =>
patterns.some((pattern) => pattern.test(userContent))
)
const matchedMode = matchedModeEntry?.[0]
if (!hasKeyword && !matchedMode) return
const state = sessionStateMap.get(sessionId) ?? { activated: false }
if (hasKeyword && !state.activated) {
logger.log(`Keyword match detected in session ${sessionId}, setting activation flag`)
state.activated = true
}
if (matchedMode && !state.mode) {
logger.log(`Mode pattern detected in session ${sessionId}: ${matchedMode}`)
state.mode = matchedMode
}
sessionStateMap.set(sessionId, state)
},
isActivated(sessionId: string): boolean {
return sessionStateMap.get(sessionId)?.activated ?? false
},
getMode(sessionId: string): string | undefined {
return sessionStateMap.get(sessionId)?.mode
},
}
}
-73
View File
@@ -1,73 +0,0 @@
import type { KeywordHooks } from './keyword'
export interface ParamsHooks {
onParams: (input: unknown, output: unknown) => Promise<void>
}
interface ChatParamsInput {
sessionID?: string
agent: string
}
interface ChatParamsOutput {
temperature?: number
topP?: number
topK?: number
options?: Record<string, any>
}
type ModeConfig = {
temperature?: number
maxSteps?: number
thinkingBudgetTokens?: number
}
const MODE_CONFIGS: Record<string, ModeConfig> = {
creative: {
temperature: 0.8,
},
deepThink: {
thinkingBudgetTokens: 32000,
},
thorough: {
maxSteps: 50,
},
}
export function createParamsHooks(keywordHooks: KeywordHooks): ParamsHooks {
return {
async onParams(input, output) {
const paramsInput = input as ChatParamsInput
const paramsOutput = output as ChatParamsOutput
const sessionId = paramsInput.sessionID
if (!sessionId) return
const mode = keywordHooks.getMode(sessionId)
if (!mode) return
const config = MODE_CONFIGS[mode]
if (!config) return
if (config.temperature !== undefined) {
paramsOutput.temperature = config.temperature
}
if (config.maxSteps !== undefined) {
if (!paramsOutput.options) {
paramsOutput.options = {}
}
paramsOutput.options.maxSteps = config.maxSteps
}
if (config.thinkingBudgetTokens !== undefined) {
if (!paramsOutput.options) {
paramsOutput.options = {}
}
paramsOutput.options.thinking = {
budgetTokens: config.thinkingBudgetTokens,
}
}
},
}
}
+19 -4
View File
@@ -52,11 +52,21 @@ function formatEventProperties(props?: Record<string, unknown>): string {
}
}
function buildSubtaskPrompt(sessionId: string, compactionSummary: string): string {
return `Review the following compaction summary and extract any project knowledge worth preserving across sessions.
function buildSubtaskPrompt(
sessionId: string,
compactionSummary: string,
planningState: PlanningState | null
): string {
const planningSection = planningState
? `## Current Planning State\n\n${formatPlanningState(planningState) ?? '(no details)'}\n\n---\n`
: ''
return `Review the following and extract any project knowledge worth preserving across sessions.
${planningSection}## Compaction Summary
---
${compactionSummary}
---
For each item found, store it with the appropriate scope:
@@ -109,6 +119,11 @@ export function createSessionHooks(
logger.log(`Post-compaction: fetched compaction summary (${compactionSummary.length} chars)`)
const planningState = sessionStateService.getPlanningState(sessionId)
if (planningState) {
logger.log(`Post-compaction: fetched planning state for session ${sessionId}`)
}
await ctx.client.session.prompt({
path: { id: sessionId },
body: {
@@ -117,7 +132,7 @@ export function createSessionHooks(
type: 'subtask',
agent: 'Memory',
description: 'Memory extraction after compaction',
prompt: buildSubtaskPrompt(sessionId, compactionSummary),
prompt: buildSubtaskPrompt(sessionId, compactionSummary, planningState),
},
],
},