@a3s-lab/code 0.3.1 → 0.4.1

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.
@@ -0,0 +1,279 @@
1
+ /**
2
+ * UIMessage / ModelMessage Conversion Layer
3
+ *
4
+ * Vercel AI SDK-style message types for frontend ↔ backend conversion.
5
+ *
6
+ * - UIMessage: Frontend format with id, createdAt, parts (for rendering)
7
+ * - ModelMessage: Backend format with role, content (for LLM)
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * import { convertToModelMessages, convertToUIMessages } from '@a3s-lab/code';
12
+ *
13
+ * // Frontend → Backend (before calling generateText/streamText)
14
+ * const modelMessages = convertToModelMessages(uiMessages);
15
+ * const { text } = await generateText({ model, messages: modelMessages });
16
+ *
17
+ * // Backend → Frontend (after receiving response)
18
+ * const uiMessages = convertToUIMessages(modelMessages);
19
+ * ```
20
+ */
21
+ // ============================================================================
22
+ // Conversion: UIMessage → ModelMessage
23
+ // ============================================================================
24
+ /**
25
+ * Convert a single UIMessage to one or more ModelMessages.
26
+ *
27
+ * A single UIMessage may produce multiple ModelMessages when it contains
28
+ * tool invocations (assistant message + tool result messages).
29
+ */
30
+ function uiMessageToModelMessages(uiMessage) {
31
+ const messages = [];
32
+ if (uiMessage.role === 'system') {
33
+ messages.push({ role: 'system', content: uiMessage.content });
34
+ return messages;
35
+ }
36
+ if (uiMessage.role === 'user') {
37
+ // Extract text from parts, fall back to content
38
+ const text = uiMessage.parts
39
+ .filter((p) => p.type === 'text')
40
+ .map(p => p.text)
41
+ .join('\n') || uiMessage.content;
42
+ messages.push({ role: 'user', content: text });
43
+ return messages;
44
+ }
45
+ // Assistant message — may contain text + tool invocations
46
+ if (uiMessage.role === 'assistant') {
47
+ const textParts = uiMessage.parts.filter((p) => p.type === 'text');
48
+ const toolParts = uiMessage.parts.filter((p) => p.type === 'tool-invocation');
49
+ const text = textParts.map(p => p.text).join('\n') || uiMessage.content;
50
+ // Build tool calls from tool invocation parts
51
+ const toolCalls = toolParts
52
+ .filter(p => p.toolInvocation.state === 'call' || p.toolInvocation.state === 'result')
53
+ .map(p => ({
54
+ id: p.toolInvocation.toolCallId,
55
+ name: p.toolInvocation.toolName,
56
+ arguments: JSON.stringify(p.toolInvocation.args),
57
+ result: p.toolInvocation.state === 'result' && p.toolInvocation.result
58
+ ? {
59
+ success: true,
60
+ output: typeof p.toolInvocation.result === 'string'
61
+ ? p.toolInvocation.result
62
+ : JSON.stringify(p.toolInvocation.result),
63
+ error: '',
64
+ metadata: {},
65
+ }
66
+ : undefined,
67
+ }));
68
+ const assistantMsg = {
69
+ role: 'assistant',
70
+ content: text,
71
+ };
72
+ if (toolCalls.length > 0) {
73
+ assistantMsg.toolCalls = toolCalls;
74
+ }
75
+ messages.push(assistantMsg);
76
+ // Add tool result messages for completed invocations
77
+ for (const tp of toolParts) {
78
+ if (tp.toolInvocation.state === 'result' && tp.toolInvocation.result !== undefined) {
79
+ messages.push({
80
+ role: 'tool',
81
+ content: typeof tp.toolInvocation.result === 'string'
82
+ ? tp.toolInvocation.result
83
+ : JSON.stringify(tp.toolInvocation.result),
84
+ toolCallId: tp.toolInvocation.toolCallId,
85
+ toolName: tp.toolInvocation.toolName,
86
+ });
87
+ }
88
+ }
89
+ }
90
+ return messages;
91
+ }
92
+ /**
93
+ * Convert UIMessage[] to ModelMessage[] for use with generateText/streamText.
94
+ *
95
+ * This is the primary conversion function for frontend → backend message flow.
96
+ *
97
+ * @example
98
+ * ```typescript
99
+ * import { convertToModelMessages, generateText } from '@a3s-lab/code';
100
+ *
101
+ * // In your API route handler:
102
+ * const modelMessages = convertToModelMessages(uiMessages);
103
+ * const { text } = await generateText({
104
+ * model: openai('gpt-4o'),
105
+ * messages: modelMessages,
106
+ * });
107
+ * ```
108
+ */
109
+ export function convertToModelMessages(uiMessages) {
110
+ return uiMessages.flatMap(uiMessageToModelMessages);
111
+ }
112
+ // ============================================================================
113
+ // Conversion: ModelMessage → UIMessage
114
+ // ============================================================================
115
+ let _idCounter = 0;
116
+ function generateId() {
117
+ return `msg-${Date.now()}-${++_idCounter}`;
118
+ }
119
+ /**
120
+ * Convert ModelMessage[] to UIMessage[] for frontend rendering.
121
+ *
122
+ * Groups assistant messages with their tool results into single UIMessages
123
+ * with structured parts.
124
+ *
125
+ * @example
126
+ * ```typescript
127
+ * import { convertToUIMessages } from '@a3s-lab/code';
128
+ *
129
+ * const uiMessages = convertToUIMessages(response.messages);
130
+ * // Render uiMessages in your chat UI
131
+ * ```
132
+ */
133
+ export function convertToUIMessages(modelMessages) {
134
+ const uiMessages = [];
135
+ let i = 0;
136
+ while (i < modelMessages.length) {
137
+ const msg = modelMessages[i];
138
+ if (msg.role === 'system') {
139
+ uiMessages.push({
140
+ id: generateId(),
141
+ role: 'system',
142
+ content: msg.content,
143
+ parts: [{ type: 'text', text: msg.content }],
144
+ createdAt: new Date(),
145
+ });
146
+ i++;
147
+ continue;
148
+ }
149
+ if (msg.role === 'user') {
150
+ uiMessages.push({
151
+ id: generateId(),
152
+ role: 'user',
153
+ content: msg.content,
154
+ parts: [{ type: 'text', text: msg.content }],
155
+ createdAt: new Date(),
156
+ });
157
+ i++;
158
+ continue;
159
+ }
160
+ if (msg.role === 'assistant') {
161
+ const parts = [];
162
+ // Add text part
163
+ if (msg.content) {
164
+ parts.push({ type: 'text', text: msg.content });
165
+ }
166
+ // Add tool invocation parts
167
+ if (msg.toolCalls) {
168
+ for (const tc of msg.toolCalls) {
169
+ const args = tc.arguments ? JSON.parse(tc.arguments) : {};
170
+ // Look ahead for matching tool result
171
+ let result = undefined;
172
+ let state = 'call';
173
+ for (let j = i + 1; j < modelMessages.length; j++) {
174
+ const next = modelMessages[j];
175
+ if (next.role === 'tool' && next.toolCallId === tc.id) {
176
+ try {
177
+ result = JSON.parse(next.content);
178
+ }
179
+ catch {
180
+ result = next.content;
181
+ }
182
+ state = 'result';
183
+ break;
184
+ }
185
+ if (next.role !== 'tool')
186
+ break;
187
+ }
188
+ parts.push({
189
+ type: 'tool-invocation',
190
+ toolInvocation: {
191
+ toolCallId: tc.id,
192
+ toolName: tc.name,
193
+ args,
194
+ state,
195
+ result,
196
+ },
197
+ });
198
+ }
199
+ }
200
+ uiMessages.push({
201
+ id: generateId(),
202
+ role: 'assistant',
203
+ content: msg.content,
204
+ parts,
205
+ createdAt: new Date(),
206
+ });
207
+ i++;
208
+ // Skip consumed tool result messages
209
+ while (i < modelMessages.length && modelMessages[i].role === 'tool') {
210
+ i++;
211
+ }
212
+ continue;
213
+ }
214
+ // Standalone tool message (shouldn't happen normally, but handle gracefully)
215
+ if (msg.role === 'tool') {
216
+ i++;
217
+ continue;
218
+ }
219
+ i++;
220
+ }
221
+ return uiMessages;
222
+ }
223
+ // ============================================================================
224
+ // Conversion: A3S Message ↔ ModelMessage
225
+ // ============================================================================
226
+ /**
227
+ * Convert A3S Message to ModelMessage.
228
+ */
229
+ export function a3sMessageToModel(msg) {
230
+ switch (msg.role) {
231
+ case 'system':
232
+ return { role: 'system', content: msg.content };
233
+ case 'user':
234
+ return { role: 'user', content: msg.content };
235
+ case 'assistant':
236
+ return { role: 'assistant', content: msg.content };
237
+ case 'tool':
238
+ return { role: 'tool', content: msg.content };
239
+ default:
240
+ return { role: 'user', content: msg.content };
241
+ }
242
+ }
243
+ /**
244
+ * Convert ModelMessage to A3S Message.
245
+ */
246
+ export function modelMessageToA3s(msg) {
247
+ return {
248
+ role: msg.role,
249
+ content: msg.content,
250
+ };
251
+ }
252
+ /**
253
+ * Convert A3S Message[] to ModelMessage[].
254
+ */
255
+ export function a3sMessagesToModel(messages) {
256
+ return messages.map(a3sMessageToModel);
257
+ }
258
+ /**
259
+ * Convert ModelMessage[] to A3S Message[].
260
+ */
261
+ export function modelMessagesToA3s(messages) {
262
+ return messages.map(modelMessageToA3s);
263
+ }
264
+ // ============================================================================
265
+ // Conversion: A3S Message ↔ UIMessage (shorthand)
266
+ // ============================================================================
267
+ /**
268
+ * Convert A3S Message[] to UIMessage[] (shorthand for a3sMessagesToModel + convertToUIMessages).
269
+ */
270
+ export function a3sMessagesToUI(messages) {
271
+ return convertToUIMessages(a3sMessagesToModel(messages));
272
+ }
273
+ /**
274
+ * Convert UIMessage[] to A3S Message[] (shorthand for convertToModelMessages + modelMessagesToA3s).
275
+ */
276
+ export function uiMessagesToA3s(uiMessages) {
277
+ return modelMessagesToA3s(convertToModelMessages(uiMessages));
278
+ }
279
+ //# sourceMappingURL=message.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"message.js","sourceRoot":"","sources":["../ts/message.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AA6GH,+EAA+E;AAC/E,uCAAuC;AACvC,+EAA+E;AAE/E;;;;;GAKG;AACH,SAAS,wBAAwB,CAAC,SAAoB;IACpD,MAAM,QAAQ,GAAmB,EAAE,CAAC;IAEpC,IAAI,SAAS,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;QAChC,QAAQ,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,SAAS,CAAC,OAAO,EAAE,CAAC,CAAC;QAC9D,OAAO,QAAQ,CAAC;IAClB,CAAC;IAED,IAAI,SAAS,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;QAC9B,gDAAgD;QAChD,MAAM,IAAI,GAAG,SAAS,CAAC,KAAK;aACzB,MAAM,CAAC,CAAC,CAAC,EAA0B,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,MAAM,CAAC;aACxD,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;aAChB,IAAI,CAAC,IAAI,CAAC,IAAI,SAAS,CAAC,OAAO,CAAC;QAEnC,QAAQ,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC;QAC/C,OAAO,QAAQ,CAAC;IAClB,CAAC;IAED,0DAA0D;IAC1D,IAAI,SAAS,CAAC,IAA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,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,iBAAiB,CAAC,GAAiB;IACjD,OAAO;QACL,IAAI,EAAE,GAAG,CAAC,IAAI;QACd,OAAO,EAAE,GAAG,CAAC,OAAO;KACrB,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAAC,QAAmB;IACpD,OAAO,QAAQ,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;AACzC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAAC,QAAwB;IACzD,OAAO,QAAQ,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;AACzC,CAAC;AAED,+EAA+E;AAC/E,kDAAkD;AAClD,+EAA+E;AAE/E;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,QAAmB;IACjD,OAAO,mBAAmB,CAAC,kBAAkB,CAAC,QAAQ,CAAC,CAAC,CAAC;AAC3D,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,UAAuB;IACrD,OAAO,kBAAkB,CAAC,sBAAsB,CAAC,UAAU,CAAC,CAAC,CAAC;AAChE,CAAC"}
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Provider Factory
3
+ *
4
+ * Vercel AI SDK-style provider abstraction for A3S Code.
5
+ *
6
+ * @example
7
+ * ```typescript
8
+ * import { createProvider } from '@a3s-lab/code';
9
+ *
10
+ * const openai = createProvider({ name: 'openai', apiKey: 'sk-xxx' });
11
+ * const kimi = createProvider({ name: 'kimi', apiKey: 'sk-xxx', baseUrl: 'http://xxx/v1' });
12
+ *
13
+ * // Use as model selector
14
+ * const model = openai('gpt-4o');
15
+ * const model2 = kimi('k2.5');
16
+ * ```
17
+ */
18
+ import type { LLMConfig } from './client.js';
19
+ /**
20
+ * Provider configuration
21
+ */
22
+ export interface ProviderOptions {
23
+ /** Provider name (e.g., 'openai', 'anthropic', 'kimi') */
24
+ name: string;
25
+ /** API key */
26
+ apiKey: string;
27
+ /** Base URL override */
28
+ baseUrl?: string;
29
+ }
30
+ /**
31
+ * Model reference — a resolved provider + model pair
32
+ */
33
+ export interface ModelRef {
34
+ provider: string;
35
+ model: string;
36
+ apiKey: string;
37
+ baseUrl?: string;
38
+ }
39
+ /**
40
+ * Model selector function returned by createProvider()
41
+ */
42
+ export type ModelSelector = (modelId: string) => ModelRef;
43
+ /**
44
+ * Create a provider factory that returns model references.
45
+ *
46
+ * @example
47
+ * ```typescript
48
+ * const openai = createProvider({ name: 'openai', apiKey: 'sk-xxx' });
49
+ * const model = openai('gpt-4o');
50
+ * ```
51
+ */
52
+ export declare function createProvider(options: ProviderOptions): ModelSelector;
53
+ /**
54
+ * Shorthand: create a provider and select a model in one call.
55
+ *
56
+ * @example
57
+ * ```typescript
58
+ * const model = model({ provider: 'openai', model: 'gpt-4o', apiKey: 'sk-xxx' });
59
+ * ```
60
+ */
61
+ export declare function model(config: LLMConfig): ModelRef;
62
+ /** Convert ModelRef to LLMConfig for the underlying client */
63
+ export declare function modelRefToLLMConfig(ref: ModelRef): LLMConfig;
64
+ //# sourceMappingURL=provider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../ts/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAE7C;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,0DAA0D;IAC1D,IAAI,EAAE,MAAM,CAAC;IACb,cAAc;IACd,MAAM,EAAE,MAAM,CAAC;IACf,wBAAwB;IACxB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,QAAQ;IACvB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG,CAAC,OAAO,EAAE,MAAM,KAAK,QAAQ,CAAC;AAE1D;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,eAAe,GAAG,aAAa,CAOtE;AAED;;;;;;;GAOG;AACH,wBAAgB,KAAK,CAAC,MAAM,EAAE,SAAS,GAAG,QAAQ,CAOjD;AAED,8DAA8D;AAC9D,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,QAAQ,GAAG,SAAS,CAO5D"}
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Provider Factory
3
+ *
4
+ * Vercel AI SDK-style provider abstraction for A3S Code.
5
+ *
6
+ * @example
7
+ * ```typescript
8
+ * import { createProvider } from '@a3s-lab/code';
9
+ *
10
+ * const openai = createProvider({ name: 'openai', apiKey: 'sk-xxx' });
11
+ * const kimi = createProvider({ name: 'kimi', apiKey: 'sk-xxx', baseUrl: 'http://xxx/v1' });
12
+ *
13
+ * // Use as model selector
14
+ * const model = openai('gpt-4o');
15
+ * const model2 = kimi('k2.5');
16
+ * ```
17
+ */
18
+ /**
19
+ * Create a provider factory that returns model references.
20
+ *
21
+ * @example
22
+ * ```typescript
23
+ * const openai = createProvider({ name: 'openai', apiKey: 'sk-xxx' });
24
+ * const model = openai('gpt-4o');
25
+ * ```
26
+ */
27
+ export function createProvider(options) {
28
+ return (modelId) => ({
29
+ provider: options.name,
30
+ model: modelId,
31
+ apiKey: options.apiKey,
32
+ baseUrl: options.baseUrl,
33
+ });
34
+ }
35
+ /**
36
+ * Shorthand: create a provider and select a model in one call.
37
+ *
38
+ * @example
39
+ * ```typescript
40
+ * const model = model({ provider: 'openai', model: 'gpt-4o', apiKey: 'sk-xxx' });
41
+ * ```
42
+ */
43
+ export function model(config) {
44
+ return {
45
+ provider: config.provider,
46
+ model: config.model,
47
+ apiKey: config.apiKey || '',
48
+ baseUrl: config.baseUrl,
49
+ };
50
+ }
51
+ /** Convert ModelRef to LLMConfig for the underlying client */
52
+ export function modelRefToLLMConfig(ref) {
53
+ return {
54
+ provider: ref.provider,
55
+ model: ref.model,
56
+ apiKey: ref.apiKey,
57
+ baseUrl: ref.baseUrl,
58
+ };
59
+ }
60
+ //# sourceMappingURL=provider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.js","sourceRoot":"","sources":["../ts/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AA+BH;;;;;;;;GAQG;AACH,MAAM,UAAU,cAAc,CAAC,OAAwB;IACrD,OAAO,CAAC,OAAe,EAAY,EAAE,CAAC,CAAC;QACrC,QAAQ,EAAE,OAAO,CAAC,IAAI;QACtB,KAAK,EAAE,OAAO;QACd,MAAM,EAAE,OAAO,CAAC,MAAM;QACtB,OAAO,EAAE,OAAO,CAAC,OAAO;KACzB,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,KAAK,CAAC,MAAiB;IACrC,OAAO;QACL,QAAQ,EAAE,MAAM,CAAC,QAAQ;QACzB,KAAK,EAAE,MAAM,CAAC,KAAK;QACnB,MAAM,EAAE,MAAM,CAAC,MAAM,IAAI,EAAE;QAC3B,OAAO,EAAE,MAAM,CAAC,OAAO;KACxB,CAAC;AACJ,CAAC;AAED,8DAA8D;AAC9D,MAAM,UAAU,mBAAmB,CAAC,GAAa;IAC/C,OAAO;QACL,QAAQ,EAAE,GAAG,CAAC,QAAQ;QACtB,KAAK,EAAE,GAAG,CAAC,KAAK;QAChB,MAAM,EAAE,GAAG,CAAC,MAAM;QAClB,OAAO,EAAE,GAAG,CAAC,OAAO;KACrB,CAAC;AACJ,CAAC"}