@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,89 @@
|
|
|
1
|
+
//#region ../../@warlock.js/ai/src/utils/json-schema.ts
|
|
2
|
+
/**
|
|
3
|
+
* Best-effort JSON Schema extraction from a Standard Schema instance.
|
|
4
|
+
*
|
|
5
|
+
* Different libraries expose their JSON representation through different
|
|
6
|
+
* paths:
|
|
7
|
+
* - **Seal / Standard JSON Schema V1**: `["~standard"].jsonSchema.input({ target })`
|
|
8
|
+
* — nested under the spec object, takes a target switch. Default target
|
|
9
|
+
* is `"openai-strict"` since the primary consumer is OpenAI's native
|
|
10
|
+
* structured-output mechanism; pass `options.target` to override.
|
|
11
|
+
* - **Zod / similar**: top-level `.jsonSchema` property. Zod v4 actually
|
|
12
|
+
* ships `toJSONSchema` as a module function, not a method, so it does
|
|
13
|
+
* NOT hit this probe — Zod users pass their converted schema via the
|
|
14
|
+
* `AgentExecuteOptions.responseSchema` escape hatch.
|
|
15
|
+
*
|
|
16
|
+
* Deliberately does NOT probe `toJSON` — that's a generic JavaScript
|
|
17
|
+
* serialization hook (Seal's schemas have one that dumps internal rule
|
|
18
|
+
* state) and matching it would return garbage disguised as a JSON Schema.
|
|
19
|
+
*
|
|
20
|
+
* Returns `undefined` when no path matches. The caller then either skips
|
|
21
|
+
* native structured-output wiring or falls back to a schema-less
|
|
22
|
+
* instruction.
|
|
23
|
+
*
|
|
24
|
+
* Shared across every SDK adapter package (OpenAI, Anthropic, Bedrock…)
|
|
25
|
+
* so each provider converts schemas identically.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* const schema = extractJsonSchema(mySealSchema);
|
|
29
|
+
* // { type: "object", properties: { ... }, required: [ ... ], additionalProperties: false }
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* // Ask for a different target explicitly
|
|
33
|
+
* const draft = extractJsonSchema(mySealSchema, { target: "draft-2020-12" });
|
|
34
|
+
*/
|
|
35
|
+
function extractJsonSchema(schema, options = {}) {
|
|
36
|
+
if (!schema) return void 0;
|
|
37
|
+
const sealJsonSchema = extractFromSealPath(schema, options.target ?? "openai-strict");
|
|
38
|
+
if (sealJsonSchema) return sealJsonSchema;
|
|
39
|
+
const topLevel = extractFromCandidateKeys(schema);
|
|
40
|
+
if (topLevel) return topLevel;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Probe the Standard JSON Schema V1 extension path on `schema["~standard"]`.
|
|
44
|
+
* The spec defines `jsonSchema.input({ target, libraryOptions? })` as a
|
|
45
|
+
* function returning a JSON Schema tailored to the requested target. We
|
|
46
|
+
* pass the caller's target (default `"openai-strict"`) so the library
|
|
47
|
+
* produces output ready for OpenAI's native structured-output mode
|
|
48
|
+
* without additional post-processing.
|
|
49
|
+
*
|
|
50
|
+
* Calling `.input()` without a target would throw (or return garbage) per
|
|
51
|
+
* the spec — a failure here returns `undefined` so the fallback probe
|
|
52
|
+
* runs.
|
|
53
|
+
*/
|
|
54
|
+
function extractFromSealPath(schema, target) {
|
|
55
|
+
const standardSlot = schema["~standard"];
|
|
56
|
+
if (!standardSlot || typeof standardSlot !== "object") return;
|
|
57
|
+
const jsonSchemaSlot = standardSlot["jsonSchema"];
|
|
58
|
+
if (!jsonSchemaSlot || typeof jsonSchemaSlot !== "object") return;
|
|
59
|
+
const inputFn = jsonSchemaSlot["input"];
|
|
60
|
+
if (typeof inputFn !== "function") return;
|
|
61
|
+
try {
|
|
62
|
+
const result = inputFn.call(jsonSchemaSlot, { target });
|
|
63
|
+
if (result && typeof result === "object") return result;
|
|
64
|
+
} catch {}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Probe well-known top-level keys libraries use to expose their JSON
|
|
68
|
+
* Schema. Supports both method form (rare) and property form (common).
|
|
69
|
+
* Deliberately narrow — `toJSON` is NOT probed here because it's a
|
|
70
|
+
* generic serialization hook that returns library-internal state for
|
|
71
|
+
* many validators (Seal included), not a JSON Schema.
|
|
72
|
+
*/
|
|
73
|
+
function extractFromCandidateKeys(schemaRecord) {
|
|
74
|
+
for (const key of ["jsonSchema", "_jsonSchema"]) {
|
|
75
|
+
const value = schemaRecord[key];
|
|
76
|
+
if (typeof value === "function") {
|
|
77
|
+
try {
|
|
78
|
+
const result = value.call(schemaRecord);
|
|
79
|
+
if (result && typeof result === "object") return result;
|
|
80
|
+
} catch {}
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
if (value && typeof value === "object") return value;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
//#endregion
|
|
88
|
+
export { extractJsonSchema };
|
|
89
|
+
//# sourceMappingURL=json-schema.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json-schema.mjs","names":[],"sources":["../../../../../../@warlock.js/ai/src/utils/json-schema.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\n\n/**\n * Supported JSON Schema output targets per the Standard JSON Schema V1\n * spec. `openai-strict` is the richest for OpenAI structured outputs —\n * every property listed in `required`, optionals expressed as\n * `[\"T\", \"null\"]`, `additionalProperties: false` everywhere. Other\n * targets produce standards-compliant but looser output.\n */\nexport type JsonSchemaTarget = \"draft-2020-12\" | \"draft-07\" | \"openapi-3.0\" | \"openai-strict\";\n\n/**\n * Options for `extractJsonSchema`. `target` is forwarded to libraries that\n * implement the Standard JSON Schema V1 spec (Seal, and any future lib\n * that follows the spec). Libraries using their own top-level `.jsonSchema`\n * / `._jsonSchema` property ignore the target.\n */\nexport type ExtractJsonSchemaOptions = {\n target?: JsonSchemaTarget | (string & {});\n};\n\n/**\n * Best-effort JSON Schema extraction from a Standard Schema instance.\n *\n * Different libraries expose their JSON representation through different\n * paths:\n * - **Seal / Standard JSON Schema V1**: `[\"~standard\"].jsonSchema.input({ target })`\n * — nested under the spec object, takes a target switch. Default target\n * is `\"openai-strict\"` since the primary consumer is OpenAI's native\n * structured-output mechanism; pass `options.target` to override.\n * - **Zod / similar**: top-level `.jsonSchema` property. Zod v4 actually\n * ships `toJSONSchema` as a module function, not a method, so it does\n * NOT hit this probe — Zod users pass their converted schema via the\n * `AgentExecuteOptions.responseSchema` escape hatch.\n *\n * Deliberately does NOT probe `toJSON` — that's a generic JavaScript\n * serialization hook (Seal's schemas have one that dumps internal rule\n * state) and matching it would return garbage disguised as a JSON Schema.\n *\n * Returns `undefined` when no path matches. The caller then either skips\n * native structured-output wiring or falls back to a schema-less\n * instruction.\n *\n * Shared across every SDK adapter package (OpenAI, Anthropic, Bedrock…)\n * so each provider converts schemas identically.\n *\n * @example\n * const schema = extractJsonSchema(mySealSchema);\n * // { type: \"object\", properties: { ... }, required: [ ... ], additionalProperties: false }\n *\n * @example\n * // Ask for a different target explicitly\n * const draft = extractJsonSchema(mySealSchema, { target: \"draft-2020-12\" });\n */\nexport function extractJsonSchema(\n schema: StandardSchemaV1<unknown> | undefined,\n options: ExtractJsonSchemaOptions = {},\n): Record<string, unknown> | undefined {\n if (!schema) return undefined;\n\n const target = options.target ?? \"openai-strict\";\n\n // 1. Seal / Standard JSON Schema V1 pattern: [\"~standard\"].jsonSchema.input({ target })\n const sealJsonSchema = extractFromSealPath(schema, target);\n\n if (sealJsonSchema) {\n return sealJsonSchema;\n }\n\n // 2. Top-level jsonSchema / _jsonSchema (Zod-like, property or method form)\n const topLevel = extractFromCandidateKeys(schema as unknown as Record<string, unknown>);\n\n if (topLevel) {\n return topLevel;\n }\n\n return undefined;\n}\n\n/**\n * Probe the Standard JSON Schema V1 extension path on `schema[\"~standard\"]`.\n * The spec defines `jsonSchema.input({ target, libraryOptions? })` as a\n * function returning a JSON Schema tailored to the requested target. We\n * pass the caller's target (default `\"openai-strict\"`) so the library\n * produces output ready for OpenAI's native structured-output mode\n * without additional post-processing.\n *\n * Calling `.input()` without a target would throw (or return garbage) per\n * the spec — a failure here returns `undefined` so the fallback probe\n * runs.\n */\nfunction extractFromSealPath(\n schema: StandardSchemaV1<unknown>,\n target: string,\n): Record<string, unknown> | undefined {\n const standardSlot = (schema as unknown as Record<string, unknown>)[\"~standard\"];\n\n if (!standardSlot || typeof standardSlot !== \"object\") {\n return undefined;\n }\n\n const jsonSchemaSlot = (standardSlot as Record<string, unknown>)[\"jsonSchema\"];\n\n if (!jsonSchemaSlot || typeof jsonSchemaSlot !== \"object\") {\n return undefined;\n }\n\n const inputFn = (jsonSchemaSlot as Record<string, unknown>)[\"input\"];\n\n if (typeof inputFn !== \"function\") {\n return undefined;\n }\n\n try {\n const result = (inputFn as (options: { target: string }) => unknown).call(jsonSchemaSlot, {\n target,\n });\n\n if (result && typeof result === \"object\") {\n return result as Record<string, unknown>;\n }\n } catch {\n // fall through — library didn't support the target or threw otherwise\n }\n\n return undefined;\n}\n\n/**\n * Probe well-known top-level keys libraries use to expose their JSON\n * Schema. Supports both method form (rare) and property form (common).\n * Deliberately narrow — `toJSON` is NOT probed here because it's a\n * generic serialization hook that returns library-internal state for\n * many validators (Seal included), not a JSON Schema.\n */\nfunction extractFromCandidateKeys(\n schemaRecord: Record<string, unknown>,\n): Record<string, unknown> | undefined {\n const candidateKeys = [\"jsonSchema\", \"_jsonSchema\"] as const;\n\n for (const key of candidateKeys) {\n const value = schemaRecord[key];\n\n if (typeof value === \"function\") {\n try {\n const result = (value as () => unknown).call(schemaRecord);\n\n if (result && typeof result === \"object\") {\n return result as Record<string, unknown>;\n }\n } catch {\n // try next candidate\n }\n\n continue;\n }\n\n if (value && typeof value === \"object\") {\n return value as Record<string, unknown>;\n }\n }\n\n return undefined;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsDA,SAAgB,kBACd,QACA,UAAoC,CAAC,GACA;CACrC,IAAI,CAAC,QAAQ,OAAO;CAKpB,MAAM,iBAAiB,oBAAoB,QAH5B,QAAQ,UAAU,eAGwB;CAEzD,IAAI,gBACF,OAAO;CAIT,MAAM,WAAW,yBAAyB,MAA4C;CAEtF,IAAI,UACF,OAAO;AAIX;;;;;;;;;;;;;AAcA,SAAS,oBACP,QACA,QACqC;CACrC,MAAM,eAAgB,OAA8C;CAEpE,IAAI,CAAC,gBAAgB,OAAO,iBAAiB,UAC3C;CAGF,MAAM,iBAAkB,aAAyC;CAEjE,IAAI,CAAC,kBAAkB,OAAO,mBAAmB,UAC/C;CAGF,MAAM,UAAW,eAA2C;CAE5D,IAAI,OAAO,YAAY,YACrB;CAGF,IAAI;EACF,MAAM,SAAU,QAAqD,KAAK,gBAAgB,EACxF,OACF,CAAC;EAED,IAAI,UAAU,OAAO,WAAW,UAC9B,OAAO;CAEX,QAAQ,CAER;AAGF;;;;;;;;AASA,SAAS,yBACP,cACqC;CAGrC,KAAK,MAAM,OAAO,CAFK,cAAc,aAEP,GAAG;EAC/B,MAAM,QAAQ,aAAa;EAE3B,IAAI,OAAO,UAAU,YAAY;GAC/B,IAAI;IACF,MAAM,SAAU,MAAwB,KAAK,YAAY;IAEzD,IAAI,UAAU,OAAO,WAAW,UAC9B,OAAO;GAEX,QAAQ,CAER;GAEA;EACF;EAEA,IAAI,SAAS,OAAO,UAAU,UAC5B,OAAO;CAEX;AAGF"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Attachment } from "../contracts/attachment.type.mjs";
|
|
2
|
+
import { ContentPart } from "../contracts/content-part.type.mjs";
|
|
3
|
+
|
|
4
|
+
//#region ../../@warlock.js/ai/src/utils/prepare-attachment-part.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Convert a user-supplied `Attachment` into a provider-ready
|
|
7
|
+
* `ContentPart` the model adapter can consume without doing any I/O of
|
|
8
|
+
* its own.
|
|
9
|
+
*
|
|
10
|
+
* Kind resolution:
|
|
11
|
+
* - Tagged `{ type: "image", source }` / `{ type: "text", source }`
|
|
12
|
+
* trusts the caller's intent.
|
|
13
|
+
* - Shorthand (raw string / `StorageFileShape`) infers from the file
|
|
14
|
+
* extension. Image extensions (`.png`/`.jpg`/`.jpeg`/`.webp`/`.gif`)
|
|
15
|
+
* map to `"image"`. `.txt` maps to `"text"`. Anything else throws
|
|
16
|
+
* `InvalidRequestError` — silent inference on ambiguous inputs
|
|
17
|
+
* causes silent bugs.
|
|
18
|
+
*
|
|
19
|
+
* Local paths are read from disk; images are base64-encoded inline,
|
|
20
|
+
* text files are read as UTF-8 strings and returned as a `text`
|
|
21
|
+
* `ContentPart`. Remote URLs for image attachments are passed through
|
|
22
|
+
* unchanged; remote URLs for text attachments are fetched so the
|
|
23
|
+
* adapter never needs network access.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* await prepareAttachmentPart("./photo.png");
|
|
27
|
+
* // → { type: "image", source: { base64: "...", mediaType: "image/png" } }
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* await prepareAttachmentPart({ type: "text", source: "./notes.txt" });
|
|
31
|
+
* // → { type: "text", text: "<file contents>" }
|
|
32
|
+
*/
|
|
33
|
+
declare function prepareAttachmentPart(attachment: Attachment): Promise<ContentPart>;
|
|
34
|
+
//#endregion
|
|
35
|
+
export { prepareAttachmentPart };
|
|
36
|
+
//# sourceMappingURL=prepare-attachment-part.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prepare-attachment-part.d.mts","names":[],"sources":["../../../../../../@warlock.js/ai/src/utils/prepare-attachment-part.ts"],"mappings":";;;;;;AA+CA;;;;;;;;;;;;;AAEsB;;;;;;;;;;;;;iBAFA,qBAAA,CACpB,UAAA,EAAY,UAAA,GACX,OAAA,CAAQ,WAAA"}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { InvalidRequestError } from "../errors/invalid-request-error.mjs";
|
|
2
|
+
import "../errors/index.mjs";
|
|
3
|
+
import { resolveAttachment } from "./resolve-attachment.mjs";
|
|
4
|
+
import { readFile } from "node:fs/promises";
|
|
5
|
+
import { extname } from "node:path";
|
|
6
|
+
|
|
7
|
+
//#region ../../@warlock.js/ai/src/utils/prepare-attachment-part.ts
|
|
8
|
+
const IMAGE_EXTENSIONS_TO_MEDIA_TYPE = {
|
|
9
|
+
".png": "image/png",
|
|
10
|
+
".jpg": "image/jpeg",
|
|
11
|
+
".jpeg": "image/jpeg",
|
|
12
|
+
".webp": "image/webp",
|
|
13
|
+
".gif": "image/gif"
|
|
14
|
+
};
|
|
15
|
+
const TEXT_EXTENSIONS = new Set([".txt"]);
|
|
16
|
+
/**
|
|
17
|
+
* Convert a user-supplied `Attachment` into a provider-ready
|
|
18
|
+
* `ContentPart` the model adapter can consume without doing any I/O of
|
|
19
|
+
* its own.
|
|
20
|
+
*
|
|
21
|
+
* Kind resolution:
|
|
22
|
+
* - Tagged `{ type: "image", source }` / `{ type: "text", source }`
|
|
23
|
+
* trusts the caller's intent.
|
|
24
|
+
* - Shorthand (raw string / `StorageFileShape`) infers from the file
|
|
25
|
+
* extension. Image extensions (`.png`/`.jpg`/`.jpeg`/`.webp`/`.gif`)
|
|
26
|
+
* map to `"image"`. `.txt` maps to `"text"`. Anything else throws
|
|
27
|
+
* `InvalidRequestError` — silent inference on ambiguous inputs
|
|
28
|
+
* causes silent bugs.
|
|
29
|
+
*
|
|
30
|
+
* Local paths are read from disk; images are base64-encoded inline,
|
|
31
|
+
* text files are read as UTF-8 strings and returned as a `text`
|
|
32
|
+
* `ContentPart`. Remote URLs for image attachments are passed through
|
|
33
|
+
* unchanged; remote URLs for text attachments are fetched so the
|
|
34
|
+
* adapter never needs network access.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* await prepareAttachmentPart("./photo.png");
|
|
38
|
+
* // → { type: "image", source: { base64: "...", mediaType: "image/png" } }
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* await prepareAttachmentPart({ type: "text", source: "./notes.txt" });
|
|
42
|
+
* // → { type: "text", text: "<file contents>" }
|
|
43
|
+
*/
|
|
44
|
+
async function prepareAttachmentPart(attachment) {
|
|
45
|
+
if (resolveKind(attachment) === "text") return prepareTextPart(attachment);
|
|
46
|
+
return prepareImagePart(attachment);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Decide whether the attachment is text or image. Tagged forms win
|
|
50
|
+
* immediately; for shorthand we inspect the extension. Throws if the
|
|
51
|
+
* shorthand doesn't look like anything we recognize.
|
|
52
|
+
*/
|
|
53
|
+
function resolveKind(attachment) {
|
|
54
|
+
if (isTaggedAttachment(attachment)) return attachment.type;
|
|
55
|
+
const path = extractPath(attachment);
|
|
56
|
+
const extension = path ? extname(stripQuery(path)).toLowerCase() : "";
|
|
57
|
+
if (IMAGE_EXTENSIONS_TO_MEDIA_TYPE[extension]) return "image";
|
|
58
|
+
if (TEXT_EXTENSIONS.has(extension)) return "text";
|
|
59
|
+
throw new InvalidRequestError("Cannot infer attachment type from input — pass an explicit `{ type: 'image' | 'text', source: ... }` or use a recognized extension (.png, .jpg, .jpeg, .webp, .gif, .txt)");
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Produce an `image` ContentPart. URLs pass through; paths are
|
|
63
|
+
* read from disk and base64-encoded with an inferred media type.
|
|
64
|
+
* Inline base64 attachments pass through unchanged.
|
|
65
|
+
*/
|
|
66
|
+
async function prepareImagePart(attachment) {
|
|
67
|
+
const inferredMediaType = isTaggedAttachment(attachment) ? void 0 : inferImageMediaType(attachment);
|
|
68
|
+
const resolved = resolveAttachment(attachment);
|
|
69
|
+
if (resolved.type === "url") return {
|
|
70
|
+
type: "image",
|
|
71
|
+
source: { url: resolved.value }
|
|
72
|
+
};
|
|
73
|
+
if (resolved.type === "base64") return {
|
|
74
|
+
type: "image",
|
|
75
|
+
source: {
|
|
76
|
+
base64: resolved.value,
|
|
77
|
+
mediaType: resolved.mediaType
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
const mediaType = inferredMediaType ?? inferImageMediaType(resolved.value);
|
|
81
|
+
if (!mediaType) throw new InvalidRequestError(`Cannot infer media type for path "${resolved.value}" — use a recognized image extension or pass \`{ type: 'image', source: { base64, mediaType } }\``, { context: { path: resolved.value } });
|
|
82
|
+
return {
|
|
83
|
+
type: "image",
|
|
84
|
+
source: {
|
|
85
|
+
base64: (await readFile(resolved.value)).toString("base64"),
|
|
86
|
+
mediaType
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Produce a `text` ContentPart. URLs are fetched as UTF-8, paths are
|
|
92
|
+
* read from disk as UTF-8, inline base64 is decoded to UTF-8. The
|
|
93
|
+
* result joins the conversation as an additional text part the model
|
|
94
|
+
* sees before responding.
|
|
95
|
+
*/
|
|
96
|
+
async function prepareTextPart(attachment) {
|
|
97
|
+
const resolved = resolveAttachment(attachment);
|
|
98
|
+
if (resolved.type === "url") {
|
|
99
|
+
const response = await fetch(resolved.value);
|
|
100
|
+
if (!response.ok) throw new InvalidRequestError(`Failed to fetch text attachment "${resolved.value}" — status ${response.status}`, { context: {
|
|
101
|
+
url: resolved.value,
|
|
102
|
+
status: response.status
|
|
103
|
+
} });
|
|
104
|
+
return {
|
|
105
|
+
type: "text",
|
|
106
|
+
text: await response.text()
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
if (resolved.type === "base64") return {
|
|
110
|
+
type: "text",
|
|
111
|
+
text: Buffer.from(resolved.value, "base64").toString("utf8")
|
|
112
|
+
};
|
|
113
|
+
return {
|
|
114
|
+
type: "text",
|
|
115
|
+
text: await readFile(resolved.value, "utf8")
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
function isTaggedAttachment(attachment) {
|
|
119
|
+
return typeof attachment === "object" && attachment !== null && "type" in attachment;
|
|
120
|
+
}
|
|
121
|
+
function inferImageMediaType(input) {
|
|
122
|
+
const path = extractPath(input);
|
|
123
|
+
if (!path) return;
|
|
124
|
+
return IMAGE_EXTENSIONS_TO_MEDIA_TYPE[extname(stripQuery(path)).toLowerCase()];
|
|
125
|
+
}
|
|
126
|
+
function extractPath(input) {
|
|
127
|
+
if (typeof input === "string") return input;
|
|
128
|
+
if (typeof input === "object" && input !== null) {
|
|
129
|
+
const storage = input;
|
|
130
|
+
return storage.url ?? storage.absolutePath;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function stripQuery(path) {
|
|
134
|
+
const queryIndex = path.indexOf("?");
|
|
135
|
+
return queryIndex === -1 ? path : path.slice(0, queryIndex);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
//#endregion
|
|
139
|
+
export { prepareAttachmentPart };
|
|
140
|
+
//# sourceMappingURL=prepare-attachment-part.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prepare-attachment-part.mjs","names":[],"sources":["../../../../../../@warlock.js/ai/src/utils/prepare-attachment-part.ts"],"sourcesContent":["import { readFile } from \"node:fs/promises\";\nimport { extname } from \"node:path\";\nimport type { Attachment } from \"../contracts/attachment.type\";\nimport type { ContentPart } from \"../contracts/content-part.type\";\nimport { InvalidRequestError } from \"../errors\";\nimport { resolveAttachment } from \"./resolve-attachment\";\n\nconst IMAGE_EXTENSIONS_TO_MEDIA_TYPE: Record<string, string> = {\n \".png\": \"image/png\",\n \".jpg\": \"image/jpeg\",\n \".jpeg\": \"image/jpeg\",\n \".webp\": \"image/webp\",\n \".gif\": \"image/gif\",\n};\n\nconst TEXT_EXTENSIONS = new Set([\".txt\"]);\n\ntype AttachmentKind = \"image\" | \"text\";\n\n/**\n * Convert a user-supplied `Attachment` into a provider-ready\n * `ContentPart` the model adapter can consume without doing any I/O of\n * its own.\n *\n * Kind resolution:\n * - Tagged `{ type: \"image\", source }` / `{ type: \"text\", source }`\n * trusts the caller's intent.\n * - Shorthand (raw string / `StorageFileShape`) infers from the file\n * extension. Image extensions (`.png`/`.jpg`/`.jpeg`/`.webp`/`.gif`)\n * map to `\"image\"`. `.txt` maps to `\"text\"`. Anything else throws\n * `InvalidRequestError` — silent inference on ambiguous inputs\n * causes silent bugs.\n *\n * Local paths are read from disk; images are base64-encoded inline,\n * text files are read as UTF-8 strings and returned as a `text`\n * `ContentPart`. Remote URLs for image attachments are passed through\n * unchanged; remote URLs for text attachments are fetched so the\n * adapter never needs network access.\n *\n * @example\n * await prepareAttachmentPart(\"./photo.png\");\n * // → { type: \"image\", source: { base64: \"...\", mediaType: \"image/png\" } }\n *\n * @example\n * await prepareAttachmentPart({ type: \"text\", source: \"./notes.txt\" });\n * // → { type: \"text\", text: \"<file contents>\" }\n */\nexport async function prepareAttachmentPart(\n attachment: Attachment,\n): Promise<ContentPart> {\n const kind = resolveKind(attachment);\n\n if (kind === \"text\") {\n return prepareTextPart(attachment);\n }\n\n return prepareImagePart(attachment);\n}\n\n/**\n * Decide whether the attachment is text or image. Tagged forms win\n * immediately; for shorthand we inspect the extension. Throws if the\n * shorthand doesn't look like anything we recognize.\n */\nfunction resolveKind(attachment: Attachment): AttachmentKind {\n if (isTaggedAttachment(attachment)) {\n return attachment.type;\n }\n\n const path = extractPath(attachment);\n const extension = path ? extname(stripQuery(path)).toLowerCase() : \"\";\n\n if (IMAGE_EXTENSIONS_TO_MEDIA_TYPE[extension]) {\n return \"image\";\n }\n\n if (TEXT_EXTENSIONS.has(extension)) {\n return \"text\";\n }\n\n throw new InvalidRequestError(\n \"Cannot infer attachment type from input — pass an explicit `{ type: 'image' | 'text', source: ... }` or use a recognized extension (.png, .jpg, .jpeg, .webp, .gif, .txt)\",\n );\n}\n\n/**\n * Produce an `image` ContentPart. URLs pass through; paths are\n * read from disk and base64-encoded with an inferred media type.\n * Inline base64 attachments pass through unchanged.\n */\nasync function prepareImagePart(attachment: Attachment): Promise<ContentPart> {\n const inferredMediaType = isTaggedAttachment(attachment)\n ? undefined\n : inferImageMediaType(attachment);\n\n const resolved = resolveAttachment(attachment);\n\n if (resolved.type === \"url\") {\n return { type: \"image\", source: { url: resolved.value } };\n }\n\n if (resolved.type === \"base64\") {\n return {\n type: \"image\",\n source: { base64: resolved.value, mediaType: resolved.mediaType },\n };\n }\n\n const mediaType = inferredMediaType ?? inferImageMediaType(resolved.value);\n\n if (!mediaType) {\n throw new InvalidRequestError(\n `Cannot infer media type for path \"${resolved.value}\" — use a recognized image extension or pass ` +\n \"`{ type: 'image', source: { base64, mediaType } }`\",\n { context: { path: resolved.value } },\n );\n }\n\n const bytes = await readFile(resolved.value);\n\n return {\n type: \"image\",\n source: { base64: bytes.toString(\"base64\"), mediaType },\n };\n}\n\n/**\n * Produce a `text` ContentPart. URLs are fetched as UTF-8, paths are\n * read from disk as UTF-8, inline base64 is decoded to UTF-8. The\n * result joins the conversation as an additional text part the model\n * sees before responding.\n */\nasync function prepareTextPart(attachment: Attachment): Promise<ContentPart> {\n const resolved = resolveAttachment(attachment);\n\n if (resolved.type === \"url\") {\n const response = await fetch(resolved.value);\n\n if (!response.ok) {\n throw new InvalidRequestError(\n `Failed to fetch text attachment \"${resolved.value}\" — status ${response.status}`,\n { context: { url: resolved.value, status: response.status } },\n );\n }\n\n return { type: \"text\", text: await response.text() };\n }\n\n if (resolved.type === \"base64\") {\n const decoded = Buffer.from(resolved.value, \"base64\").toString(\"utf8\");\n\n return { type: \"text\", text: decoded };\n }\n\n const bytes = await readFile(resolved.value, \"utf8\");\n\n return { type: \"text\", text: bytes };\n}\n\nfunction isTaggedAttachment(\n attachment: Attachment,\n): attachment is Extract<Attachment, { type: string }> {\n return (\n typeof attachment === \"object\" &&\n attachment !== null &&\n \"type\" in attachment\n );\n}\n\nfunction inferImageMediaType(input: unknown): string | undefined {\n const path = extractPath(input);\n\n if (!path) {\n return undefined;\n }\n\n const extension = extname(stripQuery(path)).toLowerCase();\n\n return IMAGE_EXTENSIONS_TO_MEDIA_TYPE[extension];\n}\n\nfunction extractPath(input: unknown): string | undefined {\n if (typeof input === \"string\") {\n return input;\n }\n\n if (typeof input === \"object\" && input !== null) {\n const storage = input as { url?: string; absolutePath?: string };\n return storage.url ?? storage.absolutePath;\n }\n\n return undefined;\n}\n\nfunction stripQuery(path: string): string {\n const queryIndex = path.indexOf(\"?\");\n\n return queryIndex === -1 ? path : path.slice(0, queryIndex);\n}\n"],"mappings":";;;;;;;AAOA,MAAM,iCAAyD;CAC7D,QAAQ;CACR,QAAQ;CACR,SAAS;CACT,SAAS;CACT,QAAQ;AACV;AAEA,MAAM,kBAAkB,IAAI,IAAI,CAAC,MAAM,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCxC,eAAsB,sBACpB,YACsB;CAGtB,IAFa,YAAY,UAElB,MAAM,QACX,OAAO,gBAAgB,UAAU;CAGnC,OAAO,iBAAiB,UAAU;AACpC;;;;;;AAOA,SAAS,YAAY,YAAwC;CAC3D,IAAI,mBAAmB,UAAU,GAC/B,OAAO,WAAW;CAGpB,MAAM,OAAO,YAAY,UAAU;CACnC,MAAM,YAAY,OAAO,QAAQ,WAAW,IAAI,CAAC,EAAE,YAAY,IAAI;CAEnE,IAAI,+BAA+B,YACjC,OAAO;CAGT,IAAI,gBAAgB,IAAI,SAAS,GAC/B,OAAO;CAGT,MAAM,IAAI,oBACR,2KACF;AACF;;;;;;AAOA,eAAe,iBAAiB,YAA8C;CAC5E,MAAM,oBAAoB,mBAAmB,UAAU,IACnD,SACA,oBAAoB,UAAU;CAElC,MAAM,WAAW,kBAAkB,UAAU;CAE7C,IAAI,SAAS,SAAS,OACpB,OAAO;EAAE,MAAM;EAAS,QAAQ,EAAE,KAAK,SAAS,MAAM;CAAE;CAG1D,IAAI,SAAS,SAAS,UACpB,OAAO;EACL,MAAM;EACN,QAAQ;GAAE,QAAQ,SAAS;GAAO,WAAW,SAAS;EAAU;CAClE;CAGF,MAAM,YAAY,qBAAqB,oBAAoB,SAAS,KAAK;CAEzE,IAAI,CAAC,WACH,MAAM,IAAI,oBACR,qCAAqC,SAAS,MAAM,oGAEpD,EAAE,SAAS,EAAE,MAAM,SAAS,MAAM,EAAE,CACtC;CAKF,OAAO;EACL,MAAM;EACN,QAAQ;GAAE,SAAQ,MAJA,SAAS,SAAS,KAAK,GAIjB,SAAS,QAAQ;GAAG;EAAU;CACxD;AACF;;;;;;;AAQA,eAAe,gBAAgB,YAA8C;CAC3E,MAAM,WAAW,kBAAkB,UAAU;CAE7C,IAAI,SAAS,SAAS,OAAO;EAC3B,MAAM,WAAW,MAAM,MAAM,SAAS,KAAK;EAE3C,IAAI,CAAC,SAAS,IACZ,MAAM,IAAI,oBACR,oCAAoC,SAAS,MAAM,aAAa,SAAS,UACzE,EAAE,SAAS;GAAE,KAAK,SAAS;GAAO,QAAQ,SAAS;EAAO,EAAE,CAC9D;EAGF,OAAO;GAAE,MAAM;GAAQ,MAAM,MAAM,SAAS,KAAK;EAAE;CACrD;CAEA,IAAI,SAAS,SAAS,UAGpB,OAAO;EAAE,MAAM;EAAQ,MAFP,OAAO,KAAK,SAAS,OAAO,QAAQ,EAAE,SAAS,MAE5B;CAAE;CAKvC,OAAO;EAAE,MAAM;EAAQ,MAAM,MAFT,SAAS,SAAS,OAAO,MAAM;CAEhB;AACrC;AAEA,SAAS,mBACP,YACqD;CACrD,OACE,OAAO,eAAe,YACtB,eAAe,QACf,UAAU;AAEd;AAEA,SAAS,oBAAoB,OAAoC;CAC/D,MAAM,OAAO,YAAY,KAAK;CAE9B,IAAI,CAAC,MACH;CAKF,OAAO,+BAFW,QAAQ,WAAW,IAAI,CAAC,EAAE,YAEE;AAChD;AAEA,SAAS,YAAY,OAAoC;CACvD,IAAI,OAAO,UAAU,UACnB,OAAO;CAGT,IAAI,OAAO,UAAU,YAAY,UAAU,MAAM;EAC/C,MAAM,UAAU;EAChB,OAAO,QAAQ,OAAO,QAAQ;CAChC;AAGF;AAEA,SAAS,WAAW,MAAsB;CACxC,MAAM,aAAa,KAAK,QAAQ,GAAG;CAEnC,OAAO,eAAe,KAAK,OAAO,KAAK,MAAM,GAAG,UAAU;AAC5D"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Attachment, ResolvedAttachment } from "../contracts/attachment.type.mjs";
|
|
2
|
+
|
|
3
|
+
//#region ../../@warlock.js/ai/src/utils/resolve-attachment.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Normalize a user-supplied `Attachment` (or bare `AttachmentSource`)
|
|
6
|
+
* into a `ResolvedAttachment` the agent can hand to file-reading code
|
|
7
|
+
* without re-discriminating the input variant.
|
|
8
|
+
*
|
|
9
|
+
* Resolution rules:
|
|
10
|
+
* - `{ base64, mediaType }` → `{ type: "base64", value, mediaType }`.
|
|
11
|
+
* - `StorageFileShape` (`{ url?, absolutePath? }`) → URL wins over
|
|
12
|
+
* absolute path. URL becomes `{ type: "url" }`; absolute path
|
|
13
|
+
* becomes `{ type: "path" }`.
|
|
14
|
+
* - String starting with `http://` / `https://` → `{ type: "url" }`.
|
|
15
|
+
* - Any other string → `{ type: "path" }`.
|
|
16
|
+
* - Tagged `{ type: "image" | "text", source }` → recurses into `source`.
|
|
17
|
+
*
|
|
18
|
+
* Throws `InvalidRequestError` on obviously invalid input (empty
|
|
19
|
+
* string, storage object with neither url nor absolutePath, missing
|
|
20
|
+
* source field).
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* resolveAttachment("https://cdn.example.com/doc.pdf");
|
|
24
|
+
* // → { type: "url", value: "https://cdn.example.com/doc.pdf" }
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* resolveAttachment({ type: "image", source: "/tmp/x.png" });
|
|
28
|
+
* // → { type: "path", value: "/tmp/x.png" }
|
|
29
|
+
*/
|
|
30
|
+
declare function resolveAttachment(attachment: Attachment): ResolvedAttachment;
|
|
31
|
+
//#endregion
|
|
32
|
+
export { resolveAttachment };
|
|
33
|
+
//# sourceMappingURL=resolve-attachment.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolve-attachment.d.mts","names":[],"sources":["../../../../../../@warlock.js/ai/src/utils/resolve-attachment.ts"],"mappings":";;;;;AAmCA;;;;;;;;AAA6E;;;;;;;;;;;;;;;;iBAA7D,iBAAA,CAAkB,UAAA,EAAY,UAAA,GAAa,kBAAkB"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { InvalidRequestError } from "../errors/invalid-request-error.mjs";
|
|
2
|
+
import "../errors/index.mjs";
|
|
3
|
+
|
|
4
|
+
//#region ../../@warlock.js/ai/src/utils/resolve-attachment.ts
|
|
5
|
+
const REMOTE_URL_PATTERN = /^https?:\/\//i;
|
|
6
|
+
/**
|
|
7
|
+
* Normalize a user-supplied `Attachment` (or bare `AttachmentSource`)
|
|
8
|
+
* into a `ResolvedAttachment` the agent can hand to file-reading code
|
|
9
|
+
* without re-discriminating the input variant.
|
|
10
|
+
*
|
|
11
|
+
* Resolution rules:
|
|
12
|
+
* - `{ base64, mediaType }` → `{ type: "base64", value, mediaType }`.
|
|
13
|
+
* - `StorageFileShape` (`{ url?, absolutePath? }`) → URL wins over
|
|
14
|
+
* absolute path. URL becomes `{ type: "url" }`; absolute path
|
|
15
|
+
* becomes `{ type: "path" }`.
|
|
16
|
+
* - String starting with `http://` / `https://` → `{ type: "url" }`.
|
|
17
|
+
* - Any other string → `{ type: "path" }`.
|
|
18
|
+
* - Tagged `{ type: "image" | "text", source }` → recurses into `source`.
|
|
19
|
+
*
|
|
20
|
+
* Throws `InvalidRequestError` on obviously invalid input (empty
|
|
21
|
+
* string, storage object with neither url nor absolutePath, missing
|
|
22
|
+
* source field).
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* resolveAttachment("https://cdn.example.com/doc.pdf");
|
|
26
|
+
* // → { type: "url", value: "https://cdn.example.com/doc.pdf" }
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* resolveAttachment({ type: "image", source: "/tmp/x.png" });
|
|
30
|
+
* // → { type: "path", value: "/tmp/x.png" }
|
|
31
|
+
*/
|
|
32
|
+
function resolveAttachment(attachment) {
|
|
33
|
+
if (typeof attachment === "object" && attachment !== null && "type" in attachment) return resolveSource(attachment.source);
|
|
34
|
+
return resolveSource(attachment);
|
|
35
|
+
}
|
|
36
|
+
function resolveSource(source) {
|
|
37
|
+
if (typeof source === "string") {
|
|
38
|
+
if (!source) throw new InvalidRequestError("Cannot resolve empty attachment string");
|
|
39
|
+
if (REMOTE_URL_PATTERN.test(source)) return {
|
|
40
|
+
type: "url",
|
|
41
|
+
value: source
|
|
42
|
+
};
|
|
43
|
+
return {
|
|
44
|
+
type: "path",
|
|
45
|
+
value: source
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
if ("url" in source || "absolutePath" in source) {
|
|
49
|
+
const storage = source;
|
|
50
|
+
if (storage.absolutePath) return {
|
|
51
|
+
type: "path",
|
|
52
|
+
value: storage.absolutePath
|
|
53
|
+
};
|
|
54
|
+
if (storage.url) return {
|
|
55
|
+
type: "url",
|
|
56
|
+
value: storage.url
|
|
57
|
+
};
|
|
58
|
+
throw new InvalidRequestError("Storage attachment has neither url nor absolutePath");
|
|
59
|
+
}
|
|
60
|
+
if ("base64" in source) {
|
|
61
|
+
if (!source.base64 || !source.mediaType) throw new InvalidRequestError("Inline attachment requires both `base64` and `mediaType`");
|
|
62
|
+
return {
|
|
63
|
+
type: "base64",
|
|
64
|
+
value: source.base64,
|
|
65
|
+
mediaType: source.mediaType
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
throw new InvalidRequestError("Unrecognized attachment source — expected a string path/URL, a StorageFile, or `{ base64, mediaType }`");
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
//#endregion
|
|
72
|
+
export { resolveAttachment };
|
|
73
|
+
//# sourceMappingURL=resolve-attachment.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolve-attachment.mjs","names":[],"sources":["../../../../../../@warlock.js/ai/src/utils/resolve-attachment.ts"],"sourcesContent":["import type {\n Attachment,\n AttachmentSource,\n ResolvedAttachment,\n} from \"../contracts/attachment.type\";\nimport { InvalidRequestError } from \"../errors\";\n\nconst REMOTE_URL_PATTERN = /^https?:\\/\\//i;\n\n/**\n * Normalize a user-supplied `Attachment` (or bare `AttachmentSource`)\n * into a `ResolvedAttachment` the agent can hand to file-reading code\n * without re-discriminating the input variant.\n *\n * Resolution rules:\n * - `{ base64, mediaType }` → `{ type: \"base64\", value, mediaType }`.\n * - `StorageFileShape` (`{ url?, absolutePath? }`) → URL wins over\n * absolute path. URL becomes `{ type: \"url\" }`; absolute path\n * becomes `{ type: \"path\" }`.\n * - String starting with `http://` / `https://` → `{ type: \"url\" }`.\n * - Any other string → `{ type: \"path\" }`.\n * - Tagged `{ type: \"image\" | \"text\", source }` → recurses into `source`.\n *\n * Throws `InvalidRequestError` on obviously invalid input (empty\n * string, storage object with neither url nor absolutePath, missing\n * source field).\n *\n * @example\n * resolveAttachment(\"https://cdn.example.com/doc.pdf\");\n * // → { type: \"url\", value: \"https://cdn.example.com/doc.pdf\" }\n *\n * @example\n * resolveAttachment({ type: \"image\", source: \"/tmp/x.png\" });\n * // → { type: \"path\", value: \"/tmp/x.png\" }\n */\nexport function resolveAttachment(attachment: Attachment): ResolvedAttachment {\n if (\n typeof attachment === \"object\" &&\n attachment !== null &&\n \"type\" in attachment\n ) {\n return resolveSource(attachment.source);\n }\n\n return resolveSource(attachment);\n}\n\nfunction resolveSource(source: AttachmentSource): ResolvedAttachment {\n if (typeof source === \"string\") {\n if (!source) {\n throw new InvalidRequestError(\"Cannot resolve empty attachment string\");\n }\n\n if (REMOTE_URL_PATTERN.test(source)) {\n return { type: \"url\", value: source };\n }\n\n return { type: \"path\", value: source };\n }\n\n // StorageFile objects (from @warlock.js/core) can expose a `base64`\n // property alongside `url` / `absolutePath`. Check storage shape\n // first so we don't treat a StorageFile as an inline-bytes payload.\n if (\"url\" in source || \"absolutePath\" in source) {\n const storage = source as { url?: string; absolutePath?: string };\n\n if (storage.absolutePath) {\n return { type: \"path\", value: storage.absolutePath };\n }\n\n if (storage.url) {\n return { type: \"url\", value: storage.url };\n }\n\n throw new InvalidRequestError(\n \"Storage attachment has neither url nor absolutePath\",\n );\n }\n\n if (\"base64\" in source) {\n if (!source.base64 || !source.mediaType) {\n throw new InvalidRequestError(\n \"Inline attachment requires both `base64` and `mediaType`\",\n );\n }\n\n return {\n type: \"base64\",\n value: source.base64,\n mediaType: source.mediaType,\n };\n }\n\n throw new InvalidRequestError(\n \"Unrecognized attachment source — expected a string path/URL, a StorageFile, or `{ base64, mediaType }`\",\n );\n}\n"],"mappings":";;;;AAOA,MAAM,qBAAqB;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4B3B,SAAgB,kBAAkB,YAA4C;CAC5E,IACE,OAAO,eAAe,YACtB,eAAe,QACf,UAAU,YAEV,OAAO,cAAc,WAAW,MAAM;CAGxC,OAAO,cAAc,UAAU;AACjC;AAEA,SAAS,cAAc,QAA8C;CACnE,IAAI,OAAO,WAAW,UAAU;EAC9B,IAAI,CAAC,QACH,MAAM,IAAI,oBAAoB,wCAAwC;EAGxE,IAAI,mBAAmB,KAAK,MAAM,GAChC,OAAO;GAAE,MAAM;GAAO,OAAO;EAAO;EAGtC,OAAO;GAAE,MAAM;GAAQ,OAAO;EAAO;CACvC;CAKA,IAAI,SAAS,UAAU,kBAAkB,QAAQ;EAC/C,MAAM,UAAU;EAEhB,IAAI,QAAQ,cACV,OAAO;GAAE,MAAM;GAAQ,OAAO,QAAQ;EAAa;EAGrD,IAAI,QAAQ,KACV,OAAO;GAAE,MAAM;GAAO,OAAO,QAAQ;EAAI;EAG3C,MAAM,IAAI,oBACR,qDACF;CACF;CAEA,IAAI,YAAY,QAAQ;EACtB,IAAI,CAAC,OAAO,UAAU,CAAC,OAAO,WAC5B,MAAM,IAAI,oBACR,0DACF;EAGF,OAAO;GACL,MAAM;GACN,OAAO,OAAO;GACd,WAAW,OAAO;EACpB;CACF;CAEA,MAAM,IAAI,oBACR,wGACF;AACF"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
//#region ../../@warlock.js/ai/src/utils/safe-json-parse.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Parse a JSON string, returning a caller-supplied fallback when the input
|
|
4
|
+
* is empty or malformed instead of throwing. Useful at provider boundaries
|
|
5
|
+
* where tool-call arguments may arrive as `null`, `""`, or partial JSON
|
|
6
|
+
* during streaming — callers want a safe default, not an exception.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const args = safeJsonParse<Record<string, unknown>>(toolCall.function.arguments, {});
|
|
10
|
+
*/
|
|
11
|
+
declare function safeJsonParse<TValue>(data: string | null | undefined, defaultValue: TValue): TValue;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { safeJsonParse };
|
|
14
|
+
//# sourceMappingURL=safe-json-parse.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"safe-json-parse.d.mts","names":[],"sources":["../../../../../../@warlock.js/ai/src/utils/safe-json-parse.ts"],"mappings":";;AASA;;;;;;;;iBAAgB,aAAA,QAAA,CACd,IAAA,6BACA,YAAA,EAAc,MAAA,GACb,MAAM"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
//#region ../../@warlock.js/ai/src/utils/safe-json-parse.ts
|
|
2
|
+
/**
|
|
3
|
+
* Parse a JSON string, returning a caller-supplied fallback when the input
|
|
4
|
+
* is empty or malformed instead of throwing. Useful at provider boundaries
|
|
5
|
+
* where tool-call arguments may arrive as `null`, `""`, or partial JSON
|
|
6
|
+
* during streaming — callers want a safe default, not an exception.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const args = safeJsonParse<Record<string, unknown>>(toolCall.function.arguments, {});
|
|
10
|
+
*/
|
|
11
|
+
function safeJsonParse(data, defaultValue) {
|
|
12
|
+
if (!data) return defaultValue;
|
|
13
|
+
try {
|
|
14
|
+
return JSON.parse(data);
|
|
15
|
+
} catch {
|
|
16
|
+
return defaultValue;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
//#endregion
|
|
21
|
+
export { safeJsonParse };
|
|
22
|
+
//# sourceMappingURL=safe-json-parse.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"safe-json-parse.mjs","names":[],"sources":["../../../../../../@warlock.js/ai/src/utils/safe-json-parse.ts"],"sourcesContent":["/**\n * Parse a JSON string, returning a caller-supplied fallback when the input\n * is empty or malformed instead of throwing. Useful at provider boundaries\n * where tool-call arguments may arrive as `null`, `\"\"`, or partial JSON\n * during streaming — callers want a safe default, not an exception.\n *\n * @example\n * const args = safeJsonParse<Record<string, unknown>>(toolCall.function.arguments, {});\n */\nexport function safeJsonParse<TValue>(\n data: string | null | undefined,\n defaultValue: TValue,\n): TValue {\n if (!data) {\n return defaultValue;\n }\n\n try {\n return JSON.parse(data) as TValue;\n } catch {\n return defaultValue;\n }\n}\n"],"mappings":";;;;;;;;;;AASA,SAAgB,cACd,MACA,cACQ;CACR,IAAI,CAAC,MACH,OAAO;CAGT,IAAI;EACF,OAAO,KAAK,MAAM,IAAI;CACxB,QAAQ;EACN,OAAO;CACT;AACF"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { BaseReport } from "../contracts/result/base-report.type.mjs";
|
|
2
|
+
|
|
3
|
+
//#region ../../@warlock.js/ai/src/utils/stamp-report-lineage.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Options for {@link stampReportLineage}. Every field is optional —
|
|
6
|
+
* the caller supplies whichever pieces it knows. Missing pieces are
|
|
7
|
+
* left untouched (existing values on the report survive).
|
|
8
|
+
*/
|
|
9
|
+
type LineageStamp = {
|
|
10
|
+
/**
|
|
11
|
+
* The outermost run-id this subtree belongs to. When set, EVERY
|
|
12
|
+
* node in the subtree gets its `rootRunId` rewritten to this
|
|
13
|
+
* value — overrides any inner self-roots produced by nested
|
|
14
|
+
* `buildResult` calls.
|
|
15
|
+
*/
|
|
16
|
+
rootRunId: string;
|
|
17
|
+
/**
|
|
18
|
+
* Run-id of the immediate parent of THIS subtree's top node. Each
|
|
19
|
+
* descendant's `parentRunId` is then derived from its own walk
|
|
20
|
+
* position (its parent's `runId`).
|
|
21
|
+
*/
|
|
22
|
+
parentRunId?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Caller-supplied session identifier — propagates to every node in
|
|
25
|
+
* the subtree. Skipped when undefined.
|
|
26
|
+
*/
|
|
27
|
+
sessionId?: string;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Walk a freshly-built report tree and stamp lineage fields onto
|
|
31
|
+
* every node:
|
|
32
|
+
*
|
|
33
|
+
* - `rootRunId` — rewritten to `stamp.rootRunId` everywhere. Composite
|
|
34
|
+
* children built by inner primitives carry their own self-root; this
|
|
35
|
+
* walk overrides it so the outer root wins (single coherent run id
|
|
36
|
+
* across the whole tree).
|
|
37
|
+
* - `parentRunId` — root node gets `stamp.parentRunId`; descendants
|
|
38
|
+
* derive theirs from each parent's own `runId`.
|
|
39
|
+
* - `sessionId` — propagated when provided.
|
|
40
|
+
* - `reportSchemaVersion` — stamped only on the root (the value is the
|
|
41
|
+
* same for the whole tree; storing it on every node would waste
|
|
42
|
+
* space).
|
|
43
|
+
*
|
|
44
|
+
* Designed to run ONCE per top-level `buildResult` call. Each
|
|
45
|
+
* primitive's executor invokes this on the assembled root report just
|
|
46
|
+
* before returning; nested primitives produced their own subtree with
|
|
47
|
+
* a self-root, and this pass relinks everything to the outer caller's
|
|
48
|
+
* lineage.
|
|
49
|
+
*
|
|
50
|
+
* Mutates the report in place — internal use only, before the report
|
|
51
|
+
* is exposed via `result.report`.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* const root = this.buildBareReport();
|
|
55
|
+
* stampReportLineage(root, { rootRunId: this.runId, sessionId: this.options?.sessionId });
|
|
56
|
+
* return { ..., report: root };
|
|
57
|
+
*/
|
|
58
|
+
declare function stampReportLineage(root: BaseReport, stamp: LineageStamp): void;
|
|
59
|
+
//#endregion
|
|
60
|
+
export { LineageStamp, stampReportLineage };
|
|
61
|
+
//# sourceMappingURL=stamp-report-lineage.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stamp-report-lineage.d.mts","names":[],"sources":["../../../../../../@warlock.js/ai/src/utils/stamp-report-lineage.ts"],"mappings":";;;;;AAQA;;;KAAY,YAAA;EAOV;;;;AAWS;AAgCX;EA3CE,SAAA;;;;;;EAMA,WAAA;EAqCsE;AAAA;;;EAhCtE,SAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAgCc,kBAAA,CAAmB,IAAA,EAAM,UAAA,EAAY,KAAA,EAAO,YAAY"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { REPORT_SCHEMA_VERSION } from "../contracts/result/base-report.type.mjs";
|
|
2
|
+
|
|
3
|
+
//#region ../../@warlock.js/ai/src/utils/stamp-report-lineage.ts
|
|
4
|
+
/**
|
|
5
|
+
* Walk a freshly-built report tree and stamp lineage fields onto
|
|
6
|
+
* every node:
|
|
7
|
+
*
|
|
8
|
+
* - `rootRunId` — rewritten to `stamp.rootRunId` everywhere. Composite
|
|
9
|
+
* children built by inner primitives carry their own self-root; this
|
|
10
|
+
* walk overrides it so the outer root wins (single coherent run id
|
|
11
|
+
* across the whole tree).
|
|
12
|
+
* - `parentRunId` — root node gets `stamp.parentRunId`; descendants
|
|
13
|
+
* derive theirs from each parent's own `runId`.
|
|
14
|
+
* - `sessionId` — propagated when provided.
|
|
15
|
+
* - `reportSchemaVersion` — stamped only on the root (the value is the
|
|
16
|
+
* same for the whole tree; storing it on every node would waste
|
|
17
|
+
* space).
|
|
18
|
+
*
|
|
19
|
+
* Designed to run ONCE per top-level `buildResult` call. Each
|
|
20
|
+
* primitive's executor invokes this on the assembled root report just
|
|
21
|
+
* before returning; nested primitives produced their own subtree with
|
|
22
|
+
* a self-root, and this pass relinks everything to the outer caller's
|
|
23
|
+
* lineage.
|
|
24
|
+
*
|
|
25
|
+
* Mutates the report in place — internal use only, before the report
|
|
26
|
+
* is exposed via `result.report`.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* const root = this.buildBareReport();
|
|
30
|
+
* stampReportLineage(root, { rootRunId: this.runId, sessionId: this.options?.sessionId });
|
|
31
|
+
* return { ..., report: root };
|
|
32
|
+
*/
|
|
33
|
+
function stampReportLineage(root, stamp) {
|
|
34
|
+
root.reportSchemaVersion = 1;
|
|
35
|
+
walk(root, stamp.rootRunId, stamp.parentRunId, stamp.sessionId);
|
|
36
|
+
}
|
|
37
|
+
function walk(node, rootRunId, parentRunId, sessionId) {
|
|
38
|
+
node.rootRunId = rootRunId;
|
|
39
|
+
if (parentRunId !== void 0) node.parentRunId = parentRunId;
|
|
40
|
+
else delete node.parentRunId;
|
|
41
|
+
if (sessionId !== void 0) node.sessionId = sessionId;
|
|
42
|
+
for (const child of node.children) walk(child, rootRunId, node.runId, sessionId);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
//#endregion
|
|
46
|
+
export { stampReportLineage };
|
|
47
|
+
//# sourceMappingURL=stamp-report-lineage.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stamp-report-lineage.mjs","names":[],"sources":["../../../../../../@warlock.js/ai/src/utils/stamp-report-lineage.ts"],"sourcesContent":["import type { BaseReport } from \"../contracts/result/base-report.type\";\nimport { REPORT_SCHEMA_VERSION } from \"../contracts/result/base-report.type\";\n\n/**\n * Options for {@link stampReportLineage}. Every field is optional —\n * the caller supplies whichever pieces it knows. Missing pieces are\n * left untouched (existing values on the report survive).\n */\nexport type LineageStamp = {\n /**\n * The outermost run-id this subtree belongs to. When set, EVERY\n * node in the subtree gets its `rootRunId` rewritten to this\n * value — overrides any inner self-roots produced by nested\n * `buildResult` calls.\n */\n rootRunId: string;\n /**\n * Run-id of the immediate parent of THIS subtree's top node. Each\n * descendant's `parentRunId` is then derived from its own walk\n * position (its parent's `runId`).\n */\n parentRunId?: string;\n /**\n * Caller-supplied session identifier — propagates to every node in\n * the subtree. Skipped when undefined.\n */\n sessionId?: string;\n};\n\n/**\n * Walk a freshly-built report tree and stamp lineage fields onto\n * every node:\n *\n * - `rootRunId` — rewritten to `stamp.rootRunId` everywhere. Composite\n * children built by inner primitives carry their own self-root; this\n * walk overrides it so the outer root wins (single coherent run id\n * across the whole tree).\n * - `parentRunId` — root node gets `stamp.parentRunId`; descendants\n * derive theirs from each parent's own `runId`.\n * - `sessionId` — propagated when provided.\n * - `reportSchemaVersion` — stamped only on the root (the value is the\n * same for the whole tree; storing it on every node would waste\n * space).\n *\n * Designed to run ONCE per top-level `buildResult` call. Each\n * primitive's executor invokes this on the assembled root report just\n * before returning; nested primitives produced their own subtree with\n * a self-root, and this pass relinks everything to the outer caller's\n * lineage.\n *\n * Mutates the report in place — internal use only, before the report\n * is exposed via `result.report`.\n *\n * @example\n * const root = this.buildBareReport();\n * stampReportLineage(root, { rootRunId: this.runId, sessionId: this.options?.sessionId });\n * return { ..., report: root };\n */\nexport function stampReportLineage(root: BaseReport, stamp: LineageStamp): void {\n root.reportSchemaVersion = REPORT_SCHEMA_VERSION;\n\n walk(root, stamp.rootRunId, stamp.parentRunId, stamp.sessionId);\n}\n\nfunction walk(node: BaseReport, rootRunId: string, parentRunId?: string, sessionId?: string): void {\n node.rootRunId = rootRunId;\n\n if (parentRunId !== undefined) {\n node.parentRunId = parentRunId;\n } else {\n delete node.parentRunId;\n }\n\n if (sessionId !== undefined) {\n node.sessionId = sessionId;\n }\n\n for (const child of node.children) {\n walk(child, rootRunId, node.runId, sessionId);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0DA,SAAgB,mBAAmB,MAAkB,OAA2B;CAC9E,KAAK;CAEL,KAAK,MAAM,MAAM,WAAW,MAAM,aAAa,MAAM,SAAS;AAChE;AAEA,SAAS,KAAK,MAAkB,WAAmB,aAAsB,WAA0B;CACjG,KAAK,YAAY;CAEjB,IAAI,gBAAgB,QAClB,KAAK,cAAc;MAEnB,OAAO,KAAK;CAGd,IAAI,cAAc,QAChB,KAAK,YAAY;CAGnB,KAAK,MAAM,SAAS,KAAK,UACvB,KAAK,OAAO,WAAW,KAAK,OAAO,SAAS;AAEhD"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//#region ../../@warlock.js/ai/src/utils/token-count.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Approximate the number of tokens in a string.
|
|
4
|
+
*
|
|
5
|
+
* Uses the ~4-characters-per-token heuristic, which is accurate enough for
|
|
6
|
+
* GPT-4 family models and most English text. Use this when a real tokenizer
|
|
7
|
+
* (tiktoken, etc.) isn't available or would add native dependencies.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const tokens = approximateTokenCount("Hello, world!"); // 4
|
|
11
|
+
*/
|
|
12
|
+
declare function approximateTokenCount(text: string): number;
|
|
13
|
+
//#endregion
|
|
14
|
+
export { approximateTokenCount };
|
|
15
|
+
//# sourceMappingURL=token-count.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-count.d.mts","names":[],"sources":["../../../../../../@warlock.js/ai/src/utils/token-count.ts"],"mappings":";;AAUA;;;;AAAkD;;;;;iBAAlC,qBAAA,CAAsB,IAAY"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region ../../@warlock.js/ai/src/utils/token-count.ts
|
|
2
|
+
/**
|
|
3
|
+
* Approximate the number of tokens in a string.
|
|
4
|
+
*
|
|
5
|
+
* Uses the ~4-characters-per-token heuristic, which is accurate enough for
|
|
6
|
+
* GPT-4 family models and most English text. Use this when a real tokenizer
|
|
7
|
+
* (tiktoken, etc.) isn't available or would add native dependencies.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const tokens = approximateTokenCount("Hello, world!"); // 4
|
|
11
|
+
*/
|
|
12
|
+
function approximateTokenCount(text) {
|
|
13
|
+
return Math.ceil(text.length / 4);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
//#endregion
|
|
17
|
+
export { approximateTokenCount };
|
|
18
|
+
//# sourceMappingURL=token-count.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-count.mjs","names":[],"sources":["../../../../../../@warlock.js/ai/src/utils/token-count.ts"],"sourcesContent":["/**\n * Approximate the number of tokens in a string.\n *\n * Uses the ~4-characters-per-token heuristic, which is accurate enough for\n * GPT-4 family models and most English text. Use this when a real tokenizer\n * (tiktoken, etc.) isn't available or would add native dependencies.\n *\n * @example\n * const tokens = approximateTokenCount(\"Hello, world!\"); // 4\n */\nexport function approximateTokenCount(text: string): number {\n return Math.ceil(text.length / 4);\n}\n"],"mappings":";;;;;;;;;;;AAUA,SAAgB,sBAAsB,MAAsB;CAC1D,OAAO,KAAK,KAAK,KAAK,SAAS,CAAC;AAClC"}
|