@kindgi/agents 0.1.4 → 0.1.5

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 (137) hide show
  1. package/README.md +1 -1
  2. package/dist/blocks.d.ts +19 -0
  3. package/dist/blocks.d.ts.map +1 -1
  4. package/dist/blocks.js +59 -1
  5. package/dist/blocks.js.map +1 -1
  6. package/dist/conversation-binding.d.ts +49 -3
  7. package/dist/conversation-binding.d.ts.map +1 -1
  8. package/dist/define.d.ts +7 -1
  9. package/dist/define.d.ts.map +1 -1
  10. package/dist/define.js +132 -7
  11. package/dist/define.js.map +1 -1
  12. package/dist/drafted-template.d.ts +34 -0
  13. package/dist/drafted-template.d.ts.map +1 -0
  14. package/dist/drafted-template.js +95 -0
  15. package/dist/drafted-template.js.map +1 -0
  16. package/dist/guardrails-gate.d.ts +28 -13
  17. package/dist/guardrails-gate.d.ts.map +1 -1
  18. package/dist/guardrails-gate.js +59 -21
  19. package/dist/guardrails-gate.js.map +1 -1
  20. package/dist/handlers/build-initial-messages.d.ts +8 -2
  21. package/dist/handlers/build-initial-messages.d.ts.map +1 -1
  22. package/dist/handlers/build-initial-messages.js +23 -21
  23. package/dist/handlers/build-initial-messages.js.map +1 -1
  24. package/dist/handlers/compose-result.d.ts.map +1 -1
  25. package/dist/handlers/compose-result.js +21 -0
  26. package/dist/handlers/compose-result.js.map +1 -1
  27. package/dist/handlers/context.d.ts +6 -1
  28. package/dist/handlers/context.d.ts.map +1 -1
  29. package/dist/handlers/dispatch-tools.d.ts.map +1 -1
  30. package/dist/handlers/dispatch-tools.js +17 -5
  31. package/dist/handlers/dispatch-tools.js.map +1 -1
  32. package/dist/handlers/errors.d.ts +14 -1
  33. package/dist/handlers/errors.d.ts.map +1 -1
  34. package/dist/handlers/errors.js.map +1 -1
  35. package/dist/handlers/evaluate-guardrails.d.ts +6 -1
  36. package/dist/handlers/evaluate-guardrails.d.ts.map +1 -1
  37. package/dist/handlers/evaluate-guardrails.js +46 -4
  38. package/dist/handlers/evaluate-guardrails.js.map +1 -1
  39. package/dist/handlers/history.d.ts +24 -0
  40. package/dist/handlers/history.d.ts.map +1 -0
  41. package/dist/handlers/history.js +52 -0
  42. package/dist/handlers/history.js.map +1 -0
  43. package/dist/handlers/persist-final-message.d.ts.map +1 -1
  44. package/dist/handlers/persist-final-message.js +2 -0
  45. package/dist/handlers/persist-final-message.js.map +1 -1
  46. package/dist/handlers/persist-user-message.d.ts.map +1 -1
  47. package/dist/handlers/persist-user-message.js +2 -0
  48. package/dist/handlers/persist-user-message.js.map +1 -1
  49. package/dist/handlers/public-types.d.ts +12 -2
  50. package/dist/handlers/public-types.d.ts.map +1 -1
  51. package/dist/handlers/rehydrate.d.ts.map +1 -1
  52. package/dist/handlers/rehydrate.js +7 -4
  53. package/dist/handlers/rehydrate.js.map +1 -1
  54. package/dist/handlers/remember-tool.d.ts +22 -0
  55. package/dist/handlers/remember-tool.d.ts.map +1 -0
  56. package/dist/handlers/remember-tool.js +156 -0
  57. package/dist/handlers/remember-tool.js.map +1 -0
  58. package/dist/handlers/replay.d.ts +55 -1
  59. package/dist/handlers/replay.d.ts.map +1 -1
  60. package/dist/handlers/replay.js +23 -6
  61. package/dist/handlers/replay.js.map +1 -1
  62. package/dist/handlers/resolve-blocks.d.ts.map +1 -1
  63. package/dist/handlers/resolve-blocks.js +19 -8
  64. package/dist/handlers/resolve-blocks.js.map +1 -1
  65. package/dist/handlers/result-shape.d.ts +10 -5
  66. package/dist/handlers/result-shape.d.ts.map +1 -1
  67. package/dist/handlers/result-shape.js.map +1 -1
  68. package/dist/handlers/run-retrievals.d.ts +2 -1
  69. package/dist/handlers/run-retrievals.d.ts.map +1 -1
  70. package/dist/handlers/run-retrievals.js +42 -16
  71. package/dist/handlers/run-retrievals.js.map +1 -1
  72. package/dist/handlers/turn-environment.d.ts.map +1 -1
  73. package/dist/handlers/turn-environment.js +2 -1
  74. package/dist/handlers/turn-environment.js.map +1 -1
  75. package/dist/handlers/turn-provenance.d.ts +19 -3
  76. package/dist/handlers/turn-provenance.d.ts.map +1 -1
  77. package/dist/handlers/turn-provenance.js +116 -2
  78. package/dist/handlers/turn-provenance.js.map +1 -1
  79. package/dist/index.d.ts +13 -8
  80. package/dist/index.d.ts.map +1 -1
  81. package/dist/index.js +5 -3
  82. package/dist/index.js.map +1 -1
  83. package/dist/invoke.d.ts +1 -1
  84. package/dist/invoke.d.ts.map +1 -1
  85. package/dist/invoke.js +2 -0
  86. package/dist/invoke.js.map +1 -1
  87. package/dist/remember.d.ts +49 -0
  88. package/dist/remember.d.ts.map +1 -0
  89. package/dist/remember.js +89 -0
  90. package/dist/remember.js.map +1 -0
  91. package/dist/retrieval.d.ts +114 -28
  92. package/dist/retrieval.d.ts.map +1 -1
  93. package/dist/retrieval.js +433 -104
  94. package/dist/retrieval.js.map +1 -1
  95. package/dist/schema.d.ts +17 -0
  96. package/dist/schema.d.ts.map +1 -1
  97. package/dist/schema.js +6 -0
  98. package/dist/schema.js.map +1 -1
  99. package/dist/streaming.d.ts +16 -1
  100. package/dist/streaming.d.ts.map +1 -1
  101. package/dist/streaming.js.map +1 -1
  102. package/dist/types.d.ts +136 -10
  103. package/dist/types.d.ts.map +1 -1
  104. package/migrations/0005_condemned_hellcat.sql +1 -0
  105. package/migrations/meta/0005_snapshot.json +333 -0
  106. package/migrations/meta/_journal.json +7 -0
  107. package/package.json +15 -15
  108. package/src/blocks.ts +75 -1
  109. package/src/conversation-binding.ts +53 -3
  110. package/src/define.ts +144 -9
  111. package/src/drafted-template.ts +118 -0
  112. package/src/guardrails-gate.ts +90 -26
  113. package/src/handlers/build-initial-messages.ts +29 -22
  114. package/src/handlers/compose-result.ts +21 -0
  115. package/src/handlers/context.ts +12 -1
  116. package/src/handlers/dispatch-tools.ts +19 -5
  117. package/src/handlers/errors.ts +16 -1
  118. package/src/handlers/evaluate-guardrails.ts +47 -4
  119. package/src/handlers/history.ts +57 -0
  120. package/src/handlers/persist-final-message.ts +2 -0
  121. package/src/handlers/persist-user-message.ts +2 -0
  122. package/src/handlers/public-types.ts +18 -2
  123. package/src/handlers/rehydrate.ts +12 -8
  124. package/src/handlers/remember-tool.ts +207 -0
  125. package/src/handlers/replay.ts +80 -8
  126. package/src/handlers/resolve-blocks.ts +21 -7
  127. package/src/handlers/result-shape.ts +19 -5
  128. package/src/handlers/run-retrievals.ts +52 -19
  129. package/src/handlers/turn-environment.ts +2 -1
  130. package/src/handlers/turn-provenance.ts +133 -2
  131. package/src/index.ts +33 -2
  132. package/src/invoke.ts +3 -0
  133. package/src/remember.ts +136 -0
  134. package/src/retrieval.ts +591 -125
  135. package/src/schema.ts +6 -0
  136. package/src/streaming.ts +17 -0
  137. package/src/types.ts +134 -10
package/src/schema.ts CHANGED
@@ -58,6 +58,12 @@ export const agentConversations = pgTable(
58
58
  openedAt: timestamp('opened_at', { withTimezone: true }).notNull().defaultNow(),
59
59
  /** Set when the conversation is closed. Closed conversations are read-only. */
60
60
  closedAt: timestamp('closed_at', { withTimezone: true }),
61
+ /**
62
+ * Set when the conversation is unregistered (a tombstone): no read,
63
+ * list or recall returns it from then on, and the retention sweep
64
+ * removes it after the tenant's grace.
65
+ */
66
+ unregisteredAt: timestamp('unregistered_at', { withTimezone: true }),
61
67
  /**
62
68
  * Denormalized turn counter — incremented by `appendMessage` for
63
69
  * each turn's final (non-intermediate) agent message. Read by the
package/src/streaming.ts CHANGED
@@ -25,6 +25,7 @@ export type TurnEvent =
25
25
  | ToolFailedEvent
26
26
  | AgentMessageEvent
27
27
  | GuardrailViolatedEvent
28
+ | GuardrailErrorEvent
28
29
  | TurnCompletedEvent
29
30
  | TurnFailedEvent;
30
31
 
@@ -117,6 +118,22 @@ export interface GuardrailViolatedEvent {
117
118
  readonly reason?: string;
118
119
  }
119
120
 
121
+ /**
122
+ * A guardrail whose check couldn't run: no such check, a bad configuration, a judge that couldn't
123
+ * be routed. A `halt` guardrail's error also fails the turn (it fails closed, as a
124
+ * `guardrail-violation` with `evaluationErrors`); with any other action the turn goes on.
125
+ */
126
+ export interface GuardrailErrorEvent {
127
+ readonly kind: 'guardrail.error';
128
+ readonly guardrailId: string;
129
+ /** The guardrail's action and severity; absent when the guardrail itself isn't known. */
130
+ readonly action?: EvaluationResult['action'];
131
+ readonly severity?: EvaluationResult['severity'];
132
+ /** Why it couldn't run: the engine's error code (`unknown-check`, `invalid-check-config`, …). */
133
+ readonly code: string;
134
+ readonly message: string;
135
+ }
136
+
120
137
  export interface TurnCompletedEvent {
121
138
  readonly kind: 'turn.completed';
122
139
  readonly conversationId: ConversationId;
package/src/types.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  // Copyright (C) 2026 Kindgi Inc.
3
3
 
4
4
  import type { Capability } from '@kindgi/capabilities';
5
- import type { Fact, MemoryScope } from '@kindgi/memory';
5
+ import type { Fact, MemoryScope, RecalledMessage } from '@kindgi/memory';
6
6
  import type { ToolErrorsSpec, ToolHitlMode, ToolHitlRule } from '@kindgi/policy-contract';
7
7
  import type { Brand, ConversationId, ProjectId, Semver, TenantId, Timestamp } from '@kindgi/types';
8
8
 
@@ -92,20 +92,116 @@ export interface PromptParameter {
92
92
  * Distinct from tool invocation — retrieval is background reading the
93
93
  * agent does silently to ground its response.
94
94
  *
95
- * Scope options:
96
- * - `same-conversation`: prior messages in this conversation only.
97
- * - `same-project`: facts under the project (from Scope.projectId).
98
- * - `tenant`: any tenant-scoped fact of the declared type.
95
+ * Scope options, each within what the run may see (its project and org,
96
+ * the user it acts for, its conversation and that conversation's end
97
+ * user, and tenant-wide facts; never another conversation's or another
98
+ * end user's):
99
+ * - `same-conversation`: this conversation's facts.
100
+ * - `same-user`: the facts of this run's end user (the
101
+ * conversation's participant) only. None in a
102
+ * run that names no end user (`participantId`);
103
+ * never facts keyed to the user a credential
104
+ * acts for, which may serve many people (the
105
+ * turn says so: `memory-needs-participant`).
106
+ * - `same-project`: the run's project's facts; none in a run
107
+ * without a project.
108
+ * - `tenant`: any fact of the declared type the run may see.
109
+ *
110
+ * Mode, with the user's message as the query:
111
+ * - absent: no query, the newest facts first;
112
+ * - `keyword`: full-text search;
113
+ * - `semantic`: search by meaning. A runtime without embeddings fails
114
+ * the turn with `semantic-unavailable` (never a silent skip);
115
+ * - `both`: both, fused by rank (reciprocal rank fusion). Without
116
+ * embeddings it runs the keyword half, and the turn's
117
+ * journal says so (`degraded: no-embeddings`).
99
118
  */
100
119
  export interface RetrievalIntent {
101
- readonly types: readonly string[];
102
- readonly scope: 'same-conversation' | 'same-project' | 'tenant';
103
- /** Cap on facts loaded per turn to keep the prompt small. Default 10. */
120
+ /**
121
+ * What it reads: `facts` (the default), or `conversations`: messages of
122
+ * this agent's earlier conversations, quoted in the turn's `<memory>`
123
+ * block as earlier conversations, never as turns. For conversations:
124
+ * - `same-user` (the usual choice): this end user's other
125
+ * conversations; none in a run that names no end user
126
+ * (`participantId`);
127
+ * - `same-conversation`: this conversation's messages older than the
128
+ * history window;
129
+ * - `same-segment`: conversations in the run's segment path (the same
130
+ * customer), whoever had them;
131
+ * - `same-project`: the project's conversations, whoever had them.
132
+ * The last two quote other people's conversations: publishing warns,
133
+ * and their messages are marked as another person's.
134
+ */
135
+ readonly source?: 'facts' | 'conversations';
136
+ /**
137
+ * For conversations: whose messages it recalls. Default `['user']`: the
138
+ * people's own words. Adding `'agent'` recalls the agent's earlier
139
+ * answers too, which can carry its mistakes: they are marked as
140
+ * unverified earlier answers, and publishing warns.
141
+ */
142
+ readonly roles?: readonly ('user' | 'agent')[];
143
+ /** The fact types it retrieves: at least one, for facts. Not used for conversations. */
144
+ readonly types?: readonly string[];
145
+ readonly scope: 'same-conversation' | 'same-user' | 'same-segment' | 'same-project' | 'tenant';
146
+ /** Cap on facts (or messages) loaded per turn to keep the prompt small. Default 10. */
104
147
  readonly limit?: number;
105
- /** If `keyword` or `semantic`, biases which retrieval mode is used. */
106
148
  readonly mode?: 'keyword' | 'semantic' | 'both';
107
149
  }
108
150
 
151
+ /**
152
+ * How an agent uses what it retrieves. By default every retrieved fact
153
+ * is data, in a labelled block the model reads as information, never as
154
+ * instructions.
155
+ */
156
+ export interface AgentMemoryPolicy {
157
+ /**
158
+ * Fact types that are instructions for this agent (e.g. `policy`): a
159
+ * retrieved fact of one of these types that a person **verified** goes
160
+ * into the system message under "Policies (verified)". Unverified
161
+ * facts of these types stay data. Default: none.
162
+ */
163
+ readonly instructionTypes?: readonly string[];
164
+ /**
165
+ * Lets the agent remember: the turn offers the built-in tool
166
+ * `kindgi_remember` (`REMEMBER_TOOL_ID`). Absent: it can't.
167
+ */
168
+ readonly remember?: RememberPolicy;
169
+ }
170
+
171
+ /**
172
+ * Where an agent's remembered facts go, always within its run:
173
+ * - `same-user`: the conversation's end user. A run that
174
+ * names none (`participantId`) isn't offered
175
+ * the tool: the user a credential acts for may
176
+ * serve many people;
177
+ * - `same-conversation`: this conversation;
178
+ * - `same-project`: the run's project (a person approves each
179
+ * one first);
180
+ * - `tenant`: the whole tenant (a person approves each
181
+ * one first).
182
+ * Each but `tenant` includes the run's project when there is one.
183
+ */
184
+ export type RememberScope = 'same-user' | 'same-conversation' | 'same-project' | 'tenant';
185
+
186
+ /**
187
+ * What an agent may remember. The model picks the type (one of `types`),
188
+ * the text (up to 2,000 characters), an optional slot `key` and when it
189
+ * stops being true; never the scope. Every remembered fact is
190
+ * `unverified`, attributed to the agent version and the tool call that
191
+ * wrote it, and kept `keepDays` unless a person verifies it.
192
+ *
193
+ * A person approves a fact before any read sees it when the scope is
194
+ * wider than one person, or the text reads like an instruction ("always
195
+ * …", "ignore …", a URL, a tool name). Otherwise it's used at once.
196
+ */
197
+ export interface RememberPolicy {
198
+ /** The fact types it may write, e.g. `preference`. At least one. */
199
+ readonly types: readonly string[];
200
+ readonly scope: RememberScope;
201
+ /** Days an unverified fact is kept, 1–3650. Default 30. */
202
+ readonly keepDays?: number;
203
+ }
204
+
109
205
  /**
110
206
  * Typed reference to a tool. Every agent tool binding is `{ id, version }`
111
207
  * — no bare-id "latest" shortcut. `version` is a semver **range**
@@ -358,6 +454,8 @@ export interface Agent {
358
454
  * retrieval pass before the model call.
359
455
  */
360
456
  readonly retrieval: readonly RetrievalIntent[];
457
+ /** How the agent uses what it retrieves (`instructionTypes`). Absent: all data. */
458
+ readonly memory?: AgentMemoryPolicy;
361
459
  /**
362
460
  * Guardrail ids the agent is subject to. Resolved at turn start
363
461
  * against the guardrail definitions bound for the run
@@ -487,6 +585,8 @@ export interface Conversation {
487
585
  readonly openedAt: Timestamp;
488
586
  /** Set when the conversation is closed. Reopening is not supported. */
489
587
  readonly closedAt?: Timestamp;
588
+ /** Set when it was unregistered: reads no longer return it. */
589
+ readonly unregisteredAt?: Timestamp;
490
590
  /**
491
591
  * Denormalized turn counter — one +1 per completed agent turn.
492
592
  * Incremented by the conversation binding when a turn's final
@@ -510,10 +610,34 @@ export interface AgentBindings {
510
610
  readonly memoryPolicyRegistry?: unknown;
511
611
  }
512
612
 
613
+ /**
614
+ * A message of an earlier conversation a retrieval intent over
615
+ * conversations recalled, with the intent and why: its rank in each
616
+ * search. Quoted in the turn's `<memory>` block as earlier conversation,
617
+ * never as a turn.
618
+ */
619
+ export interface RecalledMemory {
620
+ readonly message: RecalledMessage;
621
+ readonly intent: RetrievalIntent;
622
+ readonly score?: number;
623
+ readonly ranks?: { readonly keyword?: number; readonly semantic?: number };
624
+ /**
625
+ * The conversation was another person's (neither this turn's end user
626
+ * nor the user it acts for): only `same-segment` and `same-project`
627
+ * recall those.
628
+ */
629
+ readonly anotherPerson?: true;
630
+ }
631
+
513
632
  /** A retrieved fact + the retrieval intent that pulled it. */
514
633
  export interface RetrievedFact {
515
634
  readonly fact: Fact<unknown>;
516
635
  readonly intent: RetrievalIntent;
517
- /** Similarity or keyword-rank score, if the retrieval mode produced one. */
636
+ /**
637
+ * The mode's score, if it produced one: a full-text rank (`keyword`),
638
+ * a cosine similarity (`semantic`) or the fused rank score (`both`).
639
+ */
518
640
  readonly score?: number;
641
+ /** Its 1-based rank in each search that found it: why it was retrieved. */
642
+ readonly ranks?: { readonly keyword?: number; readonly semantic?: number };
519
643
  }