Repository navigation
feat(harness-node): add Kimi (Moonshot) provider worker #173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # provider-kimi | ||
|
|
||
| Kimi (Moonshot) Chat Completions streaming provider; exposes | ||
| `provider::kimi::stream` and `provider::kimi::complete` on the iii bus. | ||
|
|
||
| ## Purpose | ||
|
|
||
| The iii-side bridge to Moonshot's Chat Completions API. It pulls a | ||
| credential from the auth worker (`auth::get_token`, provider `kimi`), | ||
| issues a streaming `chat/completions` request, parses the SSE response | ||
| into `AssistantMessageEvent` frames, and forwards each frame to the | ||
| caller-supplied channel. Mirrors the shape of | ||
| [provider-openai](harness-node/docs/workers/provider-openai.md) so the | ||
| orchestrator can swap providers without touching the FSM. | ||
|
|
||
| `provider::kimi::stream` is the modern channel-writer surface; | ||
| `provider::kimi::complete` is the legacy drain-and-return helper. | ||
|
|
||
| Tool calls are translated to/from the OpenAI function/tool wire format in | ||
| [src/provider-kimi/wire-tools.ts](harness-node/src/provider-kimi/wire-tools.ts); | ||
| message translation (text, tool-call, tool-result, system) lives in | ||
| [src/provider-kimi/wire-messages.ts](harness-node/src/provider-kimi/wire-messages.ts). | ||
|
|
||
| ## Registered functions | ||
|
|
||
| - `provider::kimi::stream` — Stream a single assistant turn from Kimi | ||
| Chat Completions into the caller-supplied channel. | ||
| - `provider::kimi::complete` — Legacy: drain a streamed Kimi | ||
| chat-completion and return the final `AssistantMessage`. | ||
|
|
||
| ## Triggers | ||
|
|
||
| None. | ||
|
|
||
| ## State keys | ||
|
|
||
| None — the worker is stateless. | ||
|
|
||
| ## Configuration | ||
|
|
||
| From the `provider_kimi` section of | ||
| [config.yaml](harness-node/config.yaml): | ||
|
|
||
| - `default_max_tokens` (default `8192`) — upper bound for the request's | ||
| `max_completion_tokens` field when the caller omits it. | ||
| - `default_api_url` (default | ||
| `https://api.moonshot.ai/v1/chat/completions`) — endpoint for outbound | ||
| calls. Override to target Moonshot's China endpoint | ||
| (`https://api.moonshot.cn/v1/chat/completions`) or any Chat-Completions | ||
| compatible gateway; `auth-credentials` looks up the credential by the | ||
| `provider` field, so adjust the gateway and the credential together. | ||
|
|
||
| ## Dependencies | ||
|
|
||
| From | ||
| [src/provider-kimi/iii.worker.yaml](harness-node/src/provider-kimi/iii.worker.yaml): | ||
| `auth-credentials ^0.2.0`. The worker also calls the SDK-provided | ||
| `ChannelWriter` injected into the `writer_ref` field of the stream input. | ||
|
|
||
| ## Source layout | ||
|
|
||
| | File | Purpose | | ||
| |---|---| | ||
| | [src/provider-kimi/main.ts](harness-node/src/provider-kimi/main.ts) | Binary entry point (`iii-provider-kimi`). | | ||
| | [src/provider-kimi/register.ts](harness-node/src/provider-kimi/register.ts) | Registers both functions. | | ||
| | [src/provider-kimi/config.ts](harness-node/src/provider-kimi/config.ts) | Loads the `provider_kimi` section. | | ||
| | [src/provider-kimi/types.ts](harness-node/src/provider-kimi/types.ts) | `ChatCompletionsConfig` + `configFromCredential` builder. | | ||
| | [src/provider-kimi/auth.ts](harness-node/src/provider-kimi/auth.ts) | `fetchCredential` (calls `auth::get_token`) + `buildConfig`. | | ||
| | [src/provider-kimi/stream.ts](harness-node/src/provider-kimi/stream.ts) | `streamKimi` async generator: builds the request body, fetches SSE, yields `AssistantMessageEvent`s. | | ||
| | [src/provider-kimi/sse.ts](harness-node/src/provider-kimi/sse.ts) | SSE parser. | | ||
| | [src/provider-kimi/wire-messages.ts](harness-node/src/provider-kimi/wire-messages.ts) | `AgentMessage[]` → Kimi `messages` translation. | | ||
| | [src/provider-kimi/wire-tools.ts](harness-node/src/provider-kimi/wire-tools.ts) | `AgentFunction[]` → Kimi `tools` translation. | | ||
| | [src/provider-kimi/stream-fn.ts](harness-node/src/provider-kimi/stream-fn.ts) | `provider::kimi::stream` handler. | | ||
| | [src/provider-kimi/complete.ts](harness-node/src/provider-kimi/complete.ts) | `provider::kimi::complete` handler (legacy drain-and-return). | | ||
| | [src/provider-kimi/iii.worker.yaml](harness-node/src/provider-kimi/iii.worker.yaml) | Worker manifest. | | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import type { Credential } from '../auth-credentials/types.js'; | ||
| import type { ISdk } from '../runtime/iii.js'; | ||
| import type { WorkerConfig } from './config.js'; | ||
| import { type ChatCompletionsConfig, configFromCredential } from './types.js'; | ||
|
|
||
| // Look up the credential under `kimi-coding`. The compiled Rust | ||
| // auth-credentials worker (the live handler on most engines today) maps | ||
| // `kimi-coding → MOONSHOT_API_KEY` but does not know the bare `kimi` slug | ||
| // our node port added. `kimi-coding` is the portable slug; the user-facing | ||
| // provider name on the AssistantMessage stays `kimi` (see buildConfig). | ||
| const CREDENTIAL_PROVIDER_SLUG = 'kimi-coding'; | ||
|
|
||
| export async function fetchCredential(iii: ISdk): Promise<Credential> { | ||
| const cred = await iii.trigger<unknown, Credential | null>({ | ||
| function_id: 'auth::get_token', | ||
| payload: { provider: CREDENTIAL_PROVIDER_SLUG }, | ||
| timeoutMs: 5_000, | ||
| }); | ||
| if (!cred || typeof cred !== 'object' || !('type' in cred)) { | ||
| throw new Error( | ||
| `auth::get_token returned no credential for provider=${CREDENTIAL_PROVIDER_SLUG} (set MOONSHOT_API_KEY)`, | ||
| ); | ||
| } | ||
| return cred; | ||
| } | ||
|
|
||
| export async function buildConfig( | ||
| iii: ISdk, | ||
| worker: WorkerConfig, | ||
| model: string, | ||
| ): Promise<ChatCompletionsConfig> { | ||
| const cred = await fetchCredential(iii); | ||
| return configFromCredential( | ||
| worker.default_api_url, | ||
| 'kimi', | ||
| model, | ||
| cred, | ||
| worker.default_max_tokens, | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { requireString } from '../runtime/handler.js'; | ||
| import type { ISdk } from '../runtime/iii.js'; | ||
| import type { AgentMessage } from '../types/agent-message.js'; | ||
| import type { AgentFunction } from '../types/function.js'; | ||
| import { buildConfig } from './auth.js'; | ||
| import type { WorkerConfig } from './config.js'; | ||
| import { collect, streamKimi } from './stream.js'; | ||
|
|
||
| export function register(iii: ISdk, worker: WorkerConfig): void { | ||
| iii.registerFunction( | ||
| 'provider::kimi::complete', | ||
| async (payload: unknown) => { | ||
| const obj = (payload ?? {}) as Record<string, unknown>; | ||
| const model = requireString(obj, 'model'); | ||
| const system_prompt = typeof obj.system_prompt === 'string' ? obj.system_prompt : ''; | ||
| const messages = Array.isArray(obj.messages) ? (obj.messages as AgentMessage[]) : []; | ||
| const tools = Array.isArray(obj.tools) ? (obj.tools as AgentFunction[]) : []; | ||
| const cfg = await buildConfig(iii, worker, model); | ||
| return await collect(streamKimi({ cfg, system_prompt, messages, tools })); | ||
| }, | ||
| { | ||
| description: | ||
| 'Legacy: drain a streamed Kimi chat-completion and return the final AssistantMessage.', | ||
| }, | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { getNumber, getSection, getString } from '../runtime/config.js'; | ||
|
|
||
| export type WorkerConfig = { | ||
| default_max_tokens: number; | ||
| default_api_url: string; | ||
| }; | ||
|
|
||
| export const DEFAULT_API_URL = 'https://api.moonshot.ai/v1/chat/completions'; | ||
|
|
||
| export function loadWorkerConfig(cfg: Record<string, unknown>): WorkerConfig { | ||
| const section = getSection(cfg, 'provider_kimi'); | ||
| return { | ||
| default_max_tokens: getNumber(section, 'default_max_tokens', 8192), | ||
| default_api_url: getString(section, 'default_api_url', DEFAULT_API_URL), | ||
| }; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Documentation inconsistency with implementation.
The documentation states the worker calls
auth::get_tokenwithprovider: 'kimi', but according to the PR objectives, the actual implementation inprovider-kimi/auth.tsusesprovider: 'kimi-coding'as a runtime workaround. Update this line to reflect the current implementation or add a note about the temporary workaround.🤖 Prompt for AI Agents