juneau 0.7.0 → 0.7.2

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.
@@ -1,114 +1,129 @@
1
- function O(n) {
2
- const t = n.map((e) => {
3
- const i = e.parts.filter((r) => r.type === "text").map((r) => r.text).join("");
4
- return { role: e.role, content: i };
5
- }), o = [];
6
- for (let e = 0; e < t.length; e++) {
7
- const i = t[e], r = o[o.length - 1];
8
- i.role === "user" && r?.role === "user" && o.push({ role: "assistant", content: "…" }), o.push(i);
1
+ function M(n) {
2
+ const e = n.map((t) => {
3
+ const r = t.parts.filter((s) => s.type === "text").map((s) => s.text).join("");
4
+ return { role: t.role, content: r };
5
+ }), i = [];
6
+ for (let t = 0; t < e.length; t++) {
7
+ const r = e[t], s = i[i.length - 1];
8
+ r.role === "user" && s?.role === "user" && i.push({ role: "assistant", content: "…" }), i.push(r);
9
9
  }
10
- return o.filter((e) => e.content.length > 0);
10
+ return i.filter((t) => t.content.length > 0);
11
11
  }
12
- function h(n) {
12
+ function y(n) {
13
13
  return `data: ${JSON.stringify(n)}
14
14
 
15
15
  `;
16
16
  }
17
- function y(n, t) {
18
- return n[t] ?? n.en;
17
+ function w(n, e) {
18
+ return n[e] ?? n.en;
19
19
  }
20
- function k(n) {
21
- const t = new Set(Object.keys(n));
22
- for (const [o, e] of Object.entries(n))
23
- if (e.tools) {
24
- for (const i of e.tools)
25
- if (!t.has(i))
20
+ function E(n) {
21
+ const e = new Set(Object.keys(n));
22
+ for (const [i, t] of Object.entries(n))
23
+ if (t.tools) {
24
+ for (const r of t.tools)
25
+ if (!e.has(r))
26
26
  throw new Error(
27
- `[juneau] createSkillSet: skill "${o}" references unknown tool "${i}". Known skills: ${[...t].join(", ")}`
27
+ `[juneau] createSkillSet: skill "${i}" references unknown tool "${r}". Known skills: ${[...e].join(", ")}`
28
28
  );
29
29
  }
30
30
  }
31
- function W(n, t = {}) {
32
- k(n);
33
- const o = t.language ?? "en", e = t.debug ?? !1, i = [], r = [];
34
- let s = !1, c = null;
35
- const f = {};
36
- for (const [l, a] of Object.entries(n))
37
- f[l] = {
38
- description: a.description,
39
- parameters: a.input,
40
- execute: async (u) => {
41
- r.includes(l) || r.push(l), e && console.debug(`[juneau] skill "${l}" → executing, input:`, u);
42
- const g = e ? Date.now() : 0;
43
- i.push(
44
- h({
31
+ function C(n, e = {}) {
32
+ E(n);
33
+ const i = e.language ?? "en", t = e.debug ?? !1, r = [], s = [], o = [];
34
+ let c = !1, d = null;
35
+ const g = /* @__PURE__ */ new Map(), u = /* @__PURE__ */ new Map();
36
+ for (const [l, f] of Object.entries(n)) {
37
+ if (g.has(f.id))
38
+ throw new Error(
39
+ `[juneau] createSkillSet: skills "${u.get(f.id)}" and "${l}" share the same ID (${f.id}). IDs must be unique.`
40
+ );
41
+ g.set(f.id, f), u.set(f.id, l);
42
+ }
43
+ const a = {};
44
+ for (const [l, f] of Object.entries(n))
45
+ a[l] = {
46
+ description: f.description,
47
+ parameters: f.input,
48
+ execute: async (S) => {
49
+ s.includes(l) || (s.push(l), o.push(f.id)), t && console.debug(`[juneau] skill "${l}" → executing, input:`, S);
50
+ const k = t ? Date.now() : 0;
51
+ r.push(
52
+ y({
45
53
  type: "activity",
46
54
  id: l,
47
- title: y(a.labels.running, o),
55
+ title: w(f.labels.running, i),
48
56
  status: "running",
49
57
  metadata: { skill: l }
50
58
  })
51
59
  );
52
- const $ = {
53
- emit: (d) => {
54
- e && console.debug(`[juneau] skill "${l}" → emitting part "${d.type}"`), i.push(h({ type: "part", part: d }));
60
+ const O = {
61
+ emit: (h) => {
62
+ t && console.debug(`[juneau] skill "${l}" → emitting part "${h.type}"`), r.push(y({ type: "part", part: h }));
55
63
  }
56
64
  };
57
65
  try {
58
- const d = await a.execute(u, $), p = e ? Date.now() - g : 0;
59
- return e && console.debug(`[juneau] skill "${l}" → done in ${p}ms, result:`, d), i.push(
60
- h({
66
+ const h = await f.execute(S, O), m = t ? Date.now() - k : 0;
67
+ return t && console.debug(`[juneau] skill "${l}" → done in ${m}ms, result:`, h), r.push(
68
+ y({
61
69
  type: "activity",
62
70
  id: l,
63
- title: y(a.labels.done, o),
71
+ title: w(f.labels.done, i),
64
72
  status: "done",
65
73
  metadata: { skill: l }
66
74
  })
67
- ), d;
68
- } catch (d) {
69
- const p = e ? Date.now() - g : 0, v = d instanceof Error ? d.message : String(d);
70
- e && console.debug(`[juneau] skill "${l}" → FAILED in ${p}ms: "${v}"`);
71
- const T = a.labels.failed ?? { cs: "Nepodařilo se", en: "Failed" };
72
- throw i.push(
73
- h({
75
+ ), h;
76
+ } catch (h) {
77
+ const m = t ? Date.now() - k : 0, $ = h instanceof Error ? h.message : String(h);
78
+ t && console.debug(`[juneau] skill "${l}" → FAILED in ${m}ms: "${$}"`);
79
+ const A = f.labels.failed ?? { cs: "Nepodařilo se", en: "Failed" };
80
+ throw r.push(
81
+ y({
74
82
  type: "activity",
75
83
  id: l,
76
- title: y(T, o),
84
+ title: w(A, i),
77
85
  status: "failed",
78
86
  metadata: { skill: l }
79
87
  })
80
- ), s = !0, c = `Tool "${l}" failed: ${v}`, d;
88
+ ), c = !0, d = `Tool "${l}" failed: ${$}`, h;
81
89
  }
82
90
  }
83
91
  };
84
92
  return {
85
- tools: f,
93
+ tools: a,
86
94
  skills: n,
87
- calledSkillNames: r,
95
+ calledSkillNames: s,
96
+ calledSkillIds: o,
97
+ skillsById: g,
88
98
  drainActivities() {
89
- return i.splice(0, i.length);
99
+ return r.splice(0, r.length);
90
100
  },
91
101
  get hadFailure() {
92
- return s;
102
+ return c;
93
103
  },
94
104
  get failureContext() {
95
- return c;
105
+ return d;
96
106
  }
97
107
  };
98
108
  }
99
- function A(n) {
100
- return Object.entries(n.skills).map(([t, o]) => {
101
- const e = [o.readOnly === !1 ? "write" : "read"];
102
- return o.requiresConfirmation && e.push("requires confirmation"), `- ${t} (${e.join(", ")}): ${o.description}`;
109
+ function P(n) {
110
+ return Object.entries(n.skills).map(([e, i]) => {
111
+ const t = [i.readOnly === !1 ? "write" : "read"];
112
+ return i.requiresConfirmation && t.push("requires confirmation"), `${`[${i.id}] `}${e} (${t.join(", ")}): ${i.description}`;
103
113
  }).join(`
104
114
  `);
105
115
  }
106
- function E(n, t) {
107
- return t.map((o) => n.skills[o]).filter((o) => o?.instructions).map((o) => o.instructions).join(`
116
+ function L(n, e) {
117
+ return e.map((i) => n.skills[i]).filter((i) => i?.instructions).map((i) => i.instructions).join(`
108
118
 
109
119
  `);
110
120
  }
111
- const w = /* @__PURE__ */ new Set(["text-delta", "text"]), S = /* @__PURE__ */ new Set([
121
+ function I(n, e) {
122
+ return e.map((i) => n.skillsById.get(i)).filter((i) => i?.instructions).map((i) => i.instructions).join(`
123
+
124
+ `);
125
+ }
126
+ const j = /* @__PURE__ */ new Set(["text-delta", "text"]), R = /* @__PURE__ */ new Set([
112
127
  "tool-call",
113
128
  "tool-result",
114
129
  "tool-input-start",
@@ -122,136 +137,203 @@ const w = /* @__PURE__ */ new Set(["text-delta", "text"]), S = /* @__PURE__ */ n
122
137
  "response-metadata",
123
138
  "stream-start"
124
139
  ]);
125
- async function* m(n, t, o) {
126
- const e = o?.debug ?? !1;
127
- for await (const i of n) {
128
- const r = i, s = r.type;
129
- if (e)
130
- if (s && w.has(s)) {
131
- const c = r.text ?? r.textDelta;
132
- console.debug(`[juneau] streamToWire chunk: ${s} ${JSON.stringify(c ?? "")}`);
133
- } else s === "tool-call" ? console.debug(`[juneau] streamToWire chunk: tool-call "${r.toolName ?? r.name ?? "?"}"`) : s === "tool-result" ? console.debug("[juneau] streamToWire chunk: tool-result (ignored)") : s && (S.has(s) ? console.debug(`[juneau] streamToWire chunk: ${s} (ignored)`) : console.debug(`[juneau] streamToWire chunk: ${s} (unknown)`));
134
- if (s && w.has(s)) {
135
- if (t) {
136
- const f = t.drainActivities();
137
- e && f.length > 0 && console.debug(`[juneau] streamToWire → draining ${f.length} activities`);
138
- for (const l of f)
139
- yield l;
140
+ async function* p(n, e, i) {
141
+ const t = i?.debug ?? !1;
142
+ for await (const r of n) {
143
+ const s = r, o = s.type;
144
+ if (t)
145
+ if (o && j.has(o)) {
146
+ const c = s.text ?? s.textDelta;
147
+ console.debug(`[juneau] streamToWire chunk: ${o} ${JSON.stringify(c ?? "")}`);
148
+ } else o === "tool-call" ? console.debug(`[juneau] streamToWire chunk: tool-call "${s.toolName ?? s.name ?? "?"}"`) : o === "tool-result" ? console.debug("[juneau] streamToWire chunk: tool-result (ignored)") : o && (R.has(o) ? console.debug(`[juneau] streamToWire chunk: ${o} (ignored)`) : console.debug(`[juneau] streamToWire chunk: ${o} (unknown)`));
149
+ if (o && j.has(o)) {
150
+ if (e) {
151
+ const d = e.drainActivities();
152
+ t && d.length > 0 && console.debug(`[juneau] streamToWire → draining ${d.length} activities`);
153
+ for (const g of d)
154
+ yield g;
140
155
  }
141
- const c = r.text ?? r.textDelta;
142
- c && (e && console.debug(`[juneau] streamToWire → yielding text ${JSON.stringify(c)}`), yield `data: ${JSON.stringify({ type: "text", text: c })}
156
+ const c = s.text ?? s.textDelta;
157
+ c && (t && console.debug(`[juneau] streamToWire → yielding text ${JSON.stringify(c)}`), yield `data: ${JSON.stringify({ type: "text", text: c })}
143
158
 
144
159
  `);
145
160
  }
146
161
  }
147
- if (t) {
148
- const i = t.drainActivities();
149
- e && i.length > 0 && console.debug(`[juneau] streamToWire → draining ${i.length} activities (end of stream)`);
150
- for (const r of i)
151
- yield r;
162
+ if (e) {
163
+ const r = e.drainActivities();
164
+ t && r.length > 0 && console.debug(`[juneau] streamToWire → draining ${r.length} activities (end of stream)`);
165
+ for (const s of r)
166
+ yield s;
152
167
  }
153
- e && console.debug("[juneau] streamToWire → end of stream, emitting done"), yield `data: ${JSON.stringify({ type: "done" })}
168
+ t && console.debug("[juneau] streamToWire → end of stream, emitting done"), yield `data: ${JSON.stringify({ type: "done" })}
154
169
 
155
170
  `;
156
171
  }
157
- function j(n) {
172
+ function v(n) {
158
173
  return `data: ${JSON.stringify({ type: "error", message: n })}
159
174
 
160
175
  `;
161
176
  }
162
- function b() {
177
+ function x() {
163
178
  return `data: ${JSON.stringify({ type: "done" })}
164
179
 
165
180
  `;
166
181
  }
167
- function x(n) {
168
- let t = "";
169
- for (const o of n.split(`
182
+ function b(n) {
183
+ let e = "";
184
+ for (const i of n.split(`
170
185
  `))
171
- if (o.startsWith("data: "))
186
+ if (i.startsWith("data: "))
172
187
  try {
173
- const e = JSON.parse(o.slice(6));
174
- e.type === "text" && typeof e.text == "string" && (t += e.text);
188
+ const t = JSON.parse(i.slice(6));
189
+ t.type === "text" && typeof t.text == "string" && (e += t.text);
175
190
  } catch {
176
191
  }
177
- return t;
192
+ return e;
178
193
  }
179
- async function* F(n) {
180
- const { phase1: t, phase2: o, phase3: e, skillSet: i, onFinish: r, debug: s = !1 } = n;
181
- s && console.debug("[juneau] withToolRecovery → phase 1 start");
182
- let c = !1, f = "";
183
- const l = t();
184
- for await (const a of m(l.fullStream, i, { debug: s }))
185
- f += x(a), a.includes('"type":"text"') && (c = !0), !a.includes('"type":"done"') && (yield a);
186
- if (s && console.debug(
187
- `[juneau] withToolRecovery → phase 1 complete, textProduced=${c}, hadFailure=${i.hadFailure}`
188
- ), i.hadFailure && !c && i.failureContext) {
189
- s && console.debug("[juneau] withToolRecovery → hadFailure=true, textProduced=false → triggering phase 2");
194
+ async function* B(n) {
195
+ const { phase1: e, phase2: i, phase3: t, skillSet: r, onFinish: s, debug: o = !1 } = n;
196
+ o && console.debug("[juneau] withToolRecovery → phase 1 start");
197
+ let c = !1, d = "";
198
+ const g = e();
199
+ for await (const u of p(g.fullStream, r, { debug: o }))
200
+ d += b(u), u.includes('"type":"text"') && (c = !0), !u.includes('"type":"done"') && (yield u);
201
+ if (o && console.debug(
202
+ `[juneau] withToolRecovery → phase 1 complete, textProduced=${c}, hadFailure=${r.hadFailure}`
203
+ ), r.hadFailure && !c && r.failureContext) {
204
+ o && console.debug("[juneau] withToolRecovery → hadFailure=true, textProduced=false → triggering phase 2");
190
205
  try {
191
- const a = o(i.failureContext);
192
- for await (const u of m(a.fullStream, void 0, { debug: s }))
193
- f += x(u), !u.includes('"type":"done"') && (yield u);
194
- s && console.debug("[juneau] withToolRecovery → phase 2 complete"), r?.({ text: f }), yield b();
195
- } catch (a) {
196
- const u = a instanceof Error ? a.message : String(a);
197
- s && console.debug(`[juneau] withToolRecovery → phase 2 FAILED: "${u}"`), yield j(u);
206
+ const u = i(r.failureContext);
207
+ for await (const a of p(u.fullStream, void 0, { debug: o }))
208
+ d += b(a), !a.includes('"type":"done"') && (yield a);
209
+ o && console.debug("[juneau] withToolRecovery → phase 2 complete"), s?.({ text: d }), yield x();
210
+ } catch (u) {
211
+ const a = u instanceof Error ? u.message : String(u);
212
+ o && console.debug(`[juneau] withToolRecovery → phase 2 FAILED: "${a}"`), yield v(a);
198
213
  }
199
214
  return;
200
215
  }
201
- if (!c && !i.hadFailure && e) {
202
- const a = await N(l);
203
- if (a) {
204
- s && (console.debug(
205
- `[juneau] withToolRecovery → phase 3 messages resolved (${a.length}):`,
206
- JSON.stringify(a, null, 2)
207
- ), R(a) || console.debug(
216
+ if (!c && !r.hadFailure && t) {
217
+ const u = await W(g);
218
+ if (u) {
219
+ o && (console.debug(
220
+ `[juneau] withToolRecovery → phase 3 messages resolved (${u.length}):`,
221
+ JSON.stringify(u, null, 2)
222
+ ), F(u) || console.debug(
208
223
  "[juneau] withToolRecovery → WARNING: resolved history contains no tool-result turn — Gemini will likely reject it as an invalid conversation structure"
209
224
  ), console.debug("[juneau] withToolRecovery → no failure, no text → triggering phase 3"));
210
225
  try {
211
- for await (const u of m(e(a).fullStream, void 0, { debug: s }))
212
- f += x(u), !u.includes('"type":"done"') && (yield u);
213
- s && console.debug("[juneau] withToolRecovery → phase 3 complete"), r?.({ text: f }), yield b();
214
- } catch (u) {
215
- const g = u instanceof Error ? u.message : String(u);
216
- s && console.debug(`[juneau] withToolRecovery → phase 3 FAILED: "${g}"`), yield j(g);
226
+ for await (const a of p(t(u).fullStream, void 0, { debug: o }))
227
+ d += b(a), !a.includes('"type":"done"') && (yield a);
228
+ o && console.debug("[juneau] withToolRecovery → phase 3 complete"), s?.({ text: d }), yield x();
229
+ } catch (a) {
230
+ const l = a instanceof Error ? a.message : String(a);
231
+ o && console.debug(`[juneau] withToolRecovery → phase 3 FAILED: "${l}"`), yield v(l);
217
232
  }
218
233
  return;
219
234
  }
220
- s && console.debug(
235
+ o && console.debug(
221
236
  "[juneau] withToolRecovery → phase 3 configured but phase 1 result exposes no message history — emitting done"
222
237
  );
223
- } else !c && !i.hadFailure && s && console.debug(
238
+ } else !c && !r.hadFailure && o && console.debug(
224
239
  "[juneau] withToolRecovery → no failure, no text, no phase3 — emitting done (Gemini silent response)"
225
240
  );
226
- r?.({ text: f }), yield b();
241
+ s?.({ text: d }), yield x();
227
242
  }
228
- async function N(n) {
243
+ async function W(n) {
229
244
  if (n.messages) {
230
- const t = await n.messages;
231
- if (Array.isArray(t) && t.length > 0)
232
- return t;
245
+ const e = await n.messages;
246
+ if (Array.isArray(e) && e.length > 0)
247
+ return e;
233
248
  }
234
249
  if (n.response) {
235
- const t = await n.response;
236
- if (Array.isArray(t?.messages) && t.messages.length > 0)
237
- return t.messages;
250
+ const e = await n.response;
251
+ if (Array.isArray(e?.messages) && e.messages.length > 0)
252
+ return e.messages;
238
253
  }
239
254
  return null;
240
255
  }
241
- function R(n) {
242
- return n.some((t) => {
243
- const o = t;
244
- return o.role === "tool" ? !0 : Array.isArray(o.content) ? o.content.some(
245
- (e) => e.type === "tool-result"
256
+ function F(n) {
257
+ return n.some((e) => {
258
+ const i = e;
259
+ return i.role === "tool" ? !0 : Array.isArray(i.content) ? i.content.some(
260
+ (t) => t.type === "tool-result"
246
261
  ) : !1;
247
262
  });
248
263
  }
264
+ function T(n) {
265
+ return `data: ${JSON.stringify({ type: "error", message: n })}
266
+
267
+ `;
268
+ }
269
+ function D() {
270
+ return `data: ${JSON.stringify({ type: "done" })}
271
+
272
+ `;
273
+ }
274
+ function N(n) {
275
+ let e = "";
276
+ for (const i of n.split(`
277
+ `))
278
+ if (i.startsWith("data: "))
279
+ try {
280
+ const t = JSON.parse(i.slice(6));
281
+ t.type === "text" && typeof t.text == "string" && (e += t.text);
282
+ } catch {
283
+ }
284
+ return e;
285
+ }
286
+ async function J(n) {
287
+ if (n.messages) {
288
+ const e = await n.messages;
289
+ if (Array.isArray(e) && e.length > 0) return e;
290
+ }
291
+ if (n.response) {
292
+ const e = await n.response;
293
+ if (Array.isArray(e?.messages) && e.messages.length > 0)
294
+ return e.messages;
295
+ }
296
+ return null;
297
+ }
298
+ async function* G(n) {
299
+ const { phase1: e, phase2: i, skillSet: t, onFinish: r, debug: s = !1 } = n;
300
+ s && console.debug("[juneau] withSkillDispatch → phase 1 start");
301
+ let o = "", c = !1;
302
+ const d = e();
303
+ for await (const a of p(d.fullStream, t, { debug: s }))
304
+ o += N(a), a.includes('"type":"text"') && (c = !0), !a.includes('"type":"done"') && (yield a);
305
+ if (s && console.debug(
306
+ `[juneau] withSkillDispatch → phase 1 complete, calledSkillIds=[${t.calledSkillIds.join(", ")}], textProduced=${c}`
307
+ ), t.calledSkillIds.length === 0) {
308
+ s && console.debug("[juneau] withSkillDispatch → no skill called, emitting done"), r?.({ text: o }), yield D();
309
+ return;
310
+ }
311
+ const g = await J(d);
312
+ if (!g) {
313
+ s && console.debug("[juneau] withSkillDispatch → could not resolve phase 1 history, emitting error"), yield T("withSkillDispatch: could not resolve phase 1 message history for phase 2");
314
+ return;
315
+ }
316
+ const u = I(t, t.calledSkillIds);
317
+ s && console.debug(
318
+ `[juneau] withSkillDispatch → phase 2 start, instructions length=${u.length}, history turns=${g.length}`
319
+ );
320
+ try {
321
+ for await (const a of p(i(u, g).fullStream, void 0, { debug: s }))
322
+ o += N(a), !a.includes('"type":"done"') && (yield a);
323
+ s && console.debug("[juneau] withSkillDispatch → phase 2 complete"), r?.({ text: o }), yield D();
324
+ } catch (a) {
325
+ const l = a instanceof Error ? a.message : String(a);
326
+ s && console.debug(`[juneau] withSkillDispatch → phase 2 FAILED: "${l}"`), yield T(l);
327
+ }
328
+ }
249
329
  export {
250
- A as buildSkillIndex,
251
- W as createSkillSet,
252
- E as selectSkills,
253
- m as streamToWire,
254
- O as toSdkMessages,
255
- F as withToolRecovery
330
+ P as buildSkillIndex,
331
+ C as createSkillSet,
332
+ L as selectSkills,
333
+ I as selectSkillsById,
334
+ p as streamToWire,
335
+ M as toSdkMessages,
336
+ G as withSkillDispatch,
337
+ B as withToolRecovery
256
338
  };
257
339
  //# sourceMappingURL=index.js.map