@herouucn/opencode-commandcode 0.1.0 → 0.1.2

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/src/convert.ts CHANGED
@@ -1,242 +1,242 @@
1
- import type { LanguageModelV3CallOptions } from "@ai-sdk/provider";
2
- import type {
3
- LanguageModelV3FunctionTool,
4
- LanguageModelV3Message,
5
- LanguageModelV3TextPart,
6
- LanguageModelV3ReasoningPart,
7
- LanguageModelV3ToolCallPart,
8
- LanguageModelV3ToolResultPart,
9
- LanguageModelV3ToolResultOutput,
10
- } from "@ai-sdk/provider";
11
-
12
- type CCMessage =
13
- | { role: "user"; content: string | unknown[] }
14
- | { role: "assistant"; content: CCAssistantContent[] }
15
- | { role: "tool"; content: CCToolResultContent[] };
16
-
17
- type CCAssistantContent =
18
- | { type: "text"; text: string }
19
- | { type: "reasoning"; text: string }
20
- | { type: "tool-call"; toolCallId: string; toolName: string; input: unknown };
21
-
22
- type CCToolResultContent = {
23
- type: "tool-result";
24
- toolCallId: string;
25
- toolName: string;
26
- output: { type: "text"; value: string } | { type: "error-text"; value: string };
27
- };
28
-
29
- type CCTool = {
30
- type: "function";
31
- name: string;
32
- description?: string;
33
- input_schema: unknown;
34
- };
35
-
36
- interface CCRequestEnvelope {
37
- config: {
38
- workingDir: string;
39
- date: string;
40
- environment: string;
41
- structure: unknown[];
42
- isGitRepo: boolean;
43
- currentBranch: string;
44
- mainBranch: string;
45
- gitStatus: string;
46
- recentCommits: unknown[];
47
- };
48
- memory: string;
49
- taste: string;
50
- skills: null;
51
- permissionMode: string;
52
- params: {
53
- model: string;
54
- messages: CCMessage[];
55
- tools: CCTool[];
56
- system: string;
57
- max_tokens: number;
58
- stream: true;
59
- temperature?: number;
60
- top_p?: number;
61
- top_k?: number;
62
- };
63
- }
64
-
65
- function hasType(p: unknown, type: string): boolean {
66
- return typeof p === "object" && p !== null && (p as { type?: string }).type === type;
67
- }
68
-
69
- function isTextPart(p: unknown): p is LanguageModelV3TextPart {
70
- return hasType(p, "text");
71
- }
72
-
73
- function isReasoningPart(p: unknown): p is LanguageModelV3ReasoningPart {
74
- return hasType(p, "reasoning");
75
- }
76
-
77
- function isToolCallPart(p: unknown): p is LanguageModelV3ToolCallPart {
78
- return hasType(p, "tool-call");
79
- }
80
-
81
- function isToolResultPart(p: unknown): p is LanguageModelV3ToolResultPart {
82
- return hasType(p, "tool-result");
83
- }
84
-
85
- function extractText(content: unknown): string {
86
- if (typeof content === "string") return content;
87
- if (Array.isArray(content)) {
88
- const textParts = content.filter(isTextPart) as LanguageModelV3TextPart[];
89
- const nonTextParts = content.filter((p) => !isTextPart(p));
90
- if (nonTextParts.length > 0 && textParts.length === 0) {
91
- console.warn(
92
- `Command Code provider: dropped ${nonTextParts.length} non-text part(s) in user message`,
93
- );
94
- }
95
- return textParts.map((p) => p.text).join("\n");
96
- }
97
- return "";
98
- }
99
-
100
- function convertToolResultOutput(
101
- output: LanguageModelV3ToolResultOutput,
102
- ): CCToolResultContent["output"] {
103
- switch (output.type) {
104
- case "text":
105
- return { type: "text", value: output.value };
106
- case "error-text":
107
- return { type: "error-text", value: output.value };
108
- case "json":
109
- return { type: "text", value: JSON.stringify(output.value) };
110
- case "execution-denied":
111
- return { type: "error-text", value: output.reason ?? "Execution denied" };
112
- case "error-json":
113
- return { type: "error-text", value: JSON.stringify(output.value) };
114
- case "content":
115
- return {
116
- type: "text",
117
- value: output.value
118
- .map((v: Record<string, unknown>) => ("text" in v ? v.text : JSON.stringify(v)))
119
- .join("\n"),
120
- };
121
- default:
122
- return { type: "text", value: JSON.stringify(output) };
123
- }
124
- }
125
-
126
- function convertMessage(msg: LanguageModelV3Message): CCMessage | null {
127
- switch (msg.role) {
128
- case "user": {
129
- const text = extractText(msg.content);
130
- return { role: "user", content: text };
131
- }
132
- case "assistant": {
133
- const parts: CCAssistantContent[] = [];
134
- for (const part of msg.content) {
135
- if (isTextPart(part)) {
136
- parts.push({ type: "text", text: part.text });
137
- } else if (isReasoningPart(part)) {
138
- parts.push({ type: "reasoning", text: part.text });
139
- } else if (isToolCallPart(part)) {
140
- parts.push({
141
- type: "tool-call",
142
- toolCallId: part.toolCallId,
143
- toolName: part.toolName,
144
- input: part.input,
145
- });
146
- }
147
- }
148
- return { role: "assistant", content: parts };
149
- }
150
- case "tool": {
151
- const parts: CCToolResultContent[] = [];
152
- for (const part of msg.content) {
153
- if (isToolResultPart(part)) {
154
- parts.push({
155
- type: "tool-result",
156
- toolCallId: part.toolCallId,
157
- toolName: part.toolName,
158
- output: convertToolResultOutput(part.output),
159
- });
160
- }
161
- }
162
- return { role: "tool", content: parts };
163
- }
164
- default:
165
- return null;
166
- }
167
- }
168
-
169
- function convertTools(
170
- tools:
171
- | Array<
172
- | LanguageModelV3FunctionTool
173
- | {
174
- type: "provider";
175
- id: `${string}.${string}`;
176
- name: string;
177
- args: Record<string, unknown>;
178
- }
179
- >
180
- | undefined,
181
- ): CCTool[] {
182
- if (!tools) return [];
183
- return tools
184
- .filter((t): t is LanguageModelV3FunctionTool => t.type === "function")
185
- .map((t) => ({
186
- type: "function" as const,
187
- name: t.name,
188
- description: t.description,
189
- input_schema: t.inputSchema,
190
- }));
191
- }
192
-
193
- export function buildRequest(
194
- modelId: string,
195
- options: LanguageModelV3CallOptions,
196
- ): CCRequestEnvelope {
197
- let systemPrompt = "";
198
- const messages: CCMessage[] = [];
199
-
200
- for (const msg of options.prompt) {
201
- if (msg.role === "system") {
202
- systemPrompt += (systemPrompt ? "\n\n" : "") + msg.content;
203
- continue;
204
- }
205
- const converted = convertMessage(msg);
206
- if (converted) messages.push(converted);
207
- }
208
-
209
- const params: CCRequestEnvelope["params"] = {
210
- model: modelId,
211
- messages,
212
- tools: convertTools(options.tools),
213
- system: systemPrompt,
214
- max_tokens: options.maxOutputTokens ?? 16384,
215
- stream: true,
216
- };
217
-
218
- if (options.temperature !== undefined) params.temperature = options.temperature;
219
- if (options.topP !== undefined) params.top_p = options.topP;
220
- if (options.topK !== undefined) params.top_k = options.topK;
221
-
222
- return {
223
- config: {
224
- workingDir: process.cwd() ?? "/",
225
- date: new Date().toISOString().split("T")[0] ?? "",
226
- environment: `${process.platform}-${process.arch}`,
227
- // Stub: opencode does not expose project structure context
228
- structure: [],
229
- isGitRepo: false,
230
- currentBranch: "",
231
- mainBranch: "",
232
- gitStatus: "",
233
- recentCommits: [],
234
- },
235
- memory: "",
236
- // Stub: taste/memory/permissionMode are Command Code CLI features not exposed via provider API
237
- taste: "",
238
- skills: null,
239
- permissionMode: "standard",
240
- params,
241
- };
242
- }
1
+ import type { LanguageModelV3CallOptions } from "@ai-sdk/provider";
2
+ import type {
3
+ LanguageModelV3FunctionTool,
4
+ LanguageModelV3Message,
5
+ LanguageModelV3TextPart,
6
+ LanguageModelV3ReasoningPart,
7
+ LanguageModelV3ToolCallPart,
8
+ LanguageModelV3ToolResultPart,
9
+ LanguageModelV3ToolResultOutput,
10
+ } from "@ai-sdk/provider";
11
+
12
+ type CCMessage =
13
+ | { role: "user"; content: string | unknown[] }
14
+ | { role: "assistant"; content: CCAssistantContent[] }
15
+ | { role: "tool"; content: CCToolResultContent[] };
16
+
17
+ type CCAssistantContent =
18
+ | { type: "text"; text: string }
19
+ | { type: "reasoning"; text: string }
20
+ | { type: "tool-call"; toolCallId: string; toolName: string; input: unknown };
21
+
22
+ type CCToolResultContent = {
23
+ type: "tool-result";
24
+ toolCallId: string;
25
+ toolName: string;
26
+ output: { type: "text"; value: string } | { type: "error-text"; value: string };
27
+ };
28
+
29
+ type CCTool = {
30
+ type: "function";
31
+ name: string;
32
+ description?: string;
33
+ input_schema: unknown;
34
+ };
35
+
36
+ interface CCRequestEnvelope {
37
+ config: {
38
+ workingDir: string;
39
+ date: string;
40
+ environment: string;
41
+ structure: unknown[];
42
+ isGitRepo: boolean;
43
+ currentBranch: string;
44
+ mainBranch: string;
45
+ gitStatus: string;
46
+ recentCommits: unknown[];
47
+ };
48
+ memory: string;
49
+ taste: string;
50
+ skills: null;
51
+ permissionMode: string;
52
+ params: {
53
+ model: string;
54
+ messages: CCMessage[];
55
+ tools: CCTool[];
56
+ system: string;
57
+ max_tokens: number;
58
+ stream: true;
59
+ temperature?: number;
60
+ top_p?: number;
61
+ top_k?: number;
62
+ };
63
+ }
64
+
65
+ function hasType(p: unknown, type: string): boolean {
66
+ return typeof p === "object" && p !== null && (p as { type?: string }).type === type;
67
+ }
68
+
69
+ function isTextPart(p: unknown): p is LanguageModelV3TextPart {
70
+ return hasType(p, "text");
71
+ }
72
+
73
+ function isReasoningPart(p: unknown): p is LanguageModelV3ReasoningPart {
74
+ return hasType(p, "reasoning");
75
+ }
76
+
77
+ function isToolCallPart(p: unknown): p is LanguageModelV3ToolCallPart {
78
+ return hasType(p, "tool-call");
79
+ }
80
+
81
+ function isToolResultPart(p: unknown): p is LanguageModelV3ToolResultPart {
82
+ return hasType(p, "tool-result");
83
+ }
84
+
85
+ function extractText(content: unknown): string {
86
+ if (typeof content === "string") return content;
87
+ if (Array.isArray(content)) {
88
+ const textParts = content.filter(isTextPart) as LanguageModelV3TextPart[];
89
+ const nonTextParts = content.filter((p) => !isTextPart(p));
90
+ if (nonTextParts.length > 0 && textParts.length === 0) {
91
+ console.warn(
92
+ `Command Code provider: dropped ${nonTextParts.length} non-text part(s) in user message`,
93
+ );
94
+ }
95
+ return textParts.map((p) => p.text).join("\n");
96
+ }
97
+ return "";
98
+ }
99
+
100
+ function convertToolResultOutput(
101
+ output: LanguageModelV3ToolResultOutput,
102
+ ): CCToolResultContent["output"] {
103
+ switch (output.type) {
104
+ case "text":
105
+ return { type: "text", value: output.value };
106
+ case "error-text":
107
+ return { type: "error-text", value: output.value };
108
+ case "json":
109
+ return { type: "text", value: JSON.stringify(output.value) };
110
+ case "execution-denied":
111
+ return { type: "error-text", value: output.reason ?? "Execution denied" };
112
+ case "error-json":
113
+ return { type: "error-text", value: JSON.stringify(output.value) };
114
+ case "content":
115
+ return {
116
+ type: "text",
117
+ value: output.value
118
+ .map((v: Record<string, unknown>) => ("text" in v ? v.text : JSON.stringify(v)))
119
+ .join("\n"),
120
+ };
121
+ default:
122
+ return { type: "text", value: JSON.stringify(output) };
123
+ }
124
+ }
125
+
126
+ function convertMessage(msg: LanguageModelV3Message): CCMessage | null {
127
+ switch (msg.role) {
128
+ case "user": {
129
+ const text = extractText(msg.content);
130
+ return { role: "user", content: text };
131
+ }
132
+ case "assistant": {
133
+ const parts: CCAssistantContent[] = [];
134
+ for (const part of msg.content) {
135
+ if (isTextPart(part)) {
136
+ parts.push({ type: "text", text: part.text });
137
+ } else if (isReasoningPart(part)) {
138
+ parts.push({ type: "reasoning", text: part.text });
139
+ } else if (isToolCallPart(part)) {
140
+ parts.push({
141
+ type: "tool-call",
142
+ toolCallId: part.toolCallId,
143
+ toolName: part.toolName,
144
+ input: part.input,
145
+ });
146
+ }
147
+ }
148
+ return { role: "assistant", content: parts };
149
+ }
150
+ case "tool": {
151
+ const parts: CCToolResultContent[] = [];
152
+ for (const part of msg.content) {
153
+ if (isToolResultPart(part)) {
154
+ parts.push({
155
+ type: "tool-result",
156
+ toolCallId: part.toolCallId,
157
+ toolName: part.toolName,
158
+ output: convertToolResultOutput(part.output),
159
+ });
160
+ }
161
+ }
162
+ return { role: "tool", content: parts };
163
+ }
164
+ default:
165
+ return null;
166
+ }
167
+ }
168
+
169
+ function convertTools(
170
+ tools:
171
+ | Array<
172
+ | LanguageModelV3FunctionTool
173
+ | {
174
+ type: "provider";
175
+ id: `${string}.${string}`;
176
+ name: string;
177
+ args: Record<string, unknown>;
178
+ }
179
+ >
180
+ | undefined,
181
+ ): CCTool[] {
182
+ if (!tools) return [];
183
+ return tools
184
+ .filter((t): t is LanguageModelV3FunctionTool => t.type === "function")
185
+ .map((t) => ({
186
+ type: "function" as const,
187
+ name: t.name,
188
+ description: t.description,
189
+ input_schema: t.inputSchema,
190
+ }));
191
+ }
192
+
193
+ export function buildRequest(
194
+ modelId: string,
195
+ options: LanguageModelV3CallOptions,
196
+ ): CCRequestEnvelope {
197
+ let systemPrompt = "";
198
+ const messages: CCMessage[] = [];
199
+
200
+ for (const msg of options.prompt) {
201
+ if (msg.role === "system") {
202
+ systemPrompt += (systemPrompt ? "\n\n" : "") + msg.content;
203
+ continue;
204
+ }
205
+ const converted = convertMessage(msg);
206
+ if (converted) messages.push(converted);
207
+ }
208
+
209
+ const params: CCRequestEnvelope["params"] = {
210
+ model: modelId,
211
+ messages,
212
+ tools: convertTools(options.tools),
213
+ system: systemPrompt,
214
+ max_tokens: options.maxOutputTokens ?? 16384,
215
+ stream: true,
216
+ };
217
+
218
+ if (options.temperature !== undefined) params.temperature = options.temperature;
219
+ if (options.topP !== undefined) params.top_p = options.topP;
220
+ if (options.topK !== undefined) params.top_k = options.topK;
221
+
222
+ return {
223
+ config: {
224
+ workingDir: process.cwd() ?? "/",
225
+ date: new Date().toISOString().split("T")[0] ?? "",
226
+ environment: `${process.platform}-${process.arch}`,
227
+ // Stub: opencode does not expose project structure context
228
+ structure: [],
229
+ isGitRepo: false,
230
+ currentBranch: "",
231
+ mainBranch: "",
232
+ gitStatus: "",
233
+ recentCommits: [],
234
+ },
235
+ memory: "",
236
+ // Stub: taste/memory/permissionMode are Command Code CLI features not exposed via provider API
237
+ taste: "",
238
+ skills: null,
239
+ permissionMode: "standard",
240
+ params,
241
+ };
242
+ }