@hanzo/studio 0.1.14 → 0.1.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/chat/turn.js +5 -2
- package/lib/wire.d.ts +2 -0
- package/lib/wire.js +54 -6
- package/package.json +1 -1
package/lib/chat/turn.js
CHANGED
|
@@ -172,8 +172,11 @@ export async function turn(start, asked, ctx) {
|
|
|
172
172
|
catch (e) {
|
|
173
173
|
if (e?.name === 'AbortError')
|
|
174
174
|
set({ doing: undefined, failed: 'Stopped.' });
|
|
175
|
-
else
|
|
176
|
-
|
|
175
|
+
else {
|
|
176
|
+
// A round that fails after a tool ran (the closing line, say) leaves what it made standing.
|
|
177
|
+
const n = mine().made?.length ?? 0;
|
|
178
|
+
set({ doing: undefined, failed: n ? `${words(e)} What this turn made is in its tab${n > 1 ? 's' : ''}.` : words(e) });
|
|
179
|
+
}
|
|
177
180
|
}
|
|
178
181
|
return chat;
|
|
179
182
|
async function call(name, args) {
|
package/lib/wire.d.ts
CHANGED
|
@@ -35,6 +35,8 @@ export declare class Refusal extends Error {
|
|
|
35
35
|
readonly code: string;
|
|
36
36
|
constructor(status: number, message: string, code?: string);
|
|
37
37
|
}
|
|
38
|
+
export declare const UNREACHED = "unreached";
|
|
39
|
+
export declare function unreached(since: number, partway?: boolean): Refusal;
|
|
38
40
|
type Base = 'api' | 'engine';
|
|
39
41
|
export interface Ask {
|
|
40
42
|
method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
package/lib/wire.js
CHANGED
|
@@ -30,6 +30,34 @@ export class Refusal extends Error {
|
|
|
30
30
|
this.name = 'Refusal';
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
|
+
/*
|
|
34
|
+
* A request the connection lost. The browser says only "Failed to fetch" (or
|
|
35
|
+
* "network error" partway through a body), which tells a person nothing: the
|
|
36
|
+
* flyer that failed with it (hanzo-inc/app#302) was a chat turn the edge dropped while the
|
|
37
|
+
* model was still thinking, and the same turn went through when sent again.
|
|
38
|
+
* It is said as a Refusal with status 0 and the code `unreached`: how long it
|
|
39
|
+
* had waited, and what to do.
|
|
40
|
+
*/
|
|
41
|
+
export const UNREACHED = 'unreached';
|
|
42
|
+
export function unreached(since, partway = false) {
|
|
43
|
+
const s = Math.max(1, Math.round((Date.now() - since) / 1000));
|
|
44
|
+
return new Refusal(0, partway
|
|
45
|
+
? `The connection to Hanzo dropped ${s} s into the answer. Try again.`
|
|
46
|
+
: `Couldn't get an answer from Hanzo: the connection dropped after ${s} s. Check your connection and try again.`, UNREACHED);
|
|
47
|
+
}
|
|
48
|
+
const stopped = (e, signal) => Boolean(signal?.aborted) || e?.name === 'AbortError';
|
|
49
|
+
/** fetch, with a lost connection said as a Refusal. A stop is still the AbortError it was. */
|
|
50
|
+
async function reach(input, init = {}) {
|
|
51
|
+
const since = Date.now();
|
|
52
|
+
try {
|
|
53
|
+
return await fetch(input, init);
|
|
54
|
+
}
|
|
55
|
+
catch (e) {
|
|
56
|
+
if (stopped(e, init.signal))
|
|
57
|
+
throw e;
|
|
58
|
+
throw unreached(since);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
33
61
|
const origin = (w, base) => (base === 'api' ? w.api : w.engine);
|
|
34
62
|
function headers(w, extra) {
|
|
35
63
|
const h = new Headers(extra);
|
|
@@ -71,7 +99,7 @@ export async function ask(w, base, path, o = {}) {
|
|
|
71
99
|
}
|
|
72
100
|
else if (o.type)
|
|
73
101
|
h.set('Content-Type', o.type);
|
|
74
|
-
const res = await
|
|
102
|
+
const res = await reach(`${origin(w, base)}${path}`, {
|
|
75
103
|
method: o.method ?? (body === undefined ? 'GET' : 'POST'),
|
|
76
104
|
headers: h,
|
|
77
105
|
body,
|
|
@@ -108,14 +136,24 @@ const enc = encodeURIComponent;
|
|
|
108
136
|
* words so far as they arrive. Answers the words and the tool calls in full.
|
|
109
137
|
*/
|
|
110
138
|
export async function complete(w, body, heard, signal) {
|
|
111
|
-
const
|
|
139
|
+
const init = {
|
|
112
140
|
method: 'POST',
|
|
113
141
|
headers: headers(w, { 'Content-Type': 'application/json', Accept: 'text/event-stream' }),
|
|
114
142
|
body: JSON.stringify({ ...body, stream: true }),
|
|
115
143
|
cache: 'no-store',
|
|
116
144
|
credentials: 'omit',
|
|
117
145
|
signal,
|
|
118
|
-
}
|
|
146
|
+
};
|
|
147
|
+
let res;
|
|
148
|
+
try {
|
|
149
|
+
res = await reach(`${w.api}/v1/chat/completions`, init);
|
|
150
|
+
}
|
|
151
|
+
catch (e) {
|
|
152
|
+
if (!(e instanceof Refusal) || e.code !== UNREACHED)
|
|
153
|
+
throw e;
|
|
154
|
+
// Lost before any answer began, so nothing was said: asked once more.
|
|
155
|
+
res = await reach(`${w.api}/v1/chat/completions`, init);
|
|
156
|
+
}
|
|
119
157
|
if (!res.ok || !res.body) {
|
|
120
158
|
const [msg, code] = await reason(res);
|
|
121
159
|
throw new Refusal(res.status, msg || `The model answered ${res.status}`, code);
|
|
@@ -131,12 +169,22 @@ export async function complete(w, body, heard, signal) {
|
|
|
131
169
|
}
|
|
132
170
|
const reader = res.body.getReader();
|
|
133
171
|
const decoder = new TextDecoder();
|
|
172
|
+
const began = Date.now();
|
|
134
173
|
let buffer = '';
|
|
135
174
|
let text = '';
|
|
136
175
|
let model = body.model;
|
|
137
176
|
const calls = new Map();
|
|
138
177
|
for (;;) {
|
|
139
|
-
|
|
178
|
+
let read;
|
|
179
|
+
try {
|
|
180
|
+
read = await reader.read();
|
|
181
|
+
}
|
|
182
|
+
catch (e) {
|
|
183
|
+
if (stopped(e, signal))
|
|
184
|
+
throw e;
|
|
185
|
+
throw unreached(began, true);
|
|
186
|
+
}
|
|
187
|
+
const { value, done } = read;
|
|
140
188
|
if (done)
|
|
141
189
|
break;
|
|
142
190
|
buffer += decoder.decode(value, { stream: true });
|
|
@@ -371,7 +419,7 @@ export async function saveToLibrary(w, key, blob) {
|
|
|
371
419
|
}
|
|
372
420
|
/** The bytes at an address a Library item gave, or the reason they did not come. */
|
|
373
421
|
export async function fileAt(href) {
|
|
374
|
-
const r = await
|
|
422
|
+
const r = await reach(href, { credentials: 'omit' });
|
|
375
423
|
if (!r.ok)
|
|
376
424
|
throw new Error(`Download failed (HTTP ${r.status})`);
|
|
377
425
|
return r.blob();
|
|
@@ -445,7 +493,7 @@ export async function read(w, key) {
|
|
|
445
493
|
throw new Refusal(404, 'This file is not in your Library.');
|
|
446
494
|
if (r.blob)
|
|
447
495
|
return r.blob;
|
|
448
|
-
const res = await
|
|
496
|
+
const res = await reach(await url(w, key), { cache: 'no-store', credentials: 'omit' });
|
|
449
497
|
if (!res.ok)
|
|
450
498
|
throw new Refusal(res.status, 'The Library would not hand the file back.');
|
|
451
499
|
const b = await res.blob();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hanzo/studio",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.15",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "BSD-3-Clause",
|
|
6
6
|
"description": "Hanzo Studio: a chat that makes images, posters, documents, slides, type systems, 3D, video and sound, with every result open in a tab beside it. Mounted by the Hanzo app at hanzo.ai/studio and served whole at studio.hanzo.ai.",
|