@vellumai/assistant 0.5.1 → 0.5.3
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/ARCHITECTURE.md +163 -54
- package/docs/architecture/integrations.md +62 -67
- package/docs/credential-execution-service.md +3 -3
- package/docs/skills.md +100 -0
- package/package.json +1 -1
- package/src/__tests__/agent-loop.test.ts +111 -0
- package/src/__tests__/always-loaded-tools-guard.test.ts +3 -4
- package/src/__tests__/app-builder-tool-scripts.test.ts +13 -151
- package/src/__tests__/app-dir-path-guard.test.ts +78 -0
- package/src/__tests__/app-executors.test.ts +1 -291
- package/src/__tests__/app-git-history.test.ts +4 -4
- package/src/__tests__/app-routes-csp.test.ts +1 -0
- package/src/__tests__/app-store-dir-names.test.ts +426 -0
- package/src/__tests__/attachments-store.test.ts +169 -21
- package/src/__tests__/attachments.test.ts +115 -1
- package/src/__tests__/btw-routes.test.ts +1 -0
- package/src/__tests__/canonical-guardian-store.test.ts +38 -0
- package/src/__tests__/channel-reply-delivery.test.ts +55 -0
- package/src/__tests__/checker.test.ts +54 -0
- package/src/__tests__/claude-code-skill-regression.test.ts +2 -0
- package/src/__tests__/claude-code-tool-profiles.test.ts +2 -0
- package/src/__tests__/compaction.benchmark.test.ts +2 -1
- package/src/__tests__/config-schema-cmd.test.ts +68 -21
- package/src/__tests__/config-schema.test.ts +1 -1
- package/src/__tests__/conversation-agent-loop-overflow.test.ts +156 -5
- package/src/__tests__/conversation-agent-loop.test.ts +297 -2
- package/src/__tests__/conversation-attachments.test.ts +17 -19
- package/src/__tests__/conversation-disk-view-integration.test.ts +277 -0
- package/src/__tests__/conversation-disk-view.test.ts +810 -0
- package/src/__tests__/conversation-error.test.ts +1 -1
- package/src/__tests__/conversation-fork-crud.test.ts +551 -0
- package/src/__tests__/conversation-fork-route.test.ts +386 -0
- package/src/__tests__/conversation-history-web-search.test.ts +1 -1
- package/src/__tests__/conversation-key-store-disk-view.test.ts +130 -0
- package/src/__tests__/conversation-media-retry.test.ts +8 -2
- package/src/__tests__/conversation-memory-dirty-tail.test.ts +150 -0
- package/src/__tests__/conversation-provider-retry-repair.test.ts +7 -0
- package/src/__tests__/conversation-queue.test.ts +36 -1
- package/src/__tests__/conversation-routes-disk-view.test.ts +439 -0
- package/src/__tests__/conversation-routes-guardian-reply.test.ts +2 -2
- package/src/__tests__/conversation-routes-slash-commands.test.ts +2 -7
- package/src/__tests__/conversation-runtime-assembly.test.ts +17 -2
- package/src/__tests__/conversation-skill-tools.test.ts +4 -9
- package/src/__tests__/conversation-slash-commands.test.ts +149 -0
- package/src/__tests__/conversation-store.test.ts +24 -21
- package/src/__tests__/conversation-surfaces-state-update.test.ts +246 -0
- package/src/__tests__/conversation-surfaces-task-progress.test.ts +1 -0
- package/src/__tests__/conversation-title-service.test.ts +137 -0
- package/src/__tests__/conversation-tool-setup-app-refresh.test.ts +25 -315
- package/src/__tests__/conversation-tool-setup-memory-scope.test.ts +1 -0
- package/src/__tests__/conversation-tool-setup-side-effect-flag.test.ts +1 -0
- package/src/__tests__/conversation-wipe.test.ts +226 -0
- package/src/__tests__/conversation-workspace-cache-state.test.ts +44 -2
- package/src/__tests__/conversation-workspace-injection.test.ts +11 -0
- package/src/__tests__/credential-security-invariants.test.ts +3 -0
- package/src/__tests__/credential-vault-unit.test.ts +5 -10
- package/src/__tests__/cu-unified-flow.test.ts +1 -0
- package/src/__tests__/db-conversation-fork-lineage-migration.test.ts +241 -0
- package/src/__tests__/db-llm-request-log-provider-migration.test.ts +214 -0
- package/src/__tests__/db-memory-archive-migration.test.ts +372 -0
- package/src/__tests__/db-memory-brief-state-migration.test.ts +213 -0
- package/src/__tests__/db-memory-reducer-checkpoints.test.ts +273 -0
- package/src/__tests__/diagnostics-export.test.ts +70 -1
- package/src/__tests__/first-greeting.test.ts +80 -0
- package/src/__tests__/gateway-only-guard.test.ts +1 -0
- package/src/__tests__/handlers-user-message-approval-consumption.test.ts +3 -7
- package/src/__tests__/history-repair.test.ts +32 -10
- package/src/__tests__/http-conversation-lineage.test.ts +251 -0
- package/src/__tests__/image-source-path-reinject.test.ts +136 -0
- package/src/__tests__/inline-command-runner.test.ts +311 -0
- package/src/__tests__/inline-skill-authoring-guard.test.ts +220 -0
- package/src/__tests__/inline-skill-load-permissions.test.ts +435 -0
- package/src/__tests__/list-messages-attachments.test.ts +96 -0
- package/src/__tests__/llm-context-normalization.test.ts +1116 -0
- package/src/__tests__/llm-context-route-provider.test.ts +217 -0
- package/src/__tests__/llm-request-log-turn-query.test.ts +270 -0
- package/src/__tests__/media-generate-image.test.ts +47 -94
- package/src/__tests__/memory-brief-open-loops.test.ts +530 -0
- package/src/__tests__/memory-brief-time.test.ts +285 -0
- package/src/__tests__/memory-brief-wrapper.test.ts +311 -0
- package/src/__tests__/memory-chunk-archive.test.ts +400 -0
- package/src/__tests__/memory-chunk-dual-write.test.ts +453 -0
- package/src/__tests__/memory-episode-archive.test.ts +370 -0
- package/src/__tests__/memory-episode-dual-write.test.ts +626 -0
- package/src/__tests__/memory-lifecycle-e2e.test.ts +3 -1
- package/src/__tests__/memory-observation-archive.test.ts +375 -0
- package/src/__tests__/memory-observation-dual-write.test.ts +318 -0
- package/src/__tests__/memory-recall-quality.test.ts +7 -7
- package/src/__tests__/memory-reducer-store.test.ts +728 -0
- package/src/__tests__/memory-reducer-types.test.ts +699 -0
- package/src/__tests__/memory-reducer.test.ts +698 -0
- package/src/__tests__/memory-regressions.test.ts +6 -4
- package/src/__tests__/memory-simplified-config.test.ts +281 -0
- package/src/__tests__/migration-cross-version-compatibility.test.ts +4 -1
- package/src/__tests__/migration-export-http.test.ts +3 -1
- package/src/__tests__/migration-import-commit-http.test.ts +18 -4
- package/src/__tests__/migration-import-preflight-http.test.ts +1 -3
- package/src/__tests__/mime-builder.test.ts +3 -2
- package/src/__tests__/non-member-access-request.test.ts +12 -1
- package/src/__tests__/notification-decision-identity.test.ts +52 -0
- package/src/__tests__/oauth-apps-routes.test.ts +103 -0
- package/src/__tests__/oauth-store.test.ts +115 -0
- package/src/__tests__/parse-identity-fields.test.ts +129 -0
- package/src/__tests__/provider-error-scenarios.test.ts +1 -3
- package/src/__tests__/provider-failover-actual-provider.test.ts +66 -0
- package/src/__tests__/recording-handler.test.ts +17 -0
- package/src/__tests__/registry.test.ts +3 -8
- package/src/__tests__/relay-server.test.ts +1 -1
- package/src/__tests__/runtime-attachment-metadata.test.ts +7 -3
- package/src/__tests__/schema-transforms.test.ts +165 -5
- package/src/__tests__/server-history-render.test.ts +2 -2
- package/src/__tests__/skill-load-inline-command.test.ts +598 -0
- package/src/__tests__/skill-load-inline-includes.test.ts +644 -0
- package/src/__tests__/skills-inline-command-expansions.test.ts +301 -0
- package/src/__tests__/skills-transitive-hash.test.ts +333 -0
- package/src/__tests__/slack-app-setup-skill-regression.test.ts +3 -1
- package/src/__tests__/slack-inbound-verification.test.ts +2 -2
- package/src/__tests__/starter-task-flow.test.ts +1 -0
- package/src/__tests__/suggestion-routes.test.ts +443 -0
- package/src/__tests__/swarm-conversation-integration.test.ts +1 -0
- package/src/__tests__/swarm-recursion.test.ts +1 -0
- package/src/__tests__/swarm-tool.test.ts +1 -0
- package/src/__tests__/tool-execution-abort-cleanup.test.ts +1 -0
- package/src/__tests__/tool-preview-lifecycle.test.ts +32 -5
- package/src/__tests__/top-level-renderer.test.ts +22 -0
- package/src/__tests__/turn-boundary-resolution.test.ts +243 -0
- package/src/__tests__/vellum-self-knowledge-inline-command.test.ts +320 -0
- package/src/__tests__/web-fetch.test.ts +6 -2
- package/src/__tests__/workspace-migration-006-services-config.test.ts +335 -0
- package/src/__tests__/workspace-migration-007-web-search-provider-rename.test.ts +312 -0
- package/src/__tests__/workspace-migration-009-backfill-conversation-disk-view.test.ts +278 -0
- package/src/__tests__/workspace-migration-010-app-dir-rename.test.ts +275 -0
- package/src/__tests__/workspace-migration-012-rename-conversation-disk-view-dirs.test.ts +77 -0
- package/src/__tests__/workspace-migration-013-repair-conversation-disk-view.test.ts +401 -0
- package/src/__tests__/workspace-migration-backfill-installation-id.test.ts +328 -0
- package/src/__tests__/workspace-migration-seed-device-id.test.ts +6 -10
- package/src/agent/attachments.ts +27 -1
- package/src/agent/loop.ts +29 -1
- package/src/avatar/traits-png-sync.ts +80 -25
- package/src/bundler/app-bundler.ts +4 -4
- package/src/calls/call-domain.ts +1 -0
- package/src/calls/voice-session-bridge.ts +1 -0
- package/src/cli/commands/auth.ts +92 -0
- package/src/cli/commands/avatar.ts +7 -6
- package/src/cli/commands/config.ts +2 -0
- package/src/cli/commands/oauth/providers.ts +29 -0
- package/src/cli/program.ts +12 -0
- package/src/cli.ts +15 -48
- package/src/config/bundled-skills/app-builder/SKILL.md +103 -28
- package/src/config/bundled-skills/app-builder/TOOLS.json +5 -199
- package/src/config/bundled-skills/app-builder/tools/{app-query.ts → app-refresh.ts} +2 -2
- package/src/config/bundled-skills/contacts/tools/google-contacts.ts +2 -3
- package/src/config/bundled-skills/gmail/tools/gmail-archive.ts +6 -9
- package/src/config/bundled-skills/gmail/tools/gmail-attachments.ts +4 -6
- package/src/config/bundled-skills/gmail/tools/gmail-draft.ts +2 -3
- package/src/config/bundled-skills/gmail/tools/gmail-filters.ts +2 -3
- package/src/config/bundled-skills/gmail/tools/gmail-follow-up.ts +2 -3
- package/src/config/bundled-skills/gmail/tools/gmail-forward.ts +2 -3
- package/src/config/bundled-skills/gmail/tools/gmail-label.ts +4 -6
- package/src/config/bundled-skills/gmail/tools/gmail-outreach-scan.ts +2 -3
- package/src/config/bundled-skills/gmail/tools/gmail-send-draft.ts +2 -3
- package/src/config/bundled-skills/gmail/tools/gmail-sender-digest.ts +2 -3
- package/src/config/bundled-skills/gmail/tools/gmail-trash.ts +2 -3
- package/src/config/bundled-skills/gmail/tools/gmail-unsubscribe.ts +2 -3
- package/src/config/bundled-skills/gmail/tools/gmail-vacation.ts +2 -3
- package/src/config/bundled-skills/google-calendar/tools/shared.ts +1 -1
- package/src/config/bundled-skills/image-studio/SKILL.md +2 -2
- package/src/config/bundled-skills/image-studio/TOOLS.json +2 -2
- package/src/config/bundled-skills/image-studio/tools/media-generate-image.ts +45 -72
- package/src/config/bundled-skills/media-processing/tools/extract-keyframes.ts +2 -2
- package/src/config/bundled-skills/messaging/tools/shared.ts +1 -1
- package/src/config/bundled-skills/settings/tools/voice-config-update.ts +19 -3
- package/src/config/bundled-skills/skill-management/SKILL.md +1 -1
- package/src/config/bundled-skills/skill-management/TOOLS.json +2 -2
- package/src/config/bundled-skills/slack/tools/shared.ts +19 -4
- package/src/config/bundled-skills/slack/tools/slack-scan-digest.ts +2 -3
- package/src/config/bundled-skills/transcribe/SKILL.md +1 -1
- package/src/config/bundled-skills/transcribe/TOOLS.json +2 -6
- package/src/config/bundled-skills/transcribe/tools/transcribe-media.ts +19 -83
- package/src/config/bundled-tool-registry.ts +2 -14
- package/src/config/feature-flag-registry.json +24 -0
- package/src/config/loader.ts +65 -0
- package/src/config/raw-config-utils.ts +58 -0
- package/src/config/schema-utils.ts +28 -7
- package/src/config/schema.ts +20 -0
- package/src/config/schemas/elevenlabs.ts +18 -0
- package/src/config/schemas/memory-lifecycle.ts +4 -2
- package/src/config/schemas/memory-simplified.ts +101 -0
- package/src/config/schemas/memory-storage.ts +1 -1
- package/src/config/schemas/memory.ts +4 -0
- package/src/config/schemas/services.ts +8 -6
- package/src/config/skills.ts +50 -4
- package/src/contacts/contact-store.ts +13 -6
- package/src/contacts/contacts-write.ts +0 -1
- package/src/context/window-manager.ts +13 -2
- package/src/daemon/conversation-agent-loop-handlers.ts +54 -8
- package/src/daemon/conversation-agent-loop.ts +127 -20
- package/src/daemon/conversation-attachments.ts +18 -36
- package/src/daemon/conversation-error.ts +2 -1
- package/src/daemon/conversation-history.ts +18 -4
- package/src/daemon/conversation-lifecycle.ts +50 -16
- package/src/daemon/conversation-messaging.ts +70 -26
- package/src/daemon/conversation-process.ts +58 -34
- package/src/daemon/conversation-runtime-assembly.ts +22 -38
- package/src/daemon/conversation-slash.ts +121 -256
- package/src/daemon/conversation-surfaces.ts +170 -24
- package/src/daemon/conversation-tool-setup.ts +0 -6
- package/src/daemon/conversation-workspace.ts +21 -1
- package/src/daemon/conversation.ts +69 -30
- package/src/daemon/first-greeting.ts +35 -0
- package/src/daemon/handlers/config-embeddings.ts +156 -0
- package/src/daemon/handlers/config-model.ts +62 -26
- package/src/daemon/handlers/conversations.ts +0 -23
- package/src/daemon/handlers/identity.ts +12 -1
- package/src/daemon/handlers/recording.ts +26 -21
- package/src/daemon/host-cu-proxy.ts +2 -2
- package/src/daemon/lifecycle.ts +115 -65
- package/src/daemon/message-protocol.ts +3 -0
- package/src/daemon/message-types/conversations.ts +18 -0
- package/src/daemon/message-types/messages.ts +1 -0
- package/src/daemon/message-types/shared.ts +2 -0
- package/src/daemon/message-types/surfaces.ts +2 -0
- package/src/daemon/message-types/upgrades.ts +23 -0
- package/src/daemon/server.ts +83 -12
- package/src/daemon/shutdown-handlers.ts +8 -5
- package/src/daemon/startup-error.ts +9 -0
- package/src/daemon/tool-side-effects.ts +11 -28
- package/src/events/tool-permission-telemetry-listener.ts +1 -3
- package/src/followups/followup-store.ts +47 -1
- package/src/instrument.ts +0 -4
- package/src/media/app-icon-generator.ts +2 -2
- package/src/memory/app-git-service.ts +28 -16
- package/src/memory/app-store.ts +230 -41
- package/src/memory/archive-store.ts +400 -0
- package/src/memory/attachments-store.ts +558 -130
- package/src/memory/brief-formatting.ts +33 -0
- package/src/memory/brief-open-loops.ts +266 -0
- package/src/memory/brief-time.ts +161 -0
- package/src/memory/brief.ts +75 -0
- package/src/memory/conversation-attention-store.ts +70 -0
- package/src/memory/conversation-crud.ts +591 -8
- package/src/memory/conversation-directories.ts +125 -0
- package/src/memory/conversation-disk-view.ts +390 -0
- package/src/memory/conversation-key-store.ts +17 -5
- package/src/memory/conversation-queries.ts +5 -1
- package/src/memory/conversation-title-service.ts +21 -49
- package/src/memory/db-init.ts +40 -0
- package/src/memory/embedding-backend.ts +42 -53
- package/src/memory/embedding-gemini.test.ts +4 -4
- package/src/memory/embedding-local.ts +1 -3
- package/src/memory/embedding-ollama.ts +1 -3
- package/src/memory/embedding-openai.ts +1 -3
- package/src/memory/indexer.ts +114 -21
- package/src/memory/items-extractor.ts +42 -13
- package/src/memory/job-handlers/conversation-starters.ts +6 -1
- package/src/memory/job-handlers/embedding.test.ts +2 -4
- package/src/memory/job-handlers/embedding.ts +83 -0
- package/src/memory/job-utils.ts +1 -1
- package/src/memory/jobs-store.ts +6 -0
- package/src/memory/jobs-worker.ts +12 -0
- package/src/memory/llm-request-log-store.ts +100 -1
- package/src/memory/migrations/102-alter-table-columns.ts +5 -0
- package/src/memory/migrations/146-schedule-oneshot-routing.ts +3 -3
- package/src/memory/migrations/147-migrate-reminders-to-schedules.ts +66 -70
- package/src/memory/migrations/148-drop-reminders-table.ts +5 -9
- package/src/memory/migrations/160-drop-loopback-port-column.ts +1 -3
- package/src/memory/migrations/174-rename-thread-starters-table.ts +0 -7
- package/src/memory/migrations/178-oauth-providers-managed-service-config-key.ts +15 -0
- package/src/memory/migrations/179-llm-request-log-message-id.ts +16 -0
- package/src/memory/migrations/180-backfill-inline-attachments-to-disk.ts +66 -0
- package/src/memory/migrations/181-rename-thread-starters-checkpoints.ts +46 -0
- package/src/memory/migrations/182-oauth-providers-display-metadata.ts +20 -0
- package/src/memory/migrations/183-add-conversation-fork-lineage.ts +22 -0
- package/src/memory/migrations/184-llm-request-log-provider.ts +12 -0
- package/src/memory/migrations/185-memory-brief-state.ts +52 -0
- package/src/memory/migrations/186-memory-archive.ts +109 -0
- package/src/memory/migrations/187-memory-reducer-checkpoints.ts +19 -0
- package/src/memory/migrations/index.ts +10 -0
- package/src/memory/migrations/registry.ts +13 -0
- package/src/memory/qdrant-client.ts +23 -4
- package/src/memory/reducer-store.ts +271 -0
- package/src/memory/reducer-types.ts +99 -0
- package/src/memory/reducer.ts +453 -0
- package/src/memory/retriever.test.ts +601 -2
- package/src/memory/retriever.ts +85 -9
- package/src/memory/schema/conversations.ts +9 -0
- package/src/memory/schema/index.ts +2 -0
- package/src/memory/schema/infrastructure.ts +13 -7
- package/src/memory/schema/memory-archive.ts +121 -0
- package/src/memory/schema/memory-brief.ts +55 -0
- package/src/memory/schema/oauth.ts +6 -0
- package/src/memory/search/semantic.ts +17 -4
- package/src/messaging/providers/gmail/mime-builder.ts +3 -1
- package/src/notifications/copy-composer.ts +26 -0
- package/src/notifications/decision-engine.ts +14 -1
- package/src/notifications/emit-signal.ts +1 -1
- package/src/notifications/signal.ts +36 -0
- package/src/oauth/byo-connection.test.ts +1 -45
- package/src/oauth/byo-connection.ts +2 -8
- package/src/oauth/connect-orchestrator.ts +15 -11
- package/src/oauth/connection-resolver.test.ts +191 -0
- package/src/oauth/connection-resolver.ts +66 -38
- package/src/oauth/connection.ts +0 -1
- package/src/oauth/oauth-store.ts +99 -47
- package/src/oauth/platform-connection.test.ts +0 -1
- package/src/oauth/platform-connection.ts +11 -3
- package/src/oauth/seed-providers.ts +78 -3
- package/src/oauth/token-persistence.ts +16 -10
- package/src/permissions/checker.ts +160 -14
- package/src/permissions/defaults.ts +14 -0
- package/src/prompts/templates/BOOTSTRAP.md +2 -0
- package/src/providers/anthropic/client.ts +8 -1
- package/src/providers/failover.ts +4 -1
- package/src/providers/gemini/client.ts +50 -0
- package/src/providers/model-catalog.ts +92 -0
- package/src/providers/model-intents.ts +29 -20
- package/src/providers/openai/client.ts +49 -0
- package/src/providers/types.ts +2 -0
- package/src/runtime/access-request-helper.ts +16 -7
- package/src/runtime/auth/credential-service.ts +3 -1
- package/src/runtime/auth/route-policy.ts +14 -1
- package/src/runtime/btw-sidechain.ts +101 -0
- package/src/runtime/channel-reply-delivery.ts +17 -1
- package/src/runtime/http-router.ts +3 -1
- package/src/runtime/http-server.ts +196 -141
- package/src/runtime/http-types.ts +1 -0
- package/src/runtime/migrations/vbundle-builder.ts +5 -1
- package/src/runtime/routes/access-request-decision.ts +41 -0
- package/src/runtime/routes/app-management-routes.ts +6 -3
- package/src/runtime/routes/app-routes.ts +7 -3
- package/src/runtime/routes/approval-routes.ts +1 -0
- package/src/runtime/routes/approval-strategies/guardian-callback-strategy.ts +34 -2
- package/src/runtime/routes/attachment-routes.ts +45 -15
- package/src/runtime/routes/btw-routes.ts +21 -61
- package/src/runtime/routes/conversation-management-routes.ts +74 -0
- package/src/runtime/routes/conversation-query-routes.ts +187 -10
- package/src/runtime/routes/conversation-routes.ts +269 -28
- package/src/runtime/routes/conversation-starter-routes.ts +9 -11
- package/src/runtime/routes/diagnostics-routes.ts +1 -0
- package/src/runtime/routes/identity-routes.ts +2 -35
- package/src/runtime/routes/inbound-stages/acl-enforcement.ts +2 -2
- package/src/runtime/routes/llm-context-normalization.ts +1212 -0
- package/src/runtime/routes/log-export-routes.ts +3 -0
- package/src/runtime/routes/memory-item-routes.test.ts +34 -0
- package/src/runtime/routes/memory-item-routes.ts +94 -5
- package/src/runtime/routes/migration-routes.ts +4 -1
- package/src/runtime/routes/oauth-apps.ts +291 -0
- package/src/runtime/routes/secret-routes.ts +30 -1
- package/src/runtime/routes/settings-routes.ts +14 -0
- package/src/runtime/routes/surface-action-routes.ts +68 -1
- package/src/runtime/routes/trace-event-routes.ts +4 -1
- package/src/schedule/schedule-store.ts +30 -21
- package/src/security/secure-keys.ts +21 -0
- package/src/signals/bash.ts +1 -1
- package/src/skills/inline-command-expansions.ts +204 -0
- package/src/skills/inline-command-render.ts +127 -0
- package/src/skills/inline-command-runner.ts +242 -0
- package/src/skills/transitive-version-hash.ts +88 -0
- package/src/swarm/backend-claude-code.ts +3 -6
- package/src/tasks/task-store.ts +43 -1
- package/src/telemetry/usage-telemetry-reporter.test.ts +3 -2
- package/src/telemetry/usage-telemetry-reporter.ts +3 -1
- package/src/tools/AGENTS.md +6 -10
- package/src/tools/apps/executors.ts +17 -232
- package/src/tools/claude-code/claude-code.ts +2 -3
- package/src/tools/credentials/vault.ts +7 -12
- package/src/tools/host-filesystem/read.ts +13 -10
- package/src/tools/network/__tests__/web-search.test.ts +4 -2
- package/src/tools/permission-checker.ts +8 -1
- package/src/tools/schedule/list.ts +2 -7
- package/src/tools/schema-transforms.ts +5 -0
- package/src/tools/shared/filesystem/format-diff.ts +2 -7
- package/src/tools/skills/execute.ts +1 -1
- package/src/tools/skills/load.ts +140 -6
- package/src/tools/tool-manifest.ts +0 -6
- package/src/tools/ui-surface/definitions.ts +2 -2
- package/src/util/device-id.ts +28 -5
- package/src/util/platform.ts +24 -0
- package/src/util/pricing.ts +1 -0
- package/src/util/retry.ts +1 -3
- package/src/workspace/migrations/003-seed-device-id.ts +3 -4
- package/src/workspace/migrations/006-services-config.ts +5 -0
- package/src/workspace/migrations/008-voice-timeout-and-max-steps.ts +12 -0
- package/src/workspace/migrations/009-backfill-conversation-disk-view.ts +10 -0
- package/src/workspace/migrations/010-app-dir-rename.ts +223 -0
- package/src/workspace/migrations/{002-backfill-installation-id.ts → 011-backfill-installation-id.ts} +24 -13
- package/src/workspace/migrations/012-rename-conversation-disk-view-dirs.ts +64 -0
- package/src/workspace/migrations/013-repair-conversation-disk-view.ts +11 -0
- package/src/workspace/migrations/rebuild-conversation-disk-view.ts +186 -0
- package/src/workspace/migrations/registry.ts +11 -1
- package/src/workspace/top-level-renderer.ts +12 -0
- package/src/__tests__/asset-materialize-tool.test.ts +0 -523
- package/src/__tests__/asset-search-tool.test.ts +0 -536
- package/src/__tests__/fixtures/media-reuse-fixtures.ts +0 -56
- package/src/__tests__/media-reuse-story.e2e.test.ts +0 -762
- package/src/__tests__/media-visibility-policy.test.ts +0 -190
- package/src/config/bundled-skills/app-builder/tools/app-file-edit.ts +0 -14
- package/src/config/bundled-skills/app-builder/tools/app-file-list.ts +0 -13
- package/src/config/bundled-skills/app-builder/tools/app-file-read.ts +0 -21
- package/src/config/bundled-skills/app-builder/tools/app-file-write.ts +0 -14
- package/src/config/bundled-skills/app-builder/tools/app-list.ts +0 -13
- package/src/config/bundled-skills/app-builder/tools/app-update.ts +0 -23
- package/src/daemon/media-visibility-policy.ts +0 -59
- package/src/tools/assets/materialize.ts +0 -248
- package/src/tools/assets/search.ts +0 -400
|
@@ -0,0 +1,1212 @@
|
|
|
1
|
+
export interface LlmContextNormalizationInput {
|
|
2
|
+
requestPayload: unknown;
|
|
3
|
+
responsePayload: unknown;
|
|
4
|
+
createdAt: number;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface LlmContextSummary {
|
|
8
|
+
provider: "openai" | "anthropic" | "gemini";
|
|
9
|
+
model?: string;
|
|
10
|
+
inputTokens?: number;
|
|
11
|
+
outputTokens?: number;
|
|
12
|
+
cacheCreationInputTokens?: number;
|
|
13
|
+
cacheReadInputTokens?: number;
|
|
14
|
+
stopReason?: string;
|
|
15
|
+
requestMessageCount?: number;
|
|
16
|
+
requestToolCount?: number;
|
|
17
|
+
responseMessageCount?: number;
|
|
18
|
+
responseToolCallCount?: number;
|
|
19
|
+
responsePreview?: string;
|
|
20
|
+
toolCallNames?: string[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface LlmContextSection {
|
|
24
|
+
kind:
|
|
25
|
+
| "system"
|
|
26
|
+
| "message"
|
|
27
|
+
| "reasoning"
|
|
28
|
+
| "settings"
|
|
29
|
+
| "tool_definitions"
|
|
30
|
+
| "tool_use"
|
|
31
|
+
| "tool_result"
|
|
32
|
+
| "function_call"
|
|
33
|
+
| "function_response";
|
|
34
|
+
label: string;
|
|
35
|
+
role?: string;
|
|
36
|
+
text?: string;
|
|
37
|
+
toolName?: string;
|
|
38
|
+
data?: unknown;
|
|
39
|
+
language?: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface LlmContextNormalizationResult {
|
|
43
|
+
summary?: LlmContextSummary;
|
|
44
|
+
requestSections?: LlmContextSection[];
|
|
45
|
+
responseSections?: LlmContextSection[];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
interface NormalizedPayloadCandidate {
|
|
49
|
+
provider: LlmContextSummary["provider"];
|
|
50
|
+
summary: LlmContextSummary;
|
|
51
|
+
requestSections?: LlmContextSection[];
|
|
52
|
+
responseSections?: LlmContextSection[];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function normalizeLlmContextPayloads(
|
|
56
|
+
input: LlmContextNormalizationInput,
|
|
57
|
+
): LlmContextNormalizationResult {
|
|
58
|
+
const requestCandidates = [
|
|
59
|
+
normalizeOpenAiRequestPayload(input.requestPayload),
|
|
60
|
+
normalizeAnthropicRequestPayload(input.requestPayload),
|
|
61
|
+
normalizeGeminiRequestPayload(input.requestPayload),
|
|
62
|
+
].filter((candidate): candidate is NormalizedPayloadCandidate =>
|
|
63
|
+
Boolean(candidate),
|
|
64
|
+
);
|
|
65
|
+
const responseCandidates = [
|
|
66
|
+
normalizeOpenAiResponsePayload(input.responsePayload),
|
|
67
|
+
normalizeAnthropicResponsePayload(input.responsePayload),
|
|
68
|
+
normalizeGeminiResponsePayload(input.responsePayload),
|
|
69
|
+
].filter((candidate): candidate is NormalizedPayloadCandidate =>
|
|
70
|
+
Boolean(candidate),
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
if (requestCandidates.length > 1 || responseCandidates.length > 1) {
|
|
74
|
+
return {};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const requestCandidate = requestCandidates[0];
|
|
78
|
+
const responseCandidate = responseCandidates[0];
|
|
79
|
+
|
|
80
|
+
if (requestCandidate && responseCandidate) {
|
|
81
|
+
if (requestCandidate.provider !== responseCandidate.provider) {
|
|
82
|
+
return {};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return mergeNormalizedCandidates(requestCandidate, responseCandidate);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (requestCandidate) {
|
|
89
|
+
const { summary, requestSections, responseSections } = requestCandidate;
|
|
90
|
+
return { summary, requestSections, responseSections };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (responseCandidate) {
|
|
94
|
+
const requestCandidate = normalizeCompatibleRequestPayload(
|
|
95
|
+
input.requestPayload,
|
|
96
|
+
responseCandidate.provider,
|
|
97
|
+
);
|
|
98
|
+
if (
|
|
99
|
+
requestCandidate &&
|
|
100
|
+
requestCandidate.provider !== responseCandidate.provider
|
|
101
|
+
) {
|
|
102
|
+
return {};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return mergeNormalizedCandidates(requestCandidate, responseCandidate);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return {};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function normalizeOpenAiRequestPayload(
|
|
112
|
+
requestPayload: unknown,
|
|
113
|
+
allowPlainText = false,
|
|
114
|
+
): NormalizedPayloadCandidate | null {
|
|
115
|
+
const request = asRecord(requestPayload);
|
|
116
|
+
if (!request) {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const messages = asRecordArray(request.messages);
|
|
121
|
+
if (!messages) {
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const requestToolNames = extractOpenAiRequestToolNames(request.tools);
|
|
126
|
+
const hasOpenAiSignal =
|
|
127
|
+
hasOpenAiModelPrefix(asString(request.model)) ||
|
|
128
|
+
requestToolNames.length > 0 ||
|
|
129
|
+
asString(request.tool_choice) !== undefined ||
|
|
130
|
+
(request.parallel_tool_calls !== undefined &&
|
|
131
|
+
typeof request.parallel_tool_calls === "boolean") ||
|
|
132
|
+
messages.some((message) => Boolean(asRecordArray(message.tool_calls)));
|
|
133
|
+
if (!allowPlainText && !hasOpenAiSignal) {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const requestSections: LlmContextSection[] = [];
|
|
138
|
+
for (const [index, message] of messages.entries()) {
|
|
139
|
+
const role = asString(message.role) ?? "unknown";
|
|
140
|
+
const messageText = extractOpenAiContentText(message.content);
|
|
141
|
+
if (messageText !== undefined) {
|
|
142
|
+
requestSections.push({
|
|
143
|
+
kind:
|
|
144
|
+
role === "system"
|
|
145
|
+
? "system"
|
|
146
|
+
: role === "tool"
|
|
147
|
+
? "tool_result"
|
|
148
|
+
: "message",
|
|
149
|
+
label: buildMessageLabel(role, index + 1),
|
|
150
|
+
role,
|
|
151
|
+
text: messageText,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
for (const toolCallSection of openAiToolCallSections(
|
|
156
|
+
message.tool_calls,
|
|
157
|
+
"Request tool call",
|
|
158
|
+
)) {
|
|
159
|
+
requestSections.push(toolCallSection);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (requestToolNames.length > 0) {
|
|
164
|
+
requestSections.push({
|
|
165
|
+
kind: "tool_definitions",
|
|
166
|
+
label: "Available tools",
|
|
167
|
+
data: {
|
|
168
|
+
tools: asRecordArray(request.tools) ?? request.tools,
|
|
169
|
+
},
|
|
170
|
+
language: "json",
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const requestSettings = omitRecordKeys(request, ["messages", "tools"]);
|
|
175
|
+
if (hasMeaningfulRequestSettings(requestSettings)) {
|
|
176
|
+
requestSections.push(
|
|
177
|
+
structuredJsonSection("settings", "Request settings", requestSettings),
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return {
|
|
182
|
+
provider: "openai",
|
|
183
|
+
summary: {
|
|
184
|
+
provider: "openai",
|
|
185
|
+
model: asString(request.model),
|
|
186
|
+
inputTokens: undefined,
|
|
187
|
+
outputTokens: undefined,
|
|
188
|
+
cacheCreationInputTokens: undefined,
|
|
189
|
+
cacheReadInputTokens: undefined,
|
|
190
|
+
stopReason: undefined,
|
|
191
|
+
requestMessageCount: messages.length,
|
|
192
|
+
requestToolCount: requestToolNames.length,
|
|
193
|
+
responseMessageCount: undefined,
|
|
194
|
+
responseToolCallCount: undefined,
|
|
195
|
+
responsePreview: undefined,
|
|
196
|
+
toolCallNames: undefined,
|
|
197
|
+
},
|
|
198
|
+
requestSections: requestSections.length > 0 ? requestSections : undefined,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function normalizeOpenAiResponsePayload(
|
|
203
|
+
responsePayload: unknown,
|
|
204
|
+
): NormalizedPayloadCandidate | null {
|
|
205
|
+
const response = asRecord(responsePayload);
|
|
206
|
+
if (!response) {
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const choices = asRecordArray(response.choices);
|
|
211
|
+
if (!choices) {
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const firstChoice = choices[0];
|
|
216
|
+
const responseMessage = asRecord(firstChoice?.message);
|
|
217
|
+
const responseText = extractOpenAiContentText(responseMessage?.content);
|
|
218
|
+
const responseSections: LlmContextSection[] = [];
|
|
219
|
+
if (responseText !== undefined) {
|
|
220
|
+
responseSections.push({
|
|
221
|
+
kind: "message",
|
|
222
|
+
label: "Assistant response",
|
|
223
|
+
role: asString(responseMessage?.role) ?? "assistant",
|
|
224
|
+
text: responseText,
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
const responseToolSections = openAiToolCallSections(
|
|
228
|
+
responseMessage?.tool_calls,
|
|
229
|
+
"Response tool call",
|
|
230
|
+
);
|
|
231
|
+
responseSections.push(...responseToolSections);
|
|
232
|
+
|
|
233
|
+
const usage = asRecord(response.usage);
|
|
234
|
+
const toolCallNames = responseToolSections
|
|
235
|
+
.map((section) => section.toolName)
|
|
236
|
+
.filter((name): name is string => typeof name === "string");
|
|
237
|
+
|
|
238
|
+
return {
|
|
239
|
+
provider: "openai",
|
|
240
|
+
summary: {
|
|
241
|
+
provider: "openai",
|
|
242
|
+
model: asString(response.model),
|
|
243
|
+
inputTokens: asNumber(usage?.prompt_tokens),
|
|
244
|
+
outputTokens: asNumber(usage?.completion_tokens),
|
|
245
|
+
cacheCreationInputTokens: undefined,
|
|
246
|
+
cacheReadInputTokens: undefined,
|
|
247
|
+
stopReason: asString(firstChoice?.finish_reason),
|
|
248
|
+
requestMessageCount: undefined,
|
|
249
|
+
requestToolCount: undefined,
|
|
250
|
+
responseMessageCount:
|
|
251
|
+
responseText !== undefined || responseToolSections.length > 0
|
|
252
|
+
? 1
|
|
253
|
+
: undefined,
|
|
254
|
+
responseToolCallCount:
|
|
255
|
+
responseToolSections.length > 0
|
|
256
|
+
? responseToolSections.length
|
|
257
|
+
: undefined,
|
|
258
|
+
responsePreview: responseText ? truncateText(responseText) : undefined,
|
|
259
|
+
toolCallNames: toolCallNames.length > 0 ? toolCallNames : undefined,
|
|
260
|
+
},
|
|
261
|
+
responseSections:
|
|
262
|
+
responseSections.length > 0 ? responseSections : undefined,
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function normalizeAnthropicRequestPayload(
|
|
267
|
+
requestPayload: unknown,
|
|
268
|
+
allowPlainText = false,
|
|
269
|
+
): NormalizedPayloadCandidate | null {
|
|
270
|
+
const request = asRecord(requestPayload);
|
|
271
|
+
if (!request) {
|
|
272
|
+
return null;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const messages = asRecordArray(request.messages);
|
|
276
|
+
if (!messages) {
|
|
277
|
+
return null;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const requestToolNames = extractAnthropicToolNames(request.tools);
|
|
281
|
+
const hasAnthropicContentSignal = messages.some((message) =>
|
|
282
|
+
(asRecordArray(message.content) ?? []).some((block) => {
|
|
283
|
+
const type = asString(block.type);
|
|
284
|
+
return (
|
|
285
|
+
type === "document" ||
|
|
286
|
+
type === "tool_use" ||
|
|
287
|
+
type === "server_tool_use" ||
|
|
288
|
+
type === "tool_result" ||
|
|
289
|
+
type === "web_search_tool_result" ||
|
|
290
|
+
type === "thinking" ||
|
|
291
|
+
type === "redacted_thinking"
|
|
292
|
+
);
|
|
293
|
+
}),
|
|
294
|
+
);
|
|
295
|
+
const hasAnthropicSignal =
|
|
296
|
+
hasAnthropicModelPrefix(asString(request.model)) ||
|
|
297
|
+
request.system !== undefined ||
|
|
298
|
+
requestToolNames.length > 0 ||
|
|
299
|
+
isAnthropicToolChoice(request.tool_choice) ||
|
|
300
|
+
hasAnthropicContentSignal;
|
|
301
|
+
if (!allowPlainText && !hasAnthropicSignal) {
|
|
302
|
+
return null;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const requestSections: LlmContextSection[] = [];
|
|
306
|
+
const systemSections = anthropicSystemSections(request.system);
|
|
307
|
+
requestSections.push(...systemSections);
|
|
308
|
+
|
|
309
|
+
for (const [index, message] of messages.entries()) {
|
|
310
|
+
requestSections.push(
|
|
311
|
+
...anthropicMessageSections(
|
|
312
|
+
message,
|
|
313
|
+
buildMessageLabel(asString(message.role) ?? "unknown", index + 1),
|
|
314
|
+
),
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (requestToolNames.length > 0) {
|
|
319
|
+
requestSections.push({
|
|
320
|
+
kind: "tool_definitions",
|
|
321
|
+
label: "Available tools",
|
|
322
|
+
data: {
|
|
323
|
+
tools: asRecordArray(request.tools) ?? request.tools,
|
|
324
|
+
},
|
|
325
|
+
language: "json",
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const requestSettings = omitRecordKeys(request, [
|
|
330
|
+
"system",
|
|
331
|
+
"messages",
|
|
332
|
+
"tools",
|
|
333
|
+
]);
|
|
334
|
+
if (hasMeaningfulRequestSettings(requestSettings)) {
|
|
335
|
+
requestSections.push(
|
|
336
|
+
structuredJsonSection("settings", "Request settings", requestSettings),
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
return {
|
|
341
|
+
provider: "anthropic",
|
|
342
|
+
summary: {
|
|
343
|
+
provider: "anthropic",
|
|
344
|
+
model: asString(request.model),
|
|
345
|
+
inputTokens: undefined,
|
|
346
|
+
outputTokens: undefined,
|
|
347
|
+
cacheCreationInputTokens: undefined,
|
|
348
|
+
cacheReadInputTokens: undefined,
|
|
349
|
+
stopReason: undefined,
|
|
350
|
+
requestMessageCount: messages.length,
|
|
351
|
+
requestToolCount: requestToolNames.length,
|
|
352
|
+
responseMessageCount: undefined,
|
|
353
|
+
responseToolCallCount: undefined,
|
|
354
|
+
responsePreview: undefined,
|
|
355
|
+
toolCallNames: undefined,
|
|
356
|
+
},
|
|
357
|
+
requestSections: requestSections.length > 0 ? requestSections : undefined,
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function normalizeAnthropicResponsePayload(
|
|
362
|
+
responsePayload: unknown,
|
|
363
|
+
): NormalizedPayloadCandidate | null {
|
|
364
|
+
const response = asRecord(responsePayload);
|
|
365
|
+
if (!response) {
|
|
366
|
+
return null;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
const content = asRecordArray(response.content);
|
|
370
|
+
if (!content) {
|
|
371
|
+
return null;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const responseSections = anthropicContentSections(
|
|
375
|
+
content,
|
|
376
|
+
"Assistant response",
|
|
377
|
+
);
|
|
378
|
+
const responseText = collectAnthropicPreviewText(content);
|
|
379
|
+
const responseToolNames = content
|
|
380
|
+
.map((block) =>
|
|
381
|
+
isAnthropicToolUseType(asString(block.type))
|
|
382
|
+
? asString(block.name)
|
|
383
|
+
: undefined,
|
|
384
|
+
)
|
|
385
|
+
.filter((name): name is string => typeof name === "string");
|
|
386
|
+
const hasAnthropicResponseMessage = responseSections.some(
|
|
387
|
+
(section) =>
|
|
388
|
+
section.kind === "message" ||
|
|
389
|
+
section.kind === "tool_use" ||
|
|
390
|
+
section.kind === "reasoning",
|
|
391
|
+
);
|
|
392
|
+
|
|
393
|
+
const usage = asRecord(response.usage);
|
|
394
|
+
return {
|
|
395
|
+
provider: "anthropic",
|
|
396
|
+
summary: {
|
|
397
|
+
provider: "anthropic",
|
|
398
|
+
model: asString(response.model),
|
|
399
|
+
inputTokens: asNumber(usage?.input_tokens),
|
|
400
|
+
outputTokens: asNumber(usage?.output_tokens),
|
|
401
|
+
cacheCreationInputTokens: asNumber(usage?.cache_creation_input_tokens),
|
|
402
|
+
cacheReadInputTokens: asNumber(usage?.cache_read_input_tokens),
|
|
403
|
+
stopReason: asString(response.stop_reason),
|
|
404
|
+
requestMessageCount: undefined,
|
|
405
|
+
requestToolCount: undefined,
|
|
406
|
+
responseMessageCount: hasAnthropicResponseMessage ? 1 : undefined,
|
|
407
|
+
responseToolCallCount:
|
|
408
|
+
responseToolNames.length > 0 ? responseToolNames.length : undefined,
|
|
409
|
+
responsePreview: responseText ? truncateText(responseText) : undefined,
|
|
410
|
+
toolCallNames:
|
|
411
|
+
responseToolNames.length > 0 ? responseToolNames : undefined,
|
|
412
|
+
},
|
|
413
|
+
responseSections:
|
|
414
|
+
responseSections.length > 0 ? responseSections : undefined,
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
function normalizeGeminiRequestPayload(
|
|
419
|
+
requestPayload: unknown,
|
|
420
|
+
): NormalizedPayloadCandidate | null {
|
|
421
|
+
const request = asRecord(requestPayload);
|
|
422
|
+
if (!request) {
|
|
423
|
+
return null;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
const contents = asRecordArray(request.contents);
|
|
427
|
+
if (!contents) {
|
|
428
|
+
return null;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
const requestSections: LlmContextSection[] = [];
|
|
432
|
+
const config = asRecord(request.config);
|
|
433
|
+
const systemText = extractGeminiSystemInstructionText(
|
|
434
|
+
config?.systemInstruction,
|
|
435
|
+
);
|
|
436
|
+
if (systemText !== undefined) {
|
|
437
|
+
requestSections.push({
|
|
438
|
+
kind: "system",
|
|
439
|
+
label: "System instruction",
|
|
440
|
+
role: "system",
|
|
441
|
+
text: systemText,
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
for (const [index, content] of contents.entries()) {
|
|
446
|
+
requestSections.push(...geminiContentSections(content, index + 1));
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
const requestToolNames = extractGeminiToolNames(config?.tools);
|
|
450
|
+
if (requestToolNames.length > 0) {
|
|
451
|
+
requestSections.push({
|
|
452
|
+
kind: "tool_definitions",
|
|
453
|
+
label: "Available tools",
|
|
454
|
+
data: {
|
|
455
|
+
tools: asRecordArray(config?.tools) ?? config?.tools,
|
|
456
|
+
},
|
|
457
|
+
language: "json",
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
const requestSettings = buildGeminiRequestSettings(request, config);
|
|
462
|
+
if (hasMeaningfulRequestSettings(requestSettings)) {
|
|
463
|
+
requestSections.push(
|
|
464
|
+
structuredJsonSection("settings", "Generation config", requestSettings),
|
|
465
|
+
);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
return {
|
|
469
|
+
provider: "gemini",
|
|
470
|
+
summary: {
|
|
471
|
+
provider: "gemini",
|
|
472
|
+
model: asString(request.model),
|
|
473
|
+
inputTokens: undefined,
|
|
474
|
+
outputTokens: undefined,
|
|
475
|
+
cacheCreationInputTokens: undefined,
|
|
476
|
+
cacheReadInputTokens: undefined,
|
|
477
|
+
stopReason: undefined,
|
|
478
|
+
requestMessageCount: contents.length,
|
|
479
|
+
requestToolCount: requestToolNames.length,
|
|
480
|
+
responseMessageCount: undefined,
|
|
481
|
+
responseToolCallCount: undefined,
|
|
482
|
+
responsePreview: undefined,
|
|
483
|
+
toolCallNames: undefined,
|
|
484
|
+
},
|
|
485
|
+
requestSections: requestSections.length > 0 ? requestSections : undefined,
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
function normalizeGeminiResponsePayload(
|
|
490
|
+
responsePayload: unknown,
|
|
491
|
+
): NormalizedPayloadCandidate | null {
|
|
492
|
+
const response = asRecord(responsePayload);
|
|
493
|
+
if (!response) {
|
|
494
|
+
return null;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
const responseText = asString(response.text);
|
|
498
|
+
const responseFunctionSections = geminiFunctionCallSections(
|
|
499
|
+
response.functionCalls,
|
|
500
|
+
"Response function call",
|
|
501
|
+
);
|
|
502
|
+
const usage = asRecord(response.usageMetadata);
|
|
503
|
+
if (responseText === undefined && responseFunctionSections.length === 0) {
|
|
504
|
+
return null;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
const responseSections: LlmContextSection[] = [];
|
|
508
|
+
if (responseText !== undefined) {
|
|
509
|
+
responseSections.push({
|
|
510
|
+
kind: "message",
|
|
511
|
+
label: "Assistant response",
|
|
512
|
+
role: "model",
|
|
513
|
+
text: responseText,
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
responseSections.push(...responseFunctionSections);
|
|
517
|
+
|
|
518
|
+
const toolCallNames = responseFunctionSections
|
|
519
|
+
.map((section) => section.toolName)
|
|
520
|
+
.filter((name): name is string => typeof name === "string");
|
|
521
|
+
|
|
522
|
+
return {
|
|
523
|
+
provider: "gemini",
|
|
524
|
+
summary: {
|
|
525
|
+
provider: "gemini",
|
|
526
|
+
model: asString(response.model),
|
|
527
|
+
inputTokens: asNumber(usage?.promptTokenCount),
|
|
528
|
+
outputTokens: asNumber(usage?.candidatesTokenCount),
|
|
529
|
+
cacheCreationInputTokens: undefined,
|
|
530
|
+
cacheReadInputTokens: undefined,
|
|
531
|
+
stopReason: asString(response.finishReason),
|
|
532
|
+
requestMessageCount: undefined,
|
|
533
|
+
requestToolCount: undefined,
|
|
534
|
+
responseMessageCount:
|
|
535
|
+
responseText !== undefined || responseFunctionSections.length > 0
|
|
536
|
+
? 1
|
|
537
|
+
: undefined,
|
|
538
|
+
responseToolCallCount:
|
|
539
|
+
responseFunctionSections.length > 0
|
|
540
|
+
? responseFunctionSections.length
|
|
541
|
+
: undefined,
|
|
542
|
+
responsePreview: responseText ? truncateText(responseText) : undefined,
|
|
543
|
+
toolCallNames: toolCallNames.length > 0 ? toolCallNames : undefined,
|
|
544
|
+
},
|
|
545
|
+
responseSections:
|
|
546
|
+
responseSections.length > 0 ? responseSections : undefined,
|
|
547
|
+
};
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
function anthropicSystemSections(system: unknown): LlmContextSection[] {
|
|
551
|
+
const text = extractAnthropicSystemText(system);
|
|
552
|
+
if (!text) {
|
|
553
|
+
return [];
|
|
554
|
+
}
|
|
555
|
+
return [
|
|
556
|
+
{
|
|
557
|
+
kind: "system",
|
|
558
|
+
label: "System prompt",
|
|
559
|
+
role: "system",
|
|
560
|
+
text,
|
|
561
|
+
},
|
|
562
|
+
];
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
function anthropicMessageSections(
|
|
566
|
+
message: Record<string, unknown>,
|
|
567
|
+
label: string,
|
|
568
|
+
): LlmContextSection[] {
|
|
569
|
+
const role = asString(message.role) ?? "unknown";
|
|
570
|
+
const content = message.content;
|
|
571
|
+
const sections: LlmContextSection[] = [];
|
|
572
|
+
const text = collectAnthropicMessageText(content);
|
|
573
|
+
if (text) {
|
|
574
|
+
sections.push({
|
|
575
|
+
kind: "message",
|
|
576
|
+
label,
|
|
577
|
+
role,
|
|
578
|
+
text,
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
for (const block of asRecordArray(content) ?? []) {
|
|
583
|
+
const type = asString(block.type);
|
|
584
|
+
if (type === "thinking" || type === "redacted_thinking") {
|
|
585
|
+
sections.push({
|
|
586
|
+
kind: "reasoning",
|
|
587
|
+
label: `${label} reasoning`,
|
|
588
|
+
role,
|
|
589
|
+
text: collectAnthropicReasoningText(block),
|
|
590
|
+
});
|
|
591
|
+
continue;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
if (isAnthropicToolUseType(type)) {
|
|
595
|
+
sections.push({
|
|
596
|
+
kind: "tool_use",
|
|
597
|
+
label: `${label} tool use`,
|
|
598
|
+
role,
|
|
599
|
+
toolName: asString(block.name),
|
|
600
|
+
data: asRecord(block.input) ?? block.input,
|
|
601
|
+
text: previewStructuredValue(block.input),
|
|
602
|
+
});
|
|
603
|
+
continue;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
if (isAnthropicToolResultType(type)) {
|
|
607
|
+
sections.push({
|
|
608
|
+
kind: "tool_result",
|
|
609
|
+
label: `${label} tool result`,
|
|
610
|
+
role,
|
|
611
|
+
toolName: asString(block.name) ?? asString(block.tool_use_id),
|
|
612
|
+
data:
|
|
613
|
+
type === "web_search_tool_result"
|
|
614
|
+
? sanitizeAnthropicWebSearchToolResultData(block)
|
|
615
|
+
: undefined,
|
|
616
|
+
text: collectAnthropicToolResultText(block),
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
return sections;
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
function anthropicContentSections(
|
|
625
|
+
content: Record<string, unknown>[],
|
|
626
|
+
label: string,
|
|
627
|
+
): LlmContextSection[] {
|
|
628
|
+
return anthropicMessageSections(
|
|
629
|
+
{
|
|
630
|
+
role: "assistant",
|
|
631
|
+
content,
|
|
632
|
+
},
|
|
633
|
+
label,
|
|
634
|
+
);
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
function geminiContentSections(
|
|
638
|
+
content: Record<string, unknown>,
|
|
639
|
+
index: number,
|
|
640
|
+
): LlmContextSection[] {
|
|
641
|
+
const role = asString(content.role) ?? "unknown";
|
|
642
|
+
const parts = asRecordArray(content.parts) ?? [];
|
|
643
|
+
const sections: LlmContextSection[] = [];
|
|
644
|
+
const textParts: string[] = [];
|
|
645
|
+
|
|
646
|
+
for (const part of parts) {
|
|
647
|
+
const text = asString(part.text);
|
|
648
|
+
if (text) {
|
|
649
|
+
textParts.push(text);
|
|
650
|
+
continue;
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
const inlineData = asRecord(part.inlineData);
|
|
654
|
+
if (inlineData) {
|
|
655
|
+
const mimeType =
|
|
656
|
+
asString(inlineData.mimeType) ?? "application/octet-stream";
|
|
657
|
+
textParts.push(`[inline data: ${mimeType}]`);
|
|
658
|
+
continue;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
const functionCall = asRecord(part.functionCall);
|
|
662
|
+
if (functionCall) {
|
|
663
|
+
sections.push({
|
|
664
|
+
kind: "function_call",
|
|
665
|
+
label: `${buildMessageLabel(role, index)} function call`,
|
|
666
|
+
role,
|
|
667
|
+
toolName: asString(functionCall.name),
|
|
668
|
+
data: asRecord(functionCall.args) ?? functionCall.args,
|
|
669
|
+
text: previewStructuredValue(functionCall.args),
|
|
670
|
+
});
|
|
671
|
+
continue;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
const functionResponse = asRecord(part.functionResponse);
|
|
675
|
+
if (functionResponse) {
|
|
676
|
+
sections.push({
|
|
677
|
+
kind: "function_response",
|
|
678
|
+
label: `${buildMessageLabel(role, index)} function response`,
|
|
679
|
+
role,
|
|
680
|
+
toolName: asString(functionResponse.name),
|
|
681
|
+
data: asRecord(functionResponse.response) ?? functionResponse.response,
|
|
682
|
+
text: previewStructuredValue(functionResponse.response),
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
const text = joinTextParts(textParts);
|
|
688
|
+
if (text) {
|
|
689
|
+
sections.unshift({
|
|
690
|
+
kind: "message",
|
|
691
|
+
label: buildMessageLabel(role, index),
|
|
692
|
+
role,
|
|
693
|
+
text,
|
|
694
|
+
});
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
return sections;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
function openAiToolCallSections(
|
|
701
|
+
toolCalls: unknown,
|
|
702
|
+
labelPrefix: string,
|
|
703
|
+
): LlmContextSection[] {
|
|
704
|
+
return (asRecordArray(toolCalls) ?? []).map((toolCall, index) => {
|
|
705
|
+
const fn = asRecord(toolCall.function);
|
|
706
|
+
return {
|
|
707
|
+
kind: "function_call",
|
|
708
|
+
label: `${labelPrefix} ${index + 1}`,
|
|
709
|
+
role: "assistant",
|
|
710
|
+
toolName: asString(fn?.name),
|
|
711
|
+
data: parseJsonValue(asString(fn?.arguments)),
|
|
712
|
+
text: previewStructuredValue(parseJsonValue(asString(fn?.arguments))),
|
|
713
|
+
};
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
function geminiFunctionCallSections(
|
|
718
|
+
functionCalls: unknown,
|
|
719
|
+
labelPrefix: string,
|
|
720
|
+
): LlmContextSection[] {
|
|
721
|
+
return (asRecordArray(functionCalls) ?? []).map((call, index) => ({
|
|
722
|
+
kind: "function_call",
|
|
723
|
+
label: `${labelPrefix} ${index + 1}`,
|
|
724
|
+
role: "model",
|
|
725
|
+
toolName: asString(call.name),
|
|
726
|
+
data: asRecord(call.args) ?? call.args,
|
|
727
|
+
text: previewStructuredValue(call.args),
|
|
728
|
+
}));
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
function extractOpenAiRequestToolNames(tools: unknown): string[] {
|
|
732
|
+
return (asRecordArray(tools) ?? [])
|
|
733
|
+
.map((tool) => asString(asRecord(tool.function)?.name))
|
|
734
|
+
.filter((name): name is string => typeof name === "string");
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
function extractAnthropicToolNames(tools: unknown): string[] {
|
|
738
|
+
return (asRecordArray(tools) ?? [])
|
|
739
|
+
.map((tool) => asString(tool.name))
|
|
740
|
+
.filter((name): name is string => typeof name === "string");
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
function isAnthropicToolChoice(toolChoice: unknown): boolean {
|
|
744
|
+
const record = asRecord(toolChoice);
|
|
745
|
+
if (!record) {
|
|
746
|
+
return false;
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
const type = asString(record.type);
|
|
750
|
+
return (
|
|
751
|
+
type === "auto" || type === "any" || type === "tool" || type === "none"
|
|
752
|
+
);
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
function extractGeminiToolNames(tools: unknown): string[] {
|
|
756
|
+
const toolGroups = asRecordArray(tools) ?? [];
|
|
757
|
+
const names: string[] = [];
|
|
758
|
+
for (const toolGroup of toolGroups) {
|
|
759
|
+
for (const declaration of asRecordArray(toolGroup.functionDeclarations) ??
|
|
760
|
+
[]) {
|
|
761
|
+
const name = asString(declaration.name);
|
|
762
|
+
if (name) {
|
|
763
|
+
names.push(name);
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
return names;
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
function extractOpenAiContentText(content: unknown): string | undefined {
|
|
771
|
+
if (typeof content === "string") {
|
|
772
|
+
return hasMeaningfulText(content) ? content : undefined;
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
const parts = asRecordArray(content);
|
|
776
|
+
if (!parts) {
|
|
777
|
+
return undefined;
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
const textParts: string[] = [];
|
|
781
|
+
for (const part of parts) {
|
|
782
|
+
const type = asString(part.type);
|
|
783
|
+
if (type === "text" || type === "input_text" || type === "output_text") {
|
|
784
|
+
const text = asString(part.text);
|
|
785
|
+
if (text) {
|
|
786
|
+
textParts.push(text);
|
|
787
|
+
}
|
|
788
|
+
continue;
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
if (type === "image_url" || type === "input_image") {
|
|
792
|
+
textParts.push("[image]");
|
|
793
|
+
continue;
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
if (type === "file") {
|
|
797
|
+
textParts.push("[file]");
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
return joinTextParts(textParts);
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
function extractAnthropicSystemText(system: unknown): string | undefined {
|
|
805
|
+
if (typeof system === "string") {
|
|
806
|
+
return hasMeaningfulText(system) ? system : undefined;
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
const parts = asRecordArray(system);
|
|
810
|
+
if (!parts) {
|
|
811
|
+
return undefined;
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
const textParts = parts
|
|
815
|
+
.map((part) => asString(part.text))
|
|
816
|
+
.filter((text): text is string => typeof text === "string");
|
|
817
|
+
return joinTextParts(textParts);
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
function extractGeminiSystemInstructionText(
|
|
821
|
+
systemInstruction: unknown,
|
|
822
|
+
): string | undefined {
|
|
823
|
+
if (typeof systemInstruction === "string") {
|
|
824
|
+
return hasMeaningfulText(systemInstruction) ? systemInstruction : undefined;
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
const record = asRecord(systemInstruction);
|
|
828
|
+
if (!record) {
|
|
829
|
+
return undefined;
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
const parts = asRecordArray(record.parts) ?? [];
|
|
833
|
+
const textParts = parts
|
|
834
|
+
.map((part) => asString(part.text))
|
|
835
|
+
.filter((text): text is string => typeof text === "string");
|
|
836
|
+
return joinTextParts(textParts);
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
function collectAnthropicText(content: unknown): string | undefined {
|
|
840
|
+
if (typeof content === "string") {
|
|
841
|
+
return hasMeaningfulText(content) ? content : undefined;
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
const blocks = asRecordArray(content);
|
|
845
|
+
if (!blocks) {
|
|
846
|
+
return undefined;
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
const textParts: string[] = [];
|
|
850
|
+
for (const block of blocks) {
|
|
851
|
+
const text = collectAnthropicBlockText(block);
|
|
852
|
+
if (text) {
|
|
853
|
+
textParts.push(text);
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
return joinTextParts(textParts);
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
function collectAnthropicMessageText(content: unknown): string | undefined {
|
|
861
|
+
if (typeof content === "string") {
|
|
862
|
+
return hasMeaningfulText(content) ? content : undefined;
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
const blocks = asRecordArray(content);
|
|
866
|
+
if (!blocks) {
|
|
867
|
+
return undefined;
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
const textParts: string[] = [];
|
|
871
|
+
for (const block of blocks) {
|
|
872
|
+
const text = collectAnthropicBlockText(block);
|
|
873
|
+
if (text) {
|
|
874
|
+
textParts.push(text);
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
return joinTextParts(textParts);
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
function collectAnthropicPreviewText(content: unknown): string | undefined {
|
|
882
|
+
if (typeof content === "string") {
|
|
883
|
+
return hasMeaningfulText(content) ? content : undefined;
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
const blocks = asRecordArray(content);
|
|
887
|
+
if (!blocks) {
|
|
888
|
+
return undefined;
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
const textParts: string[] = [];
|
|
892
|
+
for (const block of blocks) {
|
|
893
|
+
if (asString(block.type) !== "text") {
|
|
894
|
+
continue;
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
const text = asString(block.text);
|
|
898
|
+
if (text) {
|
|
899
|
+
textParts.push(text);
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
return joinTextParts(textParts);
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
function collectAnthropicBlockText(
|
|
907
|
+
block: Record<string, unknown>,
|
|
908
|
+
): string | undefined {
|
|
909
|
+
const type = asString(block.type);
|
|
910
|
+
if (type === "text") {
|
|
911
|
+
const text = asString(block.text);
|
|
912
|
+
return text ? text : undefined;
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
if (type === "image") {
|
|
916
|
+
return "[image]";
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
if (type === "document") {
|
|
920
|
+
const title = asString(block.title);
|
|
921
|
+
return title ? `[document: ${title}]` : "[document]";
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
return undefined;
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
function collectAnthropicReasoningText(
|
|
928
|
+
block: Record<string, unknown>,
|
|
929
|
+
): string | undefined {
|
|
930
|
+
const type = asString(block.type);
|
|
931
|
+
if (type === "thinking") {
|
|
932
|
+
const thinking = asString(block.thinking);
|
|
933
|
+
return thinking ? thinking : undefined;
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
if (type === "redacted_thinking") {
|
|
937
|
+
return "[redacted thinking]";
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
return undefined;
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
function isAnthropicToolUseType(type: string | undefined): boolean {
|
|
944
|
+
return type === "tool_use" || type === "server_tool_use";
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
function isAnthropicToolResultType(type: string | undefined): boolean {
|
|
948
|
+
return type === "tool_result" || type === "web_search_tool_result";
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
function collectAnthropicToolResultText(
|
|
952
|
+
block: Record<string, unknown>,
|
|
953
|
+
): string | undefined {
|
|
954
|
+
if (asString(block.type) === "web_search_tool_result") {
|
|
955
|
+
return "[Web search results]";
|
|
956
|
+
}
|
|
957
|
+
return collectAnthropicText(block.content);
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
function sanitizeAnthropicWebSearchToolResultData(
|
|
961
|
+
block: Record<string, unknown>,
|
|
962
|
+
): unknown {
|
|
963
|
+
return sanitizeAnthropicStructuredValue(block);
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
function sanitizeAnthropicStructuredValue(value: unknown): unknown {
|
|
967
|
+
if (Array.isArray(value)) {
|
|
968
|
+
return value.map((entry) => sanitizeAnthropicStructuredValue(entry));
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
const record = asRecord(value);
|
|
972
|
+
if (!record) {
|
|
973
|
+
return value;
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
return Object.fromEntries(
|
|
977
|
+
Object.entries(record)
|
|
978
|
+
.filter(
|
|
979
|
+
([key, entryValue]) =>
|
|
980
|
+
key !== "encrypted_content" && entryValue !== undefined,
|
|
981
|
+
)
|
|
982
|
+
.map(([key, entryValue]) => [
|
|
983
|
+
key,
|
|
984
|
+
sanitizeAnthropicStructuredValue(entryValue),
|
|
985
|
+
]),
|
|
986
|
+
);
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
function buildMessageLabel(role: string, index: number): string {
|
|
990
|
+
const capitalizedRole =
|
|
991
|
+
role.length > 0 ? role[0]!.toUpperCase() + role.slice(1) : "Message";
|
|
992
|
+
if (role === "system") {
|
|
993
|
+
return "System prompt";
|
|
994
|
+
}
|
|
995
|
+
return `${capitalizedRole} message ${index}`;
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
function buildGeminiRequestSettings(
|
|
999
|
+
request: Record<string, unknown>,
|
|
1000
|
+
config: Record<string, unknown> | null,
|
|
1001
|
+
): Record<string, unknown> | undefined {
|
|
1002
|
+
const topLevelSettings = omitRecordKeys(request, ["contents", "config"]);
|
|
1003
|
+
const configSettings = omitRecordKeys(config, ["systemInstruction", "tools"]);
|
|
1004
|
+
|
|
1005
|
+
if (!topLevelSettings && !configSettings) {
|
|
1006
|
+
return undefined;
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
return {
|
|
1010
|
+
...(topLevelSettings ?? {}),
|
|
1011
|
+
...(configSettings ? { config: configSettings } : {}),
|
|
1012
|
+
};
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
function structuredJsonSection(
|
|
1016
|
+
kind: LlmContextSection["kind"],
|
|
1017
|
+
label: string,
|
|
1018
|
+
data: unknown,
|
|
1019
|
+
): LlmContextSection {
|
|
1020
|
+
return {
|
|
1021
|
+
kind,
|
|
1022
|
+
label,
|
|
1023
|
+
data,
|
|
1024
|
+
language: "json",
|
|
1025
|
+
};
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
function hasMeaningfulRequestSettings(
|
|
1029
|
+
settings: Record<string, unknown> | undefined,
|
|
1030
|
+
): settings is Record<string, unknown> {
|
|
1031
|
+
if (!settings) {
|
|
1032
|
+
return false;
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
const keys = Object.keys(settings);
|
|
1036
|
+
return !(keys.length === 1 && keys[0] === "model");
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
function omitRecordKeys(
|
|
1040
|
+
record: Record<string, unknown> | null,
|
|
1041
|
+
omittedKeys: string[],
|
|
1042
|
+
): Record<string, unknown> | undefined {
|
|
1043
|
+
if (!record) {
|
|
1044
|
+
return undefined;
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
const filteredEntries = Object.entries(record).filter(
|
|
1048
|
+
([key, value]) => !omittedKeys.includes(key) && value !== undefined,
|
|
1049
|
+
);
|
|
1050
|
+
if (filteredEntries.length === 0) {
|
|
1051
|
+
return undefined;
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
return Object.fromEntries(filteredEntries);
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
function previewStructuredValue(value: unknown): string | undefined {
|
|
1058
|
+
if (value === undefined) {
|
|
1059
|
+
return undefined;
|
|
1060
|
+
}
|
|
1061
|
+
if (typeof value === "string") {
|
|
1062
|
+
return truncateText(value);
|
|
1063
|
+
}
|
|
1064
|
+
try {
|
|
1065
|
+
return truncateText(JSON.stringify(value));
|
|
1066
|
+
} catch {
|
|
1067
|
+
return undefined;
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
function parseJsonValue(value: string | undefined): unknown {
|
|
1072
|
+
if (!value) {
|
|
1073
|
+
return undefined;
|
|
1074
|
+
}
|
|
1075
|
+
try {
|
|
1076
|
+
return JSON.parse(value);
|
|
1077
|
+
} catch {
|
|
1078
|
+
return value;
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
function joinTextParts(parts: string[]): string | undefined {
|
|
1083
|
+
if (parts.length === 0) {
|
|
1084
|
+
return undefined;
|
|
1085
|
+
}
|
|
1086
|
+
const text = parts.join("\n\n");
|
|
1087
|
+
return hasMeaningfulText(text) ? text : undefined;
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
function truncateText(text: string, maxLength = 280): string {
|
|
1091
|
+
const normalized = normalizeText(text);
|
|
1092
|
+
if (normalized.length <= maxLength) {
|
|
1093
|
+
return normalized;
|
|
1094
|
+
}
|
|
1095
|
+
return `${normalized.slice(0, maxLength - 3).trimEnd()}...`;
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
function normalizeText(text: string): string {
|
|
1099
|
+
const normalized = text.replace(/\s+/g, " ").trim();
|
|
1100
|
+
return normalized.length > 0 ? normalized : "";
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
function hasMeaningfulText(text: string): boolean {
|
|
1104
|
+
return text.trim().length > 0;
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
function mergeSummaryFragments(
|
|
1108
|
+
requestSummary: LlmContextSummary | undefined,
|
|
1109
|
+
responseSummary: LlmContextSummary | undefined,
|
|
1110
|
+
): LlmContextSummary | undefined {
|
|
1111
|
+
if (!requestSummary && !responseSummary) {
|
|
1112
|
+
return undefined;
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
const summary = {
|
|
1116
|
+
...(requestSummary ?? responseSummary)!,
|
|
1117
|
+
} as LlmContextSummary;
|
|
1118
|
+
|
|
1119
|
+
if (!requestSummary || !responseSummary) {
|
|
1120
|
+
return summary;
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
for (const [key, value] of Object.entries(responseSummary) as [
|
|
1124
|
+
keyof LlmContextSummary,
|
|
1125
|
+
LlmContextSummary[keyof LlmContextSummary],
|
|
1126
|
+
][]) {
|
|
1127
|
+
if (value !== undefined) {
|
|
1128
|
+
summary[key] = value as never;
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
return summary;
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
function mergeNormalizedCandidates(
|
|
1136
|
+
requestCandidate: NormalizedPayloadCandidate | null | undefined,
|
|
1137
|
+
responseCandidate: NormalizedPayloadCandidate | null | undefined,
|
|
1138
|
+
): LlmContextNormalizationResult {
|
|
1139
|
+
if (!requestCandidate && !responseCandidate) {
|
|
1140
|
+
return {};
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
const requestSections = [
|
|
1144
|
+
...(requestCandidate?.requestSections ?? []),
|
|
1145
|
+
...(responseCandidate?.requestSections ?? []),
|
|
1146
|
+
];
|
|
1147
|
+
const responseSections = [
|
|
1148
|
+
...(requestCandidate?.responseSections ?? []),
|
|
1149
|
+
...(responseCandidate?.responseSections ?? []),
|
|
1150
|
+
];
|
|
1151
|
+
|
|
1152
|
+
return {
|
|
1153
|
+
summary: mergeSummaryFragments(
|
|
1154
|
+
requestCandidate?.summary,
|
|
1155
|
+
responseCandidate?.summary,
|
|
1156
|
+
),
|
|
1157
|
+
requestSections: requestSections.length > 0 ? requestSections : undefined,
|
|
1158
|
+
responseSections:
|
|
1159
|
+
responseSections.length > 0 ? responseSections : undefined,
|
|
1160
|
+
};
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
function normalizeCompatibleRequestPayload(
|
|
1164
|
+
requestPayload: unknown,
|
|
1165
|
+
provider: LlmContextSummary["provider"],
|
|
1166
|
+
): NormalizedPayloadCandidate | null {
|
|
1167
|
+
switch (provider) {
|
|
1168
|
+
case "openai":
|
|
1169
|
+
return normalizeOpenAiRequestPayload(requestPayload, true);
|
|
1170
|
+
case "anthropic":
|
|
1171
|
+
return normalizeAnthropicRequestPayload(requestPayload, true);
|
|
1172
|
+
case "gemini":
|
|
1173
|
+
return normalizeGeminiRequestPayload(requestPayload);
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
function hasOpenAiModelPrefix(model: string | undefined): boolean {
|
|
1178
|
+
if (!model) return false;
|
|
1179
|
+
return /^(gpt-|chatgpt-|ft:|o[1-9]\d*(-|$))/.test(model);
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
function hasAnthropicModelPrefix(model: string | undefined): boolean {
|
|
1183
|
+
if (!model) return false;
|
|
1184
|
+
return model.startsWith("claude-");
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
function asRecord(value: unknown): Record<string, unknown> | null {
|
|
1188
|
+
if (typeof value !== "object" || value == null || Array.isArray(value)) {
|
|
1189
|
+
return null;
|
|
1190
|
+
}
|
|
1191
|
+
return value as Record<string, unknown>;
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
function asRecordArray(value: unknown): Record<string, unknown>[] | null {
|
|
1195
|
+
if (!Array.isArray(value)) {
|
|
1196
|
+
return null;
|
|
1197
|
+
}
|
|
1198
|
+
return value.filter(
|
|
1199
|
+
(entry): entry is Record<string, unknown> =>
|
|
1200
|
+
typeof entry === "object" && entry != null && !Array.isArray(entry),
|
|
1201
|
+
);
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
function asString(value: unknown): string | undefined {
|
|
1205
|
+
return typeof value === "string" ? value : undefined;
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
function asNumber(value: unknown): number | undefined {
|
|
1209
|
+
return typeof value === "number" && Number.isFinite(value)
|
|
1210
|
+
? value
|
|
1211
|
+
: undefined;
|
|
1212
|
+
}
|