Skip to content

Commit 73fa4c8

Browse files
committed
feat: init llm provider
1 parent fe26f12 commit 73fa4c8

17 files changed

+1151
-4
lines changed

packages/agent-infra/agent/core/examples/mcp-agent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import { MCPAgent } from '../src';
66
import { getModel } from './model';
77

8-
const model = getModel('gpt-4o-2024-11-20');
8+
const model = getModel('qwen3:1.7b');
99

1010
async function main() {
1111
const agent = new MCPAgent({

packages/agent-infra/agent/core/examples/model.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export type ModelName =
1414
| 'gpt-4o-2024-11-20'
1515
| 'aws_sdk_claude37_sonnet'
1616
| 'qwen2.5-coder-3b-instruct'
17-
| 'qwen2.5-7b-instruct-1m';
17+
| 'qwen2.5-7b-instruct-1m'
18+
| 'qwen3:1.7b';
1819

1920
export function getModel(name: ModelName) {
2021
if (
@@ -29,6 +30,15 @@ export function getModel(name: ModelName) {
2930
return new Model(openai, name);
3031
}
3132

33+
if (name === 'qwen3:1.7b') {
34+
const apiKey = 'openai';
35+
const openai = new OpenAI({
36+
baseURL: 'http://127.0.0.1:11434/v1',
37+
apiKey,
38+
});
39+
return new Model(openai, name);
40+
}
41+
3242
if (name === 'gpt-4o-2024-11-20') {
3343
const apiKey = 'openai';
3444
console.log(
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Code Review Report for PR #534
2+
3+
## Summary of Changes
4+
This pull request introduces significant improvements to error handling and formatting in the UI-TARS desktop application. The changes focus on:
5+
6+
1. Enhanced error classification and centralization.
7+
2. Refactored error handling logic in the `GUIAgent` class.
8+
3. Improved error display in the `ErrorMessage` component.
9+
4. Addition of new error status enums and the `GUIAgentError` class.
10+
11+
---
12+
13+
## Potential Issues and Bugs
14+
1. **Error Propagation in `guiAgentErrorParser`:**
15+
- The `guiAgentErrorParser` method attempts to classify and format errors but does not account for all potential error cases, such as unexpected types.
16+
- **Example:**
17+
```typescript
18+
+ if (!parseError && type === ErrorStatusEnum.ENVIRONMENT_ERROR) {
19+
+ parseError = new GUIAgentError(
20+
+ ErrorStatusEnum.ENVIRONMENT_ERROR,
21+
+ 'The environment error occurred when parsing the action',
22+
+ );
23+
+ }
24+
```
25+
Recommendation: Ensure all possible error types are handled and provide a fallback for unclassified errors.
26+
27+
2. **JSON Parsing in `ErrorMessage` Component:**
28+
- The `ErrorMessage` component attempts to parse a JSON string without validating its structure or handling syntax errors gracefully.
29+
- **Example:**
30+
```typescript
31+
+ const parsed = JSON.parse(text);
32+
+ if (parsed && typeof parsed === 'object' && 'status' in parsed) {
33+
+ parsedError = parsed as GUIAgentError;
34+
+ }
35+
```
36+
Recommendation: Wrap `JSON.parse` in a try-catch block and validate the parsed object more robustly.
37+
38+
3. **Testing Coverage:**
39+
- Code coverage reports indicate that new changes have reduced overall test coverage, particularly in `GUIAgent.ts` and `agent.ts`.
40+
**Codecov Report:**
41+
- `GUIAgent.ts`: 12.50% coverage with 33 missing lines.
42+
- `agent.ts`: 0% coverage with 10 missing lines.
43+
44+
---
45+
46+
## Code Quality Considerations
47+
1. **Error Class Design:**
48+
- Transition to the `GUIAgentError` class is a positive improvement, but consider making the `status` property non-optional to enforce stricter type safety.
49+
50+
2. **Consistency in Logging:**
51+
- Logging statements have been updated for better readability (e.g., `[GUIAgent]` prefixes). Ensure all logs adhere to this convention.
52+
- **Example:**
53+
```diff
54+
- logger.error('[runAgent error]', settings, error);
55+
+ logger.error('[onGUIAgentError]', settings, error);
56+
```
57+
58+
3. **Redundant Comments:**
59+
- Several comments mirror the code logic without adding value. For example:
60+
```typescript
61+
+ // Avoid guiAgentErrorParser itself in stack trace
62+
+ Error.captureStackTrace(parseError, this.guiAgentErrorParser);
63+
```
64+
Recommendation: Remove such comments to improve readability.
65+
66+
---
67+
68+
## Suggested Improvements
69+
1. **Enhance Validation:**
70+
- In `ErrorMessage`, validate the JSON structure more rigorously before casting it to `GUIAgentError`.
71+
72+
2. **Catch-All Error Handling:**
73+
- Add a generic catch-all clause in `guiAgentErrorParser` to ensure no errors slip through unhandled.
74+
75+
3. **Improve Test Coverage:**
76+
- Write unit tests to cover new logic in `GUIAgent.ts` and `ErrorMessage`. Focus on edge cases like malformed inputs and unhandled error types.
77+
78+
4. **Refactor Enums:**
79+
- The `ErrorStatusEnum` has been expanded. Consider grouping related enums into nested structures for better organization.
80+
81+
---
82+
83+
## Overall Assessment
84+
This pull request significantly enhances the error handling and debugging capabilities of the application. However, some areas require further attention, particularly robustness, test coverage, and code readability.
85+
86+
### Recommended Actions
87+
- Address the identified potential issues.
88+
- Write additional tests to improve code coverage.
89+
- Refactor code to simplify and standardize error handling further.
90+
91+
Once these changes are implemented, the PR will be ready for merging.

packages/agent-infra/agent/core/examples/weather.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
import { z } from 'zod';
1414
import { getModel } from './model';
1515

16-
const model = getModel('aws_sdk_claude37_sonnet');
16+
const model = getModel('qwen3:1.7b');
1717
// Create tool call provider explicitly
1818
const toolCallProvider = new OpenAIToolCallProvider();
1919

@@ -56,7 +56,7 @@ const agent = new Agent({
5656
2. "finish_reason" should always be "tool_calls"
5757
`,
5858
toolCallProvider, // Pass the provider explicitly
59-
maxIterations: 3,
59+
maxIterations: 10,
6060
});
6161

6262
async function main() {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# @agent-infra/llm
2+
3+
A TypeScript SDK to call multiple LLM Prividers in OpenAI format.
4+
5+
## Installation
6+
7+
```bash
8+
pnpm install @agent-infra/llm
9+
```
10+
11+
## Motivation
12+
13+
14+
15+
16+
## Quick Start
17+
18+
### Create a OpenAI provider
19+
20+
```ts
21+
import { LLMClient } from '@agent-infra/llm';
22+
23+
const client = new LLMClient('openai', {
24+
apiKey: '',
25+
baseUrl: '',
26+
});
27+
28+
const result = await client.chat.completions.create({
29+
messages: [{ role: 'user', content: 'Say this is a test' }],
30+
model: 'gpt-4o',
31+
});
32+
```
33+
34+
## Features
35+
36+
### Create different providers
37+
38+
```ts
39+
import { LLMClient } from '@agent-infra/llm';
40+
41+
const client = new LLMClient('openrouter', {
42+
apiKey: '',
43+
baseUrl: '',
44+
});
45+
46+
const result = await client.chat.completions.create({
47+
messages: [{ role: 'user', content: 'Say this is a test' }],
48+
model: 'openai/gpt-4o',
49+
});
50+
```
51+
52+
## Create a Anthropic provider
53+
54+
```ts
55+
import { LLMClient } from '@agent-infra/llm';
56+
57+
const client = new LLMClient('anthropic', {
58+
apiKey: '',
59+
baseUrl: '',
60+
});
61+
62+
const result = await client.chat.completions.create({
63+
messages: [{ role: 'user', content: 'Say this is a test' }],
64+
model: 'openai/gpt-4o',
65+
});
66+
```
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "@agent-infra/llm",
3+
"version": "0.1.0",
4+
"description": "A TypeScript SDK to call multiple LLM Prividers in OpenAI format.",
5+
"main": "dist/index.js",
6+
"module": "dist/index.mjs",
7+
"types": "dist/index.d.ts",
8+
"exports": {
9+
".": {
10+
"import": "./dist/index.mjs",
11+
"require": "./dist/index.js",
12+
"types": "./dist/index.d.ts"
13+
}
14+
},
15+
"files": [
16+
"dist"
17+
],
18+
"scripts": {
19+
"dev": "rslib build --watch",
20+
"build": "rslib build",
21+
"prepare": "npm run build",
22+
"prepublishOnly": "pnpm run build"
23+
},
24+
"dependencies": {
25+
"@anthropic-ai/sdk": "0.39.0",
26+
"@google/generative-ai": "0.24.0",
27+
"@google/genai": "0.8.0",
28+
"openai": "4.93.0"
29+
},
30+
"devDependencies": {
31+
"@types/node": "^20.10.4",
32+
"typescript": "^5.5.3"
33+
}
34+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright (c) 2025 Bytedance, Inc. and its affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
import { defineConfig } from '@rslib/core';
6+
7+
const BANNER = `/**
8+
* Copyright (c) 2025 Bytedance, Inc. and its affiliates.
9+
* SPDX-License-Identifier: Apache-2.0
10+
*/`;
11+
12+
export default defineConfig({
13+
source: {
14+
entry: {
15+
index: 'src/index.ts',
16+
},
17+
},
18+
lib: [
19+
{
20+
format: 'esm',
21+
syntax: 'es2021',
22+
bundle: true,
23+
dts: true,
24+
banner: { js: BANNER },
25+
},
26+
{
27+
format: 'cjs',
28+
syntax: 'es2021',
29+
bundle: true,
30+
dts: true,
31+
banner: { js: BANNER },
32+
},
33+
],
34+
output: {
35+
target: 'web',
36+
cleanDistPath: true,
37+
sourceMap: true,
38+
},
39+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright (c) 2025 Bytedance, Inc. and its affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import { LLMClientOptions, LLMProvider, ProviderType } from './types';
7+
import { createProvider } from './providers';
8+
9+
export class LLMClient implements LLMProvider {
10+
private provider: LLMProvider;
11+
12+
constructor(providerType: ProviderType, options: LLMClientOptions) {
13+
this.provider = createProvider(providerType, options);
14+
}
15+
16+
get chat() {
17+
return this.provider.chat;
18+
}
19+
20+
get vision() {
21+
return this.provider.vision;
22+
}
23+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Copyright (c) 2025 Bytedance, Inc. and its affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
export * from './client';
7+
export * from './types';
8+
export * from './providers';
9+
export * from './utils';

0 commit comments

Comments
 (0)