gemi 0.231.0 → 0.233.0

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.
Files changed (43) hide show
  1. package/dist/ai/Agent.d.ts +51 -11
  2. package/dist/ai/Agent.d.ts.map +1 -1
  3. package/dist/ai/AgentController.d.ts +29 -0
  4. package/dist/ai/AgentController.d.ts.map +1 -1
  5. package/dist/ai/AgentProvider.d.ts +32 -2
  6. package/dist/ai/AgentProvider.d.ts.map +1 -1
  7. package/dist/ai/FallbackProvider.d.ts.map +1 -1
  8. package/dist/ai/client/index.js +95 -16
  9. package/dist/ai/client/index.js.map +1 -1
  10. package/dist/ai/client/reducer.d.ts +8 -0
  11. package/dist/ai/client/reducer.d.ts.map +1 -1
  12. package/dist/ai/index.js +27 -27
  13. package/dist/ai/index.js.map +5 -5
  14. package/dist/ai/providers/anthropicRequest.d.ts.map +1 -1
  15. package/dist/ai/providers/capabilities.d.ts +17 -0
  16. package/dist/ai/providers/capabilities.d.ts.map +1 -1
  17. package/dist/ai/providers/request.d.ts +7 -0
  18. package/dist/ai/providers/request.d.ts.map +1 -1
  19. package/dist/ai/testing/index.js +3 -3
  20. package/dist/ai/testing/index.js.map +3 -3
  21. package/dist/ai/testing/scriptedProvider.d.ts +2 -0
  22. package/dist/ai/testing/scriptedProvider.d.ts.map +1 -1
  23. package/dist/ai/types.d.ts +7 -0
  24. package/dist/ai/types.d.ts.map +1 -1
  25. package/dist/ai/useChat.d.ts.map +1 -1
  26. package/dist/{chunk-b7yksj4c.js → chunk-5038v4f1.js} +1 -1
  27. package/dist/{chunk-pwk0qab4.js → chunk-5b9fzma1.js} +2 -2
  28. package/dist/{chunk-pwk0qab4.js.map → chunk-5b9fzma1.js.map} +1 -1
  29. package/dist/chunk-607nk925.js +48 -0
  30. package/dist/{chunk-7tm00avm.js.map → chunk-607nk925.js.map} +3 -3
  31. package/dist/{chunk-f32rvs4z.js → chunk-azj481bb.js} +1 -1
  32. package/dist/{chunk-vqck5mrk.js → chunk-jnf0jhdz.js} +1 -1
  33. package/dist/chunk-t17vf1ep.js +12 -0
  34. package/dist/{chunk-8x8pbn1z.js.map → chunk-t17vf1ep.js.map} +6 -6
  35. package/dist/kernel/index.js +1 -1
  36. package/dist/server/index.js +1 -1
  37. package/dist/services/index.js +1 -1
  38. package/package.json +1 -1
  39. package/dist/chunk-7tm00avm.js +0 -48
  40. package/dist/chunk-8x8pbn1z.js +0 -12
  41. /package/dist/{chunk-b7yksj4c.js.map → chunk-5038v4f1.js.map} +0 -0
  42. /package/dist/{chunk-f32rvs4z.js.map → chunk-azj481bb.js.map} +0 -0
  43. /package/dist/{chunk-vqck5mrk.js.map → chunk-jnf0jhdz.js.map} +0 -0
@@ -16,6 +16,53 @@ function initialChatState(init = {}) {
16
16
  };
17
17
  }
18
18
  /**
19
+ * The turn each tool call in `messages` was asked in (`AgentMessage.turn` of
20
+ * the message holding it), by `toolCallId`: for a pending call without its
21
+ * own `turn`. `undefined` for a call stored without one.
22
+ */
23
+ function callTurns(messages) {
24
+ const turns = /* @__PURE__ */ new Map();
25
+ for (const message of messages) for (const part of message.content) if (part.type === "tool-call") turns.set(part.toolCallId, message.turn);
26
+ return turns;
27
+ }
28
+ /** The top-level call a pending one stands for: itself, or the call its sub-run is nested under. */
29
+ var hostOf = (call) => call.path?.[0] ?? call.toolCallId;
30
+ /**
31
+ * The held calls an answer to the calls in `ids` settles (#1355): the calls
32
+ * of the turns those belong to. Another turn's question is that turn's to
33
+ * answer, and the server leaves it open. Where a turn can't be told (a
34
+ * transcript stored before `AgentMessage.turn`, a call the transcript doesn't
35
+ * hold), every held call, as before.
36
+ */
37
+ function settledBy(messages, pending, ids) {
38
+ const turns = callTurns(messages);
39
+ const turnOf = (call) => call.turn ?? turns.get(hostOf(call));
40
+ const answered = pending.filter((call) => ids.has(call.toolCallId));
41
+ const named = new Set(answered.map(turnOf));
42
+ if (answered.length === 0 || named.has(void 0)) return new Set(pending.map((call) => call.toolCallId));
43
+ return new Set(pending.filter((call) => {
44
+ const turn = turnOf(call);
45
+ return turn === void 0 || named.has(turn);
46
+ }).map((call) => call.toolCallId));
47
+ }
48
+ /**
49
+ * The held calls in `pending` that belong to a turn none of `next` belongs
50
+ * to: what an `awaiting-input` frame for one turn leaves standing (#1355).
51
+ * None when a turn can't be told, as before.
52
+ */
53
+ function othersTurns(messages, pending, next) {
54
+ if (pending.length === 0) return [];
55
+ const turns = callTurns(messages);
56
+ const turnOf = (call) => call.turn ?? turns.get(hostOf(call));
57
+ const asking = new Set(next.map(turnOf));
58
+ if (asking.has(void 0) || asking.size === 0) return [];
59
+ const replaced = new Set(next.map((call) => call.toolCallId));
60
+ return pending.filter((call) => {
61
+ const turn = turnOf(call);
62
+ return turn !== void 0 && !asking.has(turn) && !replaced.has(call.toolCallId);
63
+ });
64
+ }
65
+ /**
19
66
  * The calls a stored transcript is still waiting on (#1052): every
20
67
  * `AgentMessage.pending` entry whose tool call has no result yet.
21
68
  *
@@ -326,7 +373,7 @@ function reduce(state, event, now) {
326
373
  case "awaiting-input": return {
327
374
  ...state,
328
375
  runId: event.runId,
329
- pending: [...event.pending]
376
+ pending: [...othersTurns(state.messages, state.pending, event.pending), ...event.pending]
330
377
  };
331
378
  case "message-end": return withMessage(state, event.messageId, now, (message) => ({
332
379
  ...message,
@@ -346,7 +393,7 @@ function reduce(state, event, now) {
346
393
  case "error": return {
347
394
  ...state,
348
395
  error: event.error,
349
- pending: []
396
+ pending: state.pending.filter((call) => call.turn !== void 0)
350
397
  };
351
398
  case "run-end": return {
352
399
  ...state,
@@ -1222,7 +1269,19 @@ function useChat(path, params = {}) {
1222
1269
  }, [commit, setPhaseSafe]);
1223
1270
  const send = useCallback(async (turn, options = {}) => {
1224
1271
  const { base: url, extraBody, ifBusy: defaultIfBusy } = requestRef.current;
1225
- const ifBusy = stateRef.current.threadId ? options.ifBusy ?? defaultIfBusy : void 0;
1272
+ const asked = stateRef.current.threadId ? options.ifBusy ?? defaultIfBusy : void 0;
1273
+ const held = stateRef.current.pending;
1274
+ const answerTurns = (() => {
1275
+ if (!turn.toolResults?.length || turn.text || turn.files?.length) return void 0;
1276
+ const turns = /* @__PURE__ */ new Set();
1277
+ for (const result of turn.toolResults) {
1278
+ const call = held.find((candidate) => candidate.toolCallId === result.toolCallId);
1279
+ if (!call?.turn) return void 0;
1280
+ turns.add(call.turn);
1281
+ }
1282
+ return turns;
1283
+ })();
1284
+ const ifBusy = stateRef.current.threadId !== void 0 && answerTurns !== void 0 && flightsRef.current.size > 0 && [...flightsRef.current].every((flight) => flight.turns === void 0 || flight.turns === "new" || ![...flight.turns].some((id) => answerTurns.has(id))) ? "parallel" : asked;
1226
1285
  const cuts = ifBusy === void 0 || ifBusy === "supersede";
1227
1286
  const body = options.body ? {
1228
1287
  ...extraBody,
@@ -1237,7 +1296,8 @@ function useChat(path, params = {}) {
1237
1296
  const controller = new AbortController();
1238
1297
  const flight = {
1239
1298
  controller,
1240
- clientRunId
1299
+ clientRunId,
1300
+ ...answerTurns ? { turns: answerTurns } : turn.text || turn.files?.length ? { turns: "new" } : {}
1241
1301
  };
1242
1302
  const wasBusy = flightsRef.current.size > 0;
1243
1303
  flightsRef.current.add(flight);
@@ -1267,7 +1327,8 @@ function useChat(path, params = {}) {
1267
1327
  createdAt: (/* @__PURE__ */ new Date()).toISOString(),
1268
1328
  ...options.metadata ? { metadata: options.metadata } : {}
1269
1329
  }] : [];
1270
- const answered = turn.toolResults?.length ? new Set(previous.pending.map((call) => call.toolCallId)) : null;
1330
+ const setAside = authored.length > 0 && !turn.toolResults?.length && ifBusy !== "parallel" && previous.threadId ? previous.pending.filter((call) => call.turn !== void 0 && !call.answering) : [];
1331
+ const answered = turn.toolResults?.length ? settledBy(previous.messages, previous.pending, new Set(turn.toolResults.map((result) => result.toolCallId))) : null;
1271
1332
  let settled = false;
1272
1333
  const settle = (taken) => {
1273
1334
  if (!answered || settled || !flightsRef.current.has(flight)) return;
@@ -1275,14 +1336,14 @@ function useChat(path, params = {}) {
1275
1336
  const current = stateRef.current;
1276
1337
  commit({
1277
1338
  ...current,
1278
- pending: taken ? [] : withAnswering(current.pending, answered, false)
1339
+ pending: taken ? current.pending.filter((call) => !answered.has(call.toolCallId)) : withAnswering(current.pending, answered, false)
1279
1340
  });
1280
1341
  };
1281
1342
  commit({
1282
1343
  ...previous,
1283
1344
  messages: [...history, ...authored],
1284
1345
  error: null,
1285
- pending: answered ? withAnswering(previous.pending, answered, true) : []
1346
+ pending: answered ? withAnswering(previous.pending, answered, true) : ifBusy === "parallel" && authored.length > 0 ? previous.pending : []
1286
1347
  });
1287
1348
  if (!wasBusy || phaseRef.current !== "streaming") setPhaseSafe("submitted");
1288
1349
  try {
@@ -1307,6 +1368,16 @@ function useChat(path, params = {}) {
1307
1368
  }
1308
1369
  settle(true);
1309
1370
  await consume(response, flight);
1371
+ if (setAside.length > 0) {
1372
+ const current = stateRef.current;
1373
+ const resolved = new Set(current.messages.flatMap((message) => message.content.flatMap((part) => part.type === "tool-result" ? [part.toolCallId] : [])));
1374
+ const held = new Set(current.pending.map((call) => call.toolCallId));
1375
+ const open = setAside.filter((call) => !resolved.has(call.path?.[0] ?? call.toolCallId) && !held.has(call.toolCallId));
1376
+ if (open.length > 0) commit({
1377
+ ...current,
1378
+ pending: [...open, ...current.pending]
1379
+ });
1380
+ }
1310
1381
  } catch (error) {
1311
1382
  if (!isAbort(error)) {
1312
1383
  settle(false);
@@ -1344,22 +1415,29 @@ function useChat(path, params = {}) {
1344
1415
  * it reads.
1345
1416
  */
1346
1417
  const queued = useRef(null);
1347
- const answering = useRef(null);
1418
+ const answering = useRef([]);
1348
1419
  const queueResult = useCallback((result) => {
1349
1420
  const batch = queued.current;
1350
1421
  if (batch) {
1351
- if (!batch.results.some((queuedResult) => queuedResult.toolCallId === result.toolCallId)) batch.results.push(result);
1422
+ if (!batch.results.some((queuedResult) => queuedResult.toolCallId === result.toolCallId)) {
1423
+ batch.results.push(result);
1424
+ const current = stateRef.current;
1425
+ commit({
1426
+ ...current,
1427
+ pending: withAnswering(current.pending, settledBy(current.messages, current.pending, /* @__PURE__ */ new Set([result.toolCallId])), true)
1428
+ });
1429
+ }
1352
1430
  return batch.flushed;
1353
1431
  }
1354
1432
  const results = [result];
1355
1433
  const flushed = Promise.resolve().then(async () => {
1356
1434
  const batch = queued.current;
1357
1435
  queued.current = null;
1358
- answering.current = batch;
1436
+ answering.current = [...answering.current, batch];
1359
1437
  try {
1360
1438
  await send({ toolResults: results });
1361
1439
  } finally {
1362
- if (answering.current === batch) answering.current = null;
1440
+ answering.current = answering.current.filter((other) => other !== batch);
1363
1441
  }
1364
1442
  });
1365
1443
  queued.current = {
@@ -1369,7 +1447,7 @@ function useChat(path, params = {}) {
1369
1447
  const current = stateRef.current;
1370
1448
  commit({
1371
1449
  ...current,
1372
- pending: withAnswering(current.pending, new Set(current.pending.map((call) => call.toolCallId)), true)
1450
+ pending: withAnswering(current.pending, settledBy(current.messages, current.pending, /* @__PURE__ */ new Set([result.toolCallId])), true)
1373
1451
  });
1374
1452
  return flushed;
1375
1453
  }, [commit, send]);
@@ -1426,13 +1504,14 @@ function useChat(path, params = {}) {
1426
1504
  * from `onError` goes through.
1427
1505
  */
1428
1506
  const submit = useCallback((toolCallId, build) => {
1429
- const inFlight = answering.current;
1430
- if (!queued.current && inFlight) {
1507
+ const flying = answering.current;
1508
+ if (!queued.current && flying.length > 0) {
1431
1509
  if (stateRef.current.pending.find((candidate) => candidate.toolCallId === toolCallId)?.answering) {
1432
1510
  const result = resultFor(toolCallId, build);
1433
1511
  if (!result) return Promise.resolve();
1434
- const sent = inFlight.results.find((sentResult) => sentResult.toolCallId === toolCallId);
1435
- if (sent && JSON.stringify(sent) === JSON.stringify(result)) return inFlight.flushed;
1512
+ const inFlight = flying.find((batch) => batch.results.some((sentResult) => sentResult.toolCallId === toolCallId));
1513
+ const sent = inFlight?.results.find((sentResult) => sentResult.toolCallId === toolCallId);
1514
+ if (inFlight && sent && JSON.stringify(sent) === JSON.stringify(result)) return inFlight.flushed;
1436
1515
  fail({
1437
1516
  code: "invalid_tool_result",
1438
1517
  message: sent ? `Tool call ${toolCallId} is already being answered differently` : `Tool call ${toolCallId} is settled by the answer in flight, which refuses it`,