Skip to content

Commit

Permalink
feat: generate-code and big models update
Browse files Browse the repository at this point in the history
BREAKING CHANGE: new services paths

chore: return typing

chore: chat-with-assistant

chore: lint

chore: update cloudapi

chore: build fix
  • Loading branch information
GermanVor committed Oct 10, 2024
1 parent b910781 commit 1571306
Show file tree
Hide file tree
Showing 394 changed files with 172,196 additions and 6,580 deletions.
2 changes: 1 addition & 1 deletion cloudapi
Submodule cloudapi updated 403 files
102 changes: 102 additions & 0 deletions examples/chat-with-assistant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { serviceClients, Session, cloudApi } from '@yandex-cloud/nodejs-sdk';
import path from 'path';
import dotenv from 'dotenv';

dotenv.config({ path: path.resolve(__dirname, '.env') });

const getEnv = (envName: string, defaultValue?: string): string => {
const envValue = process.env[envName] || defaultValue;

console.log(process.env);

if (!envValue) {
throw new Error(`Env variable ${envName} is not defined`);
}

return envValue;
};

const iamToken = getEnv('YC_IAM_TOKEN');
const folderId = getEnv('YC_FOLDER_ID');

(async () => {
const session = new Session({ iamToken });

const assistantApi = cloudApi.ai.assistants_v1_assistant_service;
const messageApi = cloudApi.ai.assistants_v1_threads_message_service;
const threadApi = cloudApi.ai.assistants_v1_threads_thread_service;
const runApi = cloudApi.ai.assistants_v1_runs_run_service;

// const assistantClient = session.client(assistantApi.AssistantServiceClient);
const assistantClient = session.client(
serviceClients.AssistantServiceClient,
);

// const messageClient = session.client(messageApi.MessageServiceClient);
const messageClient = session.client(
serviceClients.AssistantMessageServiceClient,
);

// const threadClient = session.client(threadApi.ThreadServiceClient);
const threadClient = session.client(
serviceClients.AssistantThreadServiceClient,
);

// const runClient = session.client(runApi.RunServiceClient);
const runClient = session.client(serviceClients.AssistantRunServiceClient);

const thread = await threadClient.create(
threadApi.CreateThreadRequest.fromPartial({
name: 'qwerty',
folderId,
}),
);

console.log({ thread });

const assistant = await assistantClient.create(
assistantApi.CreateAssistantRequest.fromPartial({
name: 'qwerty',
folderId,
modelUri: `gpt://${folderId}/yandexgpt/latest`,
}),
);

console.log({ assistant });

const assistantId = assistant.id;
const threadId = thread.id;

const message = await messageClient.create(
messageApi.CreateMessageRequest.fromPartial({
threadId,
content: {
content: [{ text: { content: 'qwerty' } }],
},
}),
);

console.log({ message });

const run = await runClient.create(
runApi.CreateRunRequest.fromPartial({
threadId,
assistantId,
}),
);

console.log({ run });

const asyncIterableForStreamEvent = runClient.listen(
runApi.ListenRunRequest.fromPartial({ runId: run.id }),
);

let lastStreamEvent: cloudApi.ai.assistants_v1_runs_run_service.StreamEvent | null = null;

for await (const streamEvent of asyncIterableForStreamEvent) {
lastStreamEvent = streamEvent;
}

console.dir({ lastStreamEvent });
console.dir(lastStreamEvent.completedMessage.content.content);
})();
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@
"typescript": "^4.5.4"
},
"scripts": {
"test": "cross-env NODE_OPTIONS=\"--max-old-space-size=4096\" jest -c config/jest.ts --passWithNoTests '.*\\.test\\.ts$'",
"lint": "eslint src config",
"build": "cross-env NODE_OPTIONS=\"--max-old-space-size=4096\" tsc -p .",
"test": "jest -c config/jest.ts --passWithNoTests '.*\\.test\\.ts$'",
"lint": "eslint src/ --quiet --fix",
"build": "tsc -p .",
"generate-code": "ts-node scripts/generate-code.ts",
"check-endpoints": "ts-node scripts/check-endpoints.ts",
"prepare": "husky install",
Expand Down
96 changes: 70 additions & 26 deletions scripts/generate-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ fs.mkdirSync(GENERATED_CODE_DIR);

const protoFiles = fg.sync('**/*.proto', { cwd: YA_PROTO_DIR, absolute: true });

logger.debug(`Found ${protoFiles.length} proto files in ${YA_PROTO_DIR} directory`);
logger.debug(
`Found ${protoFiles.length} proto files in ${YA_PROTO_DIR} directory`,
);

const commandArgs = [
'npx --no-install grpc_tools_node_protoc',
Expand All @@ -34,7 +36,11 @@ logger.debug(`Code generation command: \n ${command}`);

cp.execSync(command);

const projectsDirs = fg.sync('*', { cwd: GENERATED_PROJECTS_DIR, onlyDirectories: true, absolute: true });
const projectsDirs = fg.sync('*', {
cwd: GENERATED_PROJECTS_DIR,
onlyDirectories: true,
absolute: true,
});

logger.debug(`Found ${projectsDirs.length} project directories`, projectsDirs);

Expand Down Expand Up @@ -72,19 +78,14 @@ for (const projectDir of projectsDirs) {
};

const exportStatements = projectModules.map((modulePath) => {
const relativePath = path.relative(projectDir, modulePath);
const relativePath = path
.relative(projectDir, modulePath)
.replace('.ts', '');

const relativePathSegments = relativePath.split(path.sep);
const firstPathSegment = relativePathSegments[0];
const moduleName = path.basename(modulePath).replace('.ts', '');
const serviceName = moduleName.replace('_service', '');
// Do not use 'vX' as prefixes
const usePathSegmentAsPrefix = relativePathSegments.length > 1
&& firstPathSegment.length > 2
&& firstPathSegment !== serviceName;
const moduleAlias = [
usePathSegmentAsPrefix ? firstPathSegment : undefined,
moduleName,
].filter(Boolean).join('_');
const moduleName = path.basename(modulePath);

const moduleAlias = relativePathSegments.join('_');

const { ext } = path.parse(modulePath);
const moduleWithoutExt = relativePath.replace(ext, '');
Expand All @@ -101,7 +102,10 @@ for (const projectDir of projectsDirs) {

const indexModuleContent = exportStatements.join('\n');

logger.debug(`Writing export statements to ${indexModulePath} module`, indexModuleContent);
logger.debug(
`Writing export statements to ${indexModulePath} module`,
indexModuleContent,
);

fs.writeFileSync(indexModulePath, indexModuleContent, 'utf8');
}
Expand All @@ -110,34 +114,74 @@ for (const projectDir of projectsDirs) {
logger.debug('Generating root index module');

const rootIndexModulePath = path.join(GENERATED_PROJECTS_DIR, 'index.ts');
const serviceClientsModulePath = path.join(GENERATED_PROJECTS_DIR, 'service_clients.ts');
const serviceClientsModulePath = path.join(
GENERATED_PROJECTS_DIR,
'service_clients.ts',
);
const rootModuleContentParts: string[] = [];
const serviceClientsModuleContentParts: string[] = [
'import * as cloudApi from \'.\'',
"import * as cloudApi from '.'",
];

const serviceClientsExportsSet = new Set<string>();

for (const [indexModulePath, projectMeta] of Object.entries(projectsMeta)) {
logger.debug(`Processing ${indexModulePath} module`);
const relativePath = path.relative(GENERATED_PROJECTS_DIR, indexModulePath).replace('index.ts', '');
const relativePath = path
.relative(GENERATED_PROJECTS_DIR, indexModulePath)
.replace('index.ts', '');

rootModuleContentParts.push(`export * as ${projectMeta.name} from './${relativePath}'`);
rootModuleContentParts.push(
`export * as ${projectMeta.name} from './${relativePath}'`,
);

for (const serviceMeta of projectMeta.services) {
const serviceConfig = servicesConfig[projectMeta.name]?.[serviceMeta.exportAlias];

if (serviceConfig) {
serviceClientsModuleContentParts.push(
// eslint-disable-next-line max-len
`export const ${serviceConfig.exportClassName || serviceConfig.importClassName} = cloudApi.${projectMeta.name}.${serviceMeta.exportAlias}.${serviceConfig.importClassName};`,
);
delete servicesConfig[projectMeta.name]?.[serviceMeta.exportAlias];

const exportStr = `export const ${
serviceConfig.exportClassName || serviceConfig.importClassName
} = cloudApi.${projectMeta.name}.${serviceMeta.exportAlias}.${
serviceConfig.importClassName
};`;

if (serviceClientsExportsSet.has(exportStr)) continue;

serviceClientsModuleContentParts.push(exportStr);
serviceClientsExportsSet.add(exportStr);
} else {
logger.warn(`There is no configuration for service ${serviceMeta.exportAlias} in project ${projectMeta.name}`);
logger.warn(
`There is no configuration for service ${serviceMeta.exportAlias} in project ${projectMeta.name}`,
);
}
}
}

logger.debug(`Writing result to ${rootIndexModulePath} module`);
logger.debug(`Writing result to ${serviceClientsModulePath} module`);

fs.writeFileSync(rootIndexModulePath, rootModuleContentParts.join('\n'), 'utf8');
fs.writeFileSync(serviceClientsModulePath, serviceClientsModuleContentParts.join('\n'), 'utf8');
for (const serviceName of Object.keys(servicesConfig)) {
const obj = servicesConfig[serviceName];
const keys = Object.keys(obj);

if (keys.length > 0) {
logger.warn(
`There are unused config keys for service ${serviceName}: ${keys.join(
', ',
)}`,
);
}
}

fs.writeFileSync(
rootIndexModulePath,
rootModuleContentParts.join('\n'),
'utf8',
);
fs.writeFileSync(
serviceClientsModulePath,
serviceClientsModuleContentParts.join('\n'),
'utf8',
);
Loading

0 comments on commit 1571306

Please sign in to comment.