-
Notifications
You must be signed in to change notification settings - Fork 12.2k
Webui: Enable communication with parent html (if webui is in iframe): #11940
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
Changes from all commits
3645e1b
42eb6c4
cd32872
1920cc9
e7ec817
4bbd48d
8ddb353
96e2d46
ff378aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { MessageExtraContext } from './types'; | ||
|
||
// Extra context when using llama.cpp WebUI from llama-vscode, inside an iframe | ||
// Ref: https://github.com/ggml-org/llama.cpp/pull/11940 | ||
|
||
interface SetTextEvData { | ||
text: string; | ||
context: string; | ||
} | ||
|
||
/** | ||
* To test it: | ||
* window.postMessage({ command: 'setText', text: 'Spot the syntax error', context: 'def test()\n return 123' }, '*'); | ||
*/ | ||
|
||
export const useVSCodeContext = ( | ||
inputRef: React.RefObject<HTMLTextAreaElement>, | ||
setInputMsg: (text: string) => void | ||
) => { | ||
const [extraContext, setExtraContext] = useState<MessageExtraContext | null>( | ||
null | ||
); | ||
|
||
// Accept setText message from a parent window and set inputMsg and extraContext | ||
useEffect(() => { | ||
const handleMessage = (event: MessageEvent) => { | ||
if (event.data?.command === 'setText') { | ||
const data: SetTextEvData = event.data; | ||
setInputMsg(data?.text); | ||
if (data?.context && data.context.length > 0) { | ||
setExtraContext({ | ||
type: 'context', | ||
content: data.context, | ||
}); | ||
} | ||
inputRef.current?.focus(); | ||
} | ||
}; | ||
|
||
window.addEventListener('message', handleMessage); | ||
return () => window.removeEventListener('message', handleMessage); | ||
}, []); | ||
Check warning on line 43 in examples/server/webui/src/utils/llama-vscode.ts
|
||
|
||
// Add a keydown listener that sends the "escapePressed" message to the parent window | ||
useEffect(() => { | ||
const handleKeyDown = (event: KeyboardEvent) => { | ||
if (event.key === 'Escape') { | ||
window.parent.postMessage({ command: 'escapePressed' }, '*'); | ||
} | ||
}; | ||
|
||
window.addEventListener('keydown', handleKeyDown); | ||
return () => window.removeEventListener('keydown', handleKeyDown); | ||
}, []); | ||
|
||
return { | ||
extraContext, | ||
// call once the user message is sent, to clear the extra context | ||
clearExtraContext: () => setExtraContext(null), | ||
}; | ||
}; |
Uh oh!
There was an error while loading. Please reload this page.