@kindgi/agents 0.0.0-bootstrap.0 → 0.1.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.
- package/LICENSE +201 -0
- package/README.md +131 -2
- package/dist/agent-turn-flow.d.ts +57 -0
- package/dist/agent-turn-flow.d.ts.map +1 -0
- package/dist/agent-turn-flow.js +172 -0
- package/dist/agent-turn-flow.js.map +1 -0
- package/dist/conversation-binding.d.ts +111 -0
- package/dist/conversation-binding.d.ts.map +1 -0
- package/dist/conversation-binding.js +4 -0
- package/dist/conversation-binding.js.map +1 -0
- package/dist/define.d.ts +180 -0
- package/dist/define.d.ts.map +1 -0
- package/dist/define.js +361 -0
- package/dist/define.js.map +1 -0
- package/dist/errors.d.ts +62 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +4 -0
- package/dist/errors.js.map +1 -0
- package/dist/guardrails-gate.d.ts +169 -0
- package/dist/guardrails-gate.d.ts.map +1 -0
- package/dist/guardrails-gate.js +202 -0
- package/dist/guardrails-gate.js.map +1 -0
- package/dist/handlers/budget-check.d.ts +22 -0
- package/dist/handlers/budget-check.d.ts.map +1 -0
- package/dist/handlers/budget-check.js +109 -0
- package/dist/handlers/budget-check.js.map +1 -0
- package/dist/handlers/build-initial-messages.d.ts +17 -0
- package/dist/handlers/build-initial-messages.d.ts.map +1 -0
- package/dist/handlers/build-initial-messages.js +86 -0
- package/dist/handlers/build-initial-messages.js.map +1 -0
- package/dist/handlers/compose-result.d.ts +10 -0
- package/dist/handlers/compose-result.d.ts.map +1 -0
- package/dist/handlers/compose-result.js +79 -0
- package/dist/handlers/compose-result.js.map +1 -0
- package/dist/handlers/constants.d.ts +8 -0
- package/dist/handlers/constants.d.ts.map +1 -0
- package/dist/handlers/constants.js +10 -0
- package/dist/handlers/constants.js.map +1 -0
- package/dist/handlers/context.d.ts +210 -0
- package/dist/handlers/context.d.ts.map +1 -0
- package/dist/handlers/context.js +4 -0
- package/dist/handlers/context.js.map +1 -0
- package/dist/handlers/dispatch-tools.d.ts +15 -0
- package/dist/handlers/dispatch-tools.d.ts.map +1 -0
- package/dist/handlers/dispatch-tools.js +511 -0
- package/dist/handlers/dispatch-tools.js.map +1 -0
- package/dist/handlers/errors.d.ts +133 -0
- package/dist/handlers/errors.d.ts.map +1 -0
- package/dist/handlers/errors.js +134 -0
- package/dist/handlers/errors.js.map +1 -0
- package/dist/handlers/evaluate-guardrails.d.ts +14 -0
- package/dist/handlers/evaluate-guardrails.d.ts.map +1 -0
- package/dist/handlers/evaluate-guardrails.js +128 -0
- package/dist/handlers/evaluate-guardrails.js.map +1 -0
- package/dist/handlers/final-iteration.d.ts +7 -0
- package/dist/handlers/final-iteration.d.ts.map +1 -0
- package/dist/handlers/final-iteration.js +26 -0
- package/dist/handlers/final-iteration.js.map +1 -0
- package/dist/handlers/index.d.ts +5 -0
- package/dist/handlers/index.d.ts.map +1 -0
- package/dist/handlers/index.js +41 -0
- package/dist/handlers/index.js.map +1 -0
- package/dist/handlers/model-call.d.ts +13 -0
- package/dist/handlers/model-call.d.ts.map +1 -0
- package/dist/handlers/model-call.js +136 -0
- package/dist/handlers/model-call.js.map +1 -0
- package/dist/handlers/persist-final-message.d.ts +14 -0
- package/dist/handlers/persist-final-message.d.ts.map +1 -0
- package/dist/handlers/persist-final-message.js +54 -0
- package/dist/handlers/persist-final-message.js.map +1 -0
- package/dist/handlers/persist-provenance.d.ts +12 -0
- package/dist/handlers/persist-provenance.d.ts.map +1 -0
- package/dist/handlers/persist-provenance.js +34 -0
- package/dist/handlers/persist-provenance.js.map +1 -0
- package/dist/handlers/persist-user-message.d.ts +15 -0
- package/dist/handlers/persist-user-message.d.ts.map +1 -0
- package/dist/handlers/persist-user-message.js +59 -0
- package/dist/handlers/persist-user-message.js.map +1 -0
- package/dist/handlers/public-types.d.ts +201 -0
- package/dist/handlers/public-types.d.ts.map +1 -0
- package/dist/handlers/public-types.js +4 -0
- package/dist/handlers/public-types.js.map +1 -0
- package/dist/handlers/rehydrate.d.ts +8 -0
- package/dist/handlers/rehydrate.d.ts.map +1 -0
- package/dist/handlers/rehydrate.js +94 -0
- package/dist/handlers/rehydrate.js.map +1 -0
- package/dist/handlers/render-prompt.d.ts +12 -0
- package/dist/handlers/render-prompt.d.ts.map +1 -0
- package/dist/handlers/render-prompt.js +41 -0
- package/dist/handlers/render-prompt.js.map +1 -0
- package/dist/handlers/resolve-tools.d.ts +10 -0
- package/dist/handlers/resolve-tools.d.ts.map +1 -0
- package/dist/handlers/resolve-tools.js +55 -0
- package/dist/handlers/resolve-tools.js.map +1 -0
- package/dist/handlers/result-shape.d.ts +94 -0
- package/dist/handlers/result-shape.d.ts.map +1 -0
- package/dist/handlers/result-shape.js +19 -0
- package/dist/handlers/result-shape.js.map +1 -0
- package/dist/handlers/run-retrievals.d.ts +10 -0
- package/dist/handlers/run-retrievals.d.ts.map +1 -0
- package/dist/handlers/run-retrievals.js +64 -0
- package/dist/handlers/run-retrievals.js.map +1 -0
- package/dist/handlers/run-snapshot.d.ts +4 -0
- package/dist/handlers/run-snapshot.d.ts.map +1 -0
- package/dist/handlers/run-snapshot.js +26 -0
- package/dist/handlers/run-snapshot.js.map +1 -0
- package/dist/handlers/setup.d.ts +25 -0
- package/dist/handlers/setup.d.ts.map +1 -0
- package/dist/handlers/setup.js +231 -0
- package/dist/handlers/setup.js.map +1 -0
- package/dist/handlers/structured-output.d.ts +38 -0
- package/dist/handlers/structured-output.d.ts.map +1 -0
- package/dist/handlers/structured-output.js +89 -0
- package/dist/handlers/structured-output.js.map +1 -0
- package/dist/handlers/tool-errors.d.ts +56 -0
- package/dist/handlers/tool-errors.d.ts.map +1 -0
- package/dist/handlers/tool-errors.js +73 -0
- package/dist/handlers/tool-errors.js.map +1 -0
- package/dist/handlers/tool-hitl.d.ts +45 -0
- package/dist/handlers/tool-hitl.d.ts.map +1 -0
- package/dist/handlers/tool-hitl.js +81 -0
- package/dist/handlers/tool-hitl.js.map +1 -0
- package/dist/handlers/turn-environment.d.ts +26 -0
- package/dist/handlers/turn-environment.d.ts.map +1 -0
- package/dist/handlers/turn-environment.js +154 -0
- package/dist/handlers/turn-environment.js.map +1 -0
- package/dist/hitl-policy.d.ts +45 -0
- package/dist/hitl-policy.d.ts.map +1 -0
- package/dist/hitl-policy.js +74 -0
- package/dist/hitl-policy.js.map +1 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/dist/invoke.d.ts +36 -0
- package/dist/invoke.d.ts.map +1 -0
- package/dist/invoke.js +228 -0
- package/dist/invoke.js.map +1 -0
- package/dist/migrations-dir.d.ts +11 -0
- package/dist/migrations-dir.d.ts.map +1 -0
- package/dist/migrations-dir.js +14 -0
- package/dist/migrations-dir.js.map +1 -0
- package/dist/project-run-result.d.ts +23 -0
- package/dist/project-run-result.d.ts.map +1 -0
- package/dist/project-run-result.js +116 -0
- package/dist/project-run-result.js.map +1 -0
- package/dist/prompt.d.ts +83 -0
- package/dist/prompt.d.ts.map +1 -0
- package/dist/prompt.js +119 -0
- package/dist/prompt.js.map +1 -0
- package/dist/provenance-emit.d.ts +44 -0
- package/dist/provenance-emit.d.ts.map +1 -0
- package/dist/provenance-emit.js +51 -0
- package/dist/provenance-emit.js.map +1 -0
- package/dist/registry.d.ts +38 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +125 -0
- package/dist/registry.js.map +1 -0
- package/dist/retrieval.d.ts +47 -0
- package/dist/retrieval.d.ts.map +1 -0
- package/dist/retrieval.js +155 -0
- package/dist/retrieval.js.map +1 -0
- package/dist/run-snapshot-binding.d.ts +77 -0
- package/dist/run-snapshot-binding.d.ts.map +1 -0
- package/dist/run-snapshot-binding.js +4 -0
- package/dist/run-snapshot-binding.js.map +1 -0
- package/dist/schema.d.ts +497 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +133 -0
- package/dist/schema.js.map +1 -0
- package/dist/streaming.d.ts +118 -0
- package/dist/streaming.d.ts.map +1 -0
- package/dist/streaming.js +17 -0
- package/dist/streaming.js.map +1 -0
- package/dist/tenant-policy.d.ts +16 -0
- package/dist/tenant-policy.d.ts.map +1 -0
- package/dist/tenant-policy.js +77 -0
- package/dist/tenant-policy.js.map +1 -0
- package/dist/types.d.ts +435 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +4 -0
- package/dist/types.js.map +1 -0
- package/dist/versioning.d.ts +29 -0
- package/dist/versioning.d.ts.map +1 -0
- package/dist/versioning.js +58 -0
- package/dist/versioning.js.map +1 -0
- package/migrations/0000_sparkling_talkback.sql +18 -0
- package/migrations/0001_tired_warhawk.sql +16 -0
- package/migrations/0002_violet_ezekiel.sql +2 -0
- package/migrations/meta/0000_snapshot.json +172 -0
- package/migrations/meta/0001_snapshot.json +275 -0
- package/migrations/meta/0002_snapshot.json +287 -0
- package/migrations/meta/_journal.json +27 -0
- package/package.json +76 -4
- package/src/agent-turn-flow.ts +183 -0
- package/src/conversation-binding.ts +147 -0
- package/src/define.ts +572 -0
- package/src/errors.ts +80 -0
- package/src/guardrails-gate.ts +342 -0
- package/src/handlers/budget-check.ts +143 -0
- package/src/handlers/build-initial-messages.ts +103 -0
- package/src/handlers/compose-result.ts +90 -0
- package/src/handlers/constants.ts +10 -0
- package/src/handlers/context.ts +226 -0
- package/src/handlers/dispatch-tools.ts +633 -0
- package/src/handlers/errors.ts +282 -0
- package/src/handlers/evaluate-guardrails.ts +153 -0
- package/src/handlers/final-iteration.ts +30 -0
- package/src/handlers/index.ts +63 -0
- package/src/handlers/model-call.ts +151 -0
- package/src/handlers/persist-final-message.ts +67 -0
- package/src/handlers/persist-provenance.ts +39 -0
- package/src/handlers/persist-user-message.ts +70 -0
- package/src/handlers/public-types.ts +209 -0
- package/src/handlers/rehydrate.ts +161 -0
- package/src/handlers/render-prompt.ts +46 -0
- package/src/handlers/resolve-tools.ts +68 -0
- package/src/handlers/result-shape.ts +113 -0
- package/src/handlers/run-retrievals.ts +77 -0
- package/src/handlers/run-snapshot.ts +44 -0
- package/src/handlers/setup.ts +269 -0
- package/src/handlers/structured-output.ts +117 -0
- package/src/handlers/tool-errors.ts +122 -0
- package/src/handlers/tool-hitl.ts +126 -0
- package/src/handlers/turn-environment.ts +191 -0
- package/src/hitl-policy.ts +128 -0
- package/src/index.ts +154 -0
- package/src/invoke.ts +299 -0
- package/src/migrations-dir.ts +17 -0
- package/src/project-run-result.ts +131 -0
- package/src/prompt.ts +185 -0
- package/src/provenance-emit.ts +100 -0
- package/src/registry.ts +164 -0
- package/src/retrieval.ts +219 -0
- package/src/run-snapshot-binding.ts +87 -0
- package/src/schema.ts +154 -0
- package/src/streaming.ts +153 -0
- package/src/tenant-policy.ts +78 -0
- package/src/types.ts +453 -0
- package/src/versioning.ts +77 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { NodeHandler } from '@kindgi/handler';
|
|
2
|
+
import type { TurnContext } from './context.js';
|
|
3
|
+
/**
|
|
4
|
+
* Compose the initial `modelMessages` array the loop's first iteration
|
|
5
|
+
* feeds to the model:
|
|
6
|
+
*
|
|
7
|
+
* [ system: rendered prompt,
|
|
8
|
+
* system: retrieved context (if any),
|
|
9
|
+
* ...history,
|
|
10
|
+
* user: current message ]
|
|
11
|
+
*
|
|
12
|
+
* Output shape: `{ nextMessages: ModelMessage[] }` — matches the loop
|
|
13
|
+
* iteration output's `nextMessages` field so the loop body can treat
|
|
14
|
+
* iteration 0's input identically to subsequent iterations'.
|
|
15
|
+
*/
|
|
16
|
+
export declare function buildBuildInitialMessagesHandler(ctx: TurnContext): NodeHandler;
|
|
17
|
+
//# sourceMappingURL=build-initial-messages.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"build-initial-messages.d.ts","sourceRoot":"","sources":["../../src/handlers/build-initial-messages.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAKnD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAGhD;;;;;;;;;;;;GAYG;AACH,wBAAgB,gCAAgC,CAAC,GAAG,EAAE,WAAW,GAAG,WAAW,CAmD9E"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (C) 2026 Kindgi Inc.
|
|
3
|
+
import { formatRetrievedForPrompt } from '../retrieval.js';
|
|
4
|
+
import { throwAgentTurnFailure } from './errors.js';
|
|
5
|
+
/**
|
|
6
|
+
* Compose the initial `modelMessages` array the loop's first iteration
|
|
7
|
+
* feeds to the model:
|
|
8
|
+
*
|
|
9
|
+
* [ system: rendered prompt,
|
|
10
|
+
* system: retrieved context (if any),
|
|
11
|
+
* ...history,
|
|
12
|
+
* user: current message ]
|
|
13
|
+
*
|
|
14
|
+
* Output shape: `{ nextMessages: ModelMessage[] }` — matches the loop
|
|
15
|
+
* iteration output's `nextMessages` field so the loop body can treat
|
|
16
|
+
* iteration 0's input identically to subsequent iterations'.
|
|
17
|
+
*/
|
|
18
|
+
export function buildBuildInitialMessagesHandler(ctx) {
|
|
19
|
+
return async (input) => {
|
|
20
|
+
if (ctx.conversation === undefined || ctx.retrieved === undefined) {
|
|
21
|
+
throwAgentTurnFailure({
|
|
22
|
+
code: 'model-invocation-failed',
|
|
23
|
+
message: 'build-initial-messages invoked before setup / retrievals completed',
|
|
24
|
+
cause: null,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
// `input` is `run-retrievals`'s output; discarded — the rendered
|
|
28
|
+
// prompt is read from setup's sibling output via ctx (retained on
|
|
29
|
+
// the closure by render-prompt through the local field `renderedPrompt`).
|
|
30
|
+
void input;
|
|
31
|
+
const historyLimit = ctx.input.agent.conversationPolicy?.historyLimit;
|
|
32
|
+
const messages = await ctx.bindings.conversationBinding.readMessages({
|
|
33
|
+
tenantId: ctx.input.tenantId,
|
|
34
|
+
conversationId: ctx.input.conversationId,
|
|
35
|
+
});
|
|
36
|
+
if (messages.kind === 'err')
|
|
37
|
+
throwAgentTurnFailure(messages.error);
|
|
38
|
+
// The user message just appended is included in the history read;
|
|
39
|
+
// drop it because the composer adds it explicitly as the last
|
|
40
|
+
// element.
|
|
41
|
+
const historyRaw = messages.value.filter((m) => m.sequence !== ctx.userMessage?.sequence);
|
|
42
|
+
const history = historyLimit === undefined
|
|
43
|
+
? historyRaw
|
|
44
|
+
: historyRaw.slice(Math.max(0, historyRaw.length - historyLimit));
|
|
45
|
+
const contextBlock = formatRetrievedForPrompt(ctx.retrieved);
|
|
46
|
+
const contextMessage = contextBlock.length > 0 ? { role: 'system', content: contextBlock } : undefined;
|
|
47
|
+
const rendered = ctx.renderedPrompt;
|
|
48
|
+
if (rendered === undefined) {
|
|
49
|
+
throwAgentTurnFailure({
|
|
50
|
+
code: 'model-invocation-failed',
|
|
51
|
+
message: 'build-initial-messages invoked before render-prompt completed',
|
|
52
|
+
cause: null,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
const modelMessages = [
|
|
56
|
+
{ role: 'system', content: rendered },
|
|
57
|
+
...(contextMessage !== undefined ? [contextMessage] : []),
|
|
58
|
+
...history.map(conversationToModelMessage),
|
|
59
|
+
{ role: 'user', content: ctx.input.userMessage },
|
|
60
|
+
];
|
|
61
|
+
return { nextMessages: modelMessages };
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function conversationToModelMessage(msg) {
|
|
65
|
+
if (msg.role === 'tool') {
|
|
66
|
+
return {
|
|
67
|
+
role: 'tool',
|
|
68
|
+
content: typeof msg.content === 'string' ? msg.content : JSON.stringify(msg.content),
|
|
69
|
+
...(msg.toolCall !== undefined && { toolCallId: msg.toolCall.invocationId }),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
if (msg.role === 'agent' && typeof msg.content === 'object' && msg.content !== null) {
|
|
73
|
+
const structured = msg.content;
|
|
74
|
+
return {
|
|
75
|
+
role: 'assistant',
|
|
76
|
+
content: structured.text ?? '',
|
|
77
|
+
...(structured.toolCalls !== undefined && { toolCalls: structured.toolCalls }),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
const role = msg.role === 'agent' ? 'assistant' : msg.role;
|
|
81
|
+
return {
|
|
82
|
+
role,
|
|
83
|
+
content: typeof msg.content === 'string' ? msg.content : JSON.stringify(msg.content),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=build-initial-messages.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"build-initial-messages.js","sourceRoot":"","sources":["../../src/handlers/build-initial-messages.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC;AAKjC,OAAO,EAAE,wBAAwB,EAAE,MAAM,iBAAiB,CAAC;AAI3D,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAEpD;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,gCAAgC,CAAC,GAAgB;IAC/D,OAAO,KAAK,EAAE,KAAc,EAAE,EAAE;QAC9B,IAAI,GAAG,CAAC,YAAY,KAAK,SAAS,IAAI,GAAG,CAAC,SAAS,KAAK,SAAS,EAAE,CAAC;YAClE,qBAAqB,CAAC;gBACpB,IAAI,EAAE,yBAAyB;gBAC/B,OAAO,EAAE,oEAAoE;gBAC7E,KAAK,EAAE,IAAI;aACZ,CAAC,CAAC;QACL,CAAC;QACD,iEAAiE;QACjE,kEAAkE;QAClE,0EAA0E;QAC1E,KAAK,KAAK,CAAC;QAEX,MAAM,YAAY,GAAG,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,kBAAkB,EAAE,YAAY,CAAC;QACtE,MAAM,QAAQ,GAAG,MAAM,GAAG,CAAC,QAAQ,CAAC,mBAAmB,CAAC,YAAY,CAAC;YACnE,QAAQ,EAAE,GAAG,CAAC,KAAK,CAAC,QAAQ;YAC5B,cAAc,EAAE,GAAG,CAAC,KAAK,CAAC,cAAc;SACzC,CAAC,CAAC;QACH,I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;KACrF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { NodeHandler } from '@kindgi/handler';
|
|
2
|
+
import type { TurnContext } from './context.js';
|
|
3
|
+
/**
|
|
4
|
+
* The terminal node — builds the caller-facing `AgentTurnResult` from
|
|
5
|
+
* accumulated `TurnContext` state, emits `turn.completed`, and returns
|
|
6
|
+
* the fully-shaped result as the flow's output. `projectRunResult`
|
|
7
|
+
* unwraps this on the way out.
|
|
8
|
+
*/
|
|
9
|
+
export declare function buildComposeResultHandler(ctx: TurnContext): NodeHandler;
|
|
10
|
+
//# sourceMappingURL=compose-result.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compose-result.d.ts","sourceRoot":"","sources":["../../src/handlers/compose-result.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAKnD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAUhD;;;;;GAKG;AACH,wBAAgB,yBAAyB,CAAC,GAAG,EAAE,WAAW,GAAG,WAAW,CAiEvE"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (C) 2026 Kindgi Inc.
|
|
3
|
+
import { emitTurnEvent } from '../streaming.js';
|
|
4
|
+
import { throwAgentTurnFailure } from './errors.js';
|
|
5
|
+
import { parseJsonAnswer } from './structured-output.js';
|
|
6
|
+
/** The final answer as JSON — `budget-check` already validated it against the schema. */
|
|
7
|
+
function structuredAnswer(content) {
|
|
8
|
+
const parsed = parseJsonAnswer(content);
|
|
9
|
+
return parsed.kind === 'ok' ? parsed.value : null;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* The terminal node — builds the caller-facing `AgentTurnResult` from
|
|
13
|
+
* accumulated `TurnContext` state, emits `turn.completed`, and returns
|
|
14
|
+
* the fully-shaped result as the flow's output. `projectRunResult`
|
|
15
|
+
* unwraps this on the way out.
|
|
16
|
+
*/
|
|
17
|
+
export function buildComposeResultHandler(ctx) {
|
|
18
|
+
return async (_input, kctx) => {
|
|
19
|
+
if (ctx.finalMessage === undefined || ctx.conversation === undefined) {
|
|
20
|
+
throwAgentTurnFailure({
|
|
21
|
+
code: 'model-invocation-failed',
|
|
22
|
+
message: 'compose-result invoked before final message + conversation ready',
|
|
23
|
+
cause: null,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
const durationMs = Date.now() - ctx.startedAt;
|
|
27
|
+
const usage = {
|
|
28
|
+
steps: ctx.usage.steps,
|
|
29
|
+
promptTokens: ctx.usage.promptTokens,
|
|
30
|
+
completionTokens: ctx.usage.completionTokens,
|
|
31
|
+
totalCostUsd: ctx.usage.totalCostUsd,
|
|
32
|
+
durationMs,
|
|
33
|
+
};
|
|
34
|
+
const provider = ctx.lastProvider ?? {
|
|
35
|
+
id: ctx.provider?.metadata.id ?? 'unknown',
|
|
36
|
+
model: ctx.model?.name ?? 'unknown',
|
|
37
|
+
};
|
|
38
|
+
const result = {
|
|
39
|
+
runId: kctx.runId,
|
|
40
|
+
conversationId: ctx.input.conversationId,
|
|
41
|
+
// Dry-run must not advance the conversation's turn counter (no
|
|
42
|
+
// persistence happened). Real runs report the next turn number.
|
|
43
|
+
turnNumber: kctx.dryRun ? ctx.conversation.turnCount : ctx.conversation.turnCount + 1,
|
|
44
|
+
appended: ctx.appended,
|
|
45
|
+
response: ctx.finalMessage,
|
|
46
|
+
retrieved: ctx.retrieved ?? [],
|
|
47
|
+
violations: ctx.nonBlockingViolations ?? [],
|
|
48
|
+
usage,
|
|
49
|
+
provider,
|
|
50
|
+
...(ctx.provider?.metadata.fallback === true && {
|
|
51
|
+
warnings: [
|
|
52
|
+
{
|
|
53
|
+
code: 'fallback-provider',
|
|
54
|
+
message: `Answered by "${provider.id}", a fallback provider: no other registered provider satisfies agent "${ctx.input.agent.id}".`,
|
|
55
|
+
},
|
|
56
|
+
],
|
|
57
|
+
}),
|
|
58
|
+
...(ctx.input.agent.output !== undefined && {
|
|
59
|
+
output: kctx.dryRun ? null : structuredAnswer(ctx.finalMessage.content),
|
|
60
|
+
}),
|
|
61
|
+
...(ctx.persistedProvenance !== undefined && { provenance: ctx.persistedProvenance }),
|
|
62
|
+
...(kctx.dryRun && { dryRun: true }),
|
|
63
|
+
};
|
|
64
|
+
await emitTurnEvent(ctx.bindings.onEvent, {
|
|
65
|
+
kind: 'turn.completed',
|
|
66
|
+
conversationId: ctx.input.conversationId,
|
|
67
|
+
turnNumber: ctx.conversation.turnCount + 1,
|
|
68
|
+
response: ctx.finalMessage,
|
|
69
|
+
retrieved: ctx.retrieved ?? [],
|
|
70
|
+
durationMs,
|
|
71
|
+
totalCostUsd: ctx.usage.totalCostUsd,
|
|
72
|
+
});
|
|
73
|
+
// Wrapped explicitly: the runtime reads a returned object with an
|
|
74
|
+
// `output` key as `{ output, stateDelta }`, and a typed turn's
|
|
75
|
+
// result has one.
|
|
76
|
+
return { output: result };
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=compose-result.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compose-result.js","sourceRoot":"","sources":["../../src/handlers/compose-result.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC;AAIjC,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAIhD,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAEzD,yFAAyF;AACzF,SAAS,gBAAgB,CAAC,OAAgB;IACxC,MAAM,MAAM,GAAG,eAAe,CAAC,OAAO,CAAC,CAAC;IACxC,OAAO,MAAM,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC;AACpD,CAAC;AAED;;;;;G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;IAC5B,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Defaults shared across handler modules. Kept in a single tiny module
|
|
3
|
+
* so the loop-body handlers can import them without pulling in the
|
|
4
|
+
* whole invoke.ts orchestration surface.
|
|
5
|
+
*/
|
|
6
|
+
export declare const DEFAULT_MAX_STEPS = 8;
|
|
7
|
+
export declare const DEFAULT_MAX_WALL_MS = 120000;
|
|
8
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/handlers/constants.ts"],"names":[],"mappings":"AAGA;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,IAAI,CAAC;AACnC,eAAO,MAAM,mBAAmB,SAAU,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (C) 2026 Kindgi Inc.
|
|
3
|
+
/**
|
|
4
|
+
* Defaults shared across handler modules. Kept in a single tiny module
|
|
5
|
+
* so the loop-body handlers can import them without pulling in the
|
|
6
|
+
* whole invoke.ts orchestration surface.
|
|
7
|
+
*/
|
|
8
|
+
export const DEFAULT_MAX_STEPS = 8;
|
|
9
|
+
export const DEFAULT_MAX_WALL_MS = 120_000;
|
|
10
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/handlers/constants.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC;AAEjC;;;;GAIG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,CAAC;AACnC,MAAM,CAAC,MAAM,mBAAmB,GAAG,OAAO,CAAC"}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import type { ModelInfo, ModelMessage, ModelProvider, ModelToolDefinition, TenantPolicy } from '@kindgi/capabilities';
|
|
2
|
+
import type { Guardrail } from '@kindgi/guardrails';
|
|
3
|
+
import type { ProvenanceBuilder } from '@kindgi/provenance';
|
|
4
|
+
import type { Tool } from '@kindgi/tools';
|
|
5
|
+
import type { EvaluationResult } from '@kindgi/guardrails';
|
|
6
|
+
import type { EffectiveHitlPolicy } from '../hitl-policy.js';
|
|
7
|
+
import type { ProvenanceBindings } from '../provenance-emit.js';
|
|
8
|
+
import type { Agent, Conversation, ConversationMessage, RetrievedFact } from '../types.js';
|
|
9
|
+
import type { HitlBindings, InvokeAgentBindings, InvokeAgentInput } from './public-types.js';
|
|
10
|
+
import type { ToolErrorPolicy } from './tool-errors.js';
|
|
11
|
+
/**
|
|
12
|
+
* Per-invocation closure state shared across every node handler. This is
|
|
13
|
+
* a mutable container populated by earlier nodes and read by later
|
|
14
|
+
* nodes. Kernel journal entries record the coarse node-level inputs and
|
|
15
|
+
* outputs; this object carries the finer-grained runtime state that is
|
|
16
|
+
* either not JSON-serializable (functions, ModelProvider instances,
|
|
17
|
+
* ProvenanceBuilder) or expensive to reserialize per node.
|
|
18
|
+
*
|
|
19
|
+
* One `TurnContext` lives for the duration of one `invokeAgent` call.
|
|
20
|
+
*/
|
|
21
|
+
export interface TurnContext {
|
|
22
|
+
readonly input: InvokeAgentInput;
|
|
23
|
+
readonly bindings: InvokeAgentBindings;
|
|
24
|
+
/**
|
|
25
|
+
* Composed abort signal: fires when the wall-clock timer expires OR
|
|
26
|
+
* when the caller-provided abortSignal fires. Handlers pass this into
|
|
27
|
+
* long-running I/O.
|
|
28
|
+
*/
|
|
29
|
+
readonly turnAbort: AbortController;
|
|
30
|
+
/**
|
|
31
|
+
* Millisecond timestamp when `invokeAgent` was called. Used to
|
|
32
|
+
* compute `usage.durationMs` inside `compose-result`.
|
|
33
|
+
*/
|
|
34
|
+
readonly startedAt: number;
|
|
35
|
+
/**
|
|
36
|
+
* Populated by `setup` — the loaded conversation row.
|
|
37
|
+
*/
|
|
38
|
+
conversation?: Conversation;
|
|
39
|
+
/**
|
|
40
|
+
* Populated by `setup` — resolved tools (per-name map + model
|
|
41
|
+
* definitions).
|
|
42
|
+
*/
|
|
43
|
+
tools?: {
|
|
44
|
+
readonly definitions: readonly ModelToolDefinition[];
|
|
45
|
+
/**
|
|
46
|
+
* Per-tool resolved binding: the exact `Tool` instance the runtime
|
|
47
|
+
* dispatches, the `resolvedVersion` picked by `resolve` on the
|
|
48
|
+
* tenant's registry (`toolRegistry.forTenant`)
|
|
49
|
+
* (captured for provenance + telemetry so a replay can pin against
|
|
50
|
+
* the same version), and the `requestedRange` the agent binding
|
|
51
|
+
* declared.
|
|
52
|
+
*/
|
|
53
|
+
readonly byName: ReadonlyMap<string, {
|
|
54
|
+
readonly tool: Tool;
|
|
55
|
+
readonly resolvedVersion: string;
|
|
56
|
+
readonly requestedRange: string;
|
|
57
|
+
}>;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Populated by `setup` — the provider the router picked for the
|
|
61
|
+
* agent's first capability.
|
|
62
|
+
*/
|
|
63
|
+
provider?: ModelProvider;
|
|
64
|
+
/**
|
|
65
|
+
* Populated by `setup` alongside `provider` — the specific
|
|
66
|
+
* `ModelInfo` within that provider the router picked. Threaded into
|
|
67
|
+
* `ModelCallInput.model` on every invocation this turn.
|
|
68
|
+
*/
|
|
69
|
+
model?: ModelInfo;
|
|
70
|
+
/**
|
|
71
|
+
* Populated by `setup` — resolved guardrails for the final-response gate.
|
|
72
|
+
*/
|
|
73
|
+
guardrails?: readonly Guardrail[];
|
|
74
|
+
/**
|
|
75
|
+
* Populated by `persist-user-message` — the persisted user message row.
|
|
76
|
+
*/
|
|
77
|
+
userMessage?: ConversationMessage;
|
|
78
|
+
/**
|
|
79
|
+
* Populated by `render-prompt` — the rendered system-instructions
|
|
80
|
+
* string that seeds the first model call.
|
|
81
|
+
*/
|
|
82
|
+
renderedPrompt?: string;
|
|
83
|
+
/**
|
|
84
|
+
* Populated by `run-retrievals` — facts pulled by declared intents.
|
|
85
|
+
*/
|
|
86
|
+
retrieved?: readonly RetrievedFact[];
|
|
87
|
+
/**
|
|
88
|
+
* Populated by `persist-final-message` — the terminal assistant
|
|
89
|
+
* message. `compose-result` reads it back for the returned
|
|
90
|
+
* `AgentTurnResult.response`.
|
|
91
|
+
*/
|
|
92
|
+
finalMessage?: ConversationMessage;
|
|
93
|
+
/**
|
|
94
|
+
* Populated by `dispatch-tools` and `persist-final-message` — the
|
|
95
|
+
* ordered list of every message appended during this turn. Starts
|
|
96
|
+
* with the user message; then intermediate assistant + tool
|
|
97
|
+
* messages; then the final assistant message.
|
|
98
|
+
*/
|
|
99
|
+
appended: ConversationMessage[];
|
|
100
|
+
/**
|
|
101
|
+
* Cumulative usage across all model calls this turn. Updated by
|
|
102
|
+
* `model-call`; read by `budget-check` and `compose-result`.
|
|
103
|
+
*/
|
|
104
|
+
usage: {
|
|
105
|
+
steps: number;
|
|
106
|
+
promptTokens: number;
|
|
107
|
+
completionTokens: number;
|
|
108
|
+
totalCostUsd: number;
|
|
109
|
+
};
|
|
110
|
+
/**
|
|
111
|
+
* Provider + model of the most recent model call (the one that
|
|
112
|
+
* produced the final response). Set by `model-call`; `compose-result`
|
|
113
|
+
* surfaces it as `AgentTurnResult.provider`.
|
|
114
|
+
*/
|
|
115
|
+
lastProvider?: {
|
|
116
|
+
readonly id: string;
|
|
117
|
+
readonly model: string;
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* Provenance builder — undefined when the caller didn't wire one.
|
|
121
|
+
*/
|
|
122
|
+
provenance?: ProvenanceBuilder;
|
|
123
|
+
/**
|
|
124
|
+
* Provenance bindings kept for reference (needed by
|
|
125
|
+
* `persist-provenance` for `emit` + `keyProvider` details).
|
|
126
|
+
*/
|
|
127
|
+
provenanceBindings?: ProvenanceBindings;
|
|
128
|
+
/**
|
|
129
|
+
* The persisted provenance snapshot (post `persist-provenance`).
|
|
130
|
+
* Present only when the builder was wired AND persistence succeeded.
|
|
131
|
+
*/
|
|
132
|
+
persistedProvenance?: import('@kindgi/provenance').Provenance;
|
|
133
|
+
/**
|
|
134
|
+
* The tenant policy this turn's model was routed under (bound policy
|
|
135
|
+
* merged with the policy registry's). Populated by `setup`; guardrail
|
|
136
|
+
* judges route under it too.
|
|
137
|
+
*/
|
|
138
|
+
tenantPolicy?: TenantPolicy;
|
|
139
|
+
/**
|
|
140
|
+
* How this turn retries failed tool calls: the agent's `toolErrors`
|
|
141
|
+
* (or the default) capped by the tenant's `tool-errors` policy.
|
|
142
|
+
* Populated by `setup`.
|
|
143
|
+
*/
|
|
144
|
+
toolErrorPolicy?: ToolErrorPolicy;
|
|
145
|
+
/**
|
|
146
|
+
* The turn's approval (HITL) rules: the agent's, held to the tenant's
|
|
147
|
+
* `hitl` policy. Populated at the start of the turn (`setup`, or the
|
|
148
|
+
* rebuild of a resumed turn) by `resolveTurnHitlPolicy`.
|
|
149
|
+
*/
|
|
150
|
+
hitlPolicy?: EffectiveHitlPolicy;
|
|
151
|
+
/**
|
|
152
|
+
* Set when the turn resumes after a park in `dispatch-tools` (a
|
|
153
|
+
* tool-call approval): the messages that step stored before it parked.
|
|
154
|
+
* The step runs again, and takes its assistant message and the results
|
|
155
|
+
* of calls that already ran from here instead of storing (and running)
|
|
156
|
+
* them again. Populated by `rehydrateTurnContext`.
|
|
157
|
+
*/
|
|
158
|
+
storedBeforePark?: ConversationMessage[];
|
|
159
|
+
/**
|
|
160
|
+
* Warnings + other (non-halt) guardrail violations from the
|
|
161
|
+
* final-response gate. Read by `compose-result` to populate
|
|
162
|
+
* `AgentTurnResult.violations`.
|
|
163
|
+
*/
|
|
164
|
+
nonBlockingViolations?: readonly EvaluationResult[];
|
|
165
|
+
/**
|
|
166
|
+
* Reason recorded when `turnAbort` fires. Used to distinguish
|
|
167
|
+
* external cancellation from wall-clock timeout in the projected
|
|
168
|
+
* error.
|
|
169
|
+
*/
|
|
170
|
+
abortReason: 'external' | 'timeout' | undefined;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Iteration output shape produced by the loop body's `budget-check`
|
|
174
|
+
* node (and by extension, the loop's `$loop-end` output). Mirrors the
|
|
175
|
+
* `iterationOutputSchema` declared in `agent-turn-flow.ts`.
|
|
176
|
+
*/
|
|
177
|
+
export interface AgentTurnIterationOutput {
|
|
178
|
+
readonly finishReason: 'stop' | 'length' | 'tool-use' | 'content-filter' | 'error';
|
|
179
|
+
readonly message: ModelMessage;
|
|
180
|
+
readonly iterationAppended: readonly ConversationMessage[];
|
|
181
|
+
readonly iterationUsage: {
|
|
182
|
+
readonly promptTokens: number;
|
|
183
|
+
readonly completionTokens: number;
|
|
184
|
+
readonly costUsd: number;
|
|
185
|
+
};
|
|
186
|
+
readonly provider: {
|
|
187
|
+
readonly id: string;
|
|
188
|
+
readonly model: string;
|
|
189
|
+
};
|
|
190
|
+
readonly finishedTurn: boolean;
|
|
191
|
+
readonly nextMessages: readonly ModelMessage[];
|
|
192
|
+
/**
|
|
193
|
+
* Set by `dispatch-tools` when a tool invocation surfaces an
|
|
194
|
+
* `unresolved-tool` / `tool-invocation-failed`, or by
|
|
195
|
+
* `budget-check` when the cost cap is exceeded. `compose-result`
|
|
196
|
+
* inspects it and short-circuits the run into an error result.
|
|
197
|
+
*/
|
|
198
|
+
readonly errorPayload?: {
|
|
199
|
+
readonly code: 'unresolved-tool' | 'tool-invocation-failed' | 'budget-exceeded' | 'agent-turn-aborted' | 'model-invocation-failed';
|
|
200
|
+
readonly message: string;
|
|
201
|
+
readonly extra?: Readonly<Record<string, unknown>>;
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Public re-export helper for callers building test doubles. Kept in
|
|
206
|
+
* this module to avoid making `context.ts` a circular hub — everything
|
|
207
|
+
* a handler needs is imported through here.
|
|
208
|
+
*/
|
|
209
|
+
export type { Agent, HitlBindings, InvokeAgentBindings, InvokeAgentInput, TenantPolicy };
|
|
210
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +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AAmB,EAAE,gBAAgB,EAAE,YAAY,EAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../src/handlers/context.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { NodeHandler } from '@kindgi/handler';
|
|
2
|
+
import type { TurnContext } from './context.js';
|
|
3
|
+
/**
|
|
4
|
+
* Loop-body node #2. If the model returned `finishReason='tool-use'`
|
|
5
|
+
* with a non-empty tool-call list, persist the assistant tool-call
|
|
6
|
+
* message + dispatch every tool + persist every tool result. Update
|
|
7
|
+
* `nextMessages` so the next iteration's model call sees the tool
|
|
8
|
+
* outputs.
|
|
9
|
+
*
|
|
10
|
+
* When the model returned a terminal `finishReason` (or `tool-use`
|
|
11
|
+
* with no calls), pass through unchanged so
|
|
12
|
+
* `budget-check` marks the iteration finished.
|
|
13
|
+
*/
|
|
14
|
+
export declare function buildDispatchToolsHandler(ctx: TurnContext): NodeHandler;
|
|
15
|
+
//# sourceMappingURL=dispatch-tools.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dispatch-tools.d.ts","sourceRoot":"","sources":["../../src/handlers/dispatch-tools.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAe,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAWhE,OAAO,KAAK,EAA4B,WAAW,EAAE,MAAM,cAAc,CAAC;AAkE1E;;;;;;;;;;GAUG;AACH,wBAAgB,yBAAyB,CAAC,GAAG,EAAE,WAAW,GAAG,WAAW,CAySvE"}
|