@namzu/sdk 67.0.0 → 68.0.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 (87) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/pricing/catalogue.generated.d.ts.map +1 -1
  3. package/dist/pricing/catalogue.generated.js +6 -0
  4. package/dist/pricing/catalogue.generated.js.map +1 -1
  5. package/dist/provider/fallback.d.ts.map +1 -1
  6. package/dist/provider/fallback.js +12 -0
  7. package/dist/provider/fallback.js.map +1 -1
  8. package/dist/provider/idle-timeout.d.ts.map +1 -1
  9. package/dist/provider/idle-timeout.js +5 -0
  10. package/dist/provider/idle-timeout.js.map +1 -1
  11. package/dist/provider/model-window-suffix.d.ts.map +1 -1
  12. package/dist/provider/model-window-suffix.js +1 -0
  13. package/dist/provider/model-window-suffix.js.map +1 -1
  14. package/dist/provider/retry.d.ts.map +1 -1
  15. package/dist/provider/retry.js +5 -0
  16. package/dist/provider/retry.js.map +1 -1
  17. package/dist/public-runtime.d.ts +2 -2
  18. package/dist/public-runtime.d.ts.map +1 -1
  19. package/dist/public-runtime.js +2 -2
  20. package/dist/public-runtime.js.map +1 -1
  21. package/dist/public-types.d.ts +3 -3
  22. package/dist/public-types.d.ts.map +1 -1
  23. package/dist/registry/command/kernel-commands.d.ts +11 -1
  24. package/dist/registry/command/kernel-commands.d.ts.map +1 -1
  25. package/dist/registry/command/kernel-commands.js +114 -46
  26. package/dist/registry/command/kernel-commands.js.map +1 -1
  27. package/dist/runtime/query/iteration/index.d.ts +1 -1
  28. package/dist/runtime/query/iteration/index.d.ts.map +1 -1
  29. package/dist/runtime/query/iteration/index.js +3 -3
  30. package/dist/runtime/query/iteration/index.js.map +1 -1
  31. package/dist/store/goal/index.d.ts +53 -3
  32. package/dist/store/goal/index.d.ts.map +1 -1
  33. package/dist/store/goal/index.js +245 -9
  34. package/dist/store/goal/index.js.map +1 -1
  35. package/dist/tools/goal/confirmation.d.ts +82 -1
  36. package/dist/tools/goal/confirmation.d.ts.map +1 -1
  37. package/dist/tools/goal/confirmation.js +153 -3
  38. package/dist/tools/goal/confirmation.js.map +1 -1
  39. package/dist/tools/goal/index.d.ts.map +1 -1
  40. package/dist/tools/goal/index.js +34 -3
  41. package/dist/tools/goal/index.js.map +1 -1
  42. package/dist/types/command/index.d.ts +26 -2
  43. package/dist/types/command/index.d.ts.map +1 -1
  44. package/dist/types/command/index.js +5 -2
  45. package/dist/types/command/index.js.map +1 -1
  46. package/dist/types/common/index.d.ts +4 -2
  47. package/dist/types/common/index.d.ts.map +1 -1
  48. package/dist/types/common/index.js.map +1 -1
  49. package/dist/types/goal/index.d.ts +59 -0
  50. package/dist/types/goal/index.d.ts.map +1 -1
  51. package/dist/types/provider/chat.d.ts +10 -5
  52. package/dist/types/provider/chat.d.ts.map +1 -1
  53. package/dist/types/provider/interface.d.ts +7 -0
  54. package/dist/types/provider/interface.d.ts.map +1 -1
  55. package/dist/types/provider/model.d.ts +5 -0
  56. package/dist/types/provider/model.d.ts.map +1 -1
  57. package/dist/types/session/config.d.ts +2 -2
  58. package/dist/types/session/outcome.d.ts +5 -2
  59. package/dist/types/session/outcome.d.ts.map +1 -1
  60. package/dist/types/session/outcome.js.map +1 -1
  61. package/dist/utils/cost.d.ts +1 -1
  62. package/dist/utils/cost.d.ts.map +1 -1
  63. package/dist/utils/cost.js +14 -5
  64. package/dist/utils/cost.js.map +1 -1
  65. package/package.json +1 -1
  66. package/src/pricing/catalogue.generated.ts +6 -0
  67. package/src/pricing/rates.source.json +13 -7
  68. package/src/provider/fallback.ts +11 -0
  69. package/src/provider/idle-timeout.ts +5 -0
  70. package/src/provider/model-window-suffix.ts +1 -0
  71. package/src/provider/retry.ts +5 -0
  72. package/src/public-runtime.ts +9 -0
  73. package/src/public-types.ts +7 -0
  74. package/src/registry/command/kernel-commands.ts +136 -49
  75. package/src/runtime/query/iteration/index.ts +9 -6
  76. package/src/store/goal/index.ts +320 -21
  77. package/src/tools/goal/confirmation.ts +227 -3
  78. package/src/tools/goal/index.ts +40 -4
  79. package/src/types/command/index.ts +27 -2
  80. package/src/types/common/index.ts +4 -2
  81. package/src/types/goal/index.ts +61 -0
  82. package/src/types/provider/chat.ts +10 -5
  83. package/src/types/provider/interface.ts +8 -0
  84. package/src/types/provider/model.ts +5 -0
  85. package/src/types/session/config.ts +2 -2
  86. package/src/types/session/outcome.ts +3 -1
  87. package/src/utils/cost.ts +15 -6
@@ -6,9 +6,11 @@ import {
6
6
  GoalTransitionError,
7
7
  type SessionGoalStore,
8
8
  StaleGoalError,
9
+ goalCostText,
10
+ goalTokensText,
9
11
  } from '../../store/goal/index.js'
10
12
  import type { HostCommandDescriptor } from '../../types/command/index.js'
11
- import type { GoalConfirmedBy, GoalRef, SessionGoal } from '../../types/goal/index.js'
13
+ import type { GoalConfirmedBy, GoalGuards, GoalRef, SessionGoal } from '../../types/goal/index.js'
12
14
  import type { SessionId, TenantId } from '../../types/ids/index.js'
13
15
  import type { Skill } from '../../types/skills/index.js'
14
16
  import { isInvocableBy, skillInvocation } from '../../types/skills/index.js'
@@ -50,10 +52,21 @@ export interface GoalCommandScope {
50
52
  readonly activation?: SessionGoalActivation
51
53
  /** What a goal that does not choose is confirmed by; shown by `/goal status`. Default `model`. */
52
54
  readonly defaultConfirmedBy?: GoalConfirmedBy
55
+ /**
56
+ * What a goal created here gets when the command does not say: the round cap
57
+ * and the stop rules the host applies. Absent leaves the kernel's own cap and
58
+ * no stop rules.
59
+ */
60
+ readonly defaults?: GoalCommandDefaults
61
+ }
62
+
63
+ export interface GoalCommandDefaults {
64
+ readonly maxGoalRounds?: number
65
+ readonly guards?: GoalGuards
53
66
  }
54
67
 
55
68
  const GOAL_ARGUMENTS =
56
- '[status|set [--confirmed-by record|model] <objective>|<objective>|edit <objective>|pause|resume|clear]'
69
+ '[status|set [--confirmed-by record|model] [--token-budget <n>] [--max-rounds <n>] <objective>|<objective>|edit <objective>|pause|resume|clear]'
57
70
  const GOAL_USAGE = `Usage: /goal ${GOAL_ARGUMENTS}`
58
71
 
59
72
  type ParsedGoalCommand =
@@ -81,58 +94,85 @@ function parseGoalCommand(args: readonly string[]): ParsedGoalCommand {
81
94
  return { kind: 'create', objective: input }
82
95
  }
83
96
 
84
- type ConfirmedByFlag =
85
- | {
86
- readonly kind: 'ok'
87
- readonly args: readonly string[]
88
- readonly confirmedBy?: GoalConfirmedBy
89
- }
90
- | { readonly kind: 'invalid' }
97
+ interface GoalFlags {
98
+ readonly confirmedBy?: GoalConfirmedBy
99
+ readonly tokenBudget?: number
100
+ readonly maxRounds?: number
101
+ }
102
+
103
+ type TakenFlags =
104
+ | { readonly kind: 'ok'; readonly args: readonly string[]; readonly flags: GoalFlags }
105
+ | { readonly kind: 'invalid'; readonly flag: string }
106
+
107
+ const GOAL_FLAG_NAMES = ['--confirmed-by', '--token-budget', '--max-rounds'] as const
108
+ type GoalFlagName = (typeof GOAL_FLAG_NAMES)[number]
109
+
110
+ /** `--name value` or `--name=value` at `args[i]`; the flag, its value and how many words it took. */
111
+ function flagAt(
112
+ args: readonly string[],
113
+ i: number,
114
+ ): { readonly name: GoalFlagName; readonly value: string; readonly width: number } | undefined {
115
+ const arg = args[i] ?? ''
116
+ for (const name of GOAL_FLAG_NAMES) {
117
+ if (arg === name) return { name, value: args[i + 1] ?? '', width: 2 }
118
+ if (arg.startsWith(`${name}=`)) return { name, value: arg.slice(name.length + 1), width: 1 }
119
+ }
120
+ return undefined
121
+ }
122
+
123
+ function applyFlag(flags: GoalFlags, name: GoalFlagName, value: string): GoalFlags | undefined {
124
+ if (name === '--confirmed-by') {
125
+ return value === 'model' || value === 'record' ? { ...flags, confirmedBy: value } : undefined
126
+ }
127
+ if (!/^[1-9][0-9]{0,15}$/u.test(value)) return undefined
128
+ const n = Number(value)
129
+ if (!Number.isSafeInteger(n)) return undefined
130
+ return name === '--token-budget' ? { ...flags, tokenBudget: n } : { ...flags, maxRounds: n }
131
+ }
91
132
 
92
133
  /**
93
- * Takes `--confirmed-by <model|record>` (or `=`) out of the arguments, but only
94
- * where a flag stands: at the front, right after `set`, or closing the command. In the middle the words
95
- * belong to the objective, so a goal about the flag itself keeps its text.
134
+ * Takes `--confirmed-by <model|record>`, `--token-budget <n>` and `--max-rounds
135
+ * <n>` (or `=`) out of the arguments, but only where a flag stands: at the
136
+ * front, right after `set`, or closing the command. In the middle the words
137
+ * belong to the objective, so a goal about a flag itself keeps its text.
96
138
  */
97
- function takeConfirmedBy(args: readonly string[]): ConfirmedByFlag {
139
+ function takeGoalFlags(args: readonly string[]): TakenFlags {
98
140
  const head: string[] = []
99
141
  let i = 0
100
142
  if ((args[0] ?? '').toLowerCase() === 'set') {
101
143
  head.push(args[0] ?? '')
102
144
  i = 1
103
145
  }
104
- let found: GoalConfirmedBy | undefined
105
- for (; i < args.length; i++) {
106
- const arg = args[i] ?? ''
107
- let value: string
108
- if (arg === '--confirmed-by') {
109
- value = args[i + 1] ?? ''
110
- i++
111
- } else if (arg.startsWith('--confirmed-by=')) {
112
- value = arg.slice('--confirmed-by='.length)
146
+ let flags: GoalFlags = {}
147
+ for (;;) {
148
+ const found = flagAt(args, i)
149
+ if (!found) break
150
+ const next = applyFlag(flags, found.name, found.value)
151
+ if (!next) return { kind: 'invalid', flag: found.name }
152
+ flags = next
153
+ i += found.width
154
+ }
155
+ let tail = args.slice(i)
156
+ // Flags may also close the command; in the middle their words are the objective's.
157
+ for (;;) {
158
+ const last = tail.length - 1
159
+ const one = flagAt(tail, last)
160
+ const two = last >= 1 ? flagAt(tail, last - 1) : undefined
161
+ if (one && one.width === 1) {
162
+ const next = applyFlag(flags, one.name, one.value)
163
+ if (!next) return { kind: 'invalid', flag: one.name }
164
+ flags = next
165
+ tail = tail.slice(0, -1)
166
+ } else if (two && two.width === 2) {
167
+ const next = applyFlag(flags, two.name, two.value)
168
+ if (!next) return { kind: 'invalid', flag: two.name }
169
+ flags = next
170
+ tail = tail.slice(0, -2)
113
171
  } else {
114
172
  break
115
173
  }
116
- if (value !== 'model' && value !== 'record') return { kind: 'invalid' }
117
- found = value
118
- }
119
- let tail = args.slice(i)
120
- // A flag may also close the command; in the middle its words are the objective's.
121
- const last = tail[tail.length - 1] ?? ''
122
- const beforeLast = tail[tail.length - 2] ?? ''
123
- let closing: string | undefined
124
- if (last.startsWith('--confirmed-by=')) {
125
- closing = last.slice('--confirmed-by='.length)
126
- tail = tail.slice(0, -1)
127
- } else if (beforeLast === '--confirmed-by') {
128
- closing = last
129
- tail = tail.slice(0, -2)
130
- }
131
- if (closing !== undefined) {
132
- if (closing !== 'model' && closing !== 'record') return { kind: 'invalid' }
133
- found = closing
134
174
  }
135
- return { kind: 'ok', args: [...head, ...tail], ...(found ? { confirmedBy: found } : {}) }
175
+ return { kind: 'ok', args: [...head, ...tail], flags }
136
176
  }
137
177
 
138
178
  function goalRef(goal: SessionGoal): GoalRef {
@@ -163,10 +203,22 @@ function renderGoal(
163
203
  ? ['Confirmed by: the record of the work']
164
204
  : []),
165
205
  `Automatic continuation: ${armed ? 'enabled' : 'paused'}`,
166
- `Automatic turns: ${goal.roundsAdmitted} / ${goal.maxGoalRounds}`,
206
+ `Automatic turns: ${goal.roundsAdmitted}, up to ${goal.maxGoalRounds}`,
207
+ `Tokens: ${goalTokensText(goal)}`,
208
+ `Cost: ${goalCostText(goal)}`,
209
+ ...(goal.guards?.tokenBudget !== undefined
210
+ ? [`Token budget: ${goal.guards.tokenBudget.toLocaleString('en-US')}`]
211
+ : []),
212
+ ...(goal.guards?.checkInEvery
213
+ ? [`Check-in: every ${goal.guards.checkInEvery} automatic turns`]
214
+ : []),
215
+ ...(goal.pauseReason ? [`Paused: ${goal.pauseReason.message}`] : []),
167
216
  ...(goal.blockedReason
168
217
  ? [`Blocker: ${goal.blockedReason.code}: ${goal.blockedReason.message}`]
169
218
  : []),
219
+ ...(goal.missing && goal.missing.length > 0
220
+ ? [`Still missing: ${goal.missing.join('; ')}`]
221
+ : []),
170
222
  `Objective: ${goal.objective}`,
171
223
  '',
172
224
  `Commands: ${goalCommands(goal, armed)}`,
@@ -196,18 +248,22 @@ async function runGoalCommand(scope: GoalCommandScope | undefined, args: readonl
196
248
  reason: 'Goals need a durable session. This host did not supply one for the current command.',
197
249
  }
198
250
  }
199
- const flag = takeConfirmedBy(args)
200
- if (flag.kind === 'invalid') {
251
+ const taken = takeGoalFlags(args)
252
+ if (taken.kind === 'invalid') {
201
253
  return {
202
254
  kind: 'refused' as const,
203
- reason: `--confirmed-by takes record or model.\n${GOAL_USAGE}`,
255
+ reason:
256
+ taken.flag === '--confirmed-by'
257
+ ? `--confirmed-by takes record or model.\n${GOAL_USAGE}`
258
+ : `${taken.flag} takes a whole number of at least 1.\n${GOAL_USAGE}`,
204
259
  }
205
260
  }
206
- const command = parseGoalCommand(flag.args)
207
- if (flag.confirmedBy !== undefined && command.kind !== 'create') {
261
+ const flag = taken.flags
262
+ const command = parseGoalCommand(taken.args)
263
+ if (Object.keys(flag).length > 0 && command.kind !== 'create') {
208
264
  return {
209
265
  kind: 'refused' as const,
210
- reason: `--confirmed-by is chosen when a goal is set.\n${GOAL_USAGE}`,
266
+ reason: `Flags such as --confirmed-by, --token-budget and --max-rounds are chosen when a goal is set.\n${GOAL_USAGE}`,
211
267
  }
212
268
  }
213
269
  try {
@@ -237,10 +293,18 @@ async function runGoalCommand(scope: GoalCommandScope | undefined, args: readonl
237
293
  reason: `A goal is already ${current.phase}. Use /goal edit <objective> to change it or /goal clear before replacing it.`,
238
294
  }
239
295
  }
296
+ const defaults = scope.defaults
297
+ const guards: GoalGuards = {
298
+ ...defaults?.guards,
299
+ ...(flag.tokenBudget !== undefined ? { tokenBudget: flag.tokenBudget } : {}),
300
+ }
301
+ const maxGoalRounds = flag.maxRounds ?? defaults?.maxGoalRounds
240
302
  const created = await scope.store.createGoal(
241
303
  {
242
304
  sessionId: scope.sessionId,
243
305
  objective: command.objective,
306
+ ...(maxGoalRounds !== undefined ? { maxGoalRounds } : {}),
307
+ ...(Object.keys(guards).length > 0 ? { guards } : {}),
244
308
  ...(flag.confirmedBy !== undefined ? { confirmedBy: flag.confirmedBy } : {}),
245
309
  },
246
310
  scope.tenantId,
@@ -255,7 +319,14 @@ async function runGoalCommand(scope: GoalCommandScope | undefined, args: readonl
255
319
  if (!current) return missingGoal('edit')
256
320
  if (current.phase === 'complete') {
257
321
  const replacement = await scope.store.createGoal(
258
- { sessionId: scope.sessionId, objective: command.objective },
322
+ {
323
+ sessionId: scope.sessionId,
324
+ objective: command.objective,
325
+ ...(scope.defaults?.maxGoalRounds !== undefined
326
+ ? { maxGoalRounds: scope.defaults.maxGoalRounds }
327
+ : {}),
328
+ ...(scope.defaults?.guards ? { guards: scope.defaults.guards } : {}),
329
+ },
259
330
  scope.tenantId,
260
331
  )
261
332
  scope.activation?.arm(replacement)
@@ -299,10 +370,20 @@ async function runGoalCommand(scope: GoalCommandScope | undefined, args: readonl
299
370
  message: renderScopedGoal('Goal resumed', current, scope),
300
371
  }
301
372
  }
373
+ // A goal stopped by a limit goes on when the setting now allows more.
302
374
  const resumed = await scope.store.resumeGoal(
303
375
  scope.sessionId,
304
376
  scope.tenantId,
305
377
  goalRef(current),
378
+ {
379
+ ...(scope.defaults?.maxGoalRounds !== undefined
380
+ ? { maxGoalRounds: scope.defaults.maxGoalRounds }
381
+ : {}),
382
+ ...(scope.defaults?.guards?.tokenBudget !== undefined
383
+ ? { tokenBudget: scope.defaults.guards.tokenBudget }
384
+ : {}),
385
+ ...(scope.defaults?.guards ? { guards: scope.defaults.guards } : {}),
386
+ },
306
387
  )
307
388
  scope.activation?.arm(resumed)
308
389
  return {
@@ -346,12 +427,17 @@ export function kernelHostCommands(options: KernelCommandOptions): HostCommandDe
346
427
  name: 'goal',
347
428
  description: 'Persist or inspect a completion goal for this conversation.',
348
429
  hint: GOAL_ARGUMENTS,
430
+ // Setting, editing, pausing, resuming and clearing change what the
431
+ // conversation does next; only reading the goal runs unasked.
432
+ modelRun: 'confirm',
433
+ modelRunReads: ['', 'status'],
349
434
  handler: async ({ args }) => await runGoalCommand(options.goal, args),
350
435
  },
351
436
  {
352
437
  name: 'skills',
353
438
  description: 'List the skills an operator may invoke.',
354
439
  hint: 'name, description and policy for every operator-invocable skill',
440
+ modelRun: 'run',
355
441
  handler() {
356
442
  // Operator-invocable only, which is the point of the split: the
357
443
  // model's manifest and the operator's menu are different lists
@@ -381,6 +467,7 @@ export function kernelHostCommands(options: KernelCommandOptions): HostCommandDe
381
467
  name: 'agents',
382
468
  description: 'List the agents this turn may delegate to.',
383
469
  hint: 'the roster, as the delegation tools see it',
470
+ modelRun: 'run',
384
471
  handler() {
385
472
  // An empty roster IS the answer here, unlike the store above: the
386
473
  // question is "who may I call", and "nobody" is a complete and
@@ -2117,7 +2117,7 @@ export class IterationOrchestrator {
2117
2117
  }
2118
2118
  const endNote =
2119
2119
  !forceFinalize && closingStopReason === undefined
2120
- ? await this.askBeforeEnd()
2120
+ ? await this.askBeforeEnd(response.message.content ?? '')
2121
2121
  : undefined
2122
2122
  if (endNote !== undefined) {
2123
2123
  recorder.pushMessage(createRuntimeContextMessage(endNote, 'progress-nudge'))
@@ -3617,14 +3617,17 @@ export class IterationOrchestrator {
3617
3617
  * for the model, or `undefined` to let the turn end. Bounded, and a hook that
3618
3618
  * fails never fails the turn.
3619
3619
  */
3620
- async askBeforeEnd(): Promise<string | undefined> {
3620
+ async askBeforeEnd(finalMessage = ''): Promise<string | undefined> {
3621
3621
  const hook = this.ctx.turnConfig.outcome?.beforeEnd
3622
3622
  if (hook === undefined || this.beforeEndNotes >= MAX_BEFORE_END_NOTES) return undefined
3623
3623
  try {
3624
- const note = await hook(async () => {
3625
- this.hydrateOutcome()
3626
- return await this.outcomeLedger.preview()
3627
- })
3624
+ const note = await hook(
3625
+ async () => {
3626
+ this.hydrateOutcome()
3627
+ return await this.outcomeLedger.preview()
3628
+ },
3629
+ { finalMessage },
3630
+ )
3628
3631
  if (typeof note !== 'string' || note.trim() === '') return undefined
3629
3632
  this.beforeEndNotes += 1
3630
3633
  return note