feat(mobile): add Android development client and scoped IDE companion
This commit is contained in:
1 parent
3332458708
commit
9f9f3226a1
84 files changed
+4371
-4
No files matched your search
@@ -0,0 +1,101 @@
|
||||
import { AccountClient, openPhoneConnection } from './transport.mjs'
|
||||
import { VoiceSession } from './voice.mjs'
|
||||
import { taskState } from './behavior.mjs'
|
||||
const $ = id => document.getElementById(id)
|
||||
if (globalThis.Capacitor?.isNativePlatform()) globalThis.Capacitor.registerPlugin('MobileSecrets')
|
||||
let credentials = {}, account, connection, selected, approval, voice, pendingTask, usageTimer, voiceGeneration = 0
|
||||
const status = text => { $('status').textContent = text }
|
||||
const native = () => window.Capacitor?.Plugins?.MobileSecrets
|
||||
async function saveCredentials() { if (native()) await native().save({ value: JSON.stringify(credentials) }) }
|
||||
function addText(container, role, text) { const p = document.createElement('p'); p.textContent = `${role}: ${text}`; $(container).append(p); while ($(container).children.length > 150) $(container).firstChild.remove() }
|
||||
function action(id, work) { $(id).onclick = async () => { $(id).disabled = true; try { await work() } catch (error) { status(error.message) } finally { $(id).disabled = false } } }
|
||||
function rpc(action, params) { if (!connection) throw new Error('Önce bilgisayara bağlanın.'); return connection.requests.call(action, params) }
|
||||
function requireSession() { if (!selected) throw new Error('Önce IDE konuşmasını seçin.'); return selected }
|
||||
function setApproval(value) { approval = value; $('approval').hidden = !value; $('approval-detail').textContent = value ? `${value.command}\n\n${value.reason}` : ''; $('allow').disabled = !value?.choices.includes('once'); $('deny').disabled = !value?.choices.includes('deny') }
|
||||
function event(frame) {
|
||||
if (frame.event === 'disconnected') { stopVoice(); status('IDE bağlantısı kesildi. Yeniden bağlanın.'); setApproval(null) }
|
||||
const next = taskState(frame); if (next) $('work-status').textContent = next
|
||||
if (frame.event === 'approval.request') setApproval(frame.approval)
|
||||
if (frame.event === 'message.complete') { setApproval(null); addText('result', 'IDE sonucu', frame.text || 'Tamamlanma olayı alındı; geçmişi yenileyin.') }
|
||||
}
|
||||
action('pair', async () => {
|
||||
credentials = { accountToken: $('account-token').value.trim() }; $('account-token').value = ''
|
||||
account = new AccountClient(credentials)
|
||||
const result = await account.call('/v1/aiturk/pairings', { display_name: $('device-name').value.trim(), platform: 'android' })
|
||||
Object.assign(credentials, { deviceId: result.device_id, deviceToken: result.device_token, pairingId: result.pairing_id })
|
||||
await saveCredentials()
|
||||
$('pair-code').textContent = `Eşleştirme kimliği:\n${result.pairing_id}\n\nKod: ${result.user_code}\nSon geçerlilik: ${result.expires_at}`
|
||||
status('Bilgisayardaki açık onay bekleniyor.')
|
||||
})
|
||||
action('devices', async () => {
|
||||
if (!account) throw new Error('Önce hesabınızı eşleştirin.')
|
||||
const data = await account.call('/v1/aiturk/mobile/devices'); $('desktop').replaceChildren()
|
||||
for (const device of data.devices.filter(d => d.platform.startsWith('windows'))) { const option = document.createElement('option'); option.value = device.id; option.textContent = device.name; $('desktop').append(option) }
|
||||
status('Hesabınızdaki onaylı bilgisayarlar bulundu.')
|
||||
})
|
||||
action('connect', async () => {
|
||||
if (!account || !$('desktop').value) throw new Error('Önce bir bilgisayar seçin.')
|
||||
stopVoice(); connection?.close(); selected = null; setApproval(null)
|
||||
status('Bilgisayara bağlanılıyor…')
|
||||
connection = await openPhoneConnection(account, $('desktop').value, event)
|
||||
$('work').hidden = false; status('Bilgisayar bağlı. IDE konuşmasını seçin.')
|
||||
})
|
||||
action('sessions', async () => {
|
||||
const data = await rpc('sessions.list'); $('session').replaceChildren()
|
||||
for (const item of data.sessions) { const option = document.createElement('option'); option.value = item.id; option.textContent = `${item.title} · ${item.model || 'Model bilinmiyor'}`; $('session').append(option) }
|
||||
})
|
||||
action('select-session', async () => {
|
||||
const id = $('session').value; if (!id) throw new Error('Bir konuşma seçin.')
|
||||
const result = await rpc('session.open', { sessionId: id })
|
||||
selected = { id, model: result.model }
|
||||
pendingTask = credentials.outbox?.sessionId === id ? credentials.outbox : null
|
||||
$('retry').hidden = !pendingTask
|
||||
if (pendingTask) { $('task').value = pendingTask.text; $('intent').value = pendingTask.intent; $('preview').textContent = pendingTask.text }
|
||||
$('target').textContent = `Bilgisayar: ${$('desktop').selectedOptions[0]?.textContent} · Konuşma: ${$('session').selectedOptions[0]?.textContent} · İş modeli: ${result.model || 'Bilinmiyor'}`
|
||||
$('result').replaceChildren(); for (const message of result.messages) addText('result', message.role, message.text)
|
||||
setApproval(result.approval); $('work-status').textContent = result.running ? 'IDE çalışıyor.' : 'IDE konuşması açıldı.'
|
||||
})
|
||||
for (const [id, choice] of [['allow', 'once'], ['deny', 'deny']]) action(id, async () => { const target = requireSession(); await rpc('approval.respond', { sessionId: target.id, requestId: approval?.requestId, choice }); setApproval(null); status('Yanıt gerçek IDE onayına iletildi.') })
|
||||
action('save-google', async () => { const key = $('google-key').value.trim(); $('google-key').value = ''; await rpc('google.key.save', { key }); status('Google anahtarı bilgisayarın güvenli kasasına kaydedildi.') })
|
||||
action('delete-google', async () => { stopVoice(); await rpc('google.key.delete'); status('Google anahtarı bilgisayar kasasından silindi.') })
|
||||
function stopVoice() { voiceGeneration++; voice?.stop(); voice = null; clearInterval(usageTimer); $('voice-status').textContent = 'Mikrofon kapalı. IDE işi devam edebilir.' }
|
||||
async function startVoice(microphone) {
|
||||
if (!$('consent').checked) throw new Error('Önce Google veri ve ücret bilgisini okuyup kabul edin.')
|
||||
stopVoice(); const generation = voiceGeneration; const token = await rpc('voice.token')
|
||||
if (generation !== voiceGeneration || document.hidden) return
|
||||
const started = Date.now()
|
||||
voice = new VoiceSession({ ...token,
|
||||
onText: (role, text) => addText('discussion', role, text),
|
||||
onDraft: (text, intent) => { $('task').value = text; $('intent').value = intent; updatePreview(); status('Asistan görev taslağını hazırladı. Henüz IDE’ye gönderilmedi.') },
|
||||
onError: status, onState: text => { $('voice-status').textContent = text; if (voice?.stopped) clearInterval(usageTimer) } })
|
||||
usageTimer = setInterval(() => { $('usage').textContent = `Bu uygulamanın ölçtüğü görüşme süresi: ${Math.floor((Date.now() - started) / 1000)} sn. Google kotası/faturası değildir.` }, 1000)
|
||||
await voice.start(microphone)
|
||||
}
|
||||
action('voice-start', () => startVoice(true)); action('voice-text', () => startVoice(false))
|
||||
action('voice-stop', async () => stopVoice()); action('silence', async () => voice?.interruptPlayback())
|
||||
action('discuss', async () => { if (!voice?.ready) throw new Error('Önce asistanla yazış veya sesle konuş bağlantısını açın.'); const text = $('idea').value.trim(); if (!text) return; voice.text(text); addText('discussion', 'Siz', text); $('idea').value = '' })
|
||||
function visibleTask() { const text = $('task').value.trim(); return text ? ($('intent').value === 'research' ? 'Araştırma / inceleme isteği. Değişiklik yapma.\n\n' : '') + text : '' }
|
||||
function updatePreview() { $('preview').textContent = visibleTask(); pendingTask = null; $('retry').hidden = true }
|
||||
$('task').oninput = updatePreview; $('intent').onchange = updatePreview
|
||||
async function deliver() {
|
||||
if (!pendingTask) throw new Error('Kontrol edilecek gönderim yok.')
|
||||
credentials.outbox = pendingTask; await saveCredentials()
|
||||
const result = await rpc('task.submit', pendingTask)
|
||||
$('work-status').textContent = result.state === 'accepted' ? 'IDE görevi kabul etti. Tamamlanma sonucu bekleniyor.' : 'Gönderimin sonucu belirsiz. IDE geçmişini kontrol edin; yeniden çalıştırma yapılmadı.'
|
||||
$('retry').hidden = false
|
||||
}
|
||||
action('submit', async () => {
|
||||
const target = requireSession(), text = visibleTask(); if (!text) throw new Error('Görev metni boş olamaz.')
|
||||
if (!pendingTask) pendingTask = { taskId: crypto.randomUUID(), sessionId: target.id, model: target.model, intent: $('intent').value, text }
|
||||
$('retry').hidden = false; await deliver()
|
||||
})
|
||||
action('retry', deliver)
|
||||
action('cancel-draft', async () => { $('task').value = ''; updatePreview(); status('Gönderilmemiş görev taslağı iptal edildi. Başlamış IDE işi devam eder.') })
|
||||
action('interrupt', async () => { const target = requireSession(); await rpc('message.stop', { sessionId: target.id }); $('work-status').textContent = 'IDE’ye durdurma isteği iletildi; gerçek sonucu bekleyin.' })
|
||||
action('history', async () => { const result = await rpc('session.history', { sessionId: requireSession().id }); $('result').replaceChildren(); for (const message of result.messages) addText('result', message.role, message.text) })
|
||||
action('clear-history', async () => { $('discussion').replaceChildren(); $('result').replaceChildren(); status('Telefondaki görünür metin temizlendi. Google bağlantı bağlamını da temizlemek için görüşmeyi kapatın.') })
|
||||
action('disconnect', async () => { stopVoice(); connection?.close(); connection = null; selected = null; $('work').hidden = true; status('Telefon bağlantısı kapalı. IDE işi devam edebilir.') })
|
||||
action('revoke', async () => { const data = await account.call('/v1/aiturk/mobile/devices'); const self = data.devices.find(d => d.self); await account.call(`/v1/aiturk/devices/${self.id}/revoke`, { expected_version: self.version }); stopVoice(); connection?.close(); connection = null; credentials = {}; await saveCredentials(); account = null; $('work').hidden = true; status('Bu telefonun erişimi iptal edildi.') })
|
||||
document.addEventListener('visibilitychange', () => { if (document.hidden) stopVoice() })
|
||||
window.addEventListener('pagehide', () => { stopVoice(); connection?.close() })
|
||||
try { const saved = await native()?.load(); if (saved?.value) { credentials = JSON.parse(saved.value); if (credentials.accountToken) { account = new AccountClient(credentials); status('Kayıtlı cihaz bulundu. Bilgisayarları yenileyerek bağlanın.') } } } catch { status('Android güvenli kasası açılamadı. Hesabınızı yeniden eşleştirin.') }
|
||||
Reference in new issue
Block a user