feat(mobile): add Android development client and scoped IDE companion

This commit is contained in:
yilsem committed 2026-09-08 16:54:28 +03:00
1 parent 3332458708
commit 9f9f3226a1
84 files changed
+4371 -4

No files matched your search

+97
View File
@@ -0,0 +1,97 @@
import { ASSISTANT_INSTRUCTION, TASK_TOOL, taskText } from './behavior.mjs'
export function pcm16(samples, sampleRate) {
const length = Math.floor(samples.length * 16000 / sampleRate)
const bytes = new Uint8Array(length * 2), view = new DataView(bytes.buffer)
for (let i = 0; i < length; i++) {
const value = Math.max(-1, Math.min(1, samples[Math.floor(i * sampleRate / 16000)]))
view.setInt16(i * 2, Math.round(value * (value < 0 ? 32768 : 32767)), true)
}
let binary = ''; for (const byte of bytes) binary += String.fromCharCode(byte)
return btoa(binary)
}
export class VoiceSession {
constructor({ token, model, onText, onDraft, onError, onState }) {
Object.assign(this, { token, model, onText, onDraft, onError, onState })
this.sources = new Set(); this.stopped = false; this.nextPlayback = 0
}
async start(microphone = true) {
try {
this.context = new AudioContext()
await this.context.resume()
if (microphone) {
this.stream = await navigator.mediaDevices.getUserMedia({ audio: { channelCount: 1, echoCancellation: true, noiseSuppression: true } })
if (this.stopped) { this.stream.getTracks().forEach(t => t.stop()); return }
await this.context.audioWorklet.addModule('./pcm-worklet.js')
this.input = this.context.createMediaStreamSource(this.stream)
this.capture = new AudioWorkletNode(this.context, 'aiturk-pcm')
this.silence = this.context.createGain(); this.silence.gain.value = 0
this.capture.port.onmessage = event => {
if (this.ready && !this.stopped && this.socket.bufferedAmount < 128000)
this.send({ realtimeInput: { audio: { data: pcm16(event.data, this.context.sampleRate), mimeType: 'audio/pcm;rate=16000' } } })
}
this.input.connect(this.capture).connect(this.silence).connect(this.context.destination)
}
if (this.stopped) return
this.socket = new WebSocket('wss://generativelanguage.googleapis.com/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContentConstrained?access_token=' + encodeURIComponent(this.token))
this.token = null
this.socket.onopen = () => this.send({ setup: { model: `models/${this.model}`,
generationConfig: { responseModalities: ['AUDIO'] },
systemInstruction: { parts: [{ text: ASSISTANT_INSTRUCTION }] },
tools: [{ functionDeclarations: [TASK_TOOL] }], inputAudioTranscription: {}, outputAudioTranscription: {} } })
this.socket.onmessage = event => { void this.receive(event).catch(() => { this.onError('Ses yanıtı işlenemedi.'); this.stop() }) }
this.socket.onerror = () => { this.onError('Google ses bağlantısı kurulamadı. Anahtar, model erişimi ve kotayı kontrol edin.'); this.stop() }
this.socket.onclose = () => this.stop()
this.limit = setTimeout(() => { this.onText('bilgi', 'Bu görüşmenin 10 dakikalık yerel süre sınırı doldu. Bu bir fatura üst sınırı değildir.'); this.stop() }, 10 * 60000)
this.setupTimer = setTimeout(() => { if (!this.ready) { this.onError('Google ses başlangıcı zaman aşımına uğradı.'); this.stop() } }, 20000)
} catch { this.onError('Mikrofon veya ses bağlantısı açılamadı. Android mikrofon iznini kontrol edin; yazıyla devam edebilirsiniz.'); this.stop() }
}
send(frame) { if (this.socket?.readyState === 1) this.socket.send(JSON.stringify(frame)) }
text(text) {
if (!this.ready) throw new Error('Ses asistanı bağlantısı henüz hazır değil.')
this.interruptPlayback()
this.send({ clientContent: { turns: [{ role: 'user', parts: [{ text }] }], turnComplete: true } })
}
async receive(event) {
const data = JSON.parse(event.data instanceof Blob ? await event.data.text() : event.data)
if (this.stopped) return
if (data.setupComplete) { this.ready = true; clearTimeout(this.setupTimer); this.onState('Google asistanı bağlı.'); return }
if (data.error) { this.onError('Google ses isteğini reddetti. AI Studio erişim ve kotanızı kontrol edin.'); this.stop(); return }
const content = data.serverContent
if (content?.interrupted) this.interruptPlayback()
if (content?.inputTranscription?.text) this.onText('siz', content.inputTranscription.text)
if (content?.outputTranscription?.text) this.onText('asistan', content.outputTranscription.text)
for (const part of content?.modelTurn?.parts || []) {
if (part.inlineData?.mimeType?.startsWith('audio/pcm')) this.play(part.inlineData.data)
}
for (const call of data.toolCall?.functionCalls || []) {
let response = { error: 'Bu araç desteklenmiyor.' }
if (call.name === 'prepare_task') {
try { this.onDraft(taskText(call.args), call.args.intent); response = { draftPrepared: true, executed: false } }
catch { response = { error: 'Taslak eksik. Kapsam ve kısıtları tamamla.' } }
}
this.send({ toolResponse: { functionResponses: [{ id: call.id, name: call.name, response }] } })
}
}
play(encoded) {
const bytes = Uint8Array.from(atob(encoded), c => c.charCodeAt(0))
const view = new DataView(bytes.buffer)
const buffer = this.context.createBuffer(1, bytes.length / 2, 24000)
for (let i = 0; i < buffer.length; i++) buffer.getChannelData(0)[i] = view.getInt16(i * 2, true) / 32768
const source = this.context.createBufferSource(); source.buffer = buffer; source.connect(this.context.destination)
this.sources.add(source); source.onended = () => this.sources.delete(source)
const start = Math.max(this.context.currentTime, this.nextPlayback)
// Bound stale audio; interruption and network bursts cannot build an endless queue.
if (start - this.context.currentTime > 15) { this.interruptPlayback(); return }
source.start(start); this.nextPlayback = start + buffer.duration
}
interruptPlayback() { for (const source of this.sources) { try { source.stop() } catch {} }; this.sources.clear(); this.nextPlayback = 0 }
stop() {
if (this.stopped) return
this.stopped = true; this.ready = false; clearTimeout(this.limit); clearTimeout(this.setupTimer)
this.interruptPlayback(); this.stream?.getTracks().forEach(t => t.stop())
this.input?.disconnect(); this.capture?.disconnect(); this.silence?.disconnect()
this.socket?.close(); void this.context?.close(); this.onState('Ses görüşmesi kapalı. IDE işi devam edebilir.')
}
}