diff --git a/index.js b/index.js index ecdc348..527b61a 100644 --- a/index.js +++ b/index.js @@ -66,7 +66,7 @@ export class MyDurableObject { this.error = null; this.controller = null; this.oaStream = null; - this.pending = { text: '', images: [] }; + this.pending = ''; this.flushTimer = null; this.lastSavedAt = 0; this.lastFlushedAt = 0; @@ -85,13 +85,7 @@ export class MyDurableObject { getConversationText() { const prompt = (this.messages || []).map(m => `## ${m.role}\n\n${this.extractTextFromMessage(m)}`).join('\n\n---\n\n'); - const response = this.buffer.map(it => { - let content = it.text || ''; - if (it.images && it.images.length > 0) { - content += '\n' + it.images.map(img => `![generated image](${String(img?.image_url?.url || '').substring(0, 60)}...)`).join('\n'); - } - return content; - }).join(''); + const response = this.buffer.map(it => it.text).join(''); if (!prompt && !response) return ''; return `${prompt}\n\n---\n\n## assistant\n\n${response}`; } @@ -130,7 +124,7 @@ export class MyDurableObject { this.phase = snap.phase || 'done'; this.error = snap.error || null; this.messages = Array.isArray(snap.messages) ? snap.messages : []; - this.pending = { text: '', images: [] }; + this.pending = ''; if (this.phase === 'running') { this.phase = 'evicted'; @@ -148,35 +142,27 @@ export class MyDurableObject { } replay(ws, after) { - this.buffer.forEach(it => { if (it.seq > after) this.send(ws, { type: 'delta', seq: it.seq, text: it.text, images: it.images }); }); + this.buffer.forEach(it => { if (it.seq > after) this.send(ws, { type: 'delta', seq: it.seq, text: it.text }); }); if (this.phase === 'done') this.send(ws, { type: 'done' }); else if (['error', 'evicted'].includes(this.phase)) this.send(ws, { type: 'err', message: this.error || 'The run was terminated unexpectedly.' }); } flush(force = false) { if (this.flushTimer) { clearTimeout(this.flushTimer); this.flushTimer = null; } - if (this.pending.text || (this.pending.images && this.pending.images.length > 0)) { - this.buffer.push({ seq: ++this.seq, ...this.pending }); - this.bcast({ type: 'delta', seq: this.seq, ...this.pending }); - this.pending = { text: '', images: [] }; + if (this.pending) { + this.buffer.push({ seq: ++this.seq, text: this.pending }); + this.bcast({ type: 'delta', seq: this.seq, text: this.pending }); + this.pending = ''; this.lastFlushedAt = Date.now(); } if (force) this.saveSnapshot(); } - queueUpdate({ text, images }) { - const hasImages = images && images.length > 0; - if (text) this.pending.text += text; - if (hasImages) { - if (!this.pending.images) this.pending.images = []; - this.pending.images.push(...images); - } - - if (hasImages || (this.pending.text && this.pending.text.length >= BATCH_BYTES)) { - this.flush(false); - } else if (!this.flushTimer && this.pending.text) { - this.flushTimer = setTimeout(() => this.flush(false), BATCH_MS); - } + queueDelta(text) { + if (!text) return; + this.pending += text; + if (this.pending.length >= BATCH_BYTES) this.flush(false); + else if (!this.flushTimer) this.flushTimer = setTimeout(() => this.flush(false), BATCH_MS); } async fetch(req) { @@ -193,7 +179,7 @@ export class MyDurableObject { if (req.method === 'GET') { await this.autopsy(); - const text = this.buffer.map(it => it.text).join('') + (this.pending.text || ''); + const text = this.buffer.map(it => it.text).join('') + this.pending; const isTerminal = ['done', 'error', 'evicted'].includes(this.phase); const isError = ['error', 'evicted'].includes(this.phase); const payload = { rid: this.rid, seq: this.seq, phase: this.phase, done: isTerminal, error: isError ? (this.error || 'The run was terminated unexpectedly.') : null, text }; @@ -256,7 +242,7 @@ export class MyDurableObject { try { for await (const event of this.oaStream) { if (this.phase !== 'running') break; - if (event.type.endsWith('.delta') && event.delta) this.queueUpdate({ text: event.delta }); + if (event.type.endsWith('.delta') && event.delta) this.queueDelta(event.delta); } } finally { try { this.oaStream?.controller?.abort(); } catch {} @@ -290,7 +276,7 @@ export class MyDurableObject { if (body.reasoning?.enabled) payload.extended_thinking = { enabled: true, ...(body.reasoning.budget && { max_thinking_tokens: body.reasoning.budget }) }; const stream = client.messages.stream(payload); - stream.on('text', text => { if (this.phase === 'running') this.queueUpdate({ text }); }); + stream.on('text', text => { if (this.phase === 'running') this.queueDelta(text); }); await stream.finalMessage(); } @@ -325,12 +311,12 @@ export class MyDurableObject { try { JSON.parse(line.substring(6))?.candidates?.[0]?.content?.parts?.forEach(p => { if (p.thought?.thought) { - this.queueUpdate({ text: p.thought.thought }); + this.queueDelta(p.thought.thought); hasReasoning = true; } if (p.text) { - if (hasReasoning && !hasContent) this.queueUpdate({ text: '\n' }); - this.queueUpdate({ text: p.text }); + if (hasReasoning && !hasContent) this.queueDelta('\n'); + this.queueDelta(p.text); hasContent = true; } }); @@ -347,26 +333,13 @@ export class MyDurableObject { for await (const chunk of stream) { if (this.phase !== 'running') break; const delta = chunk?.choices?.[0]?.delta; - if (!delta) continue; - - if (delta.reasoning && body.reasoning?.exclude !== true) { - this.queueUpdate({ text: delta.reasoning }); + if (delta?.reasoning && body.reasoning?.exclude !== true) { + this.queueDelta(delta.reasoning); hasReasoning = true; } - - const hasNewContent = delta.content || (delta.images && delta.images.length > 0); - if (hasNewContent) { - const update = {}; - if (hasReasoning && !hasContent) { - update.text = '\n'; - } - if (delta.content) { - update.text = (update.text || '') + delta.content; - } - if (delta.images) { - update.images = delta.images; - } - this.queueUpdate(update); + if (delta?.content) { + if (hasReasoning && !hasContent) this.queueDelta('\n'); + this.queueDelta(delta.content); hasContent = true; } } @@ -434,7 +407,7 @@ export class MyDurableObject { mapContentPartToResponses(part) { const type = part?.type || 'text'; if (['image_url', 'input_image'].includes(type)) return (part?.image_url?.url || part?.image_url) ? { type: 'input_image', image_url: String(part?.image_url?.url || part?.image_url) } : null; - if (['text', 'input_text'].includes(type)) return { type: 'input_text', text: String(type === 'text' ? (p.text ?? p.content ?? '') : (part.text ?? '')) }; + if (['text', 'input_text'].includes(type)) return { type: 'input_text', text: String(type === 'text' ? (part.text ?? part.content ?? '') : (part.text ?? '')) }; return { type: 'input_text', text: `[${type}:${part?.file?.filename || 'file'}]` }; } @@ -468,3 +441,4 @@ export class MyDurableObject { return contents; } } +