@sdk-it/typescript 0.34.0 → 0.35.0

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/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export * from './lib/generate.js';
2
- export * from './lib/typescript-snippet.js';
3
2
  export * from './lib/generator.js';
4
3
  export * from './lib/sdk.js';
4
+ export * from './lib/typescript-snippet.js';
5
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,oBAAoB,CAAC;AACnC,cAAc,cAAc,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,cAAc,CAAC;AAC7B,cAAc,6BAA6B,CAAC"}
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ import { template as template2 } from "lodash-es";
3
3
  import { readdir } from "node:fs/promises";
4
4
  import { join as join2 } from "node:path";
5
5
  import { npmRunPathEnv } from "npm-run-path";
6
- import { camelcase as camelcase5, spinalcase as spinalcase4 } from "stringcase";
6
+ import { camelcase as camelcase6, spinalcase as spinalcase4 } from "stringcase";
7
7
  import { methods, pascalcase as pascalcase5, toLitObject as toLitObject2 } from "@sdk-it/core";
8
8
  import {
9
9
  createWriterProxy,
@@ -19,35 +19,107 @@ import {
19
19
  } from "@sdk-it/spec";
20
20
 
21
21
  // packages/typescript/src/lib/agent/ai-sdk.ts
22
- import { camelcase, spinalcase } from "stringcase";
22
+ import { camelcase } from "stringcase";
23
23
  import {
24
24
  forEachOperation
25
25
  } from "@sdk-it/spec";
26
- function generateAISDKTools(spec) {
26
+ function generateAISDKTools(ir) {
27
27
  const groups = {};
28
- forEachOperation(spec, (entry, operation) => {
29
- groups[entry.tag] ??= [];
30
- groups[entry.tag].push(createTool(entry, operation));
28
+ forEachOperation(ir, (entry, operation) => {
29
+ const tagDef = ir.tags.find((tag) => tag.name === entry.tag);
30
+ if (!tagDef) {
31
+ console.warn(`No tag details found for tag: ${entry.tag}`);
32
+ return;
33
+ }
34
+ groups[entry.tag] ??= {
35
+ tools: [],
36
+ instructions: "",
37
+ displayName: "",
38
+ name: ""
39
+ };
40
+ groups[entry.tag].tools.push(createTool(entry, operation));
41
+ groups[entry.tag].instructions = tagDef["x-instructions"];
42
+ groups[entry.tag].name = tagDef.name;
43
+ groups[entry.tag].displayName = tagDef["x-name"];
31
44
  });
32
45
  const imports = [
33
46
  `import { z } from 'zod';`,
34
47
  `import { tool } from 'ai';`,
35
48
  `import * as schemas from './inputs/index.ts';`
36
49
  ];
37
- const tools = Object.entries(groups).map(([group, tools2]) => {
38
- return `export const ${spinalcase(group)} = (context: { client: any }) => ({ ${tools2.join(", ")} });`;
39
- });
40
- return [...imports, ...tools].join("\n\n");
50
+ const agent = Object.entries(groups).map(
51
+ ([group, { instructions, tools, displayName }]) => {
52
+ return `export const ${camelcase(group)} = {
53
+ name: '${displayName}',
54
+ instructions: \`${instructions}\`,
55
+ tools: { ${tools.join(", ")} }
56
+ }`;
57
+ }
58
+ );
59
+ const handoffs = `export const triage = {
60
+ name: 'Triage Agent',
61
+ tools:{${Object.entries(groups).map(([, { name }]) => {
62
+ return createTransferTool(name);
63
+ })}}}`;
64
+ return [...imports, ...agent, handoffs].join("\n\n");
41
65
  }
42
66
  function createTool(entry, operation) {
43
67
  const schemaName = camelcase(`${operation.operationId} schema`);
44
68
  return `'${operation["x-fn-name"]}': tool({
45
69
  description: \`${operation.description || operation.summary}\`,
46
- type: 'function',
47
70
  inputSchema: schemas.${schemaName},
48
- execute: async (input) => {
71
+ execute: async (input, options) => {
49
72
  console.log('Executing ${operation.operationId} tool with input:', input);
73
+ const context = coerceContext(options.experimental_context);
50
74
  const response = await context.client.request(
75
+ '${entry.method.toUpperCase()} ${entry.path}' ,
76
+ input,
77
+ );
78
+ return JSON.stringify(response);
79
+ },
80
+ })`;
81
+ }
82
+ function createTransferTool(agentName) {
83
+ return `transfer_to_${agentName}: tool({
84
+ type: 'function',
85
+ description: 'Transfer the conversation to the ${agentName}.',
86
+ inputSchema: z.object({}),
87
+ execute: async () => ({ agent: '${agentName}' }),
88
+ })`;
89
+ }
90
+
91
+ // packages/typescript/src/lib/agent/openai-agents.ts
92
+ import { camelcase as camelcase2, spinalcase } from "stringcase";
93
+ import {
94
+ forEachOperation as forEachOperation2
95
+ } from "@sdk-it/spec";
96
+ function generateOpenAIAgentTools(spec) {
97
+ const groups = {};
98
+ forEachOperation2(spec, (entry, operation) => {
99
+ groups[entry.tag] ??= [];
100
+ groups[entry.tag].push(createTool2(entry, operation));
101
+ });
102
+ const imports = [
103
+ `import { z } from 'zod';`,
104
+ `import { tool } from '@openai/agents';`,
105
+ `import * as schemas from './inputs/index.ts';`
106
+ ];
107
+ const tools = Object.entries(groups).map(([group, tools2]) => {
108
+ return `export const ${spinalcase(group)} = [${tools2.join(", ")}];`;
109
+ });
110
+ return [...imports, ...tools].join("\n\n");
111
+ }
112
+ function createTool2(entry, operation) {
113
+ const schemaName = camelcase2(`${operation.operationId} schema`);
114
+ return `tool({
115
+ description: \`${operation.description || operation.summary}\`,
116
+ name: '${operation["x-fn-name"]}',
117
+ parameters: makeOptionalPropsNullable(schemas.${schemaName}),
118
+ execute: async (input, maybeContext) => {
119
+ console.log('Executing ${operation.operationId} tool with input:', input);
120
+ const context = coerceContext(maybeContext?.context);
121
+ const client = context.client;
122
+ const response = await client.request(
51
123
  '${entry.method.toUpperCase()} ${entry.path}' ,
52
124
  input as any,
53
125
  );
@@ -628,14 +700,14 @@ function appendOptional2(type, isRequired) {
628
700
  // packages/typescript/src/lib/generator.ts
629
701
  import { merge, template } from "lodash-es";
630
702
  import { join } from "node:path";
631
- import { camelcase as camelcase3, spinalcase as spinalcase2 } from "stringcase";
703
+ import { camelcase as camelcase4, spinalcase as spinalcase2 } from "stringcase";
632
704
  import { followRef as followRef3, isEmpty as isEmpty2, isRef as isRef3, resolveRef } from "@sdk-it/core";
633
705
  import {
634
- forEachOperation as forEachOperation2
706
+ forEachOperation as forEachOperation3
635
707
  } from "@sdk-it/spec";
636
708
 
637
709
  // packages/typescript/src/lib/sdk.ts
638
- import { camelcase as camelcase2 } from "stringcase";
710
+ import { camelcase as camelcase3 } from "stringcase";
639
711
  import { isEmpty, pascalcase as pascalcase3 } from "@sdk-it/core";
640
712
  import {
641
713
  isStreamingContentType,
@@ -675,8 +747,8 @@ var status_map_default = {
675
747
 
676
748
  // packages/typescript/src/lib/sdk.ts
677
749
  function toEndpoint(groupName, spec, specOperation, operation, utils) {
678
- const schemaName = camelcase2(`${specOperation.operationId} schema`);
679
- const schemaRef = `${camelcase2(groupName)}.${schemaName}`;
750
+ const schemaName = camelcase3(`${specOperation.operationId} schema`);
751
+ const schemaRef = `${camelcase3(groupName)}.${schemaName}`;
680
752
  const schemas = [];
681
753
  specOperation.responses ??= {};
682
754
  const outputs = Object.keys(specOperation.responses).flatMap(
@@ -942,7 +1014,7 @@ function generateCode(config) {
942
1014
  });
943
1015
  const groups = {};
944
1016
  const endpoints = {};
945
- forEachOperation2(config.spec, (entry, operation) => {
1017
+ forEachOperation3(config.spec, (entry, operation) => {
946
1018
  console.log(`Processing ${entry.method} ${entry.path}`);
947
1019
  groups[entry.tag] ??= [];
948
1020
  endpoints[entry.tag] ??= [];
@@ -990,8 +1062,8 @@ function generateCode(config) {
990
1062
  });
991
1063
  });
992
1064
  const allSchemas = Object.keys(endpoints).map((it) => ({
993
- import: `import ${camelcase3(it)} from './${config.makeImport(spinalcase2(it))}';`,
994
- use: ` ...${camelcase3(it)}`
1065
+ import: `import ${camelcase4(it)} from './${config.makeImport(spinalcase2(it))}';`,
1066
+ use: ` ...${camelcase4(it)}`
995
1067
  }));
996
1068
  return {
997
1069
  groups,
@@ -1027,7 +1099,7 @@ ${allSchemas.map((it) => it.use).join(",\n")}
1027
1099
  `import * as outputs from '${config.makeImport("../outputs/index")}';`,
1028
1100
  `import { toRequest, json, urlencoded, empty, formdata, type HeadersInit } from '${config.makeImport("../http/request")}';`,
1029
1101
  `import { chunked, buffered } from "${config.makeImport("../http/parse-response")}";`,
1030
- `import * as ${camelcase3(name)} from '../inputs/${config.makeImport(spinalcase2(name))}';`,
1102
+ `import * as ${camelcase4(name)} from '../inputs/${config.makeImport(spinalcase2(name))}';`,
1031
1103
  `import { createBaseUrlInterceptor, createHeadersInterceptor, type Interceptor } from '${config.makeImport("../http/interceptors")}';`,
1032
1104
  `import { Dispatcher, fetchType, type InstanceType } from '${config.makeImport("../http/dispatcher")}';`,
1033
1105
  `import { Pagination, OffsetPagination, CursorPagination } from "${config.makeImport("../pagination/index")}";`
@@ -1612,7 +1684,7 @@ var page_pagination_default = "type InferPage<T> = T extends Page<infer U> ? U :
1612
1684
 
1613
1685
  // packages/typescript/src/lib/readme/readme.ts
1614
1686
  import { isEmpty as isEmpty3 } from "@sdk-it/core";
1615
- import { forEachOperation as forEachOperation3 } from "@sdk-it/spec";
1687
+ import { forEachOperation as forEachOperation4 } from "@sdk-it/spec";
1616
1688
 
1617
1689
  // packages/typescript/src/lib/readme/prop.emitter.ts
1618
1690
  import { followRef as followRef4, isRef as isRef4 } from "@sdk-it/core";
@@ -1923,7 +1995,7 @@ function toReadme(spec, generator) {
1923
1995
  markdown.push("");
1924
1996
  markdown.push("## API Reference");
1925
1997
  markdown.push("");
1926
- forEachOperation3(spec, (entry, operation) => {
1998
+ forEachOperation4(spec, (entry, operation) => {
1927
1999
  const { method, path } = entry;
1928
2000
  markdown.push(
1929
2001
  `### ${operation["x-fn-name"]} | ${`_${method.toUpperCase()} ${path}_`}`
@@ -1996,11 +2068,11 @@ ${l}`));
1996
2068
  }
1997
2069
 
1998
2070
  // packages/typescript/src/lib/typescript-snippet.ts
1999
- import { camelcase as camelcase4, spinalcase as spinalcase3 } from "stringcase";
2071
+ import { camelcase as camelcase5, spinalcase as spinalcase3 } from "stringcase";
2000
2072
  import { isEmpty as isEmpty4, pascalcase as pascalcase4, resolveRef as resolveRef3 } from "@sdk-it/core";
2001
2073
  import "@sdk-it/readme";
2002
2074
  import {
2003
- forEachOperation as forEachOperation4,
2075
+ forEachOperation as forEachOperation5,
2004
2076
  patchParameters,
2005
2077
  securityToOptions
2006
2078
  } from "@sdk-it/spec";
@@ -2021,7 +2093,7 @@ var SnippetEmitter = class {
2021
2093
  for (const [propName, propSchema] of Object.entries(properties)) {
2022
2094
  const isRequired = (schemaObj.required ?? []).includes(propName);
2023
2095
  const resolvedProp = resolveRef2(this.spec, propSchema);
2024
- if (isRequired || resolvedProp.example !== void 0 || resolvedProp.default !== void 0 || Math.random() > 0.5) {
2096
+ if (isRequired || resolvedProp.example !== void 0 || resolvedProp.default !== void 0) {
2025
2097
  result[propName] = this.handle(propSchema);
2026
2098
  }
2027
2099
  }
@@ -2323,7 +2395,7 @@ var TypeScriptSnippet = class {
2323
2395
  };
2324
2396
  }
2325
2397
  #toRequest(entry, payload) {
2326
- return `await ${camelcase4(this.#clientName)}.request('${entry.method.toUpperCase()} ${entry.path}', ${payload});`;
2398
+ return `await ${camelcase5(this.#clientName)}.request('${entry.method.toUpperCase()} ${entry.path}', ${payload});`;
2327
2399
  }
2328
2400
  snippet(entry, operation, config = {}) {
2329
2401
  const payload = this.succinct(entry, operation, config);
@@ -2364,7 +2436,7 @@ ${client.use}`;
2364
2436
  #constructClient(options = {}) {
2365
2437
  return {
2366
2438
  import: `import { ${this.#clientName} } from '${this.#packageName}';`,
2367
- use: `const ${camelcase4(this.#clientName)} = new ${this.#clientName}({
2439
+ use: `const ${camelcase5(this.#clientName)} = new ${this.#clientName}({
2368
2440
  ${Object.entries(
2369
2441
  options
2370
2442
  ).map(([key, value]) => `${key}: ${JSON.stringify(value)}`).join(",\n ")}
@@ -2899,7 +2971,7 @@ function availablePaginationTypes(spec) {
2899
2971
  let offset = false;
2900
2972
  let page = false;
2901
2973
  let cursor = false;
2902
- forEachOperation4(spec, (entry, operation) => {
2974
+ forEachOperation5(spec, (entry, operation) => {
2903
2975
  if (operation["x-pagination"]) {
2904
2976
  switch (operation["x-pagination"].type) {
2905
2977
  case "offset":
@@ -3090,12 +3162,19 @@ ${template2(dispatcher_default, {})({ throwError: !style.errorAsValue, outputTyp
3090
3162
  "models/index.ts": modelsIndex
3091
3163
  // ...(modelsImports.length ? { 'models/index.ts': modelsIndex } : {}),
3092
3164
  });
3093
- if (settings.agentTools) {
3094
- await settings.writer(output, {
3095
- "agents.ts": `${generateAISDKTools(spec)}
3165
+ switch (settings.agentTools) {
3166
+ case "openai-agents":
3167
+ await settings.writer(output, {
3168
+ "agents.ts": `${generateOpenAIAgentTools(spec)}
3096
3169
  ${utils_default}`
3097
- // 'agents.ts': `${generateOpenAIAgentTools(spec)}\n${utilsTxt}`,
3098
- });
3170
+ });
3171
+ break;
3172
+ case "ai-sdk":
3173
+ await settings.writer(output, {
3174
+ "agents.ts": `${generateAISDKTools(spec)}
3175
+ ${utils_default}`
3176
+ });
3177
+ break;
3099
3178
  }
3100
3179
  await settings.writer(output, {
3101
3180
  "index.ts": await getFolderExports(
@@ -3220,7 +3299,7 @@ function toInputs(operationsSet, commonZod, makeImport) {
3220
3299
  const output = [];
3221
3300
  const imports = /* @__PURE__ */ new Set(['import { z } from "zod";']);
3222
3301
  for (const operation of operations) {
3223
- const schemaName = camelcase5(`${operation.operationId} schema`);
3302
+ const schemaName = camelcase6(`${operation.operationId} schema`);
3224
3303
  const schema = `export const ${schemaName} = ${Object.keys(operation.schemas).length === 1 ? Object.values(operation.schemas)[0] : toLitObject2(operation.schemas)};`;
3225
3304
  for (const it of commonImports) {
3226
3305
  if (schema.includes(it)) {