mirror of
https://github.com/sune-org/ORP.git
synced 2026-01-14 00:27:59 +00:00
Revert: Update index.js
This commit is contained in:
27
index.js
27
index.js
@@ -72,7 +72,6 @@ export class MyDurableObject {
|
|||||||
this.lastFlushedAt = 0;
|
this.lastFlushedAt = 0;
|
||||||
this.hbActive = false;
|
this.hbActive = false;
|
||||||
this.age = 0;
|
this.age = 0;
|
||||||
this.messages = [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
corsJSON(obj, status = 200) {
|
corsJSON(obj, status = 200) {
|
||||||
@@ -83,28 +82,12 @@ export class MyDurableObject {
|
|||||||
|
|
||||||
bcast(obj) { this.sockets.forEach(ws => this.send(ws, obj)); }
|
bcast(obj) { this.sockets.forEach(ws => this.send(ws, obj)); }
|
||||||
|
|
||||||
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 => it.text).join('');
|
|
||||||
if (!prompt && !response) return '';
|
|
||||||
return `${prompt}\n\n---\n\n## assistant\n\n${response}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
notify(msg, pri = 3, tags = []) {
|
notify(msg, pri = 3, tags = []) {
|
||||||
if (!this.env.NTFY_TOPIC) return;
|
if (!this.env.NTFY_TOPIC) return;
|
||||||
const attachment = this.getConversationText();
|
|
||||||
const headers = { Title: 'Sune ORP', Priority: `${pri}`, Tags: tags.join(',') };
|
|
||||||
let body = msg;
|
|
||||||
if (attachment && attachment.length < 1024 * 1024) {
|
|
||||||
headers.Message = msg;
|
|
||||||
headers.Filename = `${this.rid || 'conversation'}.md`;
|
|
||||||
headers['Content-Type'] = 'text/markdown';
|
|
||||||
body = attachment;
|
|
||||||
}
|
|
||||||
this.state.waitUntil(fetch(`https://ntfy.sh/${this.env.NTFY_TOPIC}`, {
|
this.state.waitUntil(fetch(`https://ntfy.sh/${this.env.NTFY_TOPIC}`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body,
|
body: msg,
|
||||||
headers,
|
headers: { 'Title': 'Sune ORP', 'Priority': `${pri}`, 'Tags': tags.join(',') },
|
||||||
}).catch(e => console.error('ntfy failed:', e)));
|
}).catch(e => console.error('ntfy failed:', e)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,7 +106,6 @@ export class MyDurableObject {
|
|||||||
this.age = snap.age || 0;
|
this.age = snap.age || 0;
|
||||||
this.phase = snap.phase || 'done';
|
this.phase = snap.phase || 'done';
|
||||||
this.error = snap.error || null;
|
this.error = snap.error || null;
|
||||||
this.messages = Array.isArray(snap.messages) ? snap.messages : [];
|
|
||||||
this.pending = '';
|
this.pending = '';
|
||||||
|
|
||||||
if (this.phase === 'running') {
|
if (this.phase === 'running') {
|
||||||
@@ -137,7 +119,7 @@ export class MyDurableObject {
|
|||||||
|
|
||||||
saveSnapshot() {
|
saveSnapshot() {
|
||||||
this.lastSavedAt = Date.now();
|
this.lastSavedAt = Date.now();
|
||||||
const snapshot = { rid: this.rid, buffer: this.buffer, seq: this.seq, age: this.age, phase: this.phase, error: this.error, savedAt: this.lastSavedAt, messages: this.messages };
|
const snapshot = { rid: this.rid, buffer: this.buffer, seq: this.seq, age: this.age, phase: this.phase, error: this.error, savedAt: this.lastSavedAt };
|
||||||
return this.state.storage.put('run', snapshot).catch(() => {});
|
return this.state.storage.put('run', snapshot).catch(() => {});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,7 +192,6 @@ export class MyDurableObject {
|
|||||||
this.rid = rid;
|
this.rid = rid;
|
||||||
this.phase = 'running';
|
this.phase = 'running';
|
||||||
this.controller = new AbortController();
|
this.controller = new AbortController();
|
||||||
this.messages = body.messages;
|
|
||||||
await this.saveSnapshot();
|
await this.saveSnapshot();
|
||||||
|
|
||||||
this.state.waitUntil(this.startHeartbeat());
|
this.state.waitUntil(this.startHeartbeat());
|
||||||
@@ -441,3 +422,5 @@ export class MyDurableObject {
|
|||||||
return contents;
|
return contents;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user