diff --git a/plugin/scripts/context-hook.js b/plugin/scripts/context-hook.js index cbb12c5a..961250ca 100755 --- a/plugin/scripts/context-hook.js +++ b/plugin/scripts/context-hook.js @@ -1,7 +1,7 @@ #!/usr/bin/env node -import K from"path";import j from"better-sqlite3";import{join as _,dirname as X,basename as q}from"path";import{homedir as C}from"os";import{existsSync as Z,mkdirSync as F}from"fs";import{fileURLToPath as P}from"url";function G(){return typeof __dirname<"u"?__dirname:X(P(import.meta.url))}var W=G(),m=process.env.CLAUDE_MEM_DATA_DIR||_(C(),".claude-mem"),R=process.env.CLAUDE_CONFIG_DIR||_(C(),".claude"),se=_(m,"archives"),te=_(m,"logs"),re=_(m,"trash"),ne=_(m,"backups"),ie=_(m,"settings.json"),x=_(m,"claude-mem.db"),oe=_(R,"settings.json"),ae=_(R,"commands"),de=_(R,"CLAUDE.md");function D(p){F(p,{recursive:!0})}function k(){return _(W,"..","..")}var I=(i=>(i[i.DEBUG=0]="DEBUG",i[i.INFO=1]="INFO",i[i.WARN=2]="WARN",i[i.ERROR=3]="ERROR",i[i.SILENT=4]="SILENT",i))(I||{}),O=class{level;useColor;constructor(){let e=process.env.CLAUDE_MEM_LOG_LEVEL?.toUpperCase()||"INFO";this.level=I[e]??1,this.useColor=process.stdout.isTTY??!1}correlationId(e,s){return`obs-${e}-${s}`}sessionId(e){return`session-${e}`}formatData(e){if(e==null)return"";if(typeof e=="string")return e;if(typeof e=="number"||typeof e=="boolean")return e.toString();if(typeof e=="object"){if(e instanceof Error)return this.level===0?`${e.message} -${e.stack}`:e.message;if(Array.isArray(e))return`[${e.length} items]`;let s=Object.keys(e);return s.length===0?"{}":s.length<=3?JSON.stringify(e):`{${s.length} keys: ${s.slice(0,3).join(", ")}...}`}return String(e)}formatTool(e,s){if(!s)return e;try{let t=typeof s=="string"?JSON.parse(s):s;if(e==="Bash"&&t.command){let r=t.command.length>50?t.command.substring(0,50)+"...":t.command;return`${e}(${r})`}if(e==="Read"&&t.file_path){let r=t.file_path.split("/").pop()||t.file_path;return`${e}(${r})`}if(e==="Edit"&&t.file_path){let r=t.file_path.split("/").pop()||t.file_path;return`${e}(${r})`}if(e==="Write"&&t.file_path){let r=t.file_path.split("/").pop()||t.file_path;return`${e}(${r})`}return e}catch{return e}}log(e,s,t,r,i){if(e0&&(b=` {${Object.entries(u).map(([g,M])=>`${g}=${M}`).join(", ")}}`)}let S=`[${d}] [${n}] [${c}] ${E}${t}${b}${a}`;e===3?console.error(S):console.log(S)}debug(e,s,t,r){this.log(0,e,s,t,r)}info(e,s,t,r){this.log(1,e,s,t,r)}warn(e,s,t,r){this.log(2,e,s,t,r)}error(e,s,t,r){this.log(3,e,s,t,r)}dataIn(e,s,t,r){this.info(e,`\u2192 ${s}`,t,r)}dataOut(e,s,t,r){this.info(e,`\u2190 ${s}`,t,r)}success(e,s,t,r){this.info(e,`\u2713 ${s}`,t,r)}failure(e,s,t,r){this.error(e,`\u2717 ${s}`,t,r)}timing(e,s,t,r){this.info(e,`\u23F1 ${s}`,r,{duration:`${t}ms`})}},U=new O;var h=class{db;constructor(){D(m),this.db=new j(x),this.db.pragma("journal_mode = WAL"),this.db.pragma("synchronous = NORMAL"),this.db.pragma("foreign_keys = ON"),this.initializeSchema(),this.ensureWorkerPortColumn(),this.ensurePromptTrackingColumns(),this.removeSessionSummariesUniqueConstraint(),this.addObservationHierarchicalFields(),this.makeObservationsTextNullable(),this.createUserPromptsTable()}initializeSchema(){try{this.db.exec(` +import K from"path";import j from"better-sqlite3";import{join as E,dirname as X,basename as q}from"path";import{homedir as C}from"os";import{existsSync as Z,mkdirSync as F}from"fs";import{fileURLToPath as P}from"url";function G(){return typeof __dirname<"u"?__dirname:X(P(import.meta.url))}var W=G(),m=process.env.CLAUDE_MEM_DATA_DIR||E(C(),".claude-mem"),R=process.env.CLAUDE_CONFIG_DIR||E(C(),".claude"),se=E(m,"archives"),te=E(m,"logs"),re=E(m,"trash"),ne=E(m,"backups"),ie=E(m,"settings.json"),x=E(m,"claude-mem.db"),oe=E(R,"settings.json"),ae=E(R,"commands"),de=E(R,"CLAUDE.md");function D(p){F(p,{recursive:!0})}function k(){return E(W,"..","..")}var I=(i=>(i[i.DEBUG=0]="DEBUG",i[i.INFO=1]="INFO",i[i.WARN=2]="WARN",i[i.ERROR=3]="ERROR",i[i.SILENT=4]="SILENT",i))(I||{}),O=class{level;useColor;constructor(){let e=process.env.CLAUDE_MEM_LOG_LEVEL?.toUpperCase()||"INFO";this.level=I[e]??1,this.useColor=process.stdout.isTTY??!1}correlationId(e,s){return`obs-${e}-${s}`}sessionId(e){return`session-${e}`}formatData(e){if(e==null)return"";if(typeof e=="string")return e;if(typeof e=="number"||typeof e=="boolean")return e.toString();if(typeof e=="object"){if(e instanceof Error)return this.level===0?`${e.message} +${e.stack}`:e.message;if(Array.isArray(e))return`[${e.length} items]`;let s=Object.keys(e);return s.length===0?"{}":s.length<=3?JSON.stringify(e):`{${s.length} keys: ${s.slice(0,3).join(", ")}...}`}return String(e)}formatTool(e,s){if(!s)return e;try{let t=typeof s=="string"?JSON.parse(s):s;if(e==="Bash"&&t.command){let r=t.command.length>50?t.command.substring(0,50)+"...":t.command;return`${e}(${r})`}if(e==="Read"&&t.file_path){let r=t.file_path.split("/").pop()||t.file_path;return`${e}(${r})`}if(e==="Edit"&&t.file_path){let r=t.file_path.split("/").pop()||t.file_path;return`${e}(${r})`}if(e==="Write"&&t.file_path){let r=t.file_path.split("/").pop()||t.file_path;return`${e}(${r})`}return e}catch{return e}}log(e,s,t,r,i){if(e0&&(b=` {${Object.entries(u).map(([g,M])=>`${g}=${M}`).join(", ")}}`)}let S=`[${d}] [${n}] [${c}] ${_}${t}${b}${a}`;e===3?console.error(S):console.log(S)}debug(e,s,t,r){this.log(0,e,s,t,r)}info(e,s,t,r){this.log(1,e,s,t,r)}warn(e,s,t,r){this.log(2,e,s,t,r)}error(e,s,t,r){this.log(3,e,s,t,r)}dataIn(e,s,t,r){this.info(e,`\u2192 ${s}`,t,r)}dataOut(e,s,t,r){this.info(e,`\u2190 ${s}`,t,r)}success(e,s,t,r){this.info(e,`\u2713 ${s}`,t,r)}failure(e,s,t,r){this.error(e,`\u2717 ${s}`,t,r)}timing(e,s,t,r){this.info(e,`\u23F1 ${s}`,r,{duration:`${t}ms`})}},U=new O;var h=class{db;constructor(){D(m),this.db=new j(x),this.db.pragma("journal_mode = WAL"),this.db.pragma("synchronous = NORMAL"),this.db.pragma("foreign_keys = ON"),this.initializeSchema(),this.ensureWorkerPortColumn(),this.ensurePromptTrackingColumns(),this.removeSessionSummariesUniqueConstraint(),this.addObservationHierarchicalFields(),this.makeObservationsTextNullable(),this.createUserPromptsTable()}initializeSchema(){try{this.db.exec(` CREATE TABLE IF NOT EXISTS schema_versions ( id INTEGER PRIMARY KEY, version INTEGER UNIQUE NOT NULL, @@ -311,10 +311,10 @@ ${e.stack}`:e.message;if(Array.isArray(e))return`[${e.length} items]`;let s=Obje SELECT sdk_session_id, request, learned, completed, next_steps, created_at FROM session_summaries WHERE project = ? - ORDER BY created_at_epoch DESC + ORDER BY created_at DESC LIMIT 10 ) - ORDER BY created_at_epoch ASC + ORDER BY created_at ASC `).all(r);if(d.length===0)return e?` ${o.bright}${o.cyan}\u{1F4DD} [${r}] recent context${o.reset} ${o.gray}${"\u2500".repeat(60)}${o.reset} @@ -322,7 +322,7 @@ ${o.gray}${"\u2500".repeat(60)}${o.reset} ${o.dim}No previous summaries found for this project yet.${o.reset} `:`# [${r}] recent context -No previous summaries found for this project yet.`;let n=[];e?(n.push(""),n.push(`${o.bright}${o.cyan}\u{1F4DD} [${r}] recent context${o.reset}`),n.push(`${o.gray}${"\u2500".repeat(60)}${o.reset}`)):(n.push(`# [${r}] recent context`),n.push(""));let c=null,E=!0;for(let a of d){c!==null&&a.sdk_session_id!==c?e?(n.push(""),n.push(`${o.dim}${"\u2500".repeat(23)} New Summary ${"\u2500".repeat(24)}${o.reset}`),n.push("")):(n.push(""),n.push("---"),n.push("")):E?e&&n.push(""):e?(n.push(`${o.gray}${"\u2500".repeat(60)}${o.reset}`),n.push("")):(n.push("---"),n.push("")),E=!1,a.request&&(e?(n.push(`${o.bright}${o.yellow}Request:${o.reset} ${a.request}`),n.push("")):(n.push(`**Request:** ${a.request}`),n.push(""))),a.learned&&(e?(n.push(`${o.bright}${o.blue}Learned:${o.reset} ${a.learned}`),n.push("")):(n.push(`**Learned:** ${a.learned}`),n.push(""))),a.completed&&(e?(n.push(`${o.bright}${o.green}Completed:${o.reset} ${a.completed}`),n.push("")):(n.push(`**Completed:** ${a.completed}`),n.push(""))),a.next_steps&&(e?(n.push(`${o.bright}${o.magenta}Next Steps:${o.reset} ${a.next_steps}`),n.push("")):(n.push(`**Next Steps:** ${a.next_steps}`),n.push("")));let S=i.db.prepare(` +No previous summaries found for this project yet.`;let n=[];e?(n.push(""),n.push(`${o.bright}${o.cyan}\u{1F4DD} [${r}] recent context${o.reset}`),n.push(`${o.gray}${"\u2500".repeat(60)}${o.reset}`)):(n.push(`# [${r}] recent context`),n.push(""));let c=null,_=!0;for(let a of d){c!==null&&a.sdk_session_id!==c?e?(n.push(""),n.push(`${o.dim}${"\u2500".repeat(23)} New Summary ${"\u2500".repeat(24)}${o.reset}`),n.push("")):(n.push(""),n.push("---"),n.push("")):_?e&&n.push(""):e?(n.push(`${o.gray}${"\u2500".repeat(60)}${o.reset}`),n.push("")):(n.push("---"),n.push("")),_=!1,a.request&&(e?(n.push(`${o.bright}${o.yellow}Request:${o.reset} ${a.request}`),n.push("")):(n.push(`**Request:** ${a.request}`),n.push(""))),a.learned&&(e?(n.push(`${o.bright}${o.blue}Learned:${o.reset} ${a.learned}`),n.push("")):(n.push(`**Learned:** ${a.learned}`),n.push(""))),a.completed&&(e?(n.push(`${o.bright}${o.green}Completed:${o.reset} ${a.completed}`),n.push("")):(n.push(`**Completed:** ${a.completed}`),n.push(""))),a.next_steps&&(e?(n.push(`${o.bright}${o.magenta}Next Steps:${o.reset} ${a.next_steps}`),n.push("")):(n.push(`**Next Steps:** ${a.next_steps}`),n.push("")));let S=i.db.prepare(` SELECT files_read, files_modified FROM observations WHERE sdk_session_id = ? diff --git a/src/hooks/context.ts b/src/hooks/context.ts index 97c73f66..6cfc3c11 100644 --- a/src/hooks/context.ts +++ b/src/hooks/context.ts @@ -42,10 +42,10 @@ export function contextHook(input?: SessionStartInput, useColors: boolean = fals SELECT sdk_session_id, request, learned, completed, next_steps, created_at FROM session_summaries WHERE project = ? - ORDER BY created_at_epoch DESC + ORDER BY created_at DESC LIMIT 10 ) - ORDER BY created_at_epoch ASC + ORDER BY created_at ASC `).all(project) as Array<{ sdk_session_id: string; request: string | null;