@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.
- package/README.md +1 -1
- package/dist/blocks.d.ts +19 -0
- package/dist/blocks.d.ts.map +1 -1
- package/dist/blocks.js +59 -1
- package/dist/blocks.js.map +1 -1
- package/dist/conversation-binding.d.ts +49 -3
- package/dist/conversation-binding.d.ts.map +1 -1
- package/dist/define.d.ts +7 -1
- package/dist/define.d.ts.map +1 -1
- package/dist/define.js +132 -7
- package/dist/define.js.map +1 -1
- package/dist/drafted-template.d.ts +34 -0
- package/dist/drafted-template.d.ts.map +1 -0
- package/dist/drafted-template.js +95 -0
- package/dist/drafted-template.js.map +1 -0
- package/dist/guardrails-gate.d.ts +28 -13
- package/dist/guardrails-gate.d.ts.map +1 -1
- package/dist/guardrails-gate.js +59 -21
- package/dist/guardrails-gate.js.map +1 -1
- package/dist/handlers/build-initial-messages.d.ts +8 -2
- package/dist/handlers/build-initial-messages.d.ts.map +1 -1
- package/dist/handlers/build-initial-messages.js +23 -21
- package/dist/handlers/build-initial-messages.js.map +1 -1
- package/dist/handlers/compose-result.d.ts.map +1 -1
- package/dist/handlers/compose-result.js +21 -0
- package/dist/handlers/compose-result.js.map +1 -1
- package/dist/handlers/context.d.ts +6 -1
- package/dist/handlers/context.d.ts.map +1 -1
- package/dist/handlers/dispatch-tools.d.ts.map +1 -1
- package/dist/handlers/dispatch-tools.js +17 -5
- package/dist/handlers/dispatch-tools.js.map +1 -1
- package/dist/handlers/errors.d.ts +14 -1
- package/dist/handlers/errors.d.ts.map +1 -1
- package/dist/handlers/errors.js.map +1 -1
- package/dist/handlers/evaluate-guardrails.d.ts +6 -1
- package/dist/handlers/evaluate-guardrails.d.ts.map +1 -1
- package/dist/handlers/evaluate-guardrails.js +46 -4
- package/dist/handlers/evaluate-guardrails.js.map +1 -1
- package/dist/handlers/history.d.ts +24 -0
- package/dist/handlers/history.d.ts.map +1 -0
- package/dist/handlers/history.js +52 -0
- package/dist/handlers/history.js.map +1 -0
- package/dist/handlers/persist-final-message.d.ts.map +1 -1
- package/dist/handlers/persist-final-message.js +2 -0
- package/dist/handlers/persist-final-message.js.map +1 -1
- package/dist/handlers/persist-user-message.d.ts.map +1 -1
- package/dist/handlers/persist-user-message.js +2 -0
- package/dist/handlers/persist-user-message.js.map +1 -1
- package/dist/handlers/public-types.d.ts +12 -2
- package/dist/handlers/public-types.d.ts.map +1 -1
- package/dist/handlers/rehydrate.d.ts.map +1 -1
- package/dist/handlers/rehydrate.js +7 -4
- package/dist/handlers/rehydrate.js.map +1 -1
- package/dist/handlers/remember-tool.d.ts +22 -0
- package/dist/handlers/remember-tool.d.ts.map +1 -0
- package/dist/handlers/remember-tool.js +156 -0
- package/dist/handlers/remember-tool.js.map +1 -0
- package/dist/handlers/replay.d.ts +55 -1
- package/dist/handlers/replay.d.ts.map +1 -1
- package/dist/handlers/replay.js +23 -6
- package/dist/handlers/replay.js.map +1 -1
- package/dist/handlers/resolve-blocks.d.ts.map +1 -1
- package/dist/handlers/resolve-blocks.js +19 -8
- package/dist/handlers/resolve-blocks.js.map +1 -1
- package/dist/handlers/result-shape.d.ts +10 -5
- package/dist/handlers/result-shape.d.ts.map +1 -1
- package/dist/handlers/result-shape.js.map +1 -1
- package/dist/handlers/run-retrievals.d.ts +2 -1
- package/dist/handlers/run-retrievals.d.ts.map +1 -1
- package/dist/handlers/run-retrievals.js +42 -16
- package/dist/handlers/run-retrievals.js.map +1 -1
- package/dist/handlers/turn-environment.d.ts.map +1 -1
- package/dist/handlers/turn-environment.js +2 -1
- package/dist/handlers/turn-environment.js.map +1 -1
- package/dist/handlers/turn-provenance.d.ts +19 -3
- package/dist/handlers/turn-provenance.d.ts.map +1 -1
- package/dist/handlers/turn-provenance.js +116 -2
- package/dist/handlers/turn-provenance.js.map +1 -1
- package/dist/index.d.ts +13 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -3
- package/dist/index.js.map +1 -1
- package/dist/invoke.d.ts +1 -1
- package/dist/invoke.d.ts.map +1 -1
- package/dist/invoke.js +2 -0
- package/dist/invoke.js.map +1 -1
- package/dist/remember.d.ts +49 -0
- package/dist/remember.d.ts.map +1 -0
- package/dist/remember.js +89 -0
- package/dist/remember.js.map +1 -0
- package/dist/retrieval.d.ts +114 -28
- package/dist/retrieval.d.ts.map +1 -1
- package/dist/retrieval.js +433 -104
- package/dist/retrieval.js.map +1 -1
- package/dist/schema.d.ts +17 -0
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +6 -0
- package/dist/schema.js.map +1 -1
- package/dist/streaming.d.ts +16 -1
- package/dist/streaming.d.ts.map +1 -1
- package/dist/streaming.js.map +1 -1
- package/dist/types.d.ts +136 -10
- package/dist/types.d.ts.map +1 -1
- package/migrations/0005_condemned_hellcat.sql +1 -0
- package/migrations/meta/0005_snapshot.json +333 -0
- package/migrations/meta/_journal.json +7 -0
- package/package.json +15 -15
- package/src/blocks.ts +75 -1
- package/src/conversation-binding.ts +53 -3
- package/src/define.ts +144 -9
- package/src/drafted-template.ts +118 -0
- package/src/guardrails-gate.ts +90 -26
- package/src/handlers/build-initial-messages.ts +29 -22
- package/src/handlers/compose-result.ts +21 -0
- package/src/handlers/context.ts +12 -1
- package/src/handlers/dispatch-tools.ts +19 -5
- package/src/handlers/errors.ts +16 -1
- package/src/handlers/evaluate-guardrails.ts +47 -4
- package/src/handlers/history.ts +57 -0
- package/src/handlers/persist-final-message.ts +2 -0
- package/src/handlers/persist-user-message.ts +2 -0
- package/src/handlers/public-types.ts +18 -2
- package/src/handlers/rehydrate.ts +12 -8
- package/src/handlers/remember-tool.ts +207 -0
- package/src/handlers/replay.ts +80 -8
- package/src/handlers/resolve-blocks.ts +21 -7
- package/src/handlers/result-shape.ts +19 -5
- package/src/handlers/run-retrievals.ts +52 -19
- package/src/handlers/turn-environment.ts +2 -1
- package/src/handlers/turn-provenance.ts +133 -2
- package/src/index.ts +33 -2
- package/src/invoke.ts +3 -0
- package/src/remember.ts +136 -0
- package/src/retrieval.ts +591 -125
- package/src/schema.ts +6 -0
- package/src/streaming.ts +17 -0
- package/src/types.ts +134 -10
package/src/index.ts
CHANGED
|
@@ -16,11 +16,15 @@ export type {
|
|
|
16
16
|
RunSnapshotRecord,
|
|
17
17
|
RunSnapshotWriteInput,
|
|
18
18
|
} from './run-snapshot-binding.js';
|
|
19
|
+
export { DRAFTED_TEMPLATE_MAX, checkDraftedTemplate } from './drafted-template.js';
|
|
20
|
+
export type { DraftedTemplateContext } from './drafted-template.js';
|
|
19
21
|
export { defineAgent } from './define.js';
|
|
20
22
|
export {
|
|
21
23
|
BLOCK_KINDS,
|
|
22
24
|
MODEL_SETTINGS_SCHEMA,
|
|
25
|
+
TUNABLE_MARKER,
|
|
23
26
|
settingsSchemaIssues,
|
|
27
|
+
tunableKeys,
|
|
24
28
|
validateBlock,
|
|
25
29
|
} from './blocks.js';
|
|
26
30
|
export type {
|
|
@@ -32,6 +36,7 @@ export type {
|
|
|
32
36
|
ModelSettings,
|
|
33
37
|
PromptBlockContent,
|
|
34
38
|
SettingsBlockContent,
|
|
39
|
+
TunableKey,
|
|
35
40
|
} from './blocks.js';
|
|
36
41
|
export type { DefineAgentSpec } from './define.js';
|
|
37
42
|
export { resolveEffectiveHitlPolicy } from './hitl-policy.js';
|
|
@@ -45,11 +50,12 @@ export type { GateDecision, GateDecisionValue } from './handlers/gate-decision.j
|
|
|
45
50
|
export type { EffectiveHitlPolicy } from './hitl-policy.js';
|
|
46
51
|
export { agentStepOutput, invokeAgent, resumeAgentTurn } from './invoke.js';
|
|
47
52
|
export { parseFailureMessage, turnFailureMessage } from './handlers/errors.js';
|
|
48
|
-
export { isReadOnlyTool } from './handlers/replay.js';
|
|
53
|
+
export { isComputeOnlyTool, isReadOnlyTool } from './handlers/replay.js';
|
|
49
54
|
export { SESSION_GATE_RECORD } from './handlers/setup.js';
|
|
50
55
|
export type {
|
|
51
56
|
ReplayApproval,
|
|
52
57
|
ReplayBinding,
|
|
58
|
+
ReplayOverrides,
|
|
53
59
|
ReplayToolDecision,
|
|
54
60
|
ReplayToolInput,
|
|
55
61
|
ReplayToolTrace,
|
|
@@ -71,6 +77,7 @@ export type {
|
|
|
71
77
|
OutputSchemaViolationError,
|
|
72
78
|
ResumeAgentTurnInput,
|
|
73
79
|
RunSnapshotError,
|
|
80
|
+
SemanticUnavailableError,
|
|
74
81
|
ToolInvocationError,
|
|
75
82
|
UnresolvedToolError,
|
|
76
83
|
} from './invoke.js';
|
|
@@ -92,6 +99,7 @@ export { persistProvenance } from './provenance-emit.js';
|
|
|
92
99
|
export type { ProvenanceBindings } from './provenance-emit.js';
|
|
93
100
|
export type {
|
|
94
101
|
AgentMessageEvent,
|
|
102
|
+
GuardrailErrorEvent,
|
|
95
103
|
GuardrailViolatedEvent,
|
|
96
104
|
ModelCallCompletedEvent,
|
|
97
105
|
ModelCallStartedEvent,
|
|
@@ -106,7 +114,26 @@ export type {
|
|
|
106
114
|
TurnStartedEvent,
|
|
107
115
|
} from './streaming.js';
|
|
108
116
|
export { AUTO_INJECTED_VARS, renderInstructions } from './prompt.js';
|
|
109
|
-
export {
|
|
117
|
+
export {
|
|
118
|
+
EARLIER_ANSWER_NOTE,
|
|
119
|
+
MEMORY_DATA_RULE,
|
|
120
|
+
RECALL_DEFAULT_ROLES,
|
|
121
|
+
formatPoliciesForPrompt,
|
|
122
|
+
formatRetrievedForPrompt,
|
|
123
|
+
isPolicyFact,
|
|
124
|
+
retrieveForTurn,
|
|
125
|
+
runMemoryReaders,
|
|
126
|
+
runRetrievals,
|
|
127
|
+
} from './retrieval.js';
|
|
128
|
+
export type { DegradedIntent, RetrievalPass, RetrievalRun } from './retrieval.js';
|
|
129
|
+
export {
|
|
130
|
+
DEFAULT_REMEMBER_DAYS,
|
|
131
|
+
MAX_REMEMBER_DAYS,
|
|
132
|
+
REMEMBER_TOOL_ID,
|
|
133
|
+
REMEMBER_TOOL_VERSION,
|
|
134
|
+
looksLikeInstruction,
|
|
135
|
+
} from './remember.js';
|
|
136
|
+
export type { RememberToolOutput } from './handlers/remember-tool.js';
|
|
110
137
|
export type { RetrievalBindings } from './retrieval.js';
|
|
111
138
|
export type {
|
|
112
139
|
MissingParameterError,
|
|
@@ -140,6 +167,7 @@ export type {
|
|
|
140
167
|
Agent,
|
|
141
168
|
AgentBindings,
|
|
142
169
|
AgentId,
|
|
170
|
+
AgentMemoryPolicy,
|
|
143
171
|
AgentOutputSpec,
|
|
144
172
|
BlockRef,
|
|
145
173
|
Conversation,
|
|
@@ -149,6 +177,9 @@ export type {
|
|
|
149
177
|
MessageRole,
|
|
150
178
|
PromptParameter,
|
|
151
179
|
PromptRef,
|
|
180
|
+
RecalledMemory,
|
|
181
|
+
RememberPolicy,
|
|
182
|
+
RememberScope,
|
|
152
183
|
RetrievalIntent,
|
|
153
184
|
RetrievedFact,
|
|
154
185
|
ToolRef,
|
package/src/invoke.ts
CHANGED
|
@@ -95,6 +95,8 @@ export async function invokeAgent(
|
|
|
95
95
|
...(input.parent !== undefined && { parent: input.parent }),
|
|
96
96
|
// A replay's run says so, and which eval run and past run it is for.
|
|
97
97
|
...(input.replay !== undefined && { replay: input.replay }),
|
|
98
|
+
...(input.idempotencyKey !== undefined && { idempotencyKey: input.idempotencyKey }),
|
|
99
|
+
...(input.trigger !== undefined && { trigger: input.trigger }),
|
|
98
100
|
...(input.dryRun === true && { options: { dryRun: true } }),
|
|
99
101
|
// Authorization — carry principal + authz into the run so every
|
|
100
102
|
// tool invocation inside the agent's turn is checked.
|
|
@@ -289,6 +291,7 @@ export type {
|
|
|
289
291
|
ModelInvocationError,
|
|
290
292
|
OutputSchemaViolationError,
|
|
291
293
|
RunSnapshotError,
|
|
294
|
+
SemanticUnavailableError,
|
|
292
295
|
ToolInvocationError,
|
|
293
296
|
UnresolvedToolError,
|
|
294
297
|
} from './handlers/errors.js';
|
package/src/remember.ts
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (C) 2026 Kindgi Inc.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* What an agent's `remember` tool writes, decided from the agent's
|
|
6
|
+
* declaration (`memory.remember`) and the run, never from the model:
|
|
7
|
+
* where the fact goes, whom it's about, and whether a person approves
|
|
8
|
+
* it before any read sees it.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { Principal } from '@kindgi/authz';
|
|
12
|
+
import type { FactSubject, MemoryScope, RememberReviewReason } from '@kindgi/memory';
|
|
13
|
+
import type { ConversationId, ProjectId, TenantId, ThreadId, UserId } from '@kindgi/types';
|
|
14
|
+
|
|
15
|
+
import type { RememberPolicy, RememberScope } from './types.js';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The built-in tool an agent that declares `memory.remember` gets. Built-in
|
|
19
|
+
* ids (`kindgi_<verb>`) have no dots: the model calls exactly this name,
|
|
20
|
+
* the one docs and instructions use.
|
|
21
|
+
*/
|
|
22
|
+
export const REMEMBER_TOOL_ID = 'kindgi_remember';
|
|
23
|
+
export const REMEMBER_TOOL_VERSION = '1.0.0';
|
|
24
|
+
/** Days an unverified remembered fact is kept when the agent doesn't say. */
|
|
25
|
+
export const DEFAULT_REMEMBER_DAYS = 30;
|
|
26
|
+
export const MAX_REMEMBER_DAYS = 3650;
|
|
27
|
+
export const MAX_REMEMBER_TEXT = 2000;
|
|
28
|
+
export const MAX_REMEMBER_KEY = 100;
|
|
29
|
+
|
|
30
|
+
/** The Kindgi user a run acts for: the one an agent was delegated by, or the actor. */
|
|
31
|
+
export function runUserId(principal: Principal | undefined): UserId | undefined {
|
|
32
|
+
const user = principal?.onBehalfOf ?? principal?.actor;
|
|
33
|
+
return user?.kind === 'user' ? (user.id as UserId) : undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The run a remembered fact is placed by. */
|
|
37
|
+
export interface RememberRun {
|
|
38
|
+
readonly tenantId: TenantId;
|
|
39
|
+
readonly projectId?: ProjectId;
|
|
40
|
+
readonly conversationId: ConversationId;
|
|
41
|
+
/** The conversation's end user (the app's own id for them). */
|
|
42
|
+
readonly participantId?: string;
|
|
43
|
+
/** The Kindgi user the run acts for. */
|
|
44
|
+
readonly userId?: UserId;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export type RememberTarget =
|
|
48
|
+
| {
|
|
49
|
+
readonly kind: 'ok';
|
|
50
|
+
readonly scope: MemoryScope;
|
|
51
|
+
/** Whom it came from: erasing that person erases it. */
|
|
52
|
+
readonly subjects: readonly FactSubject[];
|
|
53
|
+
}
|
|
54
|
+
| { readonly kind: 'refused'; readonly reason: string };
|
|
55
|
+
|
|
56
|
+
/** Where a fact the agent remembers goes, and whom it's about. */
|
|
57
|
+
export function rememberTarget(scope: RememberScope, run: RememberRun): RememberTarget {
|
|
58
|
+
const inProject = {
|
|
59
|
+
tenantId: run.tenantId,
|
|
60
|
+
...(run.projectId !== undefined && { projectId: run.projectId }),
|
|
61
|
+
};
|
|
62
|
+
const person: FactSubject | undefined =
|
|
63
|
+
run.participantId !== undefined
|
|
64
|
+
? { kind: 'participant', id: run.participantId }
|
|
65
|
+
: run.userId !== undefined
|
|
66
|
+
? { kind: 'user', id: run.userId }
|
|
67
|
+
: undefined;
|
|
68
|
+
const subjects = person !== undefined ? [person] : [];
|
|
69
|
+
const ok = (to: MemoryScope): RememberTarget => ({ kind: 'ok', scope: to, subjects });
|
|
70
|
+
switch (scope) {
|
|
71
|
+
case 'same-user':
|
|
72
|
+
// The end user, named. Never the user the run acts for: a credential
|
|
73
|
+
// that serves many people is one user for all of them.
|
|
74
|
+
if (run.participantId !== undefined) {
|
|
75
|
+
return ok({ ...inProject, participantId: run.participantId });
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
kind: 'refused',
|
|
79
|
+
reason:
|
|
80
|
+
'Not remembered: this conversation names no end user (`participantId`), so there is no one to remember it for.',
|
|
81
|
+
};
|
|
82
|
+
case 'same-conversation':
|
|
83
|
+
return ok({ ...inProject, threadId: run.conversationId as unknown as ThreadId });
|
|
84
|
+
case 'same-project':
|
|
85
|
+
if (run.projectId === undefined) {
|
|
86
|
+
return { kind: 'refused', reason: 'Not remembered: the run has no project.' };
|
|
87
|
+
}
|
|
88
|
+
return ok(inProject);
|
|
89
|
+
case 'tenant':
|
|
90
|
+
return ok({ tenantId: run.tenantId });
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Text that reads like an instruction to an agent, by the words and forms
|
|
96
|
+
* prompt injection uses: "always" or "never", "ignore" or "disregard",
|
|
97
|
+
* "you must" and the like, a system prompt or instructions, a link, or one
|
|
98
|
+
* of the agent's own tools. This routes a fact to a person; it isn't the
|
|
99
|
+
* defense (the scope guard, the trust label and the data block are).
|
|
100
|
+
*/
|
|
101
|
+
const INSTRUCTION_LIKE: readonly RegExp[] = [
|
|
102
|
+
/\b(always|never)\b/i,
|
|
103
|
+
/\b(ignore|disregard)\b/i,
|
|
104
|
+
/\byou (must|should|shall|have to|need to|are required to)\b/i,
|
|
105
|
+
/\b(system prompt|instructions?)\b/i,
|
|
106
|
+
/\b[a-z][a-z0-9+.-]*:\/\/\S/i,
|
|
107
|
+
/\bwww\.[a-z0-9-]+\.[a-z]/i,
|
|
108
|
+
];
|
|
109
|
+
|
|
110
|
+
export function looksLikeInstruction(text: string, toolIds: readonly string[]): boolean {
|
|
111
|
+
if (INSTRUCTION_LIKE.some((pattern) => pattern.test(text))) return true;
|
|
112
|
+
return toolIds.some((id) =>
|
|
113
|
+
// The id, or the name a provider sees for a dotted one (`.` → `__`), as a whole token.
|
|
114
|
+
[id, id.replace(/\./g, '__')].some((name) =>
|
|
115
|
+
new RegExp(`(^|[^\\w.])${escapeRegExp(name)}($|[^\\w])`, 'i').test(text),
|
|
116
|
+
),
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Why a fact the agent remembers waits for a person; none when it's used at once. */
|
|
121
|
+
export function reviewReasons(
|
|
122
|
+
policy: RememberPolicy,
|
|
123
|
+
text: string,
|
|
124
|
+
toolIds: readonly string[],
|
|
125
|
+
): readonly RememberReviewReason[] {
|
|
126
|
+
return [
|
|
127
|
+
...(policy.scope === 'same-project' || policy.scope === 'tenant'
|
|
128
|
+
? (['wide-scope'] as const)
|
|
129
|
+
: []),
|
|
130
|
+
...(looksLikeInstruction(text, toolIds) ? (['instruction-like'] as const) : []),
|
|
131
|
+
];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function escapeRegExp(s: string): string {
|
|
135
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
136
|
+
}
|