@almyty/chat 1.5.10 → 1.5.12

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/dist/stream.d.ts CHANGED
@@ -86,10 +86,18 @@ export declare function formatUsage(usage: Usage): string;
86
86
  /**
87
87
  * Model attribution off a finished run's steps.
88
88
  *
89
- * The llm.response event does not carry the answering model, so a run
90
- * that streamed to completion has to be asked afterwards. Only the
91
- * tool-calling step records `routing` today, so a single-step answer
92
- * has no attribution to find and this returns null rather than
93
- * guessing.
89
+ * Used when the stream did not name the model (a reconnect, or a server
90
+ * that predates `model` on llm.response). Every llm_call step the run
91
+ * records carries the model that answered, and a routed one its
92
+ * `routing` too; the last one that says is the answer's.
94
93
  */
95
94
  export declare function routingFromSteps(steps: unknown): Pick<Usage, 'model' | 'rationale' | 'attempt'> | null;
95
+ /**
96
+ * How many steps a finished run took. The live stream only counts the
97
+ * steps that called tools (the answering step ends the run instead of
98
+ * emitting step.completed), so a one-step answer read as zero steps.
99
+ */
100
+ export declare function stepsFromRun(run: {
101
+ currentStep?: unknown;
102
+ steps?: unknown;
103
+ }): number;
package/dist/stream.js CHANGED
@@ -257,24 +257,37 @@ export function formatUsage(usage) {
257
257
  /**
258
258
  * Model attribution off a finished run's steps.
259
259
  *
260
- * The llm.response event does not carry the answering model, so a run
261
- * that streamed to completion has to be asked afterwards. Only the
262
- * tool-calling step records `routing` today, so a single-step answer
263
- * has no attribution to find and this returns null rather than
264
- * guessing.
260
+ * Used when the stream did not name the model (a reconnect, or a server
261
+ * that predates `model` on llm.response). Every llm_call step the run
262
+ * records carries the model that answered, and a routed one its
263
+ * `routing` too; the last one that says is the answer's.
265
264
  */
266
265
  export function routingFromSteps(steps) {
267
266
  if (!Array.isArray(steps))
268
267
  return null;
269
268
  for (let i = steps.length - 1; i >= 0; i--) {
270
- const routing = steps[i]?.output?.routing;
271
- if (routing && typeof routing === 'object') {
269
+ const output = steps[i]?.output;
270
+ const routing = output?.routing && typeof output.routing === 'object' ? output.routing : undefined;
271
+ const model = str(routing?.vendorModelId) ?? str(routing?.modelId) ?? str(output?.model);
272
+ if (model) {
272
273
  return {
273
- model: str(routing.vendorModelId) ?? str(routing.modelId),
274
- rationale: str(routing.rationale),
275
- attempt: typeof routing.attempt === 'number' ? routing.attempt : undefined,
274
+ model,
275
+ rationale: str(routing?.rationale),
276
+ attempt: typeof routing?.attempt === 'number' ? routing.attempt : undefined,
276
277
  };
277
278
  }
278
279
  }
279
280
  return null;
280
281
  }
282
+ /**
283
+ * How many steps a finished run took. The live stream only counts the
284
+ * steps that called tools (the answering step ends the run instead of
285
+ * emitting step.completed), so a one-step answer read as zero steps.
286
+ */
287
+ export function stepsFromRun(run) {
288
+ if (typeof run.currentStep === 'number' && Number.isFinite(run.currentStep))
289
+ return run.currentStep;
290
+ if (!Array.isArray(run.steps))
291
+ return 0;
292
+ return run.steps.filter((step) => step?.type === 'llm_call').length;
293
+ }
package/dist/turn.js CHANGED
@@ -8,7 +8,7 @@
8
8
  * cost tally. It takes the gateway as an interface so it can be tested
9
9
  * against a fake without a network or a terminal.
10
10
  */
11
- import { drain, finalText, initialStreamState, reduceStreamEvent, routingFromSteps, } from './stream.js';
11
+ import { drain, finalText, initialStreamState, reduceStreamEvent, routingFromSteps, stepsFromRun, } from './stream.js';
12
12
  function isAborted(err, signal) {
13
13
  const e = err;
14
14
  return !!signal?.aborted || (!!e && (e.name === 'AbortError' || e.code === 'ABORT_ERR'));
@@ -52,6 +52,8 @@ function settleUsage(usage, run) {
52
52
  settled.cost = run.totalCost;
53
53
  if (run && typeof run.totalTokens === 'number' && run.totalTokens > settled.tokens)
54
54
  settled.tokens = run.totalTokens;
55
+ if (run)
56
+ settled.steps = Math.max(settled.steps, stepsFromRun(run));
55
57
  return settled;
56
58
  }
57
59
  export async function runTurn(target, message, options = {}) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@almyty/chat",
3
- "version": "1.5.10",
3
+ "version": "1.5.12",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -31,7 +31,7 @@
31
31
  "author": "almyty",
32
32
  "license": "Apache-2.0",
33
33
  "dependencies": {
34
- "@almyty/client": "^1.5.10",
34
+ "@almyty/client": "^1.5.12",
35
35
  "ink": "^8.0.0",
36
36
  "ink-spinner": "^5.0.0",
37
37
  "ink-text-input": "^6.0.0",