@infinityi/engine-lib 1.3.1 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +81 -445
- package/dist/agent/index.js +14 -8
- package/dist/context/index.d.ts +1 -1
- package/dist/context/index.js +8 -5
- package/dist/context/providers.d.ts +3 -3
- package/dist/context/types.d.ts +16 -1
- package/dist/errors.d.ts +18 -0
- package/dist/errors.js +6 -1
- package/dist/events/index.js +2 -1
- package/dist/execution/index.js +9 -4
- package/dist/execution/types.d.ts +14 -0
- package/dist/index-37x76zdn.js +4 -0
- package/dist/index-4c15ysa8.js +84 -0
- package/dist/{index-pwr8179t.js → index-7nb9qwa1.js} +23 -14
- package/dist/{index-jxgj4z08.js → index-9egrqxkf.js} +4 -0
- package/dist/index-9rr5dkzh.js +0 -0
- package/dist/{index-w34cbktd.js → index-a67ej96j.js} +1 -0
- package/dist/{index-64tt9696.js → index-aed6fe4z.js} +3 -3
- package/dist/{index-7690reng.js → index-ajr3nk10.js} +15 -1
- package/dist/index-c6kgts41.js +0 -0
- package/dist/{index-xsv43c5j.js → index-e14g2e48.js} +2 -2
- package/dist/index-k0q7x4mz.js +54 -0
- package/dist/index-mnx5mqbs.js +102 -0
- package/dist/index-mr1hs98z.js +661 -0
- package/dist/{index-yrqrxwjt.js → index-nvdsr51v.js} +4 -38
- package/dist/{index-jp2b31xs.js → index-vwcyawfk.js} +4 -4
- package/dist/index-ycjsqfpq.js +39 -0
- package/dist/{index-bqg01r42.js → index-zdggff0y.js} +183 -180
- package/dist/index.d.ts +2 -2
- package/dist/index.js +49 -38
- package/dist/lifecycle/index.js +2 -0
- package/dist/messages/index.js +1 -0
- package/dist/providers/index.js +5 -4
- package/dist/retrieval/chunking.d.ts +8 -0
- package/dist/retrieval/context.d.ts +4 -0
- package/dist/retrieval/index.d.ts +19 -0
- package/dist/retrieval/index.js +747 -0
- package/dist/retrieval/loaders.d.ts +8 -0
- package/dist/retrieval/memory.d.ts +29 -0
- package/dist/retrieval/retriever.d.ts +8 -0
- package/dist/retrieval/types.d.ts +253 -0
- package/dist/retrieval/utils.d.ts +11 -0
- package/dist/retrieval/vector-store.d.ts +13 -0
- package/dist/runtime/index.js +2 -1
- package/dist/schema/index.js +10 -4
- package/dist/session/index.js +1 -0
- package/dist/session-stores/codec.d.ts +8 -0
- package/dist/session-stores/forge-data.d.ts +43 -0
- package/dist/session-stores/hooks.d.ts +4 -0
- package/dist/session-stores/ids.d.ts +3 -0
- package/dist/session-stores/index.d.ts +20 -0
- package/dist/session-stores/index.js +1382 -0
- package/dist/session-stores/jsonl.d.ts +26 -0
- package/dist/session-stores/redis.d.ts +45 -0
- package/dist/session-stores/types.d.ts +45 -0
- package/dist/session-stores/versioning.d.ts +6 -0
- package/dist/testing/conformance.js +5 -4
- package/dist/testing/index.js +1 -0
- package/dist/tools/index.js +5 -3
- package/dist/tools-fs/define.d.ts +2 -0
- package/dist/tools-fs/files.d.ts +40 -0
- package/dist/tools-fs/git.d.ts +14 -0
- package/dist/tools-fs/index.d.ts +19 -0
- package/dist/tools-fs/index.js +1695 -0
- package/dist/tools-fs/policy.d.ts +32 -0
- package/dist/tools-fs/schemas.d.ts +31 -0
- package/dist/tools-fs/search.d.ts +45 -0
- package/dist/tools-fs/symbols.d.ts +12 -0
- package/dist/tools-fs/types.d.ts +151 -0
- package/dist/tools-http/client.d.ts +12 -0
- package/dist/tools-http/define.d.ts +12 -0
- package/dist/tools-http/events.d.ts +22 -0
- package/dist/tools-http/index.d.ts +24 -0
- package/dist/tools-http/index.js +18 -0
- package/dist/tools-http/policy.d.ts +52 -0
- package/dist/tools-http/types.d.ts +116 -0
- package/dist/tools-shell/define.d.ts +23 -0
- package/dist/tools-shell/events.d.ts +38 -0
- package/dist/tools-shell/exec.d.ts +37 -0
- package/dist/tools-shell/index.d.ts +30 -0
- package/dist/tools-shell/index.js +327 -0
- package/dist/tools-shell/policy.d.ts +42 -0
- package/dist/tools-shell/types.d.ts +121 -0
- package/dist/tools-web/define.d.ts +11 -0
- package/dist/tools-web/html.d.ts +22 -0
- package/dist/tools-web/index.d.ts +21 -0
- package/dist/tools-web/index.js +472 -0
- package/dist/tools-web/types.d.ts +81 -0
- package/docs/README.md +58 -10
- package/docs/api/.nojekyll +1 -0
- package/docs/api/assets/hierarchy.js +1 -0
- package/docs/api/assets/highlight.css +99 -0
- package/docs/api/assets/icons.js +18 -0
- package/docs/api/assets/icons.svg +1 -0
- package/docs/api/assets/main.js +60 -0
- package/docs/api/assets/navigation.js +1 -0
- package/docs/api/assets/search.js +1 -0
- package/docs/api/assets/style.css +1633 -0
- package/docs/api/classes/errors.AgentError.html +47 -0
- package/docs/api/classes/errors.CancelledError.html +46 -0
- package/docs/api/classes/errors.ContextWindowError.html +48 -0
- package/docs/api/classes/errors.ExecutionError.html +46 -0
- package/docs/api/classes/errors.FilesystemPolicyError.html +48 -0
- package/docs/api/classes/errors.MaxHandoffsExceededError.html +47 -0
- package/docs/api/classes/errors.MaxStepsExceededError.html +47 -0
- package/docs/api/classes/errors.ProviderError.html +47 -0
- package/docs/api/classes/errors.SchemaValidationError.html +47 -0
- package/docs/api/classes/errors.ShellPolicyError.html +50 -0
- package/docs/api/classes/errors.ToolError.html +47 -0
- package/docs/api/classes/errors.ToolValidationError.html +48 -0
- package/docs/api/classes/index.InMemorySessionStore.html +11 -0
- package/docs/api/classes/index.Secret.html +20 -0
- package/docs/api/classes/providers.StreamAccumulator.html +8 -0
- package/docs/api/classes/session-stores.FilesystemJsonlSessionStore.html +13 -0
- package/docs/api/classes/session-stores.ForgeDataSessionStore.html +14 -0
- package/docs/api/classes/session-stores.RedisSessionStore.html +12 -0
- package/docs/api/classes/tools-http.HttpPolicyError.html +41 -0
- package/docs/api/functions/agent.createToolRegistry.html +3 -0
- package/docs/api/functions/agent.handoffProviderTools.html +3 -0
- package/docs/api/functions/agent.handoffToolName.html +2 -0
- package/docs/api/functions/agent.resolveHandoffTargets.html +8 -0
- package/docs/api/functions/context.applyContextWindow.html +5 -0
- package/docs/api/functions/context.estimateTokens.html +3 -0
- package/docs/api/functions/context.resolveContext.html +5 -0
- package/docs/api/functions/events.createRunTelemetry.html +3 -0
- package/docs/api/functions/events.eventFields.html +4 -0
- package/docs/api/functions/events.eventPayload.html +5 -0
- package/docs/api/functions/execution.addUsage.html +2 -0
- package/docs/api/functions/execution.emptyUsage.html +2 -0
- package/docs/api/functions/index.asSchema.html +4 -0
- package/docs/api/functions/index.asTool.html +8 -0
- package/docs/api/functions/index.assistant.html +2 -0
- package/docs/api/functions/index.createAgentRegistry.html +5 -0
- package/docs/api/functions/index.createAnthropic.html +5 -0
- package/docs/api/functions/index.createEventHub.html +2 -0
- package/docs/api/functions/index.createGoogle.html +5 -0
- package/docs/api/functions/index.createOpenAI.html +5 -0
- package/docs/api/functions/index.createOpenAICompatible.html +7 -0
- package/docs/api/functions/index.createSession.html +3 -0
- package/docs/api/functions/index.defineAgent.html +7 -0
- package/docs/api/functions/index.defineTool.html +7 -0
- package/docs/api/functions/index.dynamicContext.html +2 -0
- package/docs/api/functions/index.fromJsonSchema.html +3 -0
- package/docs/api/functions/index.isSecret.html +4 -0
- package/docs/api/functions/index.loggingSubscriber.html +4 -0
- package/docs/api/functions/index.messageBusSubscriber.html +6 -0
- package/docs/api/functions/index.normalizeContent.html +2 -0
- package/docs/api/functions/index.resolveSecret.html +4 -0
- package/docs/api/functions/index.runAgent.html +19 -0
- package/docs/api/functions/index.staticContext.html +3 -0
- package/docs/api/functions/index.summarizeOldest.html +4 -0
- package/docs/api/functions/index.system.html +2 -0
- package/docs/api/functions/index.text.html +2 -0
- package/docs/api/functions/index.toJsonSchema.html +2 -0
- package/docs/api/functions/index.toolResult.html +2 -0
- package/docs/api/functions/index.truncateOldest.html +5 -0
- package/docs/api/functions/index.user.html +2 -0
- package/docs/api/functions/index.validateJsonSchema.html +3 -0
- package/docs/api/functions/lifecycle.agentRuntimeComponent.html +5 -0
- package/docs/api/functions/providers.collectStream.html +2 -0
- package/docs/api/functions/providers.createProvider.html +4 -0
- package/docs/api/functions/providers.createProviderHttp.html +3 -0
- package/docs/api/functions/providers.defaultProviderResilience.html +3 -0
- package/docs/api/functions/providers.openSseStream.html +6 -0
- package/docs/api/functions/providers.parseSse.html +2 -0
- package/docs/api/functions/providers.toProviderError.html +2 -0
- package/docs/api/functions/session-stores.createPostgresSessionStore.html +1 -0
- package/docs/api/functions/session-stores.createSqliteSessionStore.html +1 -0
- package/docs/api/functions/session-stores.isCloseableSessionStore.html +1 -0
- package/docs/api/functions/session-stores.isVersionedSessionStore.html +1 -0
- package/docs/api/functions/session-stores.migrateSessionStore.html +1 -0
- package/docs/api/functions/session-stores.withSessionStoreHooks.html +2 -0
- package/docs/api/functions/testing.byteStreamOf.html +2 -0
- package/docs/api/functions/testing.collectProviderStream.html +2 -0
- package/docs/api/functions/testing.conversation.html +2 -0
- package/docs/api/functions/testing.expectValid.html +3 -0
- package/docs/api/functions/testing.inMemorySessionStore.html +2 -0
- package/docs/api/functions/testing.jsonFetch.html +3 -0
- package/docs/api/functions/testing.mockProvider.html +3 -0
- package/docs/api/functions/testing.scriptedProvider.html +4 -0
- package/docs/api/functions/testing.sseFetch.html +3 -0
- package/docs/api/functions/testing.textResult.html +3 -0
- package/docs/api/functions/testing.toolCallResult.html +3 -0
- package/docs/api/functions/testing_conformance.runProviderConformance.html +3 -0
- package/docs/api/functions/tools-fs.filesystemTools.html +1 -0
- package/docs/api/functions/tools-http.createHttpToolClient.html +2 -0
- package/docs/api/functions/tools-http.httpTools.html +2 -0
- package/docs/api/functions/tools-shell.shellTools.html +4 -0
- package/docs/api/functions/tools-web.webTools.html +2 -0
- package/docs/api/functions/tools.renderToolContent.html +6 -0
- package/docs/api/functions/tools.toProviderTool.html +3 -0
- package/docs/api/functions/tools.toToolResultMessage.html +4 -0
- package/docs/api/hierarchy.html +1 -0
- package/docs/api/index.html +1 -0
- package/docs/api/interfaces/agent.ToolRegistry.html +8 -0
- package/docs/api/interfaces/errors.SchemaIssue.html +4 -0
- package/docs/api/interfaces/events.RunTelemetry.html +10 -0
- package/docs/api/interfaces/events.SpanHandle.html +6 -0
- package/docs/api/interfaces/index.AgentDefinition.html +16 -0
- package/docs/api/interfaces/index.AgentHooks.html +21 -0
- package/docs/api/interfaces/index.AgentRegistry.html +16 -0
- package/docs/api/interfaces/index.AnthropicOptions.html +13 -0
- package/docs/api/interfaces/index.AsToolOptions.html +13 -0
- package/docs/api/interfaces/index.CompletionRequest.html +30 -0
- package/docs/api/interfaces/index.CompletionResult.html +14 -0
- package/docs/api/interfaces/index.ContextItem.html +6 -0
- package/docs/api/interfaces/index.ContextProvider.html +5 -0
- package/docs/api/interfaces/index.ContextStrategy.html +8 -0
- package/docs/api/interfaces/index.ContextStrategyContext.html +8 -0
- package/docs/api/interfaces/index.ContextWindowOptions.html +8 -0
- package/docs/api/interfaces/index.CreateSessionOptions.html +10 -0
- package/docs/api/interfaces/index.EngineContext.html +10 -0
- package/docs/api/interfaces/index.EventHub.html +4 -0
- package/docs/api/interfaces/index.EventHubOptions.html +8 -0
- package/docs/api/interfaces/index.GenerationSettings.html +12 -0
- package/docs/api/interfaces/index.GoogleOptions.html +11 -0
- package/docs/api/interfaces/index.ImagePart.html +5 -0
- package/docs/api/interfaces/index.InstructionContext.html +9 -0
- package/docs/api/interfaces/index.JsonSchema.html +18 -0
- package/docs/api/interfaces/index.LoggingSubscriberOptions.html +4 -0
- package/docs/api/interfaces/index.Message.html +8 -0
- package/docs/api/interfaces/index.MessageBusSubscriberOptions.html +4 -0
- package/docs/api/interfaces/index.OpenAICompatibleOptions.html +16 -0
- package/docs/api/interfaces/index.OpenAIOptions.html +16 -0
- package/docs/api/interfaces/index.Provider.html +11 -0
- package/docs/api/interfaces/index.ProviderCapabilities.html +9 -0
- package/docs/api/interfaces/index.ProviderTool.html +6 -0
- package/docs/api/interfaces/index.ResponseSchema.html +6 -0
- package/docs/api/interfaces/index.RunBridge.html +10 -0
- package/docs/api/interfaces/index.RunOptions.html +39 -0
- package/docs/api/interfaces/index.RunResult.html +20 -0
- package/docs/api/interfaces/index.Schema.html +13 -0
- package/docs/api/interfaces/index.Session.html +13 -0
- package/docs/api/interfaces/index.SessionState.html +5 -0
- package/docs/api/interfaces/index.SessionStore.html +15 -0
- package/docs/api/interfaces/index.TextPart.html +4 -0
- package/docs/api/interfaces/index.ToolCall.html +9 -0
- package/docs/api/interfaces/index.ToolCallPart.html +6 -0
- package/docs/api/interfaces/index.ToolContext.html +21 -0
- package/docs/api/interfaces/index.ToolDefinition.html +14 -0
- package/docs/api/interfaces/index.ToolFailure.html +9 -0
- package/docs/api/interfaces/index.ToolResultPart.html +8 -0
- package/docs/api/interfaces/index.ToolSpec.html +6 -0
- package/docs/api/interfaces/index.ToolSuccess.html +6 -0
- package/docs/api/interfaces/index.Usage.html +9 -0
- package/docs/api/interfaces/lifecycle.AgentRuntimeOptions.html +17 -0
- package/docs/api/interfaces/providers.AdapterSpec.html +17 -0
- package/docs/api/interfaces/providers.ProviderHttpOptions.html +14 -0
- package/docs/api/interfaces/providers.SseMessage.html +6 -0
- package/docs/api/interfaces/providers.SseRequest.html +8 -0
- package/docs/api/interfaces/runtime.Logger.html +22 -0
- package/docs/api/interfaces/runtime.Telemetry.html +9 -0
- package/docs/api/interfaces/schema.OptionalSchema.html +13 -0
- package/docs/api/interfaces/session-stores.CloseableSessionStore.html +11 -0
- package/docs/api/interfaces/session-stores.CreatePostgresSessionStoreOptions.html +6 -0
- package/docs/api/interfaces/session-stores.CreateSqliteSessionStoreOptions.html +7 -0
- package/docs/api/interfaces/session-stores.FilesystemJsonlSessionStoreOptions.html +3 -0
- package/docs/api/interfaces/session-stores.ForgeDataSessionStoreOptions.html +5 -0
- package/docs/api/interfaces/session-stores.RedisSessionStoreClient.html +9 -0
- package/docs/api/interfaces/session-stores.RedisSessionStoreOptions.html +4 -0
- package/docs/api/interfaces/session-stores.RedisSessionStoreTransaction.html +6 -0
- package/docs/api/interfaces/session-stores.SessionArchiveRecord.html +7 -0
- package/docs/api/interfaces/session-stores.SessionArchiver.html +2 -0
- package/docs/api/interfaces/session-stores.SessionCompactionResult.html +3 -0
- package/docs/api/interfaces/session-stores.SessionCompactor.html +3 -0
- package/docs/api/interfaces/session-stores.SessionStoreCodec.html +6 -0
- package/docs/api/interfaces/session-stores.SessionStoreHookContext.html +3 -0
- package/docs/api/interfaces/session-stores.SessionStoreHooks.html +3 -0
- package/docs/api/interfaces/session-stores.VersionedSessionStore.html +11 -0
- package/docs/api/interfaces/testing.MockProviderOptions.html +12 -0
- package/docs/api/interfaces/testing.RecordedCall.html +4 -0
- package/docs/api/interfaces/testing.RecordingFetch.html +4 -0
- package/docs/api/interfaces/testing_conformance.ConformanceFixtures.html +12 -0
- package/docs/api/interfaces/testing_conformance.ConformanceOptions.html +10 -0
- package/docs/api/interfaces/testing_conformance.ConformanceTestApi.html +4 -0
- package/docs/api/interfaces/testing_conformance.ProviderIO.html +7 -0
- package/docs/api/interfaces/tools-fs.ApplyPatchArgs.html +6 -0
- package/docs/api/interfaces/tools-fs.DiffStatusArgs.html +5 -0
- package/docs/api/interfaces/tools-fs.EditRangeArgs.html +7 -0
- package/docs/api/interfaces/tools-fs.EditReplaceArgs.html +7 -0
- package/docs/api/interfaces/tools-fs.FilesystemTools.html +13 -0
- package/docs/api/interfaces/tools-fs.FilesystemToolsConfig.html +12 -0
- package/docs/api/interfaces/tools-fs.FindFilesArgs.html +7 -0
- package/docs/api/interfaces/tools-fs.OpenWindowArgs.html +5 -0
- package/docs/api/interfaces/tools-fs.ReadArgs.html +9 -0
- package/docs/api/interfaces/tools-fs.RepoMapArgs.html +7 -0
- package/docs/api/interfaces/tools-fs.SearchSemanticArgs.html +8 -0
- package/docs/api/interfaces/tools-fs.SearchTextArgs.html +11 -0
- package/docs/api/interfaces/tools-fs.SymbolInfo.html +7 -0
- package/docs/api/interfaces/tools-fs.SymbolsArgs.html +5 -0
- package/docs/api/interfaces/tools-fs.ValidationCommandRequest.html +4 -0
- package/docs/api/interfaces/tools-fs.ValidationCommandResult.html +6 -0
- package/docs/api/interfaces/tools-fs.ValidationOptions.html +4 -0
- package/docs/api/interfaces/tools-fs.WriteFileArgs.html +6 -0
- package/docs/api/interfaces/tools-http.HeaderEntry.html +4 -0
- package/docs/api/interfaces/tools-http.HttpClientRequest.html +11 -0
- package/docs/api/interfaces/tools-http.HttpPolicy.html +13 -0
- package/docs/api/interfaces/tools-http.HttpRequestResult.html +17 -0
- package/docs/api/interfaces/tools-http.HttpRetryOptions.html +10 -0
- package/docs/api/interfaces/tools-http.HttpToolClient.html +5 -0
- package/docs/api/interfaces/tools-http.HttpTools.html +4 -0
- package/docs/api/interfaces/tools-http.HttpToolsConfig.html +38 -0
- package/docs/api/interfaces/tools-shell.ApprovalDecision.html +5 -0
- package/docs/api/interfaces/tools-shell.CommandRequest.html +14 -0
- package/docs/api/interfaces/tools-shell.CommandResult.html +21 -0
- package/docs/api/interfaces/tools-shell.EnvPolicy.html +9 -0
- package/docs/api/interfaces/tools-shell.ShellPolicy.html +7 -0
- package/docs/api/interfaces/tools-shell.ShellTools.html +6 -0
- package/docs/api/interfaces/tools-shell.ShellToolsConfig.html +26 -0
- package/docs/api/interfaces/tools-web.Citation.html +6 -0
- package/docs/api/interfaces/tools-web.SearchProvider.html +3 -0
- package/docs/api/interfaces/tools-web.SearchRequest.html +4 -0
- package/docs/api/interfaces/tools-web.SearchResult.html +7 -0
- package/docs/api/interfaces/tools-web.SourceMetadata.html +10 -0
- package/docs/api/interfaces/tools-web.WebTools.html +6 -0
- package/docs/api/interfaces/tools-web.WebToolsConfig.html +56 -0
- package/docs/api/modules/agent.html +5 -0
- package/docs/api/modules/context.html +5 -0
- package/docs/api/modules/errors.html +8 -0
- package/docs/api/modules/events.html +16 -0
- package/docs/api/modules/execution.html +4 -0
- package/docs/api/modules/index.html +16 -0
- package/docs/api/modules/lifecycle.html +6 -0
- package/docs/api/modules/messages.html +3 -0
- package/docs/api/modules/providers.html +10 -0
- package/docs/api/modules/runtime.html +4 -0
- package/docs/api/modules/schema.html +5 -0
- package/docs/api/modules/session-stores.html +6 -0
- package/docs/api/modules/session.html +4 -0
- package/docs/api/modules/testing.html +4 -0
- package/docs/api/modules/testing_conformance.html +21 -0
- package/docs/api/modules/tools-fs.html +9 -0
- package/docs/api/modules/tools-http.html +8 -0
- package/docs/api/modules/tools-shell.html +12 -0
- package/docs/api/modules/tools-web.html +7 -0
- package/docs/api/modules/tools.html +4 -0
- package/docs/api/types/events.Attrs.html +2 -0
- package/docs/api/types/index.AnyRunOptions.html +3 -0
- package/docs/api/types/index.BufferedRunOptions.html +2 -0
- package/docs/api/types/index.ContentPart.html +2 -0
- package/docs/api/types/index.FinishReason.html +2 -0
- package/docs/api/types/index.Infer.html +2 -0
- package/docs/api/types/index.Instructions.html +5 -0
- package/docs/api/types/index.LogLevel.html +2 -0
- package/docs/api/types/index.Role.html +2 -0
- package/docs/api/types/index.RunEvent.html +30 -0
- package/docs/api/types/index.RunHandle.html +7 -0
- package/docs/api/types/index.RunInput.html +2 -0
- package/docs/api/types/index.RunSubscriber.html +4 -0
- package/docs/api/types/index.SafeParseResult.html +2 -0
- package/docs/api/types/index.StreamEvent.html +8 -0
- package/docs/api/types/index.StreamingRunOptions.html +2 -0
- package/docs/api/types/index.TelemetryHandle.html +2 -0
- package/docs/api/types/index.TokenCounter.html +2 -0
- package/docs/api/types/index.ToolChoice.html +2 -0
- package/docs/api/types/index.ToolResult.html +2 -0
- package/docs/api/types/lifecycle.ProviderProbe.html +2 -0
- package/docs/api/types/session-stores.SessionStoreHookOperation.html +1 -0
- package/docs/api/types/testing_conformance.MakeProvider.html +2 -0
- package/docs/api/types/tools-fs.SymbolKind.html +1 -0
- package/docs/api/types/tools-http.HostPattern.html +2 -0
- package/docs/api/types/tools-shell.CommandPattern.html +2 -0
- package/docs/api/types/tools-web.RobotsPolicy.html +2 -0
- package/docs/api/variables/events.SPAN_PROVIDER.html +2 -0
- package/docs/api/variables/events.SPAN_RUN.html +2 -0
- package/docs/api/variables/events.SPAN_TOOL.html +2 -0
- package/docs/api/variables/execution.DEFAULT_MAX_HANDOFFS.html +2 -0
- package/docs/api/variables/execution.DEFAULT_MAX_STEPS.html +2 -0
- package/docs/api/variables/index.s.html +11 -0
- package/docs/api/variables/providers.DEFAULT_TIMEOUT_MS.html +2 -0
- package/docs/api/variables/session-stores.SESSION_STORE_SCHEMA_VERSION.html +1 -0
- package/docs/api/variables/session-stores.jsonSessionStoreCodec.html +2 -0
- package/docs/api/variables/tools-http.HTTP_EVENT.html +2 -0
- package/docs/api/variables/tools-shell.SHELL_EVENT.html +2 -0
- package/docs/events-and-telemetry.md +68 -0
- package/docs/execution.md +63 -0
- package/docs/getting-started.md +73 -0
- package/docs/multi-agent.md +52 -0
- package/docs/optional-tool-packs.md +93 -0
- package/docs/providers.md +68 -0
- package/docs/retrieval.md +83 -0
- package/docs/sessions-and-context.md +82 -0
- package/docs/testing-and-lifecycle.md +60 -0
- package/docs/tools.md +69 -0
- package/examples/01-minimal-agent.ts +19 -0
- package/examples/02-custom-tool.ts +32 -0
- package/examples/03-streaming.ts +46 -0
- package/examples/04-sessions-context.ts +40 -0
- package/examples/05-events-subscribers.ts +35 -0
- package/examples/06-multi-agent-handoff.ts +27 -0
- package/examples/07-sub-agent-tool.ts +26 -0
- package/examples/08-testing-agent.ts +31 -0
- package/examples/09-tools-filesystem.ts +49 -0
- package/examples/10-tools-http-web.ts +74 -0
- package/examples/11-shell-tools.ts +39 -0
- package/examples/12-provider-openai.ts +24 -0
- package/examples/13-lifecycle.ts +37 -0
- package/examples/14-retrieval.ts +65 -0
- package/examples/incident-analysis.ts +52 -0
- package/package.json +78 -4
- package/dist/index-02s1fjxr.js +0 -226
|
@@ -0,0 +1,661 @@
|
|
|
1
|
+
import {
|
|
2
|
+
fromJsonSchema
|
|
3
|
+
} from "./index-k0q7x4mz.js";
|
|
4
|
+
import {
|
|
5
|
+
combine,
|
|
6
|
+
exponentialBackoff,
|
|
7
|
+
retry,
|
|
8
|
+
timeout
|
|
9
|
+
} from "./index-zdggff0y.js";
|
|
10
|
+
import {
|
|
11
|
+
defineTool
|
|
12
|
+
} from "./index-a67ej96j.js";
|
|
13
|
+
|
|
14
|
+
// src/tools-http/events.ts
|
|
15
|
+
var HTTP_EVENT = {
|
|
16
|
+
policy: "http.policy",
|
|
17
|
+
requestStart: "http.request.start",
|
|
18
|
+
requestEnd: "http.request.end"
|
|
19
|
+
};
|
|
20
|
+
function emit(ctx, name, data) {
|
|
21
|
+
ctx?.run?.emit({ type: "custom", name, data });
|
|
22
|
+
}
|
|
23
|
+
function emitHttpPolicy(ctx, decision, url, reason) {
|
|
24
|
+
emit(ctx, HTTP_EVENT.policy, {
|
|
25
|
+
decision,
|
|
26
|
+
url,
|
|
27
|
+
...reason !== undefined ? { reason } : {}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
function emitHttpRequestStart(ctx, method, url, timeoutMs) {
|
|
31
|
+
emit(ctx, HTTP_EVENT.requestStart, { method, url, timeoutMs });
|
|
32
|
+
}
|
|
33
|
+
function emitHttpRequestEnd(ctx, method, url, result, error) {
|
|
34
|
+
emit(ctx, HTTP_EVENT.requestEnd, {
|
|
35
|
+
method,
|
|
36
|
+
url,
|
|
37
|
+
...result !== undefined ? {
|
|
38
|
+
finalUrl: result.finalUrl,
|
|
39
|
+
status: result.status,
|
|
40
|
+
elapsedMs: result.elapsedMs,
|
|
41
|
+
responseBytes: result.responseBytes,
|
|
42
|
+
responseBytesTruncated: result.responseBytesTruncated,
|
|
43
|
+
bodyTruncated: result.bodyTruncated
|
|
44
|
+
} : {},
|
|
45
|
+
...error !== undefined ? { error } : {}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// src/tools-http/policy.ts
|
|
50
|
+
var DEFAULT_PROTOCOLS = ["https:", "http:"];
|
|
51
|
+
var DEFAULT_RESPONSE_HEADERS = [
|
|
52
|
+
"content-type",
|
|
53
|
+
"content-length",
|
|
54
|
+
"cache-control",
|
|
55
|
+
"etag",
|
|
56
|
+
"last-modified",
|
|
57
|
+
"location",
|
|
58
|
+
"retry-after"
|
|
59
|
+
];
|
|
60
|
+
|
|
61
|
+
class HttpPolicyError extends Error {
|
|
62
|
+
constructor(message, options) {
|
|
63
|
+
super(message, options);
|
|
64
|
+
this.name = "HttpPolicyError";
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function assertPositiveInteger(name, value) {
|
|
68
|
+
if (!Number.isInteger(value) || value <= 0) {
|
|
69
|
+
throw new HttpPolicyError(`${name} must be a positive integer`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function assertNonNegativeInteger(name, value) {
|
|
73
|
+
if (!Number.isInteger(value) || value < 0) {
|
|
74
|
+
throw new HttpPolicyError(`${name} must be a non-negative integer`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function normalizeProtocol(protocol) {
|
|
78
|
+
const trimmed = protocol.trim().toLowerCase();
|
|
79
|
+
return trimmed.endsWith(":") ? trimmed : `${trimmed}:`;
|
|
80
|
+
}
|
|
81
|
+
function normalizeHeaderName(name) {
|
|
82
|
+
const trimmed = name.trim().toLowerCase();
|
|
83
|
+
if (trimmed === "")
|
|
84
|
+
throw new HttpPolicyError("header name must be non-empty");
|
|
85
|
+
if (!/^[!#$%&'*+\-.^_`|~0-9a-z]+$/.test(trimmed)) {
|
|
86
|
+
throw new HttpPolicyError(`invalid header name ${JSON.stringify(name)}`);
|
|
87
|
+
}
|
|
88
|
+
return trimmed;
|
|
89
|
+
}
|
|
90
|
+
function headerEntries(headers) {
|
|
91
|
+
if (headers === undefined)
|
|
92
|
+
return [];
|
|
93
|
+
const entries = Array.isArray(headers) ? headers : Object.entries(headers).map(([name, value]) => ({ name, value }));
|
|
94
|
+
return entries.map((entry) => {
|
|
95
|
+
const name = normalizeHeaderName(entry.name);
|
|
96
|
+
if (typeof entry.value !== "string") {
|
|
97
|
+
throw new HttpPolicyError(`header ${JSON.stringify(entry.name)} value must be a string`);
|
|
98
|
+
}
|
|
99
|
+
if (/[\r\n]/.test(entry.value)) {
|
|
100
|
+
throw new HttpPolicyError(`header ${JSON.stringify(entry.name)} value contains a newline`);
|
|
101
|
+
}
|
|
102
|
+
return { name, value: entry.value };
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
function normalizeHttpConfig(config) {
|
|
106
|
+
const allowedHosts = config.allowedHosts ?? [];
|
|
107
|
+
const allowPublicInternet = config.allowPublicInternet ?? false;
|
|
108
|
+
if (!allowPublicInternet && allowedHosts.length === 0) {
|
|
109
|
+
throw new HttpPolicyError("tools-http requires allowedHosts or allowPublicInternet: true");
|
|
110
|
+
}
|
|
111
|
+
const allowedProtocols = (config.allowedProtocols ?? DEFAULT_PROTOCOLS).map(normalizeProtocol);
|
|
112
|
+
if (allowedProtocols.length === 0) {
|
|
113
|
+
throw new HttpPolicyError("allowedProtocols must contain at least one protocol");
|
|
114
|
+
}
|
|
115
|
+
const minTimeoutMs = config.minTimeoutMs ?? 100;
|
|
116
|
+
const defaultTimeoutMs = config.defaultTimeoutMs ?? 1e4;
|
|
117
|
+
const maxTimeoutMs = config.maxTimeoutMs ?? 60000;
|
|
118
|
+
const maxResponseBytes = config.maxResponseBytes ?? 1e6;
|
|
119
|
+
const maxBodyChars = config.maxBodyChars ?? 20000;
|
|
120
|
+
const maxRedirects = config.maxRedirects ?? 5;
|
|
121
|
+
const retryMaxAttempts = config.retry?.maxAttempts ?? 3;
|
|
122
|
+
const retryInitialDelayMs = config.retry?.initialDelayMs ?? 100;
|
|
123
|
+
const retryMaxDelayMs = config.retry?.maxDelayMs ?? 2000;
|
|
124
|
+
assertPositiveInteger("minTimeoutMs", minTimeoutMs);
|
|
125
|
+
assertPositiveInteger("defaultTimeoutMs", defaultTimeoutMs);
|
|
126
|
+
assertPositiveInteger("maxTimeoutMs", maxTimeoutMs);
|
|
127
|
+
if (minTimeoutMs > maxTimeoutMs) {
|
|
128
|
+
throw new HttpPolicyError("minTimeoutMs must be <= maxTimeoutMs");
|
|
129
|
+
}
|
|
130
|
+
assertPositiveInteger("maxResponseBytes", maxResponseBytes);
|
|
131
|
+
assertPositiveInteger("maxBodyChars", maxBodyChars);
|
|
132
|
+
assertNonNegativeInteger("maxRedirects", maxRedirects);
|
|
133
|
+
assertPositiveInteger("retry.maxAttempts", retryMaxAttempts);
|
|
134
|
+
assertNonNegativeInteger("retry.initialDelayMs", retryInitialDelayMs);
|
|
135
|
+
assertNonNegativeInteger("retry.maxDelayMs", retryMaxDelayMs);
|
|
136
|
+
return {
|
|
137
|
+
allowPublicInternet,
|
|
138
|
+
allowedHosts,
|
|
139
|
+
deniedHosts: config.deniedHosts ?? [],
|
|
140
|
+
allowedProtocols,
|
|
141
|
+
allowPrivateNetwork: config.allowPrivateNetwork ?? false,
|
|
142
|
+
allowCredentialedUrls: config.allowCredentialedUrls ?? false,
|
|
143
|
+
defaultHeaders: headerEntries(config.defaultHeaders),
|
|
144
|
+
allowedRequestHeaders: new Set((config.allowedRequestHeaders ?? []).map(normalizeHeaderName)),
|
|
145
|
+
responseHeaderAllowlist: new Set((config.responseHeaderAllowlist ?? DEFAULT_RESPONSE_HEADERS).map(normalizeHeaderName)),
|
|
146
|
+
defaultTimeoutMs: clamp(defaultTimeoutMs, minTimeoutMs, maxTimeoutMs),
|
|
147
|
+
minTimeoutMs,
|
|
148
|
+
maxTimeoutMs,
|
|
149
|
+
maxResponseBytes,
|
|
150
|
+
maxBodyChars,
|
|
151
|
+
maxRedirects,
|
|
152
|
+
retryMaxAttempts,
|
|
153
|
+
retryInitialDelayMs,
|
|
154
|
+
retryMaxDelayMs,
|
|
155
|
+
retryUnsafeMethods: config.retry?.retryUnsafeMethods ?? false,
|
|
156
|
+
fetch: config.fetch ?? fetch
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
function clamp(value, min, max) {
|
|
160
|
+
return Math.max(min, Math.min(max, value));
|
|
161
|
+
}
|
|
162
|
+
function cleanHostname(hostname) {
|
|
163
|
+
return hostname.toLowerCase().replace(/^\[/, "").replace(/\]$/, "").replace(/\.$/, "");
|
|
164
|
+
}
|
|
165
|
+
function cleanHost(host) {
|
|
166
|
+
return host.toLowerCase().replace(/\.$/, "");
|
|
167
|
+
}
|
|
168
|
+
function parsePatternHost(pattern) {
|
|
169
|
+
const trimmed = pattern.trim().toLowerCase();
|
|
170
|
+
if (/^[a-z][a-z0-9+.-]*:\/\//.test(trimmed)) {
|
|
171
|
+
return new URL(trimmed).host.toLowerCase();
|
|
172
|
+
}
|
|
173
|
+
return trimmed.replace(/\.$/, "");
|
|
174
|
+
}
|
|
175
|
+
function matchesHost(url, pattern) {
|
|
176
|
+
const hostname = cleanHostname(url.hostname);
|
|
177
|
+
const host = cleanHost(url.host);
|
|
178
|
+
if (pattern instanceof RegExp) {
|
|
179
|
+
pattern.lastIndex = 0;
|
|
180
|
+
return pattern.test(hostname) || pattern.test(host);
|
|
181
|
+
}
|
|
182
|
+
const value = parsePatternHost(pattern);
|
|
183
|
+
if (value === "*")
|
|
184
|
+
return true;
|
|
185
|
+
if (value.startsWith("*.")) {
|
|
186
|
+
const suffix = value.slice(1);
|
|
187
|
+
return hostname.endsWith(suffix) && hostname.length > suffix.length;
|
|
188
|
+
}
|
|
189
|
+
return value.includes(":") ? host === value : hostname === value;
|
|
190
|
+
}
|
|
191
|
+
function parseIPv4(hostname) {
|
|
192
|
+
const parts = hostname.split(".");
|
|
193
|
+
if (parts.length !== 4)
|
|
194
|
+
return null;
|
|
195
|
+
const numbers = parts.map((part) => {
|
|
196
|
+
if (!/^\d+$/.test(part))
|
|
197
|
+
return Number.NaN;
|
|
198
|
+
const value = Number(part);
|
|
199
|
+
return value >= 0 && value <= 255 ? value : Number.NaN;
|
|
200
|
+
});
|
|
201
|
+
return numbers.every((n) => Number.isInteger(n)) ? numbers : null;
|
|
202
|
+
}
|
|
203
|
+
function isPrivateIPv4(parts) {
|
|
204
|
+
const [a = 0, b = 0] = parts;
|
|
205
|
+
if (a === 0 || a === 10 || a === 127)
|
|
206
|
+
return true;
|
|
207
|
+
if (a === 100 && b >= 64 && b <= 127)
|
|
208
|
+
return true;
|
|
209
|
+
if (a === 169 && b === 254)
|
|
210
|
+
return true;
|
|
211
|
+
if (a === 172 && b >= 16 && b <= 31)
|
|
212
|
+
return true;
|
|
213
|
+
if (a === 192 && b === 168)
|
|
214
|
+
return true;
|
|
215
|
+
if (a >= 224)
|
|
216
|
+
return true;
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
function isPrivateIPv6(hostname) {
|
|
220
|
+
const value = hostname.toLowerCase();
|
|
221
|
+
if (value === "::" || value === "::1")
|
|
222
|
+
return true;
|
|
223
|
+
if (value.startsWith("fc") || value.startsWith("fd"))
|
|
224
|
+
return true;
|
|
225
|
+
if (/^fe[89ab]/.test(value))
|
|
226
|
+
return true;
|
|
227
|
+
if (value.startsWith("ff"))
|
|
228
|
+
return true;
|
|
229
|
+
const mapped = value.match(/^::ffff:(\d+\.\d+\.\d+\.\d+)$/);
|
|
230
|
+
if (mapped?.[1] !== undefined) {
|
|
231
|
+
const ipv4 = parseIPv4(mapped[1]);
|
|
232
|
+
return ipv4 !== null && isPrivateIPv4(ipv4);
|
|
233
|
+
}
|
|
234
|
+
return false;
|
|
235
|
+
}
|
|
236
|
+
function isPrivateTarget(hostname) {
|
|
237
|
+
const host = cleanHostname(hostname);
|
|
238
|
+
if (host === "localhost" || host.endsWith(".localhost"))
|
|
239
|
+
return true;
|
|
240
|
+
const ipv4 = parseIPv4(host);
|
|
241
|
+
if (ipv4 !== null)
|
|
242
|
+
return isPrivateIPv4(ipv4);
|
|
243
|
+
if (host.includes(":"))
|
|
244
|
+
return isPrivateIPv6(host);
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
function assertUrlAllowed(url, config) {
|
|
248
|
+
if (!config.allowedProtocols.includes(url.protocol.toLowerCase())) {
|
|
249
|
+
throw new HttpPolicyError(`protocol ${url.protocol} is not allowed`);
|
|
250
|
+
}
|
|
251
|
+
if (!config.allowCredentialedUrls && (url.username !== "" || url.password !== "")) {
|
|
252
|
+
throw new HttpPolicyError("credentialed URLs are not allowed");
|
|
253
|
+
}
|
|
254
|
+
if (!config.allowPrivateNetwork && isPrivateTarget(url.hostname)) {
|
|
255
|
+
throw new HttpPolicyError(`private or localhost target ${url.hostname} is not allowed`);
|
|
256
|
+
}
|
|
257
|
+
if (config.deniedHosts.some((pattern) => matchesHost(url, pattern))) {
|
|
258
|
+
throw new HttpPolicyError(`host ${url.host} is denied by policy`);
|
|
259
|
+
}
|
|
260
|
+
if (!config.allowPublicInternet && !config.allowedHosts.some((pattern) => matchesHost(url, pattern))) {
|
|
261
|
+
throw new HttpPolicyError(`host ${url.host} is not in allowedHosts`);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
function buildRequestHeaders(config, modelHeaders, contentType, options) {
|
|
265
|
+
const headers = new Headers;
|
|
266
|
+
if (options?.includeConfiguredHeaders ?? true) {
|
|
267
|
+
for (const entry of config.defaultHeaders)
|
|
268
|
+
headers.set(entry.name, entry.value);
|
|
269
|
+
for (const entry of modelHeaders ?? []) {
|
|
270
|
+
const name = normalizeHeaderName(entry.name);
|
|
271
|
+
if (!config.allowedRequestHeaders.has(name))
|
|
272
|
+
continue;
|
|
273
|
+
if (/[\r\n]/.test(entry.value)) {
|
|
274
|
+
throw new HttpPolicyError(`header ${JSON.stringify(entry.name)} value contains a newline`);
|
|
275
|
+
}
|
|
276
|
+
headers.set(name, entry.value);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
if (contentType !== undefined && !headers.has("content-type")) {
|
|
280
|
+
headers.set("content-type", contentType);
|
|
281
|
+
}
|
|
282
|
+
return headers;
|
|
283
|
+
}
|
|
284
|
+
function allowedResponseHeaders(headers, allowlist) {
|
|
285
|
+
const entries = [];
|
|
286
|
+
for (const [name, value] of headers.entries()) {
|
|
287
|
+
if (allowlist.has(name.toLowerCase()))
|
|
288
|
+
entries.push({ name, value });
|
|
289
|
+
}
|
|
290
|
+
return entries.sort((a, b) => a.name.localeCompare(b.name));
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// src/tools-http/client.ts
|
|
294
|
+
class RetryableHttpStatusError extends Error {
|
|
295
|
+
status;
|
|
296
|
+
constructor(status) {
|
|
297
|
+
super(`retryable HTTP status ${status}`);
|
|
298
|
+
this.name = "RetryableHttpStatusError";
|
|
299
|
+
this.status = status;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
function messageOf(error) {
|
|
303
|
+
if (error instanceof Error)
|
|
304
|
+
return error.message;
|
|
305
|
+
return String(error);
|
|
306
|
+
}
|
|
307
|
+
function statusOf(error) {
|
|
308
|
+
if (typeof error === "object" && error !== null && "status" in error) {
|
|
309
|
+
const status = error.status;
|
|
310
|
+
if (typeof status === "number")
|
|
311
|
+
return status;
|
|
312
|
+
}
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
function isAbort(error) {
|
|
316
|
+
return error instanceof DOMException && error.name === "AbortError";
|
|
317
|
+
}
|
|
318
|
+
function retryableStatus(status) {
|
|
319
|
+
return status === 429 || status >= 500;
|
|
320
|
+
}
|
|
321
|
+
function combineSignals(...signals) {
|
|
322
|
+
const present = signals.filter((signal) => signal !== undefined);
|
|
323
|
+
if (present.length === 0)
|
|
324
|
+
return;
|
|
325
|
+
if (present.length === 1)
|
|
326
|
+
return present[0];
|
|
327
|
+
return AbortSignal.any(present);
|
|
328
|
+
}
|
|
329
|
+
function timeoutFor(config, requested) {
|
|
330
|
+
if (requested === undefined || requested <= 0)
|
|
331
|
+
return config.defaultTimeoutMs;
|
|
332
|
+
return clamp(requested, config.minTimeoutMs, config.maxTimeoutMs);
|
|
333
|
+
}
|
|
334
|
+
function bytesFor(config, requested) {
|
|
335
|
+
if (requested === undefined || requested <= 0)
|
|
336
|
+
return config.maxResponseBytes;
|
|
337
|
+
return clamp(requested, 1, config.maxResponseBytes);
|
|
338
|
+
}
|
|
339
|
+
function charsFor(config, requested) {
|
|
340
|
+
if (requested === undefined || requested <= 0)
|
|
341
|
+
return config.maxBodyChars;
|
|
342
|
+
return clamp(requested, 1, config.maxBodyChars);
|
|
343
|
+
}
|
|
344
|
+
function shouldRetryMethod(config, method) {
|
|
345
|
+
return method === "GET" || config.retryUnsafeMethods;
|
|
346
|
+
}
|
|
347
|
+
function isTransient(error) {
|
|
348
|
+
if (isAbort(error))
|
|
349
|
+
return false;
|
|
350
|
+
const status = statusOf(error);
|
|
351
|
+
if (status === undefined)
|
|
352
|
+
return true;
|
|
353
|
+
return retryableStatus(status);
|
|
354
|
+
}
|
|
355
|
+
function redirectedMethod(method, status) {
|
|
356
|
+
if (status === 303)
|
|
357
|
+
return "GET";
|
|
358
|
+
if ((status === 301 || status === 302) && method === "POST")
|
|
359
|
+
return "GET";
|
|
360
|
+
return method;
|
|
361
|
+
}
|
|
362
|
+
function bodyFor(req, method) {
|
|
363
|
+
if (method === "GET")
|
|
364
|
+
return {};
|
|
365
|
+
if (req.body !== undefined && req.bodyJson !== undefined) {
|
|
366
|
+
throw new HttpPolicyError("body and bodyJson cannot both be supplied");
|
|
367
|
+
}
|
|
368
|
+
if (req.bodyJson !== undefined) {
|
|
369
|
+
return { body: JSON.stringify(req.bodyJson), contentType: req.contentType ?? "application/json" };
|
|
370
|
+
}
|
|
371
|
+
if (req.body !== undefined) {
|
|
372
|
+
return { body: req.body, contentType: req.contentType };
|
|
373
|
+
}
|
|
374
|
+
return {};
|
|
375
|
+
}
|
|
376
|
+
function contentKind(contentType) {
|
|
377
|
+
const type = (contentType ?? "").toLowerCase().split(";")[0]?.trim() ?? "";
|
|
378
|
+
if (type === "application/json" || type.endsWith("+json"))
|
|
379
|
+
return "json";
|
|
380
|
+
if (type.startsWith("text/"))
|
|
381
|
+
return "text";
|
|
382
|
+
if (type === "application/xhtml+xml" || type === "application/xml" || type.endsWith("+xml"))
|
|
383
|
+
return "text";
|
|
384
|
+
return "binary";
|
|
385
|
+
}
|
|
386
|
+
function truncateChars(text, maxChars) {
|
|
387
|
+
if (text.length <= maxChars)
|
|
388
|
+
return { text, truncated: false };
|
|
389
|
+
return { text: text.slice(0, maxChars), truncated: true };
|
|
390
|
+
}
|
|
391
|
+
async function readBytes(response, maxBytes) {
|
|
392
|
+
if (response.body === null)
|
|
393
|
+
return { bytes: new Uint8Array, truncated: false };
|
|
394
|
+
const reader = response.body.getReader();
|
|
395
|
+
const chunks = [];
|
|
396
|
+
let received = 0;
|
|
397
|
+
let truncated = false;
|
|
398
|
+
try {
|
|
399
|
+
while (true) {
|
|
400
|
+
const next = await reader.read();
|
|
401
|
+
if (next.done)
|
|
402
|
+
break;
|
|
403
|
+
const value = next.value;
|
|
404
|
+
if (received + value.byteLength > maxBytes) {
|
|
405
|
+
const remaining = Math.max(0, maxBytes - received);
|
|
406
|
+
if (remaining > 0)
|
|
407
|
+
chunks.push(value.slice(0, remaining));
|
|
408
|
+
received = maxBytes;
|
|
409
|
+
truncated = true;
|
|
410
|
+
await reader.cancel();
|
|
411
|
+
break;
|
|
412
|
+
}
|
|
413
|
+
chunks.push(value);
|
|
414
|
+
received += value.byteLength;
|
|
415
|
+
}
|
|
416
|
+
} finally {
|
|
417
|
+
reader.releaseLock();
|
|
418
|
+
}
|
|
419
|
+
const out = new Uint8Array(received);
|
|
420
|
+
let offset = 0;
|
|
421
|
+
for (const chunk of chunks) {
|
|
422
|
+
out.set(chunk, offset);
|
|
423
|
+
offset += chunk.byteLength;
|
|
424
|
+
}
|
|
425
|
+
return { bytes: out, truncated };
|
|
426
|
+
}
|
|
427
|
+
function parseBody(bytes, responseBytesTruncated, contentType, maxBodyChars) {
|
|
428
|
+
const kind = contentKind(contentType);
|
|
429
|
+
if (kind === "binary") {
|
|
430
|
+
return { bodyOmitted: true, bodyTruncated: responseBytesTruncated };
|
|
431
|
+
}
|
|
432
|
+
const decoded = new TextDecoder().decode(bytes);
|
|
433
|
+
const truncated = truncateChars(decoded, maxBodyChars);
|
|
434
|
+
if (kind === "json" && !responseBytesTruncated && !truncated.truncated) {
|
|
435
|
+
try {
|
|
436
|
+
return {
|
|
437
|
+
bodyJson: decoded.length === 0 ? null : JSON.parse(decoded),
|
|
438
|
+
bodyOmitted: false,
|
|
439
|
+
bodyTruncated: false
|
|
440
|
+
};
|
|
441
|
+
} catch (error) {
|
|
442
|
+
throw new Error(`failed to parse JSON response: ${messageOf(error)}`);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
return {
|
|
446
|
+
body: truncated.text,
|
|
447
|
+
bodyOmitted: false,
|
|
448
|
+
bodyTruncated: responseBytesTruncated || truncated.truncated
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
function validateUrl(raw) {
|
|
452
|
+
try {
|
|
453
|
+
return new URL(raw);
|
|
454
|
+
} catch (error) {
|
|
455
|
+
throw new HttpPolicyError(`invalid URL: ${messageOf(error)}`);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
function checkUrl(url, config, ctx) {
|
|
459
|
+
try {
|
|
460
|
+
assertUrlAllowed(url, config);
|
|
461
|
+
emitHttpPolicy(ctx, "allow", url.href);
|
|
462
|
+
} catch (error) {
|
|
463
|
+
const message = messageOf(error);
|
|
464
|
+
emitHttpPolicy(ctx, "deny", url.href, message);
|
|
465
|
+
throw error;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
async function cancelBody(response) {
|
|
469
|
+
try {
|
|
470
|
+
await response.body?.cancel();
|
|
471
|
+
} catch {}
|
|
472
|
+
}
|
|
473
|
+
function createHttpToolClient(config) {
|
|
474
|
+
const normalized = normalizeHttpConfig(config);
|
|
475
|
+
async function perform(req, ctx) {
|
|
476
|
+
let method = req.method;
|
|
477
|
+
let url = validateUrl(req.url);
|
|
478
|
+
checkUrl(url, normalized, ctx);
|
|
479
|
+
const timeoutMs = timeoutFor(normalized, req.timeoutMs);
|
|
480
|
+
const maxBytes = bytesFor(normalized, req.maxBytes);
|
|
481
|
+
const maxBodyChars = charsFor(normalized, req.maxBodyChars);
|
|
482
|
+
const started = performance.now();
|
|
483
|
+
const redirects = [];
|
|
484
|
+
const initialOrigin = url.origin;
|
|
485
|
+
emitHttpRequestStart(ctx, method, url.href, timeoutMs);
|
|
486
|
+
try {
|
|
487
|
+
for (let redirectCount = 0;redirectCount <= normalized.maxRedirects; redirectCount += 1) {
|
|
488
|
+
const currentMethod = method;
|
|
489
|
+
const currentUrl = url;
|
|
490
|
+
const { body, contentType } = bodyFor(req, currentMethod);
|
|
491
|
+
const requestHeaders = buildRequestHeaders(normalized, req.headers, contentType, {
|
|
492
|
+
includeConfiguredHeaders: currentUrl.origin === initialOrigin
|
|
493
|
+
});
|
|
494
|
+
const retryEnabled = shouldRetryMethod(normalized, currentMethod);
|
|
495
|
+
const maxAttempts = retryEnabled ? normalized.retryMaxAttempts : 1;
|
|
496
|
+
const pipeline = combine(retry({
|
|
497
|
+
maxAttempts,
|
|
498
|
+
backoff: exponentialBackoff({
|
|
499
|
+
initial: normalized.retryInitialDelayMs,
|
|
500
|
+
max: normalized.retryMaxDelayMs
|
|
501
|
+
}),
|
|
502
|
+
shouldRetry: (error) => {
|
|
503
|
+
if (ctx?.signal?.aborted)
|
|
504
|
+
return false;
|
|
505
|
+
return retryEnabled && isTransient(error);
|
|
506
|
+
}
|
|
507
|
+
}), timeout({ ms: timeoutMs }));
|
|
508
|
+
const response = await pipeline.execute(async (rctx) => {
|
|
509
|
+
const fetched = await normalized.fetch(currentUrl.href, {
|
|
510
|
+
method: currentMethod,
|
|
511
|
+
headers: requestHeaders,
|
|
512
|
+
body: currentMethod === "GET" ? undefined : body,
|
|
513
|
+
redirect: "manual",
|
|
514
|
+
signal: combineSignals(rctx.signal, ctx?.signal)
|
|
515
|
+
});
|
|
516
|
+
if (retryEnabled && retryableStatus(fetched.status) && rctx.attempt < maxAttempts) {
|
|
517
|
+
await cancelBody(fetched);
|
|
518
|
+
throw new RetryableHttpStatusError(fetched.status);
|
|
519
|
+
}
|
|
520
|
+
return fetched;
|
|
521
|
+
});
|
|
522
|
+
const location = response.headers.get("location");
|
|
523
|
+
if (response.status >= 300 && response.status < 400 && location !== null) {
|
|
524
|
+
if (redirectCount >= normalized.maxRedirects) {
|
|
525
|
+
await cancelBody(response);
|
|
526
|
+
throw new HttpPolicyError(`too many redirects (max ${normalized.maxRedirects})`);
|
|
527
|
+
}
|
|
528
|
+
const next = new URL(location, currentUrl);
|
|
529
|
+
await cancelBody(response);
|
|
530
|
+
checkUrl(next, normalized, ctx);
|
|
531
|
+
redirects.push(next.href);
|
|
532
|
+
method = redirectedMethod(method, response.status);
|
|
533
|
+
url = next;
|
|
534
|
+
continue;
|
|
535
|
+
}
|
|
536
|
+
const contentTypeHeader = response.headers.get("content-type") ?? undefined;
|
|
537
|
+
const { bytes, truncated } = await readBytes(response, maxBytes);
|
|
538
|
+
const parsed = parseBody(bytes, truncated, contentTypeHeader, maxBodyChars);
|
|
539
|
+
const result = {
|
|
540
|
+
url: req.url,
|
|
541
|
+
finalUrl: currentUrl.href,
|
|
542
|
+
redirected: redirects.length > 0,
|
|
543
|
+
redirects,
|
|
544
|
+
status: response.status,
|
|
545
|
+
statusText: response.statusText,
|
|
546
|
+
...contentTypeHeader !== undefined ? { contentType: contentTypeHeader } : {},
|
|
547
|
+
headers: allowedResponseHeaders(response.headers, normalized.responseHeaderAllowlist),
|
|
548
|
+
...parsed,
|
|
549
|
+
responseBytes: bytes.byteLength,
|
|
550
|
+
responseBytesTruncated: truncated,
|
|
551
|
+
elapsedMs: Math.round(performance.now() - started)
|
|
552
|
+
};
|
|
553
|
+
emitHttpRequestEnd(ctx, req.method, req.url, result);
|
|
554
|
+
return result;
|
|
555
|
+
}
|
|
556
|
+
throw new HttpPolicyError(`too many redirects (max ${normalized.maxRedirects})`);
|
|
557
|
+
} catch (error) {
|
|
558
|
+
const message = messageOf(error);
|
|
559
|
+
emitHttpRequestEnd(ctx, req.method, req.url, undefined, message);
|
|
560
|
+
throw error;
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
return {
|
|
564
|
+
request: perform,
|
|
565
|
+
get(url, options, ctx) {
|
|
566
|
+
return perform({ method: "GET", url, ...options ?? {} }, ctx);
|
|
567
|
+
},
|
|
568
|
+
post(url, options, ctx) {
|
|
569
|
+
return perform({ method: "POST", url, ...options ?? {} }, ctx);
|
|
570
|
+
}
|
|
571
|
+
};
|
|
572
|
+
}
|
|
573
|
+
// src/tools-http/define.ts
|
|
574
|
+
var HEADER_SCHEMA = {
|
|
575
|
+
type: "array",
|
|
576
|
+
items: {
|
|
577
|
+
type: "object",
|
|
578
|
+
properties: {
|
|
579
|
+
name: { type: "string" },
|
|
580
|
+
value: { type: "string" }
|
|
581
|
+
},
|
|
582
|
+
required: ["name", "value"],
|
|
583
|
+
additionalProperties: false
|
|
584
|
+
}
|
|
585
|
+
};
|
|
586
|
+
var GET_PARAMS = fromJsonSchema({
|
|
587
|
+
type: "object",
|
|
588
|
+
properties: {
|
|
589
|
+
url: { type: "string", description: "Absolute http(s) URL to fetch." },
|
|
590
|
+
headers: HEADER_SCHEMA,
|
|
591
|
+
timeout_ms: { type: "integer", description: "Request timeout in milliseconds; clamped by host policy." },
|
|
592
|
+
max_bytes: { type: "integer", description: "Response byte cap; clamped by host policy." },
|
|
593
|
+
max_body_chars: { type: "integer", description: "Returned text character cap; clamped by host policy." }
|
|
594
|
+
},
|
|
595
|
+
required: ["url"],
|
|
596
|
+
additionalProperties: false
|
|
597
|
+
});
|
|
598
|
+
var POST_PARAMS = fromJsonSchema({
|
|
599
|
+
type: "object",
|
|
600
|
+
properties: {
|
|
601
|
+
url: { type: "string", description: "Absolute http(s) URL to fetch." },
|
|
602
|
+
headers: HEADER_SCHEMA,
|
|
603
|
+
body: { type: "string", description: "Raw request body." },
|
|
604
|
+
body_json: { description: "JSON value to serialize as the request body." },
|
|
605
|
+
content_type: { type: "string", description: "Content-Type for body/body_json when not already set." },
|
|
606
|
+
timeout_ms: { type: "integer", description: "Request timeout in milliseconds; clamped by host policy." },
|
|
607
|
+
max_bytes: { type: "integer", description: "Response byte cap; clamped by host policy." },
|
|
608
|
+
max_body_chars: { type: "integer", description: "Returned text character cap; clamped by host policy." }
|
|
609
|
+
},
|
|
610
|
+
required: ["url"],
|
|
611
|
+
additionalProperties: false
|
|
612
|
+
});
|
|
613
|
+
function fail(error) {
|
|
614
|
+
if (error instanceof Error)
|
|
615
|
+
return { ok: false, error: error.message };
|
|
616
|
+
return { ok: false, error: String(error) };
|
|
617
|
+
}
|
|
618
|
+
function httpTools(config) {
|
|
619
|
+
const client = createHttpToolClient(config);
|
|
620
|
+
const httpGet = defineTool({
|
|
621
|
+
name: "http_get",
|
|
622
|
+
description: "Fetch one HTTP(S) URL under the host network policy and return compact parsed response metadata.",
|
|
623
|
+
parameters: GET_PARAMS,
|
|
624
|
+
async execute(args, ctx) {
|
|
625
|
+
try {
|
|
626
|
+
const content = await client.get(args.url, {
|
|
627
|
+
headers: args.headers,
|
|
628
|
+
timeoutMs: args.timeout_ms,
|
|
629
|
+
maxBytes: args.max_bytes,
|
|
630
|
+
maxBodyChars: args.max_body_chars
|
|
631
|
+
}, ctx);
|
|
632
|
+
return { ok: true, content };
|
|
633
|
+
} catch (error) {
|
|
634
|
+
return fail(error);
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
});
|
|
638
|
+
const httpPost = defineTool({
|
|
639
|
+
name: "http_post",
|
|
640
|
+
description: "POST to one HTTP(S) URL under the host network policy and return compact parsed response metadata.",
|
|
641
|
+
parameters: POST_PARAMS,
|
|
642
|
+
async execute(args, ctx) {
|
|
643
|
+
try {
|
|
644
|
+
const content = await client.post(args.url, {
|
|
645
|
+
headers: args.headers,
|
|
646
|
+
body: args.body,
|
|
647
|
+
bodyJson: args.body_json,
|
|
648
|
+
contentType: args.content_type,
|
|
649
|
+
timeoutMs: args.timeout_ms,
|
|
650
|
+
maxBytes: args.max_bytes,
|
|
651
|
+
maxBodyChars: args.max_body_chars
|
|
652
|
+
}, ctx);
|
|
653
|
+
return { ok: true, content };
|
|
654
|
+
} catch (error) {
|
|
655
|
+
return fail(error);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
});
|
|
659
|
+
return { httpGet, httpPost };
|
|
660
|
+
}
|
|
661
|
+
export { HTTP_EVENT, HttpPolicyError, createHttpToolClient, httpTools };
|
|
@@ -1,43 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ContextWindowError
|
|
3
|
-
} from "./index-7690reng.js";
|
|
4
1
|
import {
|
|
5
2
|
system
|
|
6
3
|
} from "./index-1p6mb2vz.js";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const body = typeof item.content === "string" ? item.content : JSON.stringify(item.content, null, 2) ?? "";
|
|
11
|
-
return item.title !== undefined && item.title !== "" ? `## ${item.title}
|
|
12
|
-
${body}` : body;
|
|
13
|
-
}
|
|
14
|
-
function staticContext(content, title) {
|
|
15
|
-
const item = { content, ...title !== undefined ? { title } : {} };
|
|
16
|
-
return {
|
|
17
|
-
name: "static",
|
|
18
|
-
resolve: () => [item]
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
function dynamicContext(name, fn, title) {
|
|
22
|
-
return {
|
|
23
|
-
name,
|
|
24
|
-
resolve: async (ctx) => {
|
|
25
|
-
const content = await fn(ctx);
|
|
26
|
-
return [{ content, ...title !== undefined ? { title } : {} }];
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
async function resolveContext(providers, ctx) {
|
|
31
|
-
if (providers === undefined || providers.length === 0)
|
|
32
|
-
return [];
|
|
33
|
-
const resolved = await Promise.all(providers.map((p) => p.resolve(ctx)));
|
|
34
|
-
const blocks = resolved.flat().map(renderItem).filter((s) => s !== "");
|
|
35
|
-
if (blocks.length === 0)
|
|
36
|
-
return [];
|
|
37
|
-
return [system(blocks.join(`
|
|
38
|
-
|
|
39
|
-
`))];
|
|
40
|
-
}
|
|
4
|
+
import {
|
|
5
|
+
ContextWindowError
|
|
6
|
+
} from "./index-ajr3nk10.js";
|
|
41
7
|
|
|
42
8
|
// src/context/window.ts
|
|
43
9
|
function partChars(part) {
|
|
@@ -145,4 +111,4 @@ async function applyContextWindow(messages, window, ctx) {
|
|
|
145
111
|
});
|
|
146
112
|
}
|
|
147
113
|
|
|
148
|
-
export {
|
|
114
|
+
export { estimateTokens, truncateOldest, summarizeOldest, applyContextWindow };
|