Skip to content

Commit

Permalink
feat: add EchoAITool and turbo build pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
transitive-bullshit committed Aug 4, 2024
1 parent 336c758 commit a7647b8
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 95 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ node_modules

# next.js
.next/
out/

# production
build/
Expand All @@ -29,6 +28,9 @@ yarn-error.log*
# local env files
.env*.local

# turbo
.turbo

# vercel
.vercel

Expand Down
1 change: 0 additions & 1 deletion examples/ai-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"build": "tsc",
"clean": "del dist",
"test": "run-s test:*",
"test:format": "prettier --check \"**/*.{js,ts,tsx}\"",
"test:lint": "eslint .",
"test:typecheck": "tsc --noEmit"
},
Expand Down
27 changes: 8 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,16 @@
},
"type": "module",
"scripts": {
"build": "run-s build:*",
"build:core": "cd packages/core && pnpm build",
"build:ai-sdk": "cd packages/ai-sdk && pnpm build",
"build:weather": "cd packages/weather && pnpm build",
"build:examples": "cd examples/ai-sdk && pnpm build",
"clean": "run-s clean:*",
"clean:core": "cd packages/core && pnpm clean",
"clean:ai-sdk": "cd packages/ai-sdk && pnpm clean",
"clean:weather": "cd packages/weather && pnpm clean",
"clean:examples": "cd examples/ai-sdk && pnpm clean",
"prebuild": "run-s clean",
"build": "turbo build",
"clean": "turbo clean",
"test": "turbo test",
"test:format": "prettier --check \"**/*.{js,ts,tsx}\"",
"test:lint": "turbo test:lint",
"test:typecheck": "turbo test:typecheck",
"test:unit": "turbo test:unit",
"precommit": "lint-staged",
"predev": "run-s clean",
"preinstall": "npx only-allow pnpm",
"pretest": "run-s build",
"prepare": "husky",
"test": "run-s test:*",
"test:format": "prettier --check \"**/*.{js,ts,tsx}\"",
"test:lint": "eslint .",
"test:typecheck": "tsc --noEmit",
"test:unit": "vitest run"
"prepare": "husky"
},
"devDependencies": {
"@fisch0920/eslint-config": "^1.4.0",
Expand Down
1 change: 0 additions & 1 deletion packages/ai-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"dev": "tsup --watch",
"clean": "del dist",
"test": "run-s test:*",
"test:format": "prettier --check \"**/*.{js,ts,tsx}\"",
"test:lint": "eslint .",
"test:typecheck": "tsc --noEmit",
"test:unit": "vitest run"
Expand Down
10 changes: 10 additions & 0 deletions packages/ai-sdk/src/ai-sdk.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { EchoAITool } from '@agentic/core'
import { describe, expect, test } from 'vitest'

import { createAISDKTools } from './ai-sdk'

describe('ai-sdk', () => {
test('createAISDKTools', () => {
expect(createAISDKTools(new EchoAITool())).toHaveProperty('echo')
})
})
3 changes: 1 addition & 2 deletions packages/calculator/src/calculator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { createAIFunction } from '@agentic/core'
import { evaluate } from 'mathjs'
import { z } from 'zod'

import { createAIFunction } from '../create-ai-function'

// TODO: ensure `expr` is sanitized to not run arbitrary code

export const CalculatorInputSchema = z.object({
Expand Down
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"dev": "tsup --watch",
"clean": "del dist",
"test": "run-s test:*",
"test:format": "prettier --check \"**/*.{js,ts,tsx}\"",
"test:lint": "eslint .",
"test:typecheck": "tsc --noEmit",
"test:unit": "vitest run"
Expand All @@ -52,7 +51,8 @@
"zod": "^3.23.3"
},
"devDependencies": {
"@agentic/tsconfig": "workspace:*"
"@agentic/tsconfig": "workspace:*",
"openai-fetch": "^2.0.4"
},
"publishConfig": {
"access": "public"
Expand Down
2 changes: 0 additions & 2 deletions packages/core/src/_utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dotenv/config'

import defaultKy, {
type AfterResponseHook,
type BeforeRequestHook,
Expand Down
44 changes: 25 additions & 19 deletions packages/core/src/ai-function-set.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,38 @@ import { expect, test } from 'vitest'
import { z } from 'zod'

import { AIFunctionSet } from './ai-function-set'
import { aiFunction, AIFunctionsProvider } from './fns'
import { calculator } from './tools/calculator'

class MockAITool extends AIFunctionsProvider {
@aiFunction({
name: 'echo',
description: 'Echoes the input.',
inputSchema: z.object({
query: z.string().describe('input query to echo')
})
})
async echo({ query }: { query: string }) {
return query
import { createAIFunction } from './create-ai-function'
import { EchoAITool } from './echo'

export const CalculatorInputSchema = z.object({
expr: z.string().describe('mathematical expression to evaluate')
})
export type CalculatorInput = z.infer<typeof CalculatorInputSchema>

const mockCalculator = createAIFunction(
{
name: 'calculator',
description:
'Computes the result of simple mathematical expressions. Handles basic arithmetic operations like addition, subtraction, multiplication, and division. Example expressions: "1 + 2", "3.4 * 5 / 9", "4 - 2"',
inputSchema: CalculatorInputSchema
},
async (input: CalculatorInput) => {
// eslint-disable-next-line no-eval, security/detect-eval-with-expression
const result: number = eval(input.expr)
return result
}
}
)

test('AIFunctionSet constructor', () => {
const mockAITool = new MockAITool()
const s0 = new AIFunctionSet([mockAITool, calculator])
const mockAITool = new EchoAITool()
const s0 = new AIFunctionSet([mockAITool, mockCalculator])

expect(s0.size).toEqual(2)
expect(s0.get('echo')).toBeDefined()
expect(s0.get('calculator')).toBeDefined()
expect([...s0].length).toEqual(2)

const s1 = new AIFunctionSet([s0, mockAITool, calculator, calculator])
const s1 = new AIFunctionSet([s0, mockAITool, mockCalculator, mockCalculator])
expect(s0.size).toEqual(2)
expect(s1.size).toEqual(2)
expect(s1.get('echo')).toBeDefined()
Expand All @@ -36,9 +42,9 @@ test('AIFunctionSet constructor', () => {
})

test('AIFunctionSet constructor invalid function', () => {
const mockAITool = new MockAITool()
const mockAITool = new EchoAITool()

expect(
() => new AIFunctionSet([mockAITool, calculator, { spec: {} } as any])
() => new AIFunctionSet([mockAITool, mockCalculator, { spec: {} } as any])
).toThrowError('Invalid AIFunctionLike: [object Object]')
})
30 changes: 30 additions & 0 deletions packages/core/src/echo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { z } from 'zod'

import { createAIFunction } from './create-ai-function'
import { aiFunction, AIFunctionsProvider } from './fns'

export class EchoAITool extends AIFunctionsProvider {
@aiFunction({
name: 'echo',
description: 'Echoes the input.',
inputSchema: z.object({
query: z.string().describe('input query to echo')
})
})
async echo({ query }: { query: string }) {
return query
}
}

export const echoAIFunction = createAIFunction(
{
name: 'echo',
description: 'Echoes the input.',
inputSchema: z.object({
query: z.string().describe('input query to echo')
})
},
({ query }: { query: string }) => {
return query
}
)
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './ai-function-set'
export * from './create-ai-chain'
export * from './create-ai-function'
export * from './echo'
export * from './errors'
export * from './extract-object'
export * from './fns'
Expand Down
1 change: 0 additions & 1 deletion packages/weather/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"dev": "tsup --watch",
"clean": "del dist",
"test": "run-s test:*",
"test:format": "prettier --check \"**/*.{js,ts,tsx}\"",
"test:lint": "eslint .",
"test:typecheck": "tsc --noEmit"
},
Expand Down
29 changes: 20 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 21 additions & 38 deletions turbo.json
Original file line number Diff line number Diff line change
@@ -1,54 +1,37 @@
{
"$schema": "https://turbo.build/schema.json",
"pipeline": {
"ui": "stream",
"tasks": {
"build": {
"dependsOn": ["^build"],
"env": [
"ANTHROPIC_API_KEY",
"AWS_REGION",
"AWS_ACCESS_KEY_ID",
"AWS_SECRET_ACCESS_KEY",
"COHERE_API_KEY",
"FIREWORKS_API_KEY",
"GOOGLE_API_KEY",
"GROQ_API_KEY",
"HUGGINGFACE_API_KEY",
"MISTRAL_API_KEY",
"OPENAI_API_KEY",
"OPENAI_API_BASE",
"PERPLEXITY_API_KEY",
"REPLICATE_API_KEY",
"NODE_ENV",
"ASSISTANT_ID",
"INKEEP_API_KEY",
"INKEEP_INTEGRATION_ID",
"VERCEL_URL"
],
"outputs": ["dist/**"]
},
"lint": {
"dependsOn": ["^lint"]
},
"type-check": {
"dependsOn": ["^build", "build"]
"clean": {
"dependsOn": ["^clean"],
"outputs": ["dist/**"]
},
"test": {
"dependsOn": ["^build", "build"]
"dependsOn": [
"build",
"test:format",
"test:lint",
"test:typecheck",
"test:unit"
]
},
"publint": {
"dependsOn": ["^build", "build"]
"test:lint": {
"dependsOn": ["^test:lint"]
},
"clean": {
"dependsOn": ["^clean"]
"test:typecheck": {
"dependsOn": ["^test:typecheck"]
},
"test:unit": {
"dependsOn": ["^test:unit"]
},
"test:format": {},
"dev": {
"cache": false,
"persistent": true
},
"prettier-check": {},
"integration-test": {
"dependsOn": ["^build", "build"]
}
},
"globalEnv": ["CI", "PORT"]
}
}

0 comments on commit a7647b8

Please sign in to comment.