@getcliqio/cliq-sdk 0.0.0-stage → 1.15.32
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 +28 -2
- package/dist/agent_api.d.ts +39 -0
- package/dist/agent_api.d.ts.map +1 -0
- package/dist/agent_api.js +105 -0
- package/dist/agent_api.js.map +1 -0
- package/dist/agent_manifest.d.ts +162 -0
- package/dist/agent_manifest.d.ts.map +1 -0
- package/dist/agent_manifest.js +242 -0
- package/dist/agent_manifest.js.map +1 -0
- package/dist/agent_payloads.d.ts +103 -0
- package/dist/agent_payloads.d.ts.map +1 -0
- package/dist/agent_payloads.js +56 -0
- package/dist/agent_payloads.js.map +1 -0
- package/dist/agents/base_agent.d.ts +113 -0
- package/dist/agents/base_agent.d.ts.map +1 -0
- package/dist/agents/base_agent.js +413 -0
- package/dist/agents/base_agent.js.map +1 -0
- package/dist/agents/cli_agent.d.ts +98 -0
- package/dist/agents/cli_agent.d.ts.map +1 -0
- package/dist/agents/cli_agent.js +303 -0
- package/dist/agents/cli_agent.js.map +1 -0
- package/dist/agents/cli_rehydrate.d.ts +29 -0
- package/dist/agents/cli_rehydrate.d.ts.map +1 -0
- package/dist/agents/cli_rehydrate.js +32 -0
- package/dist/agents/cli_rehydrate.js.map +1 -0
- package/dist/agents/connector_agent.d.ts +20 -0
- package/dist/agents/connector_agent.d.ts.map +1 -0
- package/dist/agents/connector_agent.js +110 -0
- package/dist/agents/connector_agent.js.map +1 -0
- package/dist/agents/event_artifacts.d.ts +33 -0
- package/dist/agents/event_artifacts.d.ts.map +1 -0
- package/dist/agents/event_artifacts.js +73 -0
- package/dist/agents/event_artifacts.js.map +1 -0
- package/dist/agents/gate_agent.d.ts +40 -0
- package/dist/agents/gate_agent.d.ts.map +1 -0
- package/dist/agents/gate_agent.js +121 -0
- package/dist/agents/gate_agent.js.map +1 -0
- package/dist/agents/index.d.ts +32 -0
- package/dist/agents/index.d.ts.map +1 -0
- package/dist/agents/index.js +26 -0
- package/dist/agents/index.js.map +1 -0
- package/dist/agents/llm_agent.d.ts +109 -0
- package/dist/agents/llm_agent.d.ts.map +1 -0
- package/dist/agents/llm_agent.js +285 -0
- package/dist/agents/llm_agent.js.map +1 -0
- package/dist/agents/llm_contract.d.ts +7 -0
- package/dist/agents/llm_contract.d.ts.map +1 -0
- package/dist/agents/llm_contract.js +7 -0
- package/dist/agents/llm_contract.js.map +1 -0
- package/dist/agents/standard_agent.d.ts +15 -0
- package/dist/agents/standard_agent.d.ts.map +1 -0
- package/dist/agents/standard_agent.js +34 -0
- package/dist/agents/standard_agent.js.map +1 -0
- package/dist/agents/turn_result.d.ts +67 -0
- package/dist/agents/turn_result.d.ts.map +1 -0
- package/dist/agents/turn_result.js +81 -0
- package/dist/agents/turn_result.js.map +1 -0
- package/dist/agents/types.d.ts +314 -0
- package/dist/agents/types.d.ts.map +1 -0
- package/dist/agents/types.js +20 -0
- package/dist/agents/types.js.map +1 -0
- package/dist/builtin_teams/hello_hug.d.ts +9 -0
- package/dist/builtin_teams/hello_hug.d.ts.map +1 -0
- package/dist/builtin_teams/hello_hug.js +68 -0
- package/dist/builtin_teams/hello_hug.js.map +1 -0
- package/dist/builtin_teams/hello_world.d.ts +8 -0
- package/dist/builtin_teams/hello_world.d.ts.map +1 -0
- package/dist/builtin_teams/hello_world.js +65 -0
- package/dist/builtin_teams/hello_world.js.map +1 -0
- package/dist/builtin_teams/hug_example.d.ts +10 -0
- package/dist/builtin_teams/hug_example.d.ts.map +1 -0
- package/dist/builtin_teams/hug_example.js +94 -0
- package/dist/builtin_teams/hug_example.js.map +1 -0
- package/dist/builtin_teams/index.d.ts +12 -0
- package/dist/builtin_teams/index.d.ts.map +1 -0
- package/dist/builtin_teams/index.js +18 -0
- package/dist/builtin_teams/index.js.map +1 -0
- package/dist/builtin_teams/types.d.ts +29 -0
- package/dist/builtin_teams/types.d.ts.map +1 -0
- package/dist/builtin_teams/types.js +3 -0
- package/dist/builtin_teams/types.js.map +1 -0
- package/dist/core_api_client.d.ts +48 -0
- package/dist/core_api_client.d.ts.map +1 -0
- package/dist/core_api_client.js +132 -0
- package/dist/core_api_client.js.map +1 -0
- package/dist/daemon_wire.d.ts +122 -0
- package/dist/daemon_wire.d.ts.map +1 -0
- package/dist/daemon_wire.js +105 -0
- package/dist/daemon_wire.js.map +1 -0
- package/dist/events/catalog.d.ts +40 -0
- package/dist/events/catalog.d.ts.map +1 -0
- package/dist/events/catalog.js +76 -0
- package/dist/events/catalog.js.map +1 -0
- package/dist/http_daemon_client.d.ts +136 -0
- package/dist/http_daemon_client.d.ts.map +1 -0
- package/dist/http_daemon_client.js +192 -0
- package/dist/http_daemon_client.js.map +1 -0
- package/dist/hug/ask.d.ts +160 -0
- package/dist/hug/ask.d.ts.map +1 -0
- package/dist/hug/ask.js +318 -0
- package/dist/hug/ask.js.map +1 -0
- package/dist/hug/attach.d.ts +60 -0
- package/dist/hug/attach.d.ts.map +1 -0
- package/dist/hug/attach.js +103 -0
- package/dist/hug/attach.js.map +1 -0
- package/dist/hug/bridge/index.d.ts +16 -0
- package/dist/hug/bridge/index.d.ts.map +1 -0
- package/dist/hug/bridge/index.js +13 -0
- package/dist/hug/bridge/index.js.map +1 -0
- package/dist/hug/bridge/messages.d.ts +559 -0
- package/dist/hug/bridge/messages.d.ts.map +1 -0
- package/dist/hug/bridge/messages.js +165 -0
- package/dist/hug/bridge/messages.js.map +1 -0
- package/dist/hug/client.d.ts +70 -0
- package/dist/hug/client.d.ts.map +1 -0
- package/dist/hug/client.js +144 -0
- package/dist/hug/client.js.map +1 -0
- package/dist/hug/context.d.ts +54 -0
- package/dist/hug/context.d.ts.map +1 -0
- package/dist/hug/context.js +27 -0
- package/dist/hug/context.js.map +1 -0
- package/dist/hug/define.d.ts +116 -0
- package/dist/hug/define.d.ts.map +1 -0
- package/dist/hug/define.js +119 -0
- package/dist/hug/define.js.map +1 -0
- package/dist/hug/definitions/approval.d.ts +216 -0
- package/dist/hug/definitions/approval.d.ts.map +1 -0
- package/dist/hug/definitions/approval.js +50 -0
- package/dist/hug/definitions/approval.js.map +1 -0
- package/dist/hug/definitions/common.d.ts +122 -0
- package/dist/hug/definitions/common.d.ts.map +1 -0
- package/dist/hug/definitions/common.js +70 -0
- package/dist/hug/definitions/common.js.map +1 -0
- package/dist/hug/definitions/form.d.ts +197 -0
- package/dist/hug/definitions/form.d.ts.map +1 -0
- package/dist/hug/definitions/form.js +29 -0
- package/dist/hug/definitions/form.js.map +1 -0
- package/dist/hug/definitions/index.d.ts +10 -0
- package/dist/hug/definitions/index.d.ts.map +1 -0
- package/dist/hug/definitions/index.js +8 -0
- package/dist/hug/definitions/index.js.map +1 -0
- package/dist/hug/errors.d.ts +30 -0
- package/dist/hug/errors.d.ts.map +1 -0
- package/dist/hug/errors.js +46 -0
- package/dist/hug/errors.js.map +1 -0
- package/dist/hug/file_checks.d.ts +41 -0
- package/dist/hug/file_checks.d.ts.map +1 -0
- package/dist/hug/file_checks.js +105 -0
- package/dist/hug/file_checks.js.map +1 -0
- package/dist/hug/files.d.ts +137 -0
- package/dist/hug/files.d.ts.map +1 -0
- package/dist/hug/files.js +283 -0
- package/dist/hug/files.js.map +1 -0
- package/dist/hug/hug_bundle.d.ts +70 -0
- package/dist/hug/hug_bundle.d.ts.map +1 -0
- package/dist/hug/hug_bundle.js +299 -0
- package/dist/hug/hug_bundle.js.map +1 -0
- package/dist/hug/hug_catalog.d.ts +38 -0
- package/dist/hug/hug_catalog.d.ts.map +1 -0
- package/dist/hug/hug_catalog.js +67 -0
- package/dist/hug/hug_catalog.js.map +1 -0
- package/dist/hug/hug_definitions.d.ts +32 -0
- package/dist/hug/hug_definitions.d.ts.map +1 -0
- package/dist/hug/hug_definitions.js +74 -0
- package/dist/hug/hug_definitions.js.map +1 -0
- package/dist/hug/index.d.ts +37 -0
- package/dist/hug/index.d.ts.map +1 -0
- package/dist/hug/index.js +23 -0
- package/dist/hug/index.js.map +1 -0
- package/dist/hug/llm.d.ts +106 -0
- package/dist/hug/llm.d.ts.map +1 -0
- package/dist/hug/llm.js +250 -0
- package/dist/hug/llm.js.map +1 -0
- package/dist/hug/policies.d.ts +65 -0
- package/dist/hug/policies.d.ts.map +1 -0
- package/dist/hug/policies.js +80 -0
- package/dist/hug/policies.js.map +1 -0
- package/dist/hug/problems.d.ts +35 -0
- package/dist/hug/problems.d.ts.map +1 -0
- package/dist/hug/problems.js +53 -0
- package/dist/hug/problems.js.map +1 -0
- package/dist/hug/requested_inputs.d.ts +190 -0
- package/dist/hug/requested_inputs.d.ts.map +1 -0
- package/dist/hug/requested_inputs.js +220 -0
- package/dist/hug/requested_inputs.js.map +1 -0
- package/dist/hug/schema_for_inputs.d.ts +20 -0
- package/dist/hug/schema_for_inputs.d.ts.map +1 -0
- package/dist/hug/schema_for_inputs.js +95 -0
- package/dist/hug/schema_for_inputs.js.map +1 -0
- package/dist/hug/session.d.ts +113 -0
- package/dist/hug/session.d.ts.map +1 -0
- package/dist/hug/session.js +210 -0
- package/dist/hug/session.js.map +1 -0
- package/dist/hug/types.d.ts +141 -0
- package/dist/hug/types.d.ts.map +1 -0
- package/dist/hug/types.js +80 -0
- package/dist/hug/types.js.map +1 -0
- package/dist/index.d.ts +72 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +54 -0
- package/dist/index.js.map +1 -0
- package/dist/logging.d.ts +172 -0
- package/dist/logging.d.ts.map +1 -0
- package/dist/logging.js +306 -0
- package/dist/logging.js.map +1 -0
- package/dist/mcp.d.ts +119 -0
- package/dist/mcp.d.ts.map +1 -0
- package/dist/mcp.js +259 -0
- package/dist/mcp.js.map +1 -0
- package/dist/mcp_client.d.ts +145 -0
- package/dist/mcp_client.d.ts.map +1 -0
- package/dist/mcp_client.js +374 -0
- package/dist/mcp_client.js.map +1 -0
- package/dist/paths.d.ts +20 -0
- package/dist/paths.d.ts.map +1 -0
- package/dist/paths.js +20 -0
- package/dist/paths.js.map +1 -0
- package/dist/protocol/factory.d.ts +32 -0
- package/dist/protocol/factory.d.ts.map +1 -0
- package/dist/protocol/factory.js +31 -0
- package/dist/protocol/factory.js.map +1 -0
- package/dist/protocol/framing.d.ts +35 -0
- package/dist/protocol/framing.d.ts.map +1 -0
- package/dist/protocol/framing.js +73 -0
- package/dist/protocol/framing.js.map +1 -0
- package/dist/protocol/index.d.ts +13 -0
- package/dist/protocol/index.d.ts.map +1 -0
- package/dist/protocol/index.js +13 -0
- package/dist/protocol/index.js.map +1 -0
- package/dist/protocol/types/agent.d.ts +42 -0
- package/dist/protocol/types/agent.d.ts.map +1 -0
- package/dist/protocol/types/agent.js +9 -0
- package/dist/protocol/types/agent.js.map +1 -0
- package/dist/protocol/types/cliq_core.d.ts +79 -0
- package/dist/protocol/types/cliq_core.d.ts.map +1 -0
- package/dist/protocol/types/cliq_core.js +17 -0
- package/dist/protocol/types/cliq_core.js.map +1 -0
- package/dist/protocol/types/foundations.d.ts +41 -0
- package/dist/protocol/types/foundations.d.ts.map +1 -0
- package/dist/protocol/types/foundations.js +10 -0
- package/dist/protocol/types/foundations.js.map +1 -0
- package/dist/protocol/types/index.d.ts +32 -0
- package/dist/protocol/types/index.d.ts.map +1 -0
- package/dist/protocol/types/index.js +27 -0
- package/dist/protocol/types/index.js.map +1 -0
- package/dist/protocol/types/messages.d.ts +827 -0
- package/dist/protocol/types/messages.d.ts.map +1 -0
- package/dist/protocol/types/messages.js +117 -0
- package/dist/protocol/types/messages.js.map +1 -0
- package/dist/protocol/types/pipeline.d.ts +122 -0
- package/dist/protocol/types/pipeline.d.ts.map +1 -0
- package/dist/protocol/types/pipeline.js +16 -0
- package/dist/protocol/types/pipeline.js.map +1 -0
- package/dist/protocol/types/schemas.d.ts +814 -0
- package/dist/protocol/types/schemas.d.ts.map +1 -0
- package/dist/protocol/types/schemas.js +772 -0
- package/dist/protocol/types/schemas.js.map +1 -0
- package/dist/protocol/types/team.d.ts +42 -0
- package/dist/protocol/types/team.d.ts.map +1 -0
- package/dist/protocol/types/team.js +15 -0
- package/dist/protocol/types/team.js.map +1 -0
- package/dist/protocol/types/workspace.d.ts +46 -0
- package/dist/protocol/types/workspace.d.ts.map +1 -0
- package/dist/protocol/types/workspace.js +9 -0
- package/dist/protocol/types/workspace.js.map +1 -0
- package/dist/settings_keys.d.ts +12 -0
- package/dist/settings_keys.d.ts.map +1 -0
- package/dist/settings_keys.js +35 -0
- package/dist/settings_keys.js.map +1 -0
- package/dist/tcp/client.d.ts +15 -0
- package/dist/tcp/client.d.ts.map +1 -0
- package/dist/tcp/client.js +28 -0
- package/dist/tcp/client.js.map +1 -0
- package/dist/tcp/connection.d.ts +79 -0
- package/dist/tcp/connection.d.ts.map +1 -0
- package/dist/tcp/connection.js +153 -0
- package/dist/tcp/connection.js.map +1 -0
- package/dist/tcp/index.d.ts +10 -0
- package/dist/tcp/index.d.ts.map +1 -0
- package/dist/tcp/index.js +7 -0
- package/dist/tcp/index.js.map +1 -0
- package/dist/tcp/server.d.ts +31 -0
- package/dist/tcp/server.d.ts.map +1 -0
- package/dist/tcp/server.js +54 -0
- package/dist/tcp/server.js.map +1 -0
- package/dist/team/bundle.d.ts +88 -0
- package/dist/team/bundle.d.ts.map +1 -0
- package/dist/team/bundle.js +214 -0
- package/dist/team/bundle.js.map +1 -0
- package/dist/team/identity.d.ts +31 -0
- package/dist/team/identity.d.ts.map +1 -0
- package/dist/team/identity.js +83 -0
- package/dist/team/identity.js.map +1 -0
- package/dist/team/index.d.ts +13 -0
- package/dist/team/index.d.ts.map +1 -0
- package/dist/team/index.js +8 -0
- package/dist/team/index.js.map +1 -0
- package/dist/team/lint_bundle.d.ts +16 -0
- package/dist/team/lint_bundle.d.ts.map +1 -0
- package/dist/team/lint_bundle.js +35 -0
- package/dist/team/lint_bundle.js.map +1 -0
- package/dist/team/ref.d.ts +48 -0
- package/dist/team/ref.d.ts.map +1 -0
- package/dist/team/ref.js +70 -0
- package/dist/team/ref.js.map +1 -0
- package/dist/team/roles.d.ts +22 -0
- package/dist/team/roles.d.ts.map +1 -0
- package/dist/team/roles.js +33 -0
- package/dist/team/roles.js.map +1 -0
- package/dist/team/tree.d.ts +101 -0
- package/dist/team/tree.d.ts.map +1 -0
- package/dist/team/tree.js +125 -0
- package/dist/team/tree.js.map +1 -0
- package/dist/team_lint.d.ts +57 -0
- package/dist/team_lint.d.ts.map +1 -0
- package/dist/team_lint.js +277 -0
- package/dist/team_lint.js.map +1 -0
- package/dist/telemetry/stream_json_usage.d.ts +39 -0
- package/dist/telemetry/stream_json_usage.d.ts.map +1 -0
- package/dist/telemetry/stream_json_usage.js +139 -0
- package/dist/telemetry/stream_json_usage.js.map +1 -0
- package/dist/telemetry/usage.d.ts +288 -0
- package/dist/telemetry/usage.d.ts.map +1 -0
- package/dist/telemetry/usage.js +421 -0
- package/dist/telemetry/usage.js.map +1 -0
- package/dist/utils/duration.d.ts +11 -0
- package/dist/utils/duration.d.ts.map +1 -0
- package/dist/utils/duration.js +22 -0
- package/dist/utils/duration.js.map +1 -0
- package/dist/utils/redact.d.ts +26 -0
- package/dist/utils/redact.d.ts.map +1 -0
- package/dist/utils/redact.js +52 -0
- package/dist/utils/redact.js.map +1 -0
- package/dist/utils/url.d.ts +6 -0
- package/dist/utils/url.d.ts.map +1 -0
- package/dist/utils/url.js +11 -0
- package/dist/utils/url.js.map +1 -0
- package/package.json +72 -5
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GateAgent — base class for gate phase agents.
|
|
3
|
+
*
|
|
4
|
+
* Extends LlmAgent — gate phases use LLM capabilities to evaluate
|
|
5
|
+
* upstream output and render a verdict.
|
|
6
|
+
*
|
|
7
|
+
* The agent calls exactly one of `ctx.pass()`, `ctx.route()`, or
|
|
8
|
+
* `ctx.escalate()`. Each emits a `gate_verdict` event and then
|
|
9
|
+
* calls `phase_complete` to the daemon.
|
|
10
|
+
*/
|
|
11
|
+
import type { Invoke } from '../protocol/types/index.js';
|
|
12
|
+
import { LlmAgent, type LlmAgentOptions } from './llm_agent.js';
|
|
13
|
+
import type { EventPoster } from './base_agent.js';
|
|
14
|
+
import type { GateContext, AgentOutput } from './types.js';
|
|
15
|
+
/** Options for a {@link GateAgent}; same as the LLM agent's, named so gate-specific options can be added later. */
|
|
16
|
+
export interface GateAgentOptions extends LlmAgentOptions {
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Base class for gate agents. Subclasses override `on_invoke` and call one of
|
|
20
|
+
* `ctx.pass()`, `ctx.route()` or `ctx.escalate()`. The constructor also accepts
|
|
21
|
+
* just a name (default `gate`).
|
|
22
|
+
*/
|
|
23
|
+
export declare abstract class GateAgent extends LlmAgent {
|
|
24
|
+
constructor(options?: GateAgentOptions | string);
|
|
25
|
+
/**
|
|
26
|
+
* A gate's `on_invoke` is its code, run from the top again on a resume (not LlmAgent's saved conversation): its
|
|
27
|
+
* `ctx.ask` repeats and must find the Hug it opened before by position. Without this, a resumed gate — after a
|
|
28
|
+
* cliqd restart, or an expiry while cliqd was down — numbered its ask past that Hug and opened a second one, and
|
|
29
|
+
* the run waited on it forever (HugAgent: "its ctx.ask finds the same Hug").
|
|
30
|
+
*/
|
|
31
|
+
protected replays_asks(): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Default call_llm — gate agents override on_invoke directly and
|
|
34
|
+
* handle LLM calls themselves, so this is a no-op fallback.
|
|
35
|
+
*/
|
|
36
|
+
protected call_llm(_ctx: import('./types.js').LlmContext): Promise<import('./turn_result.js').TurnResult>;
|
|
37
|
+
protected build_context(invoke: Invoke, post_event: EventPoster, transport?: import('./base_agent.js').AgentDaemonTransport): GateContext;
|
|
38
|
+
protected abstract on_invoke(ctx: GateContext): Promise<AgentOutput | void>;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=gate_agent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gate_agent.d.ts","sourceRoot":"","sources":["../../src/agents/gate_agent.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAC;AACzD,OAAO,EAAE,QAAQ,EAAE,KAAK,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAChE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AACnD,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAG3D,mHAAmH;AACnH,MAAM,WAAW,gBAAiB,SAAQ,eAAe;CAAG;AAE5D;;;;GAIG;AACH,8BAAsB,SAAU,SAAQ,QAAQ;gBAChC,OAAO,CAAC,EAAE,gBAAgB,GAAG,MAAM;IAO/C;;;;;OAKG;cACgB,YAAY,IAAI,OAAO;IAI1C;;;OAGG;IACH,SAAS,CAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,YAAY,EAAE,UAAU,GAAG,OAAO,CAAC,OAAO,kBAAkB,EAAE,UAAU,CAAC;cAItF,aAAa,CAC5B,MAAM,EAAE,MAAM,EACd,UAAU,EAAE,WAAW,EACvB,SAAS,CAAC,EAAE,OAAO,iBAAiB,EAAE,oBAAoB,GAC3D,WAAW;uBAqEc,SAAS,CAAC,GAAG,EAAE,WAAW,GAAG,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC;CACvF"}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GateAgent — base class for gate phase agents.
|
|
3
|
+
*
|
|
4
|
+
* Extends LlmAgent — gate phases use LLM capabilities to evaluate
|
|
5
|
+
* upstream output and render a verdict.
|
|
6
|
+
*
|
|
7
|
+
* The agent calls exactly one of `ctx.pass()`, `ctx.route()`, or
|
|
8
|
+
* `ctx.escalate()`. Each emits a `gate_verdict` event and then
|
|
9
|
+
* calls `phase_complete` to the daemon.
|
|
10
|
+
*/
|
|
11
|
+
import { LlmAgent } from './llm_agent.js';
|
|
12
|
+
import { report_usage } from '../telemetry/usage.js';
|
|
13
|
+
/**
|
|
14
|
+
* Base class for gate agents. Subclasses override `on_invoke` and call one of
|
|
15
|
+
* `ctx.pass()`, `ctx.route()` or `ctx.escalate()`. The constructor also accepts
|
|
16
|
+
* just a name (default `gate`).
|
|
17
|
+
*/
|
|
18
|
+
export class GateAgent extends LlmAgent {
|
|
19
|
+
constructor(options) {
|
|
20
|
+
const opts = typeof options === 'string'
|
|
21
|
+
? { name: options }
|
|
22
|
+
: (options ?? { name: 'gate' });
|
|
23
|
+
super(opts);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* A gate's `on_invoke` is its code, run from the top again on a resume (not LlmAgent's saved conversation): its
|
|
27
|
+
* `ctx.ask` repeats and must find the Hug it opened before by position. Without this, a resumed gate — after a
|
|
28
|
+
* cliqd restart, or an expiry while cliqd was down — numbered its ask past that Hug and opened a second one, and
|
|
29
|
+
* the run waited on it forever (HugAgent: "its ctx.ask finds the same Hug").
|
|
30
|
+
*/
|
|
31
|
+
replays_asks() {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Default call_llm — gate agents override on_invoke directly and
|
|
36
|
+
* handle LLM calls themselves, so this is a no-op fallback.
|
|
37
|
+
*/
|
|
38
|
+
call_llm(_ctx) {
|
|
39
|
+
return Promise.resolve({ kind: 'complete', text: '' });
|
|
40
|
+
}
|
|
41
|
+
build_context(invoke, post_event, transport) {
|
|
42
|
+
const llm_ctx = super.build_context(invoke, post_event, transport);
|
|
43
|
+
const check_results = invoke.check_results ?? [];
|
|
44
|
+
const iteration = invoke.iteration ?? 1;
|
|
45
|
+
const max_iterations = invoke.max_iterations ?? 3;
|
|
46
|
+
const started_at_unix_ms = Date.now();
|
|
47
|
+
const provider = this.name;
|
|
48
|
+
let verdict_sent = false;
|
|
49
|
+
const send_verdict = (outcome, extra = {}) => {
|
|
50
|
+
if (verdict_sent)
|
|
51
|
+
return;
|
|
52
|
+
verdict_sent = true;
|
|
53
|
+
llm_ctx.emit('gate_verdict', {
|
|
54
|
+
phase: invoke.phase.name ?? 'unknown',
|
|
55
|
+
outcome,
|
|
56
|
+
...extra,
|
|
57
|
+
});
|
|
58
|
+
// Auto-usage: gate agents produce a `records` unit — one
|
|
59
|
+
// "record" per check-result evaluated, with the verdict
|
|
60
|
+
// stashed in extras. The daemon still emits a baseline;
|
|
61
|
+
// this call enriches it with agent-domain data.
|
|
62
|
+
const ended_at_unix_ms = Date.now();
|
|
63
|
+
const extras = {
|
|
64
|
+
'gate.verdict': outcome,
|
|
65
|
+
'gate.iteration': iteration,
|
|
66
|
+
'gate.max_iterations': max_iterations,
|
|
67
|
+
};
|
|
68
|
+
const target = extra['target'];
|
|
69
|
+
if (typeof target === 'string' && target)
|
|
70
|
+
extras['gate.route_target'] = target;
|
|
71
|
+
void report_usage({
|
|
72
|
+
agent_kind: 'gate',
|
|
73
|
+
provider,
|
|
74
|
+
started_at_unix_ms,
|
|
75
|
+
ended_at_unix_ms,
|
|
76
|
+
duration_ms: Math.max(0, ended_at_unix_ms - started_at_unix_ms),
|
|
77
|
+
outcome: outcome === 'PASS' ? 'success' : 'failure',
|
|
78
|
+
unit_kind: 'records',
|
|
79
|
+
units_in: check_results.length,
|
|
80
|
+
units_out: outcome === 'PASS' ? check_results.length : 0,
|
|
81
|
+
extras,
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
const gate_ctx = {
|
|
85
|
+
...llm_ctx,
|
|
86
|
+
check_results,
|
|
87
|
+
route_targets: invoke.route_targets ?? [],
|
|
88
|
+
iteration,
|
|
89
|
+
max_iterations,
|
|
90
|
+
pass(comment, data) {
|
|
91
|
+
send_verdict('PASS', { reason: comment });
|
|
92
|
+
gate_ctx.complete({ text: _verdict_text('PASS', comment), ...(data ? { data } : {}) });
|
|
93
|
+
},
|
|
94
|
+
route(target, comment, data) {
|
|
95
|
+
send_verdict('ROUTE', { target, reason: comment });
|
|
96
|
+
gate_ctx.complete({ text: _verdict_text('ROUTE', comment, target), ...(data ? { data } : {}) });
|
|
97
|
+
},
|
|
98
|
+
escalate(comment, data) {
|
|
99
|
+
send_verdict('ESCALATE', { reason: comment });
|
|
100
|
+
gate_ctx.complete({ text: _verdict_text('ESCALATE', comment), ...(data ? { data } : {}) });
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
return gate_ctx;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Build output text with an embedded ```gate_verdict fenced block.
|
|
108
|
+
*
|
|
109
|
+
* The orchestrator extracts the verdict from either the `gate_verdict`
|
|
110
|
+
* event OR the output text. Because events are fire-and-forget HTTP,
|
|
111
|
+
* the event can arrive after `phase.completed` — the fenced block in
|
|
112
|
+
* the text is the reliable fallback that guarantees the orchestrator
|
|
113
|
+
* always finds the verdict.
|
|
114
|
+
*/
|
|
115
|
+
function _verdict_text(outcome, reason, target) {
|
|
116
|
+
const obj = { outcome, reason };
|
|
117
|
+
if (target)
|
|
118
|
+
obj.target = target;
|
|
119
|
+
return `${outcome}: ${reason}\n\n\`\`\`gate_verdict\n${JSON.stringify(obj)}\n\`\`\``;
|
|
120
|
+
}
|
|
121
|
+
//# sourceMappingURL=gate_agent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gate_agent.js","sourceRoot":"","sources":["../../src/agents/gate_agent.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,EAAE,QAAQ,EAAwB,MAAM,gBAAgB,CAAC;AAGhE,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAKrD;;;;GAIG;AACH,MAAM,OAAgB,SAAU,SAAQ,QAAQ;IAC5C,YAAY,OAAmC;QAC3C,MAAM,IAAI,GAAoB,OAAO,OAAO,KAAK,QAAQ;YACrD,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE;YACnB,CAAC,CAAC,CAAC,OAAO,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC;QACpC,KAAK,CAAC,IAAI,CAAC,CAAC;IAChB,CAAC;IAED;;;;;OAK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AAO,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;YACpG,CAAC;YAED,QAAQ,CAAC,OAAe,EAAE,IAAqC;gBAC3D,YAAY,CAAC,UAAU,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,CAAC;gBAC9C,QAAQ,CAAC,QAAQ,CAAC,EAAE,IAAI,EAAE,aAAa,CAAC,UAAU,EAAE,OAAO,CAAC,EAAE,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;YAC/F,CAAC;SACJ,CAAC;QAEF,OAAO,QAAQ,CAAC;IACpB,CAAC;CAGJ;AAED;;;;;;;;GAQG;AACH,SAAS,aAAa,CAAC,OAAe,EAAE,MAAc,EAAE,MAAe;IACnE,MAAM,GAAG,GAA2B,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC;IACxD,IAAI,MAAM;QAAE,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC;IAChC,OAAO,GAAG,OAAO,KAAK,MAAM,2BAA2B,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,UAAU,CAAC;AACzF,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent class hierarchy — HTTP callback-based agent framework.
|
|
3
|
+
*
|
|
4
|
+
* Class tree:
|
|
5
|
+
*
|
|
6
|
+
* BaseAgent
|
|
7
|
+
* ├── StandardAgent (upstream, commands)
|
|
8
|
+
* │ ├── LlmAgent (prompt assembly, LLM call)
|
|
9
|
+
* │ │ ├── CliAgent (binary spawning, stdout capture)
|
|
10
|
+
* │ │ └── GateAgent (verdict helpers)
|
|
11
|
+
* │ └── ConnectorAgent (action, sources, targets)
|
|
12
|
+
*/
|
|
13
|
+
export type { AgentEventType, AgentEvent, BaseContext, StandardContext, GateContext, LlmContext, CliContext, ConnectorContext, AgentOutput, AgentLogger, Command, Source, Target, AgentCheckpoint, } from './types.js';
|
|
14
|
+
export { BaseAgent } from './base_agent.js';
|
|
15
|
+
export type { AgentStartOptions, EventPoster, AgentDaemonTransport } from './base_agent.js';
|
|
16
|
+
export { StandardAgent } from './standard_agent.js';
|
|
17
|
+
export { GateAgent } from './gate_agent.js';
|
|
18
|
+
export type { GateAgentOptions } from './gate_agent.js';
|
|
19
|
+
export { LlmAgent } from './llm_agent.js';
|
|
20
|
+
export type { LlmAgentOptions } from './llm_agent.js';
|
|
21
|
+
export { CliAgent } from './cli_agent.js';
|
|
22
|
+
export type { CliAgentOptions, CliResult } from './cli_agent.js';
|
|
23
|
+
export { build_cli_turn_prompt } from './cli_rehydrate.js';
|
|
24
|
+
export type { CliPromptParts } from './cli_rehydrate.js';
|
|
25
|
+
export { turn_from_text, turn_from_cli_stdout, extract_question, } from './turn_result.js';
|
|
26
|
+
export type { TurnResult, ChatMessage } from './turn_result.js';
|
|
27
|
+
export { CLIQ_ALLOWED_RESPONSE_INSTRUCTION, CLIQ_HUG_FENCE } from './llm_contract.js';
|
|
28
|
+
export { answers_from_resume } from './llm_agent.js';
|
|
29
|
+
export { normalize_event_artifacts, artifacts_from_event_payload, is_event_artifact_ref, is_event_artifact_inline, } from './event_artifacts.js';
|
|
30
|
+
export type { EventArtifact, EventArtifactRef, EventArtifactInline, } from './event_artifacts.js';
|
|
31
|
+
export { ConnectorAgent } from './connector_agent.js';
|
|
32
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/agents/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,YAAY,EACR,cAAc,EACd,UAAU,EACV,WAAW,EACX,eAAe,EACf,WAAW,EACX,UAAU,EACV,UAAU,EACV,gBAAgB,EAChB,WAAW,EACX,WA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Agent class hierarchy — HTTP callback-based agent framework.
|
|
3
|
+
*
|
|
4
|
+
* Class tree:
|
|
5
|
+
*
|
|
6
|
+
* BaseAgent
|
|
7
|
+
* ├── StandardAgent (upstream, commands)
|
|
8
|
+
* │ ├── LlmAgent (prompt assembly, LLM call)
|
|
9
|
+
* │ │ ├── CliAgent (binary spawning, stdout capture)
|
|
10
|
+
* │ │ └── GateAgent (verdict helpers)
|
|
11
|
+
* │ └── ConnectorAgent (action, sources, targets)
|
|
12
|
+
*/
|
|
13
|
+
// Base classes
|
|
14
|
+
export { BaseAgent } from './base_agent.js';
|
|
15
|
+
export { StandardAgent } from './standard_agent.js';
|
|
16
|
+
export { GateAgent } from './gate_agent.js';
|
|
17
|
+
// Domain-specific agents
|
|
18
|
+
export { LlmAgent } from './llm_agent.js';
|
|
19
|
+
export { CliAgent } from './cli_agent.js';
|
|
20
|
+
export { build_cli_turn_prompt } from './cli_rehydrate.js';
|
|
21
|
+
export { turn_from_text, turn_from_cli_stdout, extract_question, } from './turn_result.js';
|
|
22
|
+
export { CLIQ_ALLOWED_RESPONSE_INSTRUCTION, CLIQ_HUG_FENCE } from './llm_contract.js';
|
|
23
|
+
export { answers_from_resume } from './llm_agent.js';
|
|
24
|
+
export { normalize_event_artifacts, artifacts_from_event_payload, is_event_artifact_ref, is_event_artifact_inline, } from './event_artifacts.js';
|
|
25
|
+
export { ConnectorAgent } from './connector_agent.js';
|
|
26
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/agents/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAoBH,eAAe;AACf,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAG5C,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAG5C,yBAAyB;AACzB,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAG1C,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAG3D,OAAO,EACH,cAAc,EACd,oBAAoB,EACpB,gBAAgB,GACnB,MAAM,kBAAkB,CAAC;AAG1B,OAAO,EAAE,iCAAiC,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACtF,OAAO,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAErD,OAAO,EACH,yBAAyB,EACzB,4BAA4B,EAC5B,qBAAqB,EACrB,wBAAwB,GAC3B,MAAM,sBAAsB,CAAC;AAO9B,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LlmAgent — base class for LLM API agents (gemini-api, claude-api, openai-api).
|
|
3
|
+
*
|
|
4
|
+
* Extends StandardAgent by:
|
|
5
|
+
* - Assembling a prompt from role + requirement + upstream_text
|
|
6
|
+
* - Extracting model config from invoke.config and settings
|
|
7
|
+
* - Running a response-driven turn loop over `call_llm()` → TurnResult
|
|
8
|
+
*/
|
|
9
|
+
import type { Invoke } from '../protocol/types/index.js';
|
|
10
|
+
import { StandardAgent } from './standard_agent.js';
|
|
11
|
+
import type { EventPoster } from './base_agent.js';
|
|
12
|
+
import type { LlmContext, AgentOutput } from './types.js';
|
|
13
|
+
import type { ChatMessage, TurnResult } from './turn_result.js';
|
|
14
|
+
import { type PhaseHug } from '../hug/llm.js';
|
|
15
|
+
import type { BaseContext } from './types.js';
|
|
16
|
+
/**
|
|
17
|
+
* Args accepted by `LlmAgent.report_llm_usage`.
|
|
18
|
+
*
|
|
19
|
+
* Agents report tokens only — Hub calculates cost_usd from tokens
|
|
20
|
+
* via the model_pricing table. Do NOT add cost_usd here.
|
|
21
|
+
*/
|
|
22
|
+
export interface LlmUsageInputs {
|
|
23
|
+
started_at_unix_ms: number;
|
|
24
|
+
ended_at_unix_ms: number;
|
|
25
|
+
provider: string;
|
|
26
|
+
model?: string;
|
|
27
|
+
input_tokens?: number;
|
|
28
|
+
output_tokens?: number;
|
|
29
|
+
outcome?: 'success' | 'failure' | 'cancelled' | 'timeout';
|
|
30
|
+
exit_code?: number | null;
|
|
31
|
+
extras?: Record<string, string | number | boolean>;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Whether the model may reach the whole workspace: `workspace_access`, or its old name
|
|
35
|
+
* `needs_filesystem` — the phase's first, then the agent's setting. Off unless set.
|
|
36
|
+
*/
|
|
37
|
+
export declare function workspace_access(config: Record<string, unknown>, settings: Record<string, unknown>): boolean;
|
|
38
|
+
/** Times a model may resend a `cliq_hug` block that failed the check, per invoke (not counted as turns). */
|
|
39
|
+
export declare const MAX_ASK_RETRIES = 2;
|
|
40
|
+
/** Options for an {@link LlmAgent}. */
|
|
41
|
+
export interface LlmAgentOptions {
|
|
42
|
+
/** Agent name (used in logs and events). */
|
|
43
|
+
name: string;
|
|
44
|
+
/** Model used when neither config nor settings name one. Defaults to `gemini-2.0-flash`. */
|
|
45
|
+
default_model?: string;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Base class for agents that call a model. Adds model, api_key, prompt,
|
|
49
|
+
* temperature and max_turns to the context, and runs a multi-turn loop that
|
|
50
|
+
* subclasses drive by implementing `call_llm`.
|
|
51
|
+
*/
|
|
52
|
+
export declare abstract class LlmAgent extends StandardAgent {
|
|
53
|
+
/** A resumed LLM phase continues from its saved conversation; it never repeats an earlier ask. */
|
|
54
|
+
protected replays_asks(): boolean;
|
|
55
|
+
/** The hug this invoke's phase asks with (null: `form`), loaded at the start of `on_invoke`. */
|
|
56
|
+
protected phase_hug: PhaseHug | null;
|
|
57
|
+
readonly default_model: string;
|
|
58
|
+
constructor(options: LlmAgentOptions);
|
|
59
|
+
protected build_context(invoke: Invoke, post_event: EventPoster, transport?: import('./base_agent.js').AgentDaemonTransport): LlmContext;
|
|
60
|
+
protected assemble_prompt(role: string, requirement: string, upstream_text: string): string;
|
|
61
|
+
/**
|
|
62
|
+
* Response-driven multi-turn loop (Yamazaki W12).
|
|
63
|
+
*
|
|
64
|
+
* Seeds `messages` from invoke transcript + prompt, passes them on each
|
|
65
|
+
* `call_llm`, and branches on TurnResult. An ask yields (the process ends);
|
|
66
|
+
* the daemon re-invokes with the transcript and the answers.
|
|
67
|
+
*/
|
|
68
|
+
protected on_invoke(ctx: LlmContext): Promise<AgentOutput | void>;
|
|
69
|
+
/**
|
|
70
|
+
* A turn's ask, checked against the phase's hug before anyone is asked: asked (with yield — the phase's hug
|
|
71
|
+
* defaults apply and the process ends while people answer, §10.5; returns null), or the problems to send back.
|
|
72
|
+
* Any other turn result passes through.
|
|
73
|
+
*/
|
|
74
|
+
private settle_ask;
|
|
75
|
+
/**
|
|
76
|
+
* Nobody was asked and the reply isn't the answer: tell the model what's wrong so it sends the block again.
|
|
77
|
+
*
|
|
78
|
+
* @returns false when it has had its last chance (the phase fails with `INVALID_HUG`)
|
|
79
|
+
*/
|
|
80
|
+
private resend;
|
|
81
|
+
/** A turn that continues in process: the model's text, then any messages the turn appends to the transcript. */
|
|
82
|
+
private record_turn;
|
|
83
|
+
/**
|
|
84
|
+
* Build the message list for this invoke: transcript + system prompt + the answers, when this
|
|
85
|
+
* invoke resumes after a wait ({@link answers_from_resume}).
|
|
86
|
+
*/
|
|
87
|
+
protected seed_messages(ctx: LlmContext, answers?: string | null): ChatMessage[];
|
|
88
|
+
/**
|
|
89
|
+
* One provider call. Subclasses parse the provider response structure
|
|
90
|
+
* into a TurnResult (complete / continue / ask / fail).
|
|
91
|
+
*/
|
|
92
|
+
protected abstract call_llm(ctx: LlmContext): Promise<TurnResult>;
|
|
93
|
+
/**
|
|
94
|
+
* Report a UsageRecord for one LLM API call. Any missing token
|
|
95
|
+
* counts are treated as zero. Non-blocking — fire-and-forget via
|
|
96
|
+
* `report_usage`. Subclasses call this once per completed call
|
|
97
|
+
* (or once after aggregating a streaming/batched response).
|
|
98
|
+
*/
|
|
99
|
+
protected report_llm_usage(inputs: LlmUsageInputs): void;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* After a wait on a Hug, the answers as the next user turn (§10.5) — or null on a fresh start. Files
|
|
103
|
+
* people sent are saved in `workspace` first and the answers give their paths
|
|
104
|
+
* ({@link save_answer_files}), so the model reads them with its file tools.
|
|
105
|
+
*
|
|
106
|
+
* @throws HugExpiredError / HugCancelledError / HugError when the Hug ended without an answer
|
|
107
|
+
*/
|
|
108
|
+
export declare function answers_from_resume(ctx: Pick<BaseContext, 'resumed_outcome' | 'download'>, workspace: string): Promise<string | null>;
|
|
109
|
+
//# sourceMappingURL=llm_agent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llm_agent.d.ts","sourceRoot":"","sources":["../../src/agents/llm_agent.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAC;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AACnD,OAAO,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE1D,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAChE,OAAO,EAAmH,KAAK,QAAQ,EAAE,MAAM,eAAe,CAAC;AAI/J,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C;;;;;GAKG;AACH,MAAM,WAAW,cAAc;IAC3B,kBAAkB,EAAE,MAAM,CAAC;IAC3B,gBAAgB,EAAE,MAAM,CAAC;IACzB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,YAAY,CAAC,EAAE,MAAM,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IACH,OAAO,CAAC,MAAM;IAgBd,gHAAgH;IAChH,OAAO,CAAC,WAAW;IAMnB;;;OAGG;IACH,SAAS,CAAC,aAAa,CAAC,GAAG,EAAE,UAAU,EAAE,OAAO,GAAE,MAAM,GAAG,IAAW,GAAG,WAAW,EAAE;IA8BtF;;;OAGG;IACH,SAAS,CAAC,QAAQ,CAAC,QAAQ,CAAC,GAAG,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;IAEjE;;;;;OAKG;IACH,SAAS,CAAC,gBAAgB,CAAC,MAAM,EAAE,cAAc,GAAG,IAAI;CAkB3D;AAED;;;;;;GAMG;AACH,wBAAsB,mBAAmB,CAAC,GAAG,EAAE,IAAI,CAAC,WAAW,EAAE,iBAAiB,GAAG,UAAU,CAAC,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAqB3I"}
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LlmAgent — base class for LLM API agents (gemini-api, claude-api, openai-api).
|
|
3
|
+
*
|
|
4
|
+
* Extends StandardAgent by:
|
|
5
|
+
* - Assembling a prompt from role + requirement + upstream_text
|
|
6
|
+
* - Extracting model config from invoke.config and settings
|
|
7
|
+
* - Running a response-driven turn loop over `call_llm()` → TurnResult
|
|
8
|
+
*/
|
|
9
|
+
import { StandardAgent } from './standard_agent.js';
|
|
10
|
+
import { report_usage } from '../telemetry/usage.js';
|
|
11
|
+
import { allowed_response_instruction, answers_message, ask_as_fence, check_llm_ask, invalid_hug_message, load_phase_hug } from '../hug/llm.js';
|
|
12
|
+
import { error_for } from '../hug/ask.js';
|
|
13
|
+
import { save_answer_files } from '../hug/files.js';
|
|
14
|
+
function coerce_optional_number(value) {
|
|
15
|
+
if (typeof value === 'number' && Number.isFinite(value))
|
|
16
|
+
return value;
|
|
17
|
+
if (typeof value === 'string' && value.trim() !== '') {
|
|
18
|
+
const n = Number(value);
|
|
19
|
+
if (Number.isFinite(n))
|
|
20
|
+
return n;
|
|
21
|
+
}
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Whether the model may reach the whole workspace: `workspace_access`, or its old name
|
|
26
|
+
* `needs_filesystem` — the phase's first, then the agent's setting. Off unless set.
|
|
27
|
+
*/
|
|
28
|
+
export function workspace_access(config, settings) {
|
|
29
|
+
for (const source of [config, settings]) {
|
|
30
|
+
for (const key of ['workspace_access', 'needs_filesystem']) {
|
|
31
|
+
const v = source[key];
|
|
32
|
+
if (v !== undefined && v !== null && v !== '')
|
|
33
|
+
return v === true || v === 'true';
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
/** Turns allowed for one invoke: the configured `max_turns` (floored) when it is at least 1, otherwise 1. */
|
|
39
|
+
function turn_limit(configured) {
|
|
40
|
+
return (configured !== undefined && configured >= 1) ? Math.floor(configured) : 1;
|
|
41
|
+
}
|
|
42
|
+
/** Times a model may resend a `cliq_hug` block that failed the check, per invoke (not counted as turns). */
|
|
43
|
+
export const MAX_ASK_RETRIES = 2;
|
|
44
|
+
/** The error when a model's `cliq_hug` block still fails the check: nobody was asked, so the phase must not pass. */
|
|
45
|
+
function invalid_hug_error(problems) {
|
|
46
|
+
return `The model's question was not sent: its cliq_hug block failed the check — ${problems.join('; ')}`;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Base class for agents that call a model. Adds model, api_key, prompt,
|
|
50
|
+
* temperature and max_turns to the context, and runs a multi-turn loop that
|
|
51
|
+
* subclasses drive by implementing `call_llm`.
|
|
52
|
+
*/
|
|
53
|
+
export class LlmAgent extends StandardAgent {
|
|
54
|
+
/** A resumed LLM phase continues from its saved conversation; it never repeats an earlier ask. */
|
|
55
|
+
replays_asks() {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
/** The hug this invoke's phase asks with (null: `form`), loaded at the start of `on_invoke`. */
|
|
59
|
+
phase_hug = null;
|
|
60
|
+
default_model;
|
|
61
|
+
constructor(options) {
|
|
62
|
+
super(options.name);
|
|
63
|
+
this.default_model = options.default_model ?? 'gemini-2.0-flash';
|
|
64
|
+
}
|
|
65
|
+
build_context(invoke, post_event, transport) {
|
|
66
|
+
const standard = super.build_context(invoke, post_event, transport);
|
|
67
|
+
const config = invoke.config;
|
|
68
|
+
const settings = invoke.settings;
|
|
69
|
+
const model = String(config['model'] ?? settings['model'] ?? this.default_model);
|
|
70
|
+
const api_key = String(settings['api_key'] ?? '');
|
|
71
|
+
const temperature = coerce_optional_number(config['temperature'] ?? settings['temperature']);
|
|
72
|
+
const max_turns = coerce_optional_number(config['max_turns'] ?? settings['max_turns']);
|
|
73
|
+
const prompt = this.assemble_prompt(standard.role, standard.requirement, standard.upstream_text);
|
|
74
|
+
return {
|
|
75
|
+
...standard,
|
|
76
|
+
model,
|
|
77
|
+
api_key,
|
|
78
|
+
prompt,
|
|
79
|
+
temperature,
|
|
80
|
+
max_turns,
|
|
81
|
+
project_dir: invoke.project_dir,
|
|
82
|
+
workspace_dir: workspace_access(config, settings) ? invoke.project_dir : undefined,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
assemble_prompt(role, requirement, upstream_text) {
|
|
86
|
+
const parts = [];
|
|
87
|
+
if (role)
|
|
88
|
+
parts.push(role);
|
|
89
|
+
if (upstream_text) {
|
|
90
|
+
parts.push('## Previous Phase Output\n\n' + upstream_text);
|
|
91
|
+
}
|
|
92
|
+
if (requirement) {
|
|
93
|
+
parts.push('## Requirement\n\n' + requirement);
|
|
94
|
+
}
|
|
95
|
+
return parts.join('\n\n');
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Response-driven multi-turn loop (Yamazaki W12).
|
|
99
|
+
*
|
|
100
|
+
* Seeds `messages` from invoke transcript + prompt, passes them on each
|
|
101
|
+
* `call_llm`, and branches on TurnResult. An ask yields (the process ends);
|
|
102
|
+
* the daemon re-invokes with the transcript and the answers.
|
|
103
|
+
*/
|
|
104
|
+
async on_invoke(ctx) {
|
|
105
|
+
if (!ctx.api_key) {
|
|
106
|
+
ctx.error('MISSING_API_KEY', `No api_key in settings for agent ${ctx.agent.name ?? ctx.agent.id}`);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
// The hug this phase asks with (PLAN-hug-widgets §9.4): its instruction, and the check of the model's ask.
|
|
110
|
+
const phase_hug = await phase_hug_or_fail(ctx);
|
|
111
|
+
if (phase_hug === undefined)
|
|
112
|
+
return;
|
|
113
|
+
this.phase_hug = phase_hug;
|
|
114
|
+
const messages = this.seed_messages(ctx, await answers_from_resume(ctx, ctx.project_dir));
|
|
115
|
+
if (messages.length === 0) {
|
|
116
|
+
ctx.error('EMPTY_REQUIREMENT', 'No requirement or upstream context provided — nothing to do');
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const max_turns = turn_limit(ctx.max_turns);
|
|
120
|
+
const retries = { count: 0 };
|
|
121
|
+
for (let turn = 1; turn <= max_turns; turn++) {
|
|
122
|
+
ctx.progress(`LLM turn ${turn}/${max_turns}`, Math.min(90, turn * (80 / max_turns)));
|
|
123
|
+
const turn_ctx = { ...ctx, messages: [...messages] };
|
|
124
|
+
const result = await this.settle_ask(ctx, await this.call_llm(turn_ctx), phase_hug);
|
|
125
|
+
if (!result)
|
|
126
|
+
return; // asked: the phase yields while people answer
|
|
127
|
+
if (result.kind === 'invalid_ask') {
|
|
128
|
+
if (!this.resend(ctx, result, retries, messages, Math.min(90, turn * (80 / max_turns)), turn))
|
|
129
|
+
return;
|
|
130
|
+
turn--; // a resend is not a turn
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (result.kind === 'fail') {
|
|
134
|
+
ctx.error(result.code, result.message);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (result.kind === 'complete') {
|
|
138
|
+
ctx.emit('assistant_message', { content: result.text, turn });
|
|
139
|
+
messages.push({ role: 'assistant', content: result.text });
|
|
140
|
+
return { text: result.text };
|
|
141
|
+
}
|
|
142
|
+
// continue — another in-process turn
|
|
143
|
+
this.record_turn(ctx, result, turn, messages);
|
|
144
|
+
}
|
|
145
|
+
ctx.error('MAX_TURNS', `Exhausted max_turns (${max_turns}) without completion`);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* A turn's ask, checked against the phase's hug before anyone is asked: asked (with yield — the phase's hug
|
|
149
|
+
* defaults apply and the process ends while people answer, §10.5; returns null), or the problems to send back.
|
|
150
|
+
* Any other turn result passes through.
|
|
151
|
+
*/
|
|
152
|
+
async settle_ask(ctx, result, phase_hug) {
|
|
153
|
+
if (result.kind !== 'ask')
|
|
154
|
+
return result;
|
|
155
|
+
const checked = check_llm_ask(result.ask, phase_hug);
|
|
156
|
+
if (!checked.ok)
|
|
157
|
+
return { kind: 'invalid_ask', assistant_text: result.assistant_text ?? ask_as_fence(result.ask), problems: checked.problems };
|
|
158
|
+
await ctx.ask(checked.definition, { ...checked.options, wait: 'yield' });
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Nobody was asked and the reply isn't the answer: tell the model what's wrong so it sends the block again.
|
|
163
|
+
*
|
|
164
|
+
* @returns false when it has had its last chance (the phase fails with `INVALID_HUG`)
|
|
165
|
+
*/
|
|
166
|
+
resend(ctx, result, retries, messages, percent, turn) {
|
|
167
|
+
if (retries.count >= MAX_ASK_RETRIES) {
|
|
168
|
+
ctx.error('INVALID_HUG', invalid_hug_error(result.problems));
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
retries.count++;
|
|
172
|
+
ctx.emit('assistant_message', { content: result.assistant_text, turn });
|
|
173
|
+
ctx.progress(`The model's question failed the check; asking it to fix it (${retries.count}/${MAX_ASK_RETRIES})`, percent);
|
|
174
|
+
messages.push({ role: 'assistant', content: result.assistant_text });
|
|
175
|
+
messages.push({ role: 'user', content: invalid_hug_message(result.problems) });
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
/** A turn that continues in process: the model's text, then any messages the turn appends to the transcript. */
|
|
179
|
+
record_turn(ctx, result, turn, messages) {
|
|
180
|
+
ctx.emit('assistant_message', { content: result.assistant_text, turn });
|
|
181
|
+
messages.push({ role: 'assistant', content: result.assistant_text });
|
|
182
|
+
for (const m of result.append ?? [])
|
|
183
|
+
messages.push({ role: m.role, content: m.content });
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Build the message list for this invoke: transcript + system prompt + the answers, when this
|
|
187
|
+
* invoke resumes after a wait ({@link answers_from_resume}).
|
|
188
|
+
*/
|
|
189
|
+
seed_messages(ctx, answers = null) {
|
|
190
|
+
const messages = ctx.messages.map((m) => ({
|
|
191
|
+
role: m.role,
|
|
192
|
+
content: m.content,
|
|
193
|
+
}));
|
|
194
|
+
const has_system = messages.some((m) => m.role === 'system');
|
|
195
|
+
if (!has_system && ctx.prompt.trim()) {
|
|
196
|
+
messages.unshift({ role: 'system', content: ctx.prompt });
|
|
197
|
+
}
|
|
198
|
+
if (messages.length === 0 && ctx.prompt.trim()) {
|
|
199
|
+
messages.push({ role: 'user', content: ctx.prompt });
|
|
200
|
+
}
|
|
201
|
+
// Gemini (and similar) require non-empty `contents` — systemInstruction alone is not enough.
|
|
202
|
+
if (!messages.some((m) => m.role !== 'system')) {
|
|
203
|
+
const user_text = ctx.requirement?.trim()
|
|
204
|
+
|| 'Please proceed with the instructions in the system prompt.';
|
|
205
|
+
messages.push({ role: 'user', content: user_text });
|
|
206
|
+
}
|
|
207
|
+
if (answers)
|
|
208
|
+
messages.push({ role: 'user', content: answers });
|
|
209
|
+
// Guardrail: only the allowed endings (normal text, QUESTION:, a cliq_hug fence for the phase's hug).
|
|
210
|
+
messages.push({ role: 'system', content: allowed_response_instruction(this.phase_hug) });
|
|
211
|
+
return messages;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Report a UsageRecord for one LLM API call. Any missing token
|
|
215
|
+
* counts are treated as zero. Non-blocking — fire-and-forget via
|
|
216
|
+
* `report_usage`. Subclasses call this once per completed call
|
|
217
|
+
* (or once after aggregating a streaming/batched response).
|
|
218
|
+
*/
|
|
219
|
+
report_llm_usage(inputs) {
|
|
220
|
+
const duration_ms = inputs.ended_at_unix_ms - inputs.started_at_unix_ms;
|
|
221
|
+
const partial = {
|
|
222
|
+
agent_kind: 'llm',
|
|
223
|
+
provider: inputs.provider,
|
|
224
|
+
started_at_unix_ms: inputs.started_at_unix_ms,
|
|
225
|
+
ended_at_unix_ms: inputs.ended_at_unix_ms,
|
|
226
|
+
duration_ms: duration_ms >= 0 ? duration_ms : 0,
|
|
227
|
+
outcome: inputs.outcome ?? 'success',
|
|
228
|
+
unit_kind: 'tokens',
|
|
229
|
+
units_in: inputs.input_tokens ?? 0,
|
|
230
|
+
units_out: inputs.output_tokens ?? 0,
|
|
231
|
+
};
|
|
232
|
+
if (inputs.model)
|
|
233
|
+
partial.model = inputs.model;
|
|
234
|
+
if (inputs.exit_code !== undefined)
|
|
235
|
+
partial.exit_code = inputs.exit_code;
|
|
236
|
+
if (inputs.extras)
|
|
237
|
+
partial.extras = inputs.extras;
|
|
238
|
+
void report_usage(partial);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* After a wait on a Hug, the answers as the next user turn (§10.5) — or null on a fresh start. Files
|
|
243
|
+
* people sent are saved in `workspace` first and the answers give their paths
|
|
244
|
+
* ({@link save_answer_files}), so the model reads them with its file tools.
|
|
245
|
+
*
|
|
246
|
+
* @throws HugExpiredError / HugCancelledError / HugError when the Hug ended without an answer
|
|
247
|
+
*/
|
|
248
|
+
export async function answers_from_resume(ctx, workspace) {
|
|
249
|
+
const outcomes = ctx.resumed_outcome ?? [];
|
|
250
|
+
for (const { session, result } of outcomes) {
|
|
251
|
+
if (result?.kind === 'failed')
|
|
252
|
+
throw error_for(session.id, result.reason, result.response);
|
|
253
|
+
}
|
|
254
|
+
const answered = outcomes.filter((o) => o.result?.kind === 'done');
|
|
255
|
+
if (answered.length === 0)
|
|
256
|
+
return null;
|
|
257
|
+
const save = (ref, dest) => ctx.download(ref, dest);
|
|
258
|
+
const parts = [];
|
|
259
|
+
for (const { session, responses: checked } of answered) {
|
|
260
|
+
// Only the answers the hug's definition accepted (§9.4): a refused one never reaches the model.
|
|
261
|
+
const responses = [];
|
|
262
|
+
let saved = false;
|
|
263
|
+
for (const r of checked) {
|
|
264
|
+
const answer = await save_answer_files(save, { id: session.id, spec: session.spec }, r, workspace);
|
|
265
|
+
saved ||= answer.saved;
|
|
266
|
+
responses.push({ ...r, values: answer.values });
|
|
267
|
+
}
|
|
268
|
+
parts.push(answers_message(responses, saved));
|
|
269
|
+
}
|
|
270
|
+
return parts.join('\n\n');
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* The phase's hug (null: `form`), or undefined after failing the phase with `HUG_UNKNOWN` when this daemon can't see
|
|
274
|
+
* it (cliqd's pre-run check normally stops the run first).
|
|
275
|
+
*/
|
|
276
|
+
async function phase_hug_or_fail(ctx) {
|
|
277
|
+
try {
|
|
278
|
+
return await load_phase_hug();
|
|
279
|
+
}
|
|
280
|
+
catch (err) {
|
|
281
|
+
ctx.error('HUG_UNKNOWN', err instanceof Error ? err.message : String(err));
|
|
282
|
+
return undefined;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
//# sourceMappingURL=llm_agent.js.map
|