-
Notifications
You must be signed in to change notification settings - Fork 156
feat: add session-color patch for env-based prompt bar color #657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
VitalyOstanin
wants to merge
5
commits into
Piebald-AI:main
Choose a base branch
from
VitalyOstanin:feature/session-color
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+225
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b837af3
feat: add session-color patch for env-based prompt bar color
VitalyOstanin ecfbe75
Address review feedback: ordering and idempotency guard
VitalyOstanin 46f3e6a
Fix showDiff args: use pre-patch buffer for consistent indices
VitalyOstanin 644621a
Persist session color to transcript for resume support
VitalyOstanin 9cbff05
Fix showDiff injectedText args and move implementation to Always Appl…
VitalyOstanin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { writeSessionColor, patchSaveAgentColor } from './sessionColor'; | ||
|
|
||
| const makeSaveAgentColor = () => | ||
| ';async function Mr$(H,$,q){let K=q??sT(H);' + | ||
| 'if(Hv(K,{type:"agent-color",agentColor:$,sessionId:H}),H===V$())' + | ||
| 'WA().currentSessionAgentColor=$;c("tengu_agent_color_set",{})}'; | ||
|
|
||
| const makeCLIState = () => | ||
| 'effortValue:oR(w.effort),' + | ||
| 'activeOverlays:new Set,fastMode:cP8(N5),' + | ||
| '...(uF()&&d1&&{advisorModel:d1})'; | ||
|
|
||
| const makeDefaultState = () => | ||
| 'effortValue:void 0,' + 'activeOverlays:new Set,fastMode:!1}'; | ||
|
|
||
| const makeBoth = () => | ||
| 'first{' + makeDefaultState() + 'second{' + makeCLIState(); | ||
|
|
||
| const makeFullFile = () => makeBoth() + makeSaveAgentColor(); | ||
|
|
||
| describe('sessionColor', () => { | ||
| describe('writeSessionColor', () => { | ||
| it('should inject into CLI initialState and patch saveAgentColor', () => { | ||
| const result = writeSessionColor(makeFullFile()); | ||
| expect(result).not.toBeNull(); | ||
| expect(result).toContain('TWEAKCC_SESSION_COLOR'); | ||
| expect(result).toContain('{name:"",color:__c}'); | ||
| expect(result).toContain('__tweakccSaveAgentColor'); | ||
| }); | ||
|
|
||
| it('should inject into default app state', () => { | ||
| const input = makeDefaultState() + makeSaveAgentColor(); | ||
| const result = writeSessionColor(input); | ||
| expect(result).not.toBeNull(); | ||
| expect(result).toContain('TWEAKCC_SESSION_COLOR'); | ||
| }); | ||
|
|
||
| it('should patch both initialState locations', () => { | ||
| const result = writeSessionColor(makeFullFile())!; | ||
| expect(result).not.toBeNull(); | ||
| const count = (result.match(/TWEAKCC_SESSION_COLOR/g) || []).length; | ||
| expect(count).toBe(2); | ||
| }); | ||
|
|
||
| it('should validate color against allowed list', () => { | ||
| const result = writeSessionColor(makeFullFile())!; | ||
| expect(result).toContain('.includes(__c)'); | ||
| expect(result).toContain('"green"'); | ||
| expect(result).toContain('"cyan"'); | ||
| }); | ||
|
|
||
| it('should be idempotent', () => { | ||
| const first = writeSessionColor(makeFullFile())!; | ||
| const second = writeSessionColor(first)!; | ||
| expect(second).toBe(first); | ||
| }); | ||
|
|
||
| it('should return null when no pattern found', () => { | ||
| const result = writeSessionColor('not a valid file'); | ||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| it('should schedule color save via queueMicrotask', () => { | ||
| const result = writeSessionColor(makeFullFile())!; | ||
| expect(result).toContain('queueMicrotask'); | ||
| expect(result).toContain('__tweakccSaveAgentColor'); | ||
| }); | ||
|
|
||
| it('should expose saveAgentColor on globalThis', () => { | ||
| const result = writeSessionColor(makeFullFile())!; | ||
| expect(result).toContain( | ||
| 'globalThis.__tweakccSaveAgentColor=(c)=>Mr$(V$(),c)' | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('patchSaveAgentColor', () => { | ||
| it('should find and patch saveAgentColor', () => { | ||
| const result = patchSaveAgentColor(makeSaveAgentColor()); | ||
| expect(result).not.toBeNull(); | ||
| expect(result).toContain('globalThis.__tweakccSaveAgentColor'); | ||
| }); | ||
|
|
||
| it('should return null when pattern not found', () => { | ||
| const result = patchSaveAgentColor('no match here'); | ||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| it('should preserve original function', () => { | ||
| const result = patchSaveAgentColor(makeSaveAgentColor())!; | ||
| expect(result).toContain('async function Mr$'); | ||
| expect(result).toContain('type:"agent-color"'); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import { showDiff } from './index'; | ||
| import { debug } from '../utils'; | ||
|
|
||
| const VALID_COLORS = [ | ||
| 'red', | ||
| 'blue', | ||
| 'green', | ||
| 'yellow', | ||
| 'purple', | ||
| 'orange', | ||
| 'pink', | ||
| 'cyan', | ||
| ]; | ||
|
|
||
| const INJECTION = | ||
| `,standaloneAgentContext:` + | ||
| `(()=>{` + | ||
| `let __c=process.env.TWEAKCC_SESSION_COLOR;` + | ||
| `if(!__c||!${JSON.stringify(VALID_COLORS)}.includes(__c))return void 0;` + | ||
| `queueMicrotask(()=>{` + | ||
| `if(globalThis.__tweakccSaveAgentColor)globalThis.__tweakccSaveAgentColor(__c)` + | ||
| `});` + | ||
| `return{name:"",color:__c}` + | ||
| `})()`; | ||
|
|
||
| export const writeSessionColor = (oldFile: string): string | null => { | ||
| if ( | ||
| oldFile.includes( | ||
| 'standaloneAgentContext:(()=>{let __c=process.env.TWEAKCC_SESSION_COLOR;' | ||
| ) | ||
| ) { | ||
| return oldFile; | ||
| } | ||
|
|
||
| const patterns = [ | ||
| /,activeOverlays:new Set,fastMode:[$\w]+\([$\w]+\)/, | ||
| /,activeOverlays:new Set,fastMode:!1\}/, | ||
| ]; | ||
|
|
||
| let result = oldFile; | ||
| let patched = false; | ||
|
|
||
| for (const pattern of patterns) { | ||
| const match = result.match(pattern); | ||
| if (!match || match.index === undefined) continue; | ||
|
|
||
| const prePatch = result; | ||
| const replacement = INJECTION + match[0]; | ||
| result = | ||
| prePatch.slice(0, match.index) + | ||
| replacement + | ||
| prePatch.slice(match.index + match[0].length); | ||
|
|
||
| showDiff( | ||
| prePatch, | ||
| result, | ||
| INJECTION, | ||
| match.index, | ||
| match.index + match[0].length | ||
| ); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| patched = true; | ||
| } | ||
|
|
||
| if (!patched) { | ||
| debug('patch: sessionColor: failed to find app state init patterns'); | ||
| return null; | ||
| } | ||
|
|
||
| const saveColorResult = patchSaveAgentColor(result); | ||
| if (!saveColorResult) { | ||
| debug('patch: sessionColor: failed to patch saveAgentColor'); | ||
| return null; | ||
| } | ||
|
|
||
| return saveColorResult; | ||
| }; | ||
|
|
||
| export const patchSaveAgentColor = (oldFile: string): string | null => { | ||
| const pattern = new RegExp( | ||
| '([,;{}])' + | ||
| '(async function ([$\\w]+)' + | ||
| '\\(([$\\w]+),([$\\w]+),([$\\w]+)\\)' + | ||
| '\\{let [$\\w]+=\\6\\?\\?[$\\w]+\\(\\4\\);' + | ||
| 'if\\([$\\w]+\\([$\\w]+,' + | ||
| '\\{type:"agent-color",agentColor:\\5,sessionId:\\4\\}\\),' + | ||
| '\\4===([$\\w]+)\\(\\)\\))' | ||
| ); | ||
| const match = oldFile.match(pattern); | ||
| if (!match || match.index === undefined) { | ||
| return null; | ||
| } | ||
|
|
||
| const delimiter = match[1]; | ||
| const funcBody = match[2]; | ||
| const funcName = match[3]; | ||
| const getSessionIdName = match[7]; | ||
|
|
||
| const injection = | ||
| `globalThis.__tweakccSaveAgentColor=` + | ||
| `(c)=>${funcName}(${getSessionIdName}(),c);`; | ||
|
|
||
| const replacement = `${delimiter}${injection}${funcBody}`; | ||
|
|
||
| const result = | ||
| oldFile.slice(0, match.index) + | ||
| replacement + | ||
| oldFile.slice(match.index + match[0].length); | ||
|
|
||
| showDiff( | ||
| oldFile, | ||
| result, | ||
| injection, | ||
| match.index, | ||
| match.index + match[0].length | ||
| ); | ||
|
|
||
| return result; | ||
| }; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.