Skip to content
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

FEAT: get parameter is_output and is_array from chatgpt #176

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions .github/actions/fetch_ai_json/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Fetch AI JSON
inputs:
url:
description: 'URL of the native headers'
type: 'string'
required: true
product_type:
description: 'product type, available options: rtc, rtm'
type: 'string'
required: true
GH_TOKEN:
description: 'GitHub token'
type: 'string'
required: true

runs:
using: composite
steps:
# Setup .npmrc file to publish to GitHub Packages
- uses: actions/setup-node@v3
with:
node-version: '18.x'

- name: Reconfigure git to use HTTP authentication
run: >
git config --global url."https://${{ inputs.GH_TOKEN }}@github.com/".insteadOf ssh://[email protected]/
shell: bash

- name: Fetch AI JSON from URL
run: |
yarn
mkdir -p ai/temp
curl -o ai/temp/differences.json ${{ inputs.url }}
yarn ts-node ai/index.ts ai/temp/differences.json configs/${{ inputs.product_type }}/ai/parameter_list.ts
shell: bash

- name: Create pull request
uses: AgoraIO-Extensions/actions/.github/actions/pr@main
with:
github-token: ${{ inputs.GH_TOKEN }}
target-repo: ${{ github.workspace }}
target-branch: ${{ github.ref_name }}
target-branch-name-surffix: fetch-ai-json
pull-request-title: |
[AUTO] Fetch AI JSON with ${{ inputs.url }}
add-paths: configs/*
30 changes: 4 additions & 26 deletions .github/workflows/fetch_ai_json.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,8 @@ jobs:
- uses: actions/checkout@v3
with:
persist-credentials: false
# Setup .npmrc file to publish to GitHub Packages
- uses: actions/setup-node@v3
- uses: ./.github/actions/fetch_ai_json
with:
node-version: '18.x'

- name: Reconfigure git to use HTTP authentication
run: >
git config --global url."https://${{ secrets.GH_TOKEN }}@github.com/".insteadOf ssh://[email protected]/

- name: Fetch AI JSON from URL
run: |
yarn
ts-node ai/index.ts ${{ inputs.url }} configs/${{ inputs.product_type }}/ai/parameter_list.ts

- name: Create pull request
uses: AgoraIO-Extensions/actions/.github/actions/pr@main
with:
github-token: ${{ secrets.GH_TOKEN }}
target-repo: ${{ github.workspace }}
target-branch: ${{ github.ref_name }}
target-branch-name-surffix: fetch-ai-json
pull-request-title: |
[AUTO] Fetch AI JSON
pull-request-body: |
AI JSON source:
${{ inputs.url }}
add-paths: configs/*
url: ${{ inputs.url }}
product_type: ${{ inputs.product_type }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5 changes: 1 addition & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ jobs:
# Setup .npmrc file to publish to GitHub Packages
- uses: actions/setup-node@v3
with:
node-version: "18.x"
registry-url: "https://npm.pkg.github.com"
# Defaults to the user or organization that owns the workflow file
scope: "@agoraio-extensions"
node-version: '18.x'

- name: Reconfigure git to use HTTP authentication
run: >
Expand Down
12 changes: 12 additions & 0 deletions .github/workflows/update_headers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ on:
description: 'URL of the native headers'
type: 'string'
required: true
ai_json_url:
description: 'URL of the AI JSON'
type: 'string'
required: true
version:
description: 'native headers version'
type: 'string'
Expand All @@ -31,6 +35,14 @@ jobs:
bash scripts/update_headers.sh ${{ inputs.url }} ${{ inputs.version }} ${{ inputs.product_type }}
shell: bash

- name: Fetch AI JSON if ai_json_url is provided
if: ${{ inputs.ai_json_url != '' }}
uses: ./.github/actions/fetch_ai_json
with:
url: ${{ inputs.ai_json_url }}
product_type: ${{ inputs.product_type }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Create pull request
uses: AgoraIO-Extensions/actions/.github/actions/pr@main
with:
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.ccls-cache/
node_modules/
dist/
package-lock.json
package-lock.json
ai/temp/
1 change: 0 additions & 1 deletion .npmrc

This file was deleted.

Binary file modified .yarn/install-state.gz
Binary file not shown.
94 changes: 94 additions & 0 deletions ai/doc_ai_tool_processor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { exec } from 'child_process';
import * as fs from 'fs';

interface DocParameter {
name: string;
type: string;
change_type: string;
old_value: string;
new_value: string;
is_output: boolean;
is_array: boolean;
}

interface DocChanges {
diff: string[];
parent_class: string;
language: string;
details: {
api_name: string;
api_signature: string;
change_type: string;
parameters: DocParameter[];
};
}

export interface AIParameter {
parent_name: string;
parent_class: string;
is_output: boolean;
is_array: boolean;
}

interface AIParameterObject {
[key: string]: AIParameter;
}

export interface DocAIToolJson {
changes: {
api_changes: DocChanges[];
struct_changes: DocChanges[];
enum_changes: DocChanges[];
};
}

export class DocAIToolJsonProcessor {
private data: DocAIToolJson[] | undefined;

constructor(filepath: string) {
this.readJsonFromFile(filepath);
}

private readJsonFromFile(filepath: string): void {
try {
const jsonData = fs.readFileSync(filepath, 'utf-8');
this.data = JSON.parse(jsonData);
} catch (error) {
console.error('Error reading JSON file:', error);
}
}

generateConfigFromDocAPIChanges(): AIParameterObject {
let output: AIParameterObject = {};
if (!this.data) {
console.error('call readJsonFromFile() first.');
return output;
}
this.data.map((docAIToolJson: DocAIToolJson) => {
docAIToolJson.changes.api_changes.map((item: DocChanges) => {
item.details.parameters.map((param: DocParameter) => {
let key = `${item.parent_class}:${item.details.api_name}.${param.name}@type`;
output[key] = {
parent_class: item.parent_class,
parent_name: item.details.api_name,
is_output: param.is_output,
is_array: param.is_array,
};
});
});
});
return output;
}

saveConfigToFile(outputPath: string): void {
const config = this.generateConfigFromDocAPIChanges();
const configString = `module.exports = ${JSON.stringify(config, null, 2)};`;
try {
fs.writeFileSync(outputPath, configString, 'utf-8');
exec(`yarn eslint --fix ${outputPath}`);
console.log(`Configuration saved to ${outputPath}`);
} catch (error) {
console.error('Error writing configuration to file:', error);
}
}
}
13 changes: 13 additions & 0 deletions ai/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { DocAIToolJsonProcessor } from './doc_ai_tool_processor';

const args = process.argv.slice(2);

if (args.length !== 2) {
console.error('Usage: node script.js <input_filepath> <output_filepath>');
process.exit(1);
}

const [inputFilePath, outputFilePath] = args;

const docAIToolJsonProcessor = new DocAIToolJsonProcessor(inputFilePath);
docAIToolJsonProcessor.saveConfigToFile(outputFilePath);
1 change: 1 addition & 0 deletions configs/rtc/ai/parameter_list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@agoraio-extensions/terra-core": "[email protected]:AgoraIO-Extensions/terra.git#head=main&workspace=terra-core",
"lodash": "^4.17.21",
"mustache": "^4.2.0",
"openai": "^4.29.1"
"openai": "^4.77.0"
},
"devDependencies": {
"@types/jest": "^29.5.1",
Expand All @@ -40,6 +40,7 @@
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-jest": "^26.9.0",
"eslint-plugin-prettier": "^4.0.0",
"https-proxy-agent": "^7.0.6",
"jest": "^29.5.0",
"prettier": "^2.0.5",
"ts-jest": "^29.1.0",
Expand Down
4 changes: 3 additions & 1 deletion src/__tests__/parsers/cud_node_parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ import {
MemberFunction,
Struct,
} from '@agoraio-extensions/cxx-parser';

import { ParseResult, TerraContext } from '@agoraio-extensions/terra-core';

import CUDNodeParser, {
CUDNodeParserArgs,
isNodeMatched,
} from '../../parsers/cud_node_parser';
import { ParseResult, TerraContext } from '@agoraio-extensions/terra-core';

describe('CUDNodeParser', () => {
it('isNodeMatched', () => {
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/parsers/override_node_parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '@agoraio-extensions/cxx-parser';

import { TerraContext } from '@agoraio-extensions/terra-core';

import {
OverrideNodeParser,
getOverrideNodeParserUserData,
Expand Down
59 changes: 59 additions & 0 deletions src/generators/custom_headers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Diff } from '../utils/diff';
import { askGPT } from '../utils/gpt_utils';

const prompt = `
You are a C++ Code Inspector. Your task is to rename within some given C++ methods.
You should reply shortly and no need to explain the code.
You should provide all the methods in the same reply.
The first method is no need to change, but the left methods need to be renamed.
Given method:
\`\`\`c++
{{ METHOD_SOURCE }}
\`\`\`
`;

let methodSource = `
virtual int joinChannel(const char* token, const char* channelId, const char* info, uid_t uid) = 0;
virtual int joinChannel(const char* token, const char* channelId, uid_t uid, const ChannelMediaOptions& options) = 0;
`;

const prompt2 = `
You are a file diff tool. Your task is to compare two versions of a C++ code.
I will provide you a diff result of two versions of a C++ code that is from bash command \`diff -r -u -N\`.
Given diff result:
{{ DIFF_SOURCE }}


Now, you need to provide a summary of the changes between the two versions.
`;

const old_version = 'rtc_4.4.0';
const new_version = 'rtc_4.5.0';

const old_version_path = `headers/${old_version}/include`;
const new_version_path = `headers/${new_version}/include`;
const blackList = ['include/rte_base', 'include/internal'];

const diffTool = new Diff(old_version_path, new_version_path, blackList);
diffTool.setOutputDirectory(`temp/${old_version}↔${new_version}`);
diffTool.run();

let promptWithMethod = prompt
.replace('{{ METHOD_SOURCE }}', methodSource)
.trim();

// let promptWithDiff = prompt
// .replace(
// '{{ DIFF_SOURCE }}',
// fs.readFileSync(
// `temp/${old_version}↔${new_version}/AgoraBase.h.diff`,
// 'utf8'
// )
// )
// .trim();

// (async () => {
// // let res = await askGPT(promptWithMethod);
// let res = await askGPT(promptWithDiff);
// console.log(res);
// })();
1 change: 1 addition & 0 deletions src/generators/prompt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const systemPrompt = `You are a C++ Code Inspector. Your task is to rename within some given C++ methods.`;
1 change: 1 addition & 0 deletions src/parsers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ export type BaseParserArgs = {
configFilePath?: string;
defaultConfig?: any;
ignoreDefaultConfig?: boolean;
useAI?: boolean;
};
Loading
Loading