@kindgi/agents 0.1.2 → 0.1.4-rc.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 (159) hide show
  1. package/dist/blocks.d.ts +101 -0
  2. package/dist/blocks.d.ts.map +1 -0
  3. package/dist/blocks.js +149 -0
  4. package/dist/blocks.js.map +1 -0
  5. package/dist/conversation-binding.d.ts +9 -1
  6. package/dist/conversation-binding.d.ts.map +1 -1
  7. package/dist/define.d.ts +17 -2
  8. package/dist/define.d.ts.map +1 -1
  9. package/dist/define.js +93 -6
  10. package/dist/define.js.map +1 -1
  11. package/dist/guardrails-gate.d.ts +7 -4
  12. package/dist/guardrails-gate.d.ts.map +1 -1
  13. package/dist/guardrails-gate.js +5 -2
  14. package/dist/guardrails-gate.js.map +1 -1
  15. package/dist/handlers/compose-result.d.ts.map +1 -1
  16. package/dist/handlers/compose-result.js +3 -0
  17. package/dist/handlers/compose-result.js.map +1 -1
  18. package/dist/handlers/context.d.ts +24 -0
  19. package/dist/handlers/context.d.ts.map +1 -1
  20. package/dist/handlers/dispatch-tools.d.ts.map +1 -1
  21. package/dist/handlers/dispatch-tools.js +69 -43
  22. package/dist/handlers/dispatch-tools.js.map +1 -1
  23. package/dist/handlers/errors.d.ts +12 -1
  24. package/dist/handlers/errors.d.ts.map +1 -1
  25. package/dist/handlers/errors.js.map +1 -1
  26. package/dist/handlers/evaluate-guardrails.d.ts.map +1 -1
  27. package/dist/handlers/evaluate-guardrails.js +36 -1
  28. package/dist/handlers/evaluate-guardrails.js.map +1 -1
  29. package/dist/handlers/gate-decision.d.ts +62 -0
  30. package/dist/handlers/gate-decision.d.ts.map +1 -0
  31. package/dist/handlers/gate-decision.js +55 -0
  32. package/dist/handlers/gate-decision.js.map +1 -0
  33. package/dist/handlers/model-call.d.ts +10 -0
  34. package/dist/handlers/model-call.d.ts.map +1 -1
  35. package/dist/handlers/model-call.js +120 -34
  36. package/dist/handlers/model-call.js.map +1 -1
  37. package/dist/handlers/persist-provenance.d.ts.map +1 -1
  38. package/dist/handlers/persist-provenance.js +3 -1
  39. package/dist/handlers/persist-provenance.js.map +1 -1
  40. package/dist/handlers/persist-user-message.d.ts.map +1 -1
  41. package/dist/handlers/persist-user-message.js +5 -16
  42. package/dist/handlers/persist-user-message.js.map +1 -1
  43. package/dist/handlers/public-types.d.ts +37 -3
  44. package/dist/handlers/public-types.d.ts.map +1 -1
  45. package/dist/handlers/rehydrate.d.ts.map +1 -1
  46. package/dist/handlers/rehydrate.js +91 -1
  47. package/dist/handlers/rehydrate.js.map +1 -1
  48. package/dist/handlers/render-prompt.d.ts.map +1 -1
  49. package/dist/handlers/render-prompt.js +4 -1
  50. package/dist/handlers/render-prompt.js.map +1 -1
  51. package/dist/handlers/replay.d.ts +143 -0
  52. package/dist/handlers/replay.d.ts.map +1 -0
  53. package/dist/handlers/replay.js +177 -0
  54. package/dist/handlers/replay.js.map +1 -0
  55. package/dist/handlers/resolve-blocks.d.ts +32 -0
  56. package/dist/handlers/resolve-blocks.d.ts.map +1 -0
  57. package/dist/handlers/resolve-blocks.js +129 -0
  58. package/dist/handlers/resolve-blocks.js.map +1 -0
  59. package/dist/handlers/resolve-tools.d.ts +6 -2
  60. package/dist/handlers/resolve-tools.d.ts.map +1 -1
  61. package/dist/handlers/resolve-tools.js +59 -27
  62. package/dist/handlers/resolve-tools.js.map +1 -1
  63. package/dist/handlers/result-shape.d.ts +7 -0
  64. package/dist/handlers/result-shape.d.ts.map +1 -1
  65. package/dist/handlers/result-shape.js.map +1 -1
  66. package/dist/handlers/run-retrievals.d.ts.map +1 -1
  67. package/dist/handlers/run-retrievals.js +35 -35
  68. package/dist/handlers/run-retrievals.js.map +1 -1
  69. package/dist/handlers/run-snapshot.d.ts.map +1 -1
  70. package/dist/handlers/run-snapshot.js +1 -0
  71. package/dist/handlers/run-snapshot.js.map +1 -1
  72. package/dist/handlers/setup.d.ts +2 -0
  73. package/dist/handlers/setup.d.ts.map +1 -1
  74. package/dist/handlers/setup.js +29 -12
  75. package/dist/handlers/setup.js.map +1 -1
  76. package/dist/handlers/tool-hitl.d.ts +5 -9
  77. package/dist/handlers/tool-hitl.d.ts.map +1 -1
  78. package/dist/handlers/tool-hitl.js +5 -0
  79. package/dist/handlers/tool-hitl.js.map +1 -1
  80. package/dist/handlers/turn-environment.d.ts +13 -2
  81. package/dist/handlers/turn-environment.d.ts.map +1 -1
  82. package/dist/handlers/turn-environment.js +12 -3
  83. package/dist/handlers/turn-environment.js.map +1 -1
  84. package/dist/handlers/turn-provenance.d.ts +67 -0
  85. package/dist/handlers/turn-provenance.d.ts.map +1 -0
  86. package/dist/handlers/turn-provenance.js +160 -0
  87. package/dist/handlers/turn-provenance.js.map +1 -0
  88. package/dist/index.d.ts +10 -1
  89. package/dist/index.d.ts.map +1 -1
  90. package/dist/index.js +5 -0
  91. package/dist/index.js.map +1 -1
  92. package/dist/invoke.d.ts +14 -1
  93. package/dist/invoke.d.ts.map +1 -1
  94. package/dist/invoke.js +37 -19
  95. package/dist/invoke.js.map +1 -1
  96. package/dist/pins.d.ts +54 -0
  97. package/dist/pins.d.ts.map +1 -0
  98. package/dist/pins.js +51 -0
  99. package/dist/pins.js.map +1 -0
  100. package/dist/prompt.d.ts +12 -2
  101. package/dist/prompt.d.ts.map +1 -1
  102. package/dist/prompt.js +41 -4
  103. package/dist/prompt.js.map +1 -1
  104. package/dist/provenance-emit.d.ts +4 -2
  105. package/dist/provenance-emit.d.ts.map +1 -1
  106. package/dist/provenance-emit.js +4 -2
  107. package/dist/provenance-emit.js.map +1 -1
  108. package/dist/run-snapshot-binding.d.ts +5 -0
  109. package/dist/run-snapshot-binding.d.ts.map +1 -1
  110. package/dist/schema.d.ts +34 -0
  111. package/dist/schema.d.ts.map +1 -1
  112. package/dist/schema.js +16 -0
  113. package/dist/schema.js.map +1 -1
  114. package/dist/streaming.d.ts +5 -0
  115. package/dist/streaming.d.ts.map +1 -1
  116. package/dist/streaming.js.map +1 -1
  117. package/dist/types.d.ts +65 -2
  118. package/dist/types.d.ts.map +1 -1
  119. package/migrations/0003_hesitant_captain_cross.sql +2 -0
  120. package/migrations/0004_stormy_moondragon.sql +1 -0
  121. package/migrations/meta/0003_snapshot.json +321 -0
  122. package/migrations/meta/0004_snapshot.json +327 -0
  123. package/migrations/meta/_journal.json +14 -0
  124. package/package.json +15 -15
  125. package/src/blocks.ts +231 -0
  126. package/src/conversation-binding.ts +9 -1
  127. package/src/define.ts +104 -7
  128. package/src/guardrails-gate.ts +17 -3
  129. package/src/handlers/compose-result.ts +3 -0
  130. package/src/handlers/context.ts +24 -0
  131. package/src/handlers/dispatch-tools.ts +75 -44
  132. package/src/handlers/errors.ts +13 -0
  133. package/src/handlers/evaluate-guardrails.ts +41 -1
  134. package/src/handlers/gate-decision.ts +101 -0
  135. package/src/handlers/model-call.ts +163 -33
  136. package/src/handlers/persist-provenance.ts +3 -1
  137. package/src/handlers/persist-user-message.ts +3 -16
  138. package/src/handlers/public-types.ts +38 -3
  139. package/src/handlers/rehydrate.ts +141 -9
  140. package/src/handlers/render-prompt.ts +13 -7
  141. package/src/handlers/replay.ts +314 -0
  142. package/src/handlers/resolve-blocks.ts +188 -0
  143. package/src/handlers/resolve-tools.ts +76 -28
  144. package/src/handlers/result-shape.ts +8 -0
  145. package/src/handlers/run-retrievals.ts +45 -41
  146. package/src/handlers/run-snapshot.ts +1 -0
  147. package/src/handlers/setup.ts +30 -22
  148. package/src/handlers/tool-hitl.ts +6 -10
  149. package/src/handlers/turn-environment.ts +28 -3
  150. package/src/handlers/turn-provenance.ts +230 -0
  151. package/src/index.ts +44 -0
  152. package/src/invoke.ts +49 -20
  153. package/src/pins.ts +98 -0
  154. package/src/prompt.ts +52 -4
  155. package/src/provenance-emit.ts +4 -1
  156. package/src/run-snapshot-binding.ts +6 -0
  157. package/src/schema.ts +16 -0
  158. package/src/streaming.ts +5 -0
  159. package/src/types.ts +68 -2
@@ -6,6 +6,7 @@ import type {
6
6
  Provenance,
7
7
  ProvenanceBuilder,
8
8
  ProvenanceEmitBinding,
9
+ ProvenanceEmitContext,
9
10
  } from '@kindgi/provenance';
10
11
  import { signProvenance } from '@kindgi/provenance';
11
12
  import type { Result } from '@kindgi/types';
@@ -55,6 +56,8 @@ export interface ProvenanceBindings {
55
56
  export async function persistProvenance(
56
57
  builder: ProvenanceBuilder,
57
58
  bindings: ProvenanceBindings,
59
+ /** Stored beside the record (the turn's project); see `ProvenanceEmitContext`. */
60
+ context?: ProvenanceEmitContext,
58
61
  ): Promise<Result<Provenance, PersistenceError>> {
59
62
  let provenance: Provenance = builder.snapshot();
60
63
  if (bindings.keyProvider !== undefined) {
@@ -84,7 +87,7 @@ export async function persistProvenance(
84
87
  },
85
88
  };
86
89
  }
87
- const emitted = await bindings.emitBinding.emit(provenance);
90
+ const emitted = await bindings.emitBinding.emit(provenance, context);
88
91
  if (emitted.kind === 'err') {
89
92
  return {
90
93
  kind: 'err',
@@ -3,6 +3,8 @@
3
3
 
4
4
  import type { ProjectId, Result, RunId, Semver, TenantId, Timestamp } from '@kindgi/types';
5
5
 
6
+ import type { RunReplayRef } from '@kindgi/runtime';
7
+
6
8
  import type { PersistenceError } from './errors.js';
7
9
  import type { AgentId, ConversationId } from './types.js';
8
10
 
@@ -37,6 +39,8 @@ export interface RunSnapshotWriteInput {
37
39
  * as `principal`.
38
40
  */
39
41
  readonly authz?: unknown;
42
+ /** The turn's replay marker (`InvokeAgentInput.replay`): a resumed replay stays one. */
43
+ readonly replay?: RunReplayRef;
40
44
  }
41
45
 
42
46
  /**
@@ -59,6 +63,8 @@ export interface RunSnapshotRecord {
59
63
  readonly dryRun: boolean;
60
64
  readonly principal?: unknown;
61
65
  readonly authz?: unknown;
66
+ /** The turn's replay marker (`InvokeAgentInput.replay`): a resumed replay stays one. */
67
+ readonly replay?: RunReplayRef;
62
68
  readonly createdAt: Timestamp;
63
69
  }
64
70
 
package/src/schema.ts CHANGED
@@ -43,6 +43,13 @@ export const agentConversations = pgTable(
43
43
  * SSO subject). Kept as text so the caller decides the shape.
44
44
  */
45
45
  participantId: text('participant_id'),
46
+ /**
47
+ * The project the conversation is in, set when it is opened (a run
48
+ * opens it in the run's project). Null when opened without one, and
49
+ * on conversations from before the column (nothing is backfilled).
50
+ * No foreign key: `projects` belongs to the runtime's schema.
51
+ */
52
+ projectId: uuid('project_id'),
46
53
  /**
47
54
  * Structural scope — project id, matter id, etc. Persisted through
48
55
  * the versioning envelope so shape evolution is migrate-on-read.
@@ -74,6 +81,10 @@ export const agentConversations = pgTable(
74
81
  t.tenantId,
75
82
  t.participantId,
76
83
  ),
84
+ /** One project's conversations, newest first (`listConversationsPage({ scope })`). */
85
+ tenantProjectOpenedIdx: index('agent_conversations_tenant_project_opened_idx')
86
+ .on(t.tenantId, t.projectId, t.openedAt.desc())
87
+ .where(sql`${t.projectId} IS NOT NULL`),
77
88
  /** Partial index: open conversations sorted by recency. Used by the UI list. */
78
89
  openRecentIdx: index('agent_conversations_open_recent_idx')
79
90
  .on(t.tenantId, t.lastMessageAt.desc())
@@ -137,6 +148,11 @@ export const agentRunSnapshots = pgTable(
137
148
  * Threaded back into the resumed TurnContext.
138
149
  */
139
150
  authz: jsonb('authz'),
151
+ /**
152
+ * The replay marker (`{of, evalRunId}`) of a replay turn, so a resumed
153
+ * replay stays one: its tool calls are still decided by the replay rules.
154
+ */
155
+ replay: jsonb('replay'),
140
156
  createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
141
157
  },
142
158
  (t) => ({
package/src/streaming.ts CHANGED
@@ -86,6 +86,11 @@ export interface ToolCompletedEvent {
86
86
  readonly invocationId: string;
87
87
  readonly output: unknown;
88
88
  readonly durationMs: number;
89
+ /**
90
+ * In a replay turn: whether the tool ran (`live`), the past run's result
91
+ * was used (`recorded`), or the call was refused (`refused`).
92
+ */
93
+ readonly replay?: 'live' | 'recorded' | 'refused';
89
94
  }
90
95
 
91
96
  export interface ToolFailedEvent {
package/src/types.ts CHANGED
@@ -4,7 +4,9 @@
4
4
  import type { Capability } from '@kindgi/capabilities';
5
5
  import type { Fact, MemoryScope } from '@kindgi/memory';
6
6
  import type { ToolErrorsSpec, ToolHitlMode, ToolHitlRule } from '@kindgi/policy-contract';
7
- import type { Brand, ConversationId, Semver, TenantId, Timestamp } from '@kindgi/types';
7
+ import type { Brand, ConversationId, ProjectId, Semver, TenantId, Timestamp } from '@kindgi/types';
8
+
9
+ import type { AgentDerivation, AgentPins } from './pins.js';
8
10
 
9
11
  /**
10
12
  * Branded agent id. Convention: dotted namespace under the tenant's
@@ -121,6 +123,22 @@ export interface ToolRef {
121
123
  readonly version: string;
122
124
  }
123
125
 
126
+ /**
127
+ * A prompt block an agent's instructions come from: its id and a semver
128
+ * range, resolved like a tool's (`pickVersion`) and pinned when the
129
+ * agent version is published.
130
+ */
131
+ export interface PromptRef {
132
+ readonly prompt: string;
133
+ readonly version: string;
134
+ }
135
+
136
+ /** A settings block an agent reads: its id and a semver range, pinned at publish. */
137
+ export interface BlockRef {
138
+ readonly id: string;
139
+ readonly version: string;
140
+ }
141
+
124
142
  /**
125
143
  * Per-agent behavior for multi-turn conversations. The agent chooses:
126
144
  * - How many prior messages to load (`historyLimit`; unset = all).
@@ -204,6 +222,14 @@ export interface ConversationHitlPolicy {
204
222
  * (see `resolveEffectiveHitlPolicy`).
205
223
  */
206
224
  readonly timeoutMs?: number;
225
+ /**
226
+ * What happens when an approval's time runs out: it escalates one
227
+ * reviewer tier, and at `admin` it expires (the turn fails with
228
+ * `hitl-cancelled`). `'escalate'` is the only behavior today, and the
229
+ * default; `'auto-approve'` and `'auto-reject'` are refused rather than
230
+ * accepted and ignored.
231
+ */
232
+ readonly onTimeout?: 'escalate';
207
233
  }
208
234
 
209
235
  /**
@@ -284,8 +310,13 @@ export interface Agent {
284
310
  * unresolved reference fails the turn at invoke time
285
311
  * (`model-invocation-failed` whose `cause` is the `missing-parameter`
286
312
  * render error), never a silent empty string.
313
+ *
314
+ * Or a prompt block, by range (`{ prompt: 'acme.intake-prompt',
315
+ * version: '^1.0.0' }`): its template renders here instead, with the
316
+ * parameters it declares, and the version that runs is pinned when the
317
+ * agent version is published (`pins.prompts`).
287
318
  */
288
- readonly instructions: string;
319
+ readonly instructions: string | PromptRef;
289
320
  /**
290
321
  * Typed parameters the caller supplies at invoke time. The UI reads
291
322
  * this to build a "configure agent" form; the runtime validates each
@@ -397,6 +428,35 @@ export interface Agent {
397
428
  * A tenant's `tool-errors` policy can lower it. See `ToolErrorsSpec`.
398
429
  */
399
430
  readonly toolErrors?: ToolErrorsSpec;
431
+ /**
432
+ * The exact block versions this agent version runs, resolved by the
433
+ * runtime when the version was published (see `AgentPins`). Never
434
+ * authored: `defineAgent` doesn't take it. Absent on an agent defined
435
+ * in code and on a version published before pins existed; its tool
436
+ * ranges then resolve per run.
437
+ */
438
+ readonly pins?: AgentPins;
439
+ /**
440
+ * Settings blocks the agent reads, by range. Each block's values reach
441
+ * its tools as `ToolContext.settings[<block id>]` and its templates as
442
+ * `settings.<block id>.<key>`; the versions are pinned at publish
443
+ * (`pins.settings`).
444
+ */
445
+ readonly settings?: readonly BlockRef[];
446
+ /**
447
+ * A settings block of model settings (`MODEL_SETTINGS_SCHEMA`:
448
+ * `temperature`, `maxOutputTokens`) the turn's model calls use. Pinned
449
+ * at publish with the other settings.
450
+ */
451
+ readonly modelSettings?: BlockRef;
452
+ /** `pinsDigest(pins)`, recorded when the version was published. */
453
+ readonly pinsDigest?: string;
454
+ /**
455
+ * Set by the runtime on a version it registered under another number
456
+ * than the definition's, when a deploy couldn't register that number
457
+ * as it was (see `AgentDerivation`). Never authored.
458
+ */
459
+ readonly derivedFrom?: AgentDerivation;
400
460
  }
401
461
 
402
462
  /**
@@ -416,6 +476,12 @@ export interface Conversation {
416
476
  readonly title: string;
417
477
  /** UUID or free-form identifier for the human participant. */
418
478
  readonly participantId?: string;
479
+ /**
480
+ * The project the conversation is in, set when it is opened (by a run,
481
+ * from the run's project). Absent on conversations opened without one,
482
+ * and on those from before it was stored.
483
+ */
484
+ readonly projectId?: ProjectId;
419
485
  /** Additional attributes (project id, matter id, etc.). */
420
486
  readonly scope: MemoryScope;
421
487
  readonly openedAt: Timestamp;