mirror of
https://github.com/sune-org/ORP.git
synced 2026-01-14 00:27:59 +00:00
Feat: Support image output in proxy streaming
This commit is contained in:
35
index.js
35
index.js
@@ -68,6 +68,7 @@ export class MyDurableObject {
|
|||||||
this.controller = null;
|
this.controller = null;
|
||||||
this.oaStream = null;
|
this.oaStream = null;
|
||||||
this.pending = '';
|
this.pending = '';
|
||||||
|
this.pendingImages = [];
|
||||||
this.flushTimer = null;
|
this.flushTimer = null;
|
||||||
this.lastSavedAt = 0;
|
this.lastSavedAt = 0;
|
||||||
this.lastFlushedAt = 0;
|
this.lastFlushedAt = 0;
|
||||||
@@ -118,6 +119,7 @@ export class MyDurableObject {
|
|||||||
this.error = snap.error || null;
|
this.error = snap.error || null;
|
||||||
this.messages = Array.isArray(snap.messages) ? snap.messages : [];
|
this.messages = Array.isArray(snap.messages) ? snap.messages : [];
|
||||||
this.pending = '';
|
this.pending = '';
|
||||||
|
this.pendingImages = [];
|
||||||
|
|
||||||
if (this.phase === 'running') {
|
if (this.phase === 'running') {
|
||||||
this.phase = 'evicted';
|
this.phase = 'evicted';
|
||||||
@@ -135,26 +137,33 @@ export class MyDurableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
replay(ws, after) {
|
replay(ws, after) {
|
||||||
this.buffer.forEach(it => { if (it.seq > after) this.send(ws, { type: 'delta', seq: it.seq, text: it.text }); });
|
this.buffer.forEach(it => { if (it.seq > after) this.send(ws, { type: 'delta', seq: it.seq, text: it.text, images: it.images }); });
|
||||||
if (this.phase === 'done') this.send(ws, { type: 'done' });
|
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.' });
|
else if (['error', 'evicted'].includes(this.phase)) this.send(ws, { type: 'err', message: this.error || 'The run was terminated unexpectedly.' });
|
||||||
}
|
}
|
||||||
|
|
||||||
flush(force = false) {
|
flush(force = false) {
|
||||||
if (this.flushTimer) { clearTimeout(this.flushTimer); this.flushTimer = null; }
|
if (this.flushTimer) { clearTimeout(this.flushTimer); this.flushTimer = null; }
|
||||||
if (this.pending) {
|
if (this.pending || this.pendingImages.length > 0) {
|
||||||
this.buffer.push({ seq: ++this.seq, text: this.pending });
|
const item = { seq: ++this.seq, text: this.pending };
|
||||||
this.bcast({ type: 'delta', seq: this.seq, text: this.pending });
|
if (this.pendingImages.length > 0) item.images = [...this.pendingImages];
|
||||||
|
|
||||||
|
this.buffer.push(item);
|
||||||
|
this.bcast({ type: 'delta', seq: this.seq, text: this.pending, images: item.images });
|
||||||
|
|
||||||
this.pending = '';
|
this.pending = '';
|
||||||
|
this.pendingImages = [];
|
||||||
this.lastFlushedAt = Date.now();
|
this.lastFlushedAt = Date.now();
|
||||||
}
|
}
|
||||||
if (force) this.saveSnapshot();
|
if (force) this.saveSnapshot();
|
||||||
}
|
}
|
||||||
|
|
||||||
queueDelta(text) {
|
queueDelta(text, images) {
|
||||||
if (!text) return;
|
if (!text && (!images || !images.length)) return;
|
||||||
this.pending += text;
|
if (text) this.pending += text;
|
||||||
if (this.pending.length >= BATCH_BYTES) this.flush(false);
|
if (images) this.pendingImages.push(...images);
|
||||||
|
|
||||||
|
if (this.pending.length >= BATCH_BYTES || this.pendingImages.length > 0) this.flush(false);
|
||||||
else if (!this.flushTimer) this.flushTimer = setTimeout(() => this.flush(false), BATCH_MS);
|
else if (!this.flushTimer) this.flushTimer = setTimeout(() => this.flush(false), BATCH_MS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,9 +182,10 @@ export class MyDurableObject {
|
|||||||
if (req.method === 'GET') {
|
if (req.method === 'GET') {
|
||||||
await this.autopsy();
|
await this.autopsy();
|
||||||
const text = this.buffer.map(it => it.text).join('') + this.pending;
|
const text = this.buffer.map(it => it.text).join('') + this.pending;
|
||||||
|
const images = [...this.buffer.flatMap(it => it.images || []), ...this.pendingImages];
|
||||||
const isTerminal = ['done', 'error', 'evicted'].includes(this.phase);
|
const isTerminal = ['done', 'error', 'evicted'].includes(this.phase);
|
||||||
const isError = ['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 };
|
const payload = { rid: this.rid, seq: this.seq, phase: this.phase, done: isTerminal, error: isError ? (this.error || 'The run was terminated unexpectedly.') : null, text, images };
|
||||||
return this.corsJSON(payload);
|
return this.corsJSON(payload);
|
||||||
}
|
}
|
||||||
return this.corsJSON({ error: 'not allowed' }, 405);
|
return this.corsJSON({ error: 'not allowed' }, 405);
|
||||||
@@ -326,6 +336,8 @@ export class MyDurableObject {
|
|||||||
for await (const chunk of stream) {
|
for await (const chunk of stream) {
|
||||||
if (this.phase !== 'running') break;
|
if (this.phase !== 'running') break;
|
||||||
const delta = chunk?.choices?.[0]?.delta;
|
const delta = chunk?.choices?.[0]?.delta;
|
||||||
|
const images = delta?.images;
|
||||||
|
|
||||||
if (delta?.reasoning && body.reasoning?.exclude !== true) {
|
if (delta?.reasoning && body.reasoning?.exclude !== true) {
|
||||||
this.queueDelta(delta.reasoning);
|
this.queueDelta(delta.reasoning);
|
||||||
hasReasoning = true;
|
hasReasoning = true;
|
||||||
@@ -335,6 +347,9 @@ export class MyDurableObject {
|
|||||||
this.queueDelta(delta.content);
|
this.queueDelta(delta.content);
|
||||||
hasContent = true;
|
hasContent = true;
|
||||||
}
|
}
|
||||||
|
if (images) {
|
||||||
|
this.queueDelta('', images);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -434,5 +449,3 @@ export class MyDurableObject {
|
|||||||
return contents;
|
return contents;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user