@kindgi/agents 0.1.4 → 0.1.5-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 (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 +2 -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 +20 -0
  55. package/dist/handlers/remember-tool.d.ts.map +1 -0
  56. package/dist/handlers/remember-tool.js +149 -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 +3 -1
  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 +111 -28
  92. package/dist/retrieval.d.ts.map +1 -1
  93. package/dist/retrieval.js +432 -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 +131 -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 +2 -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 +200 -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 +8 -1
  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 +135 -0
  134. package/src/retrieval.ts +587 -125
  135. package/src/schema.ts +6 -0
  136. package/src/streaming.ts +17 -0
  137. package/src/types.ts +129 -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,111 @@ 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) and of the Kindgi
102
+ * user it acts for; none when it has neither.
103
+ * - `same-project`: the run's project's facts; none in a run
104
+ * without a project.
105
+ * - `tenant`: any fact of the declared type the run may see.
106
+ *
107
+ * Mode, with the user's message as the query:
108
+ * - absent: no query, the newest facts first;
109
+ * - `keyword`: full-text search;
110
+ * - `semantic`: search by meaning. A runtime without embeddings fails
111
+ * the turn with `semantic-unavailable` (never a silent skip);
112
+ * - `both`: both, fused by rank (reciprocal rank fusion). Without
113
+ * embeddings it runs the keyword half, and the turn's
114
+ * journal says so (`degraded: no-embeddings`).
99
115
  */
100
116
  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. */
117
+ /**
118
+ * What it reads: `facts` (the default), or `conversations`: messages of
119
+ * this agent's earlier conversations, quoted in the turn's `<memory>`
120
+ * block as earlier conversations, never as turns. For conversations:
121
+ * - `same-user` (the usual choice): this end user's (or user's) other
122
+ * conversations;
123
+ * - `same-conversation`: this conversation's messages older than the
124
+ * history window;
125
+ * - `same-segment`: conversations in the run's segment path (the same
126
+ * customer), whoever had them;
127
+ * - `same-project`: the project's conversations, whoever had them.
128
+ * The last two quote other people's conversations: publishing warns,
129
+ * and their messages are marked as another person's.
130
+ */
131
+ readonly source?: 'facts' | 'conversations';
132
+ /**
133
+ * For conversations: whose messages it recalls. Default `['user']`: the
134
+ * people's own words. Adding `'agent'` recalls the agent's earlier
135
+ * answers too, which can carry its mistakes: they are marked as
136
+ * unverified earlier answers, and publishing warns.
137
+ */
138
+ readonly roles?: readonly ('user' | 'agent')[];
139
+ /** The fact types it retrieves: at least one, for facts. Not used for conversations. */
140
+ readonly types?: readonly string[];
141
+ readonly scope: 'same-conversation' | 'same-user' | 'same-segment' | 'same-project' | 'tenant';
142
+ /** Cap on facts (or messages) loaded per turn to keep the prompt small. Default 10. */
104
143
  readonly limit?: number;
105
- /** If `keyword` or `semantic`, biases which retrieval mode is used. */
106
144
  readonly mode?: 'keyword' | 'semantic' | 'both';
107
145
  }
108
146
 
147
+ /**
148
+ * How an agent uses what it retrieves. By default every retrieved fact
149
+ * is data, in a labelled block the model reads as information, never as
150
+ * instructions.
151
+ */
152
+ export interface AgentMemoryPolicy {
153
+ /**
154
+ * Fact types that are instructions for this agent (e.g. `policy`): a
155
+ * retrieved fact of one of these types that a person **verified** goes
156
+ * into the system message under "Policies (verified)". Unverified
157
+ * facts of these types stay data. Default: none.
158
+ */
159
+ readonly instructionTypes?: readonly string[];
160
+ /**
161
+ * Lets the agent remember: the turn offers the built-in tool
162
+ * `kindgi_remember` (`REMEMBER_TOOL_ID`). Absent: it can't.
163
+ */
164
+ readonly remember?: RememberPolicy;
165
+ }
166
+
167
+ /**
168
+ * Where an agent's remembered facts go, always within its run:
169
+ * - `same-user`: the conversation's end user, else the user
170
+ * the run acts for (a run with neither can't
171
+ * remember);
172
+ * - `same-conversation`: this conversation;
173
+ * - `same-project`: the run's project (a person approves each
174
+ * one first);
175
+ * - `tenant`: the whole tenant (a person approves each
176
+ * one first).
177
+ * Each but `tenant` includes the run's project when there is one.
178
+ */
179
+ export type RememberScope = 'same-user' | 'same-conversation' | 'same-project' | 'tenant';
180
+
181
+ /**
182
+ * What an agent may remember. The model picks the type (one of `types`),
183
+ * the text (up to 2,000 characters), an optional slot `key` and when it
184
+ * stops being true; never the scope. Every remembered fact is
185
+ * `unverified`, attributed to the agent version and the tool call that
186
+ * wrote it, and kept `keepDays` unless a person verifies it.
187
+ *
188
+ * A person approves a fact before any read sees it when the scope is
189
+ * wider than one person, or the text reads like an instruction ("always
190
+ * …", "ignore …", a URL, a tool name). Otherwise it's used at once.
191
+ */
192
+ export interface RememberPolicy {
193
+ /** The fact types it may write, e.g. `preference`. At least one. */
194
+ readonly types: readonly string[];
195
+ readonly scope: RememberScope;
196
+ /** Days an unverified fact is kept, 1–3650. Default 30. */
197
+ readonly keepDays?: number;
198
+ }
199
+
109
200
  /**
110
201
  * Typed reference to a tool. Every agent tool binding is `{ id, version }`
111
202
  * — no bare-id "latest" shortcut. `version` is a semver **range**
@@ -358,6 +449,8 @@ export interface Agent {
358
449
  * retrieval pass before the model call.
359
450
  */
360
451
  readonly retrieval: readonly RetrievalIntent[];
452
+ /** How the agent uses what it retrieves (`instructionTypes`). Absent: all data. */
453
+ readonly memory?: AgentMemoryPolicy;
361
454
  /**
362
455
  * Guardrail ids the agent is subject to. Resolved at turn start
363
456
  * against the guardrail definitions bound for the run
@@ -487,6 +580,8 @@ export interface Conversation {
487
580
  readonly openedAt: Timestamp;
488
581
  /** Set when the conversation is closed. Reopening is not supported. */
489
582
  readonly closedAt?: Timestamp;
583
+ /** Set when it was unregistered: reads no longer return it. */
584
+ readonly unregisteredAt?: Timestamp;
490
585
  /**
491
586
  * Denormalized turn counter — one +1 per completed agent turn.
492
587
  * Incremented by the conversation binding when a turn's final
@@ -510,10 +605,34 @@ export interface AgentBindings {
510
605
  readonly memoryPolicyRegistry?: unknown;
511
606
  }
512
607
 
608
+ /**
609
+ * A message of an earlier conversation a retrieval intent over
610
+ * conversations recalled, with the intent and why: its rank in each
611
+ * search. Quoted in the turn's `<memory>` block as earlier conversation,
612
+ * never as a turn.
613
+ */
614
+ export interface RecalledMemory {
615
+ readonly message: RecalledMessage;
616
+ readonly intent: RetrievalIntent;
617
+ readonly score?: number;
618
+ readonly ranks?: { readonly keyword?: number; readonly semantic?: number };
619
+ /**
620
+ * The conversation was another person's (neither this turn's end user
621
+ * nor the user it acts for): only `same-segment` and `same-project`
622
+ * recall those.
623
+ */
624
+ readonly anotherPerson?: true;
625
+ }
626
+
513
627
  /** A retrieved fact + the retrieval intent that pulled it. */
514
628
  export interface RetrievedFact {
515
629
  readonly fact: Fact<unknown>;
516
630
  readonly intent: RetrievalIntent;
517
- /** Similarity or keyword-rank score, if the retrieval mode produced one. */
631
+ /**
632
+ * The mode's score, if it produced one: a full-text rank (`keyword`),
633
+ * a cosine similarity (`semantic`) or the fused rank score (`both`).
634
+ */
518
635
  readonly score?: number;
636
+ /** Its 1-based rank in each search that found it: why it was retrieved. */
637
+ readonly ranks?: { readonly keyword?: number; readonly semantic?: number };
519
638
  }