melchizedek-agents 0.18.0 → 0.19.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/.env.example +12 -0
- package/CHANGELOG.md +825 -0
- package/DOCUMENTATION.md +217 -59
- package/README.md +42 -1
- package/config/agents/examples/harness.yaml +3 -1
- package/config/agents/examples/model_zoo.yaml +10 -9
- package/config/agents/syndicateSchema.yaml +50 -16
- package/config/agents/templates/account_memory.yaml +2 -6
- package/config/agents/templates/case_desk.yaml +3 -9
- package/config/agents/templates/draft_review.yaml +3 -9
- package/config/agents/templates/intake_extractor.yaml +1 -3
- package/config/agents/templates/research_brief.yaml +3 -9
- package/config/agents/templates/research_desk.yaml +3 -9
- package/config/agents/templates/review_panel.yaml +4 -12
- package/config/agents/templates/support_triage.yaml +4 -12
- package/config/agents/templates/systems_operator.yaml +2 -6
- package/db/hardening.sql +7 -1
- package/db/migrations/0013_tool_credentials.sql +260 -0
- package/dist/lib/a2a/app.d.ts +70 -6
- package/dist/lib/a2a/app.js +107 -2
- package/dist/lib/a2a/executor.d.ts +20 -3
- package/dist/lib/a2a/executor.js +66 -7
- package/dist/lib/a2a/remoteAgent.d.ts +24 -9
- package/dist/lib/a2a/remoteAgent.js +31 -14
- package/dist/lib/compile.d.ts +121 -30
- package/dist/lib/compile.js +137 -124
- package/dist/lib/compileAdk.d.ts +29 -0
- package/dist/lib/compileAdk.js +132 -0
- package/dist/lib/compileNative.d.ts +85 -0
- package/dist/lib/compileNative.js +222 -0
- package/dist/lib/config.d.ts +6 -5
- package/dist/lib/config.js +6 -5
- package/dist/lib/doctor.js +1 -0
- package/dist/lib/index.d.ts +2 -1
- package/dist/lib/index.js +2 -1
- package/dist/lib/loadSyndicate.d.ts +18 -0
- package/dist/lib/memory/erase.d.ts +5 -3
- package/dist/lib/memory/erase.js +5 -2
- package/dist/lib/memory/namespace.d.ts +8 -1
- package/dist/lib/memory/namespace.js +12 -6
- package/dist/lib/memory/providers.js +2 -1
- package/dist/lib/memory/supabaseMemoryService.d.ts +37 -3
- package/dist/lib/memory/supabaseMemoryService.js +42 -2
- package/dist/lib/model.d.ts +47 -0
- package/dist/lib/model.js +37 -0
- package/dist/lib/models/adapterResolver.d.ts +113 -0
- package/dist/lib/models/adapterResolver.js +136 -0
- package/dist/lib/models/adkGeminiAdapter.d.ts +90 -0
- package/dist/lib/models/adkGeminiAdapter.js +367 -0
- package/dist/lib/models/adkShim.d.ts +111 -0
- package/dist/lib/models/adkShim.js +136 -0
- package/dist/lib/models/capabilities.d.ts +3 -0
- package/dist/lib/models/capabilities.js +20 -13
- package/dist/lib/models/chatCompletionsAdapter.d.ts +279 -0
- package/dist/lib/models/chatCompletionsAdapter.js +854 -0
- package/dist/lib/models/circuitBreaker.d.ts +42 -0
- package/dist/lib/models/circuitBreaker.js +70 -0
- package/dist/lib/models/claudeAdapter.d.ts +103 -0
- package/dist/lib/models/claudeAdapter.js +669 -0
- package/dist/lib/models/claudeLlm.d.ts +39 -62
- package/dist/lib/models/claudeLlm.js +44 -349
- package/dist/lib/models/claudeModels.d.ts +127 -0
- package/dist/lib/models/claudeModels.js +237 -0
- package/dist/lib/models/contract.d.ts +395 -0
- package/dist/lib/models/contract.js +39 -0
- package/dist/lib/models/endpoints.js +2 -1
- package/dist/lib/models/errorResponse.d.ts +50 -0
- package/dist/lib/models/errorResponse.js +48 -0
- package/dist/lib/models/fallback.d.ts +17 -10
- package/dist/lib/models/fallback.js +53 -42
- package/dist/lib/models/fallbackAdapter.d.ts +55 -0
- package/dist/lib/models/fallbackAdapter.js +62 -0
- package/dist/lib/models/gateway.js +2 -1
- package/dist/lib/models/gatewayAdapter.d.ts +51 -0
- package/dist/lib/models/gatewayAdapter.js +81 -0
- package/dist/lib/models/gatewayLlm.d.ts +12 -33
- package/dist/lib/models/gatewayLlm.js +14 -75
- package/dist/lib/models/geminiAdapter.d.ts +123 -0
- package/dist/lib/models/geminiAdapter.js +831 -0
- package/dist/lib/models/geminiState.d.ts +25 -0
- package/dist/lib/models/geminiState.js +25 -0
- package/dist/lib/models/genaiMapping.d.ts +263 -0
- package/dist/lib/models/genaiMapping.js +862 -0
- package/dist/lib/models/gptAdapter.d.ts +240 -0
- package/dist/lib/models/gptAdapter.js +804 -0
- package/dist/lib/models/gptLlm.d.ts +63 -123
- package/dist/lib/models/gptLlm.js +88 -602
- package/dist/lib/models/grokAdapter.d.ts +86 -0
- package/dist/lib/models/grokAdapter.js +142 -0
- package/dist/lib/models/grokLlm.d.ts +22 -45
- package/dist/lib/models/grokLlm.js +22 -67
- package/dist/lib/models/kimiAdapter.d.ts +157 -0
- package/dist/lib/models/kimiAdapter.js +203 -0
- package/dist/lib/models/kimiLlm.d.ts +15 -107
- package/dist/lib/models/kimiLlm.js +17 -153
- package/dist/lib/models/ollamaAdapter.d.ts +125 -0
- package/dist/lib/models/ollamaAdapter.js +155 -0
- package/dist/lib/models/ollamaLlm.d.ts +11 -107
- package/dist/lib/models/ollamaLlm.js +13 -151
- package/dist/lib/models/openAiCompatibleLlm.d.ts +73 -216
- package/dist/lib/models/openAiCompatibleLlm.js +109 -671
- package/dist/lib/models/providerState.d.ts +63 -0
- package/dist/lib/models/providerState.js +61 -0
- package/dist/lib/models/reasoning.d.ts +26 -0
- package/dist/lib/models/reasoning.js +59 -0
- package/dist/lib/models/registry.d.ts +58 -35
- package/dist/lib/models/registry.js +89 -118
- package/dist/lib/models/retry.d.ts +2 -1
- package/dist/lib/models/retry.js +3 -2
- package/dist/lib/models/schemaNormalize.d.ts +98 -0
- package/dist/lib/models/schemaNormalize.js +321 -1
- package/dist/lib/models/tracedGemini.d.ts +41 -0
- package/dist/lib/models/tracedGemini.js +98 -0
- package/dist/lib/models/urls.d.ts +11 -0
- package/dist/lib/models/urls.js +16 -0
- package/dist/lib/net/redirects.d.ts +21 -2
- package/dist/lib/net/redirects.js +11 -34
- package/dist/lib/observability/audit.d.ts +5 -1
- package/dist/lib/observability/lineage.d.ts +14 -0
- package/dist/lib/observability/lineage.js +33 -0
- package/dist/lib/observability/supabaseSpanExporter.d.ts +10 -4
- package/dist/lib/observability/supabaseSpanExporter.js +17 -9
- package/dist/lib/observability/tracer.d.ts +26 -8
- package/dist/lib/observability/tracer.js +43 -18
- package/dist/lib/runtime/adkMemoryBridge.d.ts +46 -0
- package/dist/lib/runtime/adkMemoryBridge.js +68 -0
- package/dist/lib/runtime/adkSessionBridge.d.ts +78 -0
- package/dist/lib/runtime/adkSessionBridge.js +176 -0
- package/dist/lib/runtime/credentials.d.ts +59 -0
- package/dist/lib/runtime/credentials.js +62 -0
- package/dist/lib/runtime/events.d.ts +229 -0
- package/dist/lib/runtime/events.js +274 -0
- package/dist/lib/runtime/logging.d.ts +32 -0
- package/dist/lib/runtime/logging.js +55 -0
- package/dist/lib/runtime/memoryService.d.ts +72 -0
- package/dist/lib/runtime/memoryService.js +25 -0
- package/dist/lib/runtime/native/agentLoop.d.ts +203 -0
- package/dist/lib/runtime/native/agentLoop.js +687 -0
- package/dist/lib/runtime/native/compaction.d.ts +103 -0
- package/dist/lib/runtime/native/compaction.js +191 -0
- package/dist/lib/runtime/native/delegate.d.ts +98 -0
- package/dist/lib/runtime/native/delegate.js +173 -0
- package/dist/lib/runtime/native/history.d.ts +83 -0
- package/dist/lib/runtime/native/history.js +312 -0
- package/dist/lib/runtime/native/interrupts.d.ts +126 -0
- package/dist/lib/runtime/native/interrupts.js +291 -0
- package/dist/lib/runtime/native/request.d.ts +215 -0
- package/dist/lib/runtime/native/request.js +585 -0
- package/dist/lib/runtime/native/selfCorrection.d.ts +106 -0
- package/dist/lib/runtime/native/selfCorrection.js +253 -0
- package/dist/lib/runtime/native/step.d.ts +148 -0
- package/dist/lib/runtime/native/step.js +239 -0
- package/dist/lib/runtime/native/taskMode.d.ts +64 -0
- package/dist/lib/runtime/native/taskMode.js +126 -0
- package/dist/lib/runtime/native/telemetry.d.ts +75 -0
- package/dist/lib/runtime/native/telemetry.js +299 -0
- package/dist/lib/runtime/native/tempState.d.ts +31 -0
- package/dist/lib/runtime/native/tempState.js +56 -0
- package/dist/lib/runtime/nativeTurn.d.ts +86 -0
- package/dist/lib/runtime/nativeTurn.js +107 -0
- package/dist/lib/runtime/questions.d.ts +29 -7
- package/dist/lib/runtime/questions.js +47 -32
- package/dist/lib/runtime/runtimeFlag.d.ts +30 -0
- package/dist/lib/runtime/runtimeFlag.js +50 -0
- package/dist/lib/runtime/sessions.d.ts +166 -0
- package/dist/lib/runtime/sessions.js +201 -0
- package/dist/lib/runtime/syndicateTurn.d.ts +41 -5
- package/dist/lib/runtime/syndicateTurn.js +202 -32
- package/dist/lib/session/supabaseSessionService.d.ts +64 -17
- package/dist/lib/session/supabaseSessionService.js +148 -105
- package/dist/lib/session/transcript.d.ts +68 -22
- package/dist/lib/session/transcript.js +102 -28
- package/dist/lib/skills.js +33 -7
- package/dist/lib/storage/postgres/credentialStore.d.ts +20 -0
- package/dist/lib/storage/postgres/credentialStore.js +82 -0
- package/dist/lib/storage/postgres/index.d.ts +12 -0
- package/dist/lib/storage/postgres/index.js +6 -1
- package/dist/lib/storage/postgres/sessionService.d.ts +43 -15
- package/dist/lib/storage/postgres/sessionService.js +128 -66
- package/dist/lib/syndicateSchema.d.ts +47 -0
- package/dist/lib/syndicateSchema.js +72 -3
- package/dist/lib/toolRegistry.d.ts +15 -2
- package/dist/lib/toolRegistry.js +33 -17
- package/dist/lib/tools/adkTool.d.ts +81 -0
- package/dist/lib/tools/adkTool.js +268 -0
- package/dist/lib/tools/auth.d.ts +118 -0
- package/dist/lib/tools/auth.js +89 -0
- package/dist/lib/tools/collectionsSearchTool.d.ts +7 -6
- package/dist/lib/tools/collectionsSearchTool.js +11 -26
- package/dist/lib/tools/credentialCipher.d.ts +58 -0
- package/dist/lib/tools/credentialCipher.js +116 -0
- package/dist/lib/tools/credentialStore.d.ts +77 -0
- package/dist/lib/tools/credentialStore.js +246 -0
- package/dist/lib/tools/examples.d.ts +43 -0
- package/dist/lib/tools/examples.js +62 -0
- package/dist/lib/tools/generateImageTool.d.ts +15 -6
- package/dist/lib/tools/generateImageTool.js +19 -29
- package/dist/lib/tools/inspectImageTool.d.ts +9 -6
- package/dist/lib/tools/inspectImageTool.js +13 -17
- package/dist/lib/tools/mcpToolFactory.d.ts +42 -1
- package/dist/lib/tools/mcpToolFactory.js +46 -47
- package/dist/lib/tools/memoryTools.d.ts +61 -0
- package/dist/lib/tools/memoryTools.js +119 -0
- package/dist/lib/tools/nativeTools.d.ts +38 -0
- package/dist/lib/tools/nativeTools.js +43 -0
- package/dist/lib/tools/oauthConsent.d.ts +181 -0
- package/dist/lib/tools/oauthConsent.js +330 -0
- package/dist/lib/tools/openapi/call.d.ts +108 -0
- package/dist/lib/tools/openapi/call.js +295 -0
- package/dist/lib/tools/openapi/parse.d.ts +147 -0
- package/dist/lib/tools/openapi/parse.js +570 -0
- package/dist/lib/tools/openapiTools.d.ts +25 -17
- package/dist/lib/tools/openapiTools.js +124 -121
- package/dist/lib/tools/scienceTools.d.ts +14 -14
- package/dist/lib/tools/skillToolset.d.ts +58 -87
- package/dist/lib/tools/skillToolset.js +61 -142
- package/dist/lib/tools/skills/env.d.ts +58 -0
- package/dist/lib/tools/skills/env.js +165 -0
- package/dist/lib/tools/skills/executor.d.ts +114 -0
- package/dist/lib/tools/skills/executor.js +331 -0
- package/dist/lib/tools/skills/frontmatter.d.ts +79 -0
- package/dist/lib/tools/skills/frontmatter.js +215 -0
- package/dist/lib/tools/skills/loader.d.ts +91 -0
- package/dist/lib/tools/skills/loader.js +257 -0
- package/dist/lib/tools/skills/tools.d.ts +187 -0
- package/dist/lib/tools/skills/tools.js +386 -0
- package/dist/lib/tools/taskTools.d.ts +15 -15
- package/dist/lib/tools/tool.d.ts +354 -0
- package/dist/lib/tools/tool.js +304 -0
- package/dist/lib/tools/toolContract.d.ts +89 -33
- package/dist/lib/tools/toolContract.js +104 -82
- package/dist/lib/tools/urlContextTool.d.ts +3 -0
- package/dist/lib/tools/urlContextTool.js +5 -1
- package/dist/lib/tools/webExtractTool.d.ts +2 -2
- package/dist/lib/tools/webExtractTool.js +2 -1
- package/dist/lib/tools/webSearchTool.d.ts +6 -9
- package/dist/lib/tools/webSearchTool.js +14 -59
- package/dist/lib/tools/wikiTools.d.ts +19 -19
- package/dist/lib/tools/wikiTools.js +13 -9
- package/dist/lib/tools/xApiSearchTool.d.ts +2 -2
- package/dist/lib/tools/xApiSearchTool.js +2 -1
- package/dist/lib/tools/xSearchTool.d.ts +7 -9
- package/dist/lib/tools/xSearchTool.js +11 -62
- package/dist/lib/tools/xaiSearchParams.d.ts +41 -0
- package/dist/lib/tools/xaiSearchParams.js +126 -0
- package/dist/lib/wiki/agentRun.d.ts +12 -0
- package/dist/lib/wiki/agentRun.js +72 -34
- package/dist/lib/wiki/entities.d.ts +12 -0
- package/dist/lib/wiki/entities.js +45 -3
- package/dist/lib/workflow/agentNode.d.ts +136 -0
- package/dist/lib/workflow/agentNode.js +274 -0
- package/dist/lib/workflow/graph.d.ts +177 -0
- package/dist/lib/workflow/graph.js +458 -0
- package/dist/lib/workflow/nodeEvents.d.ts +50 -0
- package/dist/lib/workflow/nodeEvents.js +101 -0
- package/dist/lib/workflow/pause.d.ts +122 -0
- package/dist/lib/workflow/pause.js +180 -0
- package/dist/lib/workflow/resume.d.ts +165 -0
- package/dist/lib/workflow/resume.js +440 -0
- package/dist/lib/workflow/route.d.ts +58 -0
- package/dist/lib/workflow/route.js +69 -0
- package/dist/lib/workflow/scheduler.d.ts +415 -0
- package/dist/lib/workflow/scheduler.js +743 -0
- package/dist/lib/workflow/toolNode.d.ts +127 -0
- package/dist/lib/workflow/toolNode.js +236 -0
- package/dist/lib/workflow/turn.d.ts +118 -0
- package/dist/lib/workflow/turn.js +216 -0
- package/dist/lib/workflowConfig.d.ts +3 -2
- package/dist/lib/workflowConfig.js +3 -12
- package/dist/scripts/a2a_server.js +4 -3
- package/dist/scripts/assistant_worker.d.ts +2 -1
- package/dist/scripts/assistant_worker.js +9 -4
- package/dist/scripts/syndicate_chat.js +19 -10
- package/package.json +4 -1
- package/skills/briefs/melchizedek-author.md +3 -2
- package/skills/briefs/melchizedek-models.md +13 -9
- package/skills/briefs/melchizedek.md +3 -3
- package/skills/melchizedek/SKILL.md +2 -1
- package/skills/melchizedek-author/SKILL.md +2 -1
- package/skills/melchizedek-author/assets/minimal.yaml +2 -0
- package/skills/melchizedek-models/SKILL.md +23 -6
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* lib/models/kimiAdapter.ts — Moonshot AI (Kimi) behind the engine's own
|
|
3
|
+
* model contract (lib/models/contract.ts, ADR 0048).
|
|
4
|
+
*
|
|
5
|
+
* WHY this file exists:
|
|
6
|
+
* Model optionality is a primary driver of this framework: the agent YAML
|
|
7
|
+
* declares `model`, and the registry routes it to the right provider.
|
|
8
|
+
* Kimi K3 (July 2026) is the strongest open-weight model on the public
|
|
9
|
+
* coding and research boards, and Moonshot serves it first-party with
|
|
10
|
+
* tool calling, strict structured output and vision, so it earns a
|
|
11
|
+
* direct adapter rather than only the gateway path. Under ADK it runs
|
|
12
|
+
* behind KimiLlm (lib/models/kimiLlm.ts), which any `kimi-*` id routes to.
|
|
13
|
+
*
|
|
14
|
+
* WHY the chat-completions base (lib/models/chatCompletionsAdapter.ts):
|
|
15
|
+
* Moonshot's API is OpenAI Chat Completions at https://api.moonshot.ai/v1
|
|
16
|
+
* (it also serves Responses on the same base and Anthropic Messages at
|
|
17
|
+
* /anthropic; neither adds a capability the chat dialect lacks here).
|
|
18
|
+
* The base provides tool calling, strict `json_schema` structured output
|
|
19
|
+
* (Moonshot documents `strict: true`), SSE streaming with usage, image
|
|
20
|
+
* parts as base64 data URIs (Moonshot rejects public image URLs, so a URL
|
|
21
|
+
* blob is never sent), `reasoning_content` surfaced as thinking, and
|
|
22
|
+
* retries.
|
|
23
|
+
*
|
|
24
|
+
* HOW TO ENABLE:
|
|
25
|
+
* 1. Create a key at https://platform.moonshot.ai (international; the
|
|
26
|
+
* .cn console serves mainland China) and put it in .env as
|
|
27
|
+
* MOONSHOT_API_KEY.
|
|
28
|
+
* 2. Set model: "kimi-k3" (or any kimi-* id) in your YAML.
|
|
29
|
+
* registerAvailableProviders() registers this adapter when the key is set.
|
|
30
|
+
* Optional: MOONSHOT_BASE_URL for a proxy that speaks the same dialect.
|
|
31
|
+
*
|
|
32
|
+
* THE FAMILY (platform.moonshot.ai › Model list, October 2026; USD per 1M tokens):
|
|
33
|
+
*
|
|
34
|
+
* model context in / cache hit / out use it for
|
|
35
|
+
* ──────────────────────────────────────────────────────────────────────
|
|
36
|
+
* kimi-k3 1M $3.00 / $0.30 / $15 DEFAULT — the flagship: 2.8T MoE, vision,
|
|
37
|
+
* always-on thinking, effort low|high|max
|
|
38
|
+
* kimi-k2.6 256K $0.95 / $0.16 / $4 the cheaper general tier: vision, thinking
|
|
39
|
+
* switchable; fragile on complex schemas
|
|
40
|
+
* kimi-k2.7-code 256K $0.95 / $0.19 / $4 coding; thinking cannot be turned off
|
|
41
|
+
* kimi-k2.7-code-highspeed 256K $1.90 / $0.38 / $8 the same model at ~180 tokens/s
|
|
42
|
+
*
|
|
43
|
+
* CHOOSING A MODEL (cost is a design property; see the model-routing skill):
|
|
44
|
+
* K3 is priced like a mid-tier closed model, not like an open-weight
|
|
45
|
+
* bargain: $3 / $15 is Claude Sonnet 4.6's price, and Claude Sonnet 5.5
|
|
46
|
+
* ($2 / $10, cache reads $0.20) undercuts it on every token while sitting
|
|
47
|
+
* above it on the coding boards. Two things make K3 dearer than its list
|
|
48
|
+
* price: thinking is always on and billed as output, and Moonshot's
|
|
49
|
+
* default effort is max — hence the pinned DEFAULT_KIMI_REASONING_EFFORT
|
|
50
|
+
* and the zoo agent at "low". What K3 buys is the strongest open-weight
|
|
51
|
+
* model (a self-host option) and a 1M context at a flat rate. For cost,
|
|
52
|
+
* reach for kimi-k2.6 (a quarter of K3's price, vision and tools) on
|
|
53
|
+
* subagent and bulk work, or serve K3 through the gateway: OpenRouter's
|
|
54
|
+
* endpoints range from about $0.88 to $3.45 per 1M input, the cheap ones
|
|
55
|
+
* quantized, with fidelity as the trade.
|
|
56
|
+
*
|
|
57
|
+
* There is no small or "flash" K3; K2.6 is the light tier. K3's weights
|
|
58
|
+
* are open (moonshotai/Kimi-K3 on Hugging Face), so the id is also served
|
|
59
|
+
* by OpenRouter, Together, Fireworks, Vercel AI Gateway and others; with
|
|
60
|
+
* no direct key, MODEL_GATEWAY serves it as `moonshotai/kimi-k3`
|
|
61
|
+
* (lib/models/gateway.ts). kimi-k2.5 and the moonshot-v1 ids were retired
|
|
62
|
+
* on 2026-08-31 and are not routed here on purpose.
|
|
63
|
+
*
|
|
64
|
+
* REASONING (ADR 0047):
|
|
65
|
+
* K3 always thinks; the lever is the top-level `reasoning_effort`
|
|
66
|
+
* (low | high | max, Moonshot's default max). A request's `reasoning`
|
|
67
|
+
* goes as its Kimi word (`medium` → `high`, a budget → the level that
|
|
68
|
+
* covers it), `none` as `low` since K3 cannot switch thinking off, and a
|
|
69
|
+
* request with none pins DEFAULT_KIMI_REASONING_EFFORT (lib/config.ts).
|
|
70
|
+
* `max` has no contract level: an agent that needs it writes the older
|
|
71
|
+
* spelling `generateContentConfig.reasoningEffort: max`, which the ADK
|
|
72
|
+
* path carries beside the contract (ChatCompletionsRequest.olderSpelling).
|
|
73
|
+
* K2.x models take no reasoning_effort: a `thinking: { type }` switch
|
|
74
|
+
* instead, so `none` (or a budget of 0) sends `disabled` and any other
|
|
75
|
+
* setting sends nothing. K2.7 Code (and its highspeed variant) cannot
|
|
76
|
+
* switch thinking off and Moonshot refuses `disabled` for it, so there
|
|
77
|
+
* `none` sends nothing either: the model thinks at its default.
|
|
78
|
+
*
|
|
79
|
+
* TOOL CHOICE (toolChoiceModes; checked live against Moonshot on
|
|
80
|
+
* 2026-10-08, PR #87): auto and none always (none by sending no tools).
|
|
81
|
+
* The forced modes depend on the model and on whether it thinks:
|
|
82
|
+
* kimi-k3 `required` is honoured with thinking on, so it goes as
|
|
83
|
+
* asked; a named tool is refused with thinking on (400
|
|
84
|
+
* "tool_choice 'specified' is incompatible with thinking
|
|
85
|
+
* enabled") and K3 always thinks, so it goes as `required`.
|
|
86
|
+
* kimi-k2.6 both forced modes are refused with thinking on and
|
|
87
|
+
* honoured with it off, so they go as asked only when the
|
|
88
|
+
* request's reasoning is `none` (or a budget of 0), which
|
|
89
|
+
* sends `thinking: disabled`; otherwise auto.
|
|
90
|
+
* others (K2.7 Code) unverified: weakened to auto.
|
|
91
|
+
* A weakened choice is marked on the span (llm.tool_choice.weakened).
|
|
92
|
+
*
|
|
93
|
+
* LIMITATIONS:
|
|
94
|
+
* - web_search: Moonshot's model-side `$web_search` built-in retires on
|
|
95
|
+
* 2026-10-20 and its replacement is a separate REST API
|
|
96
|
+
* (POST /v1/tools/search, billed per call), not a tool the model
|
|
97
|
+
* enables in the request. The tool is therefore dropped with a
|
|
98
|
+
* warning, like on Ollama; `web_extract` works, and `MOONSHOT_API_KEY`
|
|
99
|
+
* would fund a client-side search tool over that REST API if one is
|
|
100
|
+
* added (lib/tools/).
|
|
101
|
+
* - Thinking with tools: Moonshot's thinking-model guide (October 2026)
|
|
102
|
+
* asks for every id above that an assistant message's
|
|
103
|
+
* `reasoning_content` be sent back on the next request of a tool loop
|
|
104
|
+
* ("required" on K3). This adapter turns on the base's
|
|
105
|
+
* replaysReasoningContent for those ids (wantsReasoningReplay), so the
|
|
106
|
+
* field rides as providerState on the part that followed it and goes
|
|
107
|
+
* back on that message within the turn's tool loop, for the same model
|
|
108
|
+
* only (ADR 0046). Earlier turns' reasoning is not sent: K3 and
|
|
109
|
+
* K2.7 Code also ask for it across turns ("preserved thinking"), and
|
|
110
|
+
* the stored history of a past turn is not what the model saw. The
|
|
111
|
+
* replayed text is billed again as input on every later step of the
|
|
112
|
+
* loop, mostly at the cache-hit price, since the prefix is unchanged.
|
|
113
|
+
* - Every cell of its capability row is asserted against the request body
|
|
114
|
+
* the adapter sends (tests/capabilityMatrix.test.ts), as for every other
|
|
115
|
+
* provider. Verified live on 2026-10-03 through `npm run demo:models`:
|
|
116
|
+
* kimi-k3 at low effort answered the zoo's explainer prompt in 17.9 s,
|
|
117
|
+
* 440 tokens in, 486 out, 262 of them thinking, the scratchpad surfaced
|
|
118
|
+
* from reasoning_content; tool loops and search were not exercised.
|
|
119
|
+
*/
|
|
120
|
+
import { DEFAULT_KIMI_REASONING_EFFORT } from '../config.js';
|
|
121
|
+
import { ChatCompletionsAdapter, reasonsNotAtAll } from './chatCompletionsAdapter.js';
|
|
122
|
+
import { reasoningConfig } from './reasoning.js';
|
|
123
|
+
import { trimTrailingSlashes } from './urls.js';
|
|
124
|
+
export const MOONSHOT_BASE_URL = 'https://api.moonshot.ai/v1';
|
|
125
|
+
/** The flagship takes `reasoning_effort`; the K2 generation takes a `thinking` switch. */
|
|
126
|
+
export function isKimiK3(model) {
|
|
127
|
+
return /^kimi-k3\b/.test(model);
|
|
128
|
+
}
|
|
129
|
+
/** K2.6: thinking is on by default and switchable, and forcing a tool needs it off. */
|
|
130
|
+
export function isKimiK26(model) {
|
|
131
|
+
return /^kimi-k2\.6\b/.test(model);
|
|
132
|
+
}
|
|
133
|
+
/** K2.7 Code and its highspeed variant: thinking cannot be switched off, and `disabled` is refused. */
|
|
134
|
+
export function isKimiK27Code(model) {
|
|
135
|
+
return /^kimi-k2\.7-code\b/.test(model);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* The ids Moonshot documents as wanting `reasoning_content` back on a tool
|
|
139
|
+
* loop: K3, K2.6 and K2.7 Code (with its highspeed variant).
|
|
140
|
+
*/
|
|
141
|
+
export function wantsReasoningReplay(model) {
|
|
142
|
+
return isKimiK3(model) || /^kimi-k2\.(6|7-code)\b/.test(model);
|
|
143
|
+
}
|
|
144
|
+
export class KimiAdapter extends ChatCompletionsAdapter {
|
|
145
|
+
provider = 'moonshot';
|
|
146
|
+
#apiKey;
|
|
147
|
+
#baseUrl;
|
|
148
|
+
constructor({ model, apiKey, baseUrl }) {
|
|
149
|
+
super({ model });
|
|
150
|
+
this.#apiKey = apiKey;
|
|
151
|
+
this.#baseUrl = trimTrailingSlashes(baseUrl || process.env.MOONSHOT_BASE_URL || MOONSHOT_BASE_URL);
|
|
152
|
+
}
|
|
153
|
+
endpointUrl() {
|
|
154
|
+
return `${this.#baseUrl}/chat/completions`;
|
|
155
|
+
}
|
|
156
|
+
#key() {
|
|
157
|
+
return this.#apiKey ?? process.env.MOONSHOT_API_KEY;
|
|
158
|
+
}
|
|
159
|
+
headers() {
|
|
160
|
+
return { Authorization: `Bearer ${this.#key() ?? ''}` };
|
|
161
|
+
}
|
|
162
|
+
/** The forced tool choices each model honours, per the live checks in the header. */
|
|
163
|
+
toolChoiceModes(model, reasoning) {
|
|
164
|
+
if (isKimiK3(model))
|
|
165
|
+
return ['auto', 'none', 'required'];
|
|
166
|
+
if (isKimiK26(model) && reasonsNotAtAll(reasoning))
|
|
167
|
+
return ['auto', 'none', 'required', 'named'];
|
|
168
|
+
return ['auto', 'none'];
|
|
169
|
+
}
|
|
170
|
+
/** reasoning_content goes back on the tool loop for the ids that ask for it (see the header). */
|
|
171
|
+
replaysReasoningContent(model) {
|
|
172
|
+
return wantsReasoningReplay(model);
|
|
173
|
+
}
|
|
174
|
+
missingRequirement() {
|
|
175
|
+
if (this.#key())
|
|
176
|
+
return undefined;
|
|
177
|
+
return { code: 'MOONSHOT_MISSING_KEY', message: 'MOONSHOT_API_KEY is not set in environment.' };
|
|
178
|
+
}
|
|
179
|
+
httpError(status, detail, model) {
|
|
180
|
+
const hint = status === 401
|
|
181
|
+
? ' Check MOONSHOT_API_KEY (platform.moonshot.ai).'
|
|
182
|
+
: status === 404
|
|
183
|
+
? ` Is "${model}" a current Kimi id? kimi-k2.5 and moonshot-v1-* were retired on 2026-08-31.`
|
|
184
|
+
: '';
|
|
185
|
+
return {
|
|
186
|
+
code: 'MOONSHOT_HTTP_ERROR',
|
|
187
|
+
message: `Moonshot returned ${status}: ${detail.slice(0, 4000)}.${hint}`,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
/** The reasoning controls, per generation (see the header). */
|
|
191
|
+
reasoningFields(model, setting, olderWord) {
|
|
192
|
+
if (isKimiK3(model)) {
|
|
193
|
+
if (olderWord !== undefined)
|
|
194
|
+
return { reasoning_effort: olderWord };
|
|
195
|
+
if (setting === undefined)
|
|
196
|
+
return { reasoning_effort: DEFAULT_KIMI_REASONING_EFFORT };
|
|
197
|
+
const word = reasoningConfig(model, setting).reasoningEffort;
|
|
198
|
+
return { reasoning_effort: word === 'none' ? 'low' : word }; // K3 cannot switch thinking off
|
|
199
|
+
}
|
|
200
|
+
// K2.7 Code always thinks, and Moonshot refuses `disabled` for it.
|
|
201
|
+
return reasonsNotAtAll(setting) && !isKimiK27Code(model) ? { thinking: { type: 'disabled' } } : {};
|
|
202
|
+
}
|
|
203
|
+
}
|
|
@@ -1,123 +1,31 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* lib/models/kimiLlm.ts — Moonshot AI (Kimi)
|
|
2
|
+
* lib/models/kimiLlm.ts — Moonshot AI (Kimi), as an ADK BaseLlm for the
|
|
3
|
+
* LLMRegistry.
|
|
3
4
|
*
|
|
4
5
|
* WHY this file exists:
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* The base provides tool calling, strict `json_schema` structured output
|
|
17
|
-
* (Moonshot documents `strict: true`), SSE streaming with usage, image
|
|
18
|
-
* parts as base64 data URIs (Moonshot rejects public image URLs, which is
|
|
19
|
-
* what the base never sends), `reasoning_content` surfaced as THINKING
|
|
20
|
-
* output, retries, and the llm.request span.
|
|
21
|
-
*
|
|
22
|
-
* HOW TO ENABLE:
|
|
23
|
-
* 1. Create a key at https://platform.moonshot.ai (international; the
|
|
24
|
-
* .cn console serves mainland China) and put it in .env as
|
|
25
|
-
* MOONSHOT_API_KEY.
|
|
26
|
-
* 2. Set model: "kimi-k3" (or any kimi-* id) in your YAML.
|
|
27
|
-
* registerAvailableProviders() registers this adapter when the key is set.
|
|
28
|
-
* Optional: MOONSHOT_BASE_URL for a proxy that speaks the same dialect.
|
|
29
|
-
*
|
|
30
|
-
* THE FAMILY (platform.moonshot.ai › Model list, October 2026; USD per 1M tokens):
|
|
31
|
-
*
|
|
32
|
-
* model context in / cache hit / out use it for
|
|
33
|
-
* ──────────────────────────────────────────────────────────────────────
|
|
34
|
-
* kimi-k3 1M $3.00 / $0.30 / $15 DEFAULT — the flagship: 2.8T MoE, vision,
|
|
35
|
-
* always-on thinking, effort low|high|max
|
|
36
|
-
* kimi-k2.6 256K $0.95 / $0.16 / $4 the cheaper general tier: vision, thinking
|
|
37
|
-
* switchable; fragile on complex schemas
|
|
38
|
-
* kimi-k2.7-code 256K $0.95 / $0.19 / $4 coding; thinking cannot be turned off
|
|
39
|
-
* kimi-k2.7-code-highspeed 256K $1.90 / $0.38 / $8 the same model at ~180 tokens/s
|
|
40
|
-
*
|
|
41
|
-
* CHOOSING A MODEL (cost is a design property; see the model-routing skill):
|
|
42
|
-
* K3 is priced like a mid-tier closed model, not like an open-weight
|
|
43
|
-
* bargain: $3 / $15 is Claude Sonnet 4.6's price, and Claude Sonnet 5.5
|
|
44
|
-
* ($2 / $10, cache reads $0.20) undercuts it on every token while sitting
|
|
45
|
-
* above it on the coding boards. Two things make K3 dearer than its list
|
|
46
|
-
* price: thinking is always on and billed as output, and Moonshot's
|
|
47
|
-
* default effort is max — hence the pinned DEFAULT_KIMI_REASONING_EFFORT
|
|
48
|
-
* and the zoo agent at "low". What K3 buys is the strongest open-weight
|
|
49
|
-
* model (a self-host option) and a 1M context at a flat rate. For cost,
|
|
50
|
-
* reach for kimi-k2.6 (a quarter of K3's price, vision and tools) on
|
|
51
|
-
* subagent and bulk work, or serve K3 through the gateway: OpenRouter's
|
|
52
|
-
* endpoints range from about $0.88 to $3.45 per 1M input, the cheap ones
|
|
53
|
-
* quantized, with fidelity as the trade.
|
|
54
|
-
*
|
|
55
|
-
* There is no small or "flash" K3; K2.6 is the light tier. K3's weights
|
|
56
|
-
* are open (moonshotai/Kimi-K3 on Hugging Face), so the id is also served
|
|
57
|
-
* by OpenRouter, Together, Fireworks, Vercel AI Gateway and others; with
|
|
58
|
-
* no direct key, MODEL_GATEWAY serves it as `moonshotai/kimi-k3`
|
|
59
|
-
* (lib/models/gateway.ts). kimi-k2.5 and the moonshot-v1 ids were retired
|
|
60
|
-
* on 2026-08-31 and are not routed here on purpose.
|
|
61
|
-
*
|
|
62
|
-
* REASONING:
|
|
63
|
-
* K3 always thinks; the lever is the top-level `reasoning_effort`
|
|
64
|
-
* (low | high | max, Moonshot's default max). Requests pin
|
|
65
|
-
* DEFAULT_KIMI_REASONING_EFFORT (lib/config.ts) unless the agent sets
|
|
66
|
-
* generateContentConfig.reasoningEffort; "none" is mapped to "low" on
|
|
67
|
-
* K3, which cannot switch thinking off. K2.x models take no
|
|
68
|
-
* reasoning_effort: a `thinking: { type }` switch instead, so on those
|
|
69
|
-
* ids reasoningEffort "none" or thinkingBudget 0 sends `disabled` and
|
|
70
|
-
* any effort value is dropped from the body (K2.7 Code rejects
|
|
71
|
-
* disabled thinking; Moonshot's own error says so).
|
|
72
|
-
*
|
|
73
|
-
* LIMITATIONS:
|
|
74
|
-
* - web_search: Moonshot's model-side `$web_search` built-in retires on
|
|
75
|
-
* 2026-10-20 and its replacement is a separate REST API
|
|
76
|
-
* (POST /v1/tools/search, billed per call), not a tool the model
|
|
77
|
-
* enables in the request. The sentinel is therefore dropped with a
|
|
78
|
-
* warning, like on Ollama; `web_extract` works, and `MOONSHOT_API_KEY`
|
|
79
|
-
* would fund a client-side search tool over that REST API if one is
|
|
80
|
-
* added (lib/tools/).
|
|
81
|
-
* - Thinking with tools: Moonshot asks that K3's `reasoning_content` be
|
|
82
|
-
* passed back with the assistant message on a tool loop. This base
|
|
83
|
-
* keeps scratchpads out of history, so K3 re-reasons each step
|
|
84
|
-
* (degraded, stated in lib/models/capabilities.ts); if Moonshot ever
|
|
85
|
-
* enforces it, the turn ends with MOONSHOT_HTTP_ERROR naming the field.
|
|
86
|
-
* - Every cell of its capability row is asserted against the request body
|
|
87
|
-
* the adapter sends (tests/capabilityMatrix.test.ts), as for every other
|
|
88
|
-
* provider. Verified live on 2026-10-03 through `npm run demo:models`:
|
|
89
|
-
* kimi-k3 at low effort answered the zoo's explainer prompt in 17.9 s,
|
|
90
|
-
* 440 tokens in, 486 out, 262 of them thinking, the scratchpad surfaced
|
|
91
|
-
* from reasoning_content; tool loops and search were not exercised.
|
|
6
|
+
* The provider's work is KimiAdapter (lib/models/kimiAdapter.ts), on the
|
|
7
|
+
* engine's own model contract; its header covers the family, cost, the
|
|
8
|
+
* reasoning controls per generation, and reasoning_content on tool loops
|
|
9
|
+
* (ADR 0046). ADK still runs every turn, so this class runs the adapter
|
|
10
|
+
* under ADK: it is the chat-completions shim
|
|
11
|
+
* (lib/models/openAiCompatibleLlm.ts, ADR 0057), registered for any
|
|
12
|
+
* `kimi-*` id when MOONSHOT_API_KEY is set. The constructor and
|
|
13
|
+
* supportedModels are what they were before the adapter moved onto the
|
|
14
|
+
* contract, so lib/models/registry.ts and every caller are unchanged. On
|
|
15
|
+
* this path an agent's older spelling `reasoningEffort: max` still reaches
|
|
16
|
+
* K3, which the contract has no level for.
|
|
92
17
|
*/
|
|
93
|
-
import type { LlmRequest, LlmResponse } from '@google/adk';
|
|
94
18
|
import { OpenAiCompatibleLlm } from './openAiCompatibleLlm.ts';
|
|
95
|
-
export
|
|
96
|
-
/** The flagship takes `reasoning_effort`; the K2 generation takes a `thinking` switch. */
|
|
97
|
-
export declare function isKimiK3(model: string): boolean;
|
|
19
|
+
export { MOONSHOT_BASE_URL, isKimiK3, wantsReasoningReplay } from './kimiAdapter.ts';
|
|
98
20
|
export declare class KimiLlm extends OpenAiCompatibleLlm {
|
|
99
21
|
/** Any model: "kimi-*" in a YAML config routes here after registration. */
|
|
100
22
|
static readonly supportedModels: Array<string | RegExp>;
|
|
101
|
-
|
|
102
|
-
private readonly baseUrl;
|
|
23
|
+
/** `apiKey` defaults to MOONSHOT_API_KEY; `baseUrl` to MOONSHOT_BASE_URL, else https://api.moonshot.ai/v1. */
|
|
103
24
|
constructor({ model, apiKey, baseUrl }: {
|
|
104
25
|
model: string;
|
|
105
26
|
apiKey?: string;
|
|
106
27
|
baseUrl?: string;
|
|
107
28
|
});
|
|
108
|
-
protected providerId(): string;
|
|
109
|
-
protected endpointUrl(): string;
|
|
110
|
-
private key;
|
|
111
|
-
protected headers(): Record<string, string>;
|
|
112
|
-
protected missingRequirement(): LlmResponse | undefined;
|
|
113
|
-
protected httpError(status: number, detail: string): LlmResponse;
|
|
114
|
-
/**
|
|
115
|
-
* The reasoning controls, per generation (see the header). The base has
|
|
116
|
-
* already put `reasoning_effort` in the body when the agent set one;
|
|
117
|
-
* these fields are merged after it, so an `undefined` here removes it
|
|
118
|
-
* (JSON.stringify drops undefined keys).
|
|
119
|
-
*/
|
|
120
|
-
protected extraBodyFields(llmRequest: LlmRequest): Record<string, unknown>;
|
|
121
29
|
}
|
|
122
30
|
/** Registers KimiLlm with the ADK LLMRegistry (called when MOONSHOT_API_KEY is set). */
|
|
123
31
|
export declare function registerKimiLlm(): void;
|
|
@@ -1,167 +1,31 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* lib/models/kimiLlm.ts — Moonshot AI (Kimi)
|
|
2
|
+
* lib/models/kimiLlm.ts — Moonshot AI (Kimi), as an ADK BaseLlm for the
|
|
3
|
+
* LLMRegistry.
|
|
3
4
|
*
|
|
4
5
|
* WHY this file exists:
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* The base provides tool calling, strict `json_schema` structured output
|
|
17
|
-
* (Moonshot documents `strict: true`), SSE streaming with usage, image
|
|
18
|
-
* parts as base64 data URIs (Moonshot rejects public image URLs, which is
|
|
19
|
-
* what the base never sends), `reasoning_content` surfaced as THINKING
|
|
20
|
-
* output, retries, and the llm.request span.
|
|
21
|
-
*
|
|
22
|
-
* HOW TO ENABLE:
|
|
23
|
-
* 1. Create a key at https://platform.moonshot.ai (international; the
|
|
24
|
-
* .cn console serves mainland China) and put it in .env as
|
|
25
|
-
* MOONSHOT_API_KEY.
|
|
26
|
-
* 2. Set model: "kimi-k3" (or any kimi-* id) in your YAML.
|
|
27
|
-
* registerAvailableProviders() registers this adapter when the key is set.
|
|
28
|
-
* Optional: MOONSHOT_BASE_URL for a proxy that speaks the same dialect.
|
|
29
|
-
*
|
|
30
|
-
* THE FAMILY (platform.moonshot.ai › Model list, October 2026; USD per 1M tokens):
|
|
31
|
-
*
|
|
32
|
-
* model context in / cache hit / out use it for
|
|
33
|
-
* ──────────────────────────────────────────────────────────────────────
|
|
34
|
-
* kimi-k3 1M $3.00 / $0.30 / $15 DEFAULT — the flagship: 2.8T MoE, vision,
|
|
35
|
-
* always-on thinking, effort low|high|max
|
|
36
|
-
* kimi-k2.6 256K $0.95 / $0.16 / $4 the cheaper general tier: vision, thinking
|
|
37
|
-
* switchable; fragile on complex schemas
|
|
38
|
-
* kimi-k2.7-code 256K $0.95 / $0.19 / $4 coding; thinking cannot be turned off
|
|
39
|
-
* kimi-k2.7-code-highspeed 256K $1.90 / $0.38 / $8 the same model at ~180 tokens/s
|
|
40
|
-
*
|
|
41
|
-
* CHOOSING A MODEL (cost is a design property; see the model-routing skill):
|
|
42
|
-
* K3 is priced like a mid-tier closed model, not like an open-weight
|
|
43
|
-
* bargain: $3 / $15 is Claude Sonnet 4.6's price, and Claude Sonnet 5.5
|
|
44
|
-
* ($2 / $10, cache reads $0.20) undercuts it on every token while sitting
|
|
45
|
-
* above it on the coding boards. Two things make K3 dearer than its list
|
|
46
|
-
* price: thinking is always on and billed as output, and Moonshot's
|
|
47
|
-
* default effort is max — hence the pinned DEFAULT_KIMI_REASONING_EFFORT
|
|
48
|
-
* and the zoo agent at "low". What K3 buys is the strongest open-weight
|
|
49
|
-
* model (a self-host option) and a 1M context at a flat rate. For cost,
|
|
50
|
-
* reach for kimi-k2.6 (a quarter of K3's price, vision and tools) on
|
|
51
|
-
* subagent and bulk work, or serve K3 through the gateway: OpenRouter's
|
|
52
|
-
* endpoints range from about $0.88 to $3.45 per 1M input, the cheap ones
|
|
53
|
-
* quantized, with fidelity as the trade.
|
|
54
|
-
*
|
|
55
|
-
* There is no small or "flash" K3; K2.6 is the light tier. K3's weights
|
|
56
|
-
* are open (moonshotai/Kimi-K3 on Hugging Face), so the id is also served
|
|
57
|
-
* by OpenRouter, Together, Fireworks, Vercel AI Gateway and others; with
|
|
58
|
-
* no direct key, MODEL_GATEWAY serves it as `moonshotai/kimi-k3`
|
|
59
|
-
* (lib/models/gateway.ts). kimi-k2.5 and the moonshot-v1 ids were retired
|
|
60
|
-
* on 2026-08-31 and are not routed here on purpose.
|
|
61
|
-
*
|
|
62
|
-
* REASONING:
|
|
63
|
-
* K3 always thinks; the lever is the top-level `reasoning_effort`
|
|
64
|
-
* (low | high | max, Moonshot's default max). Requests pin
|
|
65
|
-
* DEFAULT_KIMI_REASONING_EFFORT (lib/config.ts) unless the agent sets
|
|
66
|
-
* generateContentConfig.reasoningEffort; "none" is mapped to "low" on
|
|
67
|
-
* K3, which cannot switch thinking off. K2.x models take no
|
|
68
|
-
* reasoning_effort: a `thinking: { type }` switch instead, so on those
|
|
69
|
-
* ids reasoningEffort "none" or thinkingBudget 0 sends `disabled` and
|
|
70
|
-
* any effort value is dropped from the body (K2.7 Code rejects
|
|
71
|
-
* disabled thinking; Moonshot's own error says so).
|
|
72
|
-
*
|
|
73
|
-
* LIMITATIONS:
|
|
74
|
-
* - web_search: Moonshot's model-side `$web_search` built-in retires on
|
|
75
|
-
* 2026-10-20 and its replacement is a separate REST API
|
|
76
|
-
* (POST /v1/tools/search, billed per call), not a tool the model
|
|
77
|
-
* enables in the request. The sentinel is therefore dropped with a
|
|
78
|
-
* warning, like on Ollama; `web_extract` works, and `MOONSHOT_API_KEY`
|
|
79
|
-
* would fund a client-side search tool over that REST API if one is
|
|
80
|
-
* added (lib/tools/).
|
|
81
|
-
* - Thinking with tools: Moonshot asks that K3's `reasoning_content` be
|
|
82
|
-
* passed back with the assistant message on a tool loop. This base
|
|
83
|
-
* keeps scratchpads out of history, so K3 re-reasons each step
|
|
84
|
-
* (degraded, stated in lib/models/capabilities.ts); if Moonshot ever
|
|
85
|
-
* enforces it, the turn ends with MOONSHOT_HTTP_ERROR naming the field.
|
|
86
|
-
* - Every cell of its capability row is asserted against the request body
|
|
87
|
-
* the adapter sends (tests/capabilityMatrix.test.ts), as for every other
|
|
88
|
-
* provider. Verified live on 2026-10-03 through `npm run demo:models`:
|
|
89
|
-
* kimi-k3 at low effort answered the zoo's explainer prompt in 17.9 s,
|
|
90
|
-
* 440 tokens in, 486 out, 262 of them thinking, the scratchpad surfaced
|
|
91
|
-
* from reasoning_content; tool loops and search were not exercised.
|
|
6
|
+
* The provider's work is KimiAdapter (lib/models/kimiAdapter.ts), on the
|
|
7
|
+
* engine's own model contract; its header covers the family, cost, the
|
|
8
|
+
* reasoning controls per generation, and reasoning_content on tool loops
|
|
9
|
+
* (ADR 0046). ADK still runs every turn, so this class runs the adapter
|
|
10
|
+
* under ADK: it is the chat-completions shim
|
|
11
|
+
* (lib/models/openAiCompatibleLlm.ts, ADR 0057), registered for any
|
|
12
|
+
* `kimi-*` id when MOONSHOT_API_KEY is set. The constructor and
|
|
13
|
+
* supportedModels are what they were before the adapter moved onto the
|
|
14
|
+
* contract, so lib/models/registry.ts and every caller are unchanged. On
|
|
15
|
+
* this path an agent's older spelling `reasoningEffort: max` still reaches
|
|
16
|
+
* K3, which the contract has no level for.
|
|
92
17
|
*/
|
|
93
18
|
import { LLMRegistry } from '@google/adk';
|
|
94
|
-
import {
|
|
19
|
+
import { KimiAdapter } from './kimiAdapter.js';
|
|
95
20
|
import { OpenAiCompatibleLlm } from './openAiCompatibleLlm.js';
|
|
96
|
-
export
|
|
97
|
-
/** The flagship takes `reasoning_effort`; the K2 generation takes a `thinking` switch. */
|
|
98
|
-
export function isKimiK3(model) {
|
|
99
|
-
return /^kimi-k3\b/.test(model);
|
|
100
|
-
}
|
|
21
|
+
export { MOONSHOT_BASE_URL, isKimiK3, wantsReasoningReplay } from './kimiAdapter.js';
|
|
101
22
|
// ── KimiLlm ───────────────────────────────────────────────────────────────────
|
|
102
23
|
export class KimiLlm extends OpenAiCompatibleLlm {
|
|
103
24
|
/** Any model: "kimi-*" in a YAML config routes here after registration. */
|
|
104
25
|
static supportedModels = [/^kimi-.+/];
|
|
105
|
-
apiKey;
|
|
106
|
-
baseUrl;
|
|
26
|
+
/** `apiKey` defaults to MOONSHOT_API_KEY; `baseUrl` to MOONSHOT_BASE_URL, else https://api.moonshot.ai/v1. */
|
|
107
27
|
constructor({ model, apiKey, baseUrl }) {
|
|
108
|
-
super({ model });
|
|
109
|
-
this.apiKey = apiKey;
|
|
110
|
-
this.baseUrl = (baseUrl || process.env.MOONSHOT_BASE_URL || MOONSHOT_BASE_URL).replace(/\/+$/, '');
|
|
111
|
-
}
|
|
112
|
-
providerId() {
|
|
113
|
-
return 'moonshot';
|
|
114
|
-
}
|
|
115
|
-
endpointUrl() {
|
|
116
|
-
return `${this.baseUrl}/chat/completions`;
|
|
117
|
-
}
|
|
118
|
-
key() {
|
|
119
|
-
return this.apiKey ?? process.env.MOONSHOT_API_KEY;
|
|
120
|
-
}
|
|
121
|
-
headers() {
|
|
122
|
-
return { Authorization: `Bearer ${this.key() ?? ''}` };
|
|
123
|
-
}
|
|
124
|
-
missingRequirement() {
|
|
125
|
-
if (this.key())
|
|
126
|
-
return undefined;
|
|
127
|
-
return {
|
|
128
|
-
errorCode: 'MOONSHOT_MISSING_KEY',
|
|
129
|
-
errorMessage: 'MOONSHOT_API_KEY is not set in environment.',
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
httpError(status, detail) {
|
|
133
|
-
const hint = status === 401
|
|
134
|
-
? ' Check MOONSHOT_API_KEY (platform.moonshot.ai).'
|
|
135
|
-
: status === 404
|
|
136
|
-
? ` Is "${this.model}" a current Kimi id? kimi-k2.5 and moonshot-v1-* were retired on 2026-08-31.`
|
|
137
|
-
: '';
|
|
138
|
-
return {
|
|
139
|
-
errorCode: 'MOONSHOT_HTTP_ERROR',
|
|
140
|
-
errorMessage: `Moonshot returned ${status}: ${detail.slice(0, 4000)}.${hint}`,
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* The reasoning controls, per generation (see the header). The base has
|
|
145
|
-
* already put `reasoning_effort` in the body when the agent set one;
|
|
146
|
-
* these fields are merged after it, so an `undefined` here removes it
|
|
147
|
-
* (JSON.stringify drops undefined keys).
|
|
148
|
-
*/
|
|
149
|
-
extraBodyFields(llmRequest) {
|
|
150
|
-
const cfg = llmRequest.config ?? {};
|
|
151
|
-
const effort = cfg.reasoningEffort;
|
|
152
|
-
if (isKimiK3(this.model)) {
|
|
153
|
-
if (effort === undefined)
|
|
154
|
-
return { reasoning_effort: DEFAULT_KIMI_REASONING_EFFORT };
|
|
155
|
-
if (effort === 'none')
|
|
156
|
-
return { reasoning_effort: 'low' }; // K3 cannot switch thinking off
|
|
157
|
-
return {};
|
|
158
|
-
}
|
|
159
|
-
const budget = cfg.thinkingConfig?.thinkingBudget;
|
|
160
|
-
const off = effort === 'none' || budget === 0;
|
|
161
|
-
return {
|
|
162
|
-
reasoning_effort: undefined,
|
|
163
|
-
...(off ? { thinking: { type: 'disabled' } } : {}),
|
|
164
|
-
};
|
|
28
|
+
super(new KimiAdapter({ model, apiKey, baseUrl }), { model });
|
|
165
29
|
}
|
|
166
30
|
}
|
|
167
31
|
// ── Registration helper ───────────────────────────────────────────────────────
|