@warlock.js/ai 4.1.1
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 +72 -0
- package/cjs/index.cjs +8625 -0
- package/cjs/index.cjs.map +1 -0
- package/esm/agent/agent-config.type.d.mts +160 -0
- package/esm/agent/agent-config.type.d.mts.map +1 -0
- package/esm/agent/agent-input-builder.mjs +111 -0
- package/esm/agent/agent-input-builder.mjs.map +1 -0
- package/esm/agent/agent-log-event.mjs +101 -0
- package/esm/agent/agent-log-event.mjs.map +1 -0
- package/esm/agent/agent-stream.d.mts +47 -0
- package/esm/agent/agent-stream.d.mts.map +1 -0
- package/esm/agent/agent-stream.mjs +82 -0
- package/esm/agent/agent-stream.mjs.map +1 -0
- package/esm/agent/agent-to-stream-event.mjs +86 -0
- package/esm/agent/agent-to-stream-event.mjs.map +1 -0
- package/esm/agent/agent.d.mts +38 -0
- package/esm/agent/agent.d.mts.map +1 -0
- package/esm/agent/agent.mjs +974 -0
- package/esm/agent/agent.mjs.map +1 -0
- package/esm/agent/index.d.mts +3 -0
- package/esm/agent/index.mjs +4 -0
- package/esm/agent/json-stream-guard.mjs +400 -0
- package/esm/agent/json-stream-guard.mjs.map +1 -0
- package/esm/ai.d.mts +64 -0
- package/esm/ai.d.mts.map +1 -0
- package/esm/ai.mjs +65 -0
- package/esm/ai.mjs.map +1 -0
- package/esm/config.d.mts +79 -0
- package/esm/config.d.mts.map +1 -0
- package/esm/config.mjs +43 -0
- package/esm/config.mjs.map +1 -0
- package/esm/contracts/agent/agent-options.type.d.mts +132 -0
- package/esm/contracts/agent/agent-options.type.d.mts.map +1 -0
- package/esm/contracts/agent/agent.contract.d.mts +92 -0
- package/esm/contracts/agent/agent.contract.d.mts.map +1 -0
- package/esm/contracts/agent/index.d.mts +2 -0
- package/esm/contracts/attachment.type.d.mts +100 -0
- package/esm/contracts/attachment.type.d.mts.map +1 -0
- package/esm/contracts/content-part.type.d.mts +55 -0
- package/esm/contracts/content-part.type.d.mts.map +1 -0
- package/esm/contracts/conversation-message.type.d.mts +51 -0
- package/esm/contracts/conversation-message.type.d.mts.map +1 -0
- package/esm/contracts/embedder.contract.d.mts +69 -0
- package/esm/contracts/embedder.contract.d.mts.map +1 -0
- package/esm/contracts/end.type.d.mts +53 -0
- package/esm/contracts/end.type.d.mts.map +1 -0
- package/esm/contracts/end.type.mjs +47 -0
- package/esm/contracts/end.type.mjs.map +1 -0
- package/esm/contracts/events/agent-events.type.d.mts +71 -0
- package/esm/contracts/events/agent-events.type.d.mts.map +1 -0
- package/esm/contracts/events/complete-event.type.d.mts +33 -0
- package/esm/contracts/events/complete-event.type.d.mts.map +1 -0
- package/esm/contracts/events/event-identity.type.d.mts +30 -0
- package/esm/contracts/events/event-identity.type.d.mts.map +1 -0
- package/esm/contracts/events/event-map.type.d.mts +80 -0
- package/esm/contracts/events/event-map.type.d.mts.map +1 -0
- package/esm/contracts/events/index.d.mts +7 -0
- package/esm/contracts/events/supervisor-events.type.d.mts +162 -0
- package/esm/contracts/events/supervisor-events.type.d.mts.map +1 -0
- package/esm/contracts/events/usage-event.type.d.mts +39 -0
- package/esm/contracts/events/usage-event.type.d.mts.map +1 -0
- package/esm/contracts/events/workflow-events.type.d.mts +51 -0
- package/esm/contracts/events/workflow-events.type.d.mts.map +1 -0
- package/esm/contracts/executable.contract.d.mts +25 -0
- package/esm/contracts/executable.contract.d.mts.map +1 -0
- package/esm/contracts/finish-reason.type.d.mts +17 -0
- package/esm/contracts/finish-reason.type.d.mts.map +1 -0
- package/esm/contracts/index.d.mts +63 -0
- package/esm/contracts/middleware/index.d.mts +3 -0
- package/esm/contracts/middleware/middleware-context.type.d.mts +99 -0
- package/esm/contracts/middleware/middleware-context.type.d.mts.map +1 -0
- package/esm/contracts/middleware/middleware-state.type.d.mts +34 -0
- package/esm/contracts/middleware/middleware-state.type.d.mts.map +1 -0
- package/esm/contracts/middleware/middleware.contract.d.mts +140 -0
- package/esm/contracts/middleware/middleware.contract.d.mts.map +1 -0
- package/esm/contracts/model-tool-call-request.type.d.mts +58 -0
- package/esm/contracts/model-tool-call-request.type.d.mts.map +1 -0
- package/esm/contracts/model.contract.d.mts +164 -0
- package/esm/contracts/model.contract.d.mts.map +1 -0
- package/esm/contracts/placeholders.type.d.mts +23 -0
- package/esm/contracts/placeholders.type.d.mts.map +1 -0
- package/esm/contracts/result/agent-result.type.d.mts +33 -0
- package/esm/contracts/result/agent-result.type.d.mts.map +1 -0
- package/esm/contracts/result/attempt-entry.type.d.mts +40 -0
- package/esm/contracts/result/attempt-entry.type.d.mts.map +1 -0
- package/esm/contracts/result/base-report.type.d.mts +137 -0
- package/esm/contracts/result/base-report.type.d.mts.map +1 -0
- package/esm/contracts/result/base-report.type.mjs +17 -0
- package/esm/contracts/result/base-report.type.mjs.map +1 -0
- package/esm/contracts/result/base-result.type.d.mts +26 -0
- package/esm/contracts/result/base-result.type.d.mts.map +1 -0
- package/esm/contracts/result/execute-result.type.d.mts +40 -0
- package/esm/contracts/result/execute-result.type.d.mts.map +1 -0
- package/esm/contracts/result/execution-report.type.d.mts +50 -0
- package/esm/contracts/result/execution-report.type.d.mts.map +1 -0
- package/esm/contracts/result/index.d.mts +14 -0
- package/esm/contracts/result/llm-trip.type.d.mts +36 -0
- package/esm/contracts/result/llm-trip.type.d.mts.map +1 -0
- package/esm/contracts/result/model-pricing.type.d.mts +51 -0
- package/esm/contracts/result/model-pricing.type.d.mts.map +1 -0
- package/esm/contracts/result/session-send-result.type.d.mts +29 -0
- package/esm/contracts/result/session-send-result.type.d.mts.map +1 -0
- package/esm/contracts/result/step-result.type.d.mts +43 -0
- package/esm/contracts/result/step-result.type.d.mts.map +1 -0
- package/esm/contracts/result/supervisor-result.type.d.mts +86 -0
- package/esm/contracts/result/supervisor-result.type.d.mts.map +1 -0
- package/esm/contracts/result/tool-call.type.d.mts +44 -0
- package/esm/contracts/result/tool-call.type.d.mts.map +1 -0
- package/esm/contracts/result/usage.type.d.mts +57 -0
- package/esm/contracts/result/usage.type.d.mts.map +1 -0
- package/esm/contracts/result/workflow-result.type.d.mts +45 -0
- package/esm/contracts/result/workflow-result.type.d.mts.map +1 -0
- package/esm/contracts/sdk-adapter.contract.d.mts +77 -0
- package/esm/contracts/sdk-adapter.contract.d.mts.map +1 -0
- package/esm/contracts/stream/stream-event.type.d.mts +68 -0
- package/esm/contracts/stream/stream-event.type.d.mts.map +1 -0
- package/esm/contracts/stream/stream.contract.d.mts +61 -0
- package/esm/contracts/stream/stream.contract.d.mts.map +1 -0
- package/esm/contracts/streaming-tool-guard-config.type.d.mts +78 -0
- package/esm/contracts/streaming-tool-guard-config.type.d.mts.map +1 -0
- package/esm/contracts/supervisor/ack-entry.type.d.mts +97 -0
- package/esm/contracts/supervisor/ack-entry.type.d.mts.map +1 -0
- package/esm/contracts/supervisor/classifier-context.type.d.mts +189 -0
- package/esm/contracts/supervisor/classifier-context.type.d.mts.map +1 -0
- package/esm/contracts/supervisor/dispatch-context.type.d.mts +235 -0
- package/esm/contracts/supervisor/dispatch-context.type.d.mts.map +1 -0
- package/esm/contracts/supervisor/evaluate-context.type.d.mts +124 -0
- package/esm/contracts/supervisor/evaluate-context.type.d.mts.map +1 -0
- package/esm/contracts/supervisor/index.d.mts +16 -0
- package/esm/contracts/supervisor/intent-entry.type.d.mts +254 -0
- package/esm/contracts/supervisor/intent-entry.type.d.mts.map +1 -0
- package/esm/contracts/supervisor/iteration-snapshot.type.d.mts +117 -0
- package/esm/contracts/supervisor/iteration-snapshot.type.d.mts.map +1 -0
- package/esm/contracts/supervisor/next.type.d.mts +25 -0
- package/esm/contracts/supervisor/next.type.d.mts.map +1 -0
- package/esm/contracts/supervisor/route-context.type.d.mts +91 -0
- package/esm/contracts/supervisor/route-context.type.d.mts.map +1 -0
- package/esm/contracts/supervisor/router-entry.type.d.mts +67 -0
- package/esm/contracts/supervisor/router-entry.type.d.mts.map +1 -0
- package/esm/contracts/supervisor/supervisor-config.type.d.mts +356 -0
- package/esm/contracts/supervisor/supervisor-config.type.d.mts.map +1 -0
- package/esm/contracts/supervisor/supervisor-execute-options.type.d.mts +90 -0
- package/esm/contracts/supervisor/supervisor-execute-options.type.d.mts.map +1 -0
- package/esm/contracts/supervisor/supervisor-input.type.d.mts +24 -0
- package/esm/contracts/supervisor/supervisor-input.type.d.mts.map +1 -0
- package/esm/contracts/supervisor/supervisor-snapshot.type.d.mts +49 -0
- package/esm/contracts/supervisor/supervisor-snapshot.type.d.mts.map +1 -0
- package/esm/contracts/supervisor/supervisor-stream-event.type.d.mts +16 -0
- package/esm/contracts/supervisor/supervisor-stream-event.type.d.mts.map +1 -0
- package/esm/contracts/supervisor/supervisor.contract.d.mts +126 -0
- package/esm/contracts/supervisor/supervisor.contract.d.mts.map +1 -0
- package/esm/contracts/system-prompt.contract.d.mts +118 -0
- package/esm/contracts/system-prompt.contract.d.mts.map +1 -0
- package/esm/contracts/tool.contract.d.mts +161 -0
- package/esm/contracts/tool.contract.d.mts.map +1 -0
- package/esm/contracts/workflow/index.d.mts +6 -0
- package/esm/contracts/workflow/next-step-result.type.d.mts +17 -0
- package/esm/contracts/workflow/next-step-result.type.d.mts.map +1 -0
- package/esm/contracts/workflow/retry-config.type.d.mts +19 -0
- package/esm/contracts/workflow/retry-config.type.d.mts.map +1 -0
- package/esm/contracts/workflow/step.contract.d.mts +189 -0
- package/esm/contracts/workflow/step.contract.d.mts.map +1 -0
- package/esm/contracts/workflow/workflow-context.type.d.mts +45 -0
- package/esm/contracts/workflow/workflow-context.type.d.mts.map +1 -0
- package/esm/contracts/workflow/workflow-snapshot.type.d.mts +24 -0
- package/esm/contracts/workflow/workflow-snapshot.type.d.mts.map +1 -0
- package/esm/contracts/workflow/workflow.contract.d.mts +142 -0
- package/esm/contracts/workflow/workflow.contract.d.mts.map +1 -0
- package/esm/errors/agent-cancelled-error.d.mts +49 -0
- package/esm/errors/agent-cancelled-error.d.mts.map +1 -0
- package/esm/errors/agent-cancelled-error.mjs +43 -0
- package/esm/errors/agent-cancelled-error.mjs.map +1 -0
- package/esm/errors/agent-execution-error.d.mts +32 -0
- package/esm/errors/agent-execution-error.d.mts.map +1 -0
- package/esm/errors/agent-execution-error.mjs +35 -0
- package/esm/errors/agent-execution-error.mjs.map +1 -0
- package/esm/errors/agent-max-trips-error.d.mts +45 -0
- package/esm/errors/agent-max-trips-error.d.mts.map +1 -0
- package/esm/errors/agent-max-trips-error.mjs +41 -0
- package/esm/errors/agent-max-trips-error.mjs.map +1 -0
- package/esm/errors/ai-error.d.mts +94 -0
- package/esm/errors/ai-error.d.mts.map +1 -0
- package/esm/errors/ai-error.mjs +68 -0
- package/esm/errors/ai-error.mjs.map +1 -0
- package/esm/errors/budget-exceeded-error.d.mts +41 -0
- package/esm/errors/budget-exceeded-error.d.mts.map +1 -0
- package/esm/errors/budget-exceeded-error.mjs +31 -0
- package/esm/errors/budget-exceeded-error.mjs.map +1 -0
- package/esm/errors/content-filter-error.d.mts +33 -0
- package/esm/errors/content-filter-error.d.mts.map +1 -0
- package/esm/errors/content-filter-error.mjs +28 -0
- package/esm/errors/content-filter-error.mjs.map +1 -0
- package/esm/errors/context-length-exceeded-error.d.mts +38 -0
- package/esm/errors/context-length-exceeded-error.d.mts.map +1 -0
- package/esm/errors/context-length-exceeded-error.mjs +31 -0
- package/esm/errors/context-length-exceeded-error.mjs.map +1 -0
- package/esm/errors/error-category.type.d.mts +30 -0
- package/esm/errors/error-category.type.d.mts.map +1 -0
- package/esm/errors/error-code.type.d.mts +19 -0
- package/esm/errors/error-code.type.d.mts.map +1 -0
- package/esm/errors/guardrail-violation-error.d.mts +55 -0
- package/esm/errors/guardrail-violation-error.d.mts.map +1 -0
- package/esm/errors/guardrail-violation-error.mjs +41 -0
- package/esm/errors/guardrail-violation-error.mjs.map +1 -0
- package/esm/errors/index.d.mts +29 -0
- package/esm/errors/index.mjs +29 -0
- package/esm/errors/invalid-request-error.d.mts +27 -0
- package/esm/errors/invalid-request-error.d.mts.map +1 -0
- package/esm/errors/invalid-request-error.mjs +31 -0
- package/esm/errors/invalid-request-error.mjs.map +1 -0
- package/esm/errors/max-iterations-error.d.mts +31 -0
- package/esm/errors/max-iterations-error.d.mts.map +1 -0
- package/esm/errors/max-iterations-error.mjs +32 -0
- package/esm/errors/max-iterations-error.mjs.map +1 -0
- package/esm/errors/max-steps-exceeded-error.d.mts +20 -0
- package/esm/errors/max-steps-exceeded-error.d.mts.map +1 -0
- package/esm/errors/max-steps-exceeded-error.mjs +21 -0
- package/esm/errors/max-steps-exceeded-error.mjs.map +1 -0
- package/esm/errors/provider-auth-error.d.mts +22 -0
- package/esm/errors/provider-auth-error.d.mts.map +1 -0
- package/esm/errors/provider-auth-error.mjs +26 -0
- package/esm/errors/provider-auth-error.mjs.map +1 -0
- package/esm/errors/provider-error.d.mts +38 -0
- package/esm/errors/provider-error.d.mts.map +1 -0
- package/esm/errors/provider-error.mjs +42 -0
- package/esm/errors/provider-error.mjs.map +1 -0
- package/esm/errors/provider-rate-limit-error.d.mts +34 -0
- package/esm/errors/provider-rate-limit-error.d.mts.map +1 -0
- package/esm/errors/provider-rate-limit-error.mjs +31 -0
- package/esm/errors/provider-rate-limit-error.mjs.map +1 -0
- package/esm/errors/provider-timeout-error.d.mts +22 -0
- package/esm/errors/provider-timeout-error.d.mts.map +1 -0
- package/esm/errors/provider-timeout-error.mjs +26 -0
- package/esm/errors/provider-timeout-error.mjs.map +1 -0
- package/esm/errors/quota-exceeded-error.d.mts +36 -0
- package/esm/errors/quota-exceeded-error.d.mts.map +1 -0
- package/esm/errors/quota-exceeded-error.mjs +40 -0
- package/esm/errors/quota-exceeded-error.mjs.map +1 -0
- package/esm/errors/routing-error.d.mts +23 -0
- package/esm/errors/routing-error.d.mts.map +1 -0
- package/esm/errors/routing-error.mjs +23 -0
- package/esm/errors/routing-error.mjs.map +1 -0
- package/esm/errors/schema-validation-error.d.mts +40 -0
- package/esm/errors/schema-validation-error.d.mts.map +1 -0
- package/esm/errors/schema-validation-error.mjs +37 -0
- package/esm/errors/schema-validation-error.mjs.map +1 -0
- package/esm/errors/step-failed-error.d.mts +20 -0
- package/esm/errors/step-failed-error.d.mts.map +1 -0
- package/esm/errors/step-failed-error.mjs +19 -0
- package/esm/errors/step-failed-error.mjs.map +1 -0
- package/esm/errors/supervisor-cancelled-error.d.mts +29 -0
- package/esm/errors/supervisor-cancelled-error.d.mts.map +1 -0
- package/esm/errors/supervisor-cancelled-error.mjs +29 -0
- package/esm/errors/supervisor-cancelled-error.mjs.map +1 -0
- package/esm/errors/supervisor-drift-error.d.mts +31 -0
- package/esm/errors/supervisor-drift-error.d.mts.map +1 -0
- package/esm/errors/supervisor-drift-error.mjs +30 -0
- package/esm/errors/supervisor-drift-error.mjs.map +1 -0
- package/esm/errors/supervisor-failed-error.d.mts +40 -0
- package/esm/errors/supervisor-failed-error.d.mts.map +1 -0
- package/esm/errors/supervisor-failed-error.mjs +43 -0
- package/esm/errors/supervisor-failed-error.mjs.map +1 -0
- package/esm/errors/supervisor-routing-error.d.mts +44 -0
- package/esm/errors/supervisor-routing-error.d.mts.map +1 -0
- package/esm/errors/supervisor-routing-error.mjs +39 -0
- package/esm/errors/supervisor-routing-error.mjs.map +1 -0
- package/esm/errors/tool-execution-error.d.mts +35 -0
- package/esm/errors/tool-execution-error.d.mts.map +1 -0
- package/esm/errors/tool-execution-error.mjs +32 -0
- package/esm/errors/tool-execution-error.mjs.map +1 -0
- package/esm/errors/workflow-cancelled-error.d.mts +23 -0
- package/esm/errors/workflow-cancelled-error.d.mts.map +1 -0
- package/esm/errors/workflow-cancelled-error.mjs +23 -0
- package/esm/errors/workflow-cancelled-error.mjs.map +1 -0
- package/esm/errors/workflow-drift-error.d.mts +25 -0
- package/esm/errors/workflow-drift-error.d.mts.map +1 -0
- package/esm/errors/workflow-drift-error.mjs +24 -0
- package/esm/errors/workflow-drift-error.mjs.map +1 -0
- package/esm/errors/workflow-error.d.mts +15 -0
- package/esm/errors/workflow-error.d.mts.map +1 -0
- package/esm/errors/workflow-error.mjs +18 -0
- package/esm/errors/workflow-error.mjs.map +1 -0
- package/esm/index.d.mts +137 -0
- package/esm/index.mjs +81 -0
- package/esm/middleware/builtins/budget.d.mts +103 -0
- package/esm/middleware/builtins/budget.d.mts.map +1 -0
- package/esm/middleware/builtins/budget.mjs +100 -0
- package/esm/middleware/builtins/budget.mjs.map +1 -0
- package/esm/middleware/builtins/guardrail.d.mts +92 -0
- package/esm/middleware/builtins/guardrail.d.mts.map +1 -0
- package/esm/middleware/builtins/guardrail.mjs +76 -0
- package/esm/middleware/builtins/guardrail.mjs.map +1 -0
- package/esm/middleware/builtins/semantic-cache.d.mts +106 -0
- package/esm/middleware/builtins/semantic-cache.d.mts.map +1 -0
- package/esm/middleware/builtins/semantic-cache.mjs +149 -0
- package/esm/middleware/builtins/semantic-cache.mjs.map +1 -0
- package/esm/middleware/helpers/compose.d.mts +42 -0
- package/esm/middleware/helpers/compose.d.mts.map +1 -0
- package/esm/middleware/helpers/compose.mjs +52 -0
- package/esm/middleware/helpers/compose.mjs.map +1 -0
- package/esm/middleware/helpers/for-tool.d.mts +55 -0
- package/esm/middleware/helpers/for-tool.d.mts.map +1 -0
- package/esm/middleware/helpers/for-tool.mjs +80 -0
- package/esm/middleware/helpers/for-tool.mjs.map +1 -0
- package/esm/middleware/helpers/index.d.mts +2 -0
- package/esm/middleware/index.d.mts +8 -0
- package/esm/middleware/index.mjs +10 -0
- package/esm/middleware/pipeline.d.mts +70 -0
- package/esm/middleware/pipeline.d.mts.map +1 -0
- package/esm/middleware/pipeline.mjs +100 -0
- package/esm/middleware/pipeline.mjs.map +1 -0
- package/esm/middleware/utils/extract-user-text.d.mts +33 -0
- package/esm/middleware/utils/extract-user-text.d.mts.map +1 -0
- package/esm/middleware/utils/extract-user-text.mjs +40 -0
- package/esm/middleware/utils/extract-user-text.mjs.map +1 -0
- package/esm/middleware/utils/index.d.mts +2 -0
- package/esm/middleware/utils/namespaced-state.d.mts +60 -0
- package/esm/middleware/utils/namespaced-state.d.mts.map +1 -0
- package/esm/middleware/utils/namespaced-state.mjs +60 -0
- package/esm/middleware/utils/namespaced-state.mjs.map +1 -0
- package/esm/mock/index.d.mts +4 -0
- package/esm/mock/index.mjs +5 -0
- package/esm/mock/mock-agent.d.mts +41 -0
- package/esm/mock/mock-agent.d.mts.map +1 -0
- package/esm/mock/mock-agent.mjs +39 -0
- package/esm/mock/mock-agent.mjs.map +1 -0
- package/esm/mock/mock-config.type.d.mts +31 -0
- package/esm/mock/mock-config.type.d.mts.map +1 -0
- package/esm/mock/mock-model.d.mts +94 -0
- package/esm/mock/mock-model.d.mts.map +1 -0
- package/esm/mock/mock-model.mjs +150 -0
- package/esm/mock/mock-model.mjs.map +1 -0
- package/esm/mock/mock-sdk.d.mts +25 -0
- package/esm/mock/mock-sdk.d.mts.map +1 -0
- package/esm/mock/mock-sdk.mjs +37 -0
- package/esm/mock/mock-sdk.mjs.map +1 -0
- package/esm/supervisor/as-tool.d.mts +45 -0
- package/esm/supervisor/as-tool.d.mts.map +1 -0
- package/esm/supervisor/as-tool.mjs +80 -0
- package/esm/supervisor/as-tool.mjs.map +1 -0
- package/esm/supervisor/cancellation.d.mts +12 -0
- package/esm/supervisor/cancellation.d.mts.map +1 -0
- package/esm/supervisor/cancellation.mjs +22 -0
- package/esm/supervisor/cancellation.mjs.map +1 -0
- package/esm/supervisor/decide.mjs +272 -0
- package/esm/supervisor/decide.mjs.map +1 -0
- package/esm/supervisor/emitter.d.mts +27 -0
- package/esm/supervisor/emitter.d.mts.map +1 -0
- package/esm/supervisor/emitter.mjs +47 -0
- package/esm/supervisor/emitter.mjs.map +1 -0
- package/esm/supervisor/entries.d.mts +141 -0
- package/esm/supervisor/entries.d.mts.map +1 -0
- package/esm/supervisor/entries.mjs +206 -0
- package/esm/supervisor/entries.mjs.map +1 -0
- package/esm/supervisor/execution.d.mts +637 -0
- package/esm/supervisor/execution.d.mts.map +1 -0
- package/esm/supervisor/execution.mjs +1868 -0
- package/esm/supervisor/execution.mjs.map +1 -0
- package/esm/supervisor/index.d.mts +11 -0
- package/esm/supervisor/index.mjs +12 -0
- package/esm/supervisor/router-prompt.d.mts +56 -0
- package/esm/supervisor/router-prompt.d.mts.map +1 -0
- package/esm/supervisor/router-prompt.mjs +52 -0
- package/esm/supervisor/router-prompt.mjs.map +1 -0
- package/esm/supervisor/signature.d.mts +28 -0
- package/esm/supervisor/signature.d.mts.map +1 -0
- package/esm/supervisor/signature.mjs +89 -0
- package/esm/supervisor/signature.mjs.map +1 -0
- package/esm/supervisor/snapshot.d.mts +54 -0
- package/esm/supervisor/snapshot.d.mts.map +1 -0
- package/esm/supervisor/snapshot.mjs +77 -0
- package/esm/supervisor/snapshot.mjs.map +1 -0
- package/esm/supervisor/supervisor-stream.d.mts +29 -0
- package/esm/supervisor/supervisor-stream.d.mts.map +1 -0
- package/esm/supervisor/supervisor-stream.mjs +89 -0
- package/esm/supervisor/supervisor-stream.mjs.map +1 -0
- package/esm/supervisor/supervisor.d.mts +27 -0
- package/esm/supervisor/supervisor.d.mts.map +1 -0
- package/esm/supervisor/supervisor.mjs +138 -0
- package/esm/supervisor/supervisor.mjs.map +1 -0
- package/esm/system-prompt/instruction.d.mts +52 -0
- package/esm/system-prompt/instruction.d.mts.map +1 -0
- package/esm/system-prompt/instruction.mjs +57 -0
- package/esm/system-prompt/instruction.mjs.map +1 -0
- package/esm/system-prompt/persona.d.mts +52 -0
- package/esm/system-prompt/persona.d.mts.map +1 -0
- package/esm/system-prompt/persona.mjs +57 -0
- package/esm/system-prompt/persona.mjs.map +1 -0
- package/esm/system-prompt/render-placeholders.d.mts +31 -0
- package/esm/system-prompt/render-placeholders.d.mts.map +1 -0
- package/esm/system-prompt/render-placeholders.mjs +58 -0
- package/esm/system-prompt/render-placeholders.mjs.map +1 -0
- package/esm/system-prompt/system-prompt.d.mts +117 -0
- package/esm/system-prompt/system-prompt.d.mts.map +1 -0
- package/esm/system-prompt/system-prompt.mjs +137 -0
- package/esm/system-prompt/system-prompt.mjs.map +1 -0
- package/esm/tool/index.d.mts +1 -0
- package/esm/tool/index.mjs +3 -0
- package/esm/tool/tool.d.mts +64 -0
- package/esm/tool/tool.d.mts.map +1 -0
- package/esm/tool/tool.mjs +184 -0
- package/esm/tool/tool.mjs.map +1 -0
- package/esm/utils/compute-cost.d.mts +58 -0
- package/esm/utils/compute-cost.d.mts.map +1 -0
- package/esm/utils/compute-cost.mjs +87 -0
- package/esm/utils/compute-cost.mjs.map +1 -0
- package/esm/utils/extract-json-payload.d.mts +33 -0
- package/esm/utils/extract-json-payload.d.mts.map +1 -0
- package/esm/utils/extract-json-payload.mjs +39 -0
- package/esm/utils/extract-json-payload.mjs.map +1 -0
- package/esm/utils/generate-run-id.d.mts +20 -0
- package/esm/utils/generate-run-id.d.mts.map +1 -0
- package/esm/utils/generate-run-id.mjs +23 -0
- package/esm/utils/generate-run-id.mjs.map +1 -0
- package/esm/utils/index.d.mts +9 -0
- package/esm/utils/index.mjs +11 -0
- package/esm/utils/json-schema.d.mts +57 -0
- package/esm/utils/json-schema.d.mts.map +1 -0
- package/esm/utils/json-schema.mjs +89 -0
- package/esm/utils/json-schema.mjs.map +1 -0
- package/esm/utils/prepare-attachment-part.d.mts +36 -0
- package/esm/utils/prepare-attachment-part.d.mts.map +1 -0
- package/esm/utils/prepare-attachment-part.mjs +140 -0
- package/esm/utils/prepare-attachment-part.mjs.map +1 -0
- package/esm/utils/resolve-attachment.d.mts +33 -0
- package/esm/utils/resolve-attachment.d.mts.map +1 -0
- package/esm/utils/resolve-attachment.mjs +73 -0
- package/esm/utils/resolve-attachment.mjs.map +1 -0
- package/esm/utils/safe-json-parse.d.mts +14 -0
- package/esm/utils/safe-json-parse.d.mts.map +1 -0
- package/esm/utils/safe-json-parse.mjs +22 -0
- package/esm/utils/safe-json-parse.mjs.map +1 -0
- package/esm/utils/stamp-report-lineage.d.mts +61 -0
- package/esm/utils/stamp-report-lineage.d.mts.map +1 -0
- package/esm/utils/stamp-report-lineage.mjs +47 -0
- package/esm/utils/stamp-report-lineage.mjs.map +1 -0
- package/esm/utils/token-count.d.mts +15 -0
- package/esm/utils/token-count.d.mts.map +1 -0
- package/esm/utils/token-count.mjs +18 -0
- package/esm/utils/token-count.mjs.map +1 -0
- package/esm/workflow/as-tool.mjs +55 -0
- package/esm/workflow/as-tool.mjs.map +1 -0
- package/esm/workflow/cancellation.mjs +44 -0
- package/esm/workflow/cancellation.mjs.map +1 -0
- package/esm/workflow/emitter.mjs +40 -0
- package/esm/workflow/emitter.mjs.map +1 -0
- package/esm/workflow/engine.mjs +362 -0
- package/esm/workflow/engine.mjs.map +1 -0
- package/esm/workflow/index.d.mts +3 -0
- package/esm/workflow/index.mjs +5 -0
- package/esm/workflow/retry.mjs +32 -0
- package/esm/workflow/retry.mjs.map +1 -0
- package/esm/workflow/router.mjs +56 -0
- package/esm/workflow/router.mjs.map +1 -0
- package/esm/workflow/run-scoped-emitter.mjs +32 -0
- package/esm/workflow/run-scoped-emitter.mjs.map +1 -0
- package/esm/workflow/signature.d.mts +7 -0
- package/esm/workflow/signature.d.mts.map +1 -0
- package/esm/workflow/signature.mjs +34 -0
- package/esm/workflow/signature.mjs.map +1 -0
- package/esm/workflow/snapshot.mjs +71 -0
- package/esm/workflow/snapshot.mjs.map +1 -0
- package/esm/workflow/state.mjs +37 -0
- package/esm/workflow/state.mjs.map +1 -0
- package/esm/workflow/step-runner.mjs +371 -0
- package/esm/workflow/step-runner.mjs.map +1 -0
- package/esm/workflow/step.d.mts +19 -0
- package/esm/workflow/step.d.mts.map +1 -0
- package/esm/workflow/step.mjs +37 -0
- package/esm/workflow/step.mjs.map +1 -0
- package/esm/workflow/workflow.d.mts +12 -0
- package/esm/workflow/workflow.d.mts.map +1 -0
- package/esm/workflow/workflow.mjs +116 -0
- package/esm/workflow/workflow.mjs.map +1 -0
- package/llms-full.txt +2293 -0
- package/llms.txt +20 -0
- package/package.json +37 -0
- package/skills/README.md +53 -0
- package/skills/ai-basics/SKILL.md +95 -0
- package/skills/attach-ai-middleware/SKILL.md +184 -0
- package/skills/define-ai-tool/SKILL.md +186 -0
- package/skills/embed-text/SKILL.md +105 -0
- package/skills/handle-ai-errors/SKILL.md +194 -0
- package/skills/log-ai-calls/SKILL.md +141 -0
- package/skills/persist-ai-data/SKILL.md +207 -0
- package/skills/pick-ai-provider/SKILL.md +164 -0
- package/skills/run-ai-agent/SKILL.md +267 -0
- package/skills/run-ai-workflow/SKILL.md +258 -0
- package/skills/run-supervisor/SKILL.md +309 -0
- package/skills/write-system-prompt/SKILL.md +129 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
//#region ../../@warlock.js/ai/src/middleware/helpers/compose.ts
|
|
2
|
+
/**
|
|
3
|
+
* Flatten one or more middleware sources into a single ordered
|
|
4
|
+
* array suitable for `agent({ middleware: [...] })`.
|
|
5
|
+
*
|
|
6
|
+
* **Role.** As middleware catalogs grow, agent configs accumulate
|
|
7
|
+
* long arrays that mix "always-on" stacks (cache + budget + guardrail)
|
|
8
|
+
* with per-concern extras (per-tool rate-limits, audit hooks). A
|
|
9
|
+
* single `compose` call lets callers keep those sources as named
|
|
10
|
+
* variables and flatten at the registration site.
|
|
11
|
+
*
|
|
12
|
+
* **Semantics.** Registration order is preserved across sources —
|
|
13
|
+
* `compose(a, b, c)` produces `[...a, ...b, ...c]`. Because the
|
|
14
|
+
* pipeline's onion is strictly registration-ordered, the flattened
|
|
15
|
+
* order is the execution order. No de-duplication, no sorting, no
|
|
16
|
+
* priority logic — that would hide bugs, not fix them.
|
|
17
|
+
*
|
|
18
|
+
* **Accepts arrays OR individual middlewares.** Both forms are
|
|
19
|
+
* common in callsite code; the helper flattens either.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* const standardStack = [
|
|
23
|
+
* ai.middleware.semanticCache({ ... }),
|
|
24
|
+
* ai.middleware.budget({ maxTokens: 20_000 }),
|
|
25
|
+
* ai.middleware.guardrail({ ... }),
|
|
26
|
+
* ];
|
|
27
|
+
*
|
|
28
|
+
* const toolRateLimits = [
|
|
29
|
+
* toolRateLimit({ tool: "search_web", maxCalls: 3 }),
|
|
30
|
+
* toolRateLimit({ tool: "expensive_api", maxCalls: 1 }),
|
|
31
|
+
* ];
|
|
32
|
+
*
|
|
33
|
+
* const myAgent = ai.agent({
|
|
34
|
+
* model,
|
|
35
|
+
* middleware: ai.middleware.compose(standardStack, toolRateLimits, auditMiddleware),
|
|
36
|
+
* });
|
|
37
|
+
*/
|
|
38
|
+
function composeMiddleware(...sources) {
|
|
39
|
+
const out = [];
|
|
40
|
+
for (const source of sources) {
|
|
41
|
+
if (Array.isArray(source)) {
|
|
42
|
+
out.push(...source);
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
out.push(source);
|
|
46
|
+
}
|
|
47
|
+
return out;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
//#endregion
|
|
51
|
+
export { composeMiddleware };
|
|
52
|
+
//# sourceMappingURL=compose.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compose.mjs","names":[],"sources":["../../../../../../../@warlock.js/ai/src/middleware/helpers/compose.ts"],"sourcesContent":["import type { AgentMiddleware } from \"../../contracts/middleware\";\n\n/**\n * Flatten one or more middleware sources into a single ordered\n * array suitable for `agent({ middleware: [...] })`.\n *\n * **Role.** As middleware catalogs grow, agent configs accumulate\n * long arrays that mix \"always-on\" stacks (cache + budget + guardrail)\n * with per-concern extras (per-tool rate-limits, audit hooks). A\n * single `compose` call lets callers keep those sources as named\n * variables and flatten at the registration site.\n *\n * **Semantics.** Registration order is preserved across sources —\n * `compose(a, b, c)` produces `[...a, ...b, ...c]`. Because the\n * pipeline's onion is strictly registration-ordered, the flattened\n * order is the execution order. No de-duplication, no sorting, no\n * priority logic — that would hide bugs, not fix them.\n *\n * **Accepts arrays OR individual middlewares.** Both forms are\n * common in callsite code; the helper flattens either.\n *\n * @example\n * const standardStack = [\n * ai.middleware.semanticCache({ ... }),\n * ai.middleware.budget({ maxTokens: 20_000 }),\n * ai.middleware.guardrail({ ... }),\n * ];\n *\n * const toolRateLimits = [\n * toolRateLimit({ tool: \"search_web\", maxCalls: 3 }),\n * toolRateLimit({ tool: \"expensive_api\", maxCalls: 1 }),\n * ];\n *\n * const myAgent = ai.agent({\n * model,\n * middleware: ai.middleware.compose(standardStack, toolRateLimits, auditMiddleware),\n * });\n */\nexport function composeMiddleware(\n ...sources: ReadonlyArray<AgentMiddleware | ReadonlyArray<AgentMiddleware>>\n): AgentMiddleware[] {\n const out: AgentMiddleware[] = [];\n\n for (const source of sources) {\n if (Array.isArray(source)) {\n out.push(...source);\n continue;\n }\n\n out.push(source as AgentMiddleware);\n }\n\n return out;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCA,SAAgB,kBACd,GAAG,SACgB;CACnB,MAAM,MAAyB,CAAC;CAEhC,KAAK,MAAM,UAAU,SAAS;EAC5B,IAAI,MAAM,QAAQ,MAAM,GAAG;GACzB,IAAI,KAAK,GAAG,MAAM;GAClB;EACF;EAEA,IAAI,KAAK,MAAyB;CACpC;CAEA,OAAO;AACT"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { AgentMiddleware } from "../../contracts/middleware/middleware.contract.mjs";
|
|
2
|
+
//#region ../../@warlock.js/ai/src/middleware/helpers/for-tool.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* Scope a middleware's `tool`-level hooks to only fire for a
|
|
5
|
+
* specific tool name (or a set of names). `execute` and `trip`
|
|
6
|
+
* hooks pass through unchanged.
|
|
7
|
+
*
|
|
8
|
+
* **Role.** Tool-specific concerns — "rate-limit `search_web`",
|
|
9
|
+
* "cache results for `fetch_faq`" — are common. Rather than adding
|
|
10
|
+
* a `middleware` field to `ai.tool()` (see decisions §27), the
|
|
11
|
+
* framework keeps one contract (`AgentMiddleware`) and offers this
|
|
12
|
+
* helper for the locality problem. The middleware body stays agnostic
|
|
13
|
+
* of the tool name; `forTool` handles the filtering.
|
|
14
|
+
*
|
|
15
|
+
* **What gets filtered.** Only `tool.before` / `tool.after` /
|
|
16
|
+
* `tool.onError`. Each hook is wrapped so that `ctx.tool.name`
|
|
17
|
+
* must be in the allowed set or the wrapped hook is a no-op.
|
|
18
|
+
* `execute` and `trip` hooks are NOT touched — they run normally.
|
|
19
|
+
*
|
|
20
|
+
* **Why not filter execute/trip too?** Because a middleware that
|
|
21
|
+
* reaches across levels (a tool-specific budget that initializes a
|
|
22
|
+
* counter in `execute.before` and checks it in `tool.before`) still
|
|
23
|
+
* needs `execute.before` to fire unconditionally. Scoping all hooks
|
|
24
|
+
* would break cross-level middleware; scoping only `tool` hooks
|
|
25
|
+
* matches the mental model of "this middleware cares about these
|
|
26
|
+
* tools."
|
|
27
|
+
*
|
|
28
|
+
* **Single-name vs multi-name.** A string matches one tool; a string
|
|
29
|
+
* array matches any of the listed tools. No wildcards, no regex —
|
|
30
|
+
* keep it boring.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* // Single tool
|
|
34
|
+
* const scoped = ai.middleware.forTool(
|
|
35
|
+
* "search_web",
|
|
36
|
+
* toolRateLimit({ maxCalls: 3 }),
|
|
37
|
+
* );
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* // Multiple tools sharing a rule
|
|
41
|
+
* const scoped = ai.middleware.forTool(
|
|
42
|
+
* ["paid_api", "expensive_db"],
|
|
43
|
+
* toolRateLimit({ maxCalls: 5 }),
|
|
44
|
+
* );
|
|
45
|
+
*
|
|
46
|
+
* ai.agent({
|
|
47
|
+
* model,
|
|
48
|
+
* tools: [webTool, paidApiTool, expensiveDbTool],
|
|
49
|
+
* middleware: [scoped],
|
|
50
|
+
* });
|
|
51
|
+
*/
|
|
52
|
+
declare function forTool(toolNames: string | ReadonlyArray<string>, middleware: AgentMiddleware): AgentMiddleware;
|
|
53
|
+
//#endregion
|
|
54
|
+
export { forTool };
|
|
55
|
+
//# sourceMappingURL=for-tool.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"for-tool.d.mts","names":[],"sources":["../../../../../../../@warlock.js/ai/src/middleware/helpers/for-tool.ts"],"mappings":";;;;;AAmDA;;;;;;;;;;;;;;AAGkB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAHF,OAAA,CACd,SAAA,WAAoB,aAAA,UACpB,UAAA,EAAY,eAAA,GACX,eAAA"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
//#region ../../@warlock.js/ai/src/middleware/helpers/for-tool.ts
|
|
2
|
+
/**
|
|
3
|
+
* Scope a middleware's `tool`-level hooks to only fire for a
|
|
4
|
+
* specific tool name (or a set of names). `execute` and `trip`
|
|
5
|
+
* hooks pass through unchanged.
|
|
6
|
+
*
|
|
7
|
+
* **Role.** Tool-specific concerns — "rate-limit `search_web`",
|
|
8
|
+
* "cache results for `fetch_faq`" — are common. Rather than adding
|
|
9
|
+
* a `middleware` field to `ai.tool()` (see decisions §27), the
|
|
10
|
+
* framework keeps one contract (`AgentMiddleware`) and offers this
|
|
11
|
+
* helper for the locality problem. The middleware body stays agnostic
|
|
12
|
+
* of the tool name; `forTool` handles the filtering.
|
|
13
|
+
*
|
|
14
|
+
* **What gets filtered.** Only `tool.before` / `tool.after` /
|
|
15
|
+
* `tool.onError`. Each hook is wrapped so that `ctx.tool.name`
|
|
16
|
+
* must be in the allowed set or the wrapped hook is a no-op.
|
|
17
|
+
* `execute` and `trip` hooks are NOT touched — they run normally.
|
|
18
|
+
*
|
|
19
|
+
* **Why not filter execute/trip too?** Because a middleware that
|
|
20
|
+
* reaches across levels (a tool-specific budget that initializes a
|
|
21
|
+
* counter in `execute.before` and checks it in `tool.before`) still
|
|
22
|
+
* needs `execute.before` to fire unconditionally. Scoping all hooks
|
|
23
|
+
* would break cross-level middleware; scoping only `tool` hooks
|
|
24
|
+
* matches the mental model of "this middleware cares about these
|
|
25
|
+
* tools."
|
|
26
|
+
*
|
|
27
|
+
* **Single-name vs multi-name.** A string matches one tool; a string
|
|
28
|
+
* array matches any of the listed tools. No wildcards, no regex —
|
|
29
|
+
* keep it boring.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* // Single tool
|
|
33
|
+
* const scoped = ai.middleware.forTool(
|
|
34
|
+
* "search_web",
|
|
35
|
+
* toolRateLimit({ maxCalls: 3 }),
|
|
36
|
+
* );
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* // Multiple tools sharing a rule
|
|
40
|
+
* const scoped = ai.middleware.forTool(
|
|
41
|
+
* ["paid_api", "expensive_db"],
|
|
42
|
+
* toolRateLimit({ maxCalls: 5 }),
|
|
43
|
+
* );
|
|
44
|
+
*
|
|
45
|
+
* ai.agent({
|
|
46
|
+
* model,
|
|
47
|
+
* tools: [webTool, paidApiTool, expensiveDbTool],
|
|
48
|
+
* middleware: [scoped],
|
|
49
|
+
* });
|
|
50
|
+
*/
|
|
51
|
+
function forTool(toolNames, middleware) {
|
|
52
|
+
const allowed = new Set(typeof toolNames === "string" ? [toolNames] : toolNames);
|
|
53
|
+
const scope = allowed.size === 1 ? Array.from(allowed)[0] : Array.from(allowed).join("+");
|
|
54
|
+
if (!middleware.tool) return middleware;
|
|
55
|
+
const innerBefore = middleware.tool.before;
|
|
56
|
+
const innerAfter = middleware.tool.after;
|
|
57
|
+
const innerOnError = middleware.tool.onError;
|
|
58
|
+
return {
|
|
59
|
+
...middleware,
|
|
60
|
+
name: `${middleware.name}[for:${scope}]`,
|
|
61
|
+
tool: {
|
|
62
|
+
before: innerBefore ? async (ctx) => {
|
|
63
|
+
if (!allowed.has(ctx.tool.name)) return;
|
|
64
|
+
return innerBefore(ctx);
|
|
65
|
+
} : void 0,
|
|
66
|
+
after: innerAfter ? async (ctx, result) => {
|
|
67
|
+
if (!allowed.has(ctx.tool.name)) return;
|
|
68
|
+
return innerAfter(ctx, result);
|
|
69
|
+
} : void 0,
|
|
70
|
+
onError: innerOnError ? async (ctx, error) => {
|
|
71
|
+
if (!allowed.has(ctx.tool.name)) return;
|
|
72
|
+
return innerOnError(ctx, error);
|
|
73
|
+
} : void 0
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
//#endregion
|
|
79
|
+
export { forTool };
|
|
80
|
+
//# sourceMappingURL=for-tool.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"for-tool.mjs","names":[],"sources":["../../../../../../../@warlock.js/ai/src/middleware/helpers/for-tool.ts"],"sourcesContent":["import type { AgentMiddleware } from \"../../contracts/middleware\";\n\n/**\n * Scope a middleware's `tool`-level hooks to only fire for a\n * specific tool name (or a set of names). `execute` and `trip`\n * hooks pass through unchanged.\n *\n * **Role.** Tool-specific concerns — \"rate-limit `search_web`\",\n * \"cache results for `fetch_faq`\" — are common. Rather than adding\n * a `middleware` field to `ai.tool()` (see decisions §27), the\n * framework keeps one contract (`AgentMiddleware`) and offers this\n * helper for the locality problem. The middleware body stays agnostic\n * of the tool name; `forTool` handles the filtering.\n *\n * **What gets filtered.** Only `tool.before` / `tool.after` /\n * `tool.onError`. Each hook is wrapped so that `ctx.tool.name`\n * must be in the allowed set or the wrapped hook is a no-op.\n * `execute` and `trip` hooks are NOT touched — they run normally.\n *\n * **Why not filter execute/trip too?** Because a middleware that\n * reaches across levels (a tool-specific budget that initializes a\n * counter in `execute.before` and checks it in `tool.before`) still\n * needs `execute.before` to fire unconditionally. Scoping all hooks\n * would break cross-level middleware; scoping only `tool` hooks\n * matches the mental model of \"this middleware cares about these\n * tools.\"\n *\n * **Single-name vs multi-name.** A string matches one tool; a string\n * array matches any of the listed tools. No wildcards, no regex —\n * keep it boring.\n *\n * @example\n * // Single tool\n * const scoped = ai.middleware.forTool(\n * \"search_web\",\n * toolRateLimit({ maxCalls: 3 }),\n * );\n *\n * @example\n * // Multiple tools sharing a rule\n * const scoped = ai.middleware.forTool(\n * [\"paid_api\", \"expensive_db\"],\n * toolRateLimit({ maxCalls: 5 }),\n * );\n *\n * ai.agent({\n * model,\n * tools: [webTool, paidApiTool, expensiveDbTool],\n * middleware: [scoped],\n * });\n */\nexport function forTool(\n toolNames: string | ReadonlyArray<string>,\n middleware: AgentMiddleware,\n): AgentMiddleware {\n const allowed = new Set(\n typeof toolNames === \"string\" ? [toolNames] : toolNames,\n );\n const scope =\n allowed.size === 1 ? Array.from(allowed)[0] : Array.from(allowed).join(\"+\");\n\n if (!middleware.tool) {\n return middleware;\n }\n\n const innerBefore = middleware.tool.before;\n const innerAfter = middleware.tool.after;\n const innerOnError = middleware.tool.onError;\n\n return {\n ...middleware,\n name: `${middleware.name}[for:${scope}]`,\n tool: {\n before: innerBefore\n ? async ctx => {\n if (!allowed.has(ctx.tool.name)) {\n return;\n }\n\n return innerBefore(ctx);\n }\n : undefined,\n after: innerAfter\n ? async (ctx, result) => {\n if (!allowed.has(ctx.tool.name)) {\n return;\n }\n\n return innerAfter(ctx, result);\n }\n : undefined,\n onError: innerOnError\n ? async (ctx, error) => {\n if (!allowed.has(ctx.tool.name)) {\n return;\n }\n\n return innerOnError(ctx, error);\n }\n : undefined,\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmDA,SAAgB,QACd,WACA,YACiB;CACjB,MAAM,UAAU,IAAI,IAClB,OAAO,cAAc,WAAW,CAAC,SAAS,IAAI,SAChD;CACA,MAAM,QACJ,QAAQ,SAAS,IAAI,MAAM,KAAK,OAAO,EAAE,KAAK,MAAM,KAAK,OAAO,EAAE,KAAK,GAAG;CAE5E,IAAI,CAAC,WAAW,MACd,OAAO;CAGT,MAAM,cAAc,WAAW,KAAK;CACpC,MAAM,aAAa,WAAW,KAAK;CACnC,MAAM,eAAe,WAAW,KAAK;CAErC,OAAO;EACL,GAAG;EACH,MAAM,GAAG,WAAW,KAAK,OAAO,MAAM;EACtC,MAAM;GACJ,QAAQ,cACJ,OAAM,QAAO;IACX,IAAI,CAAC,QAAQ,IAAI,IAAI,KAAK,IAAI,GAC5B;IAGF,OAAO,YAAY,GAAG;GACxB,IACA;GACJ,OAAO,aACH,OAAO,KAAK,WAAW;IACrB,IAAI,CAAC,QAAQ,IAAI,IAAI,KAAK,IAAI,GAC5B;IAGF,OAAO,WAAW,KAAK,MAAM;GAC/B,IACA;GACJ,SAAS,eACL,OAAO,KAAK,UAAU;IACpB,IAAI,CAAC,QAAQ,IAAI,IAAI,KAAK,IAAI,GAC5B;IAGF,OAAO,aAAa,KAAK,KAAK;GAChC,IACA;EACN;CACF;AACF"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { BudgetOptions, BudgetPricing, budget } from "./builtins/budget.mjs";
|
|
2
|
+
import { GuardrailCheck, GuardrailCheckResult, GuardrailOptions, guardrail } from "./builtins/guardrail.mjs";
|
|
3
|
+
import { SemanticCacheOptions, semanticCache } from "./builtins/semantic-cache.mjs";
|
|
4
|
+
import { composeMiddleware } from "./helpers/compose.mjs";
|
|
5
|
+
import { forTool } from "./helpers/for-tool.mjs";
|
|
6
|
+
import { MiddlewareContextByLevel, MiddlewareLevel, runPipeline } from "./pipeline.mjs";
|
|
7
|
+
import { extractUserText } from "./utils/extract-user-text.mjs";
|
|
8
|
+
import { NamespacedStateAccessor, namespacedState } from "./utils/namespaced-state.mjs";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { extractUserText } from "./utils/extract-user-text.mjs";
|
|
2
|
+
import { namespacedState } from "./utils/namespaced-state.mjs";
|
|
3
|
+
import { budget } from "./builtins/budget.mjs";
|
|
4
|
+
import { guardrail } from "./builtins/guardrail.mjs";
|
|
5
|
+
import { semanticCache } from "./builtins/semantic-cache.mjs";
|
|
6
|
+
import { composeMiddleware } from "./helpers/compose.mjs";
|
|
7
|
+
import { forTool } from "./helpers/for-tool.mjs";
|
|
8
|
+
import { runPipeline } from "./pipeline.mjs";
|
|
9
|
+
|
|
10
|
+
export { };
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { MiddlewareExecuteContext, MiddlewareToolContext, MiddlewareTripContext } from "../contracts/middleware/middleware-context.type.mjs";
|
|
2
|
+
import { AgentMiddleware } from "../contracts/middleware/middleware.contract.mjs";
|
|
3
|
+
import { Logger } from "@warlock.js/logger";
|
|
4
|
+
|
|
5
|
+
//#region ../../@warlock.js/ai/src/middleware/pipeline.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* The three levels at which agent middleware can hook — mirrors
|
|
8
|
+
* `AgentMiddleware`'s optional `execute` / `trip` / `tool` keys.
|
|
9
|
+
* Kept as a single named union so callers can pass it around without
|
|
10
|
+
* inline-duplicating the literals.
|
|
11
|
+
*/
|
|
12
|
+
type MiddlewareLevel = "execute" | "trip" | "tool";
|
|
13
|
+
/**
|
|
14
|
+
* Shape of the context object for each level. The pipeline is
|
|
15
|
+
* level-parameterized on the ctx type via this mapping so callers
|
|
16
|
+
* get compile-time narrowing when they instantiate `runPipeline`.
|
|
17
|
+
*/
|
|
18
|
+
type MiddlewareContextByLevel = {
|
|
19
|
+
execute: MiddlewareExecuteContext;
|
|
20
|
+
trip: MiddlewareTripContext;
|
|
21
|
+
tool: MiddlewareToolContext;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Run an inner async operation through a stack of agent middlewares
|
|
25
|
+
* at a single level, applying the onion-model before/after/onError
|
|
26
|
+
* semantics documented on `AgentMiddleware`.
|
|
27
|
+
*
|
|
28
|
+
* **Semantics.**
|
|
29
|
+
* - `before` hooks run in registration order (top-down).
|
|
30
|
+
* Returning a defined value from a `before` hook short-circuits the
|
|
31
|
+
* pipeline with that value as the result, skipping `inner()` and
|
|
32
|
+
* all deeper `before` / `after` hooks — but outer middleware
|
|
33
|
+
* `after` hooks (registered earlier) still run on the synthetic
|
|
34
|
+
* value.
|
|
35
|
+
* - `after` hooks run in reverse registration order (bottom-up).
|
|
36
|
+
* Returning a defined value replaces the result before it
|
|
37
|
+
* propagates further out. Returning `void` / `undefined` keeps the
|
|
38
|
+
* existing result.
|
|
39
|
+
* - `onError` hooks also run in reverse (bottom-up) — any error
|
|
40
|
+
* thrown by `inner()`, by a `before` hook, or by an `after` hook
|
|
41
|
+
* unwinds through each frame's `onError` in turn. Returning a
|
|
42
|
+
* defined value from `onError` recovers: the error is cleared and
|
|
43
|
+
* the returned value becomes the new result (which then flows
|
|
44
|
+
* through outer `after` hooks). Returning `void` propagates the
|
|
45
|
+
* error to the next outer frame.
|
|
46
|
+
*
|
|
47
|
+
* **Implementation.** Built by folding the middleware array from the
|
|
48
|
+
* end inward: each middleware produces a closure that wraps the
|
|
49
|
+
* previous closure (the deeper pipeline). The outermost wrap is
|
|
50
|
+
* middleware index 0 — so registration order matches onion order
|
|
51
|
+
* without any reverse iteration at call time.
|
|
52
|
+
*
|
|
53
|
+
* **No magic.** The pipeline does not swallow, retry, or translate
|
|
54
|
+
* errors. Hooks that throw propagate unchanged (subject to `onError`
|
|
55
|
+
* recovery). Pipeline-level logging is debug-only and respects each
|
|
56
|
+
* middleware's `log: false` kill-switch.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* const response = await runPipeline(
|
|
60
|
+
* middlewares,
|
|
61
|
+
* "trip",
|
|
62
|
+
* tripContext,
|
|
63
|
+
* () => model.complete(messages, callOptions),
|
|
64
|
+
* logger,
|
|
65
|
+
* );
|
|
66
|
+
*/
|
|
67
|
+
declare function runPipeline<Level extends MiddlewareLevel, TResult>(middlewares: ReadonlyArray<AgentMiddleware>, level: Level, context: MiddlewareContextByLevel[Level], inner: () => Promise<TResult>, logger?: Logger): Promise<TResult>;
|
|
68
|
+
//#endregion
|
|
69
|
+
export { MiddlewareContextByLevel, MiddlewareLevel, runPipeline };
|
|
70
|
+
//# sourceMappingURL=pipeline.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pipeline.d.mts","names":[],"sources":["../../../../../../@warlock.js/ai/src/middleware/pipeline.ts"],"mappings":";;;;;;;;;AAgBA;;KAAY,eAAA;;AAAe;AAO3B;;;KAAY,wBAAA;EACV,OAAA,EAAS,wBAAA;EACT,IAAA,EAAM,qBAAA;EACN,IAAA,EAAM,qBAAA;AAAA;;;;;;;;AAAqB;AA+C7B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAMkB;iBANI,WAAA,eAA0B,eAAA,UAAA,CAC9C,WAAA,EAAa,aAAA,CAAc,eAAA,GAC3B,KAAA,EAAO,KAAA,EACP,OAAA,EAAS,wBAAA,CAAyB,KAAA,GAClC,KAAA,QAAa,OAAA,CAAQ,OAAA,GACrB,MAAA,GAAS,MAAA,GACR,OAAA,CAAQ,OAAA"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
//#region ../../@warlock.js/ai/src/middleware/pipeline.ts
|
|
2
|
+
const LOG_MODULE = "ai.middleware";
|
|
3
|
+
/**
|
|
4
|
+
* Run an inner async operation through a stack of agent middlewares
|
|
5
|
+
* at a single level, applying the onion-model before/after/onError
|
|
6
|
+
* semantics documented on `AgentMiddleware`.
|
|
7
|
+
*
|
|
8
|
+
* **Semantics.**
|
|
9
|
+
* - `before` hooks run in registration order (top-down).
|
|
10
|
+
* Returning a defined value from a `before` hook short-circuits the
|
|
11
|
+
* pipeline with that value as the result, skipping `inner()` and
|
|
12
|
+
* all deeper `before` / `after` hooks — but outer middleware
|
|
13
|
+
* `after` hooks (registered earlier) still run on the synthetic
|
|
14
|
+
* value.
|
|
15
|
+
* - `after` hooks run in reverse registration order (bottom-up).
|
|
16
|
+
* Returning a defined value replaces the result before it
|
|
17
|
+
* propagates further out. Returning `void` / `undefined` keeps the
|
|
18
|
+
* existing result.
|
|
19
|
+
* - `onError` hooks also run in reverse (bottom-up) — any error
|
|
20
|
+
* thrown by `inner()`, by a `before` hook, or by an `after` hook
|
|
21
|
+
* unwinds through each frame's `onError` in turn. Returning a
|
|
22
|
+
* defined value from `onError` recovers: the error is cleared and
|
|
23
|
+
* the returned value becomes the new result (which then flows
|
|
24
|
+
* through outer `after` hooks). Returning `void` propagates the
|
|
25
|
+
* error to the next outer frame.
|
|
26
|
+
*
|
|
27
|
+
* **Implementation.** Built by folding the middleware array from the
|
|
28
|
+
* end inward: each middleware produces a closure that wraps the
|
|
29
|
+
* previous closure (the deeper pipeline). The outermost wrap is
|
|
30
|
+
* middleware index 0 — so registration order matches onion order
|
|
31
|
+
* without any reverse iteration at call time.
|
|
32
|
+
*
|
|
33
|
+
* **No magic.** The pipeline does not swallow, retry, or translate
|
|
34
|
+
* errors. Hooks that throw propagate unchanged (subject to `onError`
|
|
35
|
+
* recovery). Pipeline-level logging is debug-only and respects each
|
|
36
|
+
* middleware's `log: false` kill-switch.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* const response = await runPipeline(
|
|
40
|
+
* middlewares,
|
|
41
|
+
* "trip",
|
|
42
|
+
* tripContext,
|
|
43
|
+
* () => model.complete(messages, callOptions),
|
|
44
|
+
* logger,
|
|
45
|
+
* );
|
|
46
|
+
*/
|
|
47
|
+
async function runPipeline(middlewares, level, context, inner, logger) {
|
|
48
|
+
if (middlewares.length === 0) return inner();
|
|
49
|
+
let next = inner;
|
|
50
|
+
for (let index = middlewares.length - 1; index >= 0; index--) {
|
|
51
|
+
const middleware = middlewares[index];
|
|
52
|
+
const hooks = middleware[level];
|
|
53
|
+
if (!hooks) continue;
|
|
54
|
+
const previous = next;
|
|
55
|
+
next = async () => {
|
|
56
|
+
const logEnabled = middleware.log !== false && logger !== void 0;
|
|
57
|
+
if (hooks.before) {
|
|
58
|
+
if (logEnabled) logger.debug(LOG_MODULE, `${level}.before`, middleware.name, {
|
|
59
|
+
middleware: middleware.name,
|
|
60
|
+
level
|
|
61
|
+
});
|
|
62
|
+
const shortCircuit = await hooks.before(context);
|
|
63
|
+
if (shortCircuit !== void 0) {
|
|
64
|
+
if (logEnabled) logger.debug(LOG_MODULE, `${level}.short-circuit`, middleware.name, {
|
|
65
|
+
middleware: middleware.name,
|
|
66
|
+
level
|
|
67
|
+
});
|
|
68
|
+
return shortCircuit;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
let result;
|
|
72
|
+
try {
|
|
73
|
+
result = await previous();
|
|
74
|
+
} catch (thrown) {
|
|
75
|
+
if (!hooks.onError) throw thrown;
|
|
76
|
+
const recovered = await hooks.onError(context, thrown);
|
|
77
|
+
if (recovered === void 0) throw thrown;
|
|
78
|
+
if (logEnabled) logger.debug(LOG_MODULE, `${level}.recovered`, middleware.name, {
|
|
79
|
+
middleware: middleware.name,
|
|
80
|
+
level
|
|
81
|
+
});
|
|
82
|
+
result = recovered;
|
|
83
|
+
}
|
|
84
|
+
if (hooks.after) {
|
|
85
|
+
const replacement = await hooks.after(context, result);
|
|
86
|
+
if (replacement !== void 0) result = replacement;
|
|
87
|
+
if (logEnabled) logger.debug(LOG_MODULE, `${level}.after`, middleware.name, {
|
|
88
|
+
middleware: middleware.name,
|
|
89
|
+
level
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
return result;
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
return next();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
//#endregion
|
|
99
|
+
export { runPipeline };
|
|
100
|
+
//# sourceMappingURL=pipeline.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pipeline.mjs","names":[],"sources":["../../../../../../@warlock.js/ai/src/middleware/pipeline.ts"],"sourcesContent":["import type { Logger } from \"@warlock.js/logger\";\nimport type {\n AgentMiddleware,\n MiddlewareExecuteContext,\n MiddlewareToolContext,\n MiddlewareTripContext,\n} from \"../contracts/middleware\";\n\nconst LOG_MODULE = \"ai.middleware\";\n\n/**\n * The three levels at which agent middleware can hook — mirrors\n * `AgentMiddleware`'s optional `execute` / `trip` / `tool` keys.\n * Kept as a single named union so callers can pass it around without\n * inline-duplicating the literals.\n */\nexport type MiddlewareLevel = \"execute\" | \"trip\" | \"tool\";\n\n/**\n * Shape of the context object for each level. The pipeline is\n * level-parameterized on the ctx type via this mapping so callers\n * get compile-time narrowing when they instantiate `runPipeline`.\n */\nexport type MiddlewareContextByLevel = {\n execute: MiddlewareExecuteContext;\n trip: MiddlewareTripContext;\n tool: MiddlewareToolContext;\n};\n\n/**\n * Run an inner async operation through a stack of agent middlewares\n * at a single level, applying the onion-model before/after/onError\n * semantics documented on `AgentMiddleware`.\n *\n * **Semantics.**\n * - `before` hooks run in registration order (top-down).\n * Returning a defined value from a `before` hook short-circuits the\n * pipeline with that value as the result, skipping `inner()` and\n * all deeper `before` / `after` hooks — but outer middleware\n * `after` hooks (registered earlier) still run on the synthetic\n * value.\n * - `after` hooks run in reverse registration order (bottom-up).\n * Returning a defined value replaces the result before it\n * propagates further out. Returning `void` / `undefined` keeps the\n * existing result.\n * - `onError` hooks also run in reverse (bottom-up) — any error\n * thrown by `inner()`, by a `before` hook, or by an `after` hook\n * unwinds through each frame's `onError` in turn. Returning a\n * defined value from `onError` recovers: the error is cleared and\n * the returned value becomes the new result (which then flows\n * through outer `after` hooks). Returning `void` propagates the\n * error to the next outer frame.\n *\n * **Implementation.** Built by folding the middleware array from the\n * end inward: each middleware produces a closure that wraps the\n * previous closure (the deeper pipeline). The outermost wrap is\n * middleware index 0 — so registration order matches onion order\n * without any reverse iteration at call time.\n *\n * **No magic.** The pipeline does not swallow, retry, or translate\n * errors. Hooks that throw propagate unchanged (subject to `onError`\n * recovery). Pipeline-level logging is debug-only and respects each\n * middleware's `log: false` kill-switch.\n *\n * @example\n * const response = await runPipeline(\n * middlewares,\n * \"trip\",\n * tripContext,\n * () => model.complete(messages, callOptions),\n * logger,\n * );\n */\nexport async function runPipeline<Level extends MiddlewareLevel, TResult>(\n middlewares: ReadonlyArray<AgentMiddleware>,\n level: Level,\n context: MiddlewareContextByLevel[Level],\n inner: () => Promise<TResult>,\n logger?: Logger,\n): Promise<TResult> {\n if (middlewares.length === 0) {\n return inner();\n }\n\n let next: () => Promise<TResult> = inner;\n\n for (let index = middlewares.length - 1; index >= 0; index--) {\n const middleware = middlewares[index];\n const hooks = middleware[level];\n\n if (!hooks) {\n continue;\n }\n\n const previous = next;\n\n next = async () => {\n const logEnabled = middleware.log !== false && logger !== undefined;\n\n if (hooks.before) {\n if (logEnabled) {\n logger!.debug(LOG_MODULE, `${level}.before`, middleware.name, {\n middleware: middleware.name,\n level,\n });\n }\n\n const shortCircuit = await (\n hooks.before as (ctx: unknown) => Promise<unknown> | unknown\n )(context);\n\n if (shortCircuit !== undefined) {\n if (logEnabled) {\n logger!.debug(\n LOG_MODULE,\n `${level}.short-circuit`,\n middleware.name,\n {\n middleware: middleware.name,\n level,\n },\n );\n }\n\n return shortCircuit as TResult;\n }\n }\n\n let result: TResult;\n\n try {\n result = await previous();\n } catch (thrown) {\n if (!hooks.onError) {\n throw thrown;\n }\n\n const recovered = await (\n hooks.onError as (\n ctx: unknown,\n error: unknown,\n ) => Promise<unknown> | unknown\n )(context, thrown);\n\n if (recovered === undefined) {\n throw thrown;\n }\n\n if (logEnabled) {\n logger!.debug(LOG_MODULE, `${level}.recovered`, middleware.name, {\n middleware: middleware.name,\n level,\n });\n }\n\n result = recovered as TResult;\n }\n\n if (hooks.after) {\n const replacement = await (\n hooks.after as (\n ctx: unknown,\n value: unknown,\n ) => Promise<unknown> | unknown\n )(context, result);\n\n if (replacement !== undefined) {\n result = replacement as TResult;\n }\n\n if (logEnabled) {\n logger!.debug(LOG_MODULE, `${level}.after`, middleware.name, {\n middleware: middleware.name,\n level,\n });\n }\n }\n\n return result;\n };\n }\n\n return next();\n}\n"],"mappings":";AAQA,MAAM,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiEnB,eAAsB,YACpB,aACA,OACA,SACA,OACA,QACkB;CAClB,IAAI,YAAY,WAAW,GACzB,OAAO,MAAM;CAGf,IAAI,OAA+B;CAEnC,KAAK,IAAI,QAAQ,YAAY,SAAS,GAAG,SAAS,GAAG,SAAS;EAC5D,MAAM,aAAa,YAAY;EAC/B,MAAM,QAAQ,WAAW;EAEzB,IAAI,CAAC,OACH;EAGF,MAAM,WAAW;EAEjB,OAAO,YAAY;GACjB,MAAM,aAAa,WAAW,QAAQ,SAAS,WAAW;GAE1D,IAAI,MAAM,QAAQ;IAChB,IAAI,YACF,OAAQ,MAAM,YAAY,GAAG,MAAM,UAAU,WAAW,MAAM;KAC5D,YAAY,WAAW;KACvB;IACF,CAAC;IAGH,MAAM,eAAe,MACnB,MAAM,OACN,OAAO;IAET,IAAI,iBAAiB,QAAW;KAC9B,IAAI,YACF,OAAQ,MACN,YACA,GAAG,MAAM,iBACT,WAAW,MACX;MACE,YAAY,WAAW;MACvB;KACF,CACF;KAGF,OAAO;IACT;GACF;GAEA,IAAI;GAEJ,IAAI;IACF,SAAS,MAAM,SAAS;GAC1B,SAAS,QAAQ;IACf,IAAI,CAAC,MAAM,SACT,MAAM;IAGR,MAAM,YAAY,MAChB,MAAM,QAIN,SAAS,MAAM;IAEjB,IAAI,cAAc,QAChB,MAAM;IAGR,IAAI,YACF,OAAQ,MAAM,YAAY,GAAG,MAAM,aAAa,WAAW,MAAM;KAC/D,YAAY,WAAW;KACvB;IACF,CAAC;IAGH,SAAS;GACX;GAEA,IAAI,MAAM,OAAO;IACf,MAAM,cAAc,MAClB,MAAM,MAIN,SAAS,MAAM;IAEjB,IAAI,gBAAgB,QAClB,SAAS;IAGX,IAAI,YACF,OAAQ,MAAM,YAAY,GAAG,MAAM,SAAS,WAAW,MAAM;KAC3D,YAAY,WAAW;KACvB;IACF,CAAC;GAEL;GAEA,OAAO;EACT;CACF;CAEA,OAAO,KAAK;AACd"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Message } from "../../contracts/conversation-message.type.mjs";
|
|
2
|
+
|
|
3
|
+
//#region ../../@warlock.js/ai/src/middleware/utils/extract-user-text.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Pull the text a content-inspection middleware should care about
|
|
6
|
+
* from the outbound message list.
|
|
7
|
+
*
|
|
8
|
+
* **Role.** Built-ins that inspect "what the user just said" — the
|
|
9
|
+
* guardrail on `trip.before`, the semantic cache on `trip.before`,
|
|
10
|
+
* future consumers like PII redactors — all need the same string:
|
|
11
|
+
* the most recent `user`-role message's text content. This helper
|
|
12
|
+
* is the single authority on how that string is resolved.
|
|
13
|
+
*
|
|
14
|
+
* **Behavior.**
|
|
15
|
+
* - Walks `messages` from the end backwards so the LAST user turn
|
|
16
|
+
* wins (correct when the agent has history + a fresh prompt).
|
|
17
|
+
* - Returns a plain string directly when `content` is a string.
|
|
18
|
+
* - Joins `text` parts with `"\n"` when `content` is a multipart
|
|
19
|
+
* `ContentPart[]`. Non-text parts (images, audio) are skipped —
|
|
20
|
+
* callers concerned with multimodal content inspect `request`
|
|
21
|
+
* / attachments separately.
|
|
22
|
+
* - Returns `""` when there is no user message at all (e.g. a trip
|
|
23
|
+
* composed entirely of tool results).
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* const prompt = extractUserText(context.messages);
|
|
27
|
+
* if (!prompt) return;
|
|
28
|
+
* const verdict = await inputCheck(prompt);
|
|
29
|
+
*/
|
|
30
|
+
declare function extractUserText(messages: ReadonlyArray<Message>): string;
|
|
31
|
+
//#endregion
|
|
32
|
+
export { extractUserText };
|
|
33
|
+
//# sourceMappingURL=extract-user-text.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extract-user-text.d.mts","names":[],"sources":["../../../../../../../@warlock.js/ai/src/middleware/utils/extract-user-text.ts"],"mappings":";;;;;AA4BA;;;;;;;;AAAgE;;;;;;;;;;;;;;;;iBAAhD,eAAA,CAAgB,QAAA,EAAU,aAAa,CAAC,OAAA"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
//#region ../../@warlock.js/ai/src/middleware/utils/extract-user-text.ts
|
|
2
|
+
/**
|
|
3
|
+
* Pull the text a content-inspection middleware should care about
|
|
4
|
+
* from the outbound message list.
|
|
5
|
+
*
|
|
6
|
+
* **Role.** Built-ins that inspect "what the user just said" — the
|
|
7
|
+
* guardrail on `trip.before`, the semantic cache on `trip.before`,
|
|
8
|
+
* future consumers like PII redactors — all need the same string:
|
|
9
|
+
* the most recent `user`-role message's text content. This helper
|
|
10
|
+
* is the single authority on how that string is resolved.
|
|
11
|
+
*
|
|
12
|
+
* **Behavior.**
|
|
13
|
+
* - Walks `messages` from the end backwards so the LAST user turn
|
|
14
|
+
* wins (correct when the agent has history + a fresh prompt).
|
|
15
|
+
* - Returns a plain string directly when `content` is a string.
|
|
16
|
+
* - Joins `text` parts with `"\n"` when `content` is a multipart
|
|
17
|
+
* `ContentPart[]`. Non-text parts (images, audio) are skipped —
|
|
18
|
+
* callers concerned with multimodal content inspect `request`
|
|
19
|
+
* / attachments separately.
|
|
20
|
+
* - Returns `""` when there is no user message at all (e.g. a trip
|
|
21
|
+
* composed entirely of tool results).
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* const prompt = extractUserText(context.messages);
|
|
25
|
+
* if (!prompt) return;
|
|
26
|
+
* const verdict = await inputCheck(prompt);
|
|
27
|
+
*/
|
|
28
|
+
function extractUserText(messages) {
|
|
29
|
+
for (let index = messages.length - 1; index >= 0; index--) {
|
|
30
|
+
const message = messages[index];
|
|
31
|
+
if (message.role !== "user") continue;
|
|
32
|
+
if (typeof message.content === "string") return message.content;
|
|
33
|
+
if (Array.isArray(message.content)) return message.content.filter((part) => part.type === "text").map((part) => part.text).join("\n");
|
|
34
|
+
}
|
|
35
|
+
return "";
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
//#endregion
|
|
39
|
+
export { extractUserText };
|
|
40
|
+
//# sourceMappingURL=extract-user-text.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extract-user-text.mjs","names":[],"sources":["../../../../../../../@warlock.js/ai/src/middleware/utils/extract-user-text.ts"],"sourcesContent":["import type { Message } from \"../../contracts/conversation-message.type\";\n\n/**\n * Pull the text a content-inspection middleware should care about\n * from the outbound message list.\n *\n * **Role.** Built-ins that inspect \"what the user just said\" — the\n * guardrail on `trip.before`, the semantic cache on `trip.before`,\n * future consumers like PII redactors — all need the same string:\n * the most recent `user`-role message's text content. This helper\n * is the single authority on how that string is resolved.\n *\n * **Behavior.**\n * - Walks `messages` from the end backwards so the LAST user turn\n * wins (correct when the agent has history + a fresh prompt).\n * - Returns a plain string directly when `content` is a string.\n * - Joins `text` parts with `\"\\n\"` when `content` is a multipart\n * `ContentPart[]`. Non-text parts (images, audio) are skipped —\n * callers concerned with multimodal content inspect `request`\n * / attachments separately.\n * - Returns `\"\"` when there is no user message at all (e.g. a trip\n * composed entirely of tool results).\n *\n * @example\n * const prompt = extractUserText(context.messages);\n * if (!prompt) return;\n * const verdict = await inputCheck(prompt);\n */\nexport function extractUserText(messages: ReadonlyArray<Message>): string {\n for (let index = messages.length - 1; index >= 0; index--) {\n const message = messages[index];\n\n if (message.role !== \"user\") {\n continue;\n }\n\n if (typeof message.content === \"string\") {\n return message.content;\n }\n\n if (Array.isArray(message.content)) {\n return message.content\n .filter((part) => part.type === \"text\")\n .map((part) => (part as { text: string }).text)\n .join(\"\\n\");\n }\n }\n\n return \"\";\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA,SAAgB,gBAAgB,UAA0C;CACxE,KAAK,IAAI,QAAQ,SAAS,SAAS,GAAG,SAAS,GAAG,SAAS;EACzD,MAAM,UAAU,SAAS;EAEzB,IAAI,QAAQ,SAAS,QACnB;EAGF,IAAI,OAAO,QAAQ,YAAY,UAC7B,OAAO,QAAQ;EAGjB,IAAI,MAAM,QAAQ,QAAQ,OAAO,GAC/B,OAAO,QAAQ,QACZ,QAAQ,SAAS,KAAK,SAAS,MAAM,EACrC,KAAK,SAAU,KAA0B,IAAI,EAC7C,KAAK,IAAI;CAEhB;CAEA,OAAO;AACT"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { MiddlewareState } from "../../contracts/middleware/middleware-state.type.mjs";
|
|
2
|
+
//#region ../../@warlock.js/ai/src/middleware/utils/namespaced-state.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* Typed accessor over `ctx.state` for a single namespace key. Wraps
|
|
5
|
+
* the raw `Map<string, unknown>` so middleware authors stop typing
|
|
6
|
+
* `as Counters | undefined` on every read.
|
|
7
|
+
*
|
|
8
|
+
* **Role.** Every built-in middleware reads and writes one or two
|
|
9
|
+
* entries in `ctx.state` under its own name. Without a helper,
|
|
10
|
+
* every call-site looks like:
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* const counters = context.state.get("budget.counters") as Counters | undefined;
|
|
14
|
+
* if (!counters) { ... }
|
|
15
|
+
* counters.tokens += n;
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* — cast noise, no type narrowing on `set`, no protection against
|
|
19
|
+
* key typos. `namespacedState<T>` eliminates all three.
|
|
20
|
+
*
|
|
21
|
+
* **Scope.** Deliberately narrow: one key, typed value, four methods
|
|
22
|
+
* (`get` / `set` / `delete` / `has`). Does NOT try to model compound
|
|
23
|
+
* or nested keys — if you need those, use the raw `ctx.state` Map
|
|
24
|
+
* directly, or create a second namespaced accessor for the second key.
|
|
25
|
+
*
|
|
26
|
+
* **Namespace convention.** Use the middleware's `name` as the key
|
|
27
|
+
* (or a `name.<field>` prefix when a middleware needs multiple
|
|
28
|
+
* entries). The pipeline does not enforce this — it is a convention
|
|
29
|
+
* the built-ins follow to avoid collisions between middlewares.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* // Inside a budget middleware:
|
|
33
|
+
* const counters = namespacedState<Counters>(ctx, "budget");
|
|
34
|
+
*
|
|
35
|
+
* if (!counters.has()) {
|
|
36
|
+
* counters.set({ tokens: 0, costUSD: 0 });
|
|
37
|
+
* }
|
|
38
|
+
*
|
|
39
|
+
* const current = counters.get()!;
|
|
40
|
+
* current.tokens += response.usage.total;
|
|
41
|
+
*/
|
|
42
|
+
declare function namespacedState<T>(ctx: {
|
|
43
|
+
readonly state: MiddlewareState;
|
|
44
|
+
}, namespace: string): NamespacedStateAccessor<T>;
|
|
45
|
+
/**
|
|
46
|
+
* Four-method accessor returned by `namespacedState`. Callers hold
|
|
47
|
+
* it for the lifetime of a hook body — it is a thin typed view over
|
|
48
|
+
* `ctx.state`, not a detached snapshot. Reads are live; writes hit
|
|
49
|
+
* the underlying Map immediately and are visible to every other
|
|
50
|
+
* hook that uses the same namespace.
|
|
51
|
+
*/
|
|
52
|
+
type NamespacedStateAccessor<T> = {
|
|
53
|
+
get(): T | undefined;
|
|
54
|
+
set(value: T): void;
|
|
55
|
+
delete(): void;
|
|
56
|
+
has(): boolean;
|
|
57
|
+
};
|
|
58
|
+
//#endregion
|
|
59
|
+
export { NamespacedStateAccessor, namespacedState };
|
|
60
|
+
//# sourceMappingURL=namespaced-state.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"namespaced-state.d.mts","names":[],"sources":["../../../../../../../@warlock.js/ai/src/middleware/utils/namespaced-state.ts"],"mappings":";;;;;AAyCA;;;;;;;;;;;;;;;;AAG4B;AAwB5B;;;;;;;;;;;;;AAIK;;;;;;iBA/BW,eAAA,GAAA,CACd,GAAA;EAAA,SAAgB,KAAA,EAAO,eAAA;AAAA,GACvB,SAAA,WACC,uBAAA,CAAwB,CAAA;;;;;;;;KAwBf,uBAAA;EACV,GAAA,IAAO,CAAA;EACP,GAAA,CAAI,KAAA,EAAO,CAAC;EACZ,MAAA;EACA,GAAA;AAAA"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
//#region ../../@warlock.js/ai/src/middleware/utils/namespaced-state.ts
|
|
2
|
+
/**
|
|
3
|
+
* Typed accessor over `ctx.state` for a single namespace key. Wraps
|
|
4
|
+
* the raw `Map<string, unknown>` so middleware authors stop typing
|
|
5
|
+
* `as Counters | undefined` on every read.
|
|
6
|
+
*
|
|
7
|
+
* **Role.** Every built-in middleware reads and writes one or two
|
|
8
|
+
* entries in `ctx.state` under its own name. Without a helper,
|
|
9
|
+
* every call-site looks like:
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* const counters = context.state.get("budget.counters") as Counters | undefined;
|
|
13
|
+
* if (!counters) { ... }
|
|
14
|
+
* counters.tokens += n;
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* — cast noise, no type narrowing on `set`, no protection against
|
|
18
|
+
* key typos. `namespacedState<T>` eliminates all three.
|
|
19
|
+
*
|
|
20
|
+
* **Scope.** Deliberately narrow: one key, typed value, four methods
|
|
21
|
+
* (`get` / `set` / `delete` / `has`). Does NOT try to model compound
|
|
22
|
+
* or nested keys — if you need those, use the raw `ctx.state` Map
|
|
23
|
+
* directly, or create a second namespaced accessor for the second key.
|
|
24
|
+
*
|
|
25
|
+
* **Namespace convention.** Use the middleware's `name` as the key
|
|
26
|
+
* (or a `name.<field>` prefix when a middleware needs multiple
|
|
27
|
+
* entries). The pipeline does not enforce this — it is a convention
|
|
28
|
+
* the built-ins follow to avoid collisions between middlewares.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* // Inside a budget middleware:
|
|
32
|
+
* const counters = namespacedState<Counters>(ctx, "budget");
|
|
33
|
+
*
|
|
34
|
+
* if (!counters.has()) {
|
|
35
|
+
* counters.set({ tokens: 0, costUSD: 0 });
|
|
36
|
+
* }
|
|
37
|
+
*
|
|
38
|
+
* const current = counters.get()!;
|
|
39
|
+
* current.tokens += response.usage.total;
|
|
40
|
+
*/
|
|
41
|
+
function namespacedState(ctx, namespace) {
|
|
42
|
+
return {
|
|
43
|
+
get() {
|
|
44
|
+
return ctx.state.get(namespace);
|
|
45
|
+
},
|
|
46
|
+
set(value) {
|
|
47
|
+
ctx.state.set(namespace, value);
|
|
48
|
+
},
|
|
49
|
+
delete() {
|
|
50
|
+
ctx.state.delete(namespace);
|
|
51
|
+
},
|
|
52
|
+
has() {
|
|
53
|
+
return ctx.state.has(namespace);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
//#endregion
|
|
59
|
+
export { namespacedState };
|
|
60
|
+
//# sourceMappingURL=namespaced-state.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"namespaced-state.mjs","names":[],"sources":["../../../../../../../@warlock.js/ai/src/middleware/utils/namespaced-state.ts"],"sourcesContent":["import type { MiddlewareState } from \"../../contracts/middleware\";\n\n/**\n * Typed accessor over `ctx.state` for a single namespace key. Wraps\n * the raw `Map<string, unknown>` so middleware authors stop typing\n * `as Counters | undefined` on every read.\n *\n * **Role.** Every built-in middleware reads and writes one or two\n * entries in `ctx.state` under its own name. Without a helper,\n * every call-site looks like:\n *\n * ```ts\n * const counters = context.state.get(\"budget.counters\") as Counters | undefined;\n * if (!counters) { ... }\n * counters.tokens += n;\n * ```\n *\n * — cast noise, no type narrowing on `set`, no protection against\n * key typos. `namespacedState<T>` eliminates all three.\n *\n * **Scope.** Deliberately narrow: one key, typed value, four methods\n * (`get` / `set` / `delete` / `has`). Does NOT try to model compound\n * or nested keys — if you need those, use the raw `ctx.state` Map\n * directly, or create a second namespaced accessor for the second key.\n *\n * **Namespace convention.** Use the middleware's `name` as the key\n * (or a `name.<field>` prefix when a middleware needs multiple\n * entries). The pipeline does not enforce this — it is a convention\n * the built-ins follow to avoid collisions between middlewares.\n *\n * @example\n * // Inside a budget middleware:\n * const counters = namespacedState<Counters>(ctx, \"budget\");\n *\n * if (!counters.has()) {\n * counters.set({ tokens: 0, costUSD: 0 });\n * }\n *\n * const current = counters.get()!;\n * current.tokens += response.usage.total;\n */\nexport function namespacedState<T>(\n ctx: { readonly state: MiddlewareState },\n namespace: string,\n): NamespacedStateAccessor<T> {\n return {\n get(): T | undefined {\n return ctx.state.get(namespace) as T | undefined;\n },\n set(value: T): void {\n ctx.state.set(namespace, value);\n },\n delete(): void {\n ctx.state.delete(namespace);\n },\n has(): boolean {\n return ctx.state.has(namespace);\n },\n };\n}\n\n/**\n * Four-method accessor returned by `namespacedState`. Callers hold\n * it for the lifetime of a hook body — it is a thin typed view over\n * `ctx.state`, not a detached snapshot. Reads are live; writes hit\n * the underlying Map immediately and are visible to every other\n * hook that uses the same namespace.\n */\nexport type NamespacedStateAccessor<T> = {\n get(): T | undefined;\n set(value: T): void;\n delete(): void;\n has(): boolean;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCA,SAAgB,gBACd,KACA,WAC4B;CAC5B,OAAO;EACL,MAAqB;GACnB,OAAO,IAAI,MAAM,IAAI,SAAS;EAChC;EACA,IAAI,OAAgB;GAClB,IAAI,MAAM,IAAI,WAAW,KAAK;EAChC;EACA,SAAe;GACb,IAAI,MAAM,OAAO,SAAS;EAC5B;EACA,MAAe;GACb,OAAO,IAAI,MAAM,IAAI,SAAS;EAChC;CACF;AACF"}
|