mac: add webview debug logging
This commit is contained in:
@@ -894,13 +894,22 @@ private struct SessionDefaults {
|
|||||||
let contextTokens: Int
|
let contextTokens: Int
|
||||||
}
|
}
|
||||||
|
|
||||||
private struct ModelChoice: Identifiable, Hashable {
|
struct ModelChoice: Identifiable, Hashable {
|
||||||
let id: String
|
let id: String
|
||||||
let name: String
|
let name: String
|
||||||
let provider: String
|
let provider: String
|
||||||
let contextWindow: Int?
|
let contextWindow: Int?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension Optional where Wrapped == String {
|
||||||
|
var isNilOrEmpty: Bool {
|
||||||
|
switch self {
|
||||||
|
case .none: true
|
||||||
|
case let .some(value): value.isEmpty
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
extension [String] {
|
extension [String] {
|
||||||
fileprivate func dedupedPreserveOrder() -> [String] {
|
fileprivate func dedupedPreserveOrder() -> [String] {
|
||||||
var seen = Set<String>()
|
var seen = Set<String>()
|
||||||
@@ -1511,7 +1520,11 @@ struct ConfigSettings: View {
|
|||||||
private func chooseCatalogFile() {
|
private func chooseCatalogFile() {
|
||||||
let panel = NSOpenPanel()
|
let panel = NSOpenPanel()
|
||||||
panel.title = "Select models.generated.ts"
|
panel.title = "Select models.generated.ts"
|
||||||
panel.allowedFileTypes = ["ts"]
|
if let tsType = UTType(filenameExtension: "ts") {
|
||||||
|
panel.allowedContentTypes = [tsType]
|
||||||
|
} else {
|
||||||
|
panel.allowedFileTypes = ["ts"] // fallback
|
||||||
|
}
|
||||||
panel.allowsMultipleSelection = false
|
panel.allowsMultipleSelection = false
|
||||||
panel.directoryURL = URL(fileURLWithPath: self.modelCatalogPath).deletingLastPathComponent()
|
panel.directoryURL = URL(fileURLWithPath: self.modelCatalogPath).deletingLastPathComponent()
|
||||||
if panel.runModal() == .OK, let url = panel.url {
|
if panel.runModal() == .OK, let url = panel.url {
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ final class WebChatWindowController: NSWindowController, WKScriptMessageHandler
|
|||||||
let contentController = WKUserContentController()
|
let contentController = WKUserContentController()
|
||||||
config.userContentController = contentController
|
config.userContentController = contentController
|
||||||
config.preferences.isElementFullscreenEnabled = true
|
config.preferences.isElementFullscreenEnabled = true
|
||||||
|
config.preferences.setValue(true, forKey: "developerExtrasEnabled")
|
||||||
|
|
||||||
// Inject callback receiver stub
|
// Inject callback receiver stub
|
||||||
let callbackScript = """
|
let callbackScript = """
|
||||||
@@ -34,6 +35,11 @@ final class WebChatWindowController: NSWindowController, WKScriptMessageHandler
|
|||||||
window.webkit?.messageHandlers?.clawdis?.postMessage({ id, ...payload });
|
window.webkit?.messageHandlers?.clawdis?.postMessage({ id, ...payload });
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
window.__clawdisLog = function(msg) {
|
||||||
|
try {
|
||||||
|
window.webkit?.messageHandlers?.clawdis?.postMessage({ id: 'log', log: String(msg) });
|
||||||
|
} catch (_) {}
|
||||||
|
};
|
||||||
"""
|
"""
|
||||||
let userScript = WKUserScript(source: callbackScript, injectionTime: .atDocumentStart, forMainFrameOnly: true)
|
let userScript = WKUserScript(source: callbackScript, injectionTime: .atDocumentStart, forMainFrameOnly: true)
|
||||||
contentController.addUserScript(userScript)
|
contentController.addUserScript(userScript)
|
||||||
@@ -108,64 +114,73 @@ final class WebChatWindowController: NSWindowController, WKScriptMessageHandler
|
|||||||
<body>
|
<body>
|
||||||
<div id="app"></div>
|
<div id="app"></div>
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import { Agent, ChatPanel, AppStorage, setAppStorage } from '@mariozechner/pi-web-ui';
|
try {
|
||||||
import { getModel } from '@mariozechner/pi-ai';
|
const { Agent, ChatPanel, AppStorage, setAppStorage } = await import('@mariozechner/pi-web-ui');
|
||||||
|
const { getModel } = await import('@mariozechner/pi-ai');
|
||||||
|
|
||||||
class NativeTransport {
|
class NativeTransport {
|
||||||
async *run(messages, userMessage, cfg, signal) {
|
async *run(messages, userMessage, cfg, signal) {
|
||||||
const result = await window.__clawdisSend({ type: 'chat', payload: { text: userMessage.content?.[0]?.text ?? '', sessionKey: '\(
|
const result = await window.__clawdisSend({ type: 'chat', payload: { text: userMessage.content?.[0]?.text ?? '', sessionKey: '\(
|
||||||
sessionKey)' } });
|
sessionKey)' } });
|
||||||
const usage = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } };
|
const usage = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } };
|
||||||
const assistant = {
|
const assistant = {
|
||||||
role: 'assistant',
|
role: 'assistant',
|
||||||
content: [{ type: 'text', text: result.text ?? '' }],
|
content: [{ type: 'text', text: result.text ?? '' }],
|
||||||
api: cfg.model.api,
|
api: cfg.model.api,
|
||||||
provider: cfg.model.provider,
|
provider: cfg.model.provider,
|
||||||
model: cfg.model.id,
|
model: cfg.model.id,
|
||||||
usage,
|
usage,
|
||||||
stopReason: 'stop',
|
stopReason: 'stop',
|
||||||
|
timestamp: Date.now()
|
||||||
|
};
|
||||||
|
yield { type: 'turn_start' };
|
||||||
|
yield { type: 'message_start', message: assistant };
|
||||||
|
yield { type: 'message_end', message: assistant };
|
||||||
|
yield { type: 'turn_end' };
|
||||||
|
yield { type: 'agent_end' };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Minimal storage
|
||||||
|
const storage = new AppStorage();
|
||||||
|
setAppStorage(storage);
|
||||||
|
|
||||||
|
const agent = new Agent({
|
||||||
|
initialState: {
|
||||||
|
systemPrompt: 'You are Clawd (primary session).',
|
||||||
|
model: getModel('anthropic', 'claude-opus-4-5'),
|
||||||
|
thinkingLevel: 'off',
|
||||||
|
messages: []
|
||||||
|
},
|
||||||
|
transport: new NativeTransport()
|
||||||
|
});
|
||||||
|
|
||||||
|
// Patch prompt to append user message into history first
|
||||||
|
const origPrompt = agent.prompt.bind(agent);
|
||||||
|
agent.prompt = async (input, attachments) => {
|
||||||
|
const userMessage = {
|
||||||
|
role: 'user',
|
||||||
|
content: [{ type: 'text', text: input }],
|
||||||
|
attachments: attachments?.length ? attachments : undefined,
|
||||||
timestamp: Date.now()
|
timestamp: Date.now()
|
||||||
};
|
};
|
||||||
yield { type: 'turn_start' };
|
agent.appendMessage(userMessage);
|
||||||
yield { type: 'message_start', message: assistant };
|
return origPrompt(input, attachments);
|
||||||
yield { type: 'message_end', message: assistant };
|
|
||||||
yield { type: 'turn_end' };
|
|
||||||
yield { type: 'agent_end' };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Minimal storage
|
|
||||||
const storage = new AppStorage();
|
|
||||||
setAppStorage(storage);
|
|
||||||
|
|
||||||
const agent = new Agent({
|
|
||||||
initialState: {
|
|
||||||
systemPrompt: 'You are Clawd (primary session).',
|
|
||||||
model: getModel('anthropic', 'claude-opus-4-5'),
|
|
||||||
thinkingLevel: 'off',
|
|
||||||
messages: []
|
|
||||||
},
|
|
||||||
transport: new NativeTransport()
|
|
||||||
});
|
|
||||||
|
|
||||||
// Patch prompt to append user message into history first
|
|
||||||
const origPrompt = agent.prompt.bind(agent);
|
|
||||||
agent.prompt = async (input, attachments) => {
|
|
||||||
const userMessage = {
|
|
||||||
role: 'user',
|
|
||||||
content: [{ type: 'text', text: input }],
|
|
||||||
attachments: attachments?.length ? attachments : undefined,
|
|
||||||
timestamp: Date.now()
|
|
||||||
};
|
};
|
||||||
agent.appendMessage(userMessage);
|
|
||||||
return origPrompt(input, attachments);
|
|
||||||
};
|
|
||||||
|
|
||||||
const panel = new ChatPanel();
|
const panel = new ChatPanel();
|
||||||
panel.style.height = '100%';
|
panel.style.height = '100%';
|
||||||
panel.style.display = 'block';
|
panel.style.display = 'block';
|
||||||
await panel.setAgent(agent);
|
await panel.setAgent(agent);
|
||||||
document.getElementById('app').appendChild(panel);
|
document.getElementById('app').appendChild(panel);
|
||||||
|
} catch (err) {
|
||||||
|
const msg = err?.stack || err?.message || String(err);
|
||||||
|
window.__clawdisLog(msg);
|
||||||
|
document.body.style.color = '#e06666';
|
||||||
|
document.body.style.fontFamily = 'monospace';
|
||||||
|
document.body.style.padding = '16px';
|
||||||
|
document.body.innerText = 'Web chat failed to load:\\n' + msg;
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user