@charivo/llm 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -0
- package/dist/openai/index.d.mts +14 -2
- package/dist/openai/index.d.ts +14 -2
- package/dist/openai/index.js +2 -0
- package/dist/openai/index.js.map +1 -1
- package/dist/openai/index.mjs +1 -1
- package/dist/openai/index.mjs.map +1 -1
- package/dist/openclaw/index.d.mts +50 -3
- package/dist/openclaw/index.d.ts +50 -3
- package/dist/openclaw/index.js +25 -7
- package/dist/openclaw/index.js.map +1 -1
- package/dist/openclaw/index.mjs +24 -8
- package/dist/openclaw/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -55,6 +55,16 @@ Use `maxHistoryTurns: null` to opt out and keep unbounded history.
|
|
|
55
55
|
|
|
56
56
|
- `createLLMManager(client, options?)`
|
|
57
57
|
- `LLMManagerOptions`
|
|
58
|
+
- `@charivo/llm/openai`: `createOpenAILLMClient(config)` (browser client,
|
|
59
|
+
dev/testing only) and, for server-side use, `createOpenAILLMProvider(config)`,
|
|
60
|
+
`OpenAILLMProvider`, `type OpenAILLMConfig`
|
|
61
|
+
- `@charivo/llm/openclaw`: `createOpenClawLLMClient(config)` (browser client,
|
|
62
|
+
dev/testing only) and, for server-side use, `createOpenClawLLMProvider(config)`,
|
|
63
|
+
`OpenClawLLMProvider`, `type OpenClawLLMConfig`. `sessionKey` exists only on
|
|
64
|
+
the provider config — `LLMManager.clearHistory()` can only clear local
|
|
65
|
+
history, not rotate a pinned gateway session, so a client-side `sessionKey`
|
|
66
|
+
would silently replay the old transcript after a reset. Server routes that
|
|
67
|
+
construct the provider directly can rotate `sessionKey` themselves.
|
|
58
68
|
|
|
59
69
|
## Manager API
|
|
60
70
|
|
package/dist/openai/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { LLMClient } from '@charivo/core';
|
|
1
|
+
import { LLMProvider, LLMClient } from '@charivo/core';
|
|
2
2
|
|
|
3
3
|
interface OpenAILLMConfig {
|
|
4
4
|
apiKey: string;
|
|
@@ -7,8 +7,20 @@ interface OpenAILLMConfig {
|
|
|
7
7
|
maxTokens?: number;
|
|
8
8
|
dangerouslyAllowBrowser?: boolean;
|
|
9
9
|
}
|
|
10
|
+
declare class OpenAILLMProvider implements LLMProvider {
|
|
11
|
+
private openai;
|
|
12
|
+
private model;
|
|
13
|
+
private temperature;
|
|
14
|
+
private maxTokens;
|
|
15
|
+
constructor(config: OpenAILLMConfig);
|
|
16
|
+
generateResponse(messages: Array<{
|
|
17
|
+
role: string;
|
|
18
|
+
content: string;
|
|
19
|
+
}>): Promise<string>;
|
|
20
|
+
}
|
|
21
|
+
declare function createOpenAILLMProvider(config: OpenAILLMConfig): OpenAILLMProvider;
|
|
10
22
|
|
|
11
23
|
type OpenAILLMClientConfig = OpenAILLMConfig;
|
|
12
24
|
declare function createOpenAILLMClient(config: OpenAILLMClientConfig): LLMClient;
|
|
13
25
|
|
|
14
|
-
export { type OpenAILLMClientConfig, createOpenAILLMClient };
|
|
26
|
+
export { type OpenAILLMClientConfig, type OpenAILLMConfig, OpenAILLMProvider, createOpenAILLMClient, createOpenAILLMProvider };
|
package/dist/openai/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { LLMClient } from '@charivo/core';
|
|
1
|
+
import { LLMProvider, LLMClient } from '@charivo/core';
|
|
2
2
|
|
|
3
3
|
interface OpenAILLMConfig {
|
|
4
4
|
apiKey: string;
|
|
@@ -7,8 +7,20 @@ interface OpenAILLMConfig {
|
|
|
7
7
|
maxTokens?: number;
|
|
8
8
|
dangerouslyAllowBrowser?: boolean;
|
|
9
9
|
}
|
|
10
|
+
declare class OpenAILLMProvider implements LLMProvider {
|
|
11
|
+
private openai;
|
|
12
|
+
private model;
|
|
13
|
+
private temperature;
|
|
14
|
+
private maxTokens;
|
|
15
|
+
constructor(config: OpenAILLMConfig);
|
|
16
|
+
generateResponse(messages: Array<{
|
|
17
|
+
role: string;
|
|
18
|
+
content: string;
|
|
19
|
+
}>): Promise<string>;
|
|
20
|
+
}
|
|
21
|
+
declare function createOpenAILLMProvider(config: OpenAILLMConfig): OpenAILLMProvider;
|
|
10
22
|
|
|
11
23
|
type OpenAILLMClientConfig = OpenAILLMConfig;
|
|
12
24
|
declare function createOpenAILLMClient(config: OpenAILLMClientConfig): LLMClient;
|
|
13
25
|
|
|
14
|
-
export { type OpenAILLMClientConfig, createOpenAILLMClient };
|
|
26
|
+
export { type OpenAILLMClientConfig, type OpenAILLMConfig, OpenAILLMProvider, createOpenAILLMClient, createOpenAILLMProvider };
|
package/dist/openai/index.js
CHANGED
|
@@ -87,6 +87,8 @@ function createOpenAILLMClient(config) {
|
|
|
87
87
|
return new OpenAILLMClient(config);
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
+
exports.OpenAILLMProvider = OpenAILLMProvider;
|
|
90
91
|
exports.createOpenAILLMClient = createOpenAILLMClient;
|
|
92
|
+
exports.createOpenAILLMProvider = createOpenAILLMProvider;
|
|
91
93
|
//# sourceMappingURL=index.js.map
|
|
92
94
|
//# sourceMappingURL=index.js.map
|
package/dist/openai/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/openai/provider.ts","../../src/openai/index.ts"],"names":["CharivoStateError","OpenAI","toCharivoError","CharivoTimeoutError"],"mappings":";;;;;;;;;;AAQA,IAAM,kBAAA,GAAqB,GAAA;AAUpB,IAAM,oBAAN,MAA+C;AAAA,EAC5C,MAAA;AAAA,EACA,KAAA;AAAA,EACA,WAAA;AAAA,EACA,SAAA;AAAA,EAER,YAAY,MAAA,EAAyB;AACnC,IAAA,IAAI,OAAO,MAAA,KAAW,WAAA,IAAe,CAAC,OAAO,uBAAA,EAAyB;AACpE,MAAA,MAAM,IAAIA,sBAAA;AAAA,QACR;AAAA,OACF;AAAA,IACF;AAEA,IAAA,IAAA,CAAK,MAAA,GAAS,IAAIC,uBAAA,CAAO;AAAA,MACvB,QAAQ,MAAA,CAAO,MAAA;AAAA,MACf,yBAAyB,MAAA,CAAO;AAAA,KACjC,CAAA;AAED,IAAA,IAAA,CAAK,KAAA,GAAQ,OAAO,KAAA,IAAS,cAAA;AAC7B,IAAA,IAAA,CAAK,WAAA,GAAc,OAAO,WAAA,IAAe,GAAA;AACzC,IAAA,IAAA,CAAK,SAAA,GAAY,OAAO,SAAA,IAAa,GAAA;AAAA,EACvC;AAAA,EAEA,MAAM,iBACJ,QAAA,EACiB;AACjB,IAAA,IAAI;AACF,MAAA,MAAM,cAAA,GAAiB,QAAA,CAAS,GAAA,CAAI,CAAC,GAAA,MAAS;AAAA,QAC5C,MAAM,GAAA,CAAI,IAAA;AAAA,QACV,SAAS,GAAA,CAAI;AAAA,OACf,CAAE,CAAA;AAEF,MAAA,MAAM,aAAa,MAAM,WAAA;AAAA,QACvB,IAAA,CAAK,MAAA,CAAO,IAAA,CAAK,WAAA,CAAY,MAAA,CAAO;AAAA,UAClC,OAAO,IAAA,CAAK,KAAA;AAAA,UACZ,QAAA,EAAU,cAAA;AAAA,UACV,aAAa,IAAA,CAAK,WAAA;AAAA,UAClB,YAAY,IAAA,CAAK;AAAA,SAClB,CAAA;AAAA,QACD,sCAAsC,kBAAkB,CAAA,EAAA;AAAA,OAC1D;AAEA,MAAA,OAAO,UAAA,CAAW,OAAA,CAAQ,CAAC,CAAA,EAAG,SAAS,OAAA,IAAW,EAAA;AAAA,IACpD,SAAS,KAAA,EAAO;AACd,MAAA,MAAMC,mBAAA,CAAe,UAAA,EAAY,KAAA,EAAO,2BAA2B,CAAA;AAAA,IACrE;AAAA,EACF;AACF
|
|
1
|
+
{"version":3,"sources":["../../src/openai/provider.ts","../../src/openai/index.ts"],"names":["CharivoStateError","OpenAI","toCharivoError","CharivoTimeoutError"],"mappings":";;;;;;;;;;AAQA,IAAM,kBAAA,GAAqB,GAAA;AAUpB,IAAM,oBAAN,MAA+C;AAAA,EA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AA,CAAe,UAAA,EAAY,KAAA,EAAO,2BAA2B,CAAA;AAAA,IACrE;AAAA,EACF;AACF;AAEO,SAAS,wBACd,MAAA,EACmB;AACnB,EAAA,OAAO,IAAI,kBAAkB,MAAM,CAAA;AACrC;AAEA,eAAe,WAAA,CACb,SACA,cAAA,EACY;AACZ,EAAA,IAAI,SAAA;AAEJ,EAAA,MAAM,cAAA,GAAiB,IAAI,OAAA,CAAW,CAAC,GAAG,MAAA,KAAW;AACnD,IAAA,SAAA,GAAY,UAAA;AAAA,MACV,MAAM,MAAA,CAAO,IAAIC,wBAAA,CAAoB,cAAc,CAAC,CAAA;AAAA,MACpD;AAAA,KACF;AAAA,EACF,CAAC,CAAA;AAED,EAAA,IAAI;AACF,IAAA,OAAO,MAAM,OAAA,CAAQ,IAAA,CAAK,CAAC,OAAA,EAAS,cAAc,CAAC,CAAA;AAAA,EACrD,CAAA,SAAE;AACA,IAAA,IAAI,SAAA,EAAW;AACb,MAAA,YAAA,CAAa,SAAS,CAAA;AAAA,IACxB;AAAA,EACF;AACF;;;ACrEA,IAAM,kBAAN,MAA2C;AAAA,EACjC,QAAA;AAAA,EAER,YAAY,MAAA,EAA+B;AAGzC,IAAA,IAAA,CAAK,WAAW,uBAAA,CAAwB;AAAA,MACtC,GAAG,MAAA;AAAA,MACH,uBAAA,EAAyB;AAAA,KAC1B,CAAA;AAAA,EACH;AAAA,EAEA,MAAM,KACJ,QAAA,EACiB;AAEjB,IAAA,MAAM,gBAAA,GAAmB,MAAM,IAAA,CAAK,QAAA,CAAS,iBAAiB,QAAQ,CAAA;AAEtE,IAAA,OAAO,gBAAA;AAAA,EACT;AACF,CAAA;AAEO,SAAS,sBACd,MAAA,EACW;AACX,EAAA,OAAO,IAAI,gBAAgB,MAAM,CAAA;AACnC","file":"index.js","sourcesContent":["import OpenAI from \"openai\";\nimport {\n CharivoStateError,\n CharivoTimeoutError,\n LLMProvider,\n toCharivoError,\n} from \"@charivo/core\";\n\nconst REQUEST_TIMEOUT_MS = 30_000;\n\nexport interface OpenAILLMConfig {\n apiKey: string;\n model?: string;\n temperature?: number;\n maxTokens?: number;\n dangerouslyAllowBrowser?: boolean;\n}\n\nexport class OpenAILLMProvider implements LLMProvider {\n private openai: OpenAI;\n private model: string;\n private temperature: number;\n private maxTokens: number;\n\n constructor(config: OpenAILLMConfig) {\n if (typeof window !== \"undefined\" && !config.dangerouslyAllowBrowser) {\n throw new CharivoStateError(\n \"OpenAI LLM provider is for server-side use only. Set dangerouslyAllowBrowser: true for testing\",\n );\n }\n\n this.openai = new OpenAI({\n apiKey: config.apiKey,\n dangerouslyAllowBrowser: config.dangerouslyAllowBrowser,\n });\n\n this.model = config.model || \"gpt-4.1-nano\";\n this.temperature = config.temperature || 0.7;\n this.maxTokens = config.maxTokens || 1000;\n }\n\n async generateResponse(\n messages: Array<{ role: string; content: string }>,\n ): Promise<string> {\n try {\n const openAIMessages = messages.map((msg) => ({\n role: msg.role as \"system\" | \"user\" | \"assistant\",\n content: msg.content,\n }));\n\n const completion = await withTimeout(\n this.openai.chat.completions.create({\n model: this.model,\n messages: openAIMessages,\n temperature: this.temperature,\n max_tokens: this.maxTokens,\n }),\n `OpenAI LLM request timed out after ${REQUEST_TIMEOUT_MS}ms`,\n );\n\n return completion.choices[0]?.message?.content || \"\";\n } catch (error) {\n throw toCharivoError(\"provider\", error, \"OpenAI LLM request failed\");\n }\n }\n}\n\nexport function createOpenAILLMProvider(\n config: OpenAILLMConfig,\n): OpenAILLMProvider {\n return new OpenAILLMProvider(config);\n}\n\nasync function withTimeout<T>(\n promise: Promise<T>,\n timeoutMessage: string,\n): Promise<T> {\n let timeoutId: ReturnType<typeof setTimeout> | undefined;\n\n const timeoutPromise = new Promise<T>((_, reject) => {\n timeoutId = setTimeout(\n () => reject(new CharivoTimeoutError(timeoutMessage)),\n REQUEST_TIMEOUT_MS,\n );\n });\n\n try {\n return await Promise.race([promise, timeoutPromise]);\n } finally {\n if (timeoutId) {\n clearTimeout(timeoutId);\n }\n }\n}\n","import type { LLMClient } from \"@charivo/core\";\nimport {\n createOpenAILLMProvider,\n OpenAILLMConfig,\n OpenAILLMProvider,\n} from \"./provider\";\n\nexport {\n createOpenAILLMProvider,\n OpenAILLMProvider,\n type OpenAILLMConfig,\n} from \"./provider\";\n\n// Use OpenAILLMConfig directly\nexport type OpenAILLMClientConfig = OpenAILLMConfig;\n\n/**\n * OpenAI LLM Client - Stateless client that wraps the OpenAI provider for direct use on the client\n *\n * For use in local development or test environments. Not recommended for production for security reasons.\n * The API key is exposed to the client, so use it only in a server environment or for testing purposes.\n *\n * Stateless design: session management is handled externally, and this client only handles API calls\n */\nclass OpenAILLMClient implements LLMClient {\n private provider: OpenAILLMProvider;\n\n constructor(config: OpenAILLMClientConfig) {\n // Intentional dev/test escape hatch: this direct browser client exposes\n // credentials. For production, see docs/guide/choosing-packages.md#remote.\n this.provider = createOpenAILLMProvider({\n ...config,\n dangerouslyAllowBrowser: true,\n });\n }\n\n async call(\n messages: Array<{ role: string; content: string }>,\n ): Promise<string> {\n // Generate a response using the provider\n const assistantMessage = await this.provider.generateResponse(messages);\n\n return assistantMessage;\n }\n}\n\nexport function createOpenAILLMClient(\n config: OpenAILLMClientConfig,\n): LLMClient {\n return new OpenAILLMClient(config);\n}\n"]}
|
package/dist/openai/index.mjs
CHANGED
|
@@ -81,6 +81,6 @@ function createOpenAILLMClient(config) {
|
|
|
81
81
|
return new OpenAILLMClient(config);
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
export { createOpenAILLMClient };
|
|
84
|
+
export { OpenAILLMProvider, createOpenAILLMClient, createOpenAILLMProvider };
|
|
85
85
|
//# sourceMappingURL=index.mjs.map
|
|
86
86
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/openai/provider.ts","../../src/openai/index.ts"],"names":[],"mappings":";;;;AAQA,IAAM,kBAAA,GAAqB,GAAA;AAUpB,IAAM,oBAAN,MAA+C;AAAA,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A;AAEO,SAAS,sBACd,MAAA,EACW;AACX,EAAA,OAAO,IAAI,gBAAgB,MAAM,CAAA;AACnC","file":"index.mjs","sourcesContent":["import OpenAI from \"openai\";\nimport {\n CharivoStateError,\n CharivoTimeoutError,\n LLMProvider,\n toCharivoError,\n} from \"@charivo/core\";\n\nconst REQUEST_TIMEOUT_MS = 30_000;\n\nexport interface OpenAILLMConfig {\n apiKey: string;\n model?: string;\n temperature?: number;\n maxTokens?: number;\n dangerouslyAllowBrowser?: boolean;\n}\n\nexport class OpenAILLMProvider implements LLMProvider {\n private openai: OpenAI;\n private model: string;\n private temperature: number;\n private maxTokens: number;\n\n constructor(config: OpenAILLMConfig) {\n if (typeof window !== \"undefined\" && !config.dangerouslyAllowBrowser) {\n throw new CharivoStateError(\n \"OpenAI LLM provider is for server-side use only. Set dangerouslyAllowBrowser: true for testing\",\n );\n }\n\n this.openai = new OpenAI({\n apiKey: config.apiKey,\n dangerouslyAllowBrowser: config.dangerouslyAllowBrowser,\n });\n\n this.model = config.model || \"gpt-4.1-nano\";\n this.temperature = config.temperature || 0.7;\n this.maxTokens = config.maxTokens || 1000;\n }\n\n async generateResponse(\n messages: Array<{ role: string; content: string }>,\n ): Promise<string> {\n try {\n const openAIMessages = messages.map((msg) => ({\n role: msg.role as \"system\" | \"user\" | \"assistant\",\n content: msg.content,\n }));\n\n const completion = await withTimeout(\n this.openai.chat.completions.create({\n model: this.model,\n messages: openAIMessages,\n temperature: this.temperature,\n max_tokens: this.maxTokens,\n }),\n `OpenAI LLM request timed out after ${REQUEST_TIMEOUT_MS}ms`,\n );\n\n return completion.choices[0]?.message?.content || \"\";\n } catch (error) {\n throw toCharivoError(\"provider\", error, \"OpenAI LLM request failed\");\n }\n }\n}\n\nexport function createOpenAILLMProvider(\n config: OpenAILLMConfig,\n): OpenAILLMProvider {\n return new OpenAILLMProvider(config);\n}\n\nasync function withTimeout<T>(\n promise: Promise<T>,\n timeoutMessage: string,\n): Promise<T> {\n let timeoutId: ReturnType<typeof setTimeout> | undefined;\n\n const timeoutPromise = new Promise<T>((_, reject) => {\n timeoutId = setTimeout(\n () => reject(new CharivoTimeoutError(timeoutMessage)),\n REQUEST_TIMEOUT_MS,\n );\n });\n\n try {\n return await Promise.race([promise, timeoutPromise]);\n } finally {\n if (timeoutId) {\n clearTimeout(timeoutId);\n }\n }\n}\n","import type { LLMClient } from \"@charivo/core\";\nimport {\n createOpenAILLMProvider,\n OpenAILLMConfig,\n OpenAILLMProvider,\n} from \"./provider\";\n\nexport {\n createOpenAILLMProvider,\n OpenAILLMProvider,\n type OpenAILLMConfig,\n} from \"./provider\";\n\n// Use OpenAILLMConfig directly\nexport type OpenAILLMClientConfig = OpenAILLMConfig;\n\n/**\n * OpenAI LLM Client - Stateless client that wraps the OpenAI provider for direct use on the client\n *\n * For use in local development or test environments. Not recommended for production for security reasons.\n * The API key is exposed to the client, so use it only in a server environment or for testing purposes.\n *\n * Stateless design: session management is handled externally, and this client only handles API calls\n */\nclass OpenAILLMClient implements LLMClient {\n private provider: OpenAILLMProvider;\n\n constructor(config: OpenAILLMClientConfig) {\n // Intentional dev/test escape hatch: this direct browser client exposes\n // credentials. For production, see docs/guide/choosing-packages.md#remote.\n this.provider = createOpenAILLMProvider({\n ...config,\n dangerouslyAllowBrowser: true,\n });\n }\n\n async call(\n messages: Array<{ role: string; content: string }>,\n ): Promise<string> {\n // Generate a response using the provider\n const assistantMessage = await this.provider.generateResponse(messages);\n\n return assistantMessage;\n }\n}\n\nexport function createOpenAILLMClient(\n config: OpenAILLMClientConfig,\n): LLMClient {\n return new OpenAILLMClient(config);\n}\n"]}
|
|
@@ -1,16 +1,63 @@
|
|
|
1
|
-
import { LLMClient } from '@charivo/core';
|
|
1
|
+
import { LLMProvider, LLMClient } from '@charivo/core';
|
|
2
2
|
|
|
3
3
|
interface OpenClawLLMConfig {
|
|
4
4
|
token: string;
|
|
5
5
|
baseURL?: string;
|
|
6
|
+
/**
|
|
7
|
+
* Target a specific agent on the gateway.
|
|
8
|
+
* When omitted, the gateway's configured default agent handles the request.
|
|
9
|
+
*/
|
|
6
10
|
agentId?: string;
|
|
11
|
+
/**
|
|
12
|
+
* Agent target, not a backend model name. The gateway resolves this to an
|
|
13
|
+
* agent: `openclaw`, `openclaw/default`, or `openclaw/<agentId>`.
|
|
14
|
+
*
|
|
15
|
+
* @default "openclaw/default"
|
|
16
|
+
*/
|
|
7
17
|
model?: string;
|
|
8
18
|
temperature?: number;
|
|
9
19
|
maxTokens?: number;
|
|
20
|
+
/**
|
|
21
|
+
* Pins the conversation to a server-side session on the gateway.
|
|
22
|
+
*
|
|
23
|
+
* The gateway keeps conversation state itself, but without an identifier it
|
|
24
|
+
* opens a fresh session per request: nothing is persisted between turns and
|
|
25
|
+
* each one strands a throwaway session. Sending this as the `user` field makes
|
|
26
|
+
* the gateway derive a stable session key from it.
|
|
27
|
+
*
|
|
28
|
+
* When set, past turns are not resent — the gateway already holds them. The
|
|
29
|
+
* system prompt is still sent every turn so the persona survives a dropped
|
|
30
|
+
* session.
|
|
31
|
+
*
|
|
32
|
+
* Treat it as a conversation-scoped identifier (e.g. a UUID per conversation,
|
|
33
|
+
* rotated when the user resets the chat). Rotating it is the only way to reset
|
|
34
|
+
* the conversation; the gateway keeps the old transcript under the old key.
|
|
35
|
+
*/
|
|
36
|
+
sessionKey?: string;
|
|
10
37
|
dangerouslyAllowBrowser?: boolean;
|
|
11
38
|
}
|
|
39
|
+
declare class OpenClawLLMProvider implements LLMProvider {
|
|
40
|
+
private openai;
|
|
41
|
+
private model;
|
|
42
|
+
private temperature;
|
|
43
|
+
private maxTokens;
|
|
44
|
+
private sessionKey?;
|
|
45
|
+
constructor(config: OpenClawLLMConfig);
|
|
46
|
+
generateResponse(messages: Array<{
|
|
47
|
+
role: string;
|
|
48
|
+
content: string;
|
|
49
|
+
}>): Promise<string>;
|
|
50
|
+
/**
|
|
51
|
+
* With a pinned session the gateway already holds the past turns, and resending
|
|
52
|
+
* them would flatten a duplicate copy of the history on top of its own. Keep the
|
|
53
|
+
* system prompts (cheap persona insurance if the session was dropped) and the
|
|
54
|
+
* latest turn.
|
|
55
|
+
*/
|
|
56
|
+
private selectMessages;
|
|
57
|
+
}
|
|
58
|
+
declare function createOpenClawLLMProvider(config: OpenClawLLMConfig): OpenClawLLMProvider;
|
|
12
59
|
|
|
13
|
-
type OpenClawLLMClientConfig = OpenClawLLMConfig
|
|
60
|
+
type OpenClawLLMClientConfig = Omit<OpenClawLLMConfig, "sessionKey">;
|
|
14
61
|
declare function createOpenClawLLMClient(config: OpenClawLLMClientConfig): LLMClient;
|
|
15
62
|
|
|
16
|
-
export { type OpenClawLLMClientConfig, createOpenClawLLMClient };
|
|
63
|
+
export { type OpenClawLLMClientConfig, type OpenClawLLMConfig, OpenClawLLMProvider, createOpenClawLLMClient, createOpenClawLLMProvider };
|
package/dist/openclaw/index.d.ts
CHANGED
|
@@ -1,16 +1,63 @@
|
|
|
1
|
-
import { LLMClient } from '@charivo/core';
|
|
1
|
+
import { LLMProvider, LLMClient } from '@charivo/core';
|
|
2
2
|
|
|
3
3
|
interface OpenClawLLMConfig {
|
|
4
4
|
token: string;
|
|
5
5
|
baseURL?: string;
|
|
6
|
+
/**
|
|
7
|
+
* Target a specific agent on the gateway.
|
|
8
|
+
* When omitted, the gateway's configured default agent handles the request.
|
|
9
|
+
*/
|
|
6
10
|
agentId?: string;
|
|
11
|
+
/**
|
|
12
|
+
* Agent target, not a backend model name. The gateway resolves this to an
|
|
13
|
+
* agent: `openclaw`, `openclaw/default`, or `openclaw/<agentId>`.
|
|
14
|
+
*
|
|
15
|
+
* @default "openclaw/default"
|
|
16
|
+
*/
|
|
7
17
|
model?: string;
|
|
8
18
|
temperature?: number;
|
|
9
19
|
maxTokens?: number;
|
|
20
|
+
/**
|
|
21
|
+
* Pins the conversation to a server-side session on the gateway.
|
|
22
|
+
*
|
|
23
|
+
* The gateway keeps conversation state itself, but without an identifier it
|
|
24
|
+
* opens a fresh session per request: nothing is persisted between turns and
|
|
25
|
+
* each one strands a throwaway session. Sending this as the `user` field makes
|
|
26
|
+
* the gateway derive a stable session key from it.
|
|
27
|
+
*
|
|
28
|
+
* When set, past turns are not resent — the gateway already holds them. The
|
|
29
|
+
* system prompt is still sent every turn so the persona survives a dropped
|
|
30
|
+
* session.
|
|
31
|
+
*
|
|
32
|
+
* Treat it as a conversation-scoped identifier (e.g. a UUID per conversation,
|
|
33
|
+
* rotated when the user resets the chat). Rotating it is the only way to reset
|
|
34
|
+
* the conversation; the gateway keeps the old transcript under the old key.
|
|
35
|
+
*/
|
|
36
|
+
sessionKey?: string;
|
|
10
37
|
dangerouslyAllowBrowser?: boolean;
|
|
11
38
|
}
|
|
39
|
+
declare class OpenClawLLMProvider implements LLMProvider {
|
|
40
|
+
private openai;
|
|
41
|
+
private model;
|
|
42
|
+
private temperature;
|
|
43
|
+
private maxTokens;
|
|
44
|
+
private sessionKey?;
|
|
45
|
+
constructor(config: OpenClawLLMConfig);
|
|
46
|
+
generateResponse(messages: Array<{
|
|
47
|
+
role: string;
|
|
48
|
+
content: string;
|
|
49
|
+
}>): Promise<string>;
|
|
50
|
+
/**
|
|
51
|
+
* With a pinned session the gateway already holds the past turns, and resending
|
|
52
|
+
* them would flatten a duplicate copy of the history on top of its own. Keep the
|
|
53
|
+
* system prompts (cheap persona insurance if the session was dropped) and the
|
|
54
|
+
* latest turn.
|
|
55
|
+
*/
|
|
56
|
+
private selectMessages;
|
|
57
|
+
}
|
|
58
|
+
declare function createOpenClawLLMProvider(config: OpenClawLLMConfig): OpenClawLLMProvider;
|
|
12
59
|
|
|
13
|
-
type OpenClawLLMClientConfig = OpenClawLLMConfig
|
|
60
|
+
type OpenClawLLMClientConfig = Omit<OpenClawLLMConfig, "sessionKey">;
|
|
14
61
|
declare function createOpenClawLLMClient(config: OpenClawLLMClientConfig): LLMClient;
|
|
15
62
|
|
|
16
|
-
export { type OpenClawLLMClientConfig, createOpenClawLLMClient };
|
|
63
|
+
export { type OpenClawLLMClientConfig, type OpenClawLLMConfig, OpenClawLLMProvider, createOpenClawLLMClient, createOpenClawLLMProvider };
|
package/dist/openclaw/index.js
CHANGED
|
@@ -13,6 +13,7 @@ var OpenClawLLMProvider = class {
|
|
|
13
13
|
model;
|
|
14
14
|
temperature;
|
|
15
15
|
maxTokens;
|
|
16
|
+
sessionKey;
|
|
16
17
|
constructor(config) {
|
|
17
18
|
if (typeof window !== "undefined" && !config.dangerouslyAllowBrowser) {
|
|
18
19
|
throw new core.CharivoStateError(
|
|
@@ -22,18 +23,17 @@ var OpenClawLLMProvider = class {
|
|
|
22
23
|
this.openai = new OpenAI__default.default({
|
|
23
24
|
apiKey: config.token,
|
|
24
25
|
baseURL: config.baseURL || "http://127.0.0.1:18789/v1",
|
|
25
|
-
defaultHeaders: {
|
|
26
|
-
"x-openclaw-agent-id": config.agentId || "main"
|
|
27
|
-
},
|
|
26
|
+
defaultHeaders: config.agentId ? { "x-openclaw-agent-id": config.agentId } : void 0,
|
|
28
27
|
dangerouslyAllowBrowser: config.dangerouslyAllowBrowser
|
|
29
28
|
});
|
|
30
|
-
this.model = config.model || "openclaw";
|
|
29
|
+
this.model = config.model || "openclaw/default";
|
|
31
30
|
this.temperature = config.temperature ?? 0.7;
|
|
32
31
|
this.maxTokens = config.maxTokens || 1e3;
|
|
32
|
+
this.sessionKey = config.sessionKey;
|
|
33
33
|
}
|
|
34
34
|
async generateResponse(messages) {
|
|
35
35
|
try {
|
|
36
|
-
const openAIMessages = messages.map((msg) => ({
|
|
36
|
+
const openAIMessages = this.selectMessages(messages).map((msg) => ({
|
|
37
37
|
role: msg.role,
|
|
38
38
|
content: msg.content
|
|
39
39
|
}));
|
|
@@ -41,13 +41,28 @@ var OpenClawLLMProvider = class {
|
|
|
41
41
|
model: this.model,
|
|
42
42
|
messages: openAIMessages,
|
|
43
43
|
temperature: this.temperature,
|
|
44
|
-
max_tokens: this.maxTokens
|
|
44
|
+
max_tokens: this.maxTokens,
|
|
45
|
+
...this.sessionKey ? { user: this.sessionKey } : {}
|
|
45
46
|
});
|
|
46
47
|
return completion.choices[0]?.message?.content || "";
|
|
47
48
|
} catch (error) {
|
|
48
49
|
throw core.toCharivoError("provider", error, "OpenClaw LLM request failed");
|
|
49
50
|
}
|
|
50
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* With a pinned session the gateway already holds the past turns, and resending
|
|
54
|
+
* them would flatten a duplicate copy of the history on top of its own. Keep the
|
|
55
|
+
* system prompts (cheap persona insurance if the session was dropped) and the
|
|
56
|
+
* latest turn.
|
|
57
|
+
*/
|
|
58
|
+
selectMessages(messages) {
|
|
59
|
+
const latest = messages[messages.length - 1];
|
|
60
|
+
if (!this.sessionKey || !latest) {
|
|
61
|
+
return messages;
|
|
62
|
+
}
|
|
63
|
+
const systemMessages = messages.filter((msg) => msg.role === "system");
|
|
64
|
+
return latest.role === "system" ? systemMessages : [...systemMessages, latest];
|
|
65
|
+
}
|
|
51
66
|
};
|
|
52
67
|
function createOpenClawLLMProvider(config) {
|
|
53
68
|
return new OpenClawLLMProvider(config);
|
|
@@ -57,8 +72,9 @@ function createOpenClawLLMProvider(config) {
|
|
|
57
72
|
var OpenClawLLMClient = class {
|
|
58
73
|
provider;
|
|
59
74
|
constructor(config) {
|
|
75
|
+
const { sessionKey: _sessionKey, ...rest } = config;
|
|
60
76
|
this.provider = createOpenClawLLMProvider({
|
|
61
|
-
...
|
|
77
|
+
...rest,
|
|
62
78
|
dangerouslyAllowBrowser: true
|
|
63
79
|
});
|
|
64
80
|
}
|
|
@@ -70,6 +86,8 @@ function createOpenClawLLMClient(config) {
|
|
|
70
86
|
return new OpenClawLLMClient(config);
|
|
71
87
|
}
|
|
72
88
|
|
|
89
|
+
exports.OpenClawLLMProvider = OpenClawLLMProvider;
|
|
73
90
|
exports.createOpenClawLLMClient = createOpenClawLLMClient;
|
|
91
|
+
exports.createOpenClawLLMProvider = createOpenClawLLMProvider;
|
|
74
92
|
//# sourceMappingURL=index.js.map
|
|
75
93
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/openclaw/provider.ts","../../src/openclaw/index.ts"],"names":["CharivoStateError","OpenAI","toCharivoError"],"mappings":";;;;;;;;;;
|
|
1
|
+
{"version":3,"sources":["../../src/openclaw/provider.ts","../../src/openclaw/index.ts"],"names":["CharivoStateError","OpenAI","toCharivoError"],"mappings":";;;;;;;;;;AAwCO,IAAM,sBAAN,MAAiD;AAAA,EAC9C,MAAA;AAAA,EACA,KAAA;AAAA,EACA,WAAA;AAAA,EACA,SAAA;AAAA,EACA,UAAA;AAAA,EAER,YAAY,MAAA,EAA2B;AACrC,IAAA,IAAI,OAAO,MAAA,KAAW,WAAA,IAAe,CAAC,OAAO,uBAAA,EAAyB;AACpE,MAAA,MAAM,IAAIA,sBAAA;AAAA,QACR;AAAA,OACF;AAAA,IACF;AAEA,IAAA,IAAA,CAAK,MAAA,GAAS,IAAIC,uBAAA,CAAO;AAAA,MACvB,QAAQ,MAAA,CAAO,KAAA;AAAA,MACf,OAAA,EAAS,OAAO,OAAA,IAAW,2BAAA;AAAA,MAC3B,gBAAgB,MAAA,CAAO,OAAA,GACnB,EAAE,qBAAA,EAAuB,MAAA,CAAO,SAAQ,GACxC,MAAA;AAAA,MACJ,yBAAyB,MAAA,CAAO;AAAA,KACjC,CAAA;AAED,IAAA,IAAA,CAAK,KAAA,GAAQ,OAAO,KAAA,IAAS,kBAAA;AAC7B,IAAA,IAAA,CAAK,WAAA,GAAc,OAAO,WAAA,IAAe,GAAA;AACzC,IAAA,IAAA,CAAK,SAAA,GAAY,OAAO,SAAA,IAAa,GAAA;AACrC,IAAA,IAAA,CAAK,aAAa,MAAA,CAAO,UAAA;AAAA,EAC3B;AAAA,EAEA,MAAM,iBACJ,QAAA,EACiB;AACjB,IAAA,IAAI;AACF,MAAA,MAAM,iBAAiB,IAAA,CAAK,cAAA,CAAe,QAAQ,CAAA,CAAE,GAAA,CAAI,CAAC,GAAA,MAAS;AAAA,QACj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EAAE,UAAA,EAAY,WAAA,EAAa,GAAG,MAAK,GAAI,MAAA;AAI7C,IAAA,IAAA,CAAK,WAAW,yBAAA,CAA0B;AAAA,MACxC,GAAG,IAAA;AAAA,MACH,uBAAA,EAAyB;AAAA,KAC1B,CAAA;AAAA,EACH;AAAA,EAEA,MAAM,KACJ,QAAA,EACiB;AACjB,IAAA,OAAO,IAAA,CAAK,QAAA,CAAS,gBAAA,CAAiB,QAAQ,CAAA;AAAA,EAChD;AACF,CAAA;AAEO,SAAS,wBACd,MAAA,EACW;AACX,EAAA,OAAO,IAAI,kBAAkB,MAAM,CAAA;AACrC","file":"index.js","sourcesContent":["import OpenAI from \"openai\";\nimport { CharivoStateError, LLMProvider, toCharivoError } from \"@charivo/core\";\n\nexport interface OpenClawLLMConfig {\n token: string;\n baseURL?: string;\n /**\n * Target a specific agent on the gateway.\n * When omitted, the gateway's configured default agent handles the request.\n */\n agentId?: string;\n /**\n * Agent target, not a backend model name. The gateway resolves this to an\n * agent: `openclaw`, `openclaw/default`, or `openclaw/<agentId>`.\n *\n * @default \"openclaw/default\"\n */\n model?: string;\n temperature?: number;\n maxTokens?: number;\n /**\n * Pins the conversation to a server-side session on the gateway.\n *\n * The gateway keeps conversation state itself, but without an identifier it\n * opens a fresh session per request: nothing is persisted between turns and\n * each one strands a throwaway session. Sending this as the `user` field makes\n * the gateway derive a stable session key from it.\n *\n * When set, past turns are not resent — the gateway already holds them. The\n * system prompt is still sent every turn so the persona survives a dropped\n * session.\n *\n * Treat it as a conversation-scoped identifier (e.g. a UUID per conversation,\n * rotated when the user resets the chat). Rotating it is the only way to reset\n * the conversation; the gateway keeps the old transcript under the old key.\n */\n sessionKey?: string;\n dangerouslyAllowBrowser?: boolean;\n}\n\nexport class OpenClawLLMProvider implements LLMProvider {\n private openai: OpenAI;\n private model: string;\n private temperature: number;\n private maxTokens: number;\n private sessionKey?: string;\n\n constructor(config: OpenClawLLMConfig) {\n if (typeof window !== \"undefined\" && !config.dangerouslyAllowBrowser) {\n throw new CharivoStateError(\n \"OpenClaw LLM provider is for server-side use only. Set dangerouslyAllowBrowser: true for testing\",\n );\n }\n\n this.openai = new OpenAI({\n apiKey: config.token,\n baseURL: config.baseURL || \"http://127.0.0.1:18789/v1\",\n defaultHeaders: config.agentId\n ? { \"x-openclaw-agent-id\": config.agentId }\n : undefined,\n dangerouslyAllowBrowser: config.dangerouslyAllowBrowser,\n });\n\n this.model = config.model || \"openclaw/default\";\n this.temperature = config.temperature ?? 0.7;\n this.maxTokens = config.maxTokens || 1000;\n this.sessionKey = config.sessionKey;\n }\n\n async generateResponse(\n messages: Array<{ role: string; content: string }>,\n ): Promise<string> {\n try {\n const openAIMessages = this.selectMessages(messages).map((msg) => ({\n role: msg.role as \"system\" | \"user\" | \"assistant\",\n content: msg.content,\n }));\n\n const completion = await this.openai.chat.completions.create({\n model: this.model,\n messages: openAIMessages,\n temperature: this.temperature,\n max_tokens: this.maxTokens,\n ...(this.sessionKey ? { user: this.sessionKey } : {}),\n });\n\n return completion.choices[0]?.message?.content || \"\";\n } catch (error) {\n throw toCharivoError(\"provider\", error, \"OpenClaw LLM request failed\");\n }\n }\n\n /**\n * With a pinned session the gateway already holds the past turns, and resending\n * them would flatten a duplicate copy of the history on top of its own. Keep the\n * system prompts (cheap persona insurance if the session was dropped) and the\n * latest turn.\n */\n private selectMessages(\n messages: Array<{ role: string; content: string }>,\n ): Array<{ role: string; content: string }> {\n const latest = messages[messages.length - 1];\n if (!this.sessionKey || !latest) {\n return messages;\n }\n\n const systemMessages = messages.filter((msg) => msg.role === \"system\");\n return latest.role === \"system\"\n ? systemMessages\n : [...systemMessages, latest];\n }\n}\n\nexport function createOpenClawLLMProvider(\n config: OpenClawLLMConfig,\n): OpenClawLLMProvider {\n return new OpenClawLLMProvider(config);\n}\n","import type { LLMClient } from \"@charivo/core\";\nimport {\n createOpenClawLLMProvider,\n OpenClawLLMConfig,\n OpenClawLLMProvider,\n} from \"./provider\";\n\nexport {\n createOpenClawLLMProvider,\n OpenClawLLMProvider,\n type OpenClawLLMConfig,\n} from \"./provider\";\n\n// No `sessionKey` here, unlike the provider config. Pinning the gateway\n// session requires rotating it on reset, and this client is driven by\n// LLMManager, whose clearHistory()/character switch clear only local history and\n// cannot reach the client. A pinned session would survive that reset and silently\n// replay the old conversation. Server routes construct the provider themselves and\n// can rotate the key, so the option lives there instead.\nexport type OpenClawLLMClientConfig = Omit<OpenClawLLMConfig, \"sessionKey\">;\n\n/**\n * OpenClaw LLM Client - Stateless client that wraps the OpenClaw provider for direct use on the client\n *\n * For use in local development or test environments. Not recommended for production for security reasons.\n * The token is exposed to the client, so use it only in a server environment or for testing purposes.\n *\n * Stateless design: session management is handled externally, and this client only handles API calls\n */\nclass OpenClawLLMClient implements LLMClient {\n private provider: OpenClawLLMProvider;\n\n constructor(config: OpenClawLLMClientConfig) {\n // Strip sessionKey at runtime too: an untyped JS caller can bypass the\n // Omit above and pass it anyway.\n const { sessionKey: _sessionKey, ...rest } = config as OpenClawLLMConfig;\n\n // Intentional dev/test escape hatch: this direct browser client exposes\n // credentials. For production, see docs/guide/choosing-packages.md#remote.\n this.provider = createOpenClawLLMProvider({\n ...rest,\n dangerouslyAllowBrowser: true,\n });\n }\n\n async call(\n messages: Array<{ role: string; content: string }>,\n ): Promise<string> {\n return this.provider.generateResponse(messages);\n }\n}\n\nexport function createOpenClawLLMClient(\n config: OpenClawLLMClientConfig,\n): LLMClient {\n return new OpenClawLLMClient(config);\n}\n"]}
|
package/dist/openclaw/index.mjs
CHANGED
|
@@ -7,6 +7,7 @@ var OpenClawLLMProvider = class {
|
|
|
7
7
|
model;
|
|
8
8
|
temperature;
|
|
9
9
|
maxTokens;
|
|
10
|
+
sessionKey;
|
|
10
11
|
constructor(config) {
|
|
11
12
|
if (typeof window !== "undefined" && !config.dangerouslyAllowBrowser) {
|
|
12
13
|
throw new CharivoStateError(
|
|
@@ -16,18 +17,17 @@ var OpenClawLLMProvider = class {
|
|
|
16
17
|
this.openai = new OpenAI({
|
|
17
18
|
apiKey: config.token,
|
|
18
19
|
baseURL: config.baseURL || "http://127.0.0.1:18789/v1",
|
|
19
|
-
defaultHeaders: {
|
|
20
|
-
"x-openclaw-agent-id": config.agentId || "main"
|
|
21
|
-
},
|
|
20
|
+
defaultHeaders: config.agentId ? { "x-openclaw-agent-id": config.agentId } : void 0,
|
|
22
21
|
dangerouslyAllowBrowser: config.dangerouslyAllowBrowser
|
|
23
22
|
});
|
|
24
|
-
this.model = config.model || "openclaw";
|
|
23
|
+
this.model = config.model || "openclaw/default";
|
|
25
24
|
this.temperature = config.temperature ?? 0.7;
|
|
26
25
|
this.maxTokens = config.maxTokens || 1e3;
|
|
26
|
+
this.sessionKey = config.sessionKey;
|
|
27
27
|
}
|
|
28
28
|
async generateResponse(messages) {
|
|
29
29
|
try {
|
|
30
|
-
const openAIMessages = messages.map((msg) => ({
|
|
30
|
+
const openAIMessages = this.selectMessages(messages).map((msg) => ({
|
|
31
31
|
role: msg.role,
|
|
32
32
|
content: msg.content
|
|
33
33
|
}));
|
|
@@ -35,13 +35,28 @@ var OpenClawLLMProvider = class {
|
|
|
35
35
|
model: this.model,
|
|
36
36
|
messages: openAIMessages,
|
|
37
37
|
temperature: this.temperature,
|
|
38
|
-
max_tokens: this.maxTokens
|
|
38
|
+
max_tokens: this.maxTokens,
|
|
39
|
+
...this.sessionKey ? { user: this.sessionKey } : {}
|
|
39
40
|
});
|
|
40
41
|
return completion.choices[0]?.message?.content || "";
|
|
41
42
|
} catch (error) {
|
|
42
43
|
throw toCharivoError("provider", error, "OpenClaw LLM request failed");
|
|
43
44
|
}
|
|
44
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* With a pinned session the gateway already holds the past turns, and resending
|
|
48
|
+
* them would flatten a duplicate copy of the history on top of its own. Keep the
|
|
49
|
+
* system prompts (cheap persona insurance if the session was dropped) and the
|
|
50
|
+
* latest turn.
|
|
51
|
+
*/
|
|
52
|
+
selectMessages(messages) {
|
|
53
|
+
const latest = messages[messages.length - 1];
|
|
54
|
+
if (!this.sessionKey || !latest) {
|
|
55
|
+
return messages;
|
|
56
|
+
}
|
|
57
|
+
const systemMessages = messages.filter((msg) => msg.role === "system");
|
|
58
|
+
return latest.role === "system" ? systemMessages : [...systemMessages, latest];
|
|
59
|
+
}
|
|
45
60
|
};
|
|
46
61
|
function createOpenClawLLMProvider(config) {
|
|
47
62
|
return new OpenClawLLMProvider(config);
|
|
@@ -51,8 +66,9 @@ function createOpenClawLLMProvider(config) {
|
|
|
51
66
|
var OpenClawLLMClient = class {
|
|
52
67
|
provider;
|
|
53
68
|
constructor(config) {
|
|
69
|
+
const { sessionKey: _sessionKey, ...rest } = config;
|
|
54
70
|
this.provider = createOpenClawLLMProvider({
|
|
55
|
-
...
|
|
71
|
+
...rest,
|
|
56
72
|
dangerouslyAllowBrowser: true
|
|
57
73
|
});
|
|
58
74
|
}
|
|
@@ -64,6 +80,6 @@ function createOpenClawLLMClient(config) {
|
|
|
64
80
|
return new OpenClawLLMClient(config);
|
|
65
81
|
}
|
|
66
82
|
|
|
67
|
-
export { createOpenClawLLMClient };
|
|
83
|
+
export { OpenClawLLMProvider, createOpenClawLLMClient, createOpenClawLLMProvider };
|
|
68
84
|
//# sourceMappingURL=index.mjs.map
|
|
69
85
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/openclaw/provider.ts","../../src/openclaw/index.ts"],"names":[],"mappings":";;;;
|
|
1
|
+
{"version":3,"sources":["../../src/openclaw/provider.ts","../../src/openclaw/index.ts"],"names":[],"mappings":";;;;AAwCO,IAAM,sBAAN,MAAiD;AAAA,EAC9C,MAAA;AAAA,EACA,KAAA;AAAA,EACA,WAAA;AAAA,EACA,SAAA;AAAA,EACA,UAAA;AAAA,EAER,YAAY,MAAA,EAA2B;AACrC,IAAA,IAAI,OAAO,MAAA,KAAW,WAAA,IAAe,CAAC,OAAO,uBAAA,EAAyB;AACpE,MAAA,MAAM,IAAI,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A,EAAO;AACd,MAAA,MAAM,cAAA,CAAe,UAAA,EAAY,KAAA,EAAO,6BAA6B,CAAA;AAAA,IACvE;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQQ,eACN,QAAA,EAC0C;AAC1C,IAAA,MAAM,MAAA,GAAS,QAAA,CAAS,QAAA,CAAS,MAAA,GAAS,CAAC,CAAA;AAC3C,IAAA,IAAI,CAAC,IAAA,CAAK,UAAA,IAAc,CAAC,MAAA,EAAQ;AAC/B,MAAA,OAAO,QAAA;AAAA,IACT;AAEA,IAAA,MAAM,iBAAiB,QAAA,CAAS,MAAA,CAAO,CAAC,GAAA,KAAQ,GAAA,CAAI,SAAS,QAAQ,CAAA;AACrE,IAAA,OAAO,OAAO,IAAA,KAAS,QAAA,GACnB,iBACA,CAAC,GAAG,gBAAgB,MAAM,CAAA;AAAA,EAChC;AACF;AAEO,SAAS,0BACd,MAAA,EACqB;AACrB,EAAA,OAAO,IAAI,oBAAoB,MAAM,CAAA;AACvC;;;ACxFA,IAAM,oBAAN,MAA6C;AAAA,EACnC,QAAA;AAAA,EAER,YAAY,MAAA,EAAiC;AAG3C,IAAA,MAAM,EAAE,UAAA,EAAY,WAAA,EAAa,GAAG,MAAK,GAAI,MAAA;AAI7C,IAAA,IAAA,CAAK,WAAW,yBAAA,CAA0B;AAAA,MACxC,GAAG,IAAA;AAAA,MACH,uBAAA,EAAyB;AAAA,KAC1B,CAAA;AAAA,EACH;AAAA,EAEA,MAAM,KACJ,QAAA,EACiB;AACjB,IAAA,OAAO,IAAA,CAAK,QAAA,CAAS,gBAAA,CAAiB,QAAQ,CAAA;AAAA,EAChD;AACF,CAAA;AAEO,SAAS,wBACd,MAAA,EACW;AACX,EAAA,OAAO,IAAI,kBAAkB,MAAM,CAAA;AACrC","file":"index.mjs","sourcesContent":["import OpenAI from \"openai\";\nimport { CharivoStateError, LLMProvider, toCharivoError } from \"@charivo/core\";\n\nexport interface OpenClawLLMConfig {\n token: string;\n baseURL?: string;\n /**\n * Target a specific agent on the gateway.\n * When omitted, the gateway's configured default agent handles the request.\n */\n agentId?: string;\n /**\n * Agent target, not a backend model name. The gateway resolves this to an\n * agent: `openclaw`, `openclaw/default`, or `openclaw/<agentId>`.\n *\n * @default \"openclaw/default\"\n */\n model?: string;\n temperature?: number;\n maxTokens?: number;\n /**\n * Pins the conversation to a server-side session on the gateway.\n *\n * The gateway keeps conversation state itself, but without an identifier it\n * opens a fresh session per request: nothing is persisted between turns and\n * each one strands a throwaway session. Sending this as the `user` field makes\n * the gateway derive a stable session key from it.\n *\n * When set, past turns are not resent — the gateway already holds them. The\n * system prompt is still sent every turn so the persona survives a dropped\n * session.\n *\n * Treat it as a conversation-scoped identifier (e.g. a UUID per conversation,\n * rotated when the user resets the chat). Rotating it is the only way to reset\n * the conversation; the gateway keeps the old transcript under the old key.\n */\n sessionKey?: string;\n dangerouslyAllowBrowser?: boolean;\n}\n\nexport class OpenClawLLMProvider implements LLMProvider {\n private openai: OpenAI;\n private model: string;\n private temperature: number;\n private maxTokens: number;\n private sessionKey?: string;\n\n constructor(config: OpenClawLLMConfig) {\n if (typeof window !== \"undefined\" && !config.dangerouslyAllowBrowser) {\n throw new CharivoStateError(\n \"OpenClaw LLM provider is for server-side use only. Set dangerouslyAllowBrowser: true for testing\",\n );\n }\n\n this.openai = new OpenAI({\n apiKey: config.token,\n baseURL: config.baseURL || \"http://127.0.0.1:18789/v1\",\n defaultHeaders: config.agentId\n ? { \"x-openclaw-agent-id\": config.agentId }\n : undefined,\n dangerouslyAllowBrowser: config.dangerouslyAllowBrowser,\n });\n\n this.model = config.model || \"openclaw/default\";\n this.temperature = config.temperature ?? 0.7;\n this.maxTokens = config.maxTokens || 1000;\n this.sessionKey = config.sessionKey;\n }\n\n async generateResponse(\n messages: Array<{ role: string; content: string }>,\n ): Promise<string> {\n try {\n const openAIMessages = this.selectMessages(messages).map((msg) => ({\n role: msg.role as \"system\" | \"user\" | \"assistant\",\n content: msg.content,\n }));\n\n const completion = await this.openai.chat.completions.create({\n model: this.model,\n messages: openAIMessages,\n temperature: this.temperature,\n max_tokens: this.maxTokens,\n ...(this.sessionKey ? { user: this.sessionKey } : {}),\n });\n\n return completion.choices[0]?.message?.content || \"\";\n } catch (error) {\n throw toCharivoError(\"provider\", error, \"OpenClaw LLM request failed\");\n }\n }\n\n /**\n * With a pinned session the gateway already holds the past turns, and resending\n * them would flatten a duplicate copy of the history on top of its own. Keep the\n * system prompts (cheap persona insurance if the session was dropped) and the\n * latest turn.\n */\n private selectMessages(\n messages: Array<{ role: string; content: string }>,\n ): Array<{ role: string; content: string }> {\n const latest = messages[messages.length - 1];\n if (!this.sessionKey || !latest) {\n return messages;\n }\n\n const systemMessages = messages.filter((msg) => msg.role === \"system\");\n return latest.role === \"system\"\n ? systemMessages\n : [...systemMessages, latest];\n }\n}\n\nexport function createOpenClawLLMProvider(\n config: OpenClawLLMConfig,\n): OpenClawLLMProvider {\n return new OpenClawLLMProvider(config);\n}\n","import type { LLMClient } from \"@charivo/core\";\nimport {\n createOpenClawLLMProvider,\n OpenClawLLMConfig,\n OpenClawLLMProvider,\n} from \"./provider\";\n\nexport {\n createOpenClawLLMProvider,\n OpenClawLLMProvider,\n type OpenClawLLMConfig,\n} from \"./provider\";\n\n// No `sessionKey` here, unlike the provider config. Pinning the gateway\n// session requires rotating it on reset, and this client is driven by\n// LLMManager, whose clearHistory()/character switch clear only local history and\n// cannot reach the client. A pinned session would survive that reset and silently\n// replay the old conversation. Server routes construct the provider themselves and\n// can rotate the key, so the option lives there instead.\nexport type OpenClawLLMClientConfig = Omit<OpenClawLLMConfig, \"sessionKey\">;\n\n/**\n * OpenClaw LLM Client - Stateless client that wraps the OpenClaw provider for direct use on the client\n *\n * For use in local development or test environments. Not recommended for production for security reasons.\n * The token is exposed to the client, so use it only in a server environment or for testing purposes.\n *\n * Stateless design: session management is handled externally, and this client only handles API calls\n */\nclass OpenClawLLMClient implements LLMClient {\n private provider: OpenClawLLMProvider;\n\n constructor(config: OpenClawLLMClientConfig) {\n // Strip sessionKey at runtime too: an untyped JS caller can bypass the\n // Omit above and pass it anyway.\n const { sessionKey: _sessionKey, ...rest } = config as OpenClawLLMConfig;\n\n // Intentional dev/test escape hatch: this direct browser client exposes\n // credentials. For production, see docs/guide/choosing-packages.md#remote.\n this.provider = createOpenClawLLMProvider({\n ...rest,\n dangerouslyAllowBrowser: true,\n });\n }\n\n async call(\n messages: Array<{ role: string; content: string }>,\n ): Promise<string> {\n return this.provider.generateResponse(messages);\n }\n}\n\nexport function createOpenClawLLMClient(\n config: OpenClawLLMClientConfig,\n): LLMClient {\n return new OpenClawLLMClient(config);\n}\n"]}
|